You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 lines
8.9 KiB

  1. /*
  2. * vim:ts=4:sw=4:expandtab
  3. *
  4. * © 2010-2012 Michael Stapelberg
  5. *
  6. * xcb.c: contains all functions which use XCB to talk to X11. Mostly wrappers
  7. * around the rather complicated/ugly parts of the XCB API.
  8. *
  9. */
  10. #include <xcb/xcb.h>
  11. #include <xcb/xcb_image.h>
  12. #include <xcb/dpms.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdbool.h>
  16. #include <unistd.h>
  17. #include <assert.h>
  18. #include <err.h>
  19. #include "cursors.h"
  20. xcb_connection_t *conn;
  21. xcb_screen_t *screen;
  22. #define curs_invisible_width 8
  23. #define curs_invisible_height 8
  24. static unsigned char curs_invisible_bits[] = {
  25. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  26. #define curs_windows_width 11
  27. #define curs_windows_height 19
  28. static unsigned char curs_windows_bits[] = {
  29. 0xfe, 0x07, 0xfc, 0x07, 0xfa, 0x07, 0xf6, 0x07, 0xee, 0x07, 0xde, 0x07,
  30. 0xbe, 0x07, 0x7e, 0x07, 0xfe, 0x06, 0xfe, 0x05, 0x3e, 0x00, 0xb6, 0x07,
  31. 0x6a, 0x07, 0x6c, 0x07, 0xde, 0x06, 0xdf, 0x06, 0xbf, 0x05, 0xbf, 0x05,
  32. 0x7f, 0x06 };
  33. #define mask_windows_width 11
  34. #define mask_windows_height 19
  35. static unsigned char mask_windows_bits[] = {
  36. 0x01, 0x00, 0x03, 0x00, 0x07, 0x00, 0x0f, 0x00, 0x1f, 0x00, 0x3f, 0x00,
  37. 0x7f, 0x00, 0xff, 0x00, 0xff, 0x01, 0xff, 0x03, 0xff, 0x07, 0x7f, 0x00,
  38. 0xf7, 0x00, 0xf3, 0x00, 0xe1, 0x01, 0xe0, 0x01, 0xc0, 0x03, 0xc0, 0x03,
  39. 0x80, 0x01 };
  40. static uint32_t get_colorpixel(char *hex) {
  41. char strgroups[3][3] = {{hex[0], hex[1], '\0'},
  42. {hex[2], hex[3], '\0'},
  43. {hex[4], hex[5], '\0'}};
  44. uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
  45. (strtol(strgroups[1], NULL, 16)),
  46. (strtol(strgroups[2], NULL, 16))};
  47. return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2];
  48. }
  49. xcb_visualtype_t *get_root_visual_type(xcb_screen_t *screen) {
  50. xcb_visualtype_t *visual_type = NULL;
  51. xcb_depth_iterator_t depth_iter;
  52. xcb_visualtype_iterator_t visual_iter;
  53. for (depth_iter = xcb_screen_allowed_depths_iterator(screen);
  54. depth_iter.rem;
  55. xcb_depth_next(&depth_iter)) {
  56. for (visual_iter = xcb_depth_visuals_iterator(depth_iter.data);
  57. visual_iter.rem;
  58. xcb_visualtype_next(&visual_iter)) {
  59. if (screen->root_visual != visual_iter.data->visual_id)
  60. continue;
  61. visual_type = visual_iter.data;
  62. return visual_type;
  63. }
  64. }
  65. return NULL;
  66. }
  67. xcb_pixmap_t create_bg_pixmap(xcb_connection_t *conn, xcb_screen_t *scr, u_int32_t* resolution, char *color) {
  68. xcb_pixmap_t bg_pixmap = xcb_generate_id(conn);
  69. xcb_create_pixmap(conn, scr->root_depth, bg_pixmap, scr->root,
  70. resolution[0], resolution[1]);
  71. /* Generate a Graphics Context and fill the pixmap with background color
  72. * (for images that are smaller than your screen) */
  73. xcb_gcontext_t gc = xcb_generate_id(conn);
  74. uint32_t values[] = { get_colorpixel(color) };
  75. xcb_create_gc(conn, gc, bg_pixmap, XCB_GC_FOREGROUND, values);
  76. xcb_rectangle_t rect = { 0, 0, resolution[0], resolution[1] };
  77. xcb_poly_fill_rectangle(conn, bg_pixmap, gc, 1, &rect);
  78. xcb_free_gc(conn, gc);
  79. return bg_pixmap;
  80. }
  81. xcb_window_t open_fullscreen_window(xcb_connection_t *conn, xcb_screen_t *scr, char *color, xcb_pixmap_t pixmap) {
  82. uint32_t mask = 0;
  83. uint32_t values[3];
  84. xcb_window_t win = xcb_generate_id(conn);
  85. if (pixmap == XCB_NONE) {
  86. mask |= XCB_CW_BACK_PIXEL;
  87. values[0] = get_colorpixel(color);
  88. } else {
  89. mask |= XCB_CW_BACK_PIXMAP;
  90. values[0] = pixmap;
  91. }
  92. mask |= XCB_CW_OVERRIDE_REDIRECT;
  93. values[1] = 1;
  94. mask |= XCB_CW_EVENT_MASK;
  95. values[2] = XCB_EVENT_MASK_EXPOSURE |
  96. XCB_EVENT_MASK_KEY_PRESS |
  97. XCB_EVENT_MASK_KEY_RELEASE |
  98. XCB_EVENT_MASK_VISIBILITY_CHANGE |
  99. XCB_EVENT_MASK_STRUCTURE_NOTIFY;
  100. xcb_create_window(conn,
  101. XCB_COPY_FROM_PARENT,
  102. win, /* the window id */
  103. scr->root, /* parent == root */
  104. 0, 0,
  105. scr->width_in_pixels,
  106. scr->height_in_pixels, /* dimensions */
  107. 0, /* border = 0, we draw our own */
  108. XCB_WINDOW_CLASS_INPUT_OUTPUT,
  109. XCB_WINDOW_CLASS_COPY_FROM_PARENT, /* copy visual from parent */
  110. mask,
  111. values);
  112. /* Map the window (= make it visible) */
  113. xcb_map_window(conn, win);
  114. /* Raise window (put it on top) */
  115. values[0] = XCB_STACK_MODE_ABOVE;
  116. xcb_configure_window(conn, win, XCB_CONFIG_WINDOW_STACK_MODE, values);
  117. return win;
  118. }
  119. void dpms_turn_off_screen(xcb_connection_t *conn) {
  120. xcb_dpms_enable(conn);
  121. xcb_dpms_force_level(conn, XCB_DPMS_DPMS_MODE_OFF);
  122. xcb_flush(conn);
  123. }
  124. /*
  125. * Repeatedly tries to grab pointer and keyboard (up to 1000 times).
  126. *
  127. */
  128. void grab_pointer_and_keyboard(xcb_connection_t *conn, xcb_screen_t *screen, xcb_cursor_t cursor) {
  129. xcb_grab_pointer_cookie_t pcookie;
  130. xcb_grab_pointer_reply_t *preply;
  131. xcb_grab_keyboard_cookie_t kcookie;
  132. xcb_grab_keyboard_reply_t *kreply;
  133. int tries = 10000;
  134. while (tries-- > 0) {
  135. pcookie = xcb_grab_pointer(
  136. conn,
  137. false, /* get all pointer events specified by the following mask */
  138. screen->root, /* grab the root window */
  139. XCB_NONE, /* which events to let through */
  140. XCB_GRAB_MODE_ASYNC, /* pointer events should continue as normal */
  141. XCB_GRAB_MODE_ASYNC, /* keyboard mode */
  142. XCB_NONE, /* confine_to = in which window should the cursor stay */
  143. cursor, /* we change the cursor to whatever the user wanted */
  144. XCB_CURRENT_TIME
  145. );
  146. if ((preply = xcb_grab_pointer_reply(conn, pcookie, NULL)) &&
  147. preply->status == XCB_GRAB_STATUS_SUCCESS) {
  148. free(preply);
  149. break;
  150. }
  151. /* Make this quite a bit slower */
  152. usleep(50);
  153. }
  154. while (tries-- > 0) {
  155. kcookie = xcb_grab_keyboard(
  156. conn,
  157. true, /* report events */
  158. screen->root, /* grab the root window */
  159. XCB_CURRENT_TIME,
  160. XCB_GRAB_MODE_ASYNC, /* process events as normal, do not require sync */
  161. XCB_GRAB_MODE_ASYNC
  162. );
  163. if ((kreply = xcb_grab_keyboard_reply(conn, kcookie, NULL)) &&
  164. kreply->status == XCB_GRAB_STATUS_SUCCESS) {
  165. free(kreply);
  166. break;
  167. }
  168. /* Make this quite a bit slower */
  169. usleep(50);
  170. }
  171. if (tries <= 0)
  172. errx(EXIT_FAILURE, "Cannot grab pointer/keyboard");
  173. }
  174. xcb_cursor_t create_cursor(xcb_connection_t *conn, xcb_screen_t *screen, xcb_window_t win, int choice) {
  175. xcb_pixmap_t bitmap;
  176. xcb_pixmap_t mask;
  177. xcb_cursor_t cursor;
  178. unsigned char* curs_bits;
  179. unsigned char* mask_bits;
  180. int curs_w, curs_h;
  181. switch (choice) {
  182. case CURS_NONE:
  183. curs_bits = curs_invisible_bits;
  184. mask_bits = curs_invisible_bits;
  185. curs_w = curs_invisible_width;
  186. curs_h = curs_invisible_height;
  187. break;
  188. case CURS_WIN:
  189. curs_bits = curs_windows_bits;
  190. mask_bits = mask_windows_bits;
  191. curs_w = curs_windows_width;
  192. curs_h = curs_windows_height;
  193. break;
  194. case CURS_DEFAULT:
  195. default:
  196. return XCB_NONE; /* XCB_NONE is xcb's way of saying "don't change the cursor" */
  197. }
  198. bitmap = xcb_create_pixmap_from_bitmap_data(conn,
  199. win,
  200. curs_bits,
  201. curs_w,
  202. curs_h,
  203. 1,
  204. screen->white_pixel,
  205. screen->black_pixel,
  206. NULL);
  207. mask = xcb_create_pixmap_from_bitmap_data(conn,
  208. win,
  209. mask_bits,
  210. curs_w,
  211. curs_h,
  212. 1,
  213. screen->white_pixel,
  214. screen->black_pixel,
  215. NULL);
  216. cursor = xcb_generate_id(conn);
  217. xcb_create_cursor(conn,
  218. cursor,
  219. bitmap,
  220. mask,
  221. 65535,65535,65535,
  222. 0,0,0,
  223. 0,0);
  224. xcb_free_pixmap(conn, bitmap);
  225. xcb_free_pixmap(conn, mask);
  226. return cursor;
  227. }