wmii/cmd/wm/client.c

339 lines
7.7 KiB
C
Raw Normal View History

2005-11-18 18:54:58 +03:00
/*
* (C)opyright MMIV-MMV Anselm R. Garbe <garbeam at gmail dot com>
* See LICENSE file for license details.
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xatom.h>
#include "wm.h"
2005-12-05 01:45:59 +03:00
Client *alloc_client(Window w)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
static int id = 0;
char buf[MAX_BUF];
char buf2[MAX_BUF];
Client *c = (Client *) cext_emallocz(sizeof(Client));
2005-11-18 18:54:58 +03:00
c->win = w;
2005-12-09 00:12:12 +03:00
snprintf(buf, MAX_BUF, "/detached/client/%d", id);
2005-12-05 22:38:03 +03:00
c->file[C_PREFIX] = ixp_create(ixps, buf);
2005-11-18 18:54:58 +03:00
win_prop(dpy, c->win, XA_WM_NAME, buf2, MAX_BUF);
2005-12-09 00:12:12 +03:00
snprintf(buf, MAX_BUF, "/detached/client/%d/name", id);
2005-12-05 22:38:03 +03:00
c->file[C_NAME] = wmii_create_ixpfile(ixps, buf, buf2);
2005-11-18 18:54:58 +03:00
id++;
cext_attach_item(&clients, c);
2005-11-18 18:54:58 +03:00
return c;
}
2005-12-06 00:22:24 +03:00
void sel_client(Client * c)
{
Frame *f = 0;
2005-12-05 22:38:03 +03:00
/* sel client */
2005-12-06 00:22:24 +03:00
f = c->frame;
cext_stack_top_item(&f->clients, c);
2005-12-06 00:22:24 +03:00
f->file[F_SEL_CLIENT]->content = c->file[C_PREFIX]->content;
XRaiseWindow(dpy, c->win);
XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
2005-12-05 04:50:02 +03:00
invoke_wm_event(def[WM_EVENT_CLIENT_UPDATE]);
}
2005-12-05 01:45:59 +03:00
void set_client_state(Client * c, int state)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
long data[2];
2005-11-18 18:54:58 +03:00
data[0] = (long) state;
data[1] = (long) None;
2005-12-11 17:47:23 +03:00
XChangeProperty(dpy, c->win, wm_state, wm_state, 32, PropModeReplace, (unsigned char *) data, 2);
2005-11-18 18:54:58 +03:00
}
2005-12-05 01:45:59 +03:00
void show_client(Client * c)
2005-11-18 18:54:58 +03:00
{
XMapWindow(dpy, c->win);
set_client_state(c, NormalState);
grab_client(c, Mod1Mask, Button1);
grab_client(c, Mod1Mask, Button3);
}
2005-12-05 01:45:59 +03:00
void hide_client(Client * c)
2005-11-18 18:54:58 +03:00
{
ungrab_client(c, AnyModifier, AnyButton);
XUnmapWindow(dpy, c->win);
set_client_state(c, WithdrawnState);
}
2005-12-05 01:45:59 +03:00
void reparent_client(Client * c, Window w, int x, int y)
2005-11-18 18:54:58 +03:00
{
XReparentWindow(dpy, c->win, w, x, y);
2005-12-12 01:52:04 +03:00
c->ignore_unmap++;
2005-11-18 18:54:58 +03:00
}
2005-12-05 01:45:59 +03:00
void grab_client(Client * c, unsigned long mod, unsigned int button)
2005-11-18 18:54:58 +03:00
{
XGrabButton(dpy, button, mod, c->win, False,
2005-12-05 01:45:59 +03:00
ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None);
2005-11-18 18:54:58 +03:00
if ((mod != AnyModifier) && num_lock_mask) {
2005-12-11 17:47:23 +03:00
XGrabButton(dpy, button, mod | num_lock_mask, c->win, False, ButtonPressMask,
GrabModeAsync, GrabModeAsync, None, None);
XGrabButton(dpy, button, mod | num_lock_mask | LockMask, c->win, False, ButtonPressMask,
GrabModeAsync, GrabModeAsync, None, None);
2005-11-18 18:54:58 +03:00
}
}
2005-12-05 01:45:59 +03:00
void ungrab_client(Client * c, unsigned long mod, unsigned int button)
2005-11-18 18:54:58 +03:00
{
XUngrabButton(dpy, button, mod, c->win);
if (mod != AnyModifier && num_lock_mask) {
XUngrabButton(dpy, button, mod | num_lock_mask, c->win);
XUngrabButton(dpy, button, mod | num_lock_mask | LockMask, c->win);
}
}
2005-12-05 01:45:59 +03:00
void configure_client(Client * c)
2005-11-18 18:54:58 +03:00
{
XConfigureEvent e;
e.type = ConfigureNotify;
e.event = c->win;
e.window = c->win;
e.x = c->rect.x;
e.y = c->rect.y;
if (c->frame) {
e.x += c->frame->rect.x;
e.y += c->frame->rect.y;
2005-11-18 18:54:58 +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);
}
2005-12-05 01:45:59 +03:00
void close_client(Client * c)
2005-11-18 18:54:58 +03:00
{
if (c->proto & PROTO_DEL)
send_message(dpy, c->win, wm_protocols, wm_delete);
else
XKillClient(dpy, c->win);
}
2005-12-05 22:38:03 +03:00
void init_client(Client * c, XWindowAttributes * wa)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
long msize;
2005-11-18 18:54:58 +03:00
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);
/* size hints */
if (!XGetWMNormalHints(dpy, c->win, &c->size, &msize)
2005-12-05 01:45:59 +03:00
|| !c->size.flags)
2005-11-18 18:54:58 +03:00
c->size.flags = PSize;
XAddToSaveSet(dpy, c->win);
}
2005-12-05 01:45:59 +03:00
void handle_client_property(Client * c, XPropertyEvent * e)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
char buf[1024];
long msize;
2005-11-18 18:54:58 +03:00
buf[0] = 0;
2005-11-18 18:54:58 +03:00
if (e->state == PropertyDelete)
2005-12-05 01:45:59 +03:00
return; /* ignore */
2005-11-18 18:54:58 +03:00
if (e->atom == wm_protocols) {
/* update */
c->proto = win_proto(c->win);
return;
}
switch (e->atom) {
case XA_WM_NAME:
win_prop(dpy, c->win, XA_WM_NAME, buf, sizeof(buf));
if (strlen(buf)) {
2005-12-05 22:38:03 +03:00
if (c->file[C_NAME]->content)
free(c->file[C_NAME]->content);
c->file[C_NAME]->content = cext_estrdup(buf);
2005-12-05 22:38:03 +03:00
c->file[C_NAME]->size = strlen(buf);
2005-11-18 18:54:58 +03:00
}
if (c->frame)
draw_client(c, nil);
2005-12-05 04:50:02 +03:00
invoke_wm_event(def[WM_EVENT_CLIENT_UPDATE]);
2005-11-18 18:54:58 +03:00
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)
2005-12-05 01:45:59 +03:00
|| !c->size.flags) {
2005-11-18 18:54:58 +03:00
c->size.flags = PSize;
}
break;
}
}
2005-12-05 22:38:03 +03:00
void destroy_client(Client * c)
2005-11-18 18:54:58 +03:00
{
2005-12-11 17:47:23 +03:00
cext_detach_item(&detached, c);
cext_detach_item(&clients, c);
2005-12-05 22:38:03 +03:00
ixp_remove_file(ixps, c->file[C_PREFIX]);
2005-11-18 18:54:58 +03:00
free(c);
}
/* speed reasoned function for client property change */
void draw_client(void *item, void *aux)
2005-11-18 18:54:58 +03:00
{
Client *c = item;
2005-12-05 01:45:59 +03:00
Frame *f = c->frame;
unsigned int tw, tabh = tab_height(f);
size_t size;
int i;
2005-12-09 02:18:10 +03:00
Draw d = { 0 };
2005-11-18 18:54:58 +03:00
if (!tabh)
return;
2005-12-09 02:18:10 +03:00
size = cext_sizeof(&f->clients);
tw = f->rect.width;
2005-11-18 18:54:58 +03:00
if (size)
tw /= size;
i = cext_list_get_item_index(&f->clients, c);
2005-12-09 02:18:10 +03:00
d.drawable = f->win;
d.gc = f->gc;
d.rect.x = i * tw;
d.rect.y = 0;
d.rect.width = tw;
if (i && (i == size - 1))
d.rect.width = f->rect.width - d.rect.x;
d.rect.height = tabh;
d.data = c->file[C_NAME]->content;
d.font = font;
if ((f == get_sel_frame()) && (c == get_sel_client())) {
d.bg = blitz_loadcolor(dpy, screen_num, f->file[F_SEL_BG_COLOR]->content);
d.fg = blitz_loadcolor(dpy, screen_num, f->file[F_SEL_FG_COLOR]->content);
d.border = blitz_loadcolor(dpy, screen_num, f->file[F_SEL_BORDER_COLOR]->content);
} else {
d.bg = blitz_loadcolor(dpy, screen_num, f->file[F_NORM_BG_COLOR]->content);
d.fg = blitz_loadcolor(dpy, screen_num, f->file[F_NORM_FG_COLOR]->content);
d.border = blitz_loadcolor(dpy, screen_num, f->file[F_NORM_BORDER_COLOR]->content);
}
blitz_drawlabel(dpy, &d);
XSync(dpy, False);
2005-11-18 18:54:58 +03:00
}
2005-12-05 01:45:59 +03:00
void draw_clients(Frame * f)
2005-11-18 18:54:58 +03:00
{
cext_list_iterate(&f->clients, 0, draw_client);
2005-11-18 18:54:58 +03:00
}
2005-12-05 01:45:59 +03:00
void gravitate(Client * c, unsigned int tabh, unsigned int bw, int invert)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int dx = 0, dy = 0;
int gravity = NorthWestGravity;
2005-11-18 18:54:58 +03:00
if (c->size.flags & PWinGravity) {
gravity = c->size.win_gravity;
}
/* y */
switch (gravity) {
case StaticGravity:
case NorthWestGravity:
case NorthGravity:
case NorthEastGravity:
dy = tabh;
break;
case EastGravity:
case CenterGravity:
case WestGravity:
dy = -(c->rect.height / 2) + tabh;
break;
case SouthEastGravity:
case SouthGravity:
case SouthWestGravity:
dy = -c->rect.height;
break;
2005-12-05 01:45:59 +03:00
default: /* don't care */
2005-11-18 18:54:58 +03:00
break;
}
/* x */
switch (gravity) {
case StaticGravity:
case NorthWestGravity:
case WestGravity:
case SouthWestGravity:
dx = bw;
break;
case NorthGravity:
case CenterGravity:
case SouthGravity:
dx = -(c->rect.width / 2) + bw;
break;
case NorthEastGravity:
case EastGravity:
case SouthEastGravity:
dx = -(c->rect.width + bw);
break;
2005-12-05 01:45:59 +03:00
default: /* don't care */
2005-11-18 18:54:58 +03:00
break;
}
if (invert) {
dx = -dx;
dy = -dy;
}
c->rect.x += dx;
c->rect.y += dy;
}
2005-12-05 22:38:03 +03:00
void attach_client(Client * c)
{
Area *a = 0;
Frame *old = get_sel_frame();
if (!cext_sizeof(&pages))
2005-12-05 22:38:03 +03:00
alloc_page();
/* transient stuff */
a = get_sel_area();
2005-12-05 22:38:03 +03:00
if (c && c->trans) {
Client *t = win_to_client(c->trans);
if (t && t->frame)
a = t->frame->area;
}
cext_attach_item(&a->clients, c);
2005-12-05 22:38:03 +03:00
a->layout->attach(a, c);
2005-12-06 16:22:58 +03:00
if (old)
draw_frame(old, nil);
2005-12-05 22:38:03 +03:00
invoke_wm_event(def[WM_EVENT_PAGE_UPDATE]);
}
2005-12-06 00:22:24 +03:00
2005-12-11 18:24:52 +03:00
void detach_client(Client *c, Bool unmap) {
Page *p;
Frame *f = c->frame;
Area *a = f ? f->area : nil;
if (a) {
2005-12-11 18:24:52 +03:00
a->layout->detach(a, c, unmap);
cext_detach_item(&a->clients, c);
}
2005-12-06 00:22:24 +03:00
if (c->destroyed)
destroy_client(c);
2005-12-11 17:47:23 +03:00
if ((p = get_sel_page())) {
sel_page(p);
draw_page(p);
2005-12-11 17:47:23 +03:00
}
2005-12-06 00:22:24 +03:00
}
Client *get_sel_client()
{
Frame *f = get_sel_frame();
return f ? cext_stack_get_top_item(&f->clients) : nil;
}