wmii/view.c

435 lines
9.1 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>
2006-06-08 12:54:19 +04:00
#include <stdio.h>
#include <string.h>
2006-10-20 12:07:55 +04:00
#include "wmii.h"
static Bool
2006-10-12 18:10:57 +04:00
is_empty(View *v) {
Area *a;
for(a=v->area; a; a=a->next)
if(a->frame)
return False;
return True;
}
2006-06-28 06:16:14 +04:00
Frame *
2006-10-12 18:10:57 +04:00
clientframe_of_view(View *v, Client *c) {
2006-06-28 06:16:14 +04:00
Frame *f;
for(f=c->frame; f; f=f->cnext)
if(f->area->view == v)
break;
return f;
}
static void
2006-10-12 18:10:57 +04:00
assign_sel_view(View *v) {
if(screen->sel != v) {
if(screen->sel)
write_event("UnfocusTag %s\n",screen->sel->name);
screen->sel = v;
write_event("FocusTag %s\n", screen->sel->name);
2006-06-28 06:16:14 +04:00
}
}
Client *
sel_client_of_view(View *v) {
2007-02-05 05:02:05 +03:00
return v->sel && v->sel->sel ? v->sel->sel->client : nil;
}
View *
2006-10-12 18:10:57 +04:00
get_view(const char *name) {
View *v;
2006-06-25 17:16:03 +04:00
int cmp;
2006-10-12 18:10:57 +04:00
for(v = view; v; v=v->next)
if((cmp=strcmp(name, v->name)) >= 0)
2006-06-25 17:16:03 +04:00
break;
if(!v || cmp != 0)
v = create_view(name);
return v;
}
2006-06-08 12:54:19 +04:00
View *
2006-10-12 18:10:57 +04:00
create_view(const char *name) {
static unsigned short id = 1;
2006-10-12 18:10:57 +04:00
View **i, *v = ixp_emallocz(sizeof(View));
v->id = id++;
2006-10-12 18:10:57 +04:00
strncpy(v->name, name, sizeof(v->name));
2007-02-05 05:02:05 +03:00
create_area(v, nil, 0);
2006-06-08 12:54:19 +04:00
create_area(v, v->area, 0);
v->area->floating = True;
for(i=&view; *i; i=&(*i)->next)
if(strcmp((*i)->name, name) < 0) break;
2006-06-08 12:54:19 +04:00
v->next = *i;
*i = v;
write_event("CreateTag %s\n", v->name);
if(!screen->sel)
2006-06-08 12:54:19 +04:00
assign_sel_view(v);
return v;
}
2006-06-08 12:54:19 +04:00
void
2006-10-12 18:10:57 +04:00
destroy_view(View *v) {
2006-06-08 12:54:19 +04:00
Area *a;
View **i;
while((a = v->area)) {
v->area = a->next;
2006-06-08 12:54:19 +04:00
destroy_area(a);
};
for(i=&view; *i; i=&(*i)->next)
if(*i == v) break;
2006-06-08 12:54:19 +04:00
*i = v->next;
if(screen->sel == v)
for(screen->sel=view; screen->sel && screen->sel->next; screen->sel=screen->sel->next)
if(screen->sel->next == *i) break;
write_event("DestroyTag %s\n", v->name);
free(v);
}
static void
2006-10-12 18:10:57 +04:00
update_frame_selectors(View *v) {
2006-06-25 17:16:03 +04:00
Area *a;
2006-06-08 12:54:19 +04:00
Frame *f;
2006-10-12 18:10:57 +04:00
2006-06-25 17:16:03 +04:00
for(a=v->area; a; a=a->next)
for(f=a->frame; f; f=f->anext)
f->client->sel = f;
}
void
2006-10-12 18:10:57 +04:00
focus_view(WMScreen *s, View *v) {
2006-06-25 17:16:03 +04:00
Frame *f;
Client *c;
2006-06-08 12:54:19 +04:00
2006-07-03 20:41:14 +04:00
XGrabServer(blz.dpy);
assign_sel_view(v);
update_frame_selectors(v);
/* gives all(!) clients proper geometry (for use of different tags) */
2006-06-08 12:54:19 +04:00
for(c=client; c; c=c->next)
2006-06-25 17:16:03 +04:00
if((f = c->sel)) {
if(f->view == v) {
2006-06-08 12:54:19 +04:00
resize_client(c, &f->rect, False);
2006-07-03 20:41:14 +04:00
//XMoveWindow(blz.dpy, c->framewin, f->rect.x, f->rect.y);
2006-06-25 17:16:03 +04:00
}else
2006-07-03 20:41:14 +04:00
XMoveWindow(blz.dpy, c->framewin, 2 * s->rect.width + f->rect.x,
f->rect.y);
}
2006-06-20 02:25:49 +04:00
if((c = sel_client()))
focus_client(c, False);
draw_frames();
2006-07-03 20:41:14 +04:00
XSync(blz.dpy, False);
XUngrabServer(blz.dpy);
flush_masked_events(EnterWindowMask);
}
void
2006-10-12 18:10:57 +04:00
select_view(const char *arg) {
2006-05-24 11:26:08 +04:00
char buf[256];
2006-10-12 18:10:57 +04:00
strncpy(buf, arg, sizeof(buf));
trim(buf, " \t+/");
2006-05-24 11:26:08 +04:00
if(!strlen(buf))
return;
if(!strncmp(buf, ".", 2) || !strncmp(buf, "..", 3))
return;
assign_sel_view(get_view(buf));
update_views(); /* performs focus_view */
}
void
2006-10-12 18:10:57 +04:00
attach_to_view(View *v, Frame *f) {
Area *a;
2006-06-25 17:16:03 +04:00
Client *c = f->client;
unsigned int i;
2007-02-05 05:02:05 +03:00
c->revert = nil;
a = v->sel;
if(c->trans || c->floating || c->fixedsize
|| (c->rect.width == screen->rect.width && c->rect.height == screen->rect.height))
v->sel = v->area;
else if(starting && v->sel->floating)
v->sel = v->area->next;
attach_to_area(v->sel, f, False);
if(a != v->sel) {
for(a=v->area, i = 0; a && a != v->sel; a=a->next, i++);
if(i) write_event("ColumnFocus %d\n", i);
else write_event("FocusFloating\n");
}
}
void
2006-10-12 18:10:57 +04:00
restack_view(View *v) {
2006-06-08 12:54:19 +04:00
Area *a;
Frame *f;
Client *c;
2007-02-03 02:47:15 +03:00
unsigned int n, i;
2007-02-05 05:02:05 +03:00
static Window *wins = nil;
2007-02-03 02:47:15 +03:00
static unsigned int winssz = 0;
i = 0;
n = 1;
2006-06-08 12:54:19 +04:00
for(c=client; c; c=c->next, i++);
2007-02-03 02:47:15 +03:00
if(i >= winssz) {
winssz = 2 * i;
wins = ixp_erealloc(wins, sizeof(Window) * winssz);
}
2007-02-03 01:58:17 +03:00
2007-02-03 02:47:15 +03:00
wins[0] = screen->barwin;
2006-06-08 12:54:19 +04:00
for(a=v->area; a; a=a->next) {
if(a->frame) {
if(a == v->area) {
Frame **tf;
for(tf=&a->frame; *tf; tf=&(*tf)->anext)
if(*tf == a->sel) break;
*tf = a->sel->anext;
a->sel->anext = a->frame;
a->frame = a->sel;
}
2006-06-08 12:54:19 +04:00
wins[n++] = a->sel->client->framewin;
for(f=a->frame; f; f=f->anext) {
Client *c = f->client;
update_client_grab(c, (v->sel == a) && (a->sel == f));
if(f != a->sel)
wins[n++] = c->framewin;
}
}
}
if(n)
2006-07-03 20:41:14 +04:00
XRestackWindows(blz.dpy, wins, n);
}
void
2006-10-12 18:10:57 +04:00
scale_view(View *v, float w) {
unsigned int xoff, num_col;
unsigned int min_width;
2006-06-08 12:54:19 +04:00
Area *a;
float scale, dx = 0;
int wdiff = 0;
min_width = screen->rect.width/NCOL;
2006-06-08 12:54:19 +04:00
if(!v->area->next)
return;
num_col = 0;
for(a=v->area->next; a; a=a->next)
num_col++, dx += a->rect.width;
scale = w / dx;
2006-05-04 00:25:33 +04:00
xoff = 0;
2006-06-08 12:54:19 +04:00
for(a=v->area->next; a; a=a->next) {
a->rect.width *= scale;
2006-06-08 12:54:19 +04:00
if(!a->next)
2006-05-04 01:26:21 +04:00
a->rect.width = w - xoff;
xoff += a->rect.width;
}
/* min_width can only be respected when there is enough space;
* the caller should guarantee this */
if(num_col * min_width > w)
return;
2006-05-04 01:26:21 +04:00
xoff = 0;
for(a=v->area->next, num_col--; a; a=a->next, num_col--) {
if(a->rect.width < min_width)
a->rect.width = min_width;
else if((wdiff = xoff + a->rect.width - w + num_col * min_width) > 0)
a->rect.width -= wdiff;
2006-06-08 12:54:19 +04:00
if(!a->next)
a->rect.width = w - xoff;
xoff += a->rect.width;
}
}
void
2006-10-12 18:10:57 +04:00
arrange_view(View *v) {
2006-06-08 12:54:19 +04:00
unsigned int xoff = 0;
Area *a;
2006-06-08 12:54:19 +04:00
if(!v->area->next)
return;
scale_view(v, screen->rect.width);
2006-06-08 12:54:19 +04:00
for(a=v->area->next; a; a=a->next) {
2006-04-24 19:19:50 +04:00
a->rect.x = xoff;
a->rect.y = 0;
a->rect.height = screen->rect.height - screen->brect.height;
2006-04-24 19:19:50 +04:00
xoff += a->rect.width;
arrange_column(a, False);
}
}
2006-04-10 19:50:39 +04:00
2006-06-25 17:16:03 +04:00
XRectangle *
2006-10-12 18:10:57 +04:00
rects_of_view(View *v, unsigned int *num) {
XRectangle *result;
2006-06-25 17:16:03 +04:00
Frame *f;
2006-04-11 12:16:01 +04:00
2006-06-25 17:16:03 +04:00
*num = 2;
for(f=v->area->frame; f; f=f->anext, (*num)++);
2006-10-12 18:10:57 +04:00
result = ixp_emallocz(*num * sizeof(XRectangle));
2006-06-25 17:16:03 +04:00
for(f=v->area->frame; f; f=f->anext)
*result++ = f->rect;
*result++ = screen->rect;
*result++ = screen->brect;
return result - *num;
}
2006-06-17 15:32:49 +04:00
/* XXX: This will need cleanup */
unsigned char *
view_index(View *v) {
unsigned int a_i, buf_i, n;
int len;
Frame *f;
Area *a;
2006-06-25 17:16:03 +04:00
len = BUFFER_SIZE;
2006-06-17 15:32:49 +04:00
buf_i = 0;
for((a = v->area), (a_i = 0); a && len > 0; (a=a->next), (a_i++)) {
if(a->floating)
n = snprintf(&buffer[buf_i], len, "# ~ %d %d\n",
a->rect.width, a->rect.height);
else
n = snprintf(&buffer[buf_i], len, "# %d %d %d\n",
a_i, a->rect.x, a->rect.width);
buf_i += n;
len -= n;
2006-06-17 15:32:49 +04:00
for(f=a->frame; f && len > 0; f=f->anext) {
XRectangle *r = &f->rect;
if(a->floating)
n = snprintf(&buffer[buf_i], len, "~ 0x%x %d %d %d %d %s\n",
(unsigned int)f->client->win,
2006-06-17 15:32:49 +04:00
r->x, r->y, r->width, r->height,
f->client->props);
else
n = snprintf(&buffer[buf_i], len, "%d 0x%x %d %d %s\n",
a_i, (unsigned int)f->client->win, r->y,
r->height, f->client->props);
if(len - n < 0)
return (unsigned char *)buffer;
2006-06-17 15:32:49 +04:00
buf_i += n;
len -= n;
}
}
return (unsigned char *)buffer;
2006-06-17 15:32:49 +04:00
}
2006-06-19 15:18:09 +04:00
Client *
2006-10-12 18:10:57 +04:00
client_of_message(View *v, char *message, unsigned int *next) {
unsigned long id = 0;
2006-06-19 15:18:09 +04:00
Client *c;
2006-06-19 17:49:11 +04:00
if(!strncmp(message, "sel ", 4)) {
*next = 4;
return sel_client_of_view(v);
2006-06-19 17:49:11 +04:00
}
sscanf(message, "0x%lx %n", &id, next);
if(!id)
sscanf(message, "%lu %n", &id, next);
if(!id)
2007-02-05 05:02:05 +03:00
return nil;
for(c=client; c && c->win!=id; c=c->next);
2006-06-19 15:18:09 +04:00
return c;
}
Area *
area_of_message(View *v, char *message, unsigned int *next) {
unsigned int i;
Area *a;
if(!strncmp(message, "sel ", 4)) {
*next = 4;
return v->sel;
}
2006-06-25 17:16:03 +04:00
if(!strncmp(message, "~ ", 2))
return v->area;
if(1 != sscanf(message, "%d %n", &i, next) || i == 0)
2007-02-05 05:02:05 +03:00
return nil;
for(a=v->area; i && a; a=a->next, i--);
return a;
}
char *
message_view(View *v, char *message) {
unsigned int n, i;
Client *c;
Frame *f;
Area *a;
static char Ebadvalue[] = "bad value";
if(!strncmp(message, "send ", 5)) {
message += 5;
if(!(c = client_of_message(v, message, &n)))
return Ebadvalue;
2006-06-19 15:18:09 +04:00
if(!(f = clientframe_of_view(v, c)))
return Ebadvalue;
return send_client(f, &message[n]);
}
2006-06-20 02:25:49 +04:00
if(!strncmp(message, "select ", 7)) {
2006-06-19 15:18:09 +04:00
message += 7;
return select_area(v->sel, message);
2006-06-19 15:18:09 +04:00
}
if(!strncmp(message, "colmode ", 8)) {
message += 8;
if(!(a = area_of_message(v, message, &n)) || a == v->area)
return Ebadvalue;
if((i = column_mode_of_str(&message[n])) == -1)
return Ebadvalue;
a->mode = i;
arrange_column(a, True);
restack_view(v);
if(v == screen->sel)
focus_view(screen, v);
draw_frames();
2007-02-05 05:02:05 +03:00
return nil;
}
2006-06-20 02:25:49 +04:00
return Ebadvalue;
2006-06-17 15:32:49 +04:00
}
2006-04-11 12:16:01 +04:00
void
2006-10-12 18:10:57 +04:00
update_views() {
2006-06-25 17:16:03 +04:00
View *n, *v;
View *old = screen->sel;
2006-06-08 12:54:19 +04:00
2006-06-25 17:16:03 +04:00
for(v=view; v; v=v->next)
update_frame_selectors(v);
2007-02-05 05:02:05 +03:00
if(old && !strncmp(old->name, "nil", 4))
old = nil;
2006-06-25 17:16:03 +04:00
for((v=view) && (n=v->next); v; (v=n) && (n=v->next))
if((v != old) && is_empty(v))
destroy_view(v);
2006-04-19 18:24:19 +04:00
if(old)
focus_view(screen, old);
else if(screen->sel)
focus_view(screen, screen->sel);
2006-04-10 19:50:39 +04:00
}
unsigned int
2006-10-12 18:10:57 +04:00
newcolw_of_view(View *v) {
2006-06-08 12:54:19 +04:00
Rule *r;
Area *a;
unsigned int i, n;
regmatch_t tmpregm;
2006-06-17 15:32:49 +04:00
for(r=def.colrules.rule; r; r=r->next) {
if(!regexec(&r->regex, v->name, 1, &tmpregm, 0)) {
char buf[256];
char *toks[16];
2006-10-12 18:10:57 +04:00
strncpy(buf, r->value, sizeof(buf));
n = ixp_tokenize(toks, 16, buf, '+');
2006-06-08 15:14:33 +04:00
for(a=v->area, i=0; a; a=a->next, i++);
2006-06-08 15:16:24 +04:00
if(n && n >= i) {
2006-06-08 12:54:19 +04:00
if(sscanf(toks[i - 1], "%u", &n) == 1)
return (screen->rect.width * n) / 100;
}
break;
}
}
return 0;
}