Skip to content

Commit

Permalink
monitors: update 'number' field
Browse files Browse the repository at this point in the history
Tag each monitor with a number when inserting -- the idea being that
monitor 0 -> n represents the ordering for each monitor.
  • Loading branch information
ThomasAdam committed Nov 28, 2023
1 parent 036ce09 commit c26fdb0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion fvwm/expand.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
44 changes: 36 additions & 8 deletions libs/FScreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion libs/FScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -84,6 +83,7 @@ struct monitor {
struct screen_info *si;
int flags;
int emit;
int number;
int dx, dy;
bool is_prev, was_primary;

Expand Down

0 comments on commit c26fdb0

Please sign in to comment.