From 3baf563d0a0ea1011f83e34062a5af0847b55fa9 Mon Sep 17 00:00:00 2001 From: hippoz Date: Sat, 31 Oct 2020 20:14:15 +0200 Subject: [PATCH] finish tinywm and change directory structure --- .gitignore | 2 +- Makefile | 13 ++++--------- src/main.c | 34 ---------------------------------- wm.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 44 deletions(-) delete mode 100644 src/main.c create mode 100644 wm.c diff --git a/.gitignore b/.gitignore index efa6632..14db6aa 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -bin/* \ No newline at end of file +wm \ No newline at end of file diff --git a/Makefile b/Makefile index b77a35b..6b79d0b 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,10 @@ -SRC_DIR=src -BIN_DIR=bin -WM_SOURCES_MAIN=$(SRC_DIR)/main.c -WM_SOURCES=$(WM_SOURCES_MAIN) - PREFIX?=/usr/X11R6 CFLAGS?=-Os -pedantic -Wall -all: $(WM_SOURCES) wm +all: wm -wm: $(WM_SOURCES_MAIN) - $(CC) $(CFLAGS) -I$(PREFIX)/include $< -L$(PREFIX)/lib -lX11 -o $(BIN_DIR)/wm +wm: wm.c + $(CC) $(CFLAGS) -I$(PREFIX)/include wm.c -L$(PREFIX)/lib -lX11 -o wm clean: - rm $(BIN_DIR)/* \ No newline at end of file + rm -f wm \ No newline at end of file diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 04b2104..0000000 --- a/src/main.c +++ /dev/null @@ -1,34 +0,0 @@ -// Some things taken from https://github.com/mackstann/tinywm - -#include - -#define MODMASK Mod1Mask - -int main() { - Display* display; - XWindowAttributes attr; - - // Used to store the state of the cursor when resizing - XButtonEvent start; - - XEvent ev; - - bool isRunning = true; - - - if(!(dpy = XOpenDisplay(0x0))) return 1; - - XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("F1")), MODMASK, DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync); - XGrabButton(dpy, 1, MODMASK, DefaultRootWindow(dpy), True, ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync, GrabModeAsync, None, None); - XGrabButton(dpy, 3, MODMASK, DefaultRootWindow(dpy), True, ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync, GrabModeAsync, None, None); - - start.subwindow = None; - - while(isRunning) { - XNextEvent(dpy, &ev); - - - } - - return 0; -} \ No newline at end of file diff --git a/wm.c b/wm.c new file mode 100644 index 0000000..9fa9375 --- /dev/null +++ b/wm.c @@ -0,0 +1,48 @@ +// Taken from https://github.com/mackstann/tinywm + +#include + +#define MODMASK Mod1Mask + +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +int main() { + Display* dpy; + XWindowAttributes attr; + + // Used to store the state of the cursor when resizing + XButtonEvent start; + + XEvent ev; + + if(!(dpy = XOpenDisplay(0x0))) return 1; + + XGrabKey(dpy, XKeysymToKeycode(dpy, XStringToKeysym("F1")), MODMASK, DefaultRootWindow(dpy), True, GrabModeAsync, GrabModeAsync); + XGrabButton(dpy, 1, MODMASK, DefaultRootWindow(dpy), True, ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync, GrabModeAsync, None, None); + XGrabButton(dpy, 3, MODMASK, DefaultRootWindow(dpy), True, ButtonPressMask|ButtonReleaseMask|PointerMotionMask, GrabModeAsync, GrabModeAsync, None, None); + + start.subwindow = None; + + for (;;) { + XNextEvent(dpy, &ev); + + if (ev.type == KeyPress && ev.xkey.subwindow != None) { + XRaiseWindow(dpy, ev.xkey.subwindow); + } else if (ev.type == ButtonPress && ev.xbutton.subwindow != None) { + XGetWindowAttributes(dpy, ev.xbutton.subwindow, &attr); + start = ev.xbutton; + } else if (ev.type == MotionNotify && start.subwindow != None) { + int xdiff = ev.xbutton.x_root - start.x_root; + int ydiff = ev.xbutton.y_root - start.y_root; + XMoveResizeWindow(dpy, start.subwindow, + attr.x + (start.button==1 ? xdiff : 0), + attr.y + (start.button==1 ? ydiff : 0), + MAX(1, attr.width + (start.button==3 ? xdiff : 0)), + MAX(1, attr.height + (start.button==3 ? ydiff : 0))); + } else if (ev.type == ButtonRelease) { + start.subwindow = None; + } + } + + return 0; +} \ No newline at end of file