Skip to content

Commit

Permalink
Pass intermediate conseq. as arg. to ext. rules
Browse files Browse the repository at this point in the history
Inside the external rules script, `eval "$4"` provides a convenient way
of accessing the current values.
  • Loading branch information
baskerville committed Sep 29, 2017
1 parent d953f6f commit 49caeb8
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 7 deletions.
8 changes: 4 additions & 4 deletions doc/bspwm.1
Expand Up @@ -2,12 +2,12 @@
.\" Title: bspwm
.\" Author: [see the "Author" section]
.\" Generator: DocBook XSL Stylesheets v1.79.1 <http://docbook.sf.net/>
.\" Date: 09/08/2017
.\" Date: 09/29/2017
.\" Manual: Bspwm Manual
.\" Source: Bspwm 0.9.3-26-g412da35
.\" Source: Bspwm 0.9.3-30-gd953f6f
.\" Language: English
.\"
.TH "BSPWM" "1" "09/08/2017" "Bspwm 0\&.9\&.3\-26\-g412da35" "Bspwm Manual"
.TH "BSPWM" "1" "09/29/2017" "Bspwm 0\&.9\&.3\-30\-gd953f6f" "Bspwm Manual"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
Expand Down Expand Up @@ -1089,7 +1089,7 @@ Prefix prepended to each of the status lines\&.
.PP
\fIexternal_rules_command\fR
.RS 4
External command used to retrieve rule consequences\&. The command will receive the following arguments: window ID, class and instance names, monitor, desktop and node selectors\&. The output of that command must have the following format:
External command used to retrieve rule consequences\&. The command will receive the following arguments: window ID, class name, instance name, and intermediate consequences\&. The output of that command must have the following format:
\fBkey1=value1 key2=value2 \&...\fR
(the valid key/value pairs are given in the description of the
\fIrule\fR
Expand Down
2 changes: 1 addition & 1 deletion doc/bspwm.1.asciidoc
Expand Up @@ -646,7 +646,7 @@ Global Settings
Prefix prepended to each of the status lines.

'external_rules_command'::
External command used to retrieve rule consequences. The command will receive the following arguments: window ID, class and instance names, monitor, desktop and node selectors. The output of that command must have the following format: *key1=value1 key2=value2 ...* (the valid key/value pairs are given in the description of the 'rule' command).
External command used to retrieve rule consequences. The command will receive the following arguments: window ID, class name, instance name, and intermediate consequences. The output of that command must have the following format: *key1=value1 key2=value2 ...* (the valid key/value pairs are given in the description of the 'rule' command).

'initial_polarity'::
On which child should a new window be attached when adding a window on a single window tree in automatic mode. Accept the following values: *first_child*, *second_child*.
Expand Down
33 changes: 33 additions & 0 deletions src/helpers.c
Expand Up @@ -150,6 +150,39 @@ char *mktempfifo(const char *template)
return fifo_path;
}

int asprintf(char **buf, const char *fmt, ...)
{
int size = 0;
va_list args;
va_start(args, fmt);
size = vasprintf(buf, fmt, args);
va_end(args);
return size;
}

int vasprintf(char **buf, const char *fmt, va_list args)
{
int size = 0;

va_list tmp;
va_copy(tmp, args);
size = vsnprintf(NULL, size, fmt, tmp);
va_end(tmp);

if (size < 0) {
return -1;
}

*buf = malloc(size + 1);

if (*buf == NULL) {
return -1;
}

size = vsprintf(*buf, fmt, args);
return size;
}

/* Adapted from i3wm */
uint32_t get_color_pixel(const char *color)
{
Expand Down
4 changes: 3 additions & 1 deletion src/helpers.h
Expand Up @@ -79,7 +79,9 @@ void warn(char *fmt, ...);
void err(char *fmt, ...);
char *read_string(const char *file_path, size_t *tlen);
char *copy_string(char *str, size_t len);
char *mktempfifo(const char *template) ;
char *mktempfifo(const char *template);
int asprintf(char **buf, const char *fmt, ...);
int vasprintf(char **buf, const char *fmt, va_list args);
uint32_t get_color_pixel(const char *color);
bool is_hex_color(const char *color);

Expand Down
26 changes: 26 additions & 0 deletions src/query.c
Expand Up @@ -383,6 +383,32 @@ void print_pointer_action(pointer_action_t a, FILE *rsp)
}
}

void print_rule_consequence(char **buf, rule_consequence_t *csq)
{
char *rect_buf = NULL;
print_rectangle(&rect_buf, csq->rect);
if (rect_buf == NULL) {
rect_buf = malloc(1);
*rect_buf = '\0';
}
asprintf(buf, "monitor=%s desktop=%s node=%s state=%s layer=%s split_dir=%s split_ratio=%lf hidden=%s sticky=%s private=%s locked=%s center=%s follow=%s manage=%s focus=%s border=%s rectangle=%s",
csq->monitor_desc, csq->desktop_desc, csq->node_desc,
csq->state == NULL ? "" : STATE_STR(*csq->state),
csq->layer == NULL ? "" : LAYER_STR(*csq->layer),
csq->split_dir, csq->split_ratio,
ON_OFF_STR(csq->hidden), ON_OFF_STR(csq->sticky), ON_OFF_STR(csq->private),
ON_OFF_STR(csq->locked), ON_OFF_STR(csq->center), ON_OFF_STR(csq->follow),
ON_OFF_STR(csq->manage), ON_OFF_STR(csq->focus), ON_OFF_STR(csq->border), rect_buf);
free(rect_buf);
}

void print_rectangle(char **buf, xcb_rectangle_t *rect)
{
if (rect != NULL) {
asprintf(buf, "%hux%hu+%hi+%hi", rect->width, rect->height, rect->x, rect->y);
}
}

node_select_t make_node_select(void)
{
node_select_t sel = {
Expand Down
2 changes: 2 additions & 0 deletions src/query.h
Expand Up @@ -67,6 +67,8 @@ void fprint_desktop_name(desktop_t *d, FILE *rsp);
void print_modifier_mask(uint16_t m, FILE *rsp);
void print_button_index(int8_t b, FILE *rsp);
void print_pointer_action(pointer_action_t a, FILE *rsp);
void print_rule_consequence(char **buf, rule_consequence_t *csq);
void print_rectangle(char **buf, xcb_rectangle_t *rect);
node_select_t make_node_select(void);
desktop_select_t make_desktop_select(void);
monitor_select_t make_monitor_select(void);
Expand Down
6 changes: 5 additions & 1 deletion src/rule.c
Expand Up @@ -31,6 +31,7 @@
#include "bspwm.h"
#include "ewmh.h"
#include "window.h"
#include "query.h"
#include "parse.h"
#include "settings.h"
#include "rule.h"
Expand Down Expand Up @@ -305,9 +306,12 @@ bool schedule_rules(xcb_window_t win, rule_consequence_t *csq)
dup2(fds[1], 1);
close(fds[0]);
char wid[SMALEN];
char *csq_buf;
print_rule_consequence(&csq_buf, csq);
snprintf(wid, sizeof(wid), "%i", win);
setsid();
execl(external_rules_command, external_rules_command, wid, csq->class_name, csq->instance_name, csq->monitor_desc, csq->desktop_desc, csq->node_desc, NULL);
execl(external_rules_command, external_rules_command, wid, csq->class_name, csq->instance_name, csq_buf, NULL);
free(csq_buf);
err("Couldn't spawn rule command.\n");
} else if (pid > 0) {
close(fds[1]);
Expand Down

0 comments on commit 49caeb8

Please sign in to comment.