wmii/cmd/wm/page.c

348 lines
8.4 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 "wm.h"
2005-12-05 01:45:59 +03:00
static Page zero_page = { 0 };
2005-11-18 18:54:58 +03:00
2005-12-05 01:45:59 +03:00
static void select_frame(void *obj, char *cmd);
static void handle_after_write_page(IXPServer * s, File * f);
2005-11-18 18:54:58 +03:00
/* action table for /?/ namespace */
2005-12-05 01:45:59 +03:00
Action page_acttbl[] = {
2005-11-18 18:54:58 +03:00
{"select", select_frame},
{0, 0}
};
2005-12-05 22:38:03 +03:00
Page *alloc_page()
2005-11-18 18:54:58 +03:00
{
Page *p = cext_emalloc(sizeof(Page));
char buf[MAX_BUF], buf2[16];
2005-12-05 04:50:02 +03:00
int id = count_items((void **) page) + 1;
2005-11-18 18:54:58 +03:00
snprintf(buf2, sizeof(buf2), "%d", id);
2005-11-18 18:54:58 +03:00
*p = zero_page;
snprintf(buf, sizeof(buf), "/%d", id);
2005-12-05 22:38:03 +03:00
p->file[P_PREFIX] = ixp_create(ixps, buf);
snprintf(buf, sizeof(buf), "/%d/name", id);
p->file[P_NAME] = wmii_create_ixpfile(ixps, buf, buf2);
snprintf(buf, sizeof(buf), "/%d/a", id);
2005-12-05 22:38:03 +03:00
p->file[P_AREA_PREFIX] = ixp_create(ixps, buf);
snprintf(buf, sizeof(buf), "/%d/a/sel", id);
2005-12-05 22:38:03 +03:00
p->file[P_SEL_AREA] = ixp_create(ixps, buf);
p->file[P_SEL_AREA]->bind = 1; /* mount point */
snprintf(buf, sizeof(buf), "/%d/ctl", id);
2005-12-05 22:38:03 +03:00
p->file[P_CTL] = ixp_create(ixps, buf);
p->file[P_CTL]->after_write = handle_after_write_page;
alloc_area(p, &rect, "float");
2005-12-05 04:50:02 +03:00
page = (Page **) attach_item_end((void **) page, p, sizeof(Page *));
sel = index_item((void **) page, p);
2005-12-05 22:38:03 +03:00
def[WM_SEL_PAGE]->content = p->file[P_PREFIX]->content;
2005-11-18 18:54:58 +03:00
return p;
}
void destroy_page(Page * p)
{
unsigned int i;
2005-12-05 04:50:02 +03:00
for (i = 0; p->area[i]; i++)
destroy_area(p->area[i]);
free_page(p);
2005-12-05 04:50:02 +03:00
if (page) {
show_page(page[sel]);
2005-12-05 22:38:03 +03:00
def[WM_SEL_PAGE]->content = page[sel]->file[P_PREFIX]->content;
2005-12-06 00:22:24 +03:00
sel_page(page[sel]);
}
}
2005-12-05 01:45:59 +03:00
void free_page(Page * p)
2005-11-18 18:54:58 +03:00
{
2005-12-05 04:50:02 +03:00
page = (Page **) detach_item((void **) page, p, sizeof(Page *));
if (page) {
if (sel - 1 >= 0)
sel--;
2005-11-18 18:54:58 +03:00
else
sel = 0;
2005-11-18 18:54:58 +03:00
}
2005-12-05 04:50:02 +03:00
def[WM_SEL_PAGE]->content = 0;
2005-12-05 22:38:03 +03:00
ixp_remove_file(ixps, p->file[P_PREFIX]);
2005-11-18 18:54:58 +03:00
if (ixps->errstr)
fprintf(stderr, "wmiiwm: free_page(): %s\n", ixps->errstr);
free(p);
}
2005-12-06 00:22:24 +03:00
void sel_page(Page * p)
{
2005-12-05 04:50:02 +03:00
if (!page)
return;
2005-12-05 04:50:02 +03:00
if (p != page[sel]) {
hide_page(page[sel]);
sel = index_item((void **) page, p);
show_page(page[sel]);
2005-12-05 22:38:03 +03:00
def[WM_SEL_PAGE]->content = p->file[P_PREFIX]->content;
}
2005-12-05 22:38:03 +03:00
invoke_wm_event(def[WM_EVENT_PAGE_UPDATE]);
2005-12-06 00:22:24 +03:00
sel_area(p->area[p->sel], !p->sel);
}
2005-12-05 01:45:59 +03:00
void draw_page(Page * p)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int i;
2005-11-18 18:54:58 +03:00
if (!p)
return;
2005-12-05 04:50:02 +03:00
for (i = 0; p->area && p->area[i]; i++)
draw_area(p->area[i]);
2005-11-18 18:54:58 +03:00
}
2005-12-05 01:45:59 +03:00
XRectangle *rectangles(unsigned int *num)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
XRectangle *result = 0;
int i, j = 0;
Window d1, d2;
Window *wins;
2005-11-18 18:54:58 +03:00
XWindowAttributes wa;
2005-12-05 01:45:59 +03:00
XRectangle r;
2005-11-18 18:54:58 +03:00
if (XQueryTree(dpy, root, &d1, &d2, &wins, num)) {
result = cext_emalloc(*num * sizeof(XRectangle));
2005-11-18 18:54:58 +03:00
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;
}
}
}
if (wins) {
XFree(wins);
}
*num = j;
return result;
}
2005-12-05 01:45:59 +03:00
static void center_pointer(Frame * f)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
Window dummy;
int wex, wey, ex, ey, i;
unsigned int dmask;
2005-11-18 18:54:58 +03:00
if (!f)
return;
XQueryPointer(dpy, f->win, &dummy, &dummy, &i, &i, &wex, &wey, &dmask);
XTranslateCoordinates(dpy, f->win, root, wex, wey, &ex, &ey, &dummy);
if (blitz_ispointinrect(ex, ey, &f->rect))
2005-11-18 18:54:58 +03:00
return;
/* suppress EnterNotify's while mouse warping */
XSelectInput(dpy, root, ROOT_MASK & ~StructureNotifyMask);
2005-12-05 01:45:59 +03:00
XWarpPointer(dpy, None, f->win, 0, 0, 0, 0, f->rect.width / 2,
f->rect.height / 2);
2005-11-18 18:54:58 +03:00
XSync(dpy, False);
XSelectInput(dpy, root, ROOT_MASK);
}
2005-12-05 01:45:59 +03:00
static void select_frame(void *obj, char *cmd)
2005-11-18 18:54:58 +03:00
{
int i;
Frame *f, *old;
2005-12-05 04:50:02 +03:00
f = old = page ? SELFRAME(page[sel]) : 0;
2005-12-05 01:45:59 +03:00
if (!f || !cmd)
2005-11-18 18:54:58 +03:00
return;
if (!strncmp(cmd, "prev", 5)) {
2005-12-05 04:50:02 +03:00
i = index_prev_item((void **) f->area->frame, f);
f = f->area->frame[i];
} else if (!strncmp(cmd, "next", 5)) {
2005-12-05 04:50:02 +03:00
i = index_next_item((void **) f->area->frame, f);
f = f->area->frame[i];
}
if (old != f) {
2005-12-06 00:22:24 +03:00
sel_frame(f, 1);
center_pointer(f);
2005-11-18 18:54:58 +03:00
draw_frame(old);
draw_frame(f);
}
2005-11-18 18:54:58 +03:00
}
2005-12-05 01:45:59 +03:00
void hide_page(Page * p)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int i;
2005-12-05 04:50:02 +03:00
for (i = 0; p->area && p->area[i]; i++)
hide_area(p->area[i]);
2005-11-18 18:54:58 +03:00
}
2005-12-05 01:45:59 +03:00
void show_page(Page * p)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int i;
2005-12-05 04:50:02 +03:00
for (i = 0; p->area && p->area[i]; i++)
show_area(p->area[i]);
2005-11-18 18:54:58 +03:00
}
2005-12-05 01:45:59 +03:00
static void handle_after_write_page(IXPServer * s, File * f)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int i;
2005-11-18 18:54:58 +03:00
2005-12-05 04:50:02 +03:00
for (i = 0; page && page[i]; i++) {
Page *p = page[i];
2005-12-05 22:38:03 +03:00
if (p->file[P_CTL] == f) {
2005-11-18 18:54:58 +03:00
run_action(f, p, page_acttbl);
return;
}
/*
2005-12-05 22:38:03 +03:00
else if (p->file[P_MANAGED_SIZE] == f) {
2005-12-05 01:45:59 +03:00
/ resize stuff /
blitz_strtorect(dpy, &rect, &p->managed_rect,
2005-12-05 22:38:03 +03:00
p->file[P_MANAGED_SIZE]->content);
2005-12-05 01:45:59 +03:00
if (!p->managed_rect.width)
p->managed_rect.width = 10;
if (!p->managed_rect.height)
p->managed_rect.height = 10;
if (p->layout)
p->layout->arrange(p);
draw_page(p);
return;
2005-12-05 22:38:03 +03:00
} else if (p->file[P_MANAGED_LAYOUT] == f) {
2005-12-05 01:45:59 +03:00
int had_valid_layout = p->layout ? 1 : 0;
if (p->layout)
p->layout->deinit(p);
2005-12-05 22:38:03 +03:00
p->layout = get_layout(p->file[P_MANAGED_LAYOUT]->content);
2005-12-05 01:45:59 +03:00
if (p->layout) {
p->layout->init(p);
p->layout->arrange(p);
if (!had_valid_layout) {
int j;
Frame **tmp = 0;
for (j = 0; p->floating && p->floating[j]; j++) {
if (!p->floating[j]->floating)
tmp =
(Frame **) attach_item_begin((void **) tmp,
p->
floating[j],
sizeof(Frame
*));
}
for (j = 0; tmp && tmp[j]; j++)
toggle_frame(tmp[j]);
free(tmp);
}
}
if (!p->layout) {
2005-12-05 04:50:02 +03:00
/ make all managed client floating /
2005-12-05 01:45:59 +03:00
int j;
Frame **tmp = 0;
while (p->managed) {
tmp = (Frame **) attach_item_begin((void **) tmp,
p->managed[0],
sizeof(Frame *));
detach_frame_from_page(p->managed[0], 1);
}
for (j = 0; tmp && tmp[j]; j++) {
attach_Frameo_page(p, tmp[j], 0);
resize_frame(tmp[j], rect_of_frame(tmp[j]), 0, 1);
}
free(tmp);
}
draw_page(p);
2005-12-05 22:38:03 +03:00
invoke_wm_event(wm_file[CORE_EVENT_PAGE_UPDATE]);
2005-12-05 01:45:59 +03:00
return;
}
*/
2005-11-18 18:54:58 +03:00
}
}
/*
2005-11-18 18:54:58 +03:00
void
attach_frame_to_page(Page * p, Frame * f, int managed)
2005-11-18 18:54:58 +03:00
{
Frame *old = get_selected(p);
XSelectInput(dpy, root, ROOT_MASK & ~StructureNotifyMask);
XMapRaised(dpy, f->win);
if (!f->floating && managed && p->layout) {
int i;
p->managed = (Frame **) attach_item_end((void **) p->managed, f,
sizeof(Frame *));
p->managed_stack =
(Frame **) attach_item_begin((void **) p->managed_stack, f,
sizeof(Frame *));
2005-12-05 22:38:03 +03:00
wmii_move_ixpfile(f->file[F_PREFIX], p->file[P_MANAGED_PREFIX]);
p->file[P_MANAGED_SELECTED]->content =
f->file[F_PREFIX]->content;
2005-12-05 04:50:02 +03:00
if (p == page[sel_page])
2005-11-18 18:54:58 +03:00
for (i = 0; p->floating && p->floating[i]; i++)
XRaiseWindow(dpy, p->floating[i]->win);
} else {
p->floating = (Frame **) attach_item_end((void **) p->floating, f,
sizeof(Frame *));
p->floating_stack =
(Frame **) attach_item_begin((void **) p->floating_stack, f,
sizeof(Frame *));
2005-12-05 22:38:03 +03:00
wmii_move_ixpfile(f->file[F_PREFIX], p->file[P_FLOATING_PREFIX]);
p->file[P_FLOATING_SELECTED]->content =
f->file[F_PREFIX]->content;
p->file[P_MODE]->content = p->file[P_FLOATING_PREFIX]->content;
2005-11-18 18:54:58 +03:00
}
f->page = p;
2005-12-05 22:38:03 +03:00
sel_frame(f, 1, 0, 1);
2005-11-18 18:54:58 +03:00
if (is_managed_frame(f) && p->layout)
p->layout->manage(f);
center_pointer(f);
if (old)
draw_frame(old);
draw_frame(f);
}
void
2005-12-05 22:38:03 +03:00
detach_frame_from_page(Frame * f, int ignore_sel_and_destroy)
2005-11-18 18:54:58 +03:00
{
Page *p = f->page;
2005-12-05 22:38:03 +03:00
wmii_move_ixpfile(f->file[F_PREFIX], wm_file[CORE_DETACHED_FRAME]);
2005-11-18 18:54:58 +03:00
if (is_managed_frame(f)) {
p->managed = (Frame **) detach_item((void **) p->managed, f,
sizeof(Frame *));
p->managed_stack =
(Frame **) detach_item((void **) p->managed_stack, f,
sizeof(Frame *));
2005-12-05 22:38:03 +03:00
p->file[P_MANAGED_SELECTED]->content = 0;
2005-11-18 18:54:58 +03:00
} else {
p->floating = (Frame **) detach_item((void **) p->floating, f,
sizeof(Frame *));
p->floating_stack =
(Frame **) detach_item((void **) p->floating_stack, f,
sizeof(Frame *));
2005-12-05 22:38:03 +03:00
p->file[P_FLOATING_SELECTED]->content = 0;
2005-11-18 18:54:58 +03:00
}
XUnmapWindow(dpy, f->win);
if (is_managed_mode(p) && p->layout)
p->layout->unmanage(f);
f->page = 0;
2005-12-05 22:38:03 +03:00
if (!ignore_sel_and_destroy) {
2005-11-18 18:54:58 +03:00
Frame *fr;
if (!p->managed && !p->floating
2005-12-05 22:38:03 +03:00
&& _strtonum(p->file[P_AUTO_DESTROY]->content, 0, 1)) {
2005-11-18 18:54:58 +03:00
destroy_page(p);
return;
}
2005-12-05 22:38:03 +03:00
sel_page(p, 0, 1);
2005-11-18 18:54:58 +03:00
fr = get_selected(p);
if (fr) {
center_pointer(fr);
draw_frame(fr);
}
}
}
*/
Page *get_sel_page()
{
return cext_get_top_item(&page);
}