wmii/cmd/wm/page.c

161 lines
3.2 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>
2005-12-27 23:13:50 +03:00
#include <X11/Xatom.h>
2005-11-18 18:54:58 +03:00
#include "wm.h"
2005-12-21 18:18:11 +03:00
Page *
alloc_page()
2005-11-18 18:54:58 +03:00
{
2006-02-03 20:11:22 +03:00
static unsigned short id = 1;
Page *p = cext_emallocz(sizeof(Page));
2005-12-21 18:18:11 +03:00
p->id = id++;
alloc_area(p);
page = (Page **)cext_array_attach((void **)page, p, sizeof(Page *), &pagesz);
npage++;
focus_page(p);
XChangeProperty(dpy, root, net_atoms[NET_NUMBER_OF_DESKTOPS], XA_CARDINAL,
32, PropModeReplace, (unsigned char *) &npage, 1);
return p;
2005-11-18 18:54:58 +03:00
}
2005-12-21 18:18:11 +03:00
void
2006-01-26 16:16:04 +03:00
destroy_page(Page *p)
{
2006-01-26 16:16:04 +03:00
unsigned int i;
2006-02-16 13:53:10 +03:00
Page *new = p->revert;
for(i = 0; i < p->narea; i++)
destroy_area(p->area[i]);
free(p->area);
2006-01-26 16:16:04 +03:00
cext_array_detach((void **)page, p, &pagesz);
npage--;
2005-12-27 23:13:50 +03:00
for(i = 0; i < npage; i++) {
if(page[i]->revert == p)
page[i]->revert = nil;
XChangeProperty(dpy, root, net_atoms[NET_NUMBER_OF_DESKTOPS], XA_CARDINAL,
32, PropModeReplace, (unsigned char *) &i, 1);
}
free(p);
2006-02-16 13:53:10 +03:00
if(npage && new)
focus_page(new);
else
write_event("PN -\n");
2005-11-18 18:54:58 +03:00
}
int
page_to_index(Page *p)
{
int i;
for(i = 0; i < npage; i++)
if(p == page[i])
return i;
return -1;
}
2005-12-21 18:18:11 +03:00
void
2006-01-26 16:16:04 +03:00
focus_page(Page *p)
{
Page *old = page ? page[sel] : nil;
char buf[16];
Client *c;
int i = page_to_index(p);
2006-02-16 13:53:10 +03:00
if(!npage || (i == -1))
2006-01-26 16:16:04 +03:00
return;
2006-02-16 13:53:10 +03:00
if(old && old != p)
p->revert = old;
sel = i;
for(i = 0; i < nclient; i++) {
c = client[i];
if(old && (old != p) && (c->area && c->area->page == old))
XMoveWindow(dpy, c->frame.win, 2 * rect.width, 2 * rect.height);
else if(c->area && c->area->page == p) {
XMoveWindow(dpy, c->frame.win, c->frame.rect.x, c->frame.rect.y);
2006-02-08 20:33:01 +03:00
draw_client(c);
}
2006-01-26 16:16:04 +03:00
}
if((c = sel_client_of_page(p)))
2006-02-16 03:55:21 +03:00
focus_client(c, False);
snprintf(buf, sizeof(buf), "PN %d\n", sel + 1);
2006-02-16 13:53:10 +03:00
write_event(buf);
XChangeProperty(dpy, root, net_atoms[NET_CURRENT_DESKTOP], XA_CARDINAL,
32, PropModeReplace, (unsigned char *) &sel, 1);
XSync(dpy, False);
}
2005-12-21 18:18:11 +03:00
XRectangle *
rectangles(unsigned int *num)
2005-11-18 18:54:58 +03:00
{
2005-12-21 18:18:11 +03:00
XRectangle *result = 0;
int i, j = 0;
Window d1, d2;
Window *wins;
XWindowAttributes wa;
XRectangle r;
if(XQueryTree(dpy, root, &d1, &d2, &wins, num)) {
result = cext_emallocz(*num * sizeof(XRectangle));
for(i = 0; i < *num; i++) {
if(!XGetWindowAttributes(dpy, wins[i], &wa))
continue;
if(wa.override_redirect && (wa.map_state == IsViewable)) {
r.x = wa.x;
r.y = wa.y;
r.width = wa.width;
r.height = wa.height;
result[j++] = r;
}
}
}
2006-02-03 13:36:47 +03:00
if(wins)
2005-12-21 18:18:11 +03:00
XFree(wins);
*num = j;
return result;
2005-11-18 18:54:58 +03:00
}
int
pid_to_index(unsigned short id)
{
int i;
for(i = 0; i < npage; i++)
if(page[i]->id == id)
return i;
return -1;
}
void
select_page(char *arg)
{
size_t new = sel;
const char *err;
2006-02-13 12:31:38 +03:00
if(!npage)
return;
if(!strncmp(arg, "prev", 5)) {
if(!new)
new = npage;
new--;
} else if(!strncmp(arg, "next", 5)) {
if(new < npage - 1)
new++;
else
new = 0;
} else {
int idx = cext_strtonum(arg, 1, npage, &err);
if(idx && (idx - 1 < npage))
new = idx - 1;
}
focus_page(page[new]);
}