wmii/cmd/wm/view.c

414 lines
7.8 KiB
C
Raw Normal View History

/*
* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
* See LICENSE file for license details.
*/
#include <stdlib.h>
#include <string.h>
#include "wm.h"
2006-04-11 12:16:01 +04:00
static Vector *
2006-04-13 17:35:10 +04:00
vector_of_views(ViewVector *vv)
{
return (Vector *) vv;
}
2006-04-13 18:52:33 +04:00
static int
comp_view_name(const void *v1, const void *v2)
{
View *vv1 = *(View **)v1;
View *vv2 = *(View **)v2;
return strcmp(vv1->name, vv2->name);
}
View *
2006-05-24 11:26:08 +04:00
create_view(const char *name)
{
static unsigned short id = 1;
View *v = cext_emallocz(sizeof(View));
v->id = id++;
cext_strlcpy(v->name, name, sizeof(v->name));
create_area(v, v->area.size, 0);
create_area(v, v->area.size, 0);
2006-04-13 17:35:10 +04:00
cext_vattach(vector_of_views(&view), v);
2006-04-13 18:52:33 +04:00
qsort(view.data, view.size, sizeof(View *), comp_view_name);
return v;
}
void
destroy_view(View *v)
{
while(v->area.size)
destroy_area(v->area.data[0]);
2006-04-13 17:35:10 +04:00
cext_vdetach(vector_of_views(&view), v);
if(sel >= view.size)
sel = 0;
free(v);
}
int
2006-04-12 12:44:07 +04:00
idx_of_view(View *v)
{
int i;
for(i = 0; i < view.size; i++)
if(v == view.data[i])
return i;
return -1;
}
static void
update_frame_selectors(View *v)
{
unsigned int i, j;
/* select correct frames of clients */
for(i = 0; i < client.size; i++) {
Client *c = client.data[i];
for(j = 0; j < c->frame.size; j++)
if(c->frame.data[j]->area->view == v) {
c->sel = j;
break;
}
}
}
void
focus_view(View *v)
{
Client *c;
unsigned int i;
XGrabServer(dpy);
2006-04-12 12:44:07 +04:00
sel = idx_of_view(v);
update_frame_selectors(v);
/* gives all(!) clients proper geometry (for use of different tags) */
for(i = 0; i < client.size; i++)
if(client.data[i]->frame.size) {
Frame *f = client.data[i]->frame.data[client.data[i]->sel];
if(f->area->view == v) {
XMoveWindow(dpy, client.data[i]->framewin, f->rect.x, f->rect.y);
resize_client(client.data[i], &f->rect, False);
}
else
XMoveWindow(dpy, client.data[i]->framewin,
2 * rect.width + f->rect.x, f->rect.y);
}
if((c = sel_client_of_view(v)))
focus_client(c, True);
draw_clients();
2006-04-12 12:44:07 +04:00
update_view_bars();
XSync(dpy, False);
XUngrabServer(dpy);
flush_masked_events(EnterWindowMask);
}
XRectangle *
rects_of_view(View *v, unsigned int *num)
{
XRectangle *result = nil;
unsigned int i;
*num = v->area.data[0]->frame.size + 2;
if(*num) {
result = cext_emallocz(*num * sizeof(XRectangle));
2006-05-30 13:13:32 +04:00
for(i = 0; i < v->area.data[0]->frame.size; i++)
result[i] = v->area.data[0]->frame.data[i]->rect;
result[*num - 1] = rect;
result[*num - 2] = brect;
}
return result;
}
int
2006-04-12 12:44:07 +04:00
idx_of_view_id(unsigned short id)
{
int i;
for(i = 0; i < view.size; i++)
if(view.data[i]->id == id)
return i;
return -1;
}
View *
2006-05-24 11:26:08 +04:00
view_of_name(const char *name)
{
unsigned int i;
for(i = 0; i < view.size; i++)
if(!strncmp(view.data[i]->name, name, strlen(name))
&& !strncmp(view.data[i]->name, name, strlen(view.data[i]->name)))
2006-04-05 21:37:38 +04:00
return view.data[i];
return nil;
}
2006-04-12 12:44:07 +04:00
static View *
2006-05-24 11:26:08 +04:00
get_view(const char *name)
{
2006-04-12 12:44:07 +04:00
View *v = view_of_name(name);
return v ? v : create_view(name);
}
void
2006-05-24 11:26:08 +04:00
select_view(const char *arg)
{
2006-05-24 11:26:08 +04:00
char buf[256];
cext_strlcpy(buf, arg, sizeof(buf));
cext_trim(buf, " \t+");
if(!strlen(buf))
return;
sel = idx_of_view(get_view(arg));
update_views(); /* performs focus_view */
}
static Bool
2006-04-12 12:44:07 +04:00
is_of_view(View *v, Client *c)
{
unsigned int i;
2006-04-11 12:16:01 +04:00
for(i = 0; i < v->area.size; i++)
2006-04-12 12:44:07 +04:00
if(is_of_area(v->area.data[i], c))
return True;
return False;
}
void
2006-04-12 12:44:07 +04:00
detach_from_view(View *v, Client *c)
{
unsigned int i;
for(i = 0; i < v->area.size; i++) {
2006-04-12 12:44:07 +04:00
if(is_of_area(v->area.data[i], c)) {
detach_from_area(v->area.data[i], c);
XMoveWindow(dpy, c->framewin, 2 * rect.width, 0);
}
}
}
void
2006-04-12 12:44:07 +04:00
attach_to_view(View *v, Client *c)
{
Area *a;
2006-04-26 18:00:47 +04:00
c->revert = nil;
if(c->trans || c->floating || c->fixedsize
|| (c->rect.width == rect.width && c->rect.height == rect.height))
a = v->area.data[0];
else
a = v->area.data[v->sel];
attach_to_area(a, c, False);
v->sel = idx_of_area(a);
}
Client *
sel_client_of_view(View *v)
{
if(v) {
Area *a = v->area.size ? v->area.data[v->sel] : nil;
return sel_client_of_area(a);
}
return nil;
}
void
restack_view(View *v)
{
unsigned int i, n = 0;
int j;
static Window *wins = nil;
static unsigned int winssz = 0;
if(client.size > winssz) {
winssz = 2 * client.size;
wins = realloc(wins, sizeof(Window) * winssz);
}
for(i = 0; i < v->area.size; i++) {
Area *a = v->area.data[i];
if(a->frame.size) {
wins[n++] = a->frame.data[a->sel]->client->framewin;
for(j = a->frame.size - 1; j >= 0; j--) {
Client *c = a->frame.data[j]->client;
2006-04-27 20:32:37 +04:00
update_client_grab(c, (v->sel == i) && (a->sel == j));
if(j == a->sel)
continue;
wins[n++] = c->framewin;
}
}
}
if(n)
XRestackWindows(dpy, wins, n);
}
void
scale_view(View *v, float w)
{
2006-05-04 00:25:33 +04:00
unsigned int i, xoff;
float scale, dx = 0;
int wdiff = 0;
if(v->area.size == 1)
return;
for(i = 1; i < v->area.size; i++)
dx += v->area.data[i]->rect.width;
scale = w / dx;
2006-05-04 00:25:33 +04:00
xoff = 0;
for(i = 1; i < v->area.size; i++) {
Area *a = v->area.data[i];
a->rect.width *= scale;
2006-05-04 01:26:21 +04:00
if(i == v->area.size - 1)
a->rect.width = w - xoff;
xoff += a->rect.width;
}
/* MIN_COLWIDTH can only be respected when there is enough space; the caller should guarantee this */
if((v->area.size - 1) * MIN_COLWIDTH > w)
return;
2006-05-04 01:26:21 +04:00
xoff = 0;
for(i = 1; i < v->area.size; i++) {
Area *a = v->area.data[i];
if(a->rect.width < MIN_COLWIDTH)
a->rect.width = MIN_COLWIDTH;
else if((wdiff = xoff + a->rect.width - w + (v->area.size - 1 - i) * MIN_COLWIDTH) > 0)
a->rect.width -= wdiff;
if(i == v->area.size - 1)
a->rect.width = w - xoff;
xoff += a->rect.width;
}
}
void
2006-04-24 19:19:50 +04:00
arrange_view(View *v)
{
unsigned int i, xoff = 0;
if(v->area.size == 1)
return;
scale_view(v, rect.width);
for(i = 1; i < v->area.size; i++) {
Area *a = v->area.data[i];
2006-04-24 19:19:50 +04:00
a->rect.x = xoff;
a->rect.y = 0;
a->rect.height = rect.height - brect.height;
xoff += a->rect.width;
arrange_column(a, False);
}
}
2006-04-10 19:50:39 +04:00
2006-04-11 12:16:01 +04:00
static void
update_client_views(Client *c)
{
char buf[256];
char *toks[16];
unsigned int i, n;
cext_strlcpy(buf, c->tags, sizeof(buf));
n = cext_tokenize(toks, 16, buf, '+');
while(c->view.size)
2006-04-13 17:35:10 +04:00
cext_vdetach(vector_of_views(&c->view), c->view.data[0]);
2006-04-11 12:16:01 +04:00
for(i = 0; i < n; i++)
2006-04-13 17:35:10 +04:00
cext_vattach(vector_of_views(&c->view), get_view(toks[i]));
2006-04-11 12:16:01 +04:00
}
static Bool
2006-04-12 12:44:07 +04:00
is_view_of(Client *c, View *v)
2006-04-11 12:16:01 +04:00
{
unsigned int i;
for(i = 0; i < c->view.size; i++)
if(c->view.data[i] == v)
return True;
return False;
}
static Bool
is_empty(View *v)
{
unsigned int i;
for(i = 0; i < v->area.size; i++)
if(v->area.data[i]->frame.size)
return False;
return True;
}
2006-04-11 12:16:01 +04:00
static View *
next_empty_view(View *ignore)
2006-04-10 19:50:39 +04:00
{
unsigned int i;
2006-04-11 12:16:01 +04:00
for(i = 0; i < view.size; i++)
if((view.data[i] != ignore) && is_empty(view.data[i]))
2006-04-11 12:16:01 +04:00
return view.data[i];
return nil;
}
2006-04-10 19:50:39 +04:00
2006-04-11 12:16:01 +04:00
void
update_views()
{
unsigned int i, j;
2006-05-23 20:51:16 +04:00
View *v, *old = view.size ? view.data[sel] : nil;
2006-04-10 19:50:39 +04:00
2006-04-11 12:16:01 +04:00
for(i = 0; i < client.size; i++)
update_client_views(client.data[i]);
2006-04-10 19:50:39 +04:00
2006-04-11 12:16:01 +04:00
for(i = 0; i < client.size; i++) {
Client *c = client.data[i];
for(j = 0; j < view.size; j++) {
View *vw = view.data[j];
update_frame_selectors(vw);
if(is_view_of(c, vw)) {
if(!is_of_view(vw, c))
attach_to_view(vw, c);
}
2006-04-11 12:16:01 +04:00
else {
if(is_of_view(vw, c))
detach_from_view(vw, c);
2006-04-10 19:50:39 +04:00
}
}
}
if(old && !strncmp(old->name, "nil", 4))
2006-05-23 20:51:16 +04:00
old = nil;
while((v = next_empty_view(old)))
2006-04-11 12:16:01 +04:00
destroy_view(v);
2006-04-10 19:50:39 +04:00
2006-04-19 18:24:19 +04:00
if(old)
focus_view(old);
else if(view.size)
focus_view(view.data[sel]);
else
update_view_bars();
2006-04-10 19:50:39 +04:00
}
unsigned int
ncol_of_view(View *v)
{
unsigned int i, n;
regmatch_t tmpregm;
for(i = 0; i < vrule.size; i++) {
Rule *r = vrule.data[i];
if(!regexec(&r->regex, v->name, 1, &tmpregm, 0)) {
char buf[256];
char *toks[16];
cext_strlcpy(buf, r->value, sizeof(buf));
n = cext_tokenize(toks, 16, buf, '+');
if(n && n > v->area.size - 1) {
if(sscanf(toks[v->area.size - 1], "%u", &n) == 1)
return (rect.width * n) / 100;
}
break;
}
}
return 0;
}