Last active
June 29, 2022 02:34
-
-
Save pachuco/85694d16b3a0c0cf0b9b3df752e4401a to your computer and use it in GitHub Desktop.
Utility to switch displays in winXP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include <stdio.h> | |
#include <stdint.h> | |
#define DEVNAME_LEN 32 | |
#define A2I(x) ((uint8_t)(x-48)) | |
static DEVMODEA dmActive; | |
static DEVMODEA dmWanted; | |
static char nameActive[DEVNAME_LEN]; | |
static char nameWanted[DEVNAME_LEN]; | |
BOOL changeDisplay(int wantedNr) { | |
#define CHNGDISPTEST(...) if (ChangeDisplaySettingsExA(__VA_ARGS__) != DISP_CHANGE_SUCCESSFUL) return FALSE; | |
int devCnt = 0; | |
static DEVMODEA dm; | |
static DISPLAY_DEVICEA dd; | |
memset(&dm, 0, sizeof(DEVMODEA)); | |
memset(&dd, 0, sizeof(DISPLAY_DEVICEA)); | |
dd.cb = sizeof(DISPLAY_DEVICEA); | |
dm.dmSize = sizeof(DEVMODEA); | |
while (EnumDisplayDevicesA(NULL, devCnt, &dd, 0)) { | |
if (EnumDisplaySettingsA(dd.DeviceName, ENUM_REGISTRY_SETTINGS, &dm)) { | |
if (dm.dmPelsHeight > 0 && dm.dmPelsWidth > 0) { | |
if (dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) { | |
if (devCnt == wantedNr) return TRUE; | |
memcpy(&dmActive, &dm, sizeof(DEVMODEA)); | |
memcpy(nameActive, dd.DeviceName, DEVNAME_LEN); | |
} | |
if (devCnt == wantedNr) { | |
memcpy(&dmWanted, &dm, sizeof(DEVMODEA)); | |
memcpy(nameWanted, dd.DeviceName, DEVNAME_LEN); | |
} | |
} | |
//printf("%s\n", dd.DeviceName); | |
} | |
devCnt++; | |
} | |
if (devCnt < wantedNr) return FALSE; | |
dmActive.dmPosition.x = dmWanted.dmPosition.x = 0; | |
dmActive.dmPosition.y = dmWanted.dmPosition.y = 0; | |
dmActive.dmPelsHeight = dmWanted.dmPelsHeight = 0; | |
dmActive.dmPelsWidth = dmWanted.dmPelsWidth = 0; | |
dmWanted.dmFields = DM_PELSHEIGHT|DM_PELSWIDTH|DM_POSITION; | |
CHNGDISPTEST(nameWanted, &dmWanted, NULL, CDS_RESET|CDS_UPDATEREGISTRY, NULL); | |
dmWanted.dmFields = DM_POSITION; | |
CHNGDISPTEST(nameWanted, &dmWanted, NULL, CDS_RESET|CDS_UPDATEREGISTRY|CDS_SET_PRIMARY, NULL); | |
dmActive.dmFields = DM_PELSHEIGHT|DM_PELSWIDTH|DM_POSITION; | |
CHNGDISPTEST(nameActive, &dmActive, NULL, CDS_RESET|CDS_UPDATEREGISTRY, NULL); | |
return TRUE; | |
#undef CHNGDISPTEST | |
} | |
int main(int argc, char *argv[]) { | |
uint8_t wantedNr; | |
if (argc < 2 ) { | |
printf("Utility to switch between multiple displays in single-monitor mode.\n"); | |
printf("For windows 2000 and XP.\n\n"); | |
printf("Usage: displayswitch.exe NUMBER\n"); | |
return 1; | |
} | |
if ((wantedNr = A2I(argv[1][0])-1) > 9) return 1; | |
return changeDisplay(wantedNr) ? 0 : 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment