aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 096f25b5360497bc9c74e6c746d7fdaae2d3e50a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <ApplicationServices/ApplicationServices.h>

int main(int argc, char *argv[]) {
  double inactivity_threshold = 60.0;
  int right = 0;

  if (argc > 1) {
    if (strcmp(argv[1], "--help") == 0) {
      printf("Usage: osx-jiggler [inactivity_threshold]\n");
      printf("  inactivity_threshold: Time in seconds before the mouse moves "
             "(default: 60.0)\n");
      printf("  --help: Show this help message\n");
      return 0;
    }

    inactivity_threshold = atof(argv[1]);
    if (inactivity_threshold <= 0) {
      fprintf(stderr,
              "Invalid inactivity time. Please provide a positive number.\n");
      return 1;
    }
  }

  while (1) {
    double idle_time = CGEventSourceSecondsSinceLastEventType(
        kCGEventSourceStateHIDSystemState, kCGAnyInputEventType);

    if (idle_time >= inactivity_threshold) {
      CGEventRef event = CGEventCreate(NULL);
      CGPoint cursor = CGEventGetLocation(event);
      CFRelease(event);

      (right) ? (cursor.x += 1) : (cursor.x -= 1);
      right = !right;

      event = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, cursor,
                                      kCGMouseButtonLeft);
      CGEventPost(kCGHIDEventTap, event);
      CFRelease(event);
    }
    sleep(1);
  }

  return 0;
}