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>
30 #define FPTR(f) typeof(f) * p##f
31 #define FPTR_LINK(xf, dl, f) { \
32 xf.p##f = dlsym(dl, #f); \
33 if (xf.p##f == NULL) { \
34 fprintf(stderr, "missing symbol: %s\n", #f); \
42 FPTR(XCreateBitmapFromData);
43 FPTR(XCreatePixmapCursor);
48 FPTR(XCreateSimpleWindow);
49 FPTR(XChangeWindowAttributes);
53 FPTR(XCheckTypedEvent);
54 FPTR(XWithdrawWindow);
58 FPTR(XkbSetDetectableAutoRepeat);
61 FPTR(XMoveResizeWindow);
67 static struct xstuff g_xstuff;
69 static Cursor transparent_cursor(struct xstuff *xf, Display *display, Window win)
76 memset(&dummy, 0, sizeof(dummy));
77 pix = xf->pXCreateBitmapFromData(display, win, &d, 1, 1);
78 cursor = xf->pXCreatePixmapCursor(display, pix, pix,
79 &dummy, &dummy, 0, 0);
80 xf->pXFreePixmap(display, pix);
84 static int x11h_init(const char *window_title)
86 unsigned int display_width, display_height;
88 XSetWindowAttributes attributes;
94 memset(&g_xstuff, 0, sizeof(g_xstuff));
95 x11lib = dlopen("libX11.so.6", RTLD_LAZY);
97 fprintf(stderr, "libX11.so load failed:\n%s\n", dlerror());
100 FPTR_LINK(g_xstuff, x11lib, XCreateBitmapFromData);
101 FPTR_LINK(g_xstuff, x11lib, XCreatePixmapCursor);
102 FPTR_LINK(g_xstuff, x11lib, XFreePixmap);
103 FPTR_LINK(g_xstuff, x11lib, XOpenDisplay);
104 FPTR_LINK(g_xstuff, x11lib, XDisplayName);
105 FPTR_LINK(g_xstuff, x11lib, XCloseDisplay);
106 FPTR_LINK(g_xstuff, x11lib, XCreateSimpleWindow);
107 FPTR_LINK(g_xstuff, x11lib, XChangeWindowAttributes);
108 FPTR_LINK(g_xstuff, x11lib, XSelectInput);
109 FPTR_LINK(g_xstuff, x11lib, XMapWindow);
110 FPTR_LINK(g_xstuff, x11lib, XNextEvent);
111 FPTR_LINK(g_xstuff, x11lib, XCheckTypedEvent);
112 FPTR_LINK(g_xstuff, x11lib, XWithdrawWindow);
113 FPTR_LINK(g_xstuff, x11lib, XGrabKeyboard);
114 FPTR_LINK(g_xstuff, x11lib, XPending);
115 FPTR_LINK(g_xstuff, x11lib, XLookupKeysym);
116 FPTR_LINK(g_xstuff, x11lib, XkbSetDetectableAutoRepeat);
117 FPTR_LINK(g_xstuff, x11lib, XStoreName);
118 FPTR_LINK(g_xstuff, x11lib, XIconifyWindow);
119 FPTR_LINK(g_xstuff, x11lib, XMoveResizeWindow);
120 FPTR_LINK(g_xstuff, x11lib, XInternAtom);
121 FPTR_LINK(g_xstuff, x11lib, XSetWMHints);
122 FPTR_LINK(g_xstuff, x11lib, XSync);
126 g_xstuff.display = display = g_xstuff.pXOpenDisplay(NULL);
129 fprintf(stderr, "cannot connect to X server %s, X handling disabled.\n",
130 g_xstuff.pXDisplayName(NULL));
134 visual = DefaultVisual(display, 0);
135 if (visual->class != TrueColor)
136 fprintf(stderr, PFX "warning: non true color visual\n");
138 printf(PFX "X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display),
139 VendorRelease(display), DisplayString(display), ProtocolVersion(display),
140 ProtocolRevision(display));
142 screen = DefaultScreen(display);
144 display_width = DisplayWidth(display, screen);
145 display_height = DisplayHeight(display, screen);
146 printf(PFX "display is %dx%d\n", display_width, display_height);
148 g_xstuff.window = win = g_xstuff.pXCreateSimpleWindow(display,
149 RootWindow(display, screen), 0, 0, display_width, display_height,
150 0, BlackPixel(display, screen), BlackPixel(display, screen));
152 attributes.override_redirect = True;
153 attributes.cursor = transparent_cursor(&g_xstuff, display, win);
154 g_xstuff.pXChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
156 g_xstuff.pXStoreName(display, win, window_title);
157 g_xstuff.pXSelectInput(display, win, ExposureMask | FocusChangeMask
158 | KeyPressMask | KeyReleaseMask | ButtonPressMask
159 | ButtonReleaseMask | PointerMotionMask | PropertyChangeMask);
160 g_xstuff.pXMapWindow(display, win);
161 g_xstuff.pXGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
162 g_xstuff.pXkbSetDetectableAutoRepeat(display, 1, NULL);
163 // XSetIOErrorHandler
165 // we don't know when event dispatch will be called, so sync now
166 g_xstuff.pXSync(display, False);
172 g_xstuff.display = NULL;
173 fprintf(stderr, "x11 handling disabled.\n");
177 static void x11h_update(int (*key_cb)(void *cb_arg, int kc, int is_pressed),
178 int (*mouseb_cb)(void *cb_arg, int x, int y, int button, int is_pressed),
179 int (*mousem_cb)(void *cb_arg, int x, int y),
185 while (g_xstuff.pXPending(g_xstuff.display))
187 g_xstuff.pXNextEvent(g_xstuff.display, &evt);
191 while (g_xstuff.pXCheckTypedEvent(g_xstuff.display, Expose, &evt))
196 keysym = g_xstuff.pXLookupKeysym(&evt.xkey, 0);
198 key_cb(cb_arg, keysym, 1);
202 keysym = g_xstuff.pXLookupKeysym(&evt.xkey, 0);
204 key_cb(cb_arg, keysym, 0);
208 if (mouseb_cb != NULL)
209 mouseb_cb(cb_arg, evt.xbutton.x, evt.xbutton.y,
210 evt.xbutton.button, 1);
214 if (mouseb_cb != NULL)
215 mouseb_cb(cb_arg, evt.xbutton.x, evt.xbutton.y,
216 evt.xbutton.button, 0);
220 if (mousem_cb != NULL)
221 mousem_cb(cb_arg, evt.xmotion.x, evt.xmotion.y);
227 static void x11h_wait_vmstate(void)
229 Atom wm_state = g_xstuff.pXInternAtom(g_xstuff.display, "WM_STATE", False);
235 for (i = 0; i < 20; i++) {
236 while (g_xstuff.pXPending(g_xstuff.display)) {
237 g_xstuff.pXNextEvent(g_xstuff.display, &evt);
238 // printf("w event %d\n", evt.type);
239 if (evt.type == PropertyNotify && evt.xproperty.atom == wm_state)
245 fprintf(stderr, PFX "timeout waiting for wm_state change\n");
248 static int x11h_minimize(void)
250 XSetWindowAttributes attributes;
251 Display *display = g_xstuff.display;
252 Window window = g_xstuff.window;
253 int screen = DefaultScreen(g_xstuff.display);
254 int display_width, display_height;
258 g_xstuff.pXWithdrawWindow(display, window, screen);
260 attributes.override_redirect = False;
261 g_xstuff.pXChangeWindowAttributes(display, window,
262 CWOverrideRedirect, &attributes);
264 wm_hints.flags = StateHint;
265 wm_hints.initial_state = IconicState;
266 g_xstuff.pXSetWMHints(display, window, &wm_hints);
268 g_xstuff.pXMapWindow(display, window);
270 while (g_xstuff.pXNextEvent(display, &evt) == 0)
272 // printf("m event %d\n", evt.type);
283 g_xstuff.pXWithdrawWindow(display, window, screen);
285 // must wait for some magic vmstate property change before setting override_redirect
288 attributes.override_redirect = True;
289 g_xstuff.pXChangeWindowAttributes(display, window,
290 CWOverrideRedirect, &attributes);
292 // fixup window after resize on override_redirect loss
293 display_width = DisplayWidth(display, screen);
294 display_height = DisplayHeight(display, screen);
295 g_xstuff.pXMoveResizeWindow(display, window, 0, 0, display_width, display_height);
297 g_xstuff.pXMapWindow(display, window);
298 g_xstuff.pXGrabKeyboard(display, window, False, GrabModeAsync, GrabModeAsync, CurrentTime);
299 g_xstuff.pXkbSetDetectableAutoRepeat(display, 1, NULL);
301 // we don't know when event dispatch will be called, so sync now
302 g_xstuff.pXSync(display, False);
307 static struct termios g_kbd_termios_saved;
308 static int g_kbdfd = -1;
310 static int tty_init(void)
312 struct termios kbd_termios;
315 g_kbdfd = open("/dev/tty", O_RDWR);
317 perror(PFX "open /dev/tty");
321 if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
322 perror(PFX "(not hiding FB): KDGETMODE");
326 if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
327 perror(PFX "tcgetattr");
331 g_kbd_termios_saved = kbd_termios;
332 kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
333 kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
334 kbd_termios.c_cc[VMIN] = 0;
335 kbd_termios.c_cc[VTIME] = 0;
337 if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
338 perror(PFX "tcsetattr");
342 if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
343 perror(PFX "KDSETMODE KD_GRAPHICS");
344 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
356 static void tty_end(void)
361 if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
362 perror(PFX "KDSETMODE KD_TEXT");
364 if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
365 perror(PFX "tcsetattr");
371 int xenv_init(int *have_mouse_events, const char *window_title)
376 ret = x11h_init(window_title);
386 fprintf(stderr, PFX "error: both x11h_init and tty_init failed\n");
389 if (have_mouse_events != NULL)
390 *have_mouse_events = have_mouse;
394 int xenv_update(int (*key_cb)(void *cb_arg, int kc, int is_pressed),
395 int (*mouseb_cb)(void *cb_arg, int x, int y, int button, int is_pressed),
396 int (*mousem_cb)(void *cb_arg, int x, int y),
399 if (g_xstuff.display) {
400 x11h_update(key_cb, mouseb_cb, mousem_cb, cb_arg);
408 /* blocking minimize until user maximizes again */
409 int xenv_minimize(void)
413 if (g_xstuff.display) {
414 xenv_update(NULL, NULL, NULL, NULL);
415 ret = x11h_minimize();
416 xenv_update(NULL, NULL, NULL, NULL);
423 void xenv_finish(void)
434 xenv_init("just a test");
436 for (i = 0; i < 5; i++) {
437 while ((r = xenv_update(&d)) > 0)
438 printf("%d %x %d\n", d, r, r);
443 printf("ll %d\n", i);
446 printf("xenv_finish..\n");