diff options
| author | BossCode45 <human.cyborg42@gmail.com> | 2022-12-14 20:14:19 +1300 |
|---|---|---|
| committer | BossCode45 <human.cyborg42@gmail.com> | 2022-12-14 20:14:19 +1300 |
| commit | e7bfc85d90f92a896636f1bb9cbab1de1a29b8f2 (patch) | |
| tree | c76d43a0f80181c7724255ac6ae15566603173f8 | |
| parent | b1e0a2d87e826bec6d1270dde9063c777d90d520 (diff) | |
| parent | 7ec43dd82b8507e1cc4d1b8b1ab1af2298c77c84 (diff) | |
| download | YATwm-e7bfc85d90f92a896636f1bb9cbab1de1a29b8f2.tar.gz YATwm-e7bfc85d90f92a896636f1bb9cbab1de1a29b8f2.zip | |
Merge branch 'multi-monitor'
| -rw-r--r-- | config.h | 68 | ||||
| -rw-r--r-- | ewmh.cpp | 2 | ||||
| -rw-r--r-- | ewmh.h | 2 | ||||
| -rw-r--r-- | logs.txt | 19 | ||||
| -rw-r--r-- | main.cpp | 259 | ||||
| -rw-r--r-- | makefile | 2 | ||||
| -rw-r--r-- | notes.org | 26 | ||||
| -rw-r--r-- | structs.h | 9 | ||||
| -rw-r--r-- | test | 5 |
9 files changed, 333 insertions, 59 deletions
@@ -7,16 +7,31 @@ #include <string> //Startup -std::string startup[] = {"picom -fD 3", "feh --bg-scale /usr/share/backgrounds/vapor_trails_blue.png", "~/.config/polybar/launch.sh", "emacs --daemon"}; +const std::string startup[] = { + //"picom -fD 3", + "feh --bg-scale /usr/share/backgrounds/vapor_trails_blue.png", + //"~/.config/polybar/launch.sh", + //"emacs --daemon" +}; //Main config -int gaps = 3; -int outerGaps = 3; -std::string logFile = "/tmp/yatlog"; +// Sensible gaps +const int gaps = 3; +const int outerGaps = 3; +// Huge gaps +/* +const int gaps = 20; +const int outerGaps = 30; +*/ + +const std::string logFile = "/tmp/yatlog.txt"; //WS config const int numWS = 10; -std::string workspaceNames[] = {"1: ", "2: 拾", "3: ", "4: ", "5: ", "6: ", "7: 拾", "8: ", "9: ", "10: "}; +const std::string workspaceNames[] = {"1: ", "2: 拾", "3: ", "4: ", "5: ", "6: ", "7: 拾", "8: ", "9: ", "10: "}; +//If you have more then 2 monitors change the number below +const int maxMonitors = 2; +const int screenPreferences[][maxMonitors] = {{0}, {0}, {0}, {0}, {0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}}; //Keys //The types and perhaps functions likely to be moved to seperate header file later @@ -37,8 +52,8 @@ typedef union struct Key { - unsigned int modifiers; - KeySym keysym; + const unsigned int modifiers; + const KeySym keysym; void (*function)(const KeyArg arg); const KeyArg arg; }; @@ -54,27 +69,37 @@ KEYCOM(changeWS); KEYCOM(wToWS); KEYCOM(focChange); KEYCOM(wMove); +KEYCOM(bashSpawn); +KEYCOM(reload); +KEYCOM(wsDump); +KEYCOM(nextMonitor); + +// Super key mod +#define MOD Mod4Mask +#define ALT Mod1Mask +// Alt key mod +// #define MOD Mod1Mask +#define SHIFT ShiftMask +// Programs to run for keybinds const char* alacritty[] = {"alacritty", NULL}; const char* rofi[] = {"rofi", "-i", "-show", "drun", NULL}; const char* qutebrowser[] = {"qutebrowser", NULL}; - const char* i3lock[] = {"i3lock", "-eti", "/usr/share/backgrounds/lockscreen.png", NULL}; const char* suspend[] = {"systemctl", "suspend", NULL}; -//Super key mod -//#define MOD Mod4Mask -//Alt key mod -#define MOD Mod1Mask -#define SHIFT ShiftMask +// Scripts to run for keybinds +// Script I made to run an xrandr command +const char* monConfig[] = {"~/.yat_commands/monitor-config.sh"}; + #define WSKEY(K, X) \ {MOD, K, changeWS, {.num = X}}, \ {MOD|SHIFT, K, wToWS, {.num = X}} -static Key keyBinds[] = { - //Modifiers //Key //Func //Args - //General +const Key keyBinds[] = { + // Modifiers //Key //Func //Args + // General {MOD, XK_e, exit, {NULL}}, {MOD, XK_Return, spawn, {.str = alacritty}}, {MOD, XK_d, spawn, {.str = rofi}}, @@ -84,17 +109,22 @@ static Key keyBinds[] = { {MOD, XK_x, spawn, {.str = i3lock}}, {MOD|SHIFT, XK_x, spawn, {.str = i3lock}}, {MOD|SHIFT, XK_x, spawn, {.str = suspend}}, - //Focus + {MOD, XK_m, bashSpawn, {.str = monConfig}}, + {MOD|SHIFT, XK_r, reload, {NULL}}, + // Testing + {MOD, XK_p, wsDump, {NULL}}, + // Focus {MOD, XK_h, focChange, {.dir = Left}}, {MOD, XK_j, focChange, {.dir = Down}}, {MOD, XK_k, focChange, {.dir = Up}}, {MOD, XK_l, focChange, {.dir = Right}}, - //Window moving + {ALT, XK_Tab, nextMonitor, {NULL}}, + // Window moving {MOD|SHIFT, XK_h, wMove, {.dir = Left}}, {MOD|SHIFT, XK_j, wMove, {.dir = Down}}, {MOD|SHIFT, XK_k, wMove, {.dir = Up}}, {MOD|SHIFT, XK_l, wMove, {.dir = Right}}, - //Workspaces + // Workspaces WSKEY(XK_1, 1), WSKEY(XK_2, 2), WSKEY(XK_3, 3), @@ -5,7 +5,7 @@ Display** dpy_; Window* root_; -void initEWMH(Display** dpy, Window* root, int numWS, std::string workspaceNames[]) +void initEWMH(Display** dpy, Window* root, int numWS, const std::string workspaceNames[]) { dpy_ = dpy; root_ = root; @@ -8,7 +8,7 @@ #include "structs.h" -void initEWMH(Display** dpy, Window* root, int numWS, std::string workspaceNames[]); +void initEWMH(Display** dpy, Window* root, int numWS, const std::string workspaceNames[]); void updateClientList(std::map<int, Client> clients); diff --git a/logs.txt b/logs.txt deleted file mode 100644 index ff03f10..0000000 --- a/logs.txt +++ /dev/null @@ -1,19 +0,0 @@ ---- -Bars launched... -Successfully wrote command 'quit' to PID 196086 ---- -Bars launched... -Successfully wrote command 'quit' to PID 204881 ---- -Bars launched... -Successfully wrote command 'quit' to PID 207764 ---- -Bars launched... ---- -Bars launched... ---- -Bars launched... -Successfully wrote command 'quit' to PID 215833 -Successfully wrote command 'quit' to PID 215841 ---- -Bars launched... @@ -1,6 +1,7 @@ #include <X11/X.h> #include <X11/Xlib.h> #include <X11/Xatom.h> +#include <X11/cursorfont.h> #include <X11/Xutil.h> #include <X11/extensions/Xrandr.h> @@ -49,8 +50,11 @@ map<int, Frame> frames; int currFrameID = 1; map<Window, int> frameIDS; +ScreenInfo* screens; int* focusedWorkspaces; -int focusedScreen; +int focusedScreen = 0; +int nscreens; +int mX, mY; #define getClient(c) clients.find(c)->second #define getFrame(f) frames.find(f)->second @@ -62,15 +66,21 @@ int currWS = 1; // Usefull functions int FFCF(int sID); +void detectScreens(); +void updateMousePos(); +void focusRoot(int root); void keyPress(XKeyEvent e); void configureRequest(XConfigureRequestEvent e); void mapRequest(XMapRequestEvent e); void destroyNotify(XDestroyWindowEvent e); +void enterNotify(XEnterWindowEvent e); void clientMessage(XClientMessageEvent e); static int OnXError(Display* display, XErrorEvent* e); +void tileRoots(); +void untileRoots(); void tile(int frameID, int x, int y, int w, int h); void untile(int frameID); @@ -81,6 +91,64 @@ int FFCF(int sID) return sID; return FFCF(frames.find(sID)->second.subFrameIDs[0]); } +void detectScreens() +{ + delete[] screens; + delete[] focusedWorkspaces; + log("Detecting screens: "); + XRRMonitorInfo* monitors = XRRGetMonitors(dpy, root, true, &nscreens); + log("\t" << nscreens << " monitors"); + screens = new ScreenInfo[nscreens]; + focusedWorkspaces = new int[nscreens]; + for(int i = 0; i < nscreens; i++) + { + focusedWorkspaces[i] = i * 5 + 1; + char* name = XGetAtomName(dpy, monitors[i].name); + screens[i] = {name, monitors[i].x, monitors[i].y, monitors[i].width, monitors[i].height}; + log("\tMonitor " << i + 1 << " - " << screens[i].name); + log("\t\tx: " << screens[i].x << ", y: " << screens[i].y); + log("\t\tw: " << screens[i].w << ", h: " << screens[i].h); + XFree(name); + } + for(int i = 0; i < numWS; i++) + { + if(screenPreferences[i][0] < nscreens && focusedWorkspaces[screenPreferences[i][0]] == 0) + { + //focusedWorkspaces[screenPreferences[i][0]] = i+1; + } + } + XFree(monitors); +} +void updateMousePos() +{ + Window rootRet, childRet; + int rX, rY, cX, cY; + unsigned int maskRet; + XQueryPointer(dpy, root, &rootRet, &childRet, &rX, &rY, &cX, &cY, &maskRet); + mX = rX; + mY = rY; +} +int getClientChild(int fID) +{ + if(getFrame(fID).isClient) + return fID; + else + return getClientChild(getFrame(fID).subFrameIDs[0]); +} +void focusRoot(int root) +{ + //log("Focusing root: " << root); + if(getFrame(root).subFrameIDs.size() == 0) + { + //log("\tRoot has no children"); + XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); + return; + } + int client = getFrame(getClientChild(root)).cID; + Window w = getClient(client).w; + //log("\tFocusing window: " << w); + XSetInputFocus(dpy, w, RevertToPointerRoot, CurrentTime); +} //Keybind commands void exit(const KeyArg arg) @@ -140,9 +208,41 @@ void changeWS(const KeyArg arg) currWS = arg.num; if(prevWS == currWS) return; + untileRoots(); + + //log("Changing WS with keybind"); - untile(prevWS); - tile(currWS, outerGaps, outerGaps, sW - outerGaps*2, sH - outerGaps*2 - bH); + for(int i = 0; i < maxMonitors; i++) + { + if(nscreens > screenPreferences[arg.num - 1][i]) + { + int screen = screenPreferences[arg.num - 1][i]; + //log("Found screen (screen " << screenPreferences[arg.num - 1][i] << ")"); + prevWS = focusedWorkspaces[screenPreferences[arg.num - 1][i]]; + //log("Changed prevWS"); + focusedWorkspaces[screenPreferences[arg.num - 1][i]] = arg.num; + //log("Changed focusedWorkspaces"); + if(focusedScreen != screenPreferences[arg.num - 1][i]) + { + focusedScreen = screenPreferences[arg.num - 1][i]; + XWarpPointer(dpy, root, root, 0, 0, 0, 0, screens[screen].x + screens[screen].w/2, screens[screen].y + screens[screen].h/2); + } + //log("Changed focusedScreen"); + break; + } + } + + //log("Finished changes"); + + //log(prevWS); + if(prevWS < 1 || prevWS > numWS) + { + //untile(prevWS); + } + //log("Untiled"); + //tile(currWS, outerGaps, outerGaps, sW - outerGaps*2, sH - outerGaps*2 - bH); + tileRoots(); + //log("Roots tiled"); XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); //EWMH @@ -196,7 +296,9 @@ void wToWS(const KeyArg arg) setWindowDesktop(focusedWindow, arg.num); XUnmapWindow(dpy, focusedWindow); - tile(currWS, outerGaps, outerGaps, sW - outerGaps*2, sH - outerGaps*2 - bH); + //tile(currWS, outerGaps, outerGaps, sW - outerGaps*2, sH - outerGaps*2 - bH); + untileRoots(); + tileRoots(); XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); } int dirFind(int fID, MoveDir dir) @@ -332,7 +434,9 @@ void wMove(const KeyArg arg) std::swap(pSF[i], pSF[swapPos]); } - tile(currWS, outerGaps, outerGaps, sW - outerGaps*2, sH - outerGaps*2 - bH); + untileRoots(); + tileRoots(); + //tile(currWS, outerGaps, outerGaps, sW - outerGaps*2, sH - outerGaps*2 - bH); XSetInputFocus(dpy, focusedWindow, RevertToPointerRoot, CurrentTime); return; } @@ -342,7 +446,7 @@ void bashSpawn(const KeyArg arg) { if(fork() == 0) { - int null = open("log.txt", O_WRONLY); + int null = open("/dev/null", O_WRONLY); dup2(null, 1); dup2(null, 2); system(arg.str[0]); @@ -350,10 +454,40 @@ void bashSpawn(const KeyArg arg) } } +void reload(const KeyArg arg) +{ + detectScreens(); +} +void wsDump(const KeyArg arg) +{ + log("Workspace dump:"); + for(int i = 1; i < currFrameID; i++) + { + if(getFrame(i).isClient) + { + int id = i; + while(!getFrame(id).isRoot) + { + id=getFrame(id).pID; + } + log("\tClient with ID: " << getClient(getFrame(i).cID).w << ", on worskapce " << id); + } + } +} +void nextMonitor(const KeyArg arg) +{ + focusedScreen++; + if(focusedScreen >= nscreens) + focusedScreen = 0; + + XWarpPointer(dpy, root, root, 0, 0, 0, 0, screens[focusedScreen].x + screens[focusedScreen].w/2, screens[focusedScreen].y + screens[focusedScreen].h/2); + focusRoot(focusedWorkspaces[focusedScreen]); +} void keyPress(XKeyEvent e) { if(e.same_screen!=1) return; + updateMousePos(); KeySym keysym = XLookupKeysym(&e, 0); for(int i = 0; i < sizeof(keyBinds)/sizeof(keyBinds[0]); i++) { @@ -390,6 +524,53 @@ void mapRequest(XMapRequestEvent e) Window focusedWindow; int revertToReturn; + int pID; + XGetInputFocus(dpy, &focusedWindow, &revertToReturn); + if(focusedWindow && focusedWindow != root && frameIDS.count(focusedWindow)>0) + { + //Use focused to determine parent + pID = frames.find(frameIDS.find(focusedWindow)->second)->second.pID; + } + else + { + Window rootRet, childRet; + int rX, rY, cX, cY; + unsigned int maskRet; + XQueryPointer(dpy, root, &rootRet, &childRet, &rX, &rY, &cX, &cY, &maskRet); + mX = rX; + mY = rY; + int monitor = 0; + for(int i = 0; i < nscreens; i++) + { + if(screens[i].x <= mX && mX < screens[i].x + screens[i].w) + { + if(screens[i].y <= mY && mY < screens[i].y + screens[i].h) + { + monitor = i; + } + } + } + pID = focusedWorkspaces[monitor]; + focusedScreen = monitor; + /* + if(mX == rX && mY == rY) + { + //Use focused screen + log("\tFocused screen is: " << focusedScreen); + } + else + { + //Use mouse + //TODO: Make this find the monitor + log("\tMouse is at x: " << rX << ", y: " << rY); + mX = rX; + mY = rY; + } + */ + } + + Window focusedWindow; + int revertToReturn; XGetInputFocus(dpy, &focusedWindow, &revertToReturn); unsigned char* data; @@ -418,7 +599,7 @@ void mapRequest(XMapRequestEvent e) clients.insert(pair<int, Client>(c.ID, c)); //Make frame - int pID = (frameIDS.count(focusedWindow)>0)? frames.find(frameIDS.find(focusedWindow)->second)->second.pID : currWS; + //pID = (frameIDS.count(focusedWindow)>0)? frames.find(frameIDS.find(focusedWindow)->second)->second.pID : currWS; vector<int> v; vector<int> floating; Frame f = {currFrameID, pID, true, c.ID, noDir, v, false, floating}; @@ -438,7 +619,8 @@ void mapRequest(XMapRequestEvent e) setWindowDesktop(e.window, currWS); updateClientList(clients); XFree(data); - tile(currWS, outerGaps, outerGaps, sW - outerGaps*2, sH - outerGaps*2 - bH); + //tile(currWS, outerGaps, outerGaps, sW - outerGaps*2, sH - outerGaps*2 - bH); + tileRoots(); return; } XFree(data); @@ -485,7 +667,8 @@ void mapRequest(XMapRequestEvent e) setWindowDesktop(e.window, currWS); updateClientList(clients); - tile(currWS, outerGaps, outerGaps, sW - outerGaps*2, sH - outerGaps*2 - bH); + //tile(currWS, outerGaps, outerGaps, sW - outerGaps*2, sH - outerGaps*2 - bH); + tileRoots(); } void destroyNotify(XDestroyWindowEvent e) @@ -531,16 +714,42 @@ void destroyNotify(XDestroyWindowEvent e) } } XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); - tile(currWS, outerGaps, outerGaps, sW - outerGaps*2, sH - outerGaps*2 - bH); + //tile(currWS, outerGaps, outerGaps, sW - outerGaps*2, sH - outerGaps*2 - bH); + tileRoots(); updateClientList(clients); } +void enterNotify(XEnterWindowEvent e) +{ + //log(e.xcrossing.x); + /* Cancel if crossing into root + if(e.xcrossing.window == root) + break; + */ + XWindowAttributes attr; + XGetWindowAttributes(dpy, e.window, &attr); + int monitor = 0; + for(int i = 0; i < nscreens; i++) + { + if(screens[i].x <= attr.x && attr.x < screens[i].x + screens[i].w) + { + if(screens[i].y <= attr.y && attr.y < screens[i].y + screens[i].h) + { + monitor = i; + } + } + } + focusedScreen = monitor; + XSetInputFocus(dpy, e.window, RevertToNone, CurrentTime); +} void clientMessage(XClientMessageEvent e) { char* name = XGetAtomName(dpy, e.message_type); log("Client message: " << name); if(e.message_type == XInternAtom(dpy, "_NET_CURRENT_DESKTOP", false)) { + changeWS({.num = (int)((long)e.data.l[0] + 1)}); + /* //Change desktop int nextWS = (long)e.data.l[0] + 1; int prevWS = currWS; @@ -555,6 +764,7 @@ void clientMessage(XClientMessageEvent e) //EWMH setCurrentDesktop(currWS); + */ } XFree(name); } @@ -565,6 +775,20 @@ static int OnXError(Display* display, XErrorEvent* e) return 0; } +void tileRoots() +{ + for(int i = 0; i < nscreens; i++) + { + tile(focusedWorkspaces[i], screens[i].x + outerGaps, screens[i].y + outerGaps, screens[i].w - outerGaps*2, screens[i].h - outerGaps*2 - bH); + } +} +void untileRoots() +{ + for(int i = 0; i < nscreens; i++) + { + untile(focusedWorkspaces[i]); + } +} void tile(int frameID, int x, int y, int w, int h) { for(int fID : frames.find(frameID)->second.floatingFrameIDs) @@ -630,6 +854,7 @@ void untile(int frameID) int main(int argc, char** argv) { + mX = mY = 0; dpy = XOpenDisplay(nullptr); root = Window(DefaultRootWindow(dpy)); @@ -639,20 +864,23 @@ int main(int argc, char** argv) std::time_t time = std::chrono::system_clock::to_time_t(timeUnformatted); log("\nYAT STARTING: " << std::ctime(&time) << "--------------------------------------"); + screens = new ScreenInfo[1]; focusedWorkspaces = new int[1]; + detectScreens(); int screenNum = DefaultScreen(dpy); sW = DisplayWidth(dpy, screenNum); sH = DisplayHeight(dpy, screenNum); XSetErrorHandler(OnXError); - XSelectInput(dpy, root, SubstructureRedirectMask | SubstructureNotifyMask | KeyPressMask); + XSelectInput(dpy, root, SubstructureRedirectMask | SubstructureNotifyMask | KeyPressMask | EnterWindowMask); for(int i = 0; i < sizeof(keyBinds)/sizeof(keyBinds[0]); i++) { XGrabKey(dpy, XKeysymToKeycode(dpy, keyBinds[i].keysym), keyBinds[i].modifiers, root, false, GrabModeAsync, GrabModeAsync); + //log("Grabbing " << XKeysymToString(keyBinds[i].keysym)); } - + XDefineCursor(dpy, root, XCreateFontCursor(dpy, XC_top_left_arrow)); //EWMH initEWMH(&dpy, &root, numWS, workspaceNames); setCurrentDesktop(1); @@ -674,6 +902,7 @@ int main(int argc, char** argv) } XSetInputFocus(dpy, root, RevertToNone, CurrentTime); + XWarpPointer(dpy, root, root, 0, 0, 0, 0, 960, 540); cout << "Begin mainloop\n"; while(keepGoing) @@ -694,11 +923,9 @@ int main(int argc, char** argv) break; case DestroyNotify: destroyNotify(e.xdestroywindow); + break; case EnterNotify: - log(e.xcrossing.x); - if(e.xcrossing.window == root) - break; - XSetInputFocus(dpy, e.xcrossing.window, RevertToNone, CurrentTime); + enterNotify(e.xcrossing); break; case ClientMessage: clientMessage(e.xclient); @@ -1,7 +1,7 @@ .PHONY: clean CXX := g++ CXXFLAGS := #-g -fsanitize=address -fno-omit-frame-pointer -LINKFLAGS := -lX11 +LINKFLAGS := -lX11 -lXrandr OBJS_DIR := . OUT_DIR := . SOURCE_DIR := . diff --git a/notes.org b/notes.org new file mode 100644 index 0000000..a3b6bf1 --- /dev/null +++ b/notes.org @@ -0,0 +1,26 @@ +#+TITLE: Features to add + +* Remembering focus +Use the variable ~whichChildFocused~ for each non client frame, this is the index of the child that is focused. To then find focused child for a root recurse through all the frames going through the focused child. +** Where it should be updated ++ When focus is moved ++ When a client is moved (because it should retain focus) ++ When a client is deleted (if it had focus before) +** Where it should be used +Whenever I set focus I should use the focused client of whichever workspace is focused + +* Change focus and move between monitors +Perhaps I can just add something to the ~dirFind~ function + +* TOML config +put in ~/.config/YATwm/config.toml + +* Fullscreen windows +This should be another thing like floating windows except each workspace only gets one +I should check it first when tiling and if it exists then draw it to the size of the screeen then return + +* General EWMH things +I should really figure out what all the client messages mean + +* The eventual program to communicate with YATwm +Use a unix socket @@ -2,6 +2,8 @@ #include <X11/Xlib.h> +#include <X11/extensions/Xrandr.h> +#include <string> #include <vector> #define noID -1 @@ -35,4 +37,11 @@ struct Frame std::vector<int> subFrameIDs; bool isRoot; std::vector<int> floatingFrameIDs; + //int whichChildFocused = 0; +}; + +struct ScreenInfo +{ + std::string name; + int x, y, w, h; }; @@ -2,10 +2,11 @@ make -Xephyr :100 -screen 1600x900 & +#Xephyr +extension RANDR +xinerama -screen 1600x900 -screen 1600x900 -ac :1 & +Xephyr -screen 1600x900+2080+90 :1 & sleep 0.1 -DISPLAY=:100 ./YATwm +DISPLAY=:1 ./YATwm pkill Xephyr |
