xhelpers.c 5.5 KB

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