From: Iain Patterson Date: Sat, 7 Mar 2009 13:46:05 +0000 (+0000) Subject: Allow selecting windows X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=41ad7f9d481912ac462cae06d7fcea4ba9eeaf41;p=pager.git Allow selecting windows Left click to activate a window. Right click to raise a window (XXX: the wrong way). Middle click to lower a window (XXX: the wrong way). --- diff --git a/pager.c b/pager.c index f5f05c0..86fd07a 100644 --- a/pager.c +++ b/pager.c @@ -13,6 +13,11 @@ static Atom num_desktops_atom, current_desktop_atom, client_list_atom, client_de static Atom shaded_state, skip_pager_state, hidden_state; static long desktop = -1; +typedef struct { + Window *window; + DARect rect; +} client_t; + int error_handler(Display *display, XErrorEvent *event) { char buffer[512]; int length; @@ -58,6 +63,82 @@ long get_atom_long(Atom atom, Atom type, Window window) { return ret; } +/* XXX: enumerate_clients_and_get_a_bunch_of_other_values() */ +void enumerate_clients(client_t ***clients, unsigned long *num_clients, unsigned long *active_desktop, unsigned long *active_window, unsigned int *dockapp_width, unsigned int *dockapp_height) { + Window root, dockapp_root, child; + unsigned int x, y, width, height, border, depth; + unsigned int root_width, root_height; + unsigned int dockapp_x, dockapp_y, dockapp_border, dockapp_depth; + double scale; + long *data; + long *states; + unsigned long i, j, num_states; + long client_desktop; + int draw; + + /* XXX: Use _NET_DESKTOP_GEOMETRY. */ + root_width = DisplayWidth(DADisplay, DefaultScreen(DADisplay)); + root_height = DisplayWidth(DADisplay, DefaultScreen(DADisplay)); + + XGetGeometry(DADisplay, DAWindow, &dockapp_root, &dockapp_x, &dockapp_y, dockapp_width, dockapp_height, &dockapp_border, &dockapp_depth); + scale = (double) root_width / (double) *dockapp_width; + + *active_window = get_atom_long(active_window_atom, XA_WINDOW, dockapp_root); + *active_desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay)); + + get_atom_longs(client_list_atom, XA_WINDOW, dockapp_root, &data, num_clients); + + *clients = (client_t **) malloc(sizeof(client_t *) * *num_clients); + if (! *clients) { + fprintf(stderr, "Out of memory for clients!\n"); + exit(111); + } + + for (i = 0; i < *num_clients; i++) { + (*clients)[i] = (client_t *) malloc(sizeof(client_t)); + if (! (*clients)[i]) { + fprintf(stderr, "Out of memory for client!\n"); + exit(111); + } + (*clients)[i]->window = 0; + + /* Check the window is on our desktop (or all desktops). */ + client_desktop = get_atom_long(client_desktop_atom, XA_CARDINAL, data[i]); + if (client_desktop != desktop && client_desktop != -1) continue; + + /* Try to get its dimensions. */ + if (! XGetGeometry(DADisplay, data[i], &root, &x, &y, &width, &height, &border, &depth)) continue; + if (! XTranslateCoordinates(DADisplay, data[i], root, x, y, &x, &y, &child)) continue; + + /* Make sure it isn't hidden or shaded. */ + draw = 1; + get_atom_longs(client_state_atom, XA_ATOM, data[i], &states, &num_states); + for (j = 0; j < num_states; j++) { + if (states[j] == shaded_state) draw = 0; + if (states[j] == skip_pager_state) draw = 0; + if (states[j] == hidden_state) draw = 0; + } + if (! draw) continue; + + (*clients)[i]->window = data[i]; + + /* Scale it. */ + (*clients)[i]->rect.x = (double) x / scale; + (*clients)[i]->rect.y = (double) y / scale; + (*clients)[i]->rect.width = (double) width / scale; + (*clients)[i]->rect.height = (double) height / scale; + } +} + +void destroy_clients(client_t ***clients, unsigned long num_clients) { + unsigned long i; + + if (! *clients) return; + + for (i = 0; i < num_clients; i++) free((*clients)[i]); + free(*clients); +} + void setup_GCs() { XGCValues gcv; @@ -103,76 +184,86 @@ void draw_relief(Pixmap pixmap, unsigned int width, unsigned int height, GC desk XDrawLine(DADisplay, pixmap, dockapp_border1GC, width - 1, 1, width - 1, height - 2); } -void change() { +int clicked(DARect *rect, int x, int y) { + if (rect->x > x) return 0; + if (rect->y > y) return 0; + if (rect->x + rect->width < x) return 0; + if (rect->y + rect->height < y) return 0; + return 1; +} + +void change(int button, int state, int x, int y) { + client_t **clients; + unsigned long i, num_clients; + unsigned long active_desktop, active_window; + unsigned int dockapp_width, dockapp_height; XEvent event; Window root = DefaultRootWindow(DADisplay); event.type = ClientMessage; event.xclient.type = ClientMessage; event.xclient.window = root; - event.xclient.message_type = current_desktop_atom; + event.xclient.message_type = 0; event.xclient.format = 32; - event.xclient.data.l[0] = desktop; + + enumerate_clients(&clients, &num_clients, &active_desktop, &active_window, &dockapp_width, &dockapp_height); + for (i = num_clients - 1; i > 0; i--) { + if (! clients[i]->window) continue; + if (! clicked(&clients[i]->rect, x, y)) continue; + + /* XXX: Use NET_RESTACK_WINDOW instead. */ + if (button == 3 || button == 4) { + XRaiseWindow(DADisplay, clients[i]->window); + } + else if (button == 2 || button == 5) { + XLowerWindow(DADisplay, clients[i]->window); + } + else { + event.xclient.window = clients[i]->window; + event.xclient.message_type = active_window_atom; + event.xclient.data.l[0] = 2; + event.xclient.data.l[1] = 0; /* XXX: NET_WM_USER_TIME */ + event.xclient.data.l[2] = 0; + } + break; + } + destroy_clients(&clients, num_clients); + + if (! event.xclient.message_type) { + event.xclient.message_type = current_desktop_atom; + event.xclient.data.l[0] = desktop; + } XSendEvent(DADisplay, root, False, SubstructureNotifyMask, &event); } void page() { - Window root, dockapp_root, child, active_window; + Window child, active_window; XWindowAttributes attr; Pixmap pixmap; DARect rect; long i, j; - unsigned long num_clients, num_states; - unsigned int root_width, root_height; - unsigned int dockapp_x, dockapp_y, dockapp_width, dockapp_height, dockapp_border, dockapp_depth; - unsigned int x, y, width, height, border, depth; - double scale; - long *clients, *states; + unsigned long num_clients; long client_desktop, active_desktop; int active; GC gc; + client_t **clients; + unsigned int dockapp_width, dockapp_height; - /* XXX: Use _NET_DESKTOP_GEOMETRY. */ - root_width = DisplayWidth(DADisplay, DefaultScreen(DADisplay)); - root_height = DisplayWidth(DADisplay, DefaultScreen(DADisplay)); + enumerate_clients(&clients, &num_clients, &active_desktop, &active_window, &dockapp_width, &dockapp_height); pixmap = DAMakePixmap(); - XGetGeometry(DADisplay, pixmap, &dockapp_root, &dockapp_x, &dockapp_y, &dockapp_width, &dockapp_height, &dockapp_border, &dockapp_depth); - scale = (double) root_width / (double) dockapp_width; - - active_window = get_atom_long(active_window_atom, XA_WINDOW, dockapp_root); - active_desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay)); - if (active_desktop == desktop) gc = active_desktopGC; else gc = inactive_desktopGC; draw_relief(pixmap, dockapp_width, dockapp_height, gc); - get_atom_longs(client_list_atom, XA_WINDOW, dockapp_root, &clients, &num_clients); for (i = 0; i < num_clients; i++) { - int draw = 1; - client_desktop = get_atom_long(client_desktop_atom, XA_CARDINAL, clients[i]); - if (client_desktop != desktop && client_desktop != -1) continue; - if (! XGetGeometry(DADisplay, clients[i], &root, &x, &y, &width, &height, &border, &depth)) continue; - if (! XTranslateCoordinates(DADisplay, clients[i], root, x, y, &x, &y, &child)) continue; - get_atom_longs(client_state_atom, XA_ATOM, clients[i], &states, &num_states); - for (j = 0; j < num_states; j++) { - if (states[j] == shaded_state) draw = 0; - if (states[j] == skip_pager_state) draw = 0; - if (states[j] == hidden_state) draw = 0; - } - if (! draw) continue; - - rect.x = (double) x / scale; - rect.y = (double) y / scale; - rect.width = (double) width / scale; - rect.height = (double) height / scale; - - if (rect.width < 2 || rect.height < 2) gc = window_border1GC; - else if (clients[i] == active_window) gc = active_windowGC; + if (! clients[i]->window) continue; + if (clients[i]->rect.width < 2 || clients[i]->rect.height < 2) gc = window_border1GC; + else if (clients[i]->window == active_window) gc = active_windowGC; else gc = inactive_windowGC; - XFillRectangle(DADisplay, pixmap, gc, rect.x, rect.y, rect.width, rect.height); - if (rect.width < 2 || rect.height < 2) continue; + XFillRectangle(DADisplay, pixmap, gc, clients[i]->rect.x, clients[i]->rect.y, clients[i]->rect.width, clients[i]->rect.height); + if (clients[i]->rect.width < 2 || clients[i]->rect.height < 2) continue; /* Thumbnail. */ #if 0 @@ -194,12 +285,14 @@ void page() { XSync(DADisplay, False); #endif - XDrawLine(DADisplay, pixmap, window_border1GC, rect.x, rect.y, rect.x, rect.y + rect.height - 2); - XDrawLine(DADisplay, pixmap, window_border1GC, rect.x + 1, rect.y, rect.x + rect.width - 1, rect.y); - XDrawLine(DADisplay, pixmap, window_border2GC, rect.x, rect.y + rect.height - 1, rect.x + rect.width - 1, rect.y + rect.height - 1); - XDrawLine(DADisplay, pixmap, window_border2GC, rect.x + rect.width - 1, rect.y + 1, rect.x + rect.width - 1, rect.y + rect.height - 2); + XDrawLine(DADisplay, pixmap, window_border1GC, clients[i]->rect.x, clients[i]->rect.y, clients[i]->rect.x, clients[i]->rect.y + clients[i]->rect.height - 2); + XDrawLine(DADisplay, pixmap, window_border1GC, clients[i]->rect.x + 1, clients[i]->rect.y, clients[i]->rect.x + clients[i]->rect.width - 1, clients[i]->rect.y); + XDrawLine(DADisplay, pixmap, window_border2GC, clients[i]->rect.x, clients[i]->rect.y + clients[i]->rect.height - 1, clients[i]->rect.x + clients[i]->rect.width - 1, clients[i]->rect.y + clients[i]->rect.height - 1); + XDrawLine(DADisplay, pixmap, window_border2GC, clients[i]->rect.x + clients[i]->rect.width - 1, clients[i]->rect.y + 1, clients[i]->rect.x + clients[i]->rect.width - 1, clients[i]->rect.y + clients[i]->rect.height - 2); } + destroy_clients(&clients, num_clients); + DASetPixmap(pixmap); XFreePixmap(DADisplay, pixmap); }