CGDisplayRotation Hangs.

I'm am planning to use CoreGraphics for its low-level functionality, so I wrote up a small snippet that I expected to work:

#include <CoreGraphics/CoreGraphics.h>

int main() {
  double rot = CGDisplayRotation(0);
  printf("Rotation %f\n", rot);
  return 0;
}

Unfortunately, it seems that the call CGDisplayRotation blocks. When I tried writing a similar snippet in XCode though, it works just fine but I will not be able to use XCode for unrelated reasons. Am I compiling incorrectly? Could this be a permission issue?

I compiled with clang -Wall -framework CoreGraphics core.c -o core.o

Replies

When I tried your code in Xcode, it hung in the CGDisplayRotation(0) call. But when I changed that to CGDisplayRotation( CGMainDisplayID() ), it ran fine. What are you trying to accomplish by passing 0 as the display ID? Granted, the documentation says that it should return 0 if you pass an invalid display ID, but why tempt fate?

Yes interesting. It does run when replaced with a call to CGDisplayRotation.

Re - CGDisplayRotation. I only used it as an example. Actually I'm trying to port this package's code https://github.com/BoboTiG/python-mss/blob/main/src/mss/darwin.py to Julia but ran into issues similar blocking behavior with CGDisplayBounds. I transferred to C directly only to find out I was suffering from the same issue. One question (partly from a lack of C knowledge) - isn't CGDirectDisplayID equivalent to uint32_t? I tried passing in 1 directly and it stalls...

Interestingly enough, passing any uint32_t (0 or 1) to CGDisplayRotation is fine so long as CGMainDisplayID is called before. A bit bizarre, but why is that?

The fact that CGDirectDisplayID is the same type as uint32_t does not mean that every uint32_t value is a legitimate display ID. If you want to find out something about displays, you should first get the display IDs of interest, with calls such as CGGetActiveDisplayList, CGGetOnlineDisplayList, or CGGetDisplaysWithPoint.

By the way, since CGDisplayRotation is not behaving as documented, I filed a bug report, FB13202169.