wmii/cmd/wm/client.c

662 lines
14 KiB
C
Raw Normal View History

2005-11-18 18:54:58 +03:00
/*
2006-01-20 17:20:24 +03: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.
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xatom.h>
#include "wm.h"
#define CLIENT_MASK (StructureNotifyMask | PropertyChangeMask)
static Vector *
2006-04-13 17:35:10 +04:00
vector_of_clients(ClientVector *cv)
{
return (Vector *) cv;
}
static void
update_client_name(Client *c)
{
XTextProperty name;
XGetTextProperty(dpy, c->win, &name, XA_WM_NAME);
if(name.value) {
cext_strlcpy(c->name, (char *)name.value, sizeof(c->name));
free(name.value);
}
}
2005-12-21 18:18:11 +03:00
Client *
2006-04-12 12:44:07 +04:00
create_client(Window w, XWindowAttributes *wa)
2005-11-18 18:54:58 +03:00
{
Client *c = (Client *) cext_emallocz(sizeof(Client));
XSetWindowAttributes fwa;
2006-03-10 13:59:26 +03:00
XClassHint ch;
long msize;
2006-03-07 19:22:36 +03:00
static unsigned int id = 1;
2005-12-21 18:18:11 +03:00
2006-03-07 19:22:36 +03:00
c->id = id++;
c->win = w;
c->rect.x = wa->x;
c->rect.y = wa->y;
c->border = wa->border_width;
c->rect.width = wa->width + 2 * c->border;
c->rect.height = wa->height + 2 * c->border;
XSetWindowBorderWidth(dpy, c->win, 0);
c->proto = win_proto(c->win);
XGetTransientForHint(dpy, c->win, &c->trans);
if(!XGetWMNormalHints(dpy, c->win, &c->size, &msize) || !c->size.flags)
c->size.flags = PSize;
XAddToSaveSet(dpy, c->win);
update_client_name(c);
2006-03-10 17:41:57 +03:00
if(XGetClassHint(dpy, c->win, &ch)) {
snprintf(c->classinst, sizeof(c->classinst), "%s:%s", ch.res_class,
ch.res_name);
XFree(ch.res_class);
XFree(ch.res_name);
2006-03-10 17:41:57 +03:00
}
fwa.override_redirect = 1;
fwa.background_pixmap = ParentRelative;
fwa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
| ExposureMask | ButtonPressMask;
c->framewin = XCreateWindow(dpy, root, c->rect.x, c->rect.y,
c->rect.width + 2 * def.border,
2006-04-12 12:44:07 +04:00
c->rect.height + def.border + height_of_bar(), 0,
DefaultDepth(dpy, screen), CopyFromParent,
DefaultVisual(dpy, screen),
CWOverrideRedirect | CWBackPixmap | CWEventMask, &fwa);
c->gc = XCreateGC(dpy, c->framewin, 0, 0);
XSync(dpy, False);
2006-04-13 17:35:10 +04:00
cext_vattach(vector_of_clients(&client), c);
return c;
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void
set_client_state(Client * c, int state)
2005-11-18 18:54:58 +03:00
{
long data[2];
2005-11-18 18:54:58 +03:00
data[0] = (long) state;
data[1] = (long) None;
XChangeProperty(dpy, c->win, wm_atom[WMState], wm_atom[WMState], 32,
PropModeReplace, (unsigned char *) data, 2);
2005-11-18 18:54:58 +03:00
}
void
focus_client(Client *c)
2006-01-26 14:39:20 +03:00
{
Client *old = sel_client();
Frame *f = c->frame.data[c->sel];
View *v = f->area->view;
2006-04-12 12:44:07 +04:00
int i = idx_of_area(f->area);
v->sel = i;
2006-04-12 12:44:07 +04:00
f->area->sel = idx_of_frame(f);
if(old)
draw_client(old);
restack_view(v);
XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
draw_client(c);
XSync(dpy, False);
if(i > 0 && f->area->mode == Colstack)
arrange_column(f->area, False);
}
2005-12-21 18:18:11 +03:00
void
map_client(Client *c)
2005-11-18 18:54:58 +03:00
{
XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
XMapWindow(dpy, c->win);
XSelectInput(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
unmap_client(Client *c)
2005-11-18 18:54:58 +03:00
{
ungrab_mouse(c->win, AnyModifier, AnyButton);
XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
XUnmapWindow(dpy, c->win);
XSelectInput(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-01-25 21:41:12 +03:00
reparent_client(Client *c, Window w, int x, int y)
2005-11-18 18:54:58 +03:00
{
XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
XReparentWindow(dpy, c->win, w, x, y);
XSelectInput(dpy, c->win, CLIENT_MASK);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void
configure_client(Client *c)
2005-11-18 18:54:58 +03:00
{
XConfigureEvent e;
Frame *f = c->frame.data[c->sel];
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;
XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *) & e);
XSelectInput(dpy, c->win, CLIENT_MASK);
XSync(dpy, False);
2005-11-18 18:54:58 +03:00
}
static void
send_client_message(Window w, Atom a, long value)
{
XEvent e;
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(dpy, w, False, NoEventMask, &e);
XSync(dpy, False);
}
2005-12-21 18:18:11 +03:00
void
2006-02-16 17:28:51 +03:00
kill_client(Client * c)
2005-11-18 18:54:58 +03:00
{
if(c->proto & WM_PROTOCOL_DELWIN)
send_client_message(c->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
else
XKillClient(dpy, c->win);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void
2006-04-12 12:44:07 +04:00
prop_client(Client *c, XPropertyEvent *e)
2005-11-18 18:54:58 +03:00
{
long msize;
if(e->atom == wm_atom[WMProtocols]) {
/* update */
c->proto = win_proto(c->win);
return;
}
switch (e->atom) {
case XA_WM_NAME:
update_client_name(c);
if(c->frame.size)
draw_client(c);
break;
case XA_WM_TRANSIENT_FOR:
XGetTransientForHint(dpy, c->win, &c->trans);
break;
case XA_WM_NORMAL_HINTS:
if(!XGetWMNormalHints(dpy, c->win, &c->size, &msize) || !c->size.flags) {
c->size.flags = PSize;
}
break;
}
2005-11-18 18:54:58 +03:00
}
/* speed reasoned function for client property change */
2005-12-21 18:18:11 +03:00
void
draw_client(Client *c)
2005-11-18 18:54:58 +03:00
{
BlitzDraw d = { 0 };
2005-12-21 18:18:11 +03:00
if(!c->frame.size)
return; /* might not have been attached atm */
2006-03-05 02:25:49 +03:00
d.drawable = c->framewin;
d.font = blitzfont;
2006-03-05 02:25:49 +03:00
d.gc = c->gc;
2006-01-26 14:39:20 +03:00
if(c == sel_client())
d.color = def.sel;
else
d.color = def.norm;
2006-01-26 14:39:20 +03:00
/* draw border */
if(def.border) {
d.rect = c->frame.data[c->sel]->rect;
d.rect.x = d.rect.y = 0;
d.notch = &c->rect;
blitz_drawlabel(dpy, &d);
blitz_drawborder(dpy, &d);
}
d.rect.x = 0;
d.rect.y = 0;
2006-04-12 12:44:07 +04:00
d.rect.height = height_of_bar();
2006-02-02 22:36:10 +03:00
d.notch = nil;
/* max mode bar */
if(c->frame.data[c->sel]->area->mode == Colmax) {
unsigned long tmp = d.color.fg;
char buf[256];
d.color.fg = d.color.bg;
d.color.bg = tmp;
snprintf(buf, sizeof(buf), "%d/%d",
idx_of_frame(c->frame.data[c->sel]) + 1,
c->frame.data[c->sel]->area->frame.size);
d.align = CENTER;
d.rect.width = d.rect.height + XTextWidth(blitzfont.font, buf, strlen(buf));
d.data = buf;
blitz_drawlabel(dpy, &d);
blitz_drawborder(dpy, &d);
d.color.bg = d.color.fg;
d.color.fg = tmp;
d.rect.x += d.rect.width;
}
/* tag bar */
d.rect.width = d.rect.height + XTextWidth(blitzfont.font, c->tags, strlen(c->tags));
2006-04-06 13:31:54 +04:00
d.data = c->tags;
blitz_drawlabel(dpy, &d);
blitz_drawborder(dpy, &d);
d.rect.x += d.rect.width;
/* title bar */
d.align = WEST;
d.rect.width = c->frame.data[c->sel]->rect.width - d.rect.x;
d.data = c->name;
blitz_drawlabel(dpy, &d);
blitz_drawborder(dpy, &d);
XSync(dpy, False);
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void
2006-04-12 12:44:07 +04:00
gravitate_client(Client *c, Bool invert)
2005-11-18 18:54:58 +03:00
{
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-04-12 12:44:07 +04:00
dy = height_of_bar();
break;
case EastGravity:
case CenterGravity:
case WestGravity:
2006-04-12 12:44:07 +04:00
dy = -(c->rect.height / 2) + height_of_bar();
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
manage_client(Client *c)
{
2006-03-11 03:32:10 +03:00
Client *trans;
2006-04-12 12:44:07 +04:00
if(c->trans && (trans = client_of_win(c->trans)))
2006-04-06 14:19:20 +04:00
cext_strlcpy(c->tags, trans->tags, sizeof(c->tags));
2006-03-11 03:32:10 +03:00
else
2006-04-12 12:44:07 +04:00
apply_rules(c);
reparent_client(c, c->framewin, c->rect.x, c->rect.y);
2006-04-13 17:35:10 +04:00
update_views();
2005-12-05 22:38:03 +03:00
}
2005-12-06 00:22:24 +03:00
static int
dummy_error_handler(Display *dpy, XErrorEvent *error)
{
return 0;
}
2006-03-06 01:38:50 +03:00
void
destroy_client(Client *c)
2006-03-06 01:38:50 +03:00
{
unsigned int i;
XGrabServer(dpy);
XSetErrorHandler(dummy_error_handler);
for(i = 0; i < view.size; i++)
2006-04-12 12:44:07 +04:00
detach_from_view(view.data[i], c);
unmap_client(c);
if(c->frame.size) {
c->rect.x = c->frame.data[c->sel]->rect.x;
c->rect.y = c->frame.data[c->sel]->rect.y;
}
reparent_client(c, root, c->rect.x, c->rect.y);
XFreeGC(dpy, c->gc);
XDestroyWindow(dpy, c->framewin);
2006-04-13 17:35:10 +04:00
cext_vdetach(vector_of_clients(&client), c);
update_views();
free(c);
XSync(dpy, False);
XSetErrorHandler(wmii_error_handler);
XUngrabServer(dpy);
2006-03-06 01:38:50 +03:00
}
2006-01-26 20:29:49 +03:00
Client *
sel_client()
{
return view.size ? sel_client_of_view(view.data[sel]) : nil;
2006-01-26 20:29:49 +03:00
}
static void
match_sizehints(Client *c)
{
XSizeHints *s = &c->size;
if(s->flags & PMinSize) {
if(c->rect.width < c->size.min_width)
c->rect.width = c->size.min_width;
if(c->rect.height < c->size.min_height)
c->rect.height = c->size.min_height;
}
if(s->flags & PMaxSize) {
if(c->rect.width > c->size.max_width)
c->rect.width = c->size.max_width;
if(c->rect.height > c->size.max_height)
c->rect.height = c->size.max_height;
}
if(s->flags & PResizeInc) {
int w = 0, h = 0;
if(c->size.flags & PBaseSize) {
w = c->size.base_width;
h = c->size.base_height;
} else if(c->size.flags & PMinSize) {
/* base_{width,height} default to min_{width,height} */
w = c->size.min_width;
h = c->size.min_height;
}
/* client_width = base_width + i * c->size.width_inc for an integer i */
w = c->frame.data[c->sel]->rect.width - 2 * def.border - w;
if(s->width_inc > 0)
c->frame.data[c->sel]->rect.width -= w % s->width_inc;
2006-04-12 12:44:07 +04:00
h = c->frame.data[c->sel]->rect.height - def.border - height_of_bar() - h;
if(s->height_inc > 0)
c->frame.data[c->sel]->rect.height -= h % s->height_inc;
}
}
void
resize_client(Client *c, XRectangle *r, Bool ignore_xcall)
{
Frame *f = c->frame.data[c->sel];
f->rect = *r;
2006-04-12 12:44:07 +04:00
if((f->area->mode != Colstack) || (f->area->sel == idx_of_frame(f)))
match_sizehints(c);
if(!ignore_xcall) {
if(f->area->view == view.data[sel])
XMoveResizeWindow(dpy, c->framewin, f->rect.x,
f->rect.y, f->rect.width, f->rect.height);
else
XMoveResizeWindow(dpy, c->framewin, 2 * rect.width + f->rect.x,
f->rect.y, f->rect.width, f->rect.height);
}
2006-04-12 12:44:07 +04:00
if((f->area->mode != Colstack) || (f->area->sel == idx_of_frame(f))) {
c->rect.x = def.border;
2006-04-12 12:44:07 +04:00
c->rect.y = height_of_bar();
c->rect.width = f->rect.width - 2 * def.border;
2006-04-12 12:44:07 +04:00
c->rect.height = f->rect.height - def.border - height_of_bar();
XMoveResizeWindow(dpy, c->win, c->rect.x, c->rect.y,
c->rect.width, c->rect.height);
2006-03-01 13:30:44 +03:00
configure_client(c);
}
}
2006-02-13 12:31:38 +03:00
void
2006-02-14 14:06:16 +03:00
select_client(Client *c, char *arg)
2006-02-13 12:31:38 +03:00
{
Frame *f = c->frame.data[c->sel];
Area *a = f->area;
2006-04-12 12:44:07 +04:00
int i = idx_of_frame(f);
if(i == -1)
return;
if(!strncmp(arg, "prev", 5)) {
if(!i)
i = a->frame.size - 1;
else
i--;
} else if(!strncmp(arg, "next", 5)) {
if(i + 1 < a->frame.size)
i++;
else
i = 0;
}
2006-02-13 12:31:38 +03:00
else {
const char *errstr;
i = cext_strtonum(arg, 0, a->frame.size - 1, &errstr);
if(errstr)
2006-02-14 14:53:40 +03:00
return;
2006-02-13 12:31:38 +03:00
}
focus_client(a->frame.data[i]->client);
2006-02-13 12:31:38 +03:00
}
void
swap_client(Client *c, char *arg)
{
2006-04-07 18:04:47 +04:00
Frame *f1 = c->frame.data[c->sel], *f2;
Area *o, *a = f1->area;
View *v = a->view;
2006-04-12 12:44:07 +04:00
int i = idx_of_area(a), j = idx_of_frame(f1);
if(i == -1 || j == -1)
return;
2006-03-27 21:09:30 +04:00
if(!strncmp(arg, "prev", 5) && i) {
if(i == 1)
o = v->area.data[v->area.size - 1];
else
o = v->area.data[i - 1];
goto Swaparea;
}
2006-03-27 21:09:30 +04:00
else if(!strncmp(arg, "next", 5) && i) {
if(i < v->area.size - 1)
o = v->area.data[i + 1];
else
o = v->area.data[1];
Swaparea:
if(o == a)
return;
2006-04-07 18:04:47 +04:00
f2 = o->frame.data[o->sel];
f1->client = f2->client;
f2->client = c;
f1->client->frame.data[f1->client->sel] = f1;
f2->client->frame.data[f2->client->sel] = f2;
arrange_column(o, False);
}
2006-03-27 21:09:30 +04:00
else if(!strncmp(arg, "up", 3) && i) {
if(j)
i = j - 1;
else
i = a->frame.size - 1;
a->frame.data[j] = a->frame.data[i];
2006-04-07 18:04:47 +04:00
a->frame.data[i] = f1;
}
2006-03-27 21:09:30 +04:00
else if(!strncmp(arg, "down", 5) && i) {
if(j + 1 < a->frame.size)
i = j + 1;
else
i = 0;
a->frame.data[j] = a->frame.data[i];
2006-04-07 18:04:47 +04:00
a->frame.data[i] = f1;
}
2006-04-12 12:44:07 +04:00
if(idx_of_area(a))
arrange_column(a, False);
focus_client(c);
}
void
2006-04-12 12:44:07 +04:00
send_client_to(Client *c, char *arg)
2006-03-02 19:38:15 +03:00
{
const char *errstr;
Frame *f = c->frame.data[c->sel];
Area *to, *a = f->area;
View *v = a->view;
2006-04-12 12:44:07 +04:00
int i = idx_of_area(a);
if(i == -1)
return;
if(!strncmp(arg, "prev", 5) && i) {
if(i > 1)
to = v->area.data[i - 1];
else if(a->frame.size > 1) {
if(!(to = new_left_column(v)))
return;
}
else
return;
}
else if(!strncmp(arg, "next", 5) && i) {
if(i < v->area.size - 1)
to = v->area.data[i + 1];
else if(a->frame.size > 1) {
if(!(to = new_right_column(v)))
return;
}
else
return;
}
else if(!strncmp(arg, "toggle", 7)) {
if(i)
to = v->area.data[0];
else if(c->revert && c->revert != v->area.data[0])
to = c->revert;
else
to = v->area.data[1];
}
else {
i = cext_strtonum(arg, 0, v->area.size - 1, &errstr);
if(errstr)
return;
to = v->area.data[i];
}
2006-04-12 12:44:07 +04:00
send_to_area(to, a, c);
}
void
resize_all_clients()
{
unsigned int i;
for(i = 0; i < client.size; i++) {
Client *c = client.data[i];
if(c->frame.size && c->frame.data[c->sel]->area) {
2006-04-12 12:44:07 +04:00
if(idx_of_area(c->frame.data[c->sel]->area))
resize_column(c, &c->frame.data[c->sel]->rect, nil);
else
resize_client(c, &c->frame.data[c->sel]->rect, False);
}
}
}
/* convenience function */
void
focus(Client *c)
{
Frame *f = c->frame.size ? c->frame.data[c->sel] : nil;
View *v;
if(!f)
return;
v = f->area->view;
if(view.data[sel] != v)
focus_view(v);
focus_client(c);
}
2006-03-07 19:22:36 +03:00
int
2006-04-12 12:44:07 +04:00
idx_of_client_id(unsigned short id)
2006-03-07 19:22:36 +03:00
{
int i;
for(i = 0; i < client.size; i++)
if(client.data[i]->id == id)
2006-03-07 19:22:36 +03:00
return i;
return -1;
}
2006-04-12 12:44:07 +04:00
Client *
client_of_win(Window w)
{
unsigned int i;
for(i = 0; (i < client.size) && client.data[i]; i++)
if(client.data[i]->win == w)
return client.data[i];
return nil;
}
2006-04-13 17:35:10 +04:00
void
draw_clients()
{
unsigned int i, j;
for(i = 0; i < client.size; i++)
for(j = 0; j < client.data[i]->frame.size; j++)
if(client.data[i]->frame.data[j]->area->view == view.data[sel])
draw_client(client.data[i]);
}