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);
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, XkbKeycodeToKeysym);
122 FPTR_LINK(g_xstuff, x11lib, XStoreName);
123 FPTR_LINK(g_xstuff, x11lib, XIconifyWindow);
124 FPTR_LINK(g_xstuff, x11lib, XMoveResizeWindow);
125 FPTR_LINK(g_xstuff, x11lib, XInternAtom);
126 FPTR_LINK(g_xstuff, x11lib, XSetWMHints);
127 FPTR_LINK(g_xstuff, x11lib, XSync);
131 g_xstuff.display = display = g_xstuff.pXOpenDisplay(NULL);
134 fprintf(stderr, "cannot connect to X server %s, X handling disabled.\n",
135 g_xstuff.pXDisplayName(NULL));
139 visual = DefaultVisual(display, 0);
140 if (visual->class != TrueColor)
141 fprintf(stderr, PFX "warning: non true color visual\n");
143 printf(PFX "X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display),
144 VendorRelease(display), DisplayString(display), ProtocolVersion(display),
145 ProtocolRevision(display));
147 screen = DefaultScreen(display);
149 display_width = DisplayWidth(display, screen);
150 display_height = DisplayHeight(display, screen);
151 printf(PFX "display is %dx%d\n", display_width, display_height);
153 g_xstuff.window = win = g_xstuff.pXCreateSimpleWindow(display,
154 RootWindow(display, screen), 0, 0, display_width, display_height,
155 0, BlackPixel(display, screen), BlackPixel(display, screen));
157 attributes.override_redirect = True;
158 attributes.cursor = transparent_cursor(&g_xstuff, display, win);
159 g_xstuff.pXChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
161 if (window_title != NULL)
162 g_xstuff.pXStoreName(display, win, window_title);
163 evt_mask = ExposureMask | FocusChangeMask | PropertyChangeMask;
164 if (xenv_flags && (*xenv_flags & XENV_CAP_KEYS))
165 evt_mask |= KeyPressMask | KeyReleaseMask;
166 if (xenv_flags && (*xenv_flags & XENV_CAP_MOUSE))
167 evt_mask |= ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
168 g_xstuff.pXSelectInput(display, win, evt_mask);
169 g_xstuff.pXMapWindow(display, win);
170 g_xstuff.pXGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
171 g_xstuff.pXkbSetDetectableAutoRepeat(display, 1, NULL);
172 // XSetIOErrorHandler
174 // we don't know when event dispatch will be called, so sync now
175 g_xstuff.pXSync(display, False);
181 g_xstuff.display = NULL;
182 fprintf(stderr, "x11 handling disabled.\n");
186 static void x11h_update(int (*key_cb)(void *cb_arg, int kc, int is_pressed),
187 int (*mouseb_cb)(void *cb_arg, int x, int y, int button, int is_pressed),
188 int (*mousem_cb)(void *cb_arg, int x, int y),
194 while (g_xstuff.pXPending(g_xstuff.display))
196 g_xstuff.pXNextEvent(g_xstuff.display, &evt);
200 while (g_xstuff.pXCheckTypedEvent(g_xstuff.display, Expose, &evt))
205 keysym = g_xstuff.pXLookupKeysym(&evt.xkey, 0);
207 key_cb(cb_arg, keysym, 1);
211 keysym = g_xstuff.pXLookupKeysym(&evt.xkey, 0);
213 key_cb(cb_arg, keysym, 0);
217 if (mouseb_cb != NULL)
218 mouseb_cb(cb_arg, evt.xbutton.x, evt.xbutton.y,
219 evt.xbutton.button, 1);
223 if (mouseb_cb != NULL)
224 mouseb_cb(cb_arg, evt.xbutton.x, evt.xbutton.y,
225 evt.xbutton.button, 0);
229 if (mousem_cb != NULL)
230 mousem_cb(cb_arg, evt.xmotion.x, evt.xmotion.y);
236 static void x11h_wait_vmstate(void)
238 Atom wm_state = g_xstuff.pXInternAtom(g_xstuff.display, "WM_STATE", False);
244 for (i = 0; i < 20; i++) {
245 while (g_xstuff.pXPending(g_xstuff.display)) {
246 g_xstuff.pXNextEvent(g_xstuff.display, &evt);
247 // printf("w event %d\n", evt.type);
248 if (evt.type == PropertyNotify && evt.xproperty.atom == wm_state)
254 fprintf(stderr, PFX "timeout waiting for wm_state change\n");
257 static int x11h_minimize(void)
259 XSetWindowAttributes attributes;
260 Display *display = g_xstuff.display;
261 Window window = g_xstuff.window;
262 int screen = DefaultScreen(g_xstuff.display);
263 int display_width, display_height;
267 g_xstuff.pXWithdrawWindow(display, window, screen);
269 attributes.override_redirect = False;
270 g_xstuff.pXChangeWindowAttributes(display, window,
271 CWOverrideRedirect, &attributes);
273 wm_hints.flags = StateHint;
274 wm_hints.initial_state = IconicState;
275 g_xstuff.pXSetWMHints(display, window, &wm_hints);
277 g_xstuff.pXMapWindow(display, window);
279 while (g_xstuff.pXNextEvent(display, &evt) == 0)
281 // printf("m event %d\n", evt.type);
292 g_xstuff.pXWithdrawWindow(display, window, screen);
294 // must wait for some magic vmstate property change before setting override_redirect
297 attributes.override_redirect = True;
298 g_xstuff.pXChangeWindowAttributes(display, window,
299 CWOverrideRedirect, &attributes);
301 // fixup window after resize on override_redirect loss
302 display_width = DisplayWidth(display, screen);
303 display_height = DisplayHeight(display, screen);
304 g_xstuff.pXMoveResizeWindow(display, window, 0, 0, display_width, display_height);
306 g_xstuff.pXMapWindow(display, window);
307 g_xstuff.pXGrabKeyboard(display, window, False, GrabModeAsync, GrabModeAsync, CurrentTime);
308 g_xstuff.pXkbSetDetectableAutoRepeat(display, 1, NULL);
310 // we don't know when event dispatch will be called, so sync now
311 g_xstuff.pXSync(display, False);
316 static struct termios g_kbd_termios_saved;
317 static int g_kbdfd = -1;
319 static int tty_init(void)
321 struct termios kbd_termios;
324 g_kbdfd = open("/dev/tty", O_RDWR);
326 perror(PFX "open /dev/tty");
330 if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
331 perror(PFX "(not hiding FB): KDGETMODE");
335 if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
336 perror(PFX "tcgetattr");
340 g_kbd_termios_saved = kbd_termios;
341 kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
342 kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
343 kbd_termios.c_cc[VMIN] = 0;
344 kbd_termios.c_cc[VTIME] = 0;
346 if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
347 perror(PFX "tcsetattr");
351 if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
352 perror(PFX "KDSETMODE KD_GRAPHICS");
353 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
365 static void tty_end(void)
370 if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
371 perror(PFX "KDSETMODE KD_TEXT");
373 if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
374 perror(PFX "tcsetattr");
380 int xenv_init(int *xenv_flags, const char *window_title)
384 ret = x11h_init(xenv_flags, window_title);
388 if (xenv_flags != NULL)
389 *xenv_flags &= ~(XENV_CAP_KEYS | XENV_CAP_MOUSE); /* TODO? */
394 fprintf(stderr, PFX "error: both x11h_init and tty_init failed\n");
400 int xenv_update(int (*key_cb)(void *cb_arg, int kc, int is_pressed),
401 int (*mouseb_cb)(void *cb_arg, int x, int y, int button, int is_pressed),
402 int (*mousem_cb)(void *cb_arg, int x, int y),
405 if (g_xstuff.display) {
406 x11h_update(key_cb, mouseb_cb, mousem_cb, cb_arg);
414 /* blocking minimize until user maximizes again */
415 int xenv_minimize(void)
419 if (g_xstuff.display) {
420 xenv_update(NULL, NULL, NULL, NULL);
421 ret = x11h_minimize();
422 xenv_update(NULL, NULL, NULL, NULL);
429 int xenv_keycode_to_keysym(int kc, int shift)
431 if (g_xstuff.display)
432 return g_xstuff.pXkbKeycodeToKeysym(g_xstuff.display,
438 void xenv_finish(void)
449 xenv_init("just a test");
451 for (i = 0; i < 5; i++) {
452 while ((r = xenv_update(&d)) > 0)
453 printf("%d %x %d\n", d, r, r);
458 printf("ll %d\n", i);
461 printf("xenv_finish..\n");