Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include <X11/Xlib.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <time.h>
  7. #include <math.h>
  8.  
  9. #define NANOSECONDS_MULTIPLIER 1000000
  10.  
  11. void run(Display *display, Window *window, XEvent *event, int *screen);
  12.  
  13. const long interval = 500 * NANOSECONDS_MULTIPLIER;
  14. int frame = 0;
  15.  
  16. typedef struct Engine {
  17. Display *display;
  18. Window window;
  19. XEvent event;
  20. int screen;
  21. } Engine;
  22.  
  23. int main(int argc, char** argv, char** env) {
  24.  
  25. // Initialization
  26. Engine *engine = calloc(1, sizeof(Engine));
  27. engine->display = XOpenDisplay(NULL);
  28. if (engine->display == NULL) {
  29. fprintf(stderr, "Cannot open display\n");
  30. exit(1);
  31. }
  32. engine->screen = XDefaultScreen(engine->display);
  33. engine->window = XRootWindow(engine->display, 0);
  34.  
  35. XSelectInput(engine->display, engine->window, KeyReleaseMask | KeyPressMask);
  36.  
  37. run(engine->display, (Window *) engine->window, &engine->event, &engine->screen);
  38.  
  39. XCloseDisplay(engine->display);
  40. free(engine);
  41. return 0;
  42. }
  43.  
  44. void run(Display *display, Window *window, XEvent *event, int *screen) {
  45. while(event->type != KeyPress) {
  46. double x = 1000 * cos(frame);
  47. double y = 1000 * sin(frame);
  48. XWarpPointer(display, None, *window, 0, 0, 0, 0, x, y);
  49.  
  50. if (frame > XDisplayWidth(display, *screen))
  51. frame = 0;
  52.  
  53. frame++;
  54. fprintf(stdout, "Running! -- frame: %d\n", frame);
  55.  
  56. nanosleep((const struct timespec[]){{0, interval}}, NULL);
  57.  
  58. XFlush(display);
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement