2006-02-03 18:15:36 +03:00
|
|
|
/*
|
|
|
|
* (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>
|
2006-02-19 18:21:01 +03:00
|
|
|
#include <string.h>
|
2006-02-03 18:15:36 +03:00
|
|
|
|
|
|
|
#include "wm.h"
|
|
|
|
|
|
|
|
Area *
|
2006-06-08 12:54:19 +04:00
|
|
|
create_area(View *v, Area *pos, unsigned int w)
|
2006-02-03 18:15:36 +03:00
|
|
|
{
|
2006-04-24 19:39:58 +04:00
|
|
|
static unsigned short id = 1;
|
2006-06-08 12:54:19 +04:00
|
|
|
unsigned int area_size;
|
|
|
|
Area *a, **p = pos ? &pos->next : &v->area;
|
|
|
|
|
|
|
|
for(area_size = 0, a=v->area; a; a=a->next, area_size++);
|
2006-04-24 19:39:58 +04:00
|
|
|
|
2006-05-31 21:13:21 +04:00
|
|
|
if(!w) {
|
2006-06-08 12:54:19 +04:00
|
|
|
if(area_size > 1)
|
|
|
|
w = rect.width / area_size - 1;
|
2006-05-31 21:13:21 +04:00
|
|
|
else
|
|
|
|
w = rect.width;
|
|
|
|
}
|
2006-05-04 03:20:32 +04:00
|
|
|
if(w < MIN_COLWIDTH)
|
|
|
|
w = MIN_COLWIDTH;
|
2006-04-24 19:39:58 +04:00
|
|
|
|
2006-06-08 12:54:19 +04:00
|
|
|
if(area_size >= 2 && (area_size - 1) * MIN_COLWIDTH + w > rect.width)
|
2006-04-14 00:44:31 +04:00
|
|
|
return nil;
|
2006-04-24 19:39:58 +04:00
|
|
|
|
2006-06-08 12:54:19 +04:00
|
|
|
if(area_size > 1)
|
2006-04-24 19:39:58 +04:00
|
|
|
scale_view(v, rect.width - w);
|
|
|
|
a = cext_emallocz(sizeof(Area));
|
|
|
|
a->view = v;
|
|
|
|
a->id = id++;
|
|
|
|
a->rect = rect;
|
|
|
|
a->rect.height = rect.height - brect.height;
|
|
|
|
a->mode = def.colmode;
|
|
|
|
a->rect.width = w;
|
2006-06-08 12:54:19 +04:00
|
|
|
a->frame = nil;
|
|
|
|
a->sel = nil;
|
|
|
|
|
|
|
|
a->next = *p;
|
|
|
|
*p = a;
|
|
|
|
|
|
|
|
v->sel = a;
|
2006-04-24 19:39:58 +04:00
|
|
|
return a;
|
2006-02-03 18:15:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
destroy_area(Area *a)
|
|
|
|
{
|
2006-06-08 12:54:19 +04:00
|
|
|
Client *c;
|
|
|
|
Area *t;
|
2006-03-23 12:36:51 +03:00
|
|
|
View *v = a->view;
|
2006-06-08 12:54:19 +04:00
|
|
|
if(a->frame) {
|
2006-05-01 22:53:30 +04:00
|
|
|
fprintf(stderr, "%s", "wmiiwm: fatal, destroying non-empty area\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2006-02-03 18:15:36 +03:00
|
|
|
|
2006-06-08 12:54:19 +04:00
|
|
|
if(v->revert == a)
|
|
|
|
v->revert = nil;
|
2006-02-03 18:15:36 +03:00
|
|
|
|
2006-06-08 12:54:19 +04:00
|
|
|
for(c=client; c; c=c->next)
|
|
|
|
if(c->revert == a)
|
|
|
|
c->revert = nil;
|
|
|
|
|
|
|
|
for(t=v->area; t && t->next != a; t=t->next);
|
|
|
|
if(t) {
|
|
|
|
t->next = a->next;
|
|
|
|
if(v->sel == a)
|
2006-06-08 15:01:33 +04:00
|
|
|
v->sel = t == v->area ? t->next : t;
|
2006-06-08 12:54:19 +04:00
|
|
|
}
|
|
|
|
free(a);
|
2006-02-03 18:15:36 +03:00
|
|
|
}
|
2006-02-19 18:21:01 +03:00
|
|
|
|
2006-06-20 02:25:49 +04:00
|
|
|
char *
|
2006-02-19 18:21:01 +03:00
|
|
|
select_area(Area *a, char *arg)
|
|
|
|
{
|
|
|
|
Area *new;
|
2006-06-08 12:54:19 +04:00
|
|
|
unsigned int i;
|
2006-06-20 02:25:49 +04:00
|
|
|
Frame *p, *f;
|
|
|
|
View *v;
|
|
|
|
static char Ebadvalue[] = "bad value";
|
2006-03-31 09:57:33 +04:00
|
|
|
|
2006-06-20 02:25:49 +04:00
|
|
|
v = a->view;
|
2006-06-08 12:54:19 +04:00
|
|
|
v->revert = a;
|
2006-06-20 02:25:49 +04:00
|
|
|
f = a->sel;
|
2006-03-31 09:57:33 +04:00
|
|
|
|
2006-03-11 22:37:29 +03:00
|
|
|
if(!strncmp(arg, "toggle", 7)) {
|
2006-06-08 12:54:19 +04:00
|
|
|
if(a != v->area)
|
|
|
|
new = v->area;
|
|
|
|
else if(v->revert && v->revert != v->area)
|
|
|
|
new = v->revert;
|
2006-03-11 22:37:29 +03:00
|
|
|
else
|
2006-06-08 12:54:19 +04:00
|
|
|
new = v->area->next;
|
2006-06-20 02:25:49 +04:00
|
|
|
} else if(!strncmp(arg, "left", 5)) {
|
2006-06-08 12:54:19 +04:00
|
|
|
if(a == v->area)
|
2006-06-20 02:25:49 +04:00
|
|
|
return Ebadvalue;
|
2006-06-08 12:54:19 +04:00
|
|
|
for(new=v->area->next;
|
|
|
|
new && new->next != a;
|
|
|
|
new=new->next);
|
|
|
|
if(!new)
|
|
|
|
new=v->area->next;
|
2006-06-20 02:25:49 +04:00
|
|
|
} else if(!strncmp(arg, "right", 5)) {
|
2006-06-08 12:54:19 +04:00
|
|
|
if(a == v->area)
|
2006-06-20 02:25:49 +04:00
|
|
|
return Ebadvalue;
|
2006-06-08 12:54:19 +04:00
|
|
|
new = a->next ? a->next : a;
|
2006-02-19 18:21:01 +03:00
|
|
|
}
|
2006-06-19 18:05:02 +04:00
|
|
|
else if(!strncmp(arg, "up", 3)) {
|
2006-06-20 02:25:49 +04:00
|
|
|
for(p=a->frame; p->anext; p=p->anext)
|
|
|
|
if(p->anext == f) break;
|
2006-06-19 18:05:02 +04:00
|
|
|
focus_client(p->client, True);
|
|
|
|
flush_masked_events(EnterWindowMask);
|
2006-06-20 02:25:49 +04:00
|
|
|
return nil;
|
2006-06-19 18:05:02 +04:00
|
|
|
}
|
|
|
|
else if(!strncmp(arg, "down", 5)) {
|
|
|
|
p = f->anext ? f->anext : a->frame;
|
|
|
|
focus_client(p->client, True);
|
|
|
|
flush_masked_events(EnterWindowMask);
|
2006-06-20 02:25:49 +04:00
|
|
|
return nil;
|
2006-06-19 18:05:02 +04:00
|
|
|
}
|
2006-02-19 18:21:01 +03:00
|
|
|
else {
|
2006-05-29 14:48:21 +04:00
|
|
|
if(sscanf(arg, "%d", &i) != 1)
|
2006-06-20 02:25:49 +04:00
|
|
|
return Ebadvalue;
|
2006-06-08 12:54:19 +04:00
|
|
|
for(new=view->area; i && new->next; new=new->next, i--);
|
2006-02-19 18:21:01 +03:00
|
|
|
}
|
2006-06-08 12:54:19 +04:00
|
|
|
if(new->sel)
|
|
|
|
focus_client(new->sel->client, True);
|
|
|
|
v->sel = new;
|
2006-04-28 14:37:19 +04:00
|
|
|
draw_clients();
|
2006-06-20 02:25:49 +04:00
|
|
|
return nil;
|
2006-02-19 18:21:01 +03:00
|
|
|
}
|
2006-02-24 12:06:02 +03:00
|
|
|
|
|
|
|
void
|
2006-04-12 12:44:07 +04:00
|
|
|
send_to_area(Area *to, Area *from, Client *c)
|
2006-02-24 12:06:02 +03:00
|
|
|
{
|
2006-03-11 22:50:53 +03:00
|
|
|
c->revert = from;
|
2006-04-12 12:44:07 +04:00
|
|
|
detach_from_area(from, c);
|
2006-05-31 18:34:48 +04:00
|
|
|
attach_to_area(to, c, True);
|
2006-04-26 10:59:55 +04:00
|
|
|
focus_client(c, True);
|
2006-02-24 12:06:02 +03:00
|
|
|
}
|
2006-03-02 12:26:30 +03:00
|
|
|
|
2006-05-29 16:31:40 +04:00
|
|
|
static void
|
2006-04-07 22:06:59 +04:00
|
|
|
place_client(Area *a, Client *c)
|
|
|
|
{
|
2006-04-26 01:11:49 +04:00
|
|
|
static unsigned int mx, my;
|
2006-04-10 17:48:27 +04:00
|
|
|
static Bool *field = nil;
|
2006-06-08 12:54:19 +04:00
|
|
|
Frame *fr;
|
2006-04-14 11:34:03 +04:00
|
|
|
Bool fit = False;
|
2006-05-31 19:21:52 +04:00
|
|
|
BlitzAlign align = CENTER;
|
2006-06-08 12:54:19 +04:00
|
|
|
unsigned int i, j, x, y, maxx, maxy, dx, dy, cx, cy, diff, num = 0;
|
2006-04-10 17:48:27 +04:00
|
|
|
XPoint p1 = {0, 0}, p2 = {0, 0};
|
2006-06-08 12:54:19 +04:00
|
|
|
Frame *f = c->sel;
|
2006-05-31 10:47:07 +04:00
|
|
|
int snap = rect.height / 66;
|
2006-05-29 16:31:40 +04:00
|
|
|
XRectangle *rects;
|
2006-04-10 17:48:27 +04:00
|
|
|
|
2006-04-24 20:23:06 +04:00
|
|
|
if(c->trans)
|
|
|
|
return;
|
2006-05-13 14:21:41 +04:00
|
|
|
if(c->rect.width >= a->rect.width
|
|
|
|
|| c->rect.height >= a->rect.height
|
2006-05-13 16:19:25 +04:00
|
|
|
|| c->size.flags & USPosition
|
|
|
|
|| c->size.flags & PPosition)
|
2006-04-07 22:06:59 +04:00
|
|
|
return;
|
|
|
|
|
2006-05-29 16:31:40 +04:00
|
|
|
rects = rects_of_view(a->view, &num);
|
2006-04-10 17:48:27 +04:00
|
|
|
if(!field) {
|
|
|
|
mx = rect.width / 8;
|
|
|
|
my = rect.height / 8;
|
|
|
|
field = cext_emallocz(my * mx * sizeof(Bool));
|
|
|
|
}
|
2006-04-07 22:06:59 +04:00
|
|
|
|
2006-04-10 17:48:27 +04:00
|
|
|
for(y = 0; y < my; y++)
|
|
|
|
for(x = 0; x < mx; x++)
|
|
|
|
field[y*mx + x] = True;
|
|
|
|
|
|
|
|
dx = rect.width / mx;
|
|
|
|
dy = rect.height / my;
|
2006-06-08 12:54:19 +04:00
|
|
|
for(fr=a->frame; fr; fr=fr->anext) {
|
2006-04-14 11:34:03 +04:00
|
|
|
if(fr == f) {
|
|
|
|
cx = f->rect.width / dx;
|
|
|
|
cy = f->rect.height / dy;
|
2006-04-10 17:48:27 +04:00
|
|
|
continue;
|
2006-04-14 11:34:03 +04:00
|
|
|
}
|
2006-04-14 13:50:20 +04:00
|
|
|
if(fr->rect.x < 0)
|
|
|
|
x = 0;
|
|
|
|
else
|
|
|
|
x = fr->rect.x / dx;
|
|
|
|
if(fr->rect.y < 0)
|
|
|
|
y = 0;
|
|
|
|
else
|
|
|
|
y = fr->rect.y / dy;
|
|
|
|
maxx = (fr->rect.x + fr->rect.width) / dx;
|
|
|
|
maxy = (fr->rect.y + fr->rect.height) / dy;
|
2006-04-14 13:35:42 +04:00
|
|
|
for(j = y; j < my && j < maxy; j++)
|
|
|
|
for(i = x; i < mx && i < maxx; i++)
|
2006-04-10 17:48:27 +04:00
|
|
|
field[j*mx + i] = False;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(y = 0; y < my; y++)
|
|
|
|
for(x = 0; x < mx; x++) {
|
|
|
|
if(field[y*mx + x]) {
|
2006-04-12 00:33:09 +04:00
|
|
|
for(i = x; (i < mx) && field[y*mx + i]; i++);
|
|
|
|
for(j = y; (j < my) && field[j*mx + x]; j++);
|
2006-04-14 11:34:03 +04:00
|
|
|
if(((i - x) * (j - y) > (p2.x - p1.x) * (p2.y - p1.y))
|
|
|
|
&& (i - x > cx) && (j - y > cy))
|
|
|
|
{
|
|
|
|
fit = True;
|
2006-04-10 17:48:27 +04:00
|
|
|
p1.x = x;
|
|
|
|
p1.y = y;
|
|
|
|
p2.x = i;
|
|
|
|
p2.y = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-14 11:34:03 +04:00
|
|
|
if(fit) {
|
|
|
|
p1.x *= dx;
|
|
|
|
p1.y *= dy;
|
|
|
|
}
|
2006-04-10 17:48:27 +04:00
|
|
|
|
2006-04-14 11:34:03 +04:00
|
|
|
if(fit && (p1.x + f->rect.width < a->rect.x + a->rect.width))
|
2006-04-10 17:48:27 +04:00
|
|
|
f->rect.x = p1.x;
|
2006-04-24 23:21:11 +04:00
|
|
|
else {
|
2006-04-26 01:11:49 +04:00
|
|
|
diff = a->rect.width - f->rect.width;
|
|
|
|
f->rect.x = a->rect.x + (random() % (diff ? diff : 1));
|
2006-04-24 23:21:11 +04:00
|
|
|
}
|
2006-04-10 17:48:27 +04:00
|
|
|
|
2006-04-14 11:34:03 +04:00
|
|
|
if(fit && (p1.y + f->rect.height < a->rect.y + a->rect.height))
|
2006-04-10 17:48:27 +04:00
|
|
|
f->rect.y = p1.y;
|
2006-04-24 23:21:11 +04:00
|
|
|
else {
|
2006-04-26 01:11:49 +04:00
|
|
|
diff = a->rect.height - f->rect.height;
|
|
|
|
f->rect.y = a->rect.y + (random() % (diff ? diff : 1));
|
2006-04-24 23:21:11 +04:00
|
|
|
}
|
2006-05-29 16:31:40 +04:00
|
|
|
|
2006-05-31 19:21:52 +04:00
|
|
|
snap_rect(rects, num, &f->rect, &align, snap);
|
2006-05-29 16:31:40 +04:00
|
|
|
if(rects)
|
|
|
|
free(rects);
|
2006-04-07 22:06:59 +04:00
|
|
|
}
|
|
|
|
|
2006-03-02 12:26:30 +03:00
|
|
|
void
|
2006-05-31 18:34:48 +04:00
|
|
|
attach_to_area(Area *a, Client *c, Bool send)
|
2006-03-02 12:26:30 +03:00
|
|
|
{
|
2006-05-30 21:13:33 +04:00
|
|
|
View *v = a->view;
|
2006-06-08 12:54:19 +04:00
|
|
|
unsigned int h = 0, i;
|
2006-04-24 18:03:37 +04:00
|
|
|
Frame *f;
|
2006-06-08 12:54:19 +04:00
|
|
|
for(f=a->frame, i=1; f; f=f->anext, i++);
|
2006-03-23 18:12:06 +03:00
|
|
|
|
2006-06-08 12:54:19 +04:00
|
|
|
c->floating = (a == v->area);
|
|
|
|
if(!c->floating) {
|
|
|
|
h = a->rect.height / i;
|
|
|
|
if(a->frame)
|
2006-04-24 18:03:37 +04:00
|
|
|
scale_column(a, a->rect.height - h);
|
2006-04-06 19:03:12 +04:00
|
|
|
}
|
2006-04-24 18:03:37 +04:00
|
|
|
|
2006-06-08 12:54:19 +04:00
|
|
|
if(!send && !c->floating) { /* column */
|
2006-05-31 21:48:44 +04:00
|
|
|
unsigned int w = newcolw_of_view(v);
|
2006-06-08 12:54:19 +04:00
|
|
|
if(v->area->next->frame && w) {
|
|
|
|
a = new_column(v, a, w);
|
2006-05-31 11:51:40 +04:00
|
|
|
arrange_view(v);
|
|
|
|
}
|
2006-05-30 21:13:33 +04:00
|
|
|
}
|
|
|
|
|
2006-04-24 18:03:37 +04:00
|
|
|
f = create_frame(a, c);
|
|
|
|
|
2006-06-08 12:54:19 +04:00
|
|
|
if(!c->floating) { /* column */
|
2006-04-24 18:54:56 +04:00
|
|
|
f->rect.height = h;
|
2006-04-24 18:03:37 +04:00
|
|
|
arrange_column(a, False);
|
2006-04-24 18:54:56 +04:00
|
|
|
}
|
2006-04-07 22:06:59 +04:00
|
|
|
else { /* floating */
|
2006-04-10 17:48:27 +04:00
|
|
|
place_client(a, c);
|
2006-03-15 18:00:39 +03:00
|
|
|
resize_client(c, &f->rect, False);
|
2006-04-07 22:06:59 +04:00
|
|
|
}
|
2006-03-02 12:26:30 +03:00
|
|
|
}
|
|
|
|
|
2006-03-02 15:37:52 +03:00
|
|
|
void
|
2006-04-12 12:44:07 +04:00
|
|
|
detach_from_area(Area *a, Client *c)
|
2006-03-02 15:37:52 +03:00
|
|
|
{
|
2006-03-23 12:36:51 +03:00
|
|
|
View *v = a->view;
|
2006-06-08 12:54:19 +04:00
|
|
|
Frame *f;
|
2006-03-07 01:55:47 +03:00
|
|
|
|
2006-06-08 12:54:19 +04:00
|
|
|
for(f=c->frame; f && f->area != a; f=f->cnext);
|
2006-06-10 03:57:00 +04:00
|
|
|
cext_assert(f->area == a);
|
|
|
|
destroy_frame(f);
|
2006-03-06 19:22:54 +03:00
|
|
|
|
2006-06-08 12:54:19 +04:00
|
|
|
if(a != a->view->area) {
|
|
|
|
if(a->frame)
|
|
|
|
arrange_column(a, False);
|
|
|
|
else {
|
|
|
|
if(v->area->next->next)
|
2006-03-09 14:44:38 +03:00
|
|
|
destroy_area(a);
|
2006-06-08 12:54:19 +04:00
|
|
|
else if(!a->frame && v->area->frame)
|
|
|
|
v->sel = v->area; /* focus floating area if it contains something */
|
2006-04-24 19:19:50 +04:00
|
|
|
arrange_view(v);
|
2006-03-09 14:44:38 +03:00
|
|
|
}
|
2006-06-08 12:54:19 +04:00
|
|
|
}
|
|
|
|
else if(!a->frame) {
|
|
|
|
if(c->trans) {
|
|
|
|
/* focus area of transient, if possible */
|
|
|
|
Client *cl = client_of_win(c->trans);
|
|
|
|
if(cl && cl->frame) {
|
|
|
|
a = cl->sel->area;
|
|
|
|
if(a->view == v)
|
|
|
|
v->sel = a;
|
2006-03-09 02:27:12 +03:00
|
|
|
}
|
|
|
|
}
|
2006-06-08 12:54:19 +04:00
|
|
|
else if(v->area->next->frame)
|
|
|
|
v->sel = v->area->next; /* focus first col as fallback */
|
2006-03-04 11:23:17 +03:00
|
|
|
}
|
2006-03-02 17:28:55 +03:00
|
|
|
}
|
|
|
|
|
2006-03-06 19:22:54 +03:00
|
|
|
Bool
|
2006-04-12 12:44:07 +04:00
|
|
|
is_of_area(Area *a, Client *c)
|
2006-03-06 19:22:54 +03:00
|
|
|
{
|
2006-06-08 12:54:19 +04:00
|
|
|
Frame *f;
|
|
|
|
for(f=a->frame; f; f=f->anext)
|
|
|
|
if(f->client == c)
|
2006-03-06 19:22:54 +03:00
|
|
|
return True;
|
|
|
|
return False;
|
|
|
|
}
|
2006-05-08 23:44:56 +04:00
|
|
|
|
2006-06-08 12:54:19 +04:00
|
|
|
int
|
|
|
|
idx_of_area(Area *a)
|
|
|
|
{
|
|
|
|
Area *t;
|
|
|
|
int i = 0;
|
2006-06-19 03:04:16 +04:00
|
|
|
for(t=a->view->area; t && t != a; t=t->next)
|
|
|
|
i++;
|
2006-06-08 12:54:19 +04:00
|
|
|
return t ? i : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Area *
|
|
|
|
area_of_id(View *v, unsigned short id)
|
|
|
|
{
|
|
|
|
Area *a;
|
|
|
|
for(a=v->area; a && a->id != id; a=a->next);
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2006-05-08 23:44:56 +04:00
|
|
|
Client *
|
|
|
|
sel_client_of_area(Area *a)
|
|
|
|
{
|
2006-06-08 12:54:19 +04:00
|
|
|
return a && a->sel ? a->sel->client : nil;
|
2006-05-08 23:44:56 +04:00
|
|
|
}
|