: nply/dwhkd

commit a6005d8cf36f3bcd19597d9ec64d9e3b790c5047
parent 9cbc999c47c661bd3d43478429d16009e6fe6f09
Author: theSoberSobber <akshatshrivastava0010@gmail.com>
Date: Wed, 20 Dec 2023 01:32:36 +0530

added autofocus on switches for VD Action
3 files changed, 56 insertions(+), 4 deletions(-)
M
VD/VD.c
|
30
+++++++++++++++++++++++++++---
M
config.h
|
2
+-
M
main.c
|
28
++++++++++++++++++++++++++++
diff --git a/VD/VD.c b/VD/VD.c
@@ -1,10 +1,34 @@
+HWND topmost = NULL;
+
+BOOL CALLBACK CurrVDTopMostFinder(HWND hwnd, LPARAM lParam) {
+    char windowTitle[256];
+    GetWindowTextA(hwnd, windowTitle, sizeof(windowTitle));
+    if (IsWindowVisible(hwnd) && IsWindowOnCurrentVirtualDesktopFunc(hwnd) && windowTitle[0] != '\0') {
+        printf("windowTitle: %s %d\n", windowTitle);
+        topmost = hwnd;
+        return FALSE;
+    }
+    return TRUE;
+}
+
+HWND GetTopmostWindowOnCurrentVD(){
+    topmost=NULL;
+    EnumWindows(CurrVDTopMostFinder, 0);
+    return topmost;
+}
+
 void MoveWindowToDesktopNumberW(const Arg* arg){
-    printf("%d", arg->i - 1);
+    printf("%d\n", arg->i - 1);
     HWND fgwhwnd = (HWND)GetForegroundWindow();
-    if(GetCurrentDesktopFunc()==GetWindowDesktopNumberFunc(fgwhwnd)) MoveWindowToDesktopNumberFunc(fgwhwnd, arg->i - 1);
-    SetForegroundWindow(NULL);
+    MoveWindowToDesktopNumberFunc(fgwhwnd, arg->i - 1);
+    lastActive[arg->i - 1] = fgwhwnd;
+    // now who to focus on this desktop? focus NEEDS to shifted right now otherwise movewindow 
+    // can be called again with the same foreground window that isn't even ahead right now
+    SetForegroundWindow(GetTopmostWindowOnCurrentVD());
 }
 
 void GoToDesktopNumberW(const Arg* arg){
+    lastActive[GetCurrentDesktopFunc()] = GetForegroundWindow();
     GoToDesktopNumberFunc((arg->i - 1 + 10)%10);
+    SetForegroundWindow(lastActive[(arg->i - 1 + 10)%10]);
 } 
\ No newline at end of file
diff --git a/config.h b/config.h
@@ -20,7 +20,7 @@ static const char *leetcode[] = {"open", "https://leetcode.com", NULL};
 static Key keys[] = {
 	/* modifier                     key        function        argument */
 	{ MODKEY,                       'B',       test,          {0} },
-	{ MODKEY,                       'P',       spawn,      	  {.v = vscode} },
+	{ MODKEY,                       'O',       spawn,      	  {.v = vscode} },
 	{ MODKEY,                     VK_RETURN, spawn,         {.v = termcmd } },
 	{ MODKEY | MOD_SHIFT,         VK_RETURN, spawn,         {.v = adminCmd } },
 	{ MODKEY,                     	'C', spawn,         {.v = codeforces } },
diff --git a/main.c b/main.c
@@ -35,11 +35,17 @@ typedef int(*GoToDesktopNumber)(int);
 typedef int(*MoveWindowToDesktopNumber)(HWND, int);
 typedef int(*GetCurrentDesktop)();
 typedef int(*GetWindowDesktopNumber)(HWND);
+typedef int(*GetCurrentDesktopNumber)();
+typedef int(*IsWindowOnCurrentVirtualDesktop)(HWND);
 
 GetCurrentDesktop GetCurrentDesktopFunc;
 MoveWindowToDesktopNumber MoveWindowToDesktopNumberFunc;
 GoToDesktopNumber GoToDesktopNumberFunc;
 GetWindowDesktopNumber GetWindowDesktopNumberFunc;
+GetCurrentDesktopNumber GetCurrentDesktopNumberFunc;
+IsWindowOnCurrentVirtualDesktop IsWindowOnCurrentVirtualDesktopFunc;
+
+HWND lastActive[10]; // 0 to 9 index
 #endif
 
 #include "actions.c"

@@ -106,6 +112,23 @@ void setup(HINSTANCE hInstance) {
 	for (int i = 0; i < LENGTH(keys); i++) RegisterHotKey(dwmhwnd, i + 1, keys[i].mod, keys[i].key);
 }
 
+#ifdef VDA_FEATURES
+BOOL CALLBACK initEnumCall(HWND hwnd, LPARAM lParam) {
+    char windowTitle[256];
+    GetWindowTextA(hwnd, windowTitle, sizeof(windowTitle));
+    if (IsWindowVisible(hwnd) && windowTitle[0] != '\0') {
+		if(lastActive[GetWindowDesktopNumberFunc(hwnd)]!=NULL) return TRUE;
+        printf("windowTitle: %s %d\n", windowTitle, GetWindowDesktopNumberFunc(hwnd));
+		lastActive[GetWindowDesktopNumberFunc(hwnd)]=hwnd;
+    }
+    return TRUE;
+}
+
+HWND initLastActive(){
+    EnumWindows(initEnumCall, 0);
+    return topmost;
+}
+#endif
 
 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
 

@@ -134,6 +157,11 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
 	GoToDesktopNumberFunc = (GoToDesktopNumber)GetProcAddress(VDA, "GoToDesktopNumber");
 	GetCurrentDesktopFunc = (GetCurrentDesktop)GetProcAddress(VDA, "GetCurrentDesktopNumber");
 	GetWindowDesktopNumberFunc = (GetWindowDesktopNumber)GetProcAddress(VDA, "GetWindowDesktopNumber");
+	GetCurrentDesktopNumberFunc = (GetCurrentDesktopNumber)GetProcAddress(VDA, "GetCurrentDesktopNumber");
+	IsWindowOnCurrentVirtualDesktopFunc = (IsWindowOnCurrentVirtualDesktop)GetProcAddress(VDA, "IsWindowOnCurrentVirtualDesktop");
+	for(int i=0; i<10; i++) lastActive[i]=NULL;
+	// init with topmost windows of all VDs is essential for when not launched at startup
+	initLastActive();
 	#endif
 	MSG msg;
 	setup(hInstance);