Created
March 8, 2025 01:29
-
-
Save mdippery/8de60769cf2385aa424ef274c9fd0aee to your computer and use it in GitHub Desktop.
Get screen resolutions using low-level CoreGraphics primitives
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 <stdio.h> | |
#include <CoreGraphics/CoreGraphics.h> | |
// clang -framework CoreGraphics -o screen-size-cg -arch x86_64 -arch arm64 screen-size.c | |
static void ShowDisplay(CGDirectDisplayID id) | |
{ | |
CGRect frame = CGDisplayBounds(id); | |
CGSize resolution = frame.size; | |
printf("%ldx%ld\n", (long) resolution.width, (long) resolution.height); | |
} | |
static void ShowAllDisplays(void) | |
{ | |
uint32_t maxDisplays = 16; /* There can't be more than 16 displays, right? */ | |
uint32_t actualDisplays = 0; | |
CGDirectDisplayID displayIDs[maxDisplays]; | |
int error = CGGetActiveDisplayList(maxDisplays, displayIDs, &actualDisplays); | |
if (error != kCGErrorSuccess) { | |
fprintf(stderr, "Error: %d\n", error); | |
return; | |
} | |
for (int i = 0; i < actualDisplays; i++) { | |
printf("%2d ", i); | |
ShowDisplay(displayIDs[i]); | |
} | |
} | |
static void ShowMainDisplay(void) | |
{ | |
ShowDisplay(CGMainDisplayID()); | |
} | |
int main(int argc, char **argv) | |
{ | |
int ec = 0; | |
ShowMainDisplay(); | |
printf("\n"); | |
ShowAllDisplays(); | |
quit: | |
return ec; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment