xenv.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * (C) Gražvydas "notaz" Ignotas, 2009-2012
  3. *
  4. * This work is licensed under the terms of any of these licenses
  5. * (at your option):
  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.
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <pthread.h>
  13. #include <dlfcn.h>
  14. #include <X11/Xlib.h>
  15. #include <X11/Xutil.h>
  16. #include <X11/XKBlib.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <fcntl.h>
  20. #include <unistd.h>
  21. #include <sys/ioctl.h>
  22. #include <termios.h>
  23. #include <linux/kd.h>
  24. #include "xenv.h"
  25. #define PFX "xenv: "
  26. #define FPTR(f) typeof(f) * p##f
  27. #define FPTR_LINK(xf, dl, f) { \
  28. xf.p##f = dlsym(dl, #f); \
  29. if (xf.p##f == NULL) { \
  30. fprintf(stderr, "missing symbol: %s\n", #f); \
  31. goto fail; \
  32. } \
  33. }
  34. struct xstuff {
  35. Display *display;
  36. Window window;
  37. FPTR(XCreateBitmapFromData);
  38. FPTR(XCreatePixmapCursor);
  39. FPTR(XFreePixmap);
  40. FPTR(XOpenDisplay);
  41. FPTR(XDisplayName);
  42. FPTR(XCloseDisplay);
  43. FPTR(XCreateSimpleWindow);
  44. FPTR(XChangeWindowAttributes);
  45. FPTR(XSelectInput);
  46. FPTR(XMapWindow);
  47. FPTR(XNextEvent);
  48. FPTR(XCheckTypedEvent);
  49. FPTR(XWithdrawWindow);
  50. FPTR(XGrabKeyboard);
  51. FPTR(XPending);
  52. FPTR(XLookupKeysym);
  53. FPTR(XkbSetDetectableAutoRepeat);
  54. FPTR(XStoreName);
  55. FPTR(XIconifyWindow);
  56. FPTR(XMoveResizeWindow);
  57. FPTR(XInternAtom);
  58. FPTR(XSetWMHints);
  59. FPTR(XSync);
  60. };
  61. static struct xstuff g_xstuff;
  62. static Cursor transparent_cursor(struct xstuff *xf, Display *display, Window win)
  63. {
  64. Cursor cursor;
  65. Pixmap pix;
  66. XColor dummy;
  67. char d = 0;
  68. memset(&dummy, 0, sizeof(dummy));
  69. pix = xf->pXCreateBitmapFromData(display, win, &d, 1, 1);
  70. cursor = xf->pXCreatePixmapCursor(display, pix, pix,
  71. &dummy, &dummy, 0, 0);
  72. xf->pXFreePixmap(display, pix);
  73. return cursor;
  74. }
  75. static int x11h_init(int *xenv_flags, const char *window_title)
  76. {
  77. unsigned int display_width, display_height;
  78. Display *display;
  79. XSetWindowAttributes attributes;
  80. Window win;
  81. Visual *visual;
  82. long evt_mask;
  83. void *x11lib;
  84. int screen;
  85. memset(&g_xstuff, 0, sizeof(g_xstuff));
  86. x11lib = dlopen("libX11.so.6", RTLD_LAZY);
  87. if (x11lib == NULL) {
  88. fprintf(stderr, "libX11.so load failed:\n%s\n", dlerror());
  89. goto fail;
  90. }
  91. FPTR_LINK(g_xstuff, x11lib, XCreateBitmapFromData);
  92. FPTR_LINK(g_xstuff, x11lib, XCreatePixmapCursor);
  93. FPTR_LINK(g_xstuff, x11lib, XFreePixmap);
  94. FPTR_LINK(g_xstuff, x11lib, XOpenDisplay);
  95. FPTR_LINK(g_xstuff, x11lib, XDisplayName);
  96. FPTR_LINK(g_xstuff, x11lib, XCloseDisplay);
  97. FPTR_LINK(g_xstuff, x11lib, XCreateSimpleWindow);
  98. FPTR_LINK(g_xstuff, x11lib, XChangeWindowAttributes);
  99. FPTR_LINK(g_xstuff, x11lib, XSelectInput);
  100. FPTR_LINK(g_xstuff, x11lib, XMapWindow);
  101. FPTR_LINK(g_xstuff, x11lib, XNextEvent);
  102. FPTR_LINK(g_xstuff, x11lib, XCheckTypedEvent);
  103. FPTR_LINK(g_xstuff, x11lib, XWithdrawWindow);
  104. FPTR_LINK(g_xstuff, x11lib, XGrabKeyboard);
  105. FPTR_LINK(g_xstuff, x11lib, XPending);
  106. FPTR_LINK(g_xstuff, x11lib, XLookupKeysym);
  107. FPTR_LINK(g_xstuff, x11lib, XkbSetDetectableAutoRepeat);
  108. FPTR_LINK(g_xstuff, x11lib, XStoreName);
  109. FPTR_LINK(g_xstuff, x11lib, XIconifyWindow);
  110. FPTR_LINK(g_xstuff, x11lib, XMoveResizeWindow);
  111. FPTR_LINK(g_xstuff, x11lib, XInternAtom);
  112. FPTR_LINK(g_xstuff, x11lib, XSetWMHints);
  113. FPTR_LINK(g_xstuff, x11lib, XSync);
  114. //XInitThreads();
  115. g_xstuff.display = display = g_xstuff.pXOpenDisplay(NULL);
  116. if (display == NULL)
  117. {
  118. fprintf(stderr, "cannot connect to X server %s, X handling disabled.\n",
  119. g_xstuff.pXDisplayName(NULL));
  120. goto fail2;
  121. }
  122. visual = DefaultVisual(display, 0);
  123. if (visual->class != TrueColor)
  124. fprintf(stderr, PFX "warning: non true color visual\n");
  125. printf(PFX "X vendor: %s, rel: %d, display: %s, protocol ver: %d.%d\n", ServerVendor(display),
  126. VendorRelease(display), DisplayString(display), ProtocolVersion(display),
  127. ProtocolRevision(display));
  128. screen = DefaultScreen(display);
  129. display_width = DisplayWidth(display, screen);
  130. display_height = DisplayHeight(display, screen);
  131. printf(PFX "display is %dx%d\n", display_width, display_height);
  132. g_xstuff.window = win = g_xstuff.pXCreateSimpleWindow(display,
  133. RootWindow(display, screen), 0, 0, display_width, display_height,
  134. 0, BlackPixel(display, screen), BlackPixel(display, screen));
  135. attributes.override_redirect = True;
  136. attributes.cursor = transparent_cursor(&g_xstuff, display, win);
  137. g_xstuff.pXChangeWindowAttributes(display, win, CWOverrideRedirect | CWCursor, &attributes);
  138. if (window_title != NULL)
  139. g_xstuff.pXStoreName(display, win, window_title);
  140. evt_mask = ExposureMask | FocusChangeMask | PropertyChangeMask;
  141. if (xenv_flags && (*xenv_flags & XENV_CAP_KEYS))
  142. evt_mask |= KeyPressMask | KeyReleaseMask;
  143. if (xenv_flags && (*xenv_flags & XENV_CAP_MOUSE))
  144. evt_mask |= ButtonPressMask | ButtonReleaseMask | PointerMotionMask;
  145. g_xstuff.pXSelectInput(display, win, evt_mask);
  146. g_xstuff.pXMapWindow(display, win);
  147. g_xstuff.pXGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
  148. g_xstuff.pXkbSetDetectableAutoRepeat(display, 1, NULL);
  149. // XSetIOErrorHandler
  150. // we don't know when event dispatch will be called, so sync now
  151. g_xstuff.pXSync(display, False);
  152. return 0;
  153. fail2:
  154. dlclose(x11lib);
  155. fail:
  156. g_xstuff.display = NULL;
  157. fprintf(stderr, "x11 handling disabled.\n");
  158. return -1;
  159. }
  160. static void x11h_update(int (*key_cb)(void *cb_arg, int kc, int is_pressed),
  161. int (*mouseb_cb)(void *cb_arg, int x, int y, int button, int is_pressed),
  162. int (*mousem_cb)(void *cb_arg, int x, int y),
  163. void *cb_arg)
  164. {
  165. XEvent evt;
  166. int keysym;
  167. while (g_xstuff.pXPending(g_xstuff.display))
  168. {
  169. g_xstuff.pXNextEvent(g_xstuff.display, &evt);
  170. switch (evt.type)
  171. {
  172. case Expose:
  173. while (g_xstuff.pXCheckTypedEvent(g_xstuff.display, Expose, &evt))
  174. ;
  175. break;
  176. case KeyPress:
  177. keysym = g_xstuff.pXLookupKeysym(&evt.xkey, 0);
  178. if (key_cb != NULL)
  179. key_cb(cb_arg, keysym, 1);
  180. break;
  181. case KeyRelease:
  182. keysym = g_xstuff.pXLookupKeysym(&evt.xkey, 0);
  183. if (key_cb != NULL)
  184. key_cb(cb_arg, keysym, 0);
  185. break;
  186. case ButtonPress:
  187. if (mouseb_cb != NULL)
  188. mouseb_cb(cb_arg, evt.xbutton.x, evt.xbutton.y,
  189. evt.xbutton.button, 1);
  190. break;
  191. case ButtonRelease:
  192. if (mouseb_cb != NULL)
  193. mouseb_cb(cb_arg, evt.xbutton.x, evt.xbutton.y,
  194. evt.xbutton.button, 0);
  195. break;
  196. case MotionNotify:
  197. if (mousem_cb != NULL)
  198. mousem_cb(cb_arg, evt.xmotion.x, evt.xmotion.y);
  199. break;
  200. }
  201. }
  202. }
  203. static void x11h_wait_vmstate(void)
  204. {
  205. Atom wm_state = g_xstuff.pXInternAtom(g_xstuff.display, "WM_STATE", False);
  206. XEvent evt;
  207. int i;
  208. usleep(20000);
  209. for (i = 0; i < 20; i++) {
  210. while (g_xstuff.pXPending(g_xstuff.display)) {
  211. g_xstuff.pXNextEvent(g_xstuff.display, &evt);
  212. // printf("w event %d\n", evt.type);
  213. if (evt.type == PropertyNotify && evt.xproperty.atom == wm_state)
  214. return;
  215. }
  216. usleep(200000);
  217. }
  218. fprintf(stderr, PFX "timeout waiting for wm_state change\n");
  219. }
  220. static int x11h_minimize(void)
  221. {
  222. XSetWindowAttributes attributes;
  223. Display *display = g_xstuff.display;
  224. Window window = g_xstuff.window;
  225. int screen = DefaultScreen(g_xstuff.display);
  226. int display_width, display_height;
  227. XWMHints wm_hints;
  228. XEvent evt;
  229. g_xstuff.pXWithdrawWindow(display, window, screen);
  230. attributes.override_redirect = False;
  231. g_xstuff.pXChangeWindowAttributes(display, window,
  232. CWOverrideRedirect, &attributes);
  233. wm_hints.flags = StateHint;
  234. wm_hints.initial_state = IconicState;
  235. g_xstuff.pXSetWMHints(display, window, &wm_hints);
  236. g_xstuff.pXMapWindow(display, window);
  237. while (g_xstuff.pXNextEvent(display, &evt) == 0)
  238. {
  239. // printf("m event %d\n", evt.type);
  240. switch (evt.type)
  241. {
  242. case FocusIn:
  243. goto out;
  244. default:
  245. break;
  246. }
  247. }
  248. out:
  249. g_xstuff.pXWithdrawWindow(display, window, screen);
  250. // must wait for some magic vmstate property change before setting override_redirect
  251. x11h_wait_vmstate();
  252. attributes.override_redirect = True;
  253. g_xstuff.pXChangeWindowAttributes(display, window,
  254. CWOverrideRedirect, &attributes);
  255. // fixup window after resize on override_redirect loss
  256. display_width = DisplayWidth(display, screen);
  257. display_height = DisplayHeight(display, screen);
  258. g_xstuff.pXMoveResizeWindow(display, window, 0, 0, display_width, display_height);
  259. g_xstuff.pXMapWindow(display, window);
  260. g_xstuff.pXGrabKeyboard(display, window, False, GrabModeAsync, GrabModeAsync, CurrentTime);
  261. g_xstuff.pXkbSetDetectableAutoRepeat(display, 1, NULL);
  262. // we don't know when event dispatch will be called, so sync now
  263. g_xstuff.pXSync(display, False);
  264. return 0;
  265. }
  266. static struct termios g_kbd_termios_saved;
  267. static int g_kbdfd = -1;
  268. static int tty_init(void)
  269. {
  270. struct termios kbd_termios;
  271. int mode;
  272. g_kbdfd = open("/dev/tty", O_RDWR);
  273. if (g_kbdfd == -1) {
  274. perror(PFX "open /dev/tty");
  275. return -1;
  276. }
  277. if (ioctl(g_kbdfd, KDGETMODE, &mode) == -1) {
  278. perror(PFX "(not hiding FB): KDGETMODE");
  279. goto fail;
  280. }
  281. if (tcgetattr(g_kbdfd, &kbd_termios) == -1) {
  282. perror(PFX "tcgetattr");
  283. goto fail;
  284. }
  285. g_kbd_termios_saved = kbd_termios;
  286. kbd_termios.c_lflag &= ~(ICANON | ECHO); // | ISIG);
  287. kbd_termios.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
  288. kbd_termios.c_cc[VMIN] = 0;
  289. kbd_termios.c_cc[VTIME] = 0;
  290. if (tcsetattr(g_kbdfd, TCSAFLUSH, &kbd_termios) == -1) {
  291. perror(PFX "tcsetattr");
  292. goto fail;
  293. }
  294. if (ioctl(g_kbdfd, KDSETMODE, KD_GRAPHICS) == -1) {
  295. perror(PFX "KDSETMODE KD_GRAPHICS");
  296. tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved);
  297. goto fail;
  298. }
  299. return 0;
  300. fail:
  301. close(g_kbdfd);
  302. g_kbdfd = -1;
  303. return -1;
  304. }
  305. static void tty_end(void)
  306. {
  307. if (g_kbdfd < 0)
  308. return;
  309. if (ioctl(g_kbdfd, KDSETMODE, KD_TEXT) == -1)
  310. perror(PFX "KDSETMODE KD_TEXT");
  311. if (tcsetattr(g_kbdfd, TCSAFLUSH, &g_kbd_termios_saved) == -1)
  312. perror(PFX "tcsetattr");
  313. close(g_kbdfd);
  314. g_kbdfd = -1;
  315. }
  316. int xenv_init(int *xenv_flags, const char *window_title)
  317. {
  318. int ret;
  319. ret = x11h_init(xenv_flags, window_title);
  320. if (ret == 0)
  321. goto out;
  322. if (xenv_flags != NULL)
  323. *xenv_flags &= ~(XENV_CAP_KEYS | XENV_CAP_MOUSE); /* TODO? */
  324. ret = tty_init();
  325. if (ret == 0)
  326. goto out;
  327. fprintf(stderr, PFX "error: both x11h_init and tty_init failed\n");
  328. ret = -1;
  329. out:
  330. return ret;
  331. }
  332. int xenv_update(int (*key_cb)(void *cb_arg, int kc, int is_pressed),
  333. int (*mouseb_cb)(void *cb_arg, int x, int y, int button, int is_pressed),
  334. int (*mousem_cb)(void *cb_arg, int x, int y),
  335. void *cb_arg)
  336. {
  337. if (g_xstuff.display) {
  338. x11h_update(key_cb, mouseb_cb, mousem_cb, cb_arg);
  339. return 0;
  340. }
  341. // TODO: read tty?
  342. return -1;
  343. }
  344. /* blocking minimize until user maximizes again */
  345. int xenv_minimize(void)
  346. {
  347. int ret;
  348. if (g_xstuff.display) {
  349. xenv_update(NULL, NULL, NULL, NULL);
  350. ret = x11h_minimize();
  351. xenv_update(NULL, NULL, NULL, NULL);
  352. return ret;
  353. }
  354. return -1;
  355. }
  356. void xenv_finish(void)
  357. {
  358. // TODO: cleanup X?
  359. tty_end();
  360. }
  361. #if 0
  362. int main()
  363. {
  364. int i, r, d;
  365. xenv_init("just a test");
  366. for (i = 0; i < 5; i++) {
  367. while ((r = xenv_update(&d)) > 0)
  368. printf("%d %x %d\n", d, r, r);
  369. sleep(1);
  370. if (i == 1)
  371. xenv_minimize();
  372. printf("ll %d\n", i);
  373. }
  374. printf("xenv_finish..\n");
  375. xenv_finish();
  376. return 0;
  377. }
  378. #endif