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.

Fl_Monitor_Arrangement.cxx 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /* Copyright 2021 Hugo Lundin <huglu@cendio.se> for Cendio AB.
  2. * Copyright 2021 Pierre Ossman for Cendio AB
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. #include <set>
  28. #include <vector>
  29. #include <string>
  30. #include <utility>
  31. #include <sstream>
  32. #include <assert.h>
  33. #include <algorithm>
  34. #include <FL/Fl.H>
  35. #include <FL/fl_draw.H>
  36. #include <FL/Fl_Button.H>
  37. #include <FL/x.H>
  38. #if defined(HAVE_XRANDR) && !defined(__APPLE__)
  39. #include <X11/extensions/Xrandr.h>
  40. #include <X11/X.h>
  41. #include <X11/Xlib.h>
  42. #include <X11/Xutil.h>
  43. #endif
  44. #ifdef __APPLE__
  45. #include <Carbon/Carbon.h>
  46. #include <IOKit/graphics/IOGraphicsLib.h>
  47. #include <IOKit/hidsystem/IOHIDLib.h>
  48. #include <IOKit/hidsystem/IOHIDParameter.h>
  49. #endif
  50. #include "Fl_Monitor_Arrangement.h"
  51. static std::set<Fl_Monitor_Arrangement *> instances;
  52. static const Fl_Boxtype FL_CHECKERED_BOX = FL_FREE_BOXTYPE;
  53. Fl_Monitor_Arrangement::Fl_Monitor_Arrangement(
  54. int x, int y, int w, int h)
  55. : Fl_Group(x, y, w, h),
  56. SELECTION_COLOR(fl_lighter(FL_BLUE)),
  57. AVAILABLE_COLOR(fl_lighter(fl_lighter(fl_lighter(FL_BACKGROUND_COLOR))))
  58. {
  59. // Used for required monitors.
  60. Fl::set_boxtype(FL_CHECKERED_BOX, checkered_pattern_draw, 0, 0, 0, 0);
  61. if (instances.size() == 0)
  62. Fl::add_handler(fltk_event_handler);
  63. instances.insert(this);
  64. box(FL_DOWN_BOX);
  65. color(fl_lighter(FL_BACKGROUND_COLOR));
  66. layout();
  67. end();
  68. }
  69. Fl_Monitor_Arrangement::~Fl_Monitor_Arrangement()
  70. {
  71. instances.erase(this);
  72. if (instances.size() == 0)
  73. Fl::remove_handler(fltk_event_handler);
  74. }
  75. std::set<int> Fl_Monitor_Arrangement::value() const
  76. {
  77. std::set<int> indices;
  78. MonitorMap::const_iterator iter;
  79. for (iter = monitors.begin(); iter != monitors.end(); ++iter) {
  80. if (iter->second->value() == 1)
  81. indices.insert(iter->first);
  82. }
  83. return indices;
  84. }
  85. int Fl_Monitor_Arrangement::value(std::set<int> indices)
  86. {
  87. MonitorMap::const_iterator iter;
  88. bool changed;
  89. changed = false;
  90. for (iter = monitors.begin(); iter != monitors.end(); ++iter) {
  91. bool selected = std::find(indices.begin(), indices.end(),
  92. iter->first) != indices.end();
  93. if (iter->second->value() != selected)
  94. changed = true;
  95. iter->second->value(selected);
  96. }
  97. return changed;
  98. }
  99. void Fl_Monitor_Arrangement::draw()
  100. {
  101. MonitorMap::const_iterator iter;
  102. for (iter = monitors.begin(); iter != monitors.end(); ++iter) {
  103. Fl_Button * monitor = iter->second;
  104. if (is_required(iter->first)) {
  105. monitor->box(FL_CHECKERED_BOX);
  106. monitor->color(SELECTION_COLOR);
  107. } else {
  108. monitor->box(FL_BORDER_BOX);
  109. monitor->color(AVAILABLE_COLOR);
  110. monitor->selection_color(SELECTION_COLOR);
  111. }
  112. }
  113. Fl_Group::draw();
  114. }
  115. void Fl_Monitor_Arrangement::layout()
  116. {
  117. int x, y, w, h;
  118. double scale = this->scale();
  119. const double MARGIN_SCALE_FACTOR = 0.99;
  120. std::pair<int, int> offset = this->offset();
  121. for (int i = 0; i < Fl::screen_count(); i++) {
  122. bool match;
  123. Fl::screen_xywh(x, y, w, h, i);
  124. // Only keep a single entry for mirrored screens
  125. match = false;
  126. for (int j = 0; j < i; j++) {
  127. int x2, y2, w2, h2;
  128. Fl::screen_xywh(x2, y2, w2, h2, j);
  129. if ((x != x2) || (y != y2) || (w != w2) || (h != h2))
  130. continue;
  131. match = true;
  132. break;
  133. }
  134. if (match)
  135. continue;
  136. Fl_Button *monitor = new Fl_Button(
  137. /* x = */ this->x() + offset.first + x*scale + (1 - MARGIN_SCALE_FACTOR)*x*scale,
  138. /* y = */ this->y() + offset.second + y*scale + (1 - MARGIN_SCALE_FACTOR)*y*scale,
  139. /* w = */ w*scale*MARGIN_SCALE_FACTOR,
  140. /* h = */ h*scale*MARGIN_SCALE_FACTOR
  141. );
  142. monitor->clear_visible_focus();
  143. monitor->callback(monitor_pressed, this);
  144. monitor->type(FL_TOGGLE_BUTTON);
  145. monitor->when(FL_WHEN_CHANGED);
  146. monitor->copy_tooltip(description(i).c_str());
  147. monitors[i] = monitor;
  148. }
  149. }
  150. void Fl_Monitor_Arrangement::refresh()
  151. {
  152. // The selection state is only saved persistently when "OK" is
  153. // pressed. We need to manually restore the current selection
  154. // when the widget is refreshed.
  155. std::set<int> indices = value();
  156. monitors.clear();
  157. // FLTK recursively deletes all children for us.
  158. clear();
  159. begin();
  160. layout();
  161. end();
  162. // Restore the current selection state.
  163. value(indices);
  164. redraw();
  165. }
  166. bool Fl_Monitor_Arrangement::is_required(int m)
  167. {
  168. // A selected monitor is never required.
  169. if (monitors[m]->value() == 1)
  170. return false;
  171. // If no monitors are selected, none are required.
  172. std::set<int> selected = value();
  173. if (selected.size() <= 0)
  174. return false;
  175. // Go through all selected monitors and find the monitor
  176. // indices that bounds the fullscreen frame buffer. If
  177. // the given monitor's coordinates are inside the bounds,
  178. // while not being selected, it is instead required.
  179. int x, y, w, h;
  180. int top_y, bottom_y, left_x, right_x;
  181. std::set<int>::iterator it = selected.begin();
  182. // Base the rest of the calculations on the dimensions
  183. // obtained for the first monitor.
  184. Fl::screen_xywh(x, y, w, h, *it);
  185. top_y = y;
  186. bottom_y = y + h;
  187. left_x = x;
  188. right_x = x + w;
  189. // Go through the rest of the monitors,
  190. // exhausting the rest of the iterator.
  191. for (; it != selected.end(); it++) {
  192. Fl::screen_xywh(x, y, w, h, *it);
  193. if (y < top_y) {
  194. top_y = y;
  195. }
  196. if ((y + h) > bottom_y) {
  197. bottom_y = y + h;
  198. }
  199. if (x < left_x) {
  200. left_x = x;
  201. }
  202. if ((x + w) > right_x) {
  203. right_x = x + w;
  204. }
  205. }
  206. Fl::screen_xywh(x, y, w, h, m);
  207. if (x < left_x)
  208. return false;
  209. if ((x + w) > right_x)
  210. return false;
  211. if (y < top_y)
  212. return false;
  213. if ((y + h) > bottom_y)
  214. return false;
  215. return true;
  216. }
  217. double Fl_Monitor_Arrangement::scale()
  218. {
  219. const int MARGIN = 20;
  220. std::pair<int, int> size = this->size();
  221. double s_w = static_cast<double>(this->w()-MARGIN) / static_cast<double>(size.first);
  222. double s_h = static_cast<double>(this->h()-MARGIN) / static_cast<double>(size.second);
  223. // Choose the one that scales the least, in order to
  224. // maximize our use of the given bounding area.
  225. if (s_w > s_h)
  226. return s_h;
  227. else
  228. return s_w;
  229. }
  230. std::pair<int, int> Fl_Monitor_Arrangement::size()
  231. {
  232. int x, y, w, h;
  233. int top, bottom, left, right;
  234. int x_min, x_max, y_min, y_max;
  235. x_min = x_max = y_min = y_max = 0;
  236. for (int i = 0; i < Fl::screen_count(); i++) {
  237. Fl::screen_xywh(x, y, w, h, i);
  238. top = y;
  239. bottom = y + h;
  240. left = x;
  241. right = x + w;
  242. if (top < y_min)
  243. y_min = top;
  244. if (bottom > y_max)
  245. y_max = bottom;
  246. if (left < x_min)
  247. x_min = left;
  248. if (right > x_max)
  249. x_max = right;
  250. }
  251. return std::make_pair(x_max - x_min, y_max - y_min);
  252. }
  253. std::pair<int, int> Fl_Monitor_Arrangement::offset()
  254. {
  255. double scale = this->scale();
  256. std::pair<int, int> size = this->size();
  257. std::pair<int, int> origin = this->origin();
  258. int offset_x = (this->w()/2) - (size.first/2 * scale);
  259. int offset_y = (this->h()/2) - (size.second/2 * scale);
  260. return std::make_pair(offset_x + abs(origin.first)*scale, offset_y + abs(origin.second)*scale);
  261. }
  262. std::pair<int, int> Fl_Monitor_Arrangement::origin()
  263. {
  264. int x, y, w, h, ox, oy;
  265. ox = oy = 0;
  266. for (int i = 0; i < Fl::screen_count(); i++) {
  267. Fl::screen_xywh(x, y, w, h, i);
  268. if (x < ox)
  269. ox = x;
  270. if (y < oy)
  271. oy = y;
  272. }
  273. return std::make_pair(ox, oy);
  274. }
  275. std::string Fl_Monitor_Arrangement::description(int m)
  276. {
  277. std::string name;
  278. int x, y, w, h;
  279. std::stringstream ss;
  280. assert(m < Fl::screen_count());
  281. name = get_monitor_name(m);
  282. Fl::screen_xywh(x, y, w, h, m);
  283. if (!name.empty())
  284. ss << name << " (" << w << "x" << h << ")";
  285. else
  286. ss << w << "x" << h;
  287. return ss.str();
  288. }
  289. #if defined(WIN32)
  290. static BOOL CALLBACK EnumDisplayMonitorsCallback(
  291. HMONITOR monitor, HDC /*deviceContext*/, LPRECT /*rect*/,
  292. LPARAM userData)
  293. {
  294. std::set<HMONITOR>* sys_monitors = (std::set<HMONITOR>*)userData;
  295. sys_monitors->insert(monitor);
  296. return TRUE;
  297. }
  298. #endif
  299. std::string Fl_Monitor_Arrangement::get_monitor_name(int m)
  300. {
  301. #if defined(WIN32)
  302. std::set<HMONITOR> sys_monitors;
  303. std::set<HMONITOR>::const_iterator iter;
  304. int x, y, w, h;
  305. Fl::screen_xywh(x, y, w, h, m);
  306. EnumDisplayMonitors(NULL, NULL, EnumDisplayMonitorsCallback,
  307. (LPARAM)&sys_monitors);
  308. for (iter = sys_monitors.begin(); iter != sys_monitors.end(); ++iter) {
  309. MONITORINFOEX info;
  310. DISPLAY_DEVICE dev;
  311. std::string name;
  312. info.cbSize = sizeof(info);
  313. GetMonitorInfo(*iter, (LPMONITORINFO)&info);
  314. if (info.rcMonitor.left != x)
  315. continue;
  316. if (info.rcMonitor.top != y)
  317. continue;
  318. if ((info.rcMonitor.right - info.rcMonitor.left) != w)
  319. continue;
  320. if ((info.rcMonitor.bottom - info.rcMonitor.top) != h)
  321. continue;
  322. for (int i = 0; ; i++) {
  323. dev.cb = sizeof(dev);
  324. if (!EnumDisplayDevices(info.szDevice, i, &dev, 0))
  325. break;
  326. if (!(dev.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP))
  327. continue;
  328. if (!name.empty())
  329. name += " / ";
  330. name += dev.DeviceString;
  331. }
  332. return name;
  333. }
  334. return "";
  335. #elif defined(__APPLE__)
  336. CGDisplayCount count;
  337. CGDirectDisplayID displays[16];
  338. CGDirectDisplayID displayID;
  339. CFDictionaryRef info;
  340. CFDictionaryRef dict;
  341. CFIndex dict_len;
  342. std::string name;
  343. if (CGGetActiveDisplayList(16, displays, &count) != kCGErrorSuccess)
  344. return "";
  345. if (count != (unsigned)Fl::screen_count())
  346. return "";
  347. if (m >= (int)count)
  348. return "";
  349. // Notice: Here we assume indices to be ordered the same as in FLTK (we rely on that in cocoa.mm as well).
  350. displayID = displays[m];
  351. info = IODisplayCreateInfoDictionary(CGDisplayIOServicePort(displayID),
  352. kIODisplayOnlyPreferredName);
  353. if (info == NULL)
  354. return "";
  355. dict = (CFDictionaryRef) CFDictionaryGetValue(info, CFSTR(kDisplayProductName));
  356. if (dict == NULL) {
  357. CFRelease(info);
  358. return "";
  359. }
  360. dict_len = CFDictionaryGetCount(dict);
  361. if (dict_len > 0) {
  362. CFTypeRef * names = new CFTypeRef[dict_len];
  363. CFDictionaryGetKeysAndValues(dict, NULL, (const void **) names);
  364. if (names[0]) {
  365. // Because of `kIODisplayOnlyPreferredName` names *should* only contain the name with
  366. // the current system localization.
  367. CFStringRef localized_name = (CFStringRef) names[0];
  368. CFIndex localized_name_len = CFStringGetLength(localized_name);
  369. // Even though we already have the length of `localized_name` above, we know that we will format it
  370. // as UTF-8 when we put it in the destination buffer. Therefore we need to check whether the name
  371. // with that encoding will fit.
  372. CFIndex localized_name_max_size = CFStringGetMaximumSizeForEncoding(localized_name_len, kCFStringEncodingUTF8) + 1;
  373. if (localized_name_max_size != kCFNotFound) {
  374. char *utf8_name = new char[localized_name_max_size];
  375. if (CFStringGetCString(
  376. /* ref = */ localized_name,
  377. /* dest = */ utf8_name,
  378. /* dest_len = */ localized_name_max_size,
  379. /* encoding = */ kCFStringEncodingUTF8))
  380. {
  381. name = utf8_name;
  382. }
  383. delete [] utf8_name;
  384. }
  385. }
  386. delete[] names;
  387. }
  388. CFRelease(info);
  389. return name;
  390. #else
  391. #if defined (HAVE_XRANDR)
  392. int x, y, w, h;
  393. int ev, err, xi_major;
  394. std::string name;
  395. fl_open_display();
  396. assert(fl_display != NULL);
  397. Fl::screen_xywh(x, y, w, h, m);
  398. if (!XQueryExtension(fl_display, "RANDR", &xi_major, &ev, &err))
  399. return "";
  400. XRRScreenResources *res = XRRGetScreenResources(fl_display, DefaultRootWindow(fl_display));
  401. if (!res)
  402. return "";
  403. for (int i = 0; i < res->ncrtc; i++) {
  404. XRRCrtcInfo *crtc = XRRGetCrtcInfo(fl_display, res, res->crtcs[i]);
  405. if (!crtc)
  406. continue;
  407. if ((crtc->x != x) || (crtc->y != y) ||
  408. ((int)crtc->width != w) || ((int)crtc->height != h))
  409. continue;
  410. for (int j = 0; j < crtc->noutput; j++) {
  411. XRROutputInfo *output;
  412. output = XRRGetOutputInfo(fl_display, res, crtc->outputs[j]);
  413. if (!output)
  414. continue;
  415. if (!name.empty())
  416. name += " / ";
  417. name += output->name;
  418. }
  419. }
  420. return name;
  421. #endif // !HAVE_XRANDR
  422. return "";
  423. #endif
  424. }
  425. int Fl_Monitor_Arrangement::fltk_event_handler(int event)
  426. {
  427. std::set<Fl_Monitor_Arrangement *>::iterator it;
  428. if (event != FL_SCREEN_CONFIGURATION_CHANGED)
  429. return 0;
  430. for (it = instances.begin(); it != instances.end(); it++)
  431. (*it)->refresh();
  432. return 0;
  433. }
  434. void Fl_Monitor_Arrangement::monitor_pressed(Fl_Widget* /*widget*/,
  435. void *user_data)
  436. {
  437. Fl_Monitor_Arrangement *self = (Fl_Monitor_Arrangement *) user_data;
  438. // When a monitor is selected, FLTK changes the state of it for us.
  439. // However, selecting a monitor might implicitly change the state of
  440. // others (if they become required). FLTK only redraws the selected
  441. // monitor. Therefore, we must trigger a redraw of the whole widget
  442. // manually.
  443. self->redraw();
  444. }
  445. void Fl_Monitor_Arrangement::checkered_pattern_draw(
  446. int x, int y, int width, int height, Fl_Color color)
  447. {
  448. bool draw_checker = false;
  449. const int CHECKER_SIZE = 8;
  450. fl_color(fl_lighter(fl_lighter(fl_lighter(color))));
  451. fl_rectf(x, y, width, height);
  452. fl_color(Fl::draw_box_active() ? color : fl_inactive(color));
  453. // Round up the square count. Later on, we remove square area that are
  454. // outside the given bounding area.
  455. const int count = (width + CHECKER_SIZE - 1) / CHECKER_SIZE;
  456. for (int i = 0; i < count; i++) {
  457. for (int j = 0; j < count; j++) {
  458. draw_checker = (i + j) % 2 == 0;
  459. if (draw_checker) {
  460. fl_rectf(
  461. /* x = */ x + i * CHECKER_SIZE,
  462. /* y = */ y + j * CHECKER_SIZE,
  463. /* w = */ CHECKER_SIZE - std::max(0, ((i + 1) * CHECKER_SIZE) - width),
  464. /* h = */ CHECKER_SIZE - std::max(0, ((j + 1) * CHECKER_SIZE) - height)
  465. );
  466. }
  467. }
  468. }
  469. fl_color(Fl::draw_box_active() ? FL_BLACK : fl_inactive(FL_BLACK));
  470. fl_rect(x, y, width, height);
  471. }