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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.c_str(), &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 (!parseIndices(value, &indices, true)) {
  66. vlog.error(_("Invalid configuration specified for %s"), name);
  67. return false;
  68. }
  69. for (std::set<int>::iterator it = indices.begin(); it != indices.end(); it++) {
  70. index = *it + 1;
  71. if (index <= 0 || index > Fl::screen_count())
  72. vlog.error(_("Monitor index %d does not exist"), index);
  73. }
  74. return StringParameter::setParam(value);
  75. }
  76. bool MonitorIndicesParameter::setParam(std::set<int> indices)
  77. {
  78. static const int BUF_MAX_LEN = 1024;
  79. char buf[BUF_MAX_LEN] = {0};
  80. std::set<int> configIndices;
  81. std::vector<MonitorIndicesParameter::Monitor> monitors = fetchMonitors();
  82. if (monitors.size() <= 0) {
  83. vlog.error(_("Failed to get system monitor configuration"));
  84. // Don't return, store the configuration anyways.
  85. }
  86. for (int i = 0; i < ((int) monitors.size()); i++) {
  87. if (std::find(indices.begin(), indices.end(), monitors[i].fltkIndex) != indices.end())
  88. configIndices.insert(i);
  89. }
  90. int bytesWritten = 0;
  91. char const * separator = "";
  92. for (std::set<int>::iterator index = configIndices.begin();
  93. index != configIndices.end();
  94. index++)
  95. {
  96. bytesWritten += snprintf(
  97. buf+bytesWritten,
  98. BUF_MAX_LEN-bytesWritten,
  99. "%s%u",
  100. separator,
  101. (*index)+1
  102. );
  103. separator = ",";
  104. }
  105. return setParam(buf);
  106. }
  107. static bool parseNumber(std::string number, std::set<int> *indices)
  108. {
  109. if (number.size() <= 0)
  110. return false;
  111. int v = strtol(number.c_str(), NULL, 0);
  112. if (v <= 0)
  113. return false;
  114. if (v > INT_MAX)
  115. return false;
  116. indices->insert(v-1);
  117. return true;
  118. }
  119. bool MonitorIndicesParameter::parseIndices(const char* value,
  120. std::set<int> *indices,
  121. bool complain)
  122. {
  123. char d;
  124. std::string current;
  125. for (size_t i = 0; i < strlen(value); i++) {
  126. d = value[i];
  127. if (d == ' ')
  128. continue;
  129. else if (d >= '0' && d <= '9')
  130. current.push_back(d);
  131. else if (d == ',') {
  132. if (!parseNumber(current, indices)) {
  133. if (complain)
  134. vlog.error(_("Invalid monitor index '%s'"),
  135. current.c_str());
  136. return false;
  137. }
  138. current.clear();
  139. } else {
  140. if (complain)
  141. vlog.error(_("Unexpected character '%c'"), d);
  142. return false;
  143. }
  144. }
  145. // If we have nothing left to parse we are in a valid state.
  146. if (current.size() == 0)
  147. return true;
  148. // Parsing anything we have left.
  149. if (!parseNumber(current, indices)) {
  150. if (complain)
  151. vlog.error(_("Invalid monitor index '%s'"),
  152. current.c_str());
  153. return false;
  154. }
  155. return true;
  156. }
  157. std::vector<MonitorIndicesParameter::Monitor> MonitorIndicesParameter::fetchMonitors()
  158. {
  159. std::vector<Monitor> monitors;
  160. // Start by creating a struct for every monitor.
  161. for (int i = 0; i < Fl::screen_count(); i++) {
  162. Monitor monitor;
  163. bool match;
  164. // Get the properties of the monitor at the current index;
  165. Fl::screen_xywh(
  166. monitor.x,
  167. monitor.y,
  168. monitor.w,
  169. monitor.h,
  170. i
  171. );
  172. // Only keep a single entry for mirrored screens
  173. match = false;
  174. for (int j = 0; j < ((int) monitors.size()); j++) {
  175. if (monitors[j].x != monitor.x)
  176. continue;
  177. if (monitors[j].y != monitor.y)
  178. continue;
  179. if (monitors[j].w != monitor.w)
  180. continue;
  181. if (monitors[j].h != monitor.h)
  182. continue;
  183. match = true;
  184. break;
  185. }
  186. if (match)
  187. continue;
  188. monitor.fltkIndex = i;
  189. monitors.push_back(monitor);
  190. }
  191. // Sort the monitors according to the specification in the vncviewer manual.
  192. qsort(&monitors[0], monitors.size(), sizeof(*(&monitors[0])), compare);
  193. return monitors;
  194. }
  195. int MonitorIndicesParameter::compare(const void *a, const void *b)
  196. {
  197. MonitorIndicesParameter::Monitor * monitor1 = (MonitorIndicesParameter::Monitor *) a;
  198. MonitorIndicesParameter::Monitor * monitor2 = (MonitorIndicesParameter::Monitor *) b;
  199. if (monitor1->x < monitor2->x)
  200. return -1;
  201. if (monitor1->x > monitor2->x)
  202. return 1;
  203. if (monitor1->y < monitor2->y)
  204. return -1;
  205. if (monitor1->y > monitor2->y)
  206. return 1;
  207. return 0;
  208. }