From 73198f96e32d9dbd88abe5bc35d4dac3dc1647a6 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Sun, 8 Mar 2009 12:26:04 +0000 Subject: [PATCH] Allow dragging windows Left-click to drag and activate. Right-click to drag only. Raising and lowering windows doesn't work and will be removed. --- pager.c | 170 +++++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 130 insertions(+), 40 deletions(-) diff --git a/pager.c b/pager.c index 86fd07a..85dfd5f 100644 --- a/pager.c +++ b/pager.c @@ -7,11 +7,25 @@ #define DOCKAPP_SIZE 56 #define SPEED 100 +#ifndef FromTool +#define FromTool 2 +#endif static GC dockapp_border1GC, dockapp_border2GC, window_border1GC, window_border2GC, active_desktopGC, inactive_desktopGC, active_windowGC, inactive_windowGC; -static Atom num_desktops_atom, current_desktop_atom, client_list_atom, client_desktop_atom, client_state_atom, active_window_atom; -static Atom shaded_state, skip_pager_state, hidden_state; +static Atom supported_atom, num_desktops_atom, desktop_geometry_atom, current_desktop_atom, client_list_atom, client_desktop_atom, client_state_atom, active_window_atom, moveresize_window_atom; +static Atom above_state, below_state, shaded_state, skip_pager_state, hidden_state; static long desktop = -1; +static Window drag_window; +static int drag_x, drag_y; +static int drag_button; +static int dockapp_x, dockapp_y, dockapp_width, dockapp_height; +static unsigned int dockapp_border, dockapp_depth; +static double scale; + +static int desktop_geometry_supported; +static int current_desktop_supported; +static int active_window_supported; +static int moveresize_supported; typedef struct { Window *window; @@ -40,7 +54,9 @@ int error_handler(Display *display, XErrorEvent *event) { void setup_atom(Atom *atom, const char *prop) { *atom = XInternAtom(DADisplay, prop, True); - if (*atom == None) exit(111); + if (*atom != None) return; + fprintf(stderr, "Unknown atom %s!\n", prop); + exit(111); } void get_atom_longs(Atom atom, Atom type, Window window, long **data, unsigned long *num_items) { @@ -63,25 +79,45 @@ 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) { +void check_support() { + long *data; + unsigned long i, num_items; + + get_atom_longs(supported_atom, XA_ATOM, DefaultRootWindow(DADisplay), &data, &num_items); + for (i = 0; i < num_items; i++) { + if (data[i] == desktop_geometry_atom) desktop_geometry_supported = 1; + if (data[i] == current_desktop_atom) current_desktop_supported = 1; + if (data[i] == active_window_atom) active_window_supported = 1; + if (data[i] == moveresize_window_atom) moveresize_supported = 1; + } + + XFree(data); +} + +void enumerate_clients(client_t ***clients, unsigned long *num_clients, unsigned long *active_desktop, unsigned long *active_window) { Window root, dockapp_root, child; - unsigned int x, y, width, height, border, depth; + int x, y; + unsigned int 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)); + if (desktop_geometry_supported) { + get_atom_longs(desktop_geometry_atom, XA_CARDINAL, DefaultRootWindow(DADisplay), &states, &num_states); + root_width = states[0]; + root_height = states[0]; + XFree(states); + } + else { + 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; + 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)); @@ -128,6 +164,8 @@ void enumerate_clients(client_t ***clients, unsigned long *num_clients, unsigned (*clients)[i]->rect.width = (double) width / scale; (*clients)[i]->rect.height = (double) height / scale; } + + XFree(data); } void destroy_clients(client_t ***clients, unsigned long num_clients) { @@ -192,49 +230,83 @@ int clicked(DARect *rect, int x, int y) { 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; +int client_message(Atom message_type, Window window, long data0, long data1, long data2, long data3, long data4) { XEvent event; - Window root = DefaultRootWindow(DADisplay); + memset(&event, 0, sizeof(event)); event.type = ClientMessage; event.xclient.type = ClientMessage; - event.xclient.window = root; - event.xclient.message_type = 0; + event.xclient.send_event = True; + event.xclient.window = window; + event.xclient.message_type = message_type; event.xclient.format = 32; + event.xclient.data.l[0] = data0; + event.xclient.data.l[1] = data1; + event.xclient.data.l[2] = data2; + event.xclient.data.l[3] = data3; + event.xclient.data.l[4] = data4; - enumerate_clients(&clients, &num_clients, &active_desktop, &active_window, &dockapp_width, &dockapp_height); + return XSendEvent(DADisplay, DefaultRootWindow(DADisplay), False, SubstructureRedirectMask | SubstructureNotifyMask, &event); +} + +void change(int button, int state, int x, int y) { + client_t **clients; + unsigned long i, num_clients; + unsigned long active_desktop, active_window; + + enumerate_clients(&clients, &num_clients, &active_desktop, &active_window); 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); + if (button == 4) { + client_message(client_state_atom, clients[i]->window, 0, above_state, 0, 0, 0); + client_message(client_state_atom, clients[i]->window, 1, below_state, 0, 0, 0); } - else if (button == 2 || button == 5) { - XLowerWindow(DADisplay, clients[i]->window); + else if (button == 5) { + client_message(client_state_atom, clients[i]->window, 0, below_state, 0, 0, 0); + client_message(client_state_atom, clients[i]->window, 1, above_state, 0, 0, 0); } 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; + drag_window = clients[i]->window; + drag_button = button; + drag_x = x - clients[i]->rect.x; + drag_y = y - clients[i]->rect.y; } break; } destroy_clients(&clients, num_clients); - if (! event.xclient.message_type) { - event.xclient.message_type = current_desktop_atom; - event.xclient.data.l[0] = desktop; + if (drag_window && button == 1) { + client_message(active_window_atom, drag_window, FromTool, CurrentTime, 0, 0 ,0); + } + else { + client_message(current_desktop_atom, DefaultRootWindow(DADisplay), desktop, 0, 0, 0, 0); + } +} + +void release(int button, int state, int x, int y) { + if (button == drag_button) drag_window = 0; +} + +void move(int x, int y) { + long dest_x, dest_y; + + if (! drag_window) return; + if (x < 0 || y < 0) return; + if (x >= dockapp_width || y >= dockapp_height) return; + + dest_x = (long) ((double) (x - drag_x) * scale); + dest_y = (long) ((double) (y - drag_y) * scale); + + if (moveresize_supported) { + client_message(moveresize_window_atom, drag_window, StaticGravity | (3 << 8) | (FromTool << 12), drag_x, drag_y, 0, 0); + } + else { + XMoveWindow(DADisplay, drag_window, dest_x, dest_y); } - XSendEvent(DADisplay, root, False, SubstructureNotifyMask, &event); + page(); } void page() { @@ -248,9 +320,8 @@ void page() { int active; GC gc; client_t **clients; - unsigned int dockapp_width, dockapp_height; - enumerate_clients(&clients, &num_clients, &active_desktop, &active_window, &dockapp_width, &dockapp_height); + enumerate_clients(&clients, &num_clients, &active_desktop, &active_window); pixmap = DAMakePixmap(); if (active_desktop == desktop) gc = active_desktopGC; @@ -259,9 +330,22 @@ void page() { for (i = 0; i < num_clients; i++) { if (! clients[i]->window) continue; + + /* Draw windows which are partially off-screen. */ + if (clients[i]->rect.x < 0) { + clients[i]->rect.width += clients[i]->rect.x; + clients[i]->rect.x = 0; + } + if (clients[i]->rect.y < 0) { + clients[i]->rect.height += clients[i]->rect.y; + clients[i]->rect.y = 0; + } + + /* Draw small windows. */ 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, 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; @@ -300,10 +384,9 @@ void page() { int main(int argc, char **argv) { Display *display; unsigned int root_width, root_height; - unsigned int dockapp_width, dockapp_height; long num_desktops; double aspect; - DACallbacks callbacks = { 0, change, 0, 0, 0, 0, page }; + DACallbacks callbacks = { 0, change, release, move, 0, 0, page }; DAProgramOption options = { "-d", "--desktop", "Desktop to page", DOInteger, 0, { (int *) &desktop } }; DASetExpectedVersion(20020126); @@ -330,17 +413,24 @@ int main(int argc, char **argv) { DASetCallbacks(&callbacks); DASetTimeout(SPEED); + setup_atom(&supported_atom, "_NET_SUPPORTED"); setup_atom(&num_desktops_atom, "_NET_NUMBER_OF_DESKTOPS"); + setup_atom(&desktop_geometry_atom, "_NET_DESKTOP_GEOMETRY"); setup_atom(¤t_desktop_atom, "_NET_CURRENT_DESKTOP"); setup_atom(&client_list_atom, "_NET_CLIENT_LIST_STACKING"); setup_atom(&client_desktop_atom, "_NET_WM_DESKTOP"); setup_atom(&client_state_atom, "_NET_WM_STATE"); setup_atom(&active_window_atom, "_NET_ACTIVE_WINDOW"); + setup_atom(&moveresize_window_atom, "_NET_MOVERESIZE_WINDOW"); + setup_atom(&above_state, "_NET_WM_STATE_ABOVE"); + setup_atom(&below_state, "_NET_WM_STATE_BELOW"); setup_atom(&shaded_state, "_NET_WM_STATE_SHADED"); setup_atom(&skip_pager_state, "_NET_WM_STATE_SKIP_PAGER"); setup_atom(&hidden_state, "_NET_WM_STATE_HIDDEN"); + check_support(); + num_desktops = get_atom_long(num_desktops_atom, XA_CARDINAL, DefaultRootWindow(DADisplay)); if (desktop < 1) { desktop = get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay)); -- 2.7.4