Guest User

Untitled

a guest
Sep 18th, 2013
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. /* gcc mouse.c -framework CoreFoundation -framework ApplicationServices */
  2.  
  3. #define IS_MACOSX
  4. #include <CoreFoundation/CoreFoundation.h>
  5. #include <ApplicationServices/ApplicationServices.h>
  6.  
  7. typedef struct {
  8.   float x;
  9.   float y;
  10. } MMPoint;
  11.  
  12. MMPoint MMPointFromCGPoint(CGPoint point) {
  13.   MMPoint dummy = { point.x, point.y };
  14.   return dummy;
  15. }
  16.  
  17. MMPoint getMousePos()
  18. {
  19. #if defined(IS_MACOSX)
  20.     CGEventRef event = CGEventCreate(NULL);
  21.     CGPoint point = CGEventGetLocation(event);
  22.     CFRelease(event);
  23.  
  24.     return MMPointFromCGPoint(point);
  25. #elif defined(USE_X11)
  26.     int x, y; /* This is all we care about. Seriously. */
  27.     Window garb1, garb2; /* Why you can't specify NULL as a parameter */
  28.     int garb_x, garb_y;  /* is beyond me. */
  29.     unsigned int more_garbage;
  30.  
  31.     Display *display = XGetMainDisplay();
  32.     XQueryPointer(display, XDefaultRootWindow(display), &garb1, &garb2,
  33.               &x, &y, &garb_x, &garb_y, &more_garbage);
  34.  
  35.     return MMPointMake(x, y);
  36. #elif defined(IS_WINDOWS)
  37.     POINT point;
  38.     GetCursorPos(&point);
  39.  
  40.     return MMPointFromPOINT(point);
  41. #endif
  42. }
  43.  
  44. int main() {
  45.   MMPoint pt = getMousePos();
  46.   printf("%f %f\n", pt.x, pt.y);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment