You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.3 KiB

  1. /*
  2. * vim:ts=4:sw=4:expandtab
  3. *
  4. * © 2010-2012 Michael Stapelberg
  5. *
  6. * See LICENSE for licensing information
  7. *
  8. */
  9. #include <stdbool.h>
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <math.h>
  13. #include <xcb/xcb.h>
  14. #include <xcb/xinerama.h>
  15. #include "xcb.h"
  16. #include "xinerama.h"
  17. /* Number of Xinerama screens which are currently present. */
  18. int xr_screens = 0;
  19. /* The resolutions of the currently present Xinerama screens. */
  20. Rect *xr_resolutions;
  21. static bool xinerama_active;
  22. void xinerama_init() {
  23. if (!xcb_get_extension_data(conn, &xcb_xinerama_id)->present) {
  24. printf("Xinerama extension not found, disabling.\n");
  25. return;
  26. }
  27. xcb_xinerama_is_active_cookie_t cookie;
  28. xcb_xinerama_is_active_reply_t *reply;
  29. cookie = xcb_xinerama_is_active(conn);
  30. reply = xcb_xinerama_is_active_reply(conn, cookie, NULL);
  31. if (!reply)
  32. return;
  33. if (!reply->state) {
  34. free(reply);
  35. return;
  36. }
  37. xinerama_active = true;
  38. }
  39. void xinerama_query_screens() {
  40. if (!xinerama_active)
  41. return;
  42. xcb_xinerama_query_screens_cookie_t cookie;
  43. xcb_xinerama_query_screens_reply_t *reply;
  44. xcb_xinerama_screen_info_t *screen_info;
  45. cookie = xcb_xinerama_query_screens_unchecked(conn);
  46. reply = xcb_xinerama_query_screens_reply(conn, cookie, NULL);
  47. if (!reply) {
  48. fprintf(stderr, "Couldn't get Xinerama screens\n");
  49. return;
  50. }
  51. screen_info = xcb_xinerama_query_screens_screen_info(reply);
  52. int screens = xcb_xinerama_query_screens_screen_info_length(reply);
  53. Rect *resolutions = malloc(screens * sizeof(Rect));
  54. /* No memory? Just keep on using the old information. */
  55. if (!resolutions) {
  56. free(reply);
  57. return;
  58. }
  59. xr_resolutions = resolutions;
  60. xr_screens = screens;
  61. for (int screen = 0; screen < xr_screens; screen++) {
  62. xr_resolutions[screen].x = screen_info[screen].x_org;
  63. xr_resolutions[screen].y = screen_info[screen].y_org;
  64. xr_resolutions[screen].width = screen_info[screen].width;
  65. xr_resolutions[screen].height = screen_info[screen].height;
  66. printf("found Xinerama screen: %d x %d at %d x %d\n",
  67. screen_info[screen].width, screen_info[screen].height,
  68. screen_info[screen].x_org, screen_info[screen].y_org);
  69. }
  70. free(reply);
  71. }