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.

MonitorIndicesParameter.cxx 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* Copyright 2021 Hugo Lundin <huglu@cendio.se> for Cendio AB.
  2. * Copyright 2021 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <algorithm>
  23. #include <vector>
  24. #include <string>
  25. #include <limits>
  26. #include <set>
  27. #include <stdlib.h>
  28. #include <stdexcept>
  29. #include "i18n.h"
  30. #include <FL/Fl.H>
  31. #include <rfb/LogWriter.h>
  32. #include "MonitorIndicesParameter.h"
  33. using namespace rfb;
  34. static LogWriter vlog("MonitorIndicesParameter");
  35. MonitorIndicesParameter::MonitorIndicesParameter(const char* name_, const char* desc_, const char* v)
  36. : StringParameter(name_, desc_, v) {}
  37. std::set<int> MonitorIndicesParameter::getParam()
  38. {
  39. bool valid = false;
  40. std::set<int> indices;
  41. std::set<int> configIndices;
  42. std::vector<MonitorIndicesParameter::Monitor> monitors = fetchMonitors();
  43. if (monitors.size() <= 0) {
  44. vlog.error(_("Failed to get system monitor configuration"));
  45. return indices;
  46. }
  47. valid = parseIndices(value, &configIndices);
  48. if (!valid) {
  49. return indices;
  50. }
  51. if (configIndices.size() <= 0) {
  52. return indices;
  53. }
  54. // Go through the monitors and see what indices are present in the config.
  55. for (int i = 0; i < ((int) monitors.size()); i++) {
  56. if (std::find(configIndices.begin(), configIndices.end(), i) != configIndices.end())
  57. indices.insert(monitors[i].fltkIndex);
  58. }
  59. return indices;
  60. }
  61. bool MonitorIndicesParameter::setParam(const char* value)
  62. {
  63. int index;
  64. std::set<int> indices;
  65. if (strlen(value) < 0)
  66. return false;
  67. if (!parseIndices(value, &indices, true)) {
  68. vlog.error(_("Invalid configuration specified for %s"), name);
  69. return false;
  70. }
  71. for (std::set<int>::iterator it = indices.begin(); it != indices.end(); it++) {
  72. index = *it + 1;
  73. if (index <= 0 || index > Fl::screen_count())
  74. vlog.error(_("Monitor index %d does not exist"), index);
  75. }
  76. return StringParameter::setParam(value);
  77. }
  78. bool MonitorIndicesParameter::setParam(std::set<int> indices)
  79. {
  80. static const int BUF_MAX_LEN = 1024;
  81. char buf[BUF_MAX_LEN] = {0};
  82. std::set<int> configIndices;
  83. std::vector<MonitorIndicesParameter::Monitor> monitors = fetchMonitors();
  84. if (monitors.size() <= 0) {
  85. vlog.error(_("Failed to get system monitor configuration"));
  86. // Don't return, store the configuration anyways.
  87. }
  88. for (int i = 0; i < ((int) monitors.size()); i++) {
  89. if (std::find(indices.begin(), indices.end(), monitors[i].fltkIndex) != indices.end())
  90. configIndices.insert(i);
  91. }
  92. int bytesWritten = 0;
  93. char const * separator = "";
  94. for (std::set<int>::iterator index = configIndices.begin();
  95. index != configIndices.end();
  96. index++)
  97. {
  98. bytesWritten += snprintf(
  99. buf+bytesWritten,
  100. BUF_MAX_LEN-bytesWritten,
  101. "%s%u",
  102. separator,
  103. (*index)+1
  104. );
  105. separator = ",";
  106. }
  107. return setParam(buf);
  108. }
  109. static bool parseNumber(std::string number, std::set<int> *indices)
  110. {
  111. if (number.size() <= 0)
  112. return false;
  113. int v = strtol(number.c_str(), NULL, 0);
  114. if (v <= 0)
  115. return false;
  116. if (v > INT_MAX)
  117. return false;
  118. indices->insert(v-1);
  119. return true;
  120. }
  121. bool MonitorIndicesParameter::parseIndices(const char* value,
  122. std::set<int> *indices,
  123. bool complain)
  124. {
  125. char d;
  126. std::string current;
  127. for (size_t i = 0; i < strlen(value); i++) {
  128. d = value[i];
  129. if (d == ' ')
  130. continue;
  131. else if (d >= '0' && d <= '9')
  132. current.push_back(d);
  133. else if (d == ',') {
  134. if (!parseNumber(current, indices)) {
  135. if (complain)
  136. vlog.error(_("Invalid monitor index '%s'"),
  137. current.c_str());
  138. return false;
  139. }
  140. current.clear();
  141. } else {
  142. if (complain)
  143. vlog.error(_("Unexpected character '%c'"), d);
  144. return false;
  145. }
  146. }
  147. // If we have nothing left to parse we are in a valid state.
  148. if (current.size() == 0)
  149. return true;
  150. // Parsing anything we have left.
  151. if (!parseNumber(current, indices)) {
  152. if (complain)
  153. vlog.error(_("Invalid monitor index '%s'"),
  154. current.c_str());
  155. return false;
  156. }
  157. return true;
  158. }
  159. std::vector<MonitorIndicesParameter::Monitor> MonitorIndicesParameter::fetchMonitors()
  160. {
  161. std::vector<Monitor> monitors;
  162. // Start by creating a struct for every monitor.
  163. for (int i = 0; i < Fl::screen_count(); i++) {
  164. Monitor monitor = {0};
  165. bool match;
  166. // Get the properties of the monitor at the current index;
  167. Fl::screen_xywh(
  168. monitor.x,
  169. monitor.y,
  170. monitor.w,
  171. monitor.h,
  172. i
  173. );
  174. // Only keep a single entry for mirrored screens
  175. match = false;
  176. for (int j = 0; j < ((int) monitors.size()); j++) {
  177. if (monitors[j].x != monitor.x)
  178. continue;
  179. if (monitors[j].y != monitor.y)
  180. continue;
  181. if (monitors[j].w != monitor.w)
  182. continue;
  183. if (monitors[j].h != monitor.h)
  184. continue;
  185. match = true;
  186. break;
  187. }
  188. if (match)
  189. continue;
  190. monitor.fltkIndex = i;
  191. monitors.push_back(monitor);
  192. }
  193. // Sort the monitors according to the specification in the vncviewer manual.
  194. qsort(&monitors[0], monitors.size(), sizeof(*(&monitors[0])), compare);
  195. return monitors;
  196. }
  197. int MonitorIndicesParameter::compare(const void *a, const void *b)
  198. {
  199. MonitorIndicesParameter::Monitor * monitor1 = (MonitorIndicesParameter::Monitor *) a;
  200. MonitorIndicesParameter::Monitor * monitor2 = (MonitorIndicesParameter::Monitor *) b;
  201. if (monitor1->x < monitor2->x)
  202. return -1;
  203. if (monitor1->x > monitor2->x)
  204. return 1;
  205. if (monitor1->y < monitor2->y)
  206. return -1;
  207. if (monitor1->y > monitor2->y)
  208. return 1;
  209. return 0;
  210. }