1
0

xhelpers.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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, int_fast32_t *x, int_fast32_t *y, int_fast32_t *w,
  49. int_fast32_t *h)
  50. {
  51. if (!xcb)
  52. goto fail;
  53. bool success = false;
  54. xcb_xinerama_query_screens_cookie_t scr_c;
  55. xcb_xinerama_query_screens_reply_t *scr_r;
  56. xcb_xinerama_screen_info_iterator_t iter;
  57. scr_c = xcb_xinerama_query_screens_unchecked(xcb);
  58. scr_r = xcb_xinerama_query_screens_reply(xcb, scr_c, NULL);
  59. if (!scr_r)
  60. goto fail;
  61. iter = xcb_xinerama_query_screens_screen_info_iterator(scr_r);
  62. for (; iter.rem; --screen, xcb_xinerama_screen_info_next(&iter)) {
  63. if (!screen) {
  64. *x = iter.data->x_org;
  65. *y = iter.data->y_org;
  66. *w = iter.data->width;
  67. *h = iter.data->height;
  68. success = true;
  69. }
  70. }
  71. free(scr_r);
  72. if (success)
  73. return 0;
  74. fail:
  75. *x = *y = *w = *h = 0;
  76. return -1;
  77. }
  78. bool randr_is_active(xcb_connection_t *xcb)
  79. {
  80. if (!xcb || !xcb_get_extension_data(xcb, &xcb_randr_id)->present)
  81. return false;
  82. return true;
  83. }
  84. static bool randr_has_monitors(xcb_connection_t *xcb)
  85. {
  86. xcb_randr_query_version_cookie_t ver_c;
  87. xcb_randr_query_version_reply_t *ver_r;
  88. ver_c = xcb_randr_query_version(xcb, XCB_RANDR_MAJOR_VERSION, XCB_RANDR_MINOR_VERSION);
  89. ver_r = xcb_randr_query_version_reply(xcb, ver_c, 0);
  90. if (!ver_r)
  91. return 0;
  92. bool ret = ver_r->major_version > 1 || ver_r->minor_version >= 5;
  93. free(ver_r);
  94. return ret;
  95. }
  96. int randr_screen_count(xcb_connection_t *xcb)
  97. {
  98. if (!xcb)
  99. return 0;
  100. xcb_screen_t *screen;
  101. screen = xcb_setup_roots_iterator(xcb_get_setup(xcb)).data;
  102. if (randr_has_monitors(xcb)) {
  103. xcb_randr_get_monitors_cookie_t mon_c;
  104. xcb_randr_get_monitors_reply_t *mon_r;
  105. mon_c = xcb_randr_get_monitors(xcb, screen->root, true);
  106. mon_r = xcb_randr_get_monitors_reply(xcb, mon_c, 0);
  107. if (!mon_r)
  108. return 0;
  109. int count = xcb_randr_get_monitors_monitors_length(mon_r);
  110. free(mon_r);
  111. return count;
  112. }
  113. xcb_randr_get_screen_resources_cookie_t res_c;
  114. xcb_randr_get_screen_resources_reply_t *res_r;
  115. res_c = xcb_randr_get_screen_resources(xcb, screen->root);
  116. res_r = xcb_randr_get_screen_resources_reply(xcb, res_c, 0);
  117. if (!res_r)
  118. return 0;
  119. return xcb_randr_get_screen_resources_crtcs_length(res_r);
  120. }
  121. int randr_screen_geo(xcb_connection_t *xcb, int_fast32_t screen, int_fast32_t *x, int_fast32_t *y, int_fast32_t *w,
  122. int_fast32_t *h, xcb_screen_t **rscreen, char **name)
  123. {
  124. xcb_screen_t *xscreen;
  125. xscreen = xcb_setup_roots_iterator(xcb_get_setup(xcb)).data;
  126. if (randr_has_monitors(xcb)) {
  127. xcb_randr_get_monitors_cookie_t mon_c;
  128. xcb_randr_get_monitors_reply_t *mon_r;
  129. mon_c = xcb_randr_get_monitors(xcb, xscreen->root, true);
  130. mon_r = xcb_randr_get_monitors_reply(xcb, mon_c, 0);
  131. if (!mon_r)
  132. return 0;
  133. int monitors = xcb_randr_get_monitors_monitors_length(mon_r);
  134. if (screen < 0 || screen >= monitors) {
  135. free(mon_r);
  136. goto fail;
  137. }
  138. xcb_randr_monitor_info_iterator_t mon_i;
  139. mon_i = xcb_randr_get_monitors_monitors_iterator(mon_r);
  140. int s;
  141. for (s = 0; s < screen; s++)
  142. xcb_randr_monitor_info_next(&mon_i);
  143. xcb_randr_monitor_info_t *mon = mon_i.data;
  144. *x = mon->x;
  145. *y = mon->y;
  146. *w = mon->width;
  147. *h = mon->height;
  148. if (rscreen)
  149. *rscreen = xscreen;
  150. if (mon->name && name) {
  151. xcb_get_atom_name_cookie_t atom_c;
  152. xcb_get_atom_name_reply_t *atom_r;
  153. atom_c = xcb_get_atom_name(xcb, mon->name);
  154. atom_r = xcb_get_atom_name_reply(xcb, atom_c, 0);
  155. if (atom_r) {
  156. *name = strndup(xcb_get_atom_name_name(atom_r), xcb_get_atom_name_name_length(atom_r));
  157. free(atom_r);
  158. }
  159. }
  160. free(mon_r);
  161. return 0;
  162. }
  163. xcb_randr_get_screen_resources_cookie_t res_c;
  164. xcb_randr_get_screen_resources_reply_t *res_r;
  165. res_c = xcb_randr_get_screen_resources(xcb, xscreen->root);
  166. res_r = xcb_randr_get_screen_resources_reply(xcb, res_c, 0);
  167. if (!res_r)
  168. goto fail;
  169. int screens = xcb_randr_get_screen_resources_crtcs_length(res_r);
  170. if (screen < 0 || screen >= screens)
  171. goto fail;
  172. xcb_randr_crtc_t *crtc = xcb_randr_get_screen_resources_crtcs(res_r);
  173. xcb_randr_get_crtc_info_cookie_t crtc_c;
  174. xcb_randr_get_crtc_info_reply_t *crtc_r;
  175. crtc_c = xcb_randr_get_crtc_info(xcb, *(crtc + screen), 0);
  176. crtc_r = xcb_randr_get_crtc_info_reply(xcb, crtc_c, 0);
  177. if (!crtc_r)
  178. goto fail;
  179. *x = crtc_r->x;
  180. *y = crtc_r->y;
  181. *w = crtc_r->width;
  182. *h = crtc_r->height;
  183. if (rscreen)
  184. *rscreen = xscreen;
  185. return 0;
  186. fail:
  187. *x = *y = *w = *h = 0;
  188. return -1;
  189. }
  190. int x11_screen_geo(xcb_connection_t *xcb, int_fast32_t screen, int_fast32_t *w, int_fast32_t *h)
  191. {
  192. if (!xcb)
  193. goto fail;
  194. bool success = false;
  195. xcb_screen_iterator_t iter;
  196. iter = xcb_setup_roots_iterator(xcb_get_setup(xcb));
  197. for (; iter.rem; --screen, xcb_screen_next(&iter)) {
  198. if (!screen) {
  199. *w = iter.data->width_in_pixels;
  200. *h = iter.data->height_in_pixels;
  201. success = true;
  202. }
  203. }
  204. if (success)
  205. return 0;
  206. fail:
  207. *w = *h = 0;
  208. return -1;
  209. }
  210. xcb_shm_t *xshm_xcb_attach(xcb_connection_t *xcb, const int w, const int h)
  211. {
  212. if (!xcb)
  213. return NULL;
  214. xcb_shm_t *shm = bzalloc(sizeof(xcb_shm_t));
  215. shm->xcb = xcb;
  216. shm->seg = xcb_generate_id(shm->xcb);
  217. shm->shmid = shmget(IPC_PRIVATE, w * h * 4, IPC_CREAT | 0777);
  218. if (shm->shmid == -1)
  219. goto fail;
  220. xcb_shm_attach(shm->xcb, shm->seg, shm->shmid, false);
  221. shm->data = shmat(shm->shmid, NULL, 0);
  222. return shm;
  223. fail:
  224. xshm_xcb_detach(shm);
  225. return NULL;
  226. }
  227. void xshm_xcb_detach(xcb_shm_t *shm)
  228. {
  229. if (!shm)
  230. return;
  231. xcb_shm_detach(shm->xcb, shm->seg);
  232. if ((char *)shm->data != (char *)-1)
  233. shmdt(shm->data);
  234. if (shm->shmid != -1)
  235. shmctl(shm->shmid, IPC_RMID, NULL);
  236. bfree(shm);
  237. }
  238. xcb_screen_t *xcb_get_screen(xcb_connection_t *xcb, int screen)
  239. {
  240. xcb_screen_iterator_t iter;
  241. iter = xcb_setup_roots_iterator(xcb_get_setup(xcb));
  242. for (; iter.rem; --screen, xcb_screen_next(&iter)) {
  243. if (screen == 0)
  244. return iter.data;
  245. }
  246. return NULL;
  247. }
  248. int xcb_get_screen_for_root(xcb_connection_t *conn, xcb_window_t root)
  249. {
  250. xcb_screen_iterator_t iter = xcb_setup_roots_iterator(xcb_get_setup(conn));
  251. for (int i = 0; iter.rem > 0; xcb_screen_next(&iter)) {
  252. if (iter.data->root == root)
  253. return i;
  254. i++;
  255. }
  256. return 0;
  257. }