I need to access data about GPU and screen resolution in C, not using system_profiler, because it takes too long(.3s-.5s system_profiler SPDisplaysDataType), and needs greping and cutting, which is not that fast either.

Caching would be answer, but very short-sighted as someone can use different monitors etc.

Unfortunately system_profiler is closed source.

EDIT: I used CGDisplayPixelsWide and CGDisplayPixelsHigh to get resolution. During compilation i had to compile with -framework Cocoa -framework IOKit. Only minus that it gets system resolution rather than screen resolution. So function looks like this.

static char *get_resolution()
        int screen_width = CGDisplayPixelsWide(CGMainDisplayID());
        int screen_height = CGDisplayPixelsHigh(CGMainDisplayID());
  • This might need an edit to be on topic here. Code level questions would get migrated to Stack Overflow, but interactive scripts (python with objective C bindings specifically) and grep plus system calls can also be powerful and performant. We don’t have enough detail to go on for more than the two helpful answers provided already.
    – bmike
    Apr 19 at 16:37

displayplacer allows to read and write display settings and is open source. I'm not sure about GPU details though.

The API used seems to be defined in IOKit/graphics/IOGraphicsLib.h.


I'm not certain of specific APIs, but looking at the source code of an open source application that works with display resolutions or GPUs may be beneficial. Two examples I can think of are RDM and gSwitch (RDM hasn't been updated for a while but there are up-to-date forks available).

Not the answer you're looking for? Browse other questions tagged .