xhelpers.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. Copyright (C) 2014 by Leonhard Oelke <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdint.h>
  15. #include <sys/shm.h>
  16. #include <xcb/randr.h>
  17. #include <xcb/xcb.h>
  18. #include <xcb/xinerama.h>
  19. #include "xhelpers.h"
  20. bool xinerama_is_active(xcb_connection_t *xcb)
  21. {
  22. if (!xcb || !xcb_get_extension_data(xcb, &xcb_xinerama_id)->present)
  23. return false;
  24. bool active = true;
  25. xcb_xinerama_is_active_cookie_t xnr_c;
  26. xcb_xinerama_is_active_reply_t *xnr_r;
  27. xnr_c = xcb_xinerama_is_active_unchecked(xcb);
  28. xnr_r = xcb_xinerama_is_active_reply(xcb, xnr_c, NULL);
  29. if (!xnr_r || xnr_r->state == 0)
  30. active = false;
  31. free(xnr_r);
  32. return active;
  33. }
  34. int xinerama_screen_count(xcb_connection_t *xcb)
  35. {
  36. if (!xcb)
  37. return 0;
  38. int screens = 0;
  39. xcb_xinerama_query_screens_cookie_t scr_c;
  40. xcb_xinerama_query_screens_reply_t *scr_r;
  41. scr_c = xcb_xinerama_query_screens_unchecked(xcb);
  42. scr_r = xcb_xinerama_query_screens_reply(xcb, scr_c, NULL);
  43. if (scr_r)
  44. screens = scr_r->number;
  45. free(scr_r);
  46. return screens;
  47. }
  48. int xinerama_screen_geo(xcb_connection_t *xcb, int_fast32_t screen,
  49. int_fast32_t *x, int_fast32_t *y, int_fast32_t *w,
  50. int_fast32_t *h)
  51. {
  52. if (!xcb)
  53. goto fail;
  54. bool success = false;
  55. xcb_xinerama_query_screens_cookie_t scr_c;
  56. xcb_xinerama_query_screens_reply_t *scr_r;
  57. xcb_xinerama_screen_info_iterator_t iter;
  58. scr_c = xcb_xinerama_query_screens_unchecked(xcb);
  59. scr_r = xcb_xinerama_query_screens_reply(xcb, scr_c, NULL);
  60. if (!scr_r)
  61. goto fail;
  62. iter = xcb_xinerama_query_screens_screen_info_iterator(scr_r);
  63. for (; iter.rem; --screen, xcb_xinerama_screen_info_next(&iter)) {
  64. if (!screen) {
  65. *x = iter.data->x_org;
  66. *y = iter.data->y_org;
  67. *w = iter.data->width;
  68. *h = iter.data->height;
  69. success = true;
  70. }
  71. }
  72. free(scr_r);
  73. if (success)
  74. return 0;
  75. fail:
  76. *x = *y = *w = *h = 0;
  77. return -1;
  78. }
  79. bool randr_is_active(xcb_connection_t *xcb)
  80. {
  81. if (!xcb || !xcb_get_extension_data(xcb, &xcb_randr_id)->present)
  82. return false;
  83. return true;
  84. }
  85. static bool randr_has_monitors(xcb_connection_t *xcb)
  86. {
  87. xcb_randr_query_version_cookie_t ver_c;
  88. xcb_randr_query_version_reply_t *ver_r;
  89. ver_c = xcb_randr_query_version(xcb, XCB_RANDR_MAJOR_VERSION,
  90. XCB_RANDR_MINOR_VERSION);
  91. ver_r = xcb_randr_query_version_reply(xcb, ver_c, 0);
  92. if (!ver_r)
  93. return 0;
  94. bool ret = ver_r->major_version > 1 || ver_r->minor_version >= 5;
  95. free(ver_r);
  96. return ret;
  97. }
  98. int randr_screen_count(xcb_connection_t *xcb)
  99. {
  100. if (!xcb)
  101. return 0;
  102. xcb_screen_t *screen;
  103. screen = xcb_setup_roots_iterator(xcb_get_setup(xcb)).data;
  104. if (randr_has_monitors(xcb)) {
  105. xcb_randr_get_monitors_cookie_t mon_c;
  106. xcb_randr_get_monitors_reply_t *mon_r;
  107. mon_c = xcb_randr_get_monitors(xcb, screen->root, true);
  108. mon_r = xcb_randr_get_monitors_reply(xcb, mon_c, 0);
  109. if (!mon_r)
  110. return 0;
  111. int count = xcb_randr_get_monitors_monitors_length(mon_r);
  112. free(mon_r);
  113. return count;
  114. }
  115. xcb_randr_get_screen_resources_cookie_t res_c;
  116. xcb_randr_get_screen_resources_reply_t *res_r;
  117. res_c = xcb_randr_get_screen_resources(xcb, screen->root);
  118. res_r = xcb_randr_get_screen_resources_reply(xcb, res_c, 0);
  119. if (!res_r)
  120. return 0;
  121. return xcb_randr_get_screen_resources_crtcs_length(res_r);
  122. }
  123. int randr_screen_geo(xcb_connection_t *xcb, int_fast32_t screen,
  124. int_fast32_t *x, int_fast32_t *y, int_fast32_t *w,
  125. int_fast32_t *h, xcb_screen_t **rscreen, char **name)
  126. {
  127. xcb_screen_t *xscreen;
  128. xscreen = xcb_setup_roots_iterator(xcb_get_setup(xcb)).data;
  129. if (randr_has_monitors(xcb)) {
  130. xcb_randr_get_monitors_cookie_t mon_c;
  131. xcb_randr_get_monitors_reply_t *mon_r;
  132. mon_c = xcb_randr_get_monitors(xcb, xscreen->root, true);
  133. mon_r = xcb_randr_get_monitors_reply(xcb, mon_c, 0);
  134. if (!mon_r)
  135. return 0;
  136. int monitors = xcb_randr_get_monitors_monitors_length(mon_r);
  137. if (screen < 0 || screen >= monitors) {
  138. free(mon_r);
  139. goto fail;
  140. }
  141. xcb_randr_monitor_info_iterator_t mon_i;
  142. mon_i = xcb_randr_get_monitors_monitors_iterator(mon_r);
  143. int s;
  144. for (s = 0; s < screen; s++)
  145. xcb_randr_monitor_info_next(&mon_i);
  146. xcb_randr_monitor_info_t *mon = mon_i.data;
  147. *x = mon->x;
  148. *y = mon->y;
  149. *w = mon->width;
  150. *h = mon->height;
  151. if (rscreen)
  152. *rscreen = xscreen;
  153. if (mon->name && name) {
  154. xcb_get_atom_name_cookie_t atom_c;
  155. xcb_get_atom_name_reply_t *atom_r;
  156. atom_c = xcb_get_atom_name(xcb, mon->name);
  157. atom_r = xcb_get_atom_name_reply(xcb, atom_c, 0);
  158. if (atom_r) {
  159. *name = strndup(
  160. xcb_get_atom_name_name(atom_r),
  161. xcb_get_atom_name_name_length(atom_r));
  162. free(atom_r);
  163. }
  164. }
  165. free(mon_r);
  166. return 0;
  167. }
  168. xcb_randr_get_screen_resources_cookie_t res_c;
  169. xcb_randr_get_screen_resources_reply_t *res_r;
  170. res_c = xcb_randr_get_screen_resources(xcb, xscreen->root);
  171. res_r = xcb_randr_get_screen_resources_reply(xcb, res_c, 0);
  172. if (!res_r)
  173. goto fail;
  174. int screens = xcb_randr_get_screen_resources_crtcs_length(res_r);
  175. if (screen < 0 || screen >= screens)
  176. goto fail;
  177. xcb_randr_crtc_t *crtc = xcb_randr_get_screen_resources_crtcs(res_r);
  178. xcb_randr_get_crtc_info_cookie_t crtc_c;
  179. xcb_randr_get_crtc_info_reply_t *crtc_r;
  180. crtc_c = xcb_randr_get_crtc_info(xcb, *(crtc + screen), 0);
  181. crtc_r = xcb_randr_get_crtc_info_reply(xcb, crtc_c, 0);
  182. if (!crtc_r)
  183. goto fail;
  184. *x = crtc_r->x;
  185. *y = crtc_r->y;
  186. *w = crtc_r->width;
  187. *h = crtc_r->height;
  188. if (rscreen)
  189. *rscreen = xscreen;
  190. return 0;
  191. fail:
  192. *x = *y = *w = *h = 0;
  193. return -1;
  194. }
  195. int x11_screen_geo(xcb_connection_t *xcb, int_fast32_t screen, int_fast32_t *w,
  196. int_fast32_t *h)
  197. {
  198. if (!xcb)
  199. goto fail;
  200. bool success = false;
  201. xcb_screen_iterator_t iter;
  202. iter = xcb_setup_roots_iterator(xcb_get_setup(xcb));
  203. for (; iter.rem; --screen, xcb_screen_next(&iter)) {
  204. if (!screen) {
  205. *w = iter.data->width_in_pixels;
  206. *h = iter.data->height_in_pixels;
  207. success = true;
  208. }
  209. }
  210. if (success)
  211. return 0;
  212. fail:
  213. *w = *h = 0;
  214. return -1;
  215. }
  216. xcb_shm_t *xshm_xcb_attach(xcb_connection_t *xcb, const int w, const int h)
  217. {
  218. if (!xcb)
  219. return NULL;
  220. xcb_shm_t *shm = bzalloc(sizeof(xcb_shm_t));
  221. shm->xcb = xcb;
  222. shm->seg = xcb_generate_id(shm->xcb);
  223. shm->shmid = shmget(IPC_PRIVATE, w * h * 4, IPC_CREAT | 0777);
  224. if (shm->shmid == -1)
  225. goto fail;
  226. xcb_shm_attach(shm->xcb, shm->seg, shm->shmid, false);
  227. shm->data = shmat(shm->shmid, NULL, 0);
  228. return shm;
  229. fail:
  230. xshm_xcb_detach(shm);
  231. return NULL;
  232. }
  233. void xshm_xcb_detach(xcb_shm_t *shm)
  234. {
  235. if (!shm)
  236. return;
  237. xcb_shm_detach(shm->xcb, shm->seg);
  238. if ((char *)shm->data != (char *)-1)
  239. shmdt(shm->data);
  240. if (shm->shmid != -1)
  241. shmctl(shm->shmid, IPC_RMID, NULL);
  242. bfree(shm);
  243. }
  244. xcb_screen_t *xcb_get_screen(xcb_connection_t *xcb, int screen)
  245. {
  246. xcb_screen_iterator_t iter;
  247. iter = xcb_setup_roots_iterator(xcb_get_setup(xcb));
  248. for (; iter.rem; --screen, xcb_screen_next(&iter)) {
  249. if (screen == 0)
  250. return iter.data;
  251. }
  252. return NULL;
  253. }
  254. int xcb_get_screen_for_root(xcb_connection_t *conn, xcb_window_t root)
  255. {
  256. xcb_screen_iterator_t iter =
  257. xcb_setup_roots_iterator(xcb_get_setup(conn));
  258. for (int i = 0; iter.rem > 0; xcb_screen_next(&iter)) {
  259. if (iter.data->root == root)
  260. return i;
  261. i++;
  262. }
  263. return 0;
  264. }