5.2.3. Evaluating the start-up arguments

The first part of main() evaluates the passed arguments:

  1. Entering resolution on the command line specifies a new resolution to replace the default resolution. See Example 5.3:

    Example 5.3. Setting the resolution

    int main(int argc, char* argv[]) {
        try {
            // Run from the directory containing the executable, 
            // so that we can find the data wherever it is run from
            cdToExecutable();
    
            // parse commandline args
    
            //bool configStandalone = false;
            int configApplets = 0;
            bool configAppletsAdded = false;
            themes configTheme = THEME_BLUE;
            MDE::Array<input_descriptor> configInputs;
            MDE::vec2i configResolution(800, 480);
            unsigned int configDepth = 0; // Use platform default config depth.
    
            for (int i = 1; i < argc; i++) {
                if (strcmp(argv[i], "-resolution") == 0) {
                    if (i + 1 == argc) {
                        argErrorExit("No argument specified for -resolution switch",
                                   NULL);
                    } else if (sscanf(argv[i + 1],
                               "%dx%d", &configResolution.x,
                                &configResolution.y) != 2) {
                          argErrorExit("Invalid -resolution: %s\n", argv[i + 1]);
                    }
    
                    i++;
                } else if (strcmp(argv[i], "-depth") == 0) {
    ...
    

  2. Entering depth on the command line specifies the bits per pixel. See Example 5.4:

    Example 5.4. Setting the depth

    ...
                } else if (strcmp(argv[i], "-depth") == 0) {
                    if (i + 1 == argc) {
                        argErrorExit("No argument specified for -depth switch",
                                NULL);
                    } else if (sscanf(argv[i + 1], "%d", &configDepth) != 1) {
                        argErrorExit("Invalid -depth: %s\n", argv[i + 1]);
                    }
                    i++;
                } else if (strcmp(argv[i], "-theme") == 0) {
    ...
    

  3. Entering theme on the command line specifies which theme to use. See Example 5.5:

    Example 5.5. Setting the theme

    ...
                } else if (strcmp(argv[i], "-theme") == 0) {
                    if (i + 1 == argc) {
                        argErrorExit("No argument specified for -theme switch", NULL);
                    } else if (strcmp(argv[i + 1], "blue") == 0) {
                        configTheme = THEME_BLUE;
                    } else if (strcmp(argv[i + 1], "robot") == 0) {
                        configTheme = THEME_ROBOT;
                    } else if (strcmp(argv[i + 1], "minimal") == 0) {
                        configTheme = THEME_MINIMAL;
                    } else {
                        argErrorExit(
                                "Unknown argument specified for -theme switch: %s",
                                argv[i + 1]);
                    }
    
                    i++;
                } else if (strcmp(argv[i], "-applets") == 0) {
    ...
    

  4. Entering applets on the command line specifies which applets to load. See Example 5.6:

    Example 5.6. Setting the applets

    ...
                } else if (strcmp(argv[i], "-applets") == 0) {
                    while (i + 1 < argc) {
                        if (argv[i + 1][0] == '-') {
                            break;
                        } else if (strcmp(argv[i + 1], "all") == 0) {
                            configApplets |= APPLETS_ALL;
                        } else if (strcmp(argv[i + 1], "default") == 0) {
                            configApplets |= APPLETS_DEFAULT;
                        } else if (strcmp(argv[i + 1], "lightshow") == 0) {
                            configApplets |= APPLET_LIGHTSHOW;
                        } else if (strcmp(argv[i + 1], "planet") == 0) {
                            configApplets |= APPLET_PLANET;
                        } else if (strcmp(argv[i + 1], "buzzword") == 0) {
                            configApplets |= APPLET_BUZZWORD;
                        } else if (strcmp(argv[i + 1], "photo") == 0) {
                            configApplets |= APPLET_PHOTO_BROWSER;
                        } else if (strcmp(argv[i + 1], "presentation") == 0) {
                            configApplets |= APPLET_PRESENTATION;
                        } else if (strcmp(argv[i + 1], "slideshow") == 0) {
                            configApplets |= APPLET_SLIDESHOW_PRESENTATION;
                        } else if (strcmp(argv[i + 1], "browser") == 0) {
                            configApplets |= APPLET_BROWSER;
                        } else {
                            argErrorExit("Unknown applet: %s\n", argv[i + 1]);
                        }
    
                        configAppletsAdded = true;
                        i++;
                    }
                    if (!configAppletsAdded) {
                        argErrorExit("No arguments specified for -applets switch\n",
                                   NULL);
                    }
                } else if (strcmp(argv[i], "-input") == 0) {
    ...
    

  5. The input command sets the mouse, touchscreen, and keyboard options. See Example 5.7:

    Example 5.7. Setting the input devices

    ...
                } else if (strcmp(argv[i], "-input") == 0) {
                    input_descriptor d;
    
                    if (argc < i + 2 || argv[i + 1][0] == '-') {
                        argErrorExit("Too few arguments specified for -input\n", NULL);
                    }
    
                    if (strcmp(argv[i + 1], "mouse") == 0) {
                        d.mode = INPUT_MODE_MOUSE;
                    } else if (strcmp(argv[i + 1], "touch") == 0) {
                        d.mode = INPUT_MODE_TOUCH;
                    } else {
                        argErrorExit("Unknown argument specified for -input: %s\n",
                                   argv[i + 1]);
                    }
                    i++;
                    if (argc > i + 1 && argv[i + 1][0] != '-') {
                        d.arg = argv[i + 1];
                        i++;
                    } else {
                        d.arg = "";
                    }
                    configInputs.add(d);
                } else if (strcmp(argv[i], "-help") == 0) {
    ...
    

  6. If help is entered on the command line, the command options are listed. See Example 5.8:

    Example 5.8. Displaying command-line options

    ...
    
                } else if (strcmp(argv[i], "-help") == 0) {
                    printHelp();
    
    #ifdef MDE_OS_WIN32
                    // Pause so that we don't lose the message on exit.
                    printf("Press RETURN to exit ...");
                    getchar();
    #endif
                    return 0;
    
                } else {
                    argErrorExit("Warning: Ignoring unknown argument: %s\n", argv[i]);
                }
            }
    ...
    

Copyright © 2010 ARM. All rights reserved.ARM DUI 0527A‑02a
Non-Confidential - Draft - BetaID070710