xhelpers.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/xcb.h>
  17. #include <xcb/xinerama.h>
  18. #include "xhelpers.h"
  19. bool xinerama_is_active(xcb_connection_t *xcb)
  20. {
  21. if (!xcb || !xcb_get_extension_data(xcb, &xcb_xinerama_id)->present)
  22. return false;
  23. bool active = true;
  24. xcb_xinerama_is_active_cookie_t xnr_c;
  25. xcb_xinerama_is_active_reply_t *xnr_r;
  26. xnr_c = xcb_xinerama_is_active_unchecked(xcb);
  27. xnr_r = xcb_xinerama_is_active_reply(xcb, xnr_c, NULL);
  28. if (!xnr_r || xnr_r->state == 0)
  29. active = false;
  30. free(xnr_r);
  31. return active;
  32. }
  33. int xinerama_screen_count(xcb_connection_t *xcb)
  34. {
  35. if (!xcb)
  36. return 0;
  37. int screens = 0;
  38. xcb_xinerama_query_screens_cookie_t scr_c;
  39. xcb_xinerama_query_screens_reply_t *scr_r;
  40. scr_c = xcb_xinerama_query_screens_unchecked(xcb);
  41. scr_r = xcb_xinerama_query_screens_reply(xcb, scr_c, NULL);
  42. if (scr_r)
  43. screens = scr_r->number;
  44. free(scr_r);
  45. return screens;
  46. }
  47. int xinerama_screen_geo(xcb_connection_t *xcb, int_fast32_t screen,
  48. int_fast32_t *x, int_fast32_t *y,
  49. int_fast32_t *w, 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. int x11_screen_geo(xcb_connection_t *xcb, int_fast32_t screen,
  79. int_fast32_t *w, int_fast32_t *h)
  80. {
  81. if (!xcb)
  82. goto fail;
  83. bool success = false;
  84. xcb_screen_iterator_t iter;
  85. iter = xcb_setup_roots_iterator(xcb_get_setup(xcb));
  86. for (; iter.rem; --screen, xcb_screen_next(&iter)) {
  87. if (!screen) {
  88. *w = iter.data->width_in_pixels;
  89. *h = iter.data->height_in_pixels;
  90. success = true;
  91. }
  92. }
  93. if (success)
  94. return 0;
  95. fail:
  96. *w = *h = 0;
  97. return -1;
  98. }
  99. xcb_shm_t* xshm_xcb_attach(xcb_connection_t *xcb, const int w, const int h)
  100. {
  101. if (!xcb)
  102. return NULL;
  103. xcb_shm_t *shm = bzalloc(sizeof(xcb_shm_t));
  104. shm->xcb = xcb;
  105. shm->seg = xcb_generate_id(shm->xcb);
  106. shm->shmid = shmget(IPC_PRIVATE, w * h * 4, IPC_CREAT | 0777);
  107. if (shm->shmid == -1)
  108. goto fail;
  109. xcb_shm_attach(shm->xcb, shm->seg, shm->shmid, false);
  110. shm->data = shmat(shm->shmid, NULL, 0);
  111. return shm;
  112. fail:
  113. xshm_xcb_detach(shm);
  114. return NULL;
  115. }
  116. void xshm_xcb_detach(xcb_shm_t *shm)
  117. {
  118. if (!shm)
  119. return;
  120. xcb_shm_detach(shm->xcb, shm->seg);
  121. if ((char *) shm->data != (char *) -1)
  122. shmdt(shm->data);
  123. if (shm->shmid != -1)
  124. shmctl(shm->shmid, IPC_RMID, NULL);
  125. bfree(shm);
  126. }
  127. xcb_screen_t *xcb_get_screen(xcb_connection_t *xcb, int screen)
  128. {
  129. xcb_screen_iterator_t iter;
  130. iter = xcb_setup_roots_iterator(xcb_get_setup(xcb));
  131. for (; iter.rem; --screen, xcb_screen_next(&iter)) {
  132. if (screen == 0)
  133. return iter.data;
  134. }
  135. return NULL;
  136. }