Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

MonitorIndicesParameter.cxx 6.2KB

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