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

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