From e2f085815aa99137814b0dad61a6abde61cf55d4 Mon Sep 17 00:00:00 2001 From: Bastien Dejean Date: Mon, 2 Sep 2013 21:03:59 +0200 Subject: [PATCH] Add index selector for desktops and monitors --- doc/bspwm.1 | 26 ++++++++++++++++++++++++-- doc/bspwm.1.txt | 14 ++++++++++++++ ewmh.c | 1 - messages.c | 5 +++++ messages.h | 1 + query.c | 31 +++++++++++++++++++++++++++++++ query.h | 2 ++ 7 files changed, 77 insertions(+), 3 deletions(-) diff --git a/doc/bspwm.1 b/doc/bspwm.1 index 8ae4851..25c4064 100644 --- a/doc/bspwm.1 +++ b/doc/bspwm.1 @@ -2,12 +2,12 @@ .\" Title: bspwm .\" Author: [see the "Author" section] .\" Generator: DocBook XSL Stylesheets v1.78.1 -.\" Date: 08/28/2013 +.\" Date: 09/02/2013 .\" Manual: Bspwm Manual .\" Source: Bspwm 0.8 .\" Language: English .\" -.TH "BSPWM" "1" "08/28/2013" "Bspwm 0\&.8" "Bspwm Manual" +.TH "BSPWM" "1" "09/02/2013" "Bspwm 0\&.8" "Bspwm Manual" .\" ----------------------------------------------------------------- .\" * Define some portability stuff .\" ----------------------------------------------------------------- @@ -277,6 +277,7 @@ Select a desktop\&. .\} .nf DESKTOP_SEL := + | ^ | (CYCLE_DIR|last|focused)[\&.occupied|\&.free][\&.urgent|\&.nonurgent] .fi .if n \{\ @@ -291,6 +292,16 @@ DESKTOP_SEL := \fBPrimary Selectors\fR .RS 4 .PP + +.RS 4 +Selects the desktop with the given name\&. +.RE +.PP +^ +.RS 4 +Selects the nth desktop\&. +.RE +.PP \fICYCLE_DIR\fR .RS 4 Selects the desktop in the given direction relative to the active desktop\&. @@ -344,6 +355,7 @@ Select a monitor\&. .\} .nf MONITOR_SEL := + | ^ | (DIR|CYCLE_DIR|last|focused)[\&.occupied|\&.free] .fi .if n \{\ @@ -358,6 +370,16 @@ MONITOR_SEL := \fBPrimary Selectors\fR .RS 4 .PP + +.RS 4 +Selects the monitor with the given name\&. +.RE +.PP +^ +.RS 4 +Selects the nth monitor\&. +.RE +.PP \fIDIR\fR .RS 4 Selects the monitor in the given (spacial) direction relative to the active monitor\&. diff --git a/doc/bspwm.1.txt b/doc/bspwm.1.txt index 0cbef3f..ef56e7f 100644 --- a/doc/bspwm.1.txt +++ b/doc/bspwm.1.txt @@ -197,12 +197,19 @@ Select a desktop. ---- DESKTOP_SEL := + | ^ | (CYCLE_DIR|last|focused)[.occupied|.free][.urgent|.nonurgent] ---- Primary Selectors ^^^^^^^^^^^^^^^^^ +:: + Selects the desktop with the given name. + +^:: + Selects the nth desktop. + 'CYCLE_DIR':: Selects the desktop in the given direction relative to the active desktop. @@ -234,12 +241,19 @@ Select a monitor. ---- MONITOR_SEL := + | ^ | (DIR|CYCLE_DIR|last|focused)[.occupied|.free] ---- Primary Selectors ^^^^^^^^^^^^^^^^^ +:: + Selects the monitor with the given name. + +^:: + Selects the nth monitor. + 'DIR':: Selects the monitor in the given (spacial) direction relative to the active monitor. diff --git a/ewmh.c b/ewmh.c index 905ee19..72668ae 100644 --- a/ewmh.c +++ b/ewmh.c @@ -39,7 +39,6 @@ uint32_t ewmh_get_desktop_index(desktop_t *d) for (desktop_t *cd = m->desk_head; cd != NULL; cd = cd->next, i++) if (d == cd) return i; - return 0; } diff --git a/messages.c b/messages.c index 6b02970..8aacfe9 100644 --- a/messages.c +++ b/messages.c @@ -926,3 +926,8 @@ bool parse_window_id(char *s, long int *i) *i = ret; return true; } + +bool parse_index(char *s, int *i) +{ + return sscanf(s, "^%i", i) == 1; +} diff --git a/messages.h b/messages.h index 75f6efa..0a64fd9 100644 --- a/messages.h +++ b/messages.h @@ -31,5 +31,6 @@ bool parse_fence_move(char *, fence_move_t *); bool parse_pointer_action(char *, pointer_action_t *); bool parse_degree(char *, int *); bool parse_window_id(char *, long int *); +bool parse_index(char *, int *); #endif diff --git a/query.c b/query.c index c25bdb0..237a9ad 100644 --- a/query.c +++ b/query.c @@ -205,9 +205,12 @@ bool desktop_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst) dst->desktop = NULL; cycle_dir_t cyc; + int idx; if (parse_cycle_direction(desc, &cyc)) { dst->monitor = ref->monitor; dst->desktop = closest_desktop(ref->monitor, ref->desktop, cyc, sel); + } else if (parse_index(desc, &idx)) { + desktop_from_index(idx, dst); } else if (streq("last", desc)) { if (mon->last_desk != NULL && desktop_matches(mon->last_desk, sel)) { dst->monitor = mon; @@ -245,10 +248,13 @@ bool monitor_from_desc(char *desc, coordinates_t *ref, coordinates_t *dst) direction_t dir; cycle_dir_t cyc; + int idx; if (parse_direction(desc, &dir)) { dst->monitor = nearest_monitor(ref->monitor, dir, sel); } else if (parse_cycle_direction(desc, &cyc)) { dst->monitor = closest_monitor(ref->monitor, cyc, sel); + } else if (parse_index(desc, &idx)) { + monitor_from_index(idx, dst); } else if (streq("last", desc)) { if (last_mon != NULL && desktop_matches(last_mon->desk, sel)) { dst->monitor = last_mon; @@ -299,3 +305,28 @@ bool locate_monitor(char *name, coordinates_t *loc) } return false; } + +bool desktop_from_index(int i, coordinates_t *loc) +{ + for (monitor_t *m = mon_head; m != NULL; m = m->next) + for (desktop_t *d = m->desk_head; d != NULL; d = d->next, i--) + if (i == 1) { + loc->monitor = m; + loc->desktop = d; + loc->node = NULL; + return true; + } + return false; +} + +bool monitor_from_index(int i, coordinates_t *loc) +{ + for (monitor_t *m = mon_head; m != NULL; m = m->next, i--) + if (i == 1) { + loc->monitor = m; + loc->desktop = NULL; + loc->node = NULL; + return true; + } + return false; +} diff --git a/query.h b/query.h index 61baf72..1c345e1 100644 --- a/query.h +++ b/query.h @@ -20,5 +20,7 @@ bool locate_monitor(char *, coordinates_t *); bool node_from_desc(char *, coordinates_t *, coordinates_t *); bool desktop_from_desc(char *, coordinates_t *, coordinates_t *); bool monitor_from_desc(char *, coordinates_t *, coordinates_t *); +bool desktop_from_index(int, coordinates_t *); +bool monitor_from_index(int, coordinates_t *); #endif