xhelpers.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <X11/Xutil.h>
  17. #include <X11/extensions/Xinerama.h>
  18. #include "xhelpers.h"
  19. int_fast32_t xinerama_is_active(Display *dpy)
  20. {
  21. int minor, major;
  22. if (!dpy)
  23. return 0;
  24. if (!XineramaQueryVersion(dpy, &minor, &major))
  25. return 0;
  26. if (!XineramaIsActive(dpy))
  27. return 0;
  28. return 1;
  29. }
  30. int_fast32_t xinerama_screen_count(Display *dpy)
  31. {
  32. int screens;
  33. if (!dpy)
  34. return 0;
  35. XFree(XineramaQueryScreens(dpy, &screens));
  36. return screens;
  37. }
  38. int_fast32_t xinerama_screen_geo(Display *dpy, const int_fast32_t screen,
  39. int_fast32_t *x, int_fast32_t *y, int_fast32_t *w, int_fast32_t *h)
  40. {
  41. int screens;
  42. XineramaScreenInfo *info = NULL;
  43. if (!dpy)
  44. goto fail;
  45. info = XineramaQueryScreens(dpy, &screens);
  46. if (screen < 0 || screen >= screens)
  47. goto fail;
  48. *x = info[screen].x_org;
  49. *y = info[screen].y_org;
  50. *w = info[screen].width;
  51. *h = info[screen].height;
  52. XFree(info);
  53. return 0;
  54. fail:
  55. if (info)
  56. XFree(info);
  57. *x = *y = *w = *h = 0;
  58. return -1;
  59. }
  60. int_fast32_t x11_screen_geo(Display *dpy, const int_fast32_t screen,
  61. int_fast32_t *w, int_fast32_t *h)
  62. {
  63. Screen *scr;
  64. if (!dpy || screen < 0 || screen >= XScreenCount(dpy))
  65. goto fail;
  66. scr = XScreenOfDisplay(dpy, screen);
  67. if (!scr)
  68. goto fail;
  69. *w = XWidthOfScreen(scr);
  70. *h = XHeightOfScreen(scr);
  71. return 0;
  72. fail:
  73. *w = *h = 0;
  74. return -1;
  75. }
  76. xshm_t *xshm_attach(Display *dpy, Screen *screen,
  77. int_fast32_t w, int_fast32_t h)
  78. {
  79. if (!dpy || !screen)
  80. return NULL;
  81. xshm_t *xshm = bzalloc(sizeof(xshm_t));
  82. xshm->dpy = dpy;
  83. xshm->image = XShmCreateImage(xshm->dpy, DefaultVisualOfScreen(screen),
  84. DefaultDepthOfScreen(screen), ZPixmap, NULL, &xshm->info,
  85. w, h);
  86. if (!xshm->image)
  87. goto fail;
  88. xshm->info.shmid = shmget(IPC_PRIVATE,
  89. xshm->image->bytes_per_line * xshm->image->height,
  90. IPC_CREAT | 0700);
  91. if (xshm->info.shmid < 0)
  92. goto fail;
  93. xshm->info.shmaddr
  94. = xshm->image->data
  95. = (char *) shmat(xshm->info.shmid, 0, 0);
  96. if (xshm->info.shmaddr == (char *) -1)
  97. goto fail;
  98. xshm->info.readOnly = false;
  99. if (!XShmAttach(xshm->dpy, &xshm->info))
  100. goto fail;
  101. xshm->attached = true;
  102. return xshm;
  103. fail:
  104. xshm_detach(xshm);
  105. return NULL;
  106. }
  107. void xshm_detach(xshm_t *xshm)
  108. {
  109. if (!xshm)
  110. return;
  111. if (xshm->attached)
  112. XShmDetach(xshm->dpy, &xshm->info);
  113. if (xshm->info.shmaddr != (char *) -1)
  114. shmdt(xshm->info.shmaddr);
  115. if (xshm->info.shmid != -1)
  116. shmctl(xshm->info.shmid, IPC_RMID, NULL);
  117. if (xshm->image)
  118. XDestroyImage(xshm->image);
  119. bfree(xshm);
  120. }