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,
158 ExposureMask | FocusChangeMask | KeyPressMask | KeyReleaseMask | PropertyChangeMask);
159 g_xstuff.pXMapWindow(display, win);
160 g_xstuff.pXGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
161 g_xstuff.pXkbSetDetectableAutoRepeat(display, 1, NULL);
162 // XSetIOErrorHandler
164 // we don't know when event dispatch will be called, so sync now
165 g_xstuff.pXSync(display, False);
171 g_xstuff.display = NULL;
172 fprintf(stderr, "x11 handling disabled.\n");
176 static int x11h_update(int *is_down)
180 while (g_xstuff.pXPending(g_xstuff.display))
182 g_xstuff.pXNextEvent(g_xstuff.display, &evt);
186 while (g_xstuff.pXCheckTypedEvent(g_xstuff.display, Expose, &evt))
189 // printf("event %d\n", evt.type);
194 return g_xstuff.pXLookupKeysym(&evt.xkey, 0);
198 return g_xstuff.pXLookupKeysym(&evt.xkey, 0);
199 // printf("press %d\n", evt.xkey.keycode);
206 static void x11h_wait_vmstate(void)
208 Atom wm_state = g_xstuff.pXInternAtom(g_xstuff.display, "WM_STATE", False);
214 for (i = 0; i < 20; i++) {
215 while (g_xstuff.pXPending(g_xstuff.display)) {
216 g_xstuff.pXNextEvent(g_xstuff.display, &evt);
217 // printf("w event %d\n", evt.type);
218 if (evt.type == PropertyNotify && evt.xproperty.atom == wm_state)
224 printf("timeout waiting for wm_state change\n");
227 static int x11h_minimize(void)
229 XSetWindowAttributes attributes;
230 Display *display = g_xstuff.display;
231 Window window = g_xstuff.window;
232 int screen = DefaultScreen(g_xstuff.display);
233 int display_width, display_height;
237 g_xstuff.pXWithdrawWindow(display, window, screen);
239 attributes.override_redirect = False;
240 g_xstuff.pXChangeWindowAttributes(display, window,
241 CWOverrideRedirect, &attributes);
243 wm_hints.flags = StateHint;
244 wm_hints.initial_state = IconicState;
245 g_xstuff.pXSetWMHints(display, window, &wm_hints);
247 g_xstuff.pXMapWindow(display, window);
249 while (g_xstuff.pXNextEvent(display, &evt) == 0)
251 // printf("m event %d\n", evt.type);
262 g_xstuff.pXWithdrawWindow(display, window, screen);
264 // must wait for some magic vmstate property change before setting override_redirect
267 attributes.override_redirect = True;
268 g_xstuff.pXChangeWindowAttributes(display, window,
269 CWOverrideRedirect, &attributes);
271 // fixup window after resize on override_redirect loss
272 display_width = DisplayWidth(display, screen);
273 display_height = DisplayHeight(display, screen);
274 g_xstuff.pXMoveResizeWindow(display, window, 0, 0, display_width, display_height);
276 g_xstuff.pXMapWindow(display, window);
277 g_xstuff.pXGrabKeyboard(display, window, False, GrabModeAsync, GrabModeAsync, CurrentTime);
278 g_xstuff.pXkbSetDetectableAutoRepeat(display, 1, NULL);
280 // we don't know when event dispatch will be called, so sync now
281 g_xstuff.pXSync(display, False);
286 static struct termios g_kbd_termios_saved;
287 static int g_kbdfd = -1;
289 static int tty_init(void)
291 struct termios kbd_termios;
294 g_kbdfd = open("/dev/tty", O_RDWR);
296 perror(PFX "open /dev/tty");
300 if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
301 perror(PFX "(not hiding FB): KDGETMODE");
305 if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
306 perror(PFX "tcgetattr");
310 g_kbd_termios_saved = kbd_termios;
311 kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
312 kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
313 kbd_termios.c_cc[VMIN] = 0;
314 kbd_termios.c_cc[VTIME] = 0;
316 if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
317 perror(PFX "tcsetattr");
321 if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
322 perror(PFX "KDSETMODE KD_GRAPHICS");
323 tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
335 static void tty_end(void)
340 if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
341 perror(PFX "KDSETMODE KD_TEXT");
343 if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
344 perror(PFX "tcsetattr");
350 int xenv_init(const char *window_title)
354 ret = x11h_init(window_title);
362 fprintf(stderr, PFX "error: both x11h_init and tty_init failed\n");
366 int xenv_update(int *is_down)
368 if (g_xstuff.display)
369 return x11h_update(is_down);
375 /* blocking minimize until user maximazes again */
376 int xenv_minimize(void)
380 if (g_xstuff.display) {
382 ret = x11h_minimize();
390 void xenv_finish(void)
401 xenv_init("just a test");
403 for (i = 0; i < 5; i++) {
404 while ((r = xenv_update(&d)) > 0)
405 printf("%d %x %d\n", d, r, r);
410 printf("ll %d\n", i);
413 printf("xenv_finish..\n");