Skip to content

Instantly share code, notes, and snippets.

@pplanel
Last active June 13, 2019 21:59
Show Gist options
  • Save pplanel/bfd5283079a8272c4b8544824175312a to your computer and use it in GitHub Desktop.
Save pplanel/bfd5283079a8272c4b8544824175312a to your computer and use it in GitHub Desktop.
segfault on XSelectInput
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
#define NANOSECONDS_MULTIPLIER 1000000
void run(Display *display, Window *window, XEvent *event, int *screen);
const long interval = 500 * NANOSECONDS_MULTIPLIER;
int frame = 0;
typedef struct Engine {
Display *display;
Window window;
XEvent event;
int screen;
} Engine;
int main(int argc, char** argv, char** env) {
// Initialization
Engine *engine = calloc(1, sizeof(Engine));
engine->display = XOpenDisplay(NULL);
if (engine->display == NULL) {
fprintf(stderr, "Cannot open display\n");
exit(1);
}
engine->screen = XDefaultScreen(engine->display);
engine->window = XRootWindow(engine->display, 0);
XSelectInput(engine->display, engine->window, KeyReleaseMask | KeyPressMask);
run(engine->display, (Window *) engine->window, &engine->event, &engine->screen);
XCloseDisplay(engine->display);
free(engine);
return 0;
}
void run(Display *display, Window *window, XEvent *event, int *screen) {
while(event->type != KeyPress) {
double x = 1000 * cos(frame);
double y = 1000 * sin(frame);
XWarpPointer(display, None, *window, 0, 0, 0, 0, x, y);
if (frame > XDisplayWidth(display, *screen))
frame = 0;
frame++;
fprintf(stdout, "Running! -- frame: %d\n", frame);
nanosleep((const struct timespec[]){{0, interval}}, NULL);
XFlush(display);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment