WmMultiHead.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2016 Matthew R. Trower
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. /*
  25. * Included Files:
  26. */
  27. #include <Dt/DtXinerama.h>
  28. #include "WmMultiHead.h"
  29. /*
  30. * Global Variables
  31. */
  32. DtXineramaInfo_t *DtXI = NULL;/* Xinerama data is static for life of X server */
  33. /*************************************<->*************************************
  34. *
  35. * GetHeadInfo (pcd)
  36. *
  37. *
  38. * Description:
  39. * -----------
  40. * Search for the head containing target client.
  41. *
  42. *
  43. * Inputs:
  44. * ------
  45. *
  46. *
  47. * Outputs:
  48. * -------
  49. * Return = head metrics on success, NULL on failure.
  50. * menuWidget, and menuButtons members.
  51. *
  52. *
  53. * Comments:
  54. * --------
  55. *
  56. * Can fail if:
  57. *
  58. * - MultiHead(eg. Xinerama) is not active
  59. * - Client does not fall within any existing head
  60. * - malloc error
  61. * - pcd is NULL
  62. *
  63. *************************************<->***********************************/
  64. WmHeadInfo_t *GetHeadInfo(const ClientData *pcd) {
  65. WmHeadInfo_t *WmHI = NULL;
  66. if (!DtXI)
  67. DtXI = _DtXineramaInit(DISPLAY);
  68. if (!pcd || !DtXI)
  69. return NULL;
  70. if (!(WmHI = (WmHeadInfo_t *)malloc(sizeof(WmHeadInfo_t)))) {
  71. #ifdef DEBUG
  72. fprintf(stderr, "(dtwm) _GetScreenInfo: malloc failed\n");
  73. #endif
  74. free(DtXI);
  75. return NULL;
  76. }
  77. /*
  78. * TODO
  79. *
  80. * DtXineramaInfo_t uses unsigned ints
  81. * XineramaScreenInfo uses shorts(?)
  82. * ClientData uses ints
  83. * FrameToClient and friends use a mixture (!)
  84. *
  85. * Explicit casting would shut the compiler up, but wouldn't change the
  86. * fundamental fact that we can't agree on coordinate types.
  87. */
  88. int idx = 0;
  89. while (_DtXineramaGetScreen(DtXI, idx++,
  90. &WmHI->width, &WmHI->height, &WmHI->x_org, &WmHI->y_org)) {
  91. if (pcd->clientX >= WmHI->x_org &&
  92. pcd->clientY >= WmHI->y_org &&
  93. pcd->clientX <= WmHI->x_org + WmHI->width &&
  94. pcd->clientY <= WmHI->y_org + WmHI->height)
  95. return WmHI;
  96. }
  97. free(WmHI);
  98. /* No valid screen */
  99. return NULL;
  100. }
  101. /*************************************<->*************************************
  102. *
  103. * GetHeadInfoById (id)
  104. *
  105. *
  106. * Description:
  107. * -----------
  108. * Search for the head by ID.
  109. *
  110. *
  111. * Inputs:
  112. * ------
  113. *
  114. *
  115. * Outputs:
  116. * -------
  117. * Return = head metrics on success, NULL on failure.
  118. *
  119. *
  120. * Comments:
  121. * --------
  122. *
  123. * Can fail if:
  124. *
  125. * - MultiHead(eg. Xinerama) is not active
  126. * - malloc error
  127. * - id is less than 0
  128. *
  129. *************************************<->***********************************/
  130. WmHeadInfo_t *GetHeadInfoById(int id) {
  131. WmHeadInfo_t *WmHI = NULL;
  132. if (!DtXI)
  133. DtXI = _DtXineramaInit(DISPLAY);
  134. if (id < 0 || !DtXI)
  135. return NULL;
  136. if (!(WmHI = (WmHeadInfo_t *)malloc(sizeof(WmHeadInfo_t)))) {
  137. #ifdef DEBUG
  138. fprintf(stderr, "(dtwm) _GetHeadInfoById: malloc failed\n");
  139. #endif
  140. free(DtXI);
  141. return NULL;
  142. }
  143. if (_DtXineramaGetScreen(DtXI, id,
  144. &WmHI->width, &WmHI->height, &WmHI->x_org, &WmHI->y_org))
  145. return WmHI;
  146. free(WmHI);
  147. return NULL;
  148. }