From c26fdb09b16ce4b063ebd75b0af554c25d7735af Mon Sep 17 00:00:00 2001 From: Thomas Adam Date: Sat, 25 Nov 2023 23:42:01 +0000 Subject: [PATCH] monitors: update 'number' field Tag each monitor with a number when inserting -- the idea being that monitor 0 -> n represents the ordering for each monitor. --- fvwm/expand.c | 2 +- libs/FScreen.c | 44 ++++++++++++++++++++++++++++++++++++-------- libs/FScreen.h | 2 +- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/fvwm/expand.c b/fvwm/expand.c index e781e170f..866348f0e 100644 --- a/fvwm/expand.c +++ b/fvwm/expand.c @@ -560,7 +560,7 @@ static signed int expand_vars_extended( while ((m_name = strsep(&rest_s, ".")) != NULL) { if ((pos = atoi(m_name)) >= 0) { RB_FOREACH(m_loop, monitors, &monitor_q) { - if (m_loop->si->number == pos) { + if (m_loop->number == pos) { mon2 = m_loop; break; } diff --git a/libs/FScreen.c b/libs/FScreen.c index 190a2eb6c..5b6f2930c 100644 --- a/libs/FScreen.c +++ b/libs/FScreen.c @@ -49,6 +49,7 @@ static bool randr_initialised; static void scan_screens(Display *); static struct monitor *monitor_by_name(const char *); static void monitor_set_coords(struct monitor *, XRRMonitorInfo); +static void monitor_assign_number(void); enum monitor_tracking monitor_mode; bool is_tracking_shared; @@ -382,7 +383,7 @@ monitor_output_change(Display *dpy, XRRScreenChangeNotifyEvent *e) XRROutputInfo *oinfo = NULL; int i; - //scan_screens(dpy); + scan_screens(dpy); for (i = 0; i < res->noutput; i++) { oinfo = XRRGetOutputInfo(dpy, res, res->outputs[i]); @@ -462,15 +463,37 @@ monitor_set_coords(struct monitor *m, XRRMonitorInfo rrm) } } +static void +monitor_assign_number(void) +{ + struct monitor *m; + int n = 0; + + RB_FOREACH(m, monitors, &monitor_q) { + if (m->flags & MONITOR_DISABLED) + continue; + m->number = n++; + } +} + int monitor_compare(struct monitor *a, struct monitor *b) { fvwm_debug(__func__, "a: %s, y: %d x: %d", a->si->name, a->si->y, a->si->x); fvwm_debug(__func__, "b: %s, y: %d x: %d", b->si->name, b->si->y, b->si->x); - if (a->si->y == b->si->y) - return a->si->x - b->si->x; - return b->si->y - a->si->y; + /* XXX: Optimise. */ + if (a->si->y < b->si->y) + return -1; + else if (a->si->y > b->si->y) + return 1; + else { + if (a->si->x < b->si->x) + return -1; + else if (a->si->x > b->si->x) + return 1; + } + return 0; } static void @@ -486,8 +509,6 @@ scan_screens(Display *dpy) exit(101); } - fvwm_debug(__func__, "HERE: 1 (%d)", n); - for (i = 0; i < n; i++) { struct monitor *m = NULL; char *name = XGetAtomName(dpy, rrm[i].name); @@ -510,10 +531,11 @@ scan_screens(Display *dpy) m->virtual_scr.edge_thickness = 2; m->virtual_scr.last_edge_thickness = 2; - fvwm_debug(__func__, "HERE: 3"); monitor_set_coords(m, rrm[i]); TAILQ_INSERT_TAIL(&screen_info_q, m->si, entry); RB_INSERT(monitors, &monitor_q, m); + + goto done; } RB_FOREACH(m, monitors, &monitor_q) { @@ -526,10 +548,16 @@ scan_screens(Display *dpy) monitor_set_coords(m, rrm[i]); } } +done: if (name != NULL) XFree(name); } + + /* Now that all monitors have been inserted, assign them a number from + * 0 -> n so that they can be referenced in order. + */ + monitor_assign_number(); monitor_check_primary(); XRRFreeMonitors(rrm); } @@ -644,7 +672,7 @@ monitor_dump_state(struct monitor *m) "\t\tMyDisplayWidth: %d, MyDisplayHeight: %d\n\t}\n" "\tDesktops:\t%s\n" "\tFlags:%s\n\n", - m2->si->number, + m2->number, m2->si->name, (m2->flags & MONITOR_DISABLED) ? "true" : "false", (m2->flags & MONITOR_PRIMARY) ? "yes" : "no", diff --git a/libs/FScreen.h b/libs/FScreen.h index ac0f06e8e..aec9dd963 100644 --- a/libs/FScreen.h +++ b/libs/FScreen.h @@ -58,7 +58,6 @@ extern bool is_tracking_shared; struct screen_info { const char *name; int x, y, w, h; - int number; RROutput rr_output; TAILQ_ENTRY(screen_info) entry; @@ -84,6 +83,7 @@ struct monitor { struct screen_info *si; int flags; int emit; + int number; int dx, dy; bool is_prev, was_primary;