2 * (C) GraÅžvydas "notaz" Ignotas, 2009-2012
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
8 * See the COPYING file in the top-level directory.
17 #include <X11/Xutil.h>
18 #include <X11/XKBlib.h>
20 #include <sys/types.h>
24 #include <sys/ioctl.h>
32 #define FPTR(f) typeof(f) * p##f
33 #define FPTR_LINK(xf, dl, f) { \
34 xf.p##f = dlsym(dl, #f); \
35 if (xf.p##f == NULL) { \
36 fprintf(stderr, "missing symbol: %s\n", #f); \
44 FPTR(XCreateBitmapFromData);
45 FPTR(XCreatePixmapCursor);
50 FPTR(XCreateSimpleWindow);
51 FPTR(XChangeWindowAttributes);
55 FPTR(XCheckTypedEvent);
56 FPTR(XWithdrawWindow);
60 FPTR(XkbSetDetectableAutoRepeat);
61 FPTR(XkbKeycodeToKeysym);
64 FPTR(XMoveResizeWindow);
73 static struct xstuff g_xstuff;
75 static Cursor transparent_cursor(struct xstuff *xf, Display *display, Window win)
82 memset(&dummy, 0, sizeof(dummy));
83 pix = xf->pXCreateBitmapFromData(display, win, &d, 1, 1);
84 cursor = xf->pXCreatePixmapCursor(display, pix, pix,
85 &dummy, &dummy, 0, 0);
86 xf->pXFreePixmap(display, pix);
90 static int x11h_init(int *xenv_flags, const char *window_title)
92 unsigned int display_width, display_height;
94 XSetWindowAttributes attributes;
101 memset(&g_xstuff, 0, sizeof(g_xstuff));
102 x11lib = dlopen("libX11.so.6", RTLD_LAZY);
103 if (x11lib == NULL) {
104 fprintf(stderr, "libX11.so load failed:\n%s\n", dlerror());
107 FPTR_LINK(g_xstuff, x11lib, XCreateBitmapFromData);
108 FPTR_LINK(g_xstuff, x11lib, XCreatePixmapCursor);
109 FPTR_LINK(g_xstuff, x11lib, XFreePixmap);
110 FPTR_LINK(g_xstuff, x11lib, XOpenDisplay);
111 FPTR_LINK(g_xstuff, x11lib, XDisplayName);
112 FPTR_LINK(g_xstuff, x11lib, XCloseDisplay);
113 FPTR_LINK(g_xstuff, x11lib, XCreateSimpleWindow);
114 FPTR_LINK(g_xstuff, x11lib, XChangeWindowAttributes);
115 FPTR_LINK(g_xstuff, x11lib, XSelectInput);
116 FPTR_LINK(g_xstuff, x11lib, XMapWindow);
117 FPTR_LINK(g_xstuff, x11lib, XNextEvent);
118 FPTR_LINK(g_xstuff, x11lib, XCheckTypedEvent);
119 FPTR_LINK(g_xstuff, x11lib, XWithdrawWindow);
120 FPTR_LINK(g_xstuff, x11lib, XGrabKeyboard);
121 FPTR_LINK(g_xstuff, x11lib, XPending);
122 FPTR_LINK(g_xstuff, x11lib, XLookupKeysym);
123 FPTR_LINK(g_xstuff, x11lib, XkbSetDetectableAutoRepeat);
124 FPTR_LINK(g_xstuff, x11lib, XkbKeycodeToKeysym);
125 FPTR_LINK(g_xstuff, x11lib, XStoreName);
126 FPTR_LINK(g_xstuff, x11lib, XIconifyWindow);
127 FPTR_LINK(g_xstuff, x11lib, XMoveResizeWindow);
128 FPTR_LINK(g_xstuff, x11lib, XInternAtom);
129 FPTR_LINK(g_xstuff, x11lib, XAllocWMHints);
130 FPTR_LINK(g_xstuff, x11lib, XGetWMHints);
131 FPTR_LINK(g_xstuff, x11lib, XSetWMHints);
132 FPTR_LINK(g_xstuff, x11lib, XSync);
133 FPTR_LINK(g_xstuff, x11lib, XFree);
137 g_xstuff.display = display = g_xstuff.pXOpenDisplay(NULL);
140 fprintf(stderr, "cannot connect to X server %s, X handling disabled.\n",
141 g_xstuff.pXDisplayName(NULL));
145 visual = DefaultVisual(display, 0);
146 if (visual->class != TrueColor)
147 fprintf(stderr, PFX "warning: non true color visual\n");
149 printf(PFX "X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display),
150 VendorRelease(display), DisplayString(display), ProtocolVersion(display),
151 ProtocolRevision(display));
153 screen = DefaultScreen(display);
155 display_width = DisplayWidth(display, screen);
156 display_height = DisplayHeight(display, screen);
157 printf(PFX "display is %dx%d\n", display_width, display_height);
159 g_xstuff.window = win = g_xstuff.pXCreateSimpleWindow(display,
160 RootWindow(display, screen), 0, 0, display_width, display_height,
161 0, BlackPixel(display, screen), BlackPixel(display, screen));
163 attributes.override_redirect = True;
164 attributes.cursor = transparent_cursor(&g_xstuff, display, win);
165 g_xstuff.pXChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
167 if (window_title != NULL)
168 g_xstuff.pXStoreName(display, win, window_title);
169 evt_mask = ExposureMask | FocusChangeMask | PropertyChangeMask;
170 if (xenv_flags && (*xenv_flags & XENV_CAP_KEYS))
171 evt_mask |= KeyPressMask | KeyReleaseMask;
172 if (xenv_flags && (*xenv_flags & XENV_CAP_MOUSE))
173 evt_mask |= ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
174 g_xstuff.pXSelectInput(display, win, evt_mask);
175 g_xstuff.pXMapWindow(display, win);
176 g_xstuff.pXGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
177 g_xstuff.pXkbSetDetectableAutoRepeat(display, 1, NULL);
178 // XSetIOErrorHandler
180 // we don't know when event dispatch will be called, so sync now
181 g_xstuff.pXSync(display, False);
187 g_xstuff.display = NULL;
188 fprintf(stderr, "x11 handling disabled.\n");
192 static void x11h_update(int (*key_cb)(void *cb_arg, int kc, int is_pressed),
193 int (*mouseb_cb)(void *cb_arg, int x, int y, int button, int is_pressed),
194 int (*mousem_cb)(void *cb_arg, int x, int y),
200 while (g_xstuff.pXPending(g_xstuff.display))
202 g_xstuff.pXNextEvent(g_xstuff.display, &evt);
206 while (g_xstuff.pXCheckTypedEvent(g_xstuff.display, Expose, &evt))
211 keysym = g_xstuff.pXLookupKeysym(&evt.xkey, 0);
213 key_cb(cb_arg, keysym, 1);
217 keysym = g_xstuff.pXLookupKeysym(&evt.xkey, 0);
219 key_cb(cb_arg, keysym, 0);
223 if (mouseb_cb != NULL)
224 mouseb_cb(cb_arg, evt.xbutton.x, evt.xbutton.y,
225 evt.xbutton.button, 1);
229 if (mouseb_cb != NULL)
230 mouseb_cb(cb_arg, evt.xbutton.x, evt.xbutton.y,
231 evt.xbutton.button, 0);
235 if (mousem_cb != NULL)
236 mousem_cb(cb_arg, evt.xmotion.x, evt.xmotion.y);
242 static void x11h_wait_vmstate(void)
244 Atom wm_state = g_xstuff.pXInternAtom(g_xstuff.display, "WM_STATE", False);
250 for (i = 0; i < 20; i++) {
251 while (g_xstuff.pXPending(g_xstuff.display)) {
252 g_xstuff.pXNextEvent(g_xstuff.display, &evt);
253 // printf("w event %d\n", evt.type);
254 if (evt.type == PropertyNotify && evt.xproperty.atom == wm_state)
260 fprintf(stderr, PFX "timeout waiting for wm_state change\n");
263 static int x11h_minimize(void)
265 XSetWindowAttributes attributes;
266 Display *display = g_xstuff.display;
267 Window window = g_xstuff.window;
268 int screen = DefaultScreen(g_xstuff.display);
269 int display_width, display_height;
273 g_xstuff.pXWithdrawWindow(display, window, screen);
275 attributes.override_redirect = False;
276 g_xstuff.pXChangeWindowAttributes(display, window,
277 CWOverrideRedirect, &attributes);
279 wm_hints = g_xstuff.pXGetWMHints(display, window);
280 if (wm_hints == NULL) {
281 wm_hints = XAllocWMHints();
284 wm_hints->flags |= StateHint;
285 wm_hints->initial_state = IconicState;
286 g_xstuff.pXSetWMHints(display, window, wm_hints);
287 g_xstuff.pXFree(wm_hints);
290 g_xstuff.pXMapWindow(display, window);
292 while (g_xstuff.pXNextEvent(display, &evt) == 0)
294 // printf("m event %d\n", evt.type);
305 g_xstuff.pXWithdrawWindow(display, window, screen);
307 // must wait for some magic vmstate property change before setting override_redirect
310 attributes.override_redirect = True;
311 g_xstuff.pXChangeWindowAttributes(display, window,
312 CWOverrideRedirect, &attributes);
314 // fixup window after resize on override_redirect loss
315 display_width = DisplayWidth(display, screen);
316 display_height = DisplayHeight(display, screen);
317 g_xstuff.pXMoveResizeWindow(display, window, 0, 0, display_width, display_height);
319 g_xstuff.pXMapWindow(display, window);
320 g_xstuff.pXGrabKeyboard(display, window, False, GrabModeAsync, GrabModeAsync, CurrentTime);
321 g_xstuff.pXkbSetDetectableAutoRepeat(display, 1, NULL);
323 // we don't know when event dispatch will be called, so sync now
324 g_xstuff.pXSync(display, False);
329 static struct termios g_kbd_termios_saved;
330 static int g_kbdfd = -1;
332 static int tty_init(void)
334 struct termios kbd_termios;
337 g_kbdfd = open("/dev/tty", O_RDWR);
339 perror(PFX "open /dev/tty");
343 if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
344 perror(PFX "(not hiding FB): KDGETMODE");
348 if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
349 perror(PFX "tcgetattr");
353 g_kbd_termios_saved = kbd_termios;
354 kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
355 kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
356 kbd_termios.c_cc[VMIN] = 0;
357 kbd_termios.c_cc[VTIME] = 0;
359 if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
360 perror(PFX "tcsetattr");
364 if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
365 perror(PFX "KDSETMODE KD_GRAPHICS");
366 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
378 static void tty_end(void)
383 if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
384 perror(PFX "KDSETMODE KD_TEXT");
386 if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
387 perror(PFX "tcsetattr");
393 int xenv_init(int *xenv_flags, const char *window_title)
397 ret = x11h_init(xenv_flags, window_title);
401 if (xenv_flags != NULL)
402 *xenv_flags &= ~(XENV_CAP_KEYS | XENV_CAP_MOUSE); /* TODO? */
407 fprintf(stderr, PFX "error: both x11h_init and tty_init failed\n");
413 int xenv_update(int (*key_cb)(void *cb_arg, int kc, int is_pressed),
414 int (*mouseb_cb)(void *cb_arg, int x, int y, int button, int is_pressed),
415 int (*mousem_cb)(void *cb_arg, int x, int y),
418 if (g_xstuff.display) {
419 x11h_update(key_cb, mouseb_cb, mousem_cb, cb_arg);
427 /* blocking minimize until user maximizes again */
428 int xenv_minimize(void)
432 if (g_xstuff.display) {
433 xenv_update(NULL, NULL, NULL, NULL);
434 ret = x11h_minimize();
435 xenv_update(NULL, NULL, NULL, NULL);
442 int xenv_keycode_to_keysym(int kc, int shift)
444 if (g_xstuff.display)
445 return g_xstuff.pXkbKeycodeToKeysym(g_xstuff.display,
451 int xenv_get_window(void **display, int *screen, void **window)
453 *display = *window = NULL;
455 if (g_xstuff.display && g_xstuff.window) {
456 *display = g_xstuff.display;
457 *screen = DefaultScreen(g_xstuff.display);
458 *window = (void *)g_xstuff.window;
465 void xenv_finish(void)
476 xenv_init("just a test");
478 for (i = 0; i < 5; i++) {
479 while ((r = xenv_update(&d)) > 0)
480 printf("%d %x %d\n", d, r, r);
485 printf("ll %d\n", i);
488 printf("xenv_finish..\n");