1
0

xhelpers.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. int randr_screen_count(xcb_connection_t *xcb)
  86. {
  87. if (!xcb)
  88. return 0;
  89. xcb_screen_t *screen;
  90. screen = xcb_setup_roots_iterator(xcb_get_setup(xcb)).data;
  91. xcb_randr_get_screen_resources_cookie_t res_c;
  92. xcb_randr_get_screen_resources_reply_t *res_r;
  93. res_c = xcb_randr_get_screen_resources(xcb, screen->root);
  94. res_r = xcb_randr_get_screen_resources_reply(xcb, res_c, 0);
  95. if (!res_r)
  96. return 0;
  97. return xcb_randr_get_screen_resources_crtcs_length(res_r);
  98. }
  99. int randr_screen_geo(xcb_connection_t *xcb, int_fast32_t screen,
  100. int_fast32_t *x, int_fast32_t *y, int_fast32_t *w,
  101. int_fast32_t *h, xcb_screen_t **rscreen)
  102. {
  103. xcb_screen_t *xscreen;
  104. xscreen = xcb_setup_roots_iterator(xcb_get_setup(xcb)).data;
  105. xcb_randr_get_screen_resources_cookie_t res_c;
  106. xcb_randr_get_screen_resources_reply_t *res_r;
  107. res_c = xcb_randr_get_screen_resources(xcb, xscreen->root);
  108. res_r = xcb_randr_get_screen_resources_reply(xcb, res_c, 0);
  109. if (!res_r)
  110. goto fail;
  111. int screens = xcb_randr_get_screen_resources_crtcs_length(res_r);
  112. if (screen < 0 || screen >= screens)
  113. goto fail;
  114. xcb_randr_crtc_t *crtc = xcb_randr_get_screen_resources_crtcs(res_r);
  115. xcb_randr_get_crtc_info_cookie_t crtc_c;
  116. xcb_randr_get_crtc_info_reply_t *crtc_r;
  117. crtc_c = xcb_randr_get_crtc_info(xcb, *(crtc + screen), 0);
  118. crtc_r = xcb_randr_get_crtc_info_reply(xcb, crtc_c, 0);
  119. if (!crtc_r)
  120. goto fail;
  121. *x = crtc_r->x;
  122. *y = crtc_r->y;
  123. *w = crtc_r->width;
  124. *h = crtc_r->height;
  125. if (rscreen)
  126. *rscreen = xscreen;
  127. return 0;
  128. fail:
  129. *x = *y = *w = *h = 0;
  130. return -1;
  131. }
  132. int x11_screen_geo(xcb_connection_t *xcb, int_fast32_t screen, int_fast32_t *w,
  133. int_fast32_t *h)
  134. {
  135. if (!xcb)
  136. goto fail;
  137. bool success = false;
  138. xcb_screen_iterator_t iter;
  139. iter = xcb_setup_roots_iterator(xcb_get_setup(xcb));
  140. for (; iter.rem; --screen, xcb_screen_next(&iter)) {
  141. if (!screen) {
  142. *w = iter.data->width_in_pixels;
  143. *h = iter.data->height_in_pixels;
  144. success = true;
  145. }
  146. }
  147. if (success)
  148. return 0;
  149. fail:
  150. *w = *h = 0;
  151. return -1;
  152. }
  153. xcb_shm_t *xshm_xcb_attach(xcb_connection_t *xcb, const int w, const int h)
  154. {
  155. if (!xcb)
  156. return NULL;
  157. xcb_shm_t *shm = bzalloc(sizeof(xcb_shm_t));
  158. shm->xcb = xcb;
  159. shm->seg = xcb_generate_id(shm->xcb);
  160. shm->shmid = shmget(IPC_PRIVATE, w * h * 4, IPC_CREAT | 0777);
  161. if (shm->shmid == -1)
  162. goto fail;
  163. xcb_shm_attach(shm->xcb, shm->seg, shm->shmid, false);
  164. shm->data = shmat(shm->shmid, NULL, 0);
  165. return shm;
  166. fail:
  167. xshm_xcb_detach(shm);
  168. return NULL;
  169. }
  170. void xshm_xcb_detach(xcb_shm_t *shm)
  171. {
  172. if (!shm)
  173. return;
  174. xcb_shm_detach(shm->xcb, shm->seg);
  175. if ((char *)shm->data != (char *)-1)
  176. shmdt(shm->data);
  177. if (shm->shmid != -1)
  178. shmctl(shm->shmid, IPC_RMID, NULL);
  179. bfree(shm);
  180. }
  181. xcb_screen_t *xcb_get_screen(xcb_connection_t *xcb, int screen)
  182. {
  183. xcb_screen_iterator_t iter;
  184. iter = xcb_setup_roots_iterator(xcb_get_setup(xcb));
  185. for (; iter.rem; --screen, xcb_screen_next(&iter)) {
  186. if (screen == 0)
  187. return iter.data;
  188. }
  189. return NULL;
  190. }