1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* Desktop action adapter: this logic belongs to the plugin, not the hotkey host. */
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include "../../plugin.h"
#include "../../libs/virtualdesktopaccessor/virtualdesktopaccessor.h"
static HWND last_active[10];
static HWND topmost;
static int initialized;
static BOOL CALLBACK remember_window(HWND hwnd, LPARAM unused) {
(void)unused;
char title[256];
GetWindowTextA(hwnd, title, sizeof title);
if (IsWindowVisible(hwnd) && title[0]) {
int index = GetWindowDesktopNumber(hwnd);
if (index >= 0 && index < 10 && !last_active[index]) last_active[index] = hwnd;
}
return TRUE;
}
static BOOL CALLBACK find_current_window(HWND hwnd, LPARAM unused) {
(void)unused;
char title[256];
GetWindowTextA(hwnd, title, sizeof title);
if (IsWindowVisible(hwnd) && title[0] && IsWindowOnCurrentVirtualDesktop(hwnd) == 1) {
topmost = hwnd;
return FALSE;
}
return TRUE;
}
/* Returns config desktop number converted to the DLL's zero-based index. */
static int parse_desktop(const char *args) {
if (!args || !*args) return -1;
char *end;
long value = strtol(args, &end, 10);
if (end == args || *end || value < 0 || value > 10) return -1;
return value ? (int)value - 1 : 9;
}
__declspec(dllexport) int sxhkd_plugin_abi(void) {
if (!initialized) {
EnumWindows(remember_window, 0);
initialized = 1;
}
return SXHKD_PLUGIN_ABI;
}
__declspec(dllexport) int sxhkd_plugin_invoke(const char *action, const char *arguments) {
if (!action) return -1;
if (strcmp(action, "pin-app") == 0 || strcmp(action, "unpin-app") == 0 ||
strcmp(action, "app-pin-status") == 0) {
if (!arguments || *arguments) return -1;
HWND foreground = GetForegroundWindow();
int result = !foreground ? -1 : strcmp(action, "pin-app") == 0
? PinApp(foreground) : strcmp(action, "unpin-app") == 0
? UnPinApp(foreground) : IsPinnedApp(foreground);
/* Status succeeds only when pinned; callers choose their own feedback. */
return result == 1 ? 0 : -1;
}
int index = parse_desktop(arguments);
if (index < 0) return -1;
if (strcmp(action, "goto-desktop") == 0) {
int current = GetCurrentDesktopNumber();
if (current < 0 || current >= 10) return -1;
if (current == index) return 0;
last_active[current] = GetForegroundWindow();
if (GoToDesktopNumber(index) != 1) return -1;
if (last_active[index] && IsWindow(last_active[index]) &&
GetWindowDesktopNumber(last_active[index]) == index)
SetForegroundWindow(last_active[index]);
return 0;
}
if (strcmp(action, "move-to-desktop") == 0) {
HWND foreground = GetForegroundWindow();
if (!foreground) return -1;
if (GetCurrentDesktopNumber() == index && IsWindowOnCurrentVirtualDesktop(foreground) == 1)
return 0;
if (MoveWindowToDesktopNumber(foreground, index) != 1) return -1;
last_active[index] = foreground;
topmost = NULL;
EnumWindows(find_current_window, 0);
if (topmost) SetForegroundWindow(topmost);
return 0;
}
return -1;
}