bspwm/rule.c

251 lines
8.2 KiB
C
Raw Normal View History

2013-10-08 21:05:56 +02:00
/* * Copyright (c) 2012-2013 Bastien Dejean
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2012-09-29 17:18:45 +02:00
#include <stdio.h>
2012-09-11 13:12:53 +02:00
#include <string.h>
#include "bspwm.h"
2012-09-18 17:21:04 +02:00
#include "ewmh.h"
#include "window.h"
2013-10-05 22:32:40 +02:00
#include "messages.h"
#include "query.h"
#include "rule.h"
2012-09-11 13:12:53 +02:00
rule_t *make_rule(void)
{
rule_t *r = malloc(sizeof(rule_t));
r->effect.floating = false;
r->effect.fullscreen = false;
r->effect.locked = false;
2013-10-01 18:04:18 +02:00
r->effect.sticky = false;
r->effect.follow = false;
r->effect.focus = false;
2013-10-10 18:24:21 +02:00
r->effect.frame = false;
2013-10-12 21:16:51 +02:00
r->effect.private = false;
r->effect.center = false;
r->effect.unmanage = false;
r->one_shot = false;
r->effect.desc[0] = '\0';
r->prev = NULL;
r->next = NULL;
return r;
}
2012-12-25 19:03:35 +01:00
void add_rule(rule_t *r)
{
if (rule_head == NULL) {
rule_head = rule_tail = r;
} else {
rule_tail->next = r;
r->prev = rule_tail;
rule_tail = r;
}
}
2012-12-26 14:39:46 +01:00
void remove_rule(rule_t *r)
2012-12-25 19:28:24 +01:00
{
2012-12-26 14:39:46 +01:00
if (r == NULL)
return;
rule_t *prev = r->prev;
rule_t *next = r->next;
if (prev != NULL)
prev->next = next;
if (next != NULL)
next->prev = prev;
if (r == rule_head)
rule_head = next;
if (r == rule_tail)
rule_tail = prev;
2012-12-26 15:22:36 +01:00
free(r);
2012-12-26 14:39:46 +01:00
}
2013-10-06 18:35:08 +02:00
void remove_rule_by_name(char *name)
2012-12-26 14:39:46 +01:00
{
2013-10-06 18:35:08 +02:00
rule_t *r = rule_head;
while (r != NULL) {
rule_t *next = r->next;
if (streq(r->cause.name, name))
remove_rule(r);
r = next;
}
2012-12-25 19:28:24 +01:00
}
2013-10-06 18:35:08 +02:00
bool remove_rule_by_index(int idx)
2012-12-25 19:28:24 +01:00
{
2013-10-06 18:35:08 +02:00
for (rule_t *r = rule_head; r != NULL; r = r->next, idx--)
if (idx == 0) {
remove_rule(r);
return true;
}
return false;
2012-12-25 19:28:24 +01:00
}
2012-09-18 17:21:04 +02:00
bool is_match(rule_t *r, xcb_window_t win)
2012-09-11 13:12:53 +02:00
{
2013-03-16 19:20:20 +01:00
xcb_icccm_get_wm_class_reply_t reply;
2013-09-17 17:50:50 +02:00
int8_t success = 0;
if (streq(r->cause.name, MATCH_ALL) ||
2013-09-17 17:50:50 +02:00
((success = xcb_icccm_get_wm_class_reply(dpy, xcb_icccm_get_wm_class(dpy, win), &reply, NULL)) == 1
&& (streq(reply.class_name, r->cause.name)
|| streq(reply.instance_name, r->cause.name)))) {
2013-09-17 18:10:30 +02:00
if (success == 1)
2013-09-17 17:50:50 +02:00
xcb_icccm_get_wm_class_reply_wipe(&reply);
2012-09-18 17:21:04 +02:00
return true;
2012-09-11 13:12:53 +02:00
}
return false;
}
2012-09-18 17:21:04 +02:00
void handle_rules(xcb_window_t win, monitor_t **m, desktop_t **d, bool *floating, bool *fullscreen, bool *locked, bool *sticky, bool *follow, bool *transient, bool *takes_focus, bool *frame, bool *private, bool *center, bool *manage)
2012-09-18 17:21:04 +02:00
{
xcb_ewmh_get_atoms_reply_t win_type;
if (xcb_ewmh_get_wm_window_type_reply(ewmh, xcb_ewmh_get_wm_window_type(ewmh, win), &win_type, NULL) == 1) {
for (unsigned int i = 0; i < win_type.atoms_len; i++) {
xcb_atom_t a = win_type.atoms[i];
if (a == ewmh->_NET_WM_WINDOW_TYPE_TOOLBAR
|| a == ewmh->_NET_WM_WINDOW_TYPE_UTILITY) {
*takes_focus = false;
} else if (a == ewmh->_NET_WM_WINDOW_TYPE_DIALOG) {
*floating = true;
} else if (a == ewmh->_NET_WM_WINDOW_TYPE_DOCK || a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP || a == ewmh->_NET_WM_WINDOW_TYPE_NOTIFICATION) {
*manage = false;
if (a == ewmh->_NET_WM_WINDOW_TYPE_DESKTOP)
window_lower(win);
2012-09-18 17:21:04 +02:00
}
}
2012-09-29 13:26:56 +02:00
xcb_ewmh_get_atoms_reply_wipe(&win_type);
2012-09-18 17:21:04 +02:00
}
2012-11-13 21:27:02 +01:00
xcb_size_hints_t size_hints;
if (xcb_icccm_get_wm_normal_hints_reply(dpy, xcb_icccm_get_wm_normal_hints(dpy, win), &size_hints, NULL) == 1) {
2012-11-13 22:35:19 +01:00
if (size_hints.min_width > 0 && size_hints.min_height > 0
&& size_hints.min_width == size_hints.max_width
&& size_hints.min_height == size_hints.max_height)
2012-11-13 21:27:02 +01:00
*floating = true;
}
xcb_ewmh_get_atoms_reply_t win_state;
if (xcb_ewmh_get_wm_state_reply(ewmh, xcb_ewmh_get_wm_state(ewmh, win), &win_state, NULL) == 1) {
for (unsigned int i = 0; i < win_state.atoms_len; i++) {
xcb_atom_t a = win_state.atoms[i];
2013-10-01 18:14:58 +02:00
if (a == ewmh->_NET_WM_STATE_FULLSCREEN)
*fullscreen = true;
2013-10-01 18:14:58 +02:00
else if (a == ewmh->_NET_WM_STATE_STICKY)
*sticky = true;
}
2012-09-29 13:26:56 +02:00
xcb_ewmh_get_atoms_reply_wipe(&win_state);
}
2012-09-18 17:21:04 +02:00
xcb_window_t transient_for = XCB_NONE;
xcb_icccm_get_wm_transient_for_reply(dpy, xcb_icccm_get_wm_transient_for(dpy, win), &transient_for, NULL);
*transient = (transient_for == XCB_NONE ? false : true);
if (*transient)
*floating = true;
rule_t *rule = rule_head;
while (rule != NULL) {
if (is_match(rule, win)) {
rule_effect_t efc = rule->effect;
if (efc.floating)
2012-09-18 17:21:04 +02:00
*floating = true;
if (efc.fullscreen)
*fullscreen = true;
if (efc.locked)
*locked = true;
2013-10-01 18:04:18 +02:00
if (efc.sticky)
*sticky = true;
if (efc.follow)
*follow = true;
2013-08-06 11:26:51 +02:00
if (efc.focus)
*takes_focus = true;
2013-10-10 18:24:21 +02:00
if (efc.frame)
*frame = true;
if (efc.center)
*center = true;
2013-10-12 21:16:51 +02:00
if (efc.private)
*private = private;
if (efc.unmanage)
*manage = false;
if (efc.desc[0] != '\0') {
coordinates_t ref = {*m, *d, NULL};
coordinates_t loc;
if (desktop_from_desc(efc.desc, &ref, &loc)) {
*m = loc.monitor;
*d = loc.desktop;
}
}
2012-09-18 17:21:04 +02:00
}
2013-09-17 11:30:25 +02:00
rule_t *next = rule->next;
if (rule->one_shot)
remove_rule(rule);
rule = next;
2012-09-18 17:21:04 +02:00
}
2013-10-01 18:04:18 +02:00
if (*sticky) {
*m = mon;
*d = mon->desk;
}
2012-09-18 17:21:04 +02:00
}
2012-12-25 19:03:35 +01:00
void list_rules(char *pattern, char *rsp)
2012-12-25 19:03:35 +01:00
{
char line[MAXLEN];
for (rule_t *r = rule_head; r != NULL; r = r->next) {
if (pattern != NULL && !streq(pattern, r->cause.name))
continue;
2013-10-06 18:35:08 +02:00
snprintf(line, sizeof(line), "%s", r->cause.name);
2012-12-25 19:03:35 +01:00
strncat(rsp, line, REMLEN(rsp));
if (r->effect.floating)
strncat(rsp, " --floating", REMLEN(rsp));
if (r->effect.fullscreen)
strncat(rsp, " --fullscreen", REMLEN(rsp));
if (r->effect.locked)
strncat(rsp, " --locked", REMLEN(rsp));
2013-10-01 18:04:18 +02:00
if (r->effect.sticky)
strncat(rsp, " --sticky", REMLEN(rsp));
if (r->effect.follow)
strncat(rsp, " --follow", REMLEN(rsp));
2013-08-06 11:26:51 +02:00
if (r->effect.focus)
strncat(rsp, " --focus", REMLEN(rsp));
2013-10-10 18:24:21 +02:00
if (r->effect.frame)
strncat(rsp, " --frame", REMLEN(rsp));
2013-10-12 21:16:51 +02:00
if (r->effect.private)
strncat(rsp, " --private", REMLEN(rsp));
if (r->effect.center)
strncat(rsp, " --center", REMLEN(rsp));
if (r->effect.unmanage)
strncat(rsp, " --unmanage", REMLEN(rsp));
2013-09-17 11:30:25 +02:00
if (r->one_shot)
strncat(rsp, " --one-shot", REMLEN(rsp));
if (r->effect.desc[0] != '\0') {
snprintf(line, sizeof(line), " -d %s", r->effect.desc);
strncat(rsp, line, REMLEN(rsp));
}
strncat(rsp, "\n", REMLEN(rsp));
2012-12-25 19:03:35 +01:00
}
}