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.
9 * See the COPYING file in the top-level directory.
18 #include <X11/Xutil.h>
19 #include <X11/XKBlib.h>
21 #include <sys/types.h>
25 #include <sys/ioctl.h>
33 #define FPTR(f) typeof(f) * p##f
34 #define FPTR_LINK(xf, dl, f) { \
35 xf.p##f = dlsym(dl, #f); \
36 if (xf.p##f == NULL) { \
37 fprintf(stderr, "missing symbol: %s\n", #f); \
45 FPTR(XCreateBitmapFromData);
46 FPTR(XCreatePixmapCursor);
51 FPTR(XCreateSimpleWindow);
52 FPTR(XChangeWindowAttributes);
56 FPTR(XCheckTypedEvent);
57 FPTR(XWithdrawWindow);
61 FPTR(XkbSetDetectableAutoRepeat);
64 FPTR(XMoveResizeWindow);
70 static struct xstuff g_xstuff;
72 static Cursor transparent_cursor(struct xstuff *xf, Display *display, Window win)
79 memset(&dummy, 0, sizeof(dummy));
80 pix = xf->pXCreateBitmapFromData(display, win, &d, 1, 1);
81 cursor = xf->pXCreatePixmapCursor(display, pix, pix,
82 &dummy, &dummy, 0, 0);
83 xf->pXFreePixmap(display, pix);
87 static int x11h_init(int *xenv_flags, const char *window_title)
89 unsigned int display_width, display_height;
91 XSetWindowAttributes attributes;
98 memset(&g_xstuff, 0, sizeof(g_xstuff));
99 x11lib = dlopen("libX11.so.6", RTLD_LAZY);
100 if (x11lib == NULL) {
101 fprintf(stderr, "libX11.so load failed:\n%s\n", dlerror());
104 FPTR_LINK(g_xstuff, x11lib, XCreateBitmapFromData);
105 FPTR_LINK(g_xstuff, x11lib, XCreatePixmapCursor);
106 FPTR_LINK(g_xstuff, x11lib, XFreePixmap);
107 FPTR_LINK(g_xstuff, x11lib, XOpenDisplay);
108 FPTR_LINK(g_xstuff, x11lib, XDisplayName);
109 FPTR_LINK(g_xstuff, x11lib, XCloseDisplay);
110 FPTR_LINK(g_xstuff, x11lib, XCreateSimpleWindow);
111 FPTR_LINK(g_xstuff, x11lib, XChangeWindowAttributes);
112 FPTR_LINK(g_xstuff, x11lib, XSelectInput);
113 FPTR_LINK(g_xstuff, x11lib, XMapWindow);
114 FPTR_LINK(g_xstuff, x11lib, XNextEvent);
115 FPTR_LINK(g_xstuff, x11lib, XCheckTypedEvent);
116 FPTR_LINK(g_xstuff, x11lib, XWithdrawWindow);
117 FPTR_LINK(g_xstuff, x11lib, XGrabKeyboard);
118 FPTR_LINK(g_xstuff, x11lib, XPending);
119 FPTR_LINK(g_xstuff, x11lib, XLookupKeysym);
120 FPTR_LINK(g_xstuff, x11lib, XkbSetDetectableAutoRepeat);
121 FPTR_LINK(g_xstuff, x11lib, XStoreName);
122 FPTR_LINK(g_xstuff, x11lib, XIconifyWindow);
123 FPTR_LINK(g_xstuff, x11lib, XMoveResizeWindow);
124 FPTR_LINK(g_xstuff, x11lib, XInternAtom);
125 FPTR_LINK(g_xstuff, x11lib, XSetWMHints);
126 FPTR_LINK(g_xstuff, x11lib, XSync);
130 g_xstuff.display = display = g_xstuff.pXOpenDisplay(NULL);
133 fprintf(stderr, "cannot connect to X server %s, X handling disabled.\n",
134 g_xstuff.pXDisplayName(NULL));
138 visual = DefaultVisual(display, 0);
139 if (visual->class != TrueColor)
140 fprintf(stderr, PFX "warning: non true color visual\n");
142 printf(PFX "X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display),
143 VendorRelease(display), DisplayString(display), ProtocolVersion(display),
144 ProtocolRevision(display));
146 screen = DefaultScreen(display);
148 display_width = DisplayWidth(display, screen);
149 display_height = DisplayHeight(display, screen);
150 printf(PFX "display is %dx%d\n", display_width, display_height);
152 g_xstuff.window = win = g_xstuff.pXCreateSimpleWindow(display,
153 RootWindow(display, screen), 0, 0, display_width, display_height,
154 0, BlackPixel(display, screen), BlackPixel(display, screen));
156 attributes.override_redirect = True;
157 attributes.cursor = transparent_cursor(&g_xstuff, display, win);
158 g_xstuff.pXChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
160 if (window_title != NULL)
161 g_xstuff.pXStoreName(display, win, window_title);
162 evt_mask = ExposureMask | FocusChangeMask | PropertyChangeMask;
163 if (xenv_flags && (*xenv_flags & XENV_CAP_KEYS))
164 evt_mask |= KeyPressMask | KeyReleaseMask;
165 if (xenv_flags && (*xenv_flags & XENV_CAP_MOUSE))
166 evt_mask |= ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
167 g_xstuff.pXSelectInput(display, win, evt_mask);
168 g_xstuff.pXMapWindow(display, win);
169 g_xstuff.pXGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
170 g_xstuff.pXkbSetDetectableAutoRepeat(display, 1, NULL);
171 // XSetIOErrorHandler
173 // we don't know when event dispatch will be called, so sync now
174 g_xstuff.pXSync(display, False);
180 g_xstuff.display = NULL;
181 fprintf(stderr, "x11 handling disabled.\n");
185 static void x11h_update(int (*key_cb)(void *cb_arg, int kc, int is_pressed),
186 int (*mouseb_cb)(void *cb_arg, int x, int y, int button, int is_pressed),
187 int (*mousem_cb)(void *cb_arg, int x, int y),
193 while (g_xstuff.pXPending(g_xstuff.display))
195 g_xstuff.pXNextEvent(g_xstuff.display, &evt);
199 while (g_xstuff.pXCheckTypedEvent(g_xstuff.display, Expose, &evt))
204 keysym = g_xstuff.pXLookupKeysym(&evt.xkey, 0);
206 key_cb(cb_arg, keysym, 1);
210 keysym = g_xstuff.pXLookupKeysym(&evt.xkey, 0);
212 key_cb(cb_arg, keysym, 0);
216 if (mouseb_cb != NULL)
217 mouseb_cb(cb_arg, evt.xbutton.x, evt.xbutton.y,
218 evt.xbutton.button, 1);
222 if (mouseb_cb != NULL)
223 mouseb_cb(cb_arg, evt.xbutton.x, evt.xbutton.y,
224 evt.xbutton.button, 0);
228 if (mousem_cb != NULL)
229 mousem_cb(cb_arg, evt.xmotion.x, evt.xmotion.y);
235 static void x11h_wait_vmstate(void)
237 Atom wm_state = g_xstuff.pXInternAtom(g_xstuff.display, "WM_STATE", False);
243 for (i = 0; i < 20; i++) {
244 while (g_xstuff.pXPending(g_xstuff.display)) {
245 g_xstuff.pXNextEvent(g_xstuff.display, &evt);
246 // printf("w event %d\n", evt.type);
247 if (evt.type == PropertyNotify && evt.xproperty.atom == wm_state)
253 fprintf(stderr, PFX "timeout waiting for wm_state change\n");
256 static int x11h_minimize(void)
258 XSetWindowAttributes attributes;
259 Display *display = g_xstuff.display;
260 Window window = g_xstuff.window;
261 int screen = DefaultScreen(g_xstuff.display);
262 int display_width, display_height;
266 g_xstuff.pXWithdrawWindow(display, window, screen);
268 attributes.override_redirect = False;
269 g_xstuff.pXChangeWindowAttributes(display, window,
270 CWOverrideRedirect, &attributes);
272 wm_hints.flags = StateHint;
273 wm_hints.initial_state = IconicState;
274 g_xstuff.pXSetWMHints(display, window, &wm_hints);
276 g_xstuff.pXMapWindow(display, window);
278 while (g_xstuff.pXNextEvent(display, &evt) == 0)
280 // printf("m event %d\n", evt.type);
291 g_xstuff.pXWithdrawWindow(display, window, screen);
293 // must wait for some magic vmstate property change before setting override_redirect
296 attributes.override_redirect = True;
297 g_xstuff.pXChangeWindowAttributes(display, window,
298 CWOverrideRedirect, &attributes);
300 // fixup window after resize on override_redirect loss
301 display_width = DisplayWidth(display, screen);
302 display_height = DisplayHeight(display, screen);
303 g_xstuff.pXMoveResizeWindow(display, window, 0, 0, display_width, display_height);
305 g_xstuff.pXMapWindow(display, window);
306 g_xstuff.pXGrabKeyboard(display, window, False, GrabModeAsync, GrabModeAsync, CurrentTime);
307 g_xstuff.pXkbSetDetectableAutoRepeat(display, 1, NULL);
309 // we don't know when event dispatch will be called, so sync now
310 g_xstuff.pXSync(display, False);
315 static struct termios g_kbd_termios_saved;
316 static int g_kbdfd = -1;
318 static int tty_init(void)
320 struct termios kbd_termios;
323 g_kbdfd = open("/dev/tty", O_RDWR);
325 perror(PFX "open /dev/tty");
329 if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
330 perror(PFX "(not hiding FB): KDGETMODE");
334 if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
335 perror(PFX "tcgetattr");
339 g_kbd_termios_saved = kbd_termios;
340 kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
341 kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
342 kbd_termios.c_cc[VMIN] = 0;
343 kbd_termios.c_cc[VTIME] = 0;
345 if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
346 perror(PFX "tcsetattr");
350 if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
351 perror(PFX "KDSETMODE KD_GRAPHICS");
352 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
364 static void tty_end(void)
369 if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
370 perror(PFX "KDSETMODE KD_TEXT");
372 if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
373 perror(PFX "tcsetattr");
379 int xenv_init(int *xenv_flags, const char *window_title)
383 ret = x11h_init(xenv_flags, window_title);
387 if (xenv_flags != NULL)
388 *xenv_flags &= ~(XENV_CAP_KEYS | XENV_CAP_MOUSE); /* TODO? */
393 fprintf(stderr, PFX "error: both x11h_init and tty_init failed\n");
399 int xenv_update(int (*key_cb)(void *cb_arg, int kc, int is_pressed),
400 int (*mouseb_cb)(void *cb_arg, int x, int y, int button, int is_pressed),
401 int (*mousem_cb)(void *cb_arg, int x, int y),
404 if (g_xstuff.display) {
405 x11h_update(key_cb, mouseb_cb, mousem_cb, cb_arg);
413 /* blocking minimize until user maximizes again */
414 int xenv_minimize(void)
418 if (g_xstuff.display) {
419 xenv_update(NULL, NULL, NULL, NULL);
420 ret = x11h_minimize();
421 xenv_update(NULL, NULL, NULL, NULL);
428 void xenv_finish(void)
439 xenv_init("just a test");
441 for (i = 0; i < 5; i++) {
442 while ((r = xenv_update(&d)) > 0)
443 printf("%d %x %d\n", d, r, r);
448 printf("ll %d\n", i);
451 printf("xenv_finish..\n");