From 79e15de349c1b82780d0583cd6fdcec4db2382ca Mon Sep 17 00:00:00 2001 From: BossCode45 Date: Wed, 24 Aug 2022 17:17:46 +1200 Subject: Added workspaces --- #config.h# | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ config.h | 30 +++++++++++++++++++++++------ main.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++-------------- structs.h | 1 + 4 files changed, 137 insertions(+), 20 deletions(-) create mode 100644 #config.h# diff --git a/#config.h# b/#config.h# new file mode 100644 index 0000000..26e8004 --- /dev/null +++ b/#config.h# @@ -0,0 +1,62 @@ +#include +#include + +#include +#include + +//Startup +std::string startup[] = {"picom -fD 3", "feh --bg-scale /usr/share/backgrounds/vapor_trails_blue.png"}; + +//Main config +int gaps = 10; +int outerGaps = 30; + +//Keys +//The types and perhaps functions likely to be moved to seperate header file later +typedef union +{ + const int num; + const char** str; +} KeyArg; + +struct Key +{ + KeySym keysym; + unsigned int modifiers; + void (*function)(const KeyArg arg); + const KeyArg arg; +}; + +//Keybind commands +void exit(const KeyArg arg); +void spawn(const KeyArg arg); +void toggle(const KeyArg arg); +void kill(const KeyArg arg); +void changeWS(const KeyArg arg); + +const char* alacritty[] = {"alacritty", NULL}; +const char* rofi[] = {"rofi", "-i", "-show" "drun", NULL}; + +#define WSKEY(K, X) \ + {K, mod, changeWS, {.num = X - 1}}, + +unsigned int mod = Mod1Mask; + +static struct Key keyBinds[] = { + //Key //Modifiers //Func //Args + {XK_E, mod, exit, {NULL}}, + {XK_Return, mod, spawn, {.str = alacritty}}, + {XK_D, mod, spawn, {.str = rofi}}, + {XK_T, mod, toggle, {NULL}}, + {XK_Q, mod, kill, {NULL}}, + {XK_1, mod, changeWS, {NULL}, + {XK_2, mod, changeWS, {NULL}}, + {XK_3, mod, changeWS, {.num = 2}}, + {XK_4, mod, changeWS, {.num = 3}}, + {XK_5, mod, changeWS, {.num = 4}}, + //WSKEY(XK_1, 1) + //WSKEY(XK_2, 2) + //WSKEY(XK_3, 3) + //WSKEY(XK_4, 4) + //WSKEY(XK_5, 5) +}; diff --git a/config.h b/config.h index f7cd70e..8f9897d 100644 --- a/config.h +++ b/config.h @@ -1,6 +1,8 @@ #include +#include #include +#include //Startup std::string startup[] = {"picom -fD 3", "feh --bg-scale /usr/share/backgrounds/vapor_trails_blue.png"}; @@ -9,12 +11,14 @@ std::string startup[] = {"picom -fD 3", "feh --bg-scale /usr/share/backgrounds/v int gaps = 10; int outerGaps = 30; +int numWS = 5; + //Keys //The types and perhaps functions likely to be moved to seperate header file later typedef union { const char** str; - const int* num; + const int num; } KeyArg; struct Key @@ -30,17 +34,31 @@ void exit(const KeyArg arg); void spawn(const KeyArg arg); void toggle(const KeyArg arg); void kill(const KeyArg arg); +void changeWS(const KeyArg arg); const char* alacritty[] = {"alacritty", NULL}; const char* rofi[] = {"rofi", "-i", "-show" "drun", NULL}; +#define WSKEY(K, X) \ + {K, mod, changeWS, {.num = X - 1}}, + unsigned int mod = Mod1Mask; static struct Key keyBinds[] = { //Key //Modifiers //Func //Args - {XK_E, mod, exit, {NULL}}, - {XK_Return, mod, spawn, {alacritty}}, - {XK_D, mod, spawn, {rofi}}, - {XK_T, mod, toggle, {NULL}}, - {XK_Q, mod, kill, {NULL}} + {XK_e, mod, exit, {NULL}}, + {XK_Return, mod, spawn, {.str = alacritty}}, + {XK_d, mod, spawn, {.str = rofi}}, + {XK_t, mod, toggle, {NULL}}, + {XK_q, mod, kill, {NULL}}, + {XK_1, mod, changeWS, {.num = 1}}, + {XK_2, mod, changeWS, {.num = 2}}, + {XK_3, mod, changeWS, {.num = 3}}, + {XK_4, mod, changeWS, {.num = 4}}, + {XK_5, mod, changeWS, {.num = 5}}, + //WSKEY(XK_1, 1) + //WSKEY(XK_2, 2) + //WSKEY(XK_3, 3) + //WSKEY(XK_4, 4) + //WSKEY(XK_5, 5) }; diff --git a/main.cpp b/main.cpp index 1efabe5..2066993 100644 --- a/main.cpp +++ b/main.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -23,15 +24,16 @@ Window root; int sW, sH; TileDir nextDir = horizontal; - bool keepGoing = true; map clients; int currClientID = 0; map frames; -int currFrameID = 0; +int currFrameID = 1; map frameIDS; +int currWS = 1; + void keyPress(XKeyEvent e); void configureRequest(XConfigureRequestEvent e); void mapRequest(XMapRequestEvent e); @@ -40,6 +42,7 @@ void destroyNotify(XDestroyWindowEvent e); static int OnXError(Display* display, XErrorEvent* e); void tile(int frameID, int x, int y, int w, int h); +void untile(int frameID); //Keybind commands void exit(const KeyArg arg) @@ -89,11 +92,23 @@ void kill(const KeyArg arg) XKillClient(dpy, w); } } +void changeWS(const KeyArg arg) +{ + int prevWS = currWS; + currWS = arg.num; + + if(prevWS == currWS) + return; + + untile(prevWS); + tile(currWS, outerGaps, outerGaps, sW - outerGaps*2, sH - outerGaps*2); + XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); +} void keyPress(XKeyEvent e) { if(e.same_screen!=1) return; - KeySym keysym = XLookupKeysym(&e, 1); + KeySym keysym = XLookupKeysym(&e, 0); for(int i = 0; i < sizeof(keyBinds)/sizeof(keyBinds[0]); i++) { if(keyBinds[i].keysym == keysym && keyBinds[i].modifiers == e.state) @@ -133,9 +148,9 @@ void mapRequest(XMapRequestEvent e) clients.insert(pair(c.ID, c)); //Make frame - int pID = (frameIDS.count(focusedWindow)>0)? frames.find(frameIDS.find(focusedWindow)->second)->second.pID : 0; + int pID = (frameIDS.count(focusedWindow)>0)? frames.find(frameIDS.find(focusedWindow)->second)->second.pID : currWS; vector v; - Frame f = {currFrameID, pID, true, c.ID, noDir, v}; + Frame f = {currFrameID, pID, true, c.ID, noDir, v, false}; currFrameID++; @@ -165,7 +180,7 @@ void mapRequest(XMapRequestEvent e) vector v; v.push_back(frames.find(frameIDS.find(focusedWindow)->second)->second.ID); v.push_back(f.ID); - Frame pF = {currFrameID, pID, false, noID, nextDir, v}; + Frame pF = {currFrameID, pID, false, noID, nextDir, v, false}; //Update the IDS f.pID = currFrameID; @@ -181,7 +196,7 @@ void mapRequest(XMapRequestEvent e) //Add to frames map frames.insert(pair(f.ID, f)); - tile(0, outerGaps, outerGaps, sW - outerGaps*2, sH - outerGaps*2); + tile(currWS, outerGaps, outerGaps, sW - outerGaps*2, sH - outerGaps*2); } void destroyNotify(XDestroyWindowEvent e) @@ -201,7 +216,7 @@ void destroyNotify(XDestroyWindowEvent e) frames.erase(fID); frameIDS.erase(e.window); - if(pS.size() < 2 && pID != 0) + if(pS.size() < 2 && !frames.find(pID)->second.isRoot) { //Erase parent frame int lastChildID = frames.find(frames.find(pID)->second.subFrameIDs[0])->second.ID; @@ -221,7 +236,7 @@ void destroyNotify(XDestroyWindowEvent e) break; } } - tile(0, outerGaps, outerGaps, sW - outerGaps*2, sH - outerGaps*2); + tile(currWS, outerGaps, outerGaps, sW - outerGaps*2, sH - outerGaps*2); } static int OnXError(Display* display, XErrorEvent* e) @@ -267,6 +282,7 @@ void tile(int frameID, int x, int y, int w, int h) wH -= gaps * 2; Client c = clients.find(f.cID)->second; //printf("Arranging client with frame ID %i, client ID %i:\n\tx: %i, y: %i, w: %i, h: %i\n", fID, c.ID, wX, wY, wW, wH); + XMapWindow(dpy, c.w); XMoveWindow(dpy, c.w, wX, wY); XResizeWindow(dpy, c.w, @@ -278,6 +294,23 @@ void tile(int frameID, int x, int y, int w, int h) } } +void untile(int frameID) +{ + vector& subFrameIDs = frames.find(frameID)->second.subFrameIDs; + TileDir dir = frames.find(frameID)->second.dir; + for(int fID : subFrameIDs) + { + Frame f = frames.find(fID)->second; + if(!f.isClient) + { + untile(fID); + continue; + } + Client c = clients.find(f.cID)->second; + XUnmapWindow(dpy, c.w); + } +} + int main(int argc, char** argv) { dpy = XOpenDisplay(nullptr); @@ -288,17 +321,20 @@ int main(int argc, char** argv) sH = DisplayHeight(dpy, screenNum); XSetErrorHandler(OnXError); - XSelectInput(dpy, root, SubstructureRedirectMask | SubstructureNotifyMask); + XSelectInput(dpy, root, SubstructureRedirectMask | SubstructureNotifyMask | KeyPressMask); 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); } - vector v; - Frame rootFrame = {0, noID, false, noID, horizontal, v}; - currFrameID++; - frames.insert(pair(0, rootFrame)); + for(int i = 1; i < numWS + 1; i++) + { + vector v; + Frame rootFrame = {i, noID, false, noID, horizontal, v, true}; + frames.insert(pair(i, rootFrame)); + currFrameID++; + } for(int i = 0; i < sizeof(startup)/sizeof(startup[0]); i++) { diff --git a/structs.h b/structs.h index f4287bb..da02976 100644 --- a/structs.h +++ b/structs.h @@ -30,4 +30,5 @@ struct Frame //If it isn't a client TileDir dir; std::vector subFrameIDs; + bool isRoot; }; -- cgit v1.2.3