X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=pager.c;h=4d99ae321f358498d36251dc6eb0c57474a021ca;hb=HEAD;hp=f5f05c09241f2b6c597b10dd040a782495e53411;hpb=2383271efc00185ccb3aa0012427350b8e61c1e1;p=pager.git diff --git a/pager.c b/pager.c index f5f05c0..4d99ae3 100644 --- a/pager.c +++ b/pager.c @@ -1,21 +1,37 @@ /* gcc -o pager pager.c -lX11 -lXmu -ldockapp */ -#if 0 -#include -#endif #include #include +#include #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 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 shaded_state, skip_pager_state, hidden_state; -static long desktop = -1; +static int desktop = -1; +static Window drag_window; +static int drag_x, drag_y; +static int drag_button; +static XWindowAttributes dockapp_attr; +static double aspect, scale; + +static int desktop_geometry_supported; +static int current_desktop_supported; +static int client_list_supported; +static int active_window_supported; +static int moveresize_supported; + +typedef struct { + Window *window; + DARect rect; +} client_t; int error_handler(Display *display, XErrorEvent *event) { char buffer[512]; - int length; switch (event->error_code) { case BadWindow: @@ -35,27 +51,168 @@ 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); +} + +/* Don't page the pager. */ +void setup_pager() { + long states = { skip_pager_state }; + char buffer[16]; + + XChangeProperty(DADisplay, DAWindow, client_state_atom, XA_ATOM, 32, PropModeAppend, (unsigned char *) &states, 1); + + if (desktop >= 0) { + if (snprintf(buffer, sizeof(buffer), "Desktop %d", desktop + 1) < 0) return; + } + else { + if (snprintf(buffer, sizeof(buffer), "All desktops") < 0) return; + } + XStoreName(DADisplay, DAWindow, buffer); } -void get_atom_longs(Atom atom, Atom type, Window window, long **data, unsigned long *num_items) { +int get_atom_longs(Atom atom, Atom type, Window window, long **data, unsigned long *num_items) { Atom actual; int format; unsigned long num_bytes; - XGetWindowProperty(DADisplay, window, atom, 0, 8192, False, type, &actual, &format, num_items, &num_bytes, (unsigned char **) data); + return XGetWindowProperty(DADisplay, window, atom, 0, 8192, False, type, &actual, &format, num_items, &num_bytes, (unsigned char **) data); } -long get_atom_long(Atom atom, Atom type, Window window) { +int get_atom_long(Atom atom, Atom type, Window window, long *ret) { long *data; - long ret = 0; + int status; unsigned long num_items; - get_atom_longs(atom, type, window, &data, &num_items); - if (num_items) ret = data[0]; + *ret = 0; + + status = get_atom_longs(atom, type, window, &data, &num_items); + if (status != Success) return status; + if (num_items) *ret = data[0]; XFree(data); - return ret; + return status; +} + +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] == client_list_atom) client_list_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, qroot, qparent, child, *wins, client; + int x, y; + unsigned int width, height, border, depth; + unsigned int root_width, root_height; + long *data; + long *states; + unsigned long i, j, num_states; + long client_desktop; + int draw; + + if (desktop_geometry_supported) { + get_atom_longs(desktop_geometry_atom, XA_CARDINAL, DefaultRootWindow(DADisplay), &states, &num_states); + root_width = states[0]; + root_height = states[1]; + XFree(states); + } + else { + root_width = DisplayWidth(DADisplay, DefaultScreen(DADisplay)); + root_height = DisplayWidth(DADisplay, DefaultScreen(DADisplay)); + } + + *num_clients = 0; + *clients = 0; + + if (! XGetWindowAttributes(DADisplay, DAWindow, &dockapp_attr)) return; + scale = (double) root_width / (double) dockapp_attr.width; + aspect = (double) root_height / (double) dockapp_attr.height; + + if (get_atom_long(active_window_atom, XA_WINDOW, dockapp_attr.root, (long *) active_window) != Success) *active_window = 0; + if (get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay), (long *) active_desktop) != Success) *active_desktop = 0; + + if (client_list_supported) { + if (get_atom_longs(client_list_atom, XA_WINDOW, dockapp_attr.root, &data, num_clients) != Success) XFree(data); + } + else { + XQueryTree(DADisplay, dockapp_attr.root, &qroot, &qparent, &wins, (unsigned int *) 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; + + if (client_list_supported) client = data[i]; + else client = XmuClientWindow(DADisplay, wins[i]); + + /* Check the window is on our desktop (or all desktops). */ + if (get_atom_long(client_desktop_atom, XA_CARDINAL, client, &client_desktop) != Success) continue; + if (desktop > -1) { + if (client_desktop != desktop && client_desktop != -1) continue; + } + else { + if (client_desktop != *active_desktop && client_desktop != -1) continue; + } + + /* Try to get its dimensions. */ + if (! XGetGeometry(DADisplay, client, &root, &x, &y, &width, &height, &border, &depth)) continue; + if (! XTranslateCoordinates(DADisplay, client, root, x, y, &x, &y, &child)) continue; + + /* Make sure it isn't hidden or shaded. */ + draw = 1; + if (get_atom_longs(client_state_atom, XA_ATOM, client, &states, &num_states) == Success) { + for (j = 0; j < num_states; j++) { + if (states[j] == shaded_state) height = 3.0 * scale; + if (states[j] == skip_pager_state) draw = 0; + if (states[j] == hidden_state) draw = 0; + } + XFree(states); + } + else continue; + if (! draw) continue; + + (*clients)[i]->window = client; + + /* Scale it. */ + (*clients)[i]->rect.x = (double) x / scale; + (*clients)[i]->rect.y = (double) y / aspect; + (*clients)[i]->rect.width = (double) width / scale; + (*clients)[i]->rect.height = (double) height / aspect; + } + + if (client_list_supported) XFree(data); + else XFree(wins); +} + +void destroy_clients(client_t ***clients, unsigned long num_clients) { + unsigned long i; + + if (! *clients) return; + + for (i = 0; i < num_clients; i++) if ((*clients)[i]) free((*clients)[i]); + free(*clients); } void setup_GCs() { @@ -103,114 +260,150 @@ 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; +} + +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 = current_desktop_atom; + event.xclient.send_event = True; + event.xclient.window = window; + event.xclient.message_type = message_type; event.xclient.format = 32; - event.xclient.data.l[0] = desktop; + 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; + + 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; + + drag_window = (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 (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 draw_window(Pixmap pixmap, GC gc, client_t *client) { + /* Draw a blob for the window. */ + XFillRectangle(DADisplay, pixmap, gc, client->rect.x, client->rect.y, client->rect.width, client->rect.height); + if (client->rect.width < 2 || client->rect.height < 2) return; - XSendEvent(DADisplay, root, False, SubstructureNotifyMask, &event); + /* Draw a border around the window. */ + XDrawLine(DADisplay, pixmap, window_border1GC, client->rect.x, client->rect.y, client->rect.x, client->rect.y + client->rect.height - 2); + XDrawLine(DADisplay, pixmap, window_border1GC, client->rect.x + 1, client->rect.y, client->rect.x + client->rect.width - 1, client->rect.y); + XDrawLine(DADisplay, pixmap, window_border2GC, client->rect.x, client->rect.y + client->rect.height - 1, client->rect.x + client->rect.width - 1, client->rect.y + client->rect.height - 1); + XDrawLine(DADisplay, pixmap, window_border2GC, client->rect.x + client->rect.width - 1, client->rect.y + 1, client->rect.x + client->rect.width - 1, client->rect.y + client->rect.height - 2); + + if (dockapp_attr.width <= DOCKAPP_SIZE * 2) return; + if (dockapp_attr.height <= DOCKAPP_SIZE * 2) return; + + /* Draw a drop shadow. */ + XDrawLine(DADisplay, pixmap, dockapp_border2GC, client->rect.x + 1, client->rect.y + client->rect.height, client->rect.x + client->rect.width, client->rect.y + client->rect.height); + XDrawLine(DADisplay, pixmap, dockapp_border2GC, client->rect.x + client->rect.width, client->rect.y + 1, client->rect.x + client->rect.width, client->rect.y + client->rect.height); } void page() { - Window root, dockapp_root, child, active_window; - XWindowAttributes attr; + Window active_window; 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; - long client_desktop, active_desktop; - int active; + long i; + unsigned long num_clients; + unsigned long active_desktop; GC gc; + client_t **clients; - /* XXX: Use _NET_DESKTOP_GEOMETRY. */ - root_width = DisplayWidth(DADisplay, DefaultScreen(DADisplay)); - root_height = DisplayWidth(DADisplay, DefaultScreen(DADisplay)); - - 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)); + enumerate_clients(&clients, &num_clients, &active_desktop, &active_window); - if (active_desktop == desktop) gc = active_desktopGC; + pixmap = XCreatePixmap(DADisplay, DAWindow, dockapp_attr.width, dockapp_attr.height, dockapp_attr.depth); + if (active_desktop == desktop || desktop < 0) gc = active_desktopGC; else gc = inactive_desktopGC; - draw_relief(pixmap, dockapp_width, dockapp_height, gc); + draw_relief(pixmap, dockapp_attr.width, dockapp_attr.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; + if (! clients[i]->window) continue; - rect.x = (double) x / scale; - rect.y = (double) y / scale; - rect.width = (double) width / scale; - rect.height = (double) height / scale; + /* 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; + } - if (rect.width < 2 || rect.height < 2) gc = window_border1GC; - else if (clients[i] == active_window) gc = active_windowGC; + /* Draw small windows. */ + if (clients[i]->rect.width < 2 || clients[i]->rect.height < 2) gc = window_border1GC; + else if ((long) 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; - - /* Thumbnail. */ -#if 0 - XRenderPictureAttributes pa; - Pixmap client_pixmap = XCreatePixmap(DADisplay, wins[i], width, height, DefaultDepth(DADisplay, DefaultScreen(DADisplay))); - //XCopyArea(DADisplay, wins[i], client_pixmap, DefaultGC(DADisplay, DefaultScreen(DADisplay)), 0, 0, width, height, 0, 0); - Picture client_picture = XRenderCreatePicture(DADisplay, client_pixmap, XRenderFindVisualFormat(DADisplay, attr.visual), CPSubwindowMode, &pa); - - XTransform xform = {{ - { XDoubleToFixed(1.0), 0, 0 }, - { 0, XDoubleToFixed(1.0), 0 }, - { 0, 0, XDoubleToFixed(scale) }, - }}; - - XRenderSetPictureFilter(DADisplay, client_picture, FilterBilinear, 0, 0); - XRenderSetPictureTransform(DADisplay, client_picture, &xform); - XRenderComposite(DADisplay, PictOpSrc, client_picture, None, picture, 0, 0, 0, 0, rect.x, rect.y, rect.width, rect.height); - XFreePixmap(DADisplay, client_pixmap); - 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); + draw_window(pixmap, gc, clients[i]); } + destroy_clients(&clients, num_clients); + DASetPixmap(pixmap); XFreePixmap(DADisplay, pixmap); } +void move(int x, int y) { + long dest_x, dest_y; + + if (! drag_window) return; + if (x < 0 || y < 0) return; + if (x >= dockapp_attr.width || y >= dockapp_attr.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); + } + + 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); @@ -222,43 +415,52 @@ int main(int argc, char **argv) { aspect = (double) root_width / (double) root_height; if (root_width > root_height) { - dockapp_width = DOCKAPP_SIZE; - dockapp_height = (double) DOCKAPP_SIZE / aspect; + dockapp_attr.width = DOCKAPP_SIZE; + dockapp_attr.height = (double) DOCKAPP_SIZE / aspect; } else { - dockapp_height = DOCKAPP_SIZE; - dockapp_width = (double) DOCKAPP_SIZE / aspect; + dockapp_attr.height = DOCKAPP_SIZE; + dockapp_attr.width = (double) DOCKAPP_SIZE / aspect; } - DAParseArguments(argc, argv, &options, 1, "BLURB", "BLORB"); + DAParseArguments(argc, argv, &options, 1, "OPTIONS", "pager"); DAOpenDisplay(0, argc, argv); - DACreateIcon("pager", dockapp_width, dockapp_height, argc, argv); + DACreateIcon("pager", dockapp_attr.width, dockapp_attr.height, argc, 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(&shaded_state, "_NET_WM_STATE_SHADED"); setup_atom(&skip_pager_state, "_NET_WM_STATE_SKIP_PAGER"); setup_atom(&hidden_state, "_NET_WM_STATE_HIDDEN"); - 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)); + check_support(); + + if (get_atom_long(num_desktops_atom, XA_CARDINAL, DefaultRootWindow(DADisplay), &num_desktops) != Success) num_desktops = 1; + if (desktop == 0) { + if (get_atom_long(current_desktop_atom, XA_CARDINAL, DefaultRootWindow(DADisplay), (long *) &desktop) != Success) desktop = 0; } + else if (desktop < 0) desktop = -1; else if (desktop-- > num_desktops) { - fprintf(stderr, "There are only %d desktops!\n", num_desktops); + fprintf(stderr, "There are only %ld desktops!\n", num_desktops); exit(111); } XSetErrorHandler(error_handler); setup_GCs(); + setup_pager(); DAShow(); DAEventLoop(); + + exit(0); }