示例#1
0
__declspec(dllexport) bool SetCurrentTopology(DISPLAYCONFIG_TOPOLOGY_ID *pDesiredTopology) {
	  
	//printf("*** DisplayHelperLib: Applying Disply State: 0x%.4x  ***\n",*pDesiredTopology);   ### Debugging
	LONG result = SetDisplayConfig(0, NULL,0, NULL,(SDC_APPLY | *pDesiredTopology));
	if (result != ERROR_SUCCESS)
		  return false;
	  return true;
}
int main(int argc, char *argv[]){
    UINT32 num_of_paths = 0;
    UINT32 num_of_modes = 0;
    DISPLAYCONFIG_PATH_INFO* displayPaths = NULL; 
    DISPLAYCONFIG_MODE_INFO* displayModes = NULL;
    
    UINT32 num_of_paths2 = 0;
    UINT32 num_of_modes2 = 0;
    DISPLAYCONFIG_PATH_INFO* displayPaths2 = NULL; 
    DISPLAYCONFIG_MODE_INFO* displayModes2 = NULL;

    enum ActionType action;
    char* filename;
    int debug = 0;
    BOOL show_usage = TRUE;
    
    // -save argument given
    if (argc == 3 && strcmp(argv[1], "-save") == 0) {
        action = SAVE;
        filename = argv[2];
        show_usage = FALSE;
        
    // -equal argument given
    } else if (argc == 3 && strcmp(argv[1], "-equal") == 0) {
        action = EQUAL;
        filename = argv[2];
        show_usage = FALSE;
        
    // No arguments given, assumed open
    } else if (argc == 2) {
        action = OPEN;
        filename = argv[1];
        show_usage = FALSE;
    }

    // Show usage if invalid options or none
    if (show_usage) {
        puts(
            "Restore Monitors 0.2\n"
            "\n"
            "Usage: restoremonitors7.exe [<-save>] <filename>\n"
            "\n"
            "  Capable of restoring monitors to saved state under Windows 7.\n"
            "  Uses Windows 7 CCD API to save, and restore the settings from\n"
            "  file.\n"
            "\n"
            "  By giving only filename the program tries to open and restore\n"
            "  the saved settings in the file.\n"
            "\n"
            "   -save\n"
            "       Used to save settings to file.\n"
            "\n"
            "   -equal\n"
            "       Prints '1' if current settings equals the one in the file,\n"
            "       otherwise '0' or '  ERROR:...'.\n"
            "\n"
            "\n  Author:     Jari Pennanen (2010) <*****@*****.**>"
            "\n  License:    FreeBSD License, see COPYING"
            "\n  Repository: http://github.com/Ciantic/monitortoggler"
        );
        
        return 0;
    }
    
    switch (action) {
        case EQUAL:
            // Settings in file:
            if (!openSettingsFromFile(filename, &num_of_paths, &num_of_modes, &displayPaths, &displayModes))
                return 0;
            
            // Current settings in computer
            getCurrentSettings(&num_of_paths2, &num_of_modes2, &displayPaths2, &displayModes2);
            
            // Compare settings of file and computer
            if (num_of_paths == num_of_paths2 && num_of_modes == num_of_modes2) {
                if (memcmp(displayPaths, displayPaths2, num_of_paths*sizeof(DISPLAYCONFIG_PATH_INFO)) != 0) {
                    puts("0");
                    return 0;
                }
                if (memcmp(displayModes, displayModes2, num_of_modes*sizeof(DISPLAYCONFIG_MODE_INFO)) != 0) {
                    puts("0");
                    return 0;
                }
                puts("1");
                return 0;
            }
            puts("0");
            return 0;

        
        case OPEN:
            printf("Opening settings from '%s' file...\n", filename);
            if (!openSettingsFromFile(filename, &num_of_paths, &num_of_modes, &displayPaths, &displayModes))
                return 0;
            
            if (debug) {
                printf("num of paths %d\n", num_of_paths);
                printf("num of modes %d\n", num_of_modes);
                printDisplayPaths(num_of_paths, displayPaths);
                printDisplayModeInfos(num_of_modes, displayModes);
            }
            
            puts("Validating settings...");
            if (!Result_DCGDI(SetDisplayConfig(num_of_paths, displayPaths, num_of_modes, displayModes, SDC_VALIDATE | SDC_USE_SUPPLIED_DISPLAY_CONFIG)))
                return 0;
            puts("Restoring settings...");
            Result_DCGDI(SetDisplayConfig(num_of_paths, displayPaths, num_of_modes, displayModes, SDC_APPLY | SDC_USE_SUPPLIED_DISPLAY_CONFIG));
            break;
            
            
        case SAVE:
            puts("Querying current settings...");
            getCurrentSettings(&num_of_paths, &num_of_modes, &displayPaths, &displayModes);
            
            if (debug) {
                printf("num of paths %d\n", num_of_paths);
                printf("num of modes %d\n", num_of_modes);
                printDisplayPaths(num_of_paths, displayPaths);
                printDisplayModeInfos(num_of_modes, displayModes);
            }
            
            printf("Saving settings to '%s'...\n", filename);
            if (!saveSettingsToFile(filename, num_of_paths, num_of_modes, &displayPaths, &displayModes))
                return 0;
            break;
    }
    free(displayPaths);
    free(displayModes);
    puts("Done.");
    return 0;
}