wmii/client.c

752 lines
17 KiB
C
Raw Normal View History

2006-10-12 18:10:57 +04:00
/* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
2005-11-18 18:54:58 +03:00
* See LICENSE file for license details.
*/
2006-10-20 12:07:55 +04:00
#include "wmii.h"
2006-10-12 18:10:57 +04:00
#include <assert.h>
2005-11-18 18:54:58 +03:00
#include <stdlib.h>
#include <string.h>
#include <X11/Xatom.h>
static char *Ebadcmd = "bad command",
*Ebadvalue = "bad value";
#define CLIENT_MASK (StructureNotifyMask | PropertyChangeMask | EnterWindowMask)
#define ButtonMask (ButtonPressMask | ButtonReleaseMask)
2006-06-28 06:16:14 +04:00
Client *
2006-10-12 18:10:57 +04:00
sel_client() {
2007-02-05 05:02:05 +03:00
return screen->sel && screen->sel->sel->sel ? screen->sel->sel->sel->client : nil;
2006-06-28 06:16:14 +04:00
}
Client *
2006-10-12 18:10:57 +04:00
client_of_win(Window w) {
2006-06-28 06:16:14 +04:00
Client *c;
for(c=client; c && c->win != w; c=c->next);
return c;
}
Frame *
2006-10-12 18:10:57 +04:00
frame_of_win(Window w) {
Client *c;
for(c=client; c && c->framewin != w; c=c->next);
2007-02-05 05:02:05 +03:00
return c ? c->frame : nil;
}
static void
2006-10-12 18:10:57 +04:00
update_client_name(Client *c) {
XTextProperty name;
XClassHint ch;
int n;
2007-02-05 05:02:05 +03:00
char **list = nil;
2006-04-24 18:03:37 +04:00
name.nitems = 0;
2006-04-24 18:03:37 +04:00
c->name[0] = 0;
2006-07-03 20:41:14 +04:00
XGetTextProperty(blz.dpy, c->win, &name, net_atom[NetWMName]);
if(!name.nitems)
2006-07-03 20:41:14 +04:00
XGetWMName(blz.dpy, c->win, &name);
if(!name.nitems)
return;
if(name.encoding == XA_STRING)
2006-10-12 18:10:57 +04:00
strncpy(c->name, (char *)name.value, sizeof(c->name));
else {
2006-07-03 20:41:14 +04:00
if(XmbTextPropertyToTextList(blz.dpy, &name, &list, &n) >= Success
&& n > 0 && *list)
2006-04-24 18:03:37 +04:00
{
2006-10-12 18:10:57 +04:00
strncpy(c->name, *list, sizeof(c->name));
2006-04-24 18:03:37 +04:00
XFreeStringList(list);
}
}
XFree(name.value);
2006-07-03 20:41:14 +04:00
if(XGetClassHint(blz.dpy, c->win, &ch)) {
snprintf(c->props, sizeof(c->props), "%s:%s:%s",
ch.res_class ? ch.res_class : "",
ch.res_name ? ch.res_name : "",
c->name);
if(ch.res_class)
XFree(ch.res_class);
if(ch.res_name)
XFree(ch.res_name);
}
}
2005-12-21 18:18:11 +03:00
Client *
2006-10-12 18:10:57 +04:00
create_client(Window w, XWindowAttributes *wa) {
Client **t, *c = (Client *) ixp_emallocz(sizeof(Client));
XSetWindowAttributes fwa;
long msize;
2005-12-21 18:18:11 +03:00
c->win = w;
c->rect.x = wa->x;
c->rect.y = wa->y;
c->border = wa->border_width;
c->rect.width = wa->width;
c->rect.height = wa->height;
2006-07-03 20:41:14 +04:00
XSetWindowBorderWidth(blz.dpy, c->win, 0);
c->proto = win_proto(c->win);
2006-07-03 20:41:14 +04:00
XGetTransientForHint(blz.dpy, c->win, &c->trans);
if(!XGetWMNormalHints(blz.dpy, c->win, &c->size, &msize) || !c->size.flags)
c->size.flags = PSize;
2006-05-23 23:08:24 +04:00
if(c->size.flags & PMinSize && c->size.flags & PMaxSize
&& c->size.min_width == c->size.max_width
&& c->size.min_height == c->size.max_height)
c->fixedsize = True;
else
c->fixedsize = False;
2006-07-03 20:41:14 +04:00
XAddToSaveSet(blz.dpy, c->win);
update_client_name(c);
fwa.override_redirect = 1;
fwa.background_pixmap = ParentRelative;
fwa.event_mask =
SubstructureRedirectMask | SubstructureNotifyMask | ExposureMask
| ButtonPressMask | PointerMotionMask | ButtonReleaseMask | KeyPressMask;
2006-07-03 20:41:14 +04:00
c->framewin = XCreateWindow(blz.dpy, blz.root, c->rect.x, c->rect.y,
c->rect.width + 2 * def.border,
2006-10-12 18:12:22 +04:00
c->rect.height + def.border + labelh(&def.font), 0,
2006-07-03 20:41:14 +04:00
DefaultDepth(blz.dpy, blz.screen), CopyFromParent,
DefaultVisual(blz.dpy, blz.screen),
CWOverrideRedirect | CWBackPixmap | CWEventMask, &fwa);
XGrabButton(blz.dpy, AnyButton, AnyModifier, c->framewin, False, ButtonMask,
GrabModeSync, GrabModeSync, None, None);
2006-07-03 20:41:14 +04:00
c->gc = XCreateGC(blz.dpy, c->framewin, 0, 0);
XSync(blz.dpy, False);
for(t=&client; *t; t=&(*t)->next);
2007-02-05 05:02:05 +03:00
c->next = *t; /* *t == nil */
2006-06-08 12:54:19 +04:00
*t = c;
write_event("CreateClient 0x%x\n", c->win);
return c;
2005-11-18 18:54:58 +03:00
}
void
set_client_state(Client * c, int state)
{
long data[] = { state, None };
XChangeProperty(blz.dpy, c->win, wm_atom[WMState], wm_atom[WMState], 32,
PropModeReplace, (unsigned char *) data, 2);
}
void
2006-10-12 18:10:57 +04:00
focus_client(Client *c, Bool restack) {
Client *old_in_area;
Client *old;
Frame *f;
View *v;
unsigned int a_i;
Area *a, *old_a;
if(!sel_screen)
return;
f = c->sel;
v = f->view;
old = sel_client();
old_in_area = sel_client_of_area(f->area);
old_a = v->sel;
if(old_a->floating != f->area->floating)
v->revert = old_a;
2006-06-08 12:54:19 +04:00
v->sel = f->area;
f->area->sel = f;
c->floating = f->area->floating;
if(restack)
restack_view(v);
2006-06-08 12:54:19 +04:00
if(!c->floating && f->area->mode == Colstack)
arrange_column(f->area, False);
2006-07-03 20:41:14 +04:00
XSetInputFocus(blz.dpy, c->win, RevertToPointerRoot, CurrentTime);
if(old && old != old_in_area && old != c) {
update_frame_widget_colors(old->sel);
draw_frame(old->sel);
}
if(old_in_area && old_in_area != c) {
update_frame_widget_colors(old_in_area->sel);
draw_frame(old_in_area->sel);
}
update_frame_widget_colors(c->sel);
draw_frame(c->sel);
2006-07-03 20:41:14 +04:00
XSync(blz.dpy, False);
if(old_a != v->sel) {
for(a = v->area, a_i = 0; a; a = a->next, a_i++)
if(a == v->sel) break;
if(a_i)
write_event("ColumnFocus %d\n", a_i);
else
write_event("FocusFloating\n");
}
2007-02-09 06:28:32 +03:00
if(c != old)
write_event("ClientFocus 0x%x\n", c->win);
}
2005-12-21 18:18:11 +03:00
void
2006-10-12 18:10:57 +04:00
map_client(Client *c) {
2006-07-03 20:41:14 +04:00
XSelectInput(blz.dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
XMapWindow(blz.dpy, c->win);
XSelectInput(blz.dpy, c->win, CLIENT_MASK);
set_client_state(c, NormalState);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void
2006-10-12 18:10:57 +04:00
unmap_client(Client *c) {
2006-07-03 20:41:14 +04:00
XSelectInput(blz.dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
XUnmapWindow(blz.dpy, c->win);
XSelectInput(blz.dpy, c->win, CLIENT_MASK);
set_client_state(c, WithdrawnState);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void
2006-10-12 18:10:57 +04:00
reparent_client(Client *c, Window w, int x, int y) {
2006-07-03 20:41:14 +04:00
XSelectInput(blz.dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
XReparentWindow(blz.dpy, c->win, w, x, y);
XSelectInput(blz.dpy, c->win, CLIENT_MASK);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void
2006-10-12 18:10:57 +04:00
configure_client(Client *c) {
XConfigureEvent e;
2006-06-08 12:54:19 +04:00
Frame *f = c->sel;
2006-10-12 18:10:57 +04:00
e.type = ConfigureNotify;
e.event = c->win;
e.window = c->win;
e.x = c->rect.x;
e.y = c->rect.y;
if(f) {
e.x += f->rect.x;
e.y += f->rect.y;
2006-01-25 21:41:12 +03:00
}
e.width = c->rect.width;
e.height = c->rect.height;
e.border_width = c->border;
e.above = None;
e.override_redirect = False;
XSendEvent(blz.dpy, c->win, False,
StructureNotifyMask, (XEvent *) & e);
2006-07-03 20:41:14 +04:00
XSync(blz.dpy, False);
2005-11-18 18:54:58 +03:00
}
static void
2006-10-12 18:10:57 +04:00
send_client_message(Window w, Atom a, long value) {
XEvent e;
2006-10-12 18:10:57 +04:00
e.type = ClientMessage;
e.xclient.window = w;
e.xclient.message_type = a;
e.xclient.format = 32;
e.xclient.data.l[0] = value;
e.xclient.data.l[1] = CurrentTime;
XSendEvent(blz.dpy, w, False, NoEventMask, &e);
XSync(blz.dpy, False);
}
2005-12-21 18:18:11 +03:00
void
2006-10-12 18:10:57 +04:00
kill_client(Client * c) {
if(c->proto & WM_PROTOCOL_DELWIN)
send_client_message(c->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
else
XKillClient(blz.dpy, c->win);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void
2006-10-12 18:10:57 +04:00
prop_client(Client *c, XPropertyEvent *e) {
XWMHints *wmh;
long msize;
if(e->atom == wm_atom[WMProtocols]) {
c->proto = win_proto(c->win);
return;
}
switch (e->atom) {
case XA_WM_TRANSIENT_FOR:
2006-07-03 20:41:14 +04:00
XGetTransientForHint(blz.dpy, c->win, &c->trans);
break;
case XA_WM_NORMAL_HINTS:
2006-07-03 20:41:14 +04:00
if(!XGetWMNormalHints(blz.dpy, c->win, &c->size, &msize) || !c->size.flags) {
c->size.flags = PSize;
}
2006-05-23 23:08:24 +04:00
if(c->size.flags & PMinSize && c->size.flags & PMaxSize
&& c->size.min_width == c->size.max_width
&& c->size.min_height == c->size.max_height)
c->fixedsize = True;
else
c->fixedsize = False;
break;
case XA_WM_HINTS:
wmh = XGetWMHints(blz.dpy, c->win);
if(wmh->flags&XUrgencyHint && !client->urgent) {
write_event("Urgent 0x%x\n", client->win);;
client->urgent = True;
}
else if(!(wmh->flags&XUrgencyHint) && client->urgent) {
write_event("NotUrgent 0x%x\n", client->win);;
client->urgent = False;
}
break;
}
if(e->atom == XA_WM_NAME || e->atom == net_atom[NetWMName]) {
update_client_name(c);
2006-06-08 12:54:19 +04:00
if(c->frame)
draw_frame(c->sel);
}
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void
2006-10-12 18:10:57 +04:00
gravitate_client(Client *c, Bool invert) {
int dx = 0, dy = 0;
int gravity = NorthWestGravity;
if(c->size.flags & PWinGravity) {
gravity = c->size.win_gravity;
}
/* y */
switch (gravity) {
case StaticGravity:
case NorthWestGravity:
case NorthGravity:
case NorthEastGravity:
2006-10-12 18:12:22 +04:00
dy = labelh(&def.font);
break;
case EastGravity:
case CenterGravity:
case WestGravity:
2006-10-12 18:12:22 +04:00
dy = -(c->rect.height / 2) + labelh(&def.font);
break;
case SouthEastGravity:
case SouthGravity:
case SouthWestGravity:
dy = -c->rect.height;
break;
default:
break;
}
/* x */
switch (gravity) {
case StaticGravity:
case NorthWestGravity:
case WestGravity:
case SouthWestGravity:
dx = def.border;
break;
case NorthGravity:
case CenterGravity:
case SouthGravity:
dx = -(c->rect.width / 2) + def.border;
break;
case NorthEastGravity:
case EastGravity:
case SouthEastGravity:
dx = -(c->rect.width + def.border);
break;
default:
break;
}
if(invert) {
dx = -dx;
dy = -dy;
}
c->rect.x += dx;
c->rect.y += dy;
2005-11-18 18:54:58 +03:00
}
2005-12-05 22:38:03 +03:00
void
2006-10-12 18:10:57 +04:00
manage_client(Client *c) {
XTextProperty tags;
2006-03-11 03:32:10 +03:00
Client *trans;
tags.nitems = 0;
2006-07-03 20:41:14 +04:00
XGetTextProperty(blz.dpy, c->win, &tags, tags_atom);
2006-04-12 12:44:07 +04:00
if(c->trans && (trans = client_of_win(c->trans)))
2006-10-12 18:10:57 +04:00
strncpy(c->tags, trans->tags, sizeof(c->tags));
else if(tags.nitems)
2006-10-12 18:10:57 +04:00
strncpy(c->tags, (char *)tags.value, sizeof(c->tags));
XFree(tags.value);
if(!strlen(c->tags))
2006-04-12 12:44:07 +04:00
apply_rules(c);
2006-06-25 17:16:03 +04:00
apply_tags(c, c->tags);
reparent_client(c, c->framewin, c->rect.x, c->rect.y);
2006-06-25 17:16:03 +04:00
if(!starting)
update_views();
2006-04-27 16:17:18 +04:00
map_client(c);
2006-07-03 20:41:14 +04:00
XMapWindow(blz.dpy, c->framewin);
XSync(blz.dpy, False);
if(c->sel->area->view == screen->sel)
2006-04-27 16:17:18 +04:00
focus_client(c, False);
flush_masked_events(EnterWindowMask);
2005-12-05 22:38:03 +03:00
}
2005-12-06 00:22:24 +03:00
static int
2006-10-12 18:10:57 +04:00
dummy_error_handler(Display *dpy, XErrorEvent *error) {
return 0;
}
2006-03-06 01:38:50 +03:00
void
2006-10-12 18:10:57 +04:00
destroy_client(Client *c) {
2007-02-05 05:02:05 +03:00
char *dummy = nil;
Client **tc;
2006-07-03 20:41:14 +04:00
XGrabServer(blz.dpy);
XSetErrorHandler(dummy_error_handler);
2006-06-08 12:54:19 +04:00
if(c->frame) {
c->rect.x = c->sel->rect.x;
c->rect.y = c->sel->rect.y;
}
2006-06-25 17:16:03 +04:00
update_client_views(c, &dummy);
2006-05-14 19:27:00 +04:00
unmap_client(c);
reparent_client(c, blz.root, c->rect.x, c->rect.y);
2006-07-03 20:41:14 +04:00
XFreeGC(blz.dpy, c->gc);
XDestroyWindow(blz.dpy, c->framewin);
for(tc=&client; *tc && *tc != c; tc=&(*tc)->next);
2006-10-12 18:10:57 +04:00
assert(*tc == c);
*tc = c->next;
2006-04-13 17:35:10 +04:00
update_views();
free(c);
2006-07-03 20:41:14 +04:00
XSync(blz.dpy, False);
XSetErrorHandler(wmii_error_handler);
2006-07-03 20:41:14 +04:00
XUngrabServer(blz.dpy);
flush_masked_events(EnterWindowMask);
write_event("DestroyClient 0x%x\n", c->win);
2006-03-06 01:38:50 +03:00
}
void
2006-10-12 18:10:57 +04:00
match_sizehints(Client *c, XRectangle *r, Bool floating, BlitzAlign sticky) {
XSizeHints *s = &c->size;
unsigned int dx = 2 * def.border;
2006-10-12 18:12:22 +04:00
unsigned int dy = def.border + labelh(&def.font);
unsigned int hdiff, wdiff;
2006-06-08 12:54:19 +04:00
if(floating && (s->flags & PMinSize)) {
if(r->width < s->min_width + dx) {
wdiff = (s->min_width + dx) - r->width;
r->width += wdiff;
if((sticky & EAST) && !(sticky & WEST))
r->x -= wdiff;
}
if(r->height < s->min_height + dy) {
hdiff = (s->min_height + dy) - r->height;
r->height += hdiff;
if((sticky & SOUTH) && !(sticky & NORTH))
r->y -= hdiff;
}
}
2006-06-08 12:54:19 +04:00
if(floating && (s->flags & PMaxSize)) {
if(r->width > s->max_width + dx) {
wdiff = r->width - (s->max_width + dx);
r->width -= wdiff;
if((sticky & EAST) && !(sticky & WEST))
r->x += wdiff;
}
if(r->height > s->max_height + dy) {
hdiff = r->height - (s->max_height + dy);
r->height -= hdiff;
if((sticky & SOUTH) && !(sticky & NORTH))
r->y += hdiff;
}
}
if(s->flags & PResizeInc) {
int w = 0, h = 0;
2006-05-25 20:53:09 +04:00
if(s->flags & PBaseSize) {
w = s->base_width;
h = s->base_height;
} else if(s->flags & PMinSize) {
/* base_{width,height} default to min_{width,height} */
2006-05-25 20:53:09 +04:00
w = s->min_width;
h = s->min_height;
}
2006-05-25 20:53:09 +04:00
/* client_width = base_width + i * s->width_inc for an integer i */
w = r->width - dx - w;
if(s->width_inc > 0) {
wdiff = w % s->width_inc;
r->width -= wdiff;
if((sticky & EAST) && !(sticky & WEST))
r->x += wdiff;
}
h = r->height - dy - h;
if(s->height_inc > 0) {
hdiff = h % s->height_inc;
r->height -= hdiff;
if((sticky & SOUTH) && !(sticky & NORTH))
r->y += hdiff;
}
}
}
void
resize_client(Client *c, XRectangle *r) {
Frame *f;
Bool floating;
f = c->sel;
floating = f->area->floating;
resize_frame(f, r);
if(floating) {
if((c->rect.width == screen->rect.width) &&
(c->rect.height == screen->rect.height)) {
f->rect.x = -def.border;
f->rect.y = -labelh(&def.font);
}else{
check_frame_constraints(&f->rect);
}
}
if(f->area->view == screen->sel)
XMoveResizeWindow(blz.dpy, c->framewin, f->rect.x,
f->rect.y, f->rect.width, f->rect.height);
else
XMoveResizeWindow(blz.dpy, c->framewin, 2 * screen->rect.width + f->rect.x,
f->rect.y, f->rect.width, f->rect.height);
c->rect.x = def.border;
2006-10-12 18:12:22 +04:00
c->rect.y = labelh(&def.font);
2006-06-08 12:54:19 +04:00
if((f->area->sel == f) || (f->area->mode != Colstack)) {
c->rect.width = f->rect.width - 2 * def.border;
2006-10-12 18:12:22 +04:00
c->rect.height = f->rect.height - def.border - labelh(&def.font);
2006-03-01 13:30:44 +03:00
}
XMoveResizeWindow(blz.dpy, c->win, c->rect.x, c->rect.y,
c->rect.width, c->rect.height);
configure_client(c);
}
void
2006-10-12 18:10:57 +04:00
newcol_client(Client *c, char *arg) {
2006-06-08 12:54:19 +04:00
Frame *f = c->sel;
Area *to, *a = f->area;
View *v = a->view;
if(a->floating)
2006-06-08 12:54:19 +04:00
return;
if(!f->anext && f == a->frame)
return;
if(!strncmp(arg, "prev", 5)) {
2006-06-08 12:54:19 +04:00
for(to=v->area; to && to->next != a; to=to->next);
to = new_column(v, to, 0);
2006-06-25 17:16:03 +04:00
send_to_area(to, a, f);
}
else if(!strncmp(arg, "next", 5)) {
2006-06-08 12:54:19 +04:00
to = new_column(v, a, 0);
2006-06-25 17:16:03 +04:00
send_to_area(to, a, f);
}
else
return;
flush_masked_events(EnterWindowMask);
}
void
2006-10-12 18:10:57 +04:00
move_client(Client *c, char *arg) {
2006-06-08 12:54:19 +04:00
Frame *f = c->sel;
XRectangle new = f->rect;
2006-05-29 13:18:18 +04:00
int x, y;
2006-05-29 13:18:18 +04:00
if(sscanf(arg, "%d %d", &x, &y) != 2)
return;
2006-05-29 13:18:18 +04:00
new.x += x;
new.y += y;
2006-06-28 06:16:14 +04:00
if(!f->area->floating)
2007-02-05 09:00:42 +03:00
resize_column(f->client, &new);
else
resize_client(f->client, &new);
}
void
2006-10-12 18:10:57 +04:00
size_client(Client *c, char *arg) {
2006-06-08 12:54:19 +04:00
Frame *f = c->sel;
XRectangle new = f->rect;
2006-05-29 13:18:18 +04:00
int w, h;
2006-05-29 13:18:18 +04:00
if(sscanf(arg, "%d %d", &w, &h) != 2)
return;
2006-05-29 13:18:18 +04:00
new.width += w;
new.height += h;
if(!f->area->floating)
2007-02-05 09:00:42 +03:00
resize_column(f->client, &new);
else
resize_client(f->client, &new);
}
char *
2006-10-12 18:10:57 +04:00
send_client(Frame *f, char *arg) {
Area *to, *a;
Client *c;
Frame *tf;
View *v;
2006-06-28 06:16:14 +04:00
int j;
2006-06-20 02:25:49 +04:00
a = f->area;
v = a->view;
c = f->client;
2006-06-28 06:16:14 +04:00
if(!strncmp(arg, "toggle", 7)) {
if(!a->floating)
2006-06-08 12:54:19 +04:00
to = v->area;
else if(c->revert && !c->revert->floating)
to = c->revert;
else
2006-06-08 12:54:19 +04:00
to = v->area->next;
2006-06-25 17:16:03 +04:00
send_to_area(to, a, f);
2006-06-28 06:16:14 +04:00
}else if(!a->floating) {
if(!strncmp(arg, "left", 5)) {
if(a->floating)
return Ebadvalue;
for(to=v->area->next; to && a != to->next; to=to->next);
if(!to && (f->anext || f != a->frame))
to=new_column(v, v->area, 0);
if(!to)
return Ebadvalue;
send_to_area(to, a, f);
}
else if(!strncmp(arg, "right", 5)) {
if(a->floating)
return Ebadvalue;
if(!(to = a->next) && (f->anext || f!= a->frame))
to = new_column(v, a, 0);
if(!to)
return Ebadvalue;
send_to_area(to, a, f);
}
else if(!strncmp(arg, "up", 3)) {
for(tf=a->frame; tf && tf->anext != f; tf=tf->anext);
if(!tf)
return Ebadvalue;
remove_frame(f);
insert_frame(tf, f, True);
arrange_column(a, False);
focus_client(c, True);
}
else if(!strncmp(arg, "down", 5)) {
if(!f->anext)
return Ebadvalue;
remove_frame(f);
insert_frame(f->anext, f, False);
arrange_column(a, False);
focus_client(c, True);
}
else {
if(sscanf(arg, "%d", &j) != 1)
return Ebadvalue;
for(to=v->area; to && j; to=to->next, j--);
send_to_area(to, a, f);
}
}else
return Ebadvalue;
flush_masked_events(EnterWindowMask);
if(f->view == screen->sel)
focus(f->client, True);
2006-06-25 17:16:03 +04:00
update_views();
2007-02-05 05:02:05 +03:00
return nil;
}
/* convenience function */
void
2006-10-12 18:10:57 +04:00
focus(Client *c, Bool restack) {
View *v;
Frame *f;
if(!(f = c->sel)) return;
v = f->area->view;
arrange_column(f->area, False);
focus_client(c, restack);
focus_view(screen, v);
}
2006-03-07 19:22:36 +03:00
2006-06-25 17:16:03 +04:00
void
2006-10-12 18:10:57 +04:00
update_client_views(Client *c, char **tags) {
2006-06-25 17:16:03 +04:00
int cmp;
Frame *f;
Frame **fp = &c->frame;
2006-10-12 18:10:57 +04:00
2006-06-25 17:16:03 +04:00
while(*fp || *tags) {
while(*fp && (!*tags || (cmp=strcmp((*fp)->view->name, *tags)) < 0)) {
f = *fp;
detach_from_area(f->area, f);
*fp = f->cnext;
free(f);
if(c->sel == f)
c->sel = *fp;
}
if(*tags) {
if(!*fp || cmp > 0) {
f = create_frame(c, get_view(*tags));
if(f->view == screen->sel || !c->sel)
2006-06-25 17:16:03 +04:00
c->sel = f;
attach_to_view(f->view, f);
f->cnext = *fp;
*fp = f;
}
if(*fp) fp=&(*fp)->cnext;
tags++;
}
}
focus_view(screen, screen->sel);
}
static int
compare_tags(const void *a, const void *b) {
return strcmp(*(char **)a, *(char **)b);
}
void
2006-10-12 18:10:57 +04:00
apply_tags(Client *c, const char *tags) {
unsigned int i, j, n;
int len;
char buf[256];
2006-06-25 17:16:03 +04:00
char *toks[32];
2006-10-12 18:10:57 +04:00
strncpy(buf, tags, sizeof(buf));
trim(buf, " \t/");
2006-10-12 18:10:57 +04:00
if(!(n = ixp_tokenize(toks, 31, buf, '+')))
return;
for(i=0, j=0; i < n; i++) {
if(!strncmp(toks[i], "~", 2))
c->floating = True;
2006-06-25 17:16:03 +04:00
else if(!strncmp(toks[i], "!", 2))
2007-02-05 05:02:05 +03:00
toks[j++] = view ? screen->sel->name : "nil";
else if(strncmp(toks[i], "sel", 4)
&& strncmp(toks[i], ".", 2)
&& strncmp(toks[i], "..", 3))
2006-06-25 17:16:03 +04:00
toks[j++] = toks[i];
}
c->tags[0] = '\0';
qsort(toks, j, sizeof(char *), compare_tags);
len = sizeof(c->tags);
if(!j) return;
for(i=0, n=0; i < j && len > 1; i++)
2006-06-25 17:16:03 +04:00
if(!n || strcmp(toks[i], toks[n-1])) {
if(i)
strncat(c->tags, "+", len);
2006-10-12 18:10:57 +04:00
len -= strlen(c->tags);
strncat(c->tags, toks[i], len);
len -= strlen(c->tags);
2006-06-25 17:16:03 +04:00
toks[n++] = toks[i];
}
2007-02-05 05:02:05 +03:00
toks[n] = nil;
2006-06-25 17:16:03 +04:00
update_client_views(c, toks);
2006-07-03 20:41:14 +04:00
XChangeProperty(blz.dpy, c->win, tags_atom, XA_STRING, 8,
PropModeReplace, (unsigned char *)c->tags, strlen(c->tags));
}
static void
2006-10-12 18:10:57 +04:00
match_tags(Client *c, const char *prop) {
2006-06-08 12:54:19 +04:00
Rule *r;
regmatch_t tmpregm;
2006-06-17 15:32:49 +04:00
for(r=def.tagrules.rule; r; r=r->next)
if(!regexec(&r->regex, prop, 1, &tmpregm, 0))
2007-02-05 05:02:05 +03:00
if(!strlen(c->tags) || !strncmp(c->tags, "nil", 4))
apply_tags(c, r->value);
}
void
2006-10-12 18:10:57 +04:00
apply_rules(Client *c) {
2006-06-17 15:32:49 +04:00
if(def.tagrules.string)
2006-06-02 09:13:23 +04:00
match_tags(c, c->props);
if(!strlen(c->tags))
2007-02-05 05:02:05 +03:00
apply_tags(c, "nil");
}
char *
2006-10-12 18:10:57 +04:00
message_client(Client *c, char *message) {
if(!strncmp(message, "kill", 5)) {
kill_client(c);
2007-02-05 05:02:05 +03:00
return nil;
}
return Ebadcmd;
}