diff --git a/client.c b/client.c index 444be8b..8412f28 100644 --- a/client.c +++ b/client.c @@ -29,8 +29,6 @@ #include "client.h" XContext window_context; -//XContext frame_context; /* not used yet */ -//XContext decoration_context; /* not used yet */ void init_clients ( ) { window_context = XUniqueContext(); @@ -59,15 +57,6 @@ XGetWindowAttributes(display, w, &winattr); c->window = w; - c->width = winattr.width; - c->height = winattr.height; - c->x = winattr.x; - c->y = winattr.y; - if ( c->width < DisplayWidth(display, DefaultScreen(display)) || c->height < DisplayHeight(display, DefaultScreen(display)) ) { - c->maximized = 0; - } else { - c->maximized = 1; - } XSaveContext(display, w, window_context, (void*)c); diff --git a/client.h b/client.h index 5edddc9..fcf47f1 100644 --- a/client.h +++ b/client.h @@ -36,11 +36,6 @@ typedef struct _Client { Window window; - int width; - int height; - int x; - int y; - int maximized; } Client; void init_clients(); diff --git a/x-wm.c b/x-wm.c index 9084e71..013881f 100644 --- a/x-wm.c +++ b/x-wm.c @@ -39,19 +39,15 @@ int quit = 0; char* startscript = NULL; -void grab_keys(); -void grab_buttons(); void loop(); -void handle_motion(XMotionEvent* ev); void handle_crossing(XCrossingEvent* ev); -void handle_focus(XFocusChangeEvent* ev); void handle_create(XCreateWindowEvent* ev); void handle_destroy(XDestroyWindowEvent* ev); -void handle_buttonPress(XButtonEvent* ev); -void handle_keyPress(XKeyEvent* ev); void close_window(Window w); +void maximize_client(Client *c); int main ( int argc, char* argv[] ) { + Client *c; Window dw1, dw2; Window* existing_windows; @@ -118,9 +114,9 @@ //| PropertyChangeMask | EnterWindowMask //| LeaveWindowMask - | ButtonPressMask + //| ButtonPressMask //| ButtonReleaseMask - | KeyPressMask + //| KeyPressMask //| KeyReleaseMask //| FocusChangeMask //| PointerMotionMask @@ -138,10 +134,6 @@ wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False); wm_protocols = XInternAtom(display, "WM_PROTOCOLS", False); - grab_keys(); - - grab_buttons(); - init_clients(); XQueryTree(display, root, &dw1, &dw2, &existing_windows, &win_count); @@ -149,7 +141,8 @@ for ( i = 0; i < win_count; i++ ) { if ( existing_windows[i] != root ) { clientcounter++; - create_client(existing_windows[i]); + c = create_client(existing_windows[i]); + maximize_client(c); } } XFree(existing_windows); @@ -161,56 +154,12 @@ exit(0); } -void grab_buttons ( ) { - /* listen to mouse clicks into the root window */ - XGrabButton(display, Button1, 0, root, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None); - - /* listen to mouse clicks into the root window */ - XGrabButton(display, Button1, (Mod1Mask|ControlMask), root, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None); - - /* listen to scroll wheel + ALT */ - XGrabButton(display, Button4, Mod1Mask, root, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None); - XGrabButton(display, Button5, Mod1Mask, root, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None); - - /* listen to scroll wheel + ALT + Ctrl (+ Shift) */ - XGrabButton(display, Button4, (Mod1Mask|ControlMask), root, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None); - XGrabButton(display, Button5, (Mod1Mask|ControlMask), root, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None); - XGrabButton(display, Button4, (Mod1Mask|ControlMask|ShiftMask), root, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None); - XGrabButton(display, Button5, (Mod1Mask|ControlMask|ShiftMask), root, True, ButtonPressMask, GrabModeAsync, GrabModeAsync, None, None); -} - -void grab_keys ( ) { - /* listen to Alt+Tab in the root window */ - XGrabKey(display, XKeysymToKeycode(display, XStringToKeysym("Tab")), Mod1Mask, root, True, GrabModeAsync, GrabModeAsync); - /* listen to Alt+F4 in the root window */ - XGrabKey(display, XKeysymToKeycode(display, XStringToKeysym("F4")), Mod1Mask, root, True, GrabModeAsync, GrabModeAsync); - /* listen to Alt+F2 in the root window */ - XGrabKey(display, XKeysymToKeycode(display, XStringToKeysym("F2")), Mod1Mask, root, True, GrabModeAsync, GrabModeAsync); - /* listen to Alt+UP in the root window */ - XGrabKey(display, XKeysymToKeycode(display, XStringToKeysym("Up")), Mod1Mask, root, True, GrabModeAsync, GrabModeAsync); - /* listen to Alt+DOWN in the root window */ - XGrabKey(display, XKeysymToKeycode(display, XStringToKeysym("Down")), Mod1Mask, root, True, GrabModeAsync, GrabModeAsync); - /* listen to Alt+LEFT in the root window */ - XGrabKey(display, XKeysymToKeycode(display, XStringToKeysym("Left")), Mod1Mask, root, True, GrabModeAsync, GrabModeAsync); - /* listen to Alt+RIGHT in the root window */ - XGrabKey(display, XKeysymToKeycode(display, XStringToKeysym("Right")), Mod1Mask, root, True, GrabModeAsync, GrabModeAsync); -} - void loop ( ) { XEvent ev; while ( !quit ) { XNextEvent(display, &ev); switch ( ev.type ) { - case MotionNotify: - handle_motion(&(ev.xmotion)); - break; - case ButtonPress: - handle_buttonPress(&(ev.xbutton)); - break; - case KeyPress: - handle_keyPress(&(ev.xkey)); - break; case EnterNotify: handle_crossing(&(ev.xcrossing)); break; @@ -244,12 +193,15 @@ } void handle_create ( XCreateWindowEvent* ev ) { + Client *c; fprintf(stdout, "x-wm: received XCreateWindowEvent event\n"); - if ( find_client(ev->window) == NULL ) { - create_client(ev->window); + if ( NULL == (c = find_client(ev->window)) ) { + c = create_client(ev->window); } + + maximize_client(c); XAddToSaveSet(display, ev->window); XRaiseWindow(display, ev->window); @@ -259,11 +211,6 @@ fprintf(stderr, "x-wm: clientcounter: %d\n", clientcounter); } -void handle_focus ( XFocusChangeEvent* ev ) { - /* TODO */ - fprintf(stdout, "x-wm: received XFocusChangeEvent event\n"); -} - void handle_crossing ( XCrossingEvent* ev ) { fprintf(stdout, "x-wm: received XCrossingEvent event: state %d\n", ev->state); if ( ev->type == EnterNotify ) { @@ -279,222 +226,8 @@ } } -void handle_motion ( XMotionEvent* ev ) { - /* TODO */ - fprintf(stdout, "x-wm: received MotionNotify event: state %d\n", ev->state); -} - -void handle_buttonPress ( XButtonEvent* ev ) { - Window foo; - int bar,width,height; - XWindowAttributes winattr; - Client* c; - - fprintf(stdout, "x-wm: received ButtonPress event: button %d, state %d\n", ev->button, ev->state); - - while ( XCheckTypedEvent(display, ButtonPress, (XEvent*) ev) ) { } - - if ( XQueryPointer(display, root, &foo, &focus, &bar, &bar, &bar, &bar, &bar) == BadWindow ) { - fprintf(stderr, "x-wm: received BadWindow from XQueryPointer\n"); - } - - /* cycle through windows with scroll wheel + Alt */ - if ( ev->button == Button4 && ev->state == Mod1Mask ) { - if ( XCirculateSubwindowsUp(display, root) == BadWindow ) { - fprintf(stderr, "x-wm: error while cycling upwards through windows (BadWindow)\n"); - } else { - fprintf(stdout, "x-wm: cycle through windows\n"); - } - } - if ( ev->button == Button5 && ev->state == Mod1Mask ) { - if ( XCirculateSubwindowsDown(display, root) == BadWindow ) { - fprintf(stderr, "x-wm: error while cycling downwards through windows (BadWindow)\n"); - } else { - fprintf(stdout, "x-wm: cycle through windows\n"); - } - } - - if ( ev->button == Button1 ) { - switch ( ev->state ) { - case (Mod1Mask|ControlMask): - /* click into some window -> raise it */ - fprintf(stdout, "x-wm: raise window\n"); - XRaiseWindow(display, focus); - XSetInputFocus(display, focus, RevertToPointerRoot, CurrentTime); - //XSendEvent(display, focus, False, NoEventMask, (XEvent*)ev); - break; - case 0: - if ( focus == root || focus == None ) { - /* click into root window -> launch xterm */ - fprintf(stdout, "x-wm: launch xterm\n"); - if ( fork() == 0 ) { - execlp("xterm","xterm",0); - fprintf(stderr, "x-wm: failed to launch xterm\n"); - } - } - break; - } - } - - if ( focus == None ) { - return; - } - - /* resize focused window veticaly width scroll wheel + ALT + Ctrl */ - if ( (ev->button == Button4 || ev->button == Button5) && ev->state == (Mod1Mask|ControlMask) ) { - XGetWindowAttributes(display, focus, &winattr); - - width = winattr.width; - height = winattr.height; - - if ( ev->button == Button4 && height >= 10 ) { - XResizeWindow(display, focus, width, height-10); - - if ( (c = find_client(focus)) != NULL ) { - c->maximized = 0; - } - } - - if ( (c = find_client(focus)) == NULL || c->maximized == 0 ) { - if ( ev->button == Button5 && height+winattr.y <= DisplayHeight(display, screen)-10 ) { - XResizeWindow(display, focus, width, height+10); - } - } - } - - /* resize focused window horizontaly width scroll wheel + ALT + Ctrl + Shift */ - if ( (ev->button == Button4 || ev->button == Button5) && ev->state == (Mod1Mask|ControlMask|ShiftMask) ) { - XGetWindowAttributes(display, focus, &winattr); - - width = winattr.width; - height = winattr.height; - - if ( ev->button == Button4 && width >= 10) { - XResizeWindow(display, focus, width-10, height); - - if ( (c = find_client(focus)) != NULL ) { - c->maximized = 0; - } - } - - if ( (c = find_client(focus)) == NULL || c->maximized == 0 ) { - if ( ev->button == Button5 && width+winattr.x <= DisplayWidth(display, screen)-10) { - XResizeWindow(display, focus, width+10, height); - } - } - } -} - -void handle_keyPress ( XKeyEvent* ev ) { - Window foo; - int bar,x,y,width,height; - Client* c; - XWindowAttributes winattr; - - fprintf(stdout, "x-wm: received KeyPress event: keycode %d, state %d\n", ev->keycode, ev->state); - - if ( XQueryPointer(display, root, &foo, &focus, &bar, &bar, &bar, &bar, &bar) == BadWindow ) { - fprintf(stderr, "x-wm: received BadWindow from XQueryPointer\n"); - } - - if ( ev->keycode == XKeysymToKeycode(display, XStringToKeysym("Tab")) && ev->state == Mod1Mask ) { - /* cycle trough subwindows of root with Alt-Tab */ - if ( XCirculateSubwindowsUp(display, root) == BadWindow ) { - fprintf(stderr, "x-wm: error while cycling through windows (BadWindow)\n"); - } else { - fprintf(stdout, "x-wm: cycle through windows\n"); - } - } - - if ( focus == None ) { - return; - } - - if ( ev->keycode == XKeysymToKeycode(display, XStringToKeysym("F4")) && ev->state == Mod1Mask ) { - /* close window with Alt-F4 */ - fprintf(stdout, "x-wm: received close request for a window (Alt-F4)\n"); - close_window(focus); - } - - if ( ev->keycode == XKeysymToKeycode(display, XStringToKeysym("F2")) && ev->state == Mod1Mask ) { - /* maximize/restore window with Alt-F2 */ - - width = DisplayWidth(display, screen); - height = DisplayHeight(display, screen); - x = 0; - y = 0; - - c = find_client(focus); - - if ( c == NULL ) { - fprintf(stdout, "x-wm: found window without client struct\n"); - c = create_client(focus); - } - - if ( c->maximized ) { - fprintf(stdout, "x-wm: unmaximize window\n"); - c->maximized = 0; - x = c->x; - y = c->y; - width = c->width; - height = c->height; - } else { - XGetWindowAttributes(display, focus, &winattr); - fprintf(stdout, "x-wm: maximize window\n"); - - c->maximized = 1; - c->width = winattr.width; - c->height = winattr.height; - c->x = winattr.x; - c->y = winattr.y; - } - - XMoveResizeWindow(display, focus, x, y, width, height); - } - - if ( ev->keycode == XKeysymToKeycode(display, XStringToKeysym("Up")) && ev->state == Mod1Mask ) { - /* move window 'up' with Alt+UP */ - if ( (c = find_client(focus)) == NULL || c->maximized == 0 ) { - XGetWindowAttributes(display, focus, &winattr); - - if ( winattr.y >= 10 ) { - XMoveWindow(display, focus, winattr.x, winattr.y-10); - } - } - } - - if ( ev->keycode == XKeysymToKeycode(display, XStringToKeysym("Down")) && ev->state == Mod1Mask ) { - /* move window 'down' with Alt+Down */ - if ( (c = find_client(focus)) == NULL || c->maximized == 0 ) { - XGetWindowAttributes(display, focus, &winattr); - - if ( winattr.y <= DisplayHeight(display, screen) - winattr.height - 10 ) { - XMoveWindow(display, focus, winattr.x, winattr.y+10); - } - } - } - - if ( ev->keycode == XKeysymToKeycode(display, XStringToKeysym("Left")) && ev->state == Mod1Mask ) { - /* move window 'left' with Alt+Left */ - if ( (c = find_client(focus)) == NULL || c->maximized == 0 ) { - XGetWindowAttributes(display, focus, &winattr); - - if ( winattr.x >= 10 ) { - XMoveWindow(display, focus, winattr.x-10, winattr.y); - } - } - } - - if ( ev->keycode == XKeysymToKeycode(display, XStringToKeysym("Right")) && ev->state == Mod1Mask ) { - /* move window 'right' with Alt+Right */ - if ( (c = find_client(focus)) == NULL || c->maximized == 0 ) { - XGetWindowAttributes(display, focus, &winattr); - - if ( winattr.x <= DisplayWidth(display, screen) - winattr.width - 10 ) { - XMoveWindow(display, focus, winattr.x+10, winattr.y); - } - } - } +void maximize_client ( Client *c ) { + XMoveResizeWindow(display, c->window, 0, 0, DisplayWidth(display, screen), DisplayHeight(display, screen)); } void close_window ( Window w ) { diff --git a/x-wm.h b/x-wm.h index 6a708dc..a4eed01 100644 --- a/x-wm.h +++ b/x-wm.h @@ -45,9 +45,11 @@ "usage: x-wm\n" " Look at the source code for further information.\n" "\n" - "also: x-wm -v show version\n" - " x-wm -h display this help\n" - " x-wm -l display the (BSD) license\n" + "options:\n" + " -s startscript run startscript on startup\n" + "also: -v show version\n" + " -h display this help\n" + " -l display the (BSD) license\n" ; const char versionmsg[] = "x-wm 0.0 ($Revision$)\n";