-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleWindowManager.c
6451 lines (5723 loc) · 209 KB
/
SimpleWindowManager.c
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define COBJMACROS
#include <windows.h>
#include <oleacc.h>
#include <stdio.h>
#include <stdlib.h>
#include <psapi.h>
#include <wingdi.h>
#include <tchar.h>
#include <wingdi.h>
#include <shlwapi.h>
#include <initguid.h>
#include <mmdeviceapi.h>
#include <endpointvolume.h>
#include <commctrl.h>
#include <processthreadsapi.h>
#include <securitybaseapi.h>
#include <windowsx.h>
#include <netlistmgr.h>
#include <dwmapi.h>
#include <strsafe.h>
#include <shellapi.h>
#include <wbemidl.h>
#include <Assert.h>
#include <oleauto.h>
#include <uxtheme.h>
#include "fzf\\fzf.h"
#include "SMenu.h"
#include "ListProcesses.h"
#include "ListWindows.h"
#include "ListServices.h"
#include "SimpleWindowManager.h"
#include "dcomp_border_window.h"
#define MAX_WORKSPACES 10
static const TCHAR UWP_WRAPPER_CLASS[] = L"ApplicationFrameWindow";
static const TCHAR TASKBAR_CLASS[] = L"Shell_TrayWnd";
static const TCHAR TASKBAR2_CLASS[] = L"Shell_SecondaryTrayWnd";
DEFINE_GUID(IID_IMMDeviceEnumerator, 0xa95664d2, 0x9614, 0x4f35, 0xa7, 0x46, 0xde, 0x8d, 0xb6, 0x36, 0x17, 0xe6);
DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xbcde0395, 0xe52f, 0x467c, 0x8e, 0x3d, 0xc4, 0x57, 0x92, 0x91, 0x69, 0x2e);
DEFINE_GUID(IID_IAudioEndpointVolume, 0x5cdf2c82, 0x841e, 0x4546, 0x97, 0x22, 0x0c, 0xf7, 0x40, 0x78, 0x22, 0x9a);
DEFINE_GUID(CLSID_NetworkListManager, 0xdcb00c01, 0x570f, 0x4a9b, 0x8d,0x69, 0x19,0x9f,0xdb,0xa5,0x72,0x3b);
DEFINE_GUID(IID_INetworkListManager, 0xdcb00000, 0x570f, 0x4a9b, 0x8d,0x69, 0x19,0x9f,0xdb,0xa5,0x72,0x3b);
DEFINE_GUID(CLSID_WbemLocator, 0x4590f811, 0x1d3a, 0x11d0, 0x89, 0x1f,0x00, 0xaa, 0x00, 0x4b, 0x2e, 0x24);
DEFINE_GUID(IID_IWbemLocator, 0xdc12a687, 0x737f, 0x11cf, 0x88, 0x4d, 0x00, 0xaa, 0x00, 0x4b, 0x2e, 0x24);
typedef struct LauncherProcess LauncherProcess;
struct LauncherProcess
{
HANDLE readFileHandle;
DWORD processId;
HANDLE wait;
HANDLE event;
void (*onSuccess) (CHAR *stdOut);
};
HHOOK g_kb_hook = 0;
HHOOK g_mouse_hook = 0;
static BOOL CALLBACK enum_windows_callback(HWND hWnd, LPARAM lparam);
void tileLayout_select_next_window(Workspace *workspace);
void tileLayout_select_previous_window(Workspace *workspace);
void tileLayout_swap_clients(Client *client1, Client *client2);
void tilelayout_move_client_next(Client *client);
void tilelayout_move_client_previous(Client *client);
void tilelayout_calulate_and_apply_client_sizes(Workspace *workspace);
void deckLayout_select_next_window(Workspace *workspace);
void deckLayout_move_client_next(Client *client);
void deckLayout_client_to_main(Client *client);
void deckLayout_move_client_previous(Client *client);
void deckLayout_apply_to_workspace(Workspace *workspace);
void horizontaldeckLayout_apply_to_workspace(Workspace *workspace);
void monacleLayout_select_next_client(Workspace *workspace);
void monacleLayout_select_previous_client(Workspace *workspace);
void monacleLayout_move_client_next(Client *client);
void monacleLayout_move_client_previous(Client *client);
void monacleLayout_calculate_and_apply_client_sizes(Workspace *workspace);
void noop_swap_clients(Client *client1, Client *client2);
void process_with_stdin_start(TCHAR *cmdArgs, CHAR **lines, int numberOfLines, void (*onSuccess) (CHAR *));
void start_process(CHAR *processExe, CHAR *cmdArgs, DWORD creationFlags);
void process_with_stdout_start(CHAR *cmdArgs, void (*onSuccess) (CHAR *));
static int get_modifiers_pressed();
static void clients_add_before(Client *clientToAdd, Client *clientToAddBefore);
static void windowManager_remove_client_if_found_by_hwnd(WindowManagerState *self, HWND hwnd);
static void windowManager_move_window_to_workspace_and_arrange(WindowManagerState *self, HWND hwnd, Workspace *workspace);
static Workspace* windowManager_find_client_workspace_using_filters(WindowManagerState *self, Client *client);
static Client* windowManager_find_client_in_workspaces_by_hwnd(WindowManagerState *self, HWND hwnd);
static void workspace_add_client(Workspace *workspace, Client *client);
static BOOL workspace_remove_client(Workspace *workspace, Client *client);
static void workspace_arrange_windows(Workspace *workspace, WindowManagerState *windowManagerState);
static void workspace_arrange_windows_with_defer_handle(Workspace *workspace, HDWP hdwp, WindowManagerState *windowManagerState);
static void workspace_increase_main_width(WindowManagerState *windowManagerState, Workspace *workspace);
static void workspace_decrease_main_width(WindowManagerState *windowManagerState, Workspace *workspace);
static void workspace_add_minimized_client(Workspace *workspace, Client *client);
static void workspace_add_unminimized_client(Workspace *workspace, Client *client);
static void workspace_remove_minimized_client(Workspace *workspace, Client *client);
static void workspace_remove_unminimized_client(Workspace *workspace, Client *client);
static void workspace_remove_client_and_arrange(WindowManagerState *windowManagerState, Workspace *workspace, Client *client);
static int workspace_update_client_counts(Workspace *workspace);
static int workspace_get_number_of_clients(Workspace *workspace);
static KeyBinding* keybindings_find_existing_or_create(WindowManagerState *windowManager, CHAR* name, int modifiers, unsigned int key);
static ScratchWindow* scratch_windows_find_from_client(WindowManagerState *self, Client *client);
static ScratchWindow* scratch_windows_find_from_hwnd(WindowManagerState *self, HWND hwnd);
static void scratch_window_toggle(WindowManagerState *windowManager, ScratchWindow *self);
static void scratch_window_show(WindowManagerState *windowManagerState, ScratchWindow *self);
static void scratch_window_hide(WindowManagerState *windowManager, ScratchWindow *self);
static Client* workspace_find_client_by_hwnd(Workspace *workspace, HWND hwnd);
static Client* clientFactory_create_from_hwnd(HWND hwnd);
static void client_move_to_location_on_screen(Client *client, HDWP hdwp, BOOL setZOrder, Monitor *hiddenWindowMonitor, BOOL (*useOldMoveLogicFunc) (Client *client));
static void client_move_from_unminimized_to_minimized(WindowManagerState *windowManagerState, Client *client);
static void client_move_from_minimized_to_unminimized(WindowManagerState *windowManagerState, Client *client);
static void client_set_screen_coordinates(Client *client, int w, int h, int x, int y);
static void free_client(Client *client);
static void scratch_window_remove(WindowManagerState *windowManager, ScratchWindow *scratchWindow);
static void scratch_window_add(WindowManagerState *windowManager, ScratchWindow *scratchWindow);
static void scratch_window_focus(WindowManagerState *windowManagerState, ScratchWindow *scratchWindow);
static void menu_hide(WindowManagerState *windowManagerState);
static void button_set_selected(Button *button, BOOL value);
static void button_set_has_clients(Button *button, BOOL value);
static void button_press_handle(WindowManagerState *self, Button *button);
static void button_redraw(Button *button);
static void bar_apply_workspace_change(Bar *bar, Workspace *previousWorkspace, Workspace *newWorkspace);
static void bar_trigger_paint(Bar *bar);
static void bar_trigger_selected_window_paint(Bar *self);
static void bar_run(Bar *bar, WNDCLASSEX *barWindowClass, int barHeight);
static void border_window_update(WindowManagerState *windowManagerState);
static void border_window_update_with_defer(WindowManagerState *windowManagerState, HDWP hdwp);
static void border_window_hide(HWND self);
static LRESULT CALLBACK button_message_loop( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData);
static void monitor_select(WindowManagerState *self, Monitor *monitor);
static void monitor_set_layout(WindowManagerState *windowManagerState, Layout *layout);
static void monitor_set_workspace_and_arrange(Workspace *workspace, Monitor *monitor, HDWP hdwp, WindowManagerState *windowManagerState);
static void monitor_set_workspace(Workspace *workspace, Monitor *monitor);
static BOOL is_root_window(HWND hwnd, LONG styles, LONG exStyles);
static int get_cpu_usage(void);
static int get_memory_percent(void);
static int run (void);
static BOOL hit_test_hwnd(HWND hwnd);
static BOOL hit_test_monitor(Monitor *monitor);
static BOOL hit_test_client(Client *client);
static void drag_drop_cancel(DragDropState *self);
static IAudioEndpointVolume *g_audioEndpointVolume;
static INetworkListManager *g_networkListManager;
static WindowManagerState g_windowManagerState;
static ResizeState g_resizeState;
static DragDropState g_dragDropState;
Layout deckLayout = {
.select_next_window = deckLayout_select_next_window,
//using the same function for next and previous since there will only be 2 windows to swicth between.
//It will always be moving between the 2
.select_previous_window = deckLayout_select_next_window,
.swap_clients = tileLayout_swap_clients,
.move_client_to_main = deckLayout_client_to_main,
.move_client_next = deckLayout_move_client_next,
.move_client_previous = deckLayout_move_client_previous,
.apply_to_workspace = deckLayout_apply_to_workspace,
.next = NULL,
.tag = L"D"
};
Layout horizontaldeckLayout = {
.select_next_window = deckLayout_select_next_window,
//using the same function for next and previous since there will only be 2 windows to swicth between.
//It will always be moving between the 2
.select_previous_window = deckLayout_select_next_window,
.swap_clients = tileLayout_swap_clients,
.move_client_to_main = deckLayout_client_to_main,
.move_client_next = deckLayout_move_client_next,
.move_client_previous = deckLayout_move_client_previous,
.apply_to_workspace = horizontaldeckLayout_apply_to_workspace,
.next = NULL,
.tag = L"D"
};
Layout monacleLayout = {
.select_next_window = monacleLayout_select_next_client,
.select_previous_window = monacleLayout_select_previous_client,
.swap_clients = noop_swap_clients,
.move_client_to_main = deckLayout_client_to_main,
.move_client_next = monacleLayout_move_client_next,
.move_client_previous = monacleLayout_move_client_previous,
.apply_to_workspace = monacleLayout_calculate_and_apply_client_sizes,
.next = &deckLayout,
.tag = L"M"
};
Layout tileLayout = {
.select_next_window = tileLayout_select_next_window,
.select_previous_window = tileLayout_select_previous_window,
.swap_clients = tileLayout_swap_clients,
.move_client_to_main = deckLayout_client_to_main,
.move_client_next = tilelayout_move_client_next,
.move_client_previous = tilelayout_move_client_previous,
.apply_to_workspace = tilelayout_calulate_and_apply_client_sizes,
.next = &monacleLayout,
.tag = L"T"
};
Layout *headLayoutNode = &tileLayout;
static CHAR *cmdLineExe = "C:\\Windows\\System32\\cmd.exe";
IWbemServices *services = NULL;
Configuration *configuration;
void run_command_from_menu(char *stdOut, void *state)
{
WindowManagerState *windowManagerState = (WindowManagerState*)state;
const char deli[] = " ";
char *next_token = NULL;
char* name = strtok_s(stdOut, deli, &next_token);
for(int i = 0; i < windowManagerState->numberOfCommands; i++)
{
if(strcmp(name, windowManagerState->commands[i]->name) == 0)
{
windowManagerState->commands[i]->execute(windowManagerState->commands[i]);
return;
}
}
}
int commands_list(int maxItems, CHAR **lines, void *state)
{
WindowManagerState *windowManagerState = (WindowManagerState*)state;
size_t nameWidth = windowManagerState->longestCommandName;
const int typeWidth = 25;
const int keyBindingWidth = 30;
CHAR header[1024];
sprintf_s(
header,
1024,
"%-*s %-*s %-*s %s",
(int)nameWidth,
"Name",
typeWidth,
"Type",
keyBindingWidth,
"KeyBinding",
"Description");
lines[0] = _strdup(header);
int numberOfBindings = 1;
for(int i = 0; i < windowManagerState->numberOfCommands && windowManagerState->numberOfCommands < maxItems; i++)
{
Command *command = windowManagerState->commands[i];
CHAR keyBindingStr[MAX_PATH];
keyBindingStr[0] = '\0';
if(command->keyBinding)
{
CHAR modifiersKeyName[MAX_PATH];
modifiersKeyName[0] = '\0';
if(command->keyBinding->modifiers & LAlt)
{
strcat_s(modifiersKeyName, MAX_PATH, "+ALT");
}
if(command->keyBinding->modifiers & LCtl)
{
strcat_s(modifiersKeyName, MAX_PATH, "+CTL");
}
if(command->keyBinding->modifiers & LWin)
{
strcat_s(modifiersKeyName, MAX_PATH, "+WIN");
}
if(command->keyBinding->modifiers & LShift)
{
strcat_s(modifiersKeyName, MAX_PATH, "+SHIFT");
}
unsigned int scanCode = MapVirtualKey(command->keyBinding->key, MAPVK_VK_TO_VSC);
unsigned int scanCode2 = MapVirtualKey(VK_F14, MAPVK_VK_TO_VSC);
CHAR keyStrBuff2[MAX_PATH];
GetKeyNameTextA(scanCode2 << 16, keyStrBuff2, MAX_PATH);
BOOL keySet = false;
CHAR keyStrBuff[MAX_PATH];
switch (command->keyBinding->key)
{
case VK_LEFT: case VK_UP: case VK_RIGHT: case VK_DOWN:
case VK_PRIOR: case VK_NEXT:
case VK_END: case VK_HOME:
case VK_INSERT: case VK_DELETE:
case VK_DIVIDE:
case VK_NUMLOCK:
{
scanCode |= 0x100;
break;
}
case 0x7C:
strcpy_s(keyStrBuff, MAX_PATH, "F13");
keySet = true;
break;
case 0x7D:
strcpy_s(keyStrBuff, MAX_PATH, "F14");
keySet = true;
break;
case 0x7E:
strcpy_s(keyStrBuff, MAX_PATH, "F15");
keySet = true;
break;
case 0x7F:
strcpy_s(keyStrBuff, MAX_PATH, "F16");
keySet = true;
break;
case 0x80:
strcpy_s(keyStrBuff, MAX_PATH, "F17");
keySet = true;
break;
case 0x81:
strcpy_s(keyStrBuff, MAX_PATH, "F18");
keySet = true;
break;
case 0x82:
keySet = true;
strcpy_s(keyStrBuff, MAX_PATH, "F19");
break;
}
if(!keySet)
{
GetKeyNameTextA(scanCode << 16, keyStrBuff, MAX_PATH);
}
sprintf_s(
keyBindingStr,
50,
"%s+%s",
//+1 is hack to remove first + from concatentation
modifiersKeyName + 1,
keyStrBuff);
}
CHAR stringToAdd[1024];
CHAR commandDescription[MAX_PATH];
command->getDescription(command, MAX_PATH, commandDescription);
sprintf_s(
stringToAdd,
1024,
"%-*s %-*s %-*s %s",
(int)nameWidth,
command->name,
typeWidth,
command->type,
keyBindingWidth,
keyBindingStr,
commandDescription);
lines[numberOfBindings] = _strdup(stringToAdd);
numberOfBindings++;
}
return numberOfBindings;
}
void register_keybindings_menu_with_modifiers(int modifiers, int virtualKey)
{
MenuDefinition *definition = menu_create_and_register();
definition->itemsAction = commands_list;
definition->hasHeader = TRUE;
definition->onSelection = run_command_from_menu;
keybinding_create_with_menu_arg("ListKeyBindings", modifiers, virtualKey, menu_run, definition);
}
void register_keybindings_menu(void)
{
register_keybindings_menu_with_modifiers(LAlt, VK_OEM_2);
}
void register_list_processes_menu(int modifiers, int virtualKey)
{
MenuDefinition *listProcessMenu = menu_create_and_register();
menu_definition_set_load_action(listProcessMenu, list_processes_run_no_sort);
NamedCommand *killProcessCommand = MenuDefinition_AddNamedCommand(listProcessMenu, "procKill:cmd /c taskkill /f /pid {}", TRUE, FALSE);
NamedCommand_SetTextRange(killProcessCommand, 76, 8, TRUE);
NamedCommand *windbgCommand = MenuDefinition_AddNamedCommand(listProcessMenu, "windbg:windbgx -p {}", FALSE, TRUE);
NamedCommand_SetTextRange(windbgCommand, 76, 8, TRUE);
NamedCommand *procDumpNamedCommand = MenuDefinition_AddNamedCommand(listProcessMenu, "procdump:cmd /c procdump -accepteula -ma {} %USERPROFILE%\\memory_dumps", FALSE, FALSE);
NamedCommand_SetTextRange(procDumpNamedCommand, 76, 8, TRUE);
MenuDefinition_AddLoadActionKeyBinding(listProcessMenu, VK_CONTROL, VK_1, list_processes_run_sorted_by_private_bytes, "Sort Pvt Bytes");
MenuDefinition_AddLoadActionKeyBinding(listProcessMenu, VK_CONTROL, VK_2, list_processes_run_sorted_by_working_set, "Sort Wrk Set");
MenuDefinition_AddLoadActionKeyBinding(listProcessMenu, VK_CONTROL, VK_3, list_processes_run_sorted_by_cpu, "Sort Cpu");
MenuDefinition_AddLoadActionKeyBinding(listProcessMenu, VK_CONTROL, VK_4, list_processes_run_sorted_by_pid, "Sort Pid");
MenuDefinition_ParseAndSetRange(listProcessMenu, "76,8");
MenuDefinition_ParseAndAddKeyBinding(listProcessMenu, "ctl-k:procKill", FALSE);
MenuDefinition_ParseAndAddKeyBinding(listProcessMenu, "ctl-d:windbg", FALSE);
MenuDefinition_ParseAndAddKeyBinding(listProcessMenu, "ctl-m:procdump", FALSE);
listProcessMenu->hasHeader = TRUE;
keybinding_create_with_menu_arg("ProcessListMenu", modifiers, virtualKey, menu_run, listProcessMenu);
}
void register_list_windows_memu(int modifiers, int virtualKey)
{
MenuDefinition *listWindowsMenu = menu_create_and_register();
listWindowsMenu->hasHeader = TRUE;
menu_definition_set_load_action(listWindowsMenu, list_windows_run);
listWindowsMenu->onSelection = open_windows_scratch_exit_callback;
keybinding_create_with_menu_arg("ListWindowsMenu", modifiers, virtualKey, menu_run, listWindowsMenu);
}
void register_list_services_menu(int modifiers, int virtualKey)
{
MenuDefinition *listServicesMenu = menu_create_and_register();
listServicesMenu->hasHeader = TRUE;
menu_definition_set_load_action(listServicesMenu, list_services_run_no_sort);
NamedCommand *serviceStartCommand = MenuDefinition_AddNamedCommand(listServicesMenu, "start:sc start \"\"{}\"\"", TRUE, FALSE);
NamedCommand *serviceStopCommand = MenuDefinition_AddNamedCommand(listServicesMenu, "stop:sc stop \"\"{}\"\"", TRUE, FALSE);
NamedCommand_SetTextRange(serviceStartCommand, 0, 45, TRUE);
NamedCommand_SetTextRange(serviceStopCommand, 0, 45, TRUE);
MenuDefinition_ParseAndAddKeyBinding(listServicesMenu, "ctl-s:start", FALSE);
MenuDefinition_ParseAndAddKeyBinding(listServicesMenu, "ctl-x:stop", FALSE);
keybinding_create_with_menu_arg("ListServicesMenu", modifiers, virtualKey, menu_run, listServicesMenu);
}
void build_list_directories_cmd(char* buffer, size_t bufferSize, char* strings[], size_t numStrings)
{
buffer[0] = '\0';
strcat_s(buffer, bufferSize, "ld:cmd /c dir /s /b");
strcat_s(buffer, bufferSize, " ");
for (size_t i = 0; i < numStrings; i++) {
strcat_s(buffer, bufferSize, "\"");
strcat_s(buffer, bufferSize, strings[i]);
strcat_s(buffer, bufferSize, "\" ");
}
strcat_s(buffer, bufferSize, " | findstr /i \"\\.lnk$ \\.exe$\"");
}
void register_program_launcher_menu(int modifiers, int virtualKey, CHAR** directories, size_t numberOfDirectories, BOOL isElevated)
{
CHAR cmdBuf[4096];
build_list_directories_cmd(cmdBuf, 4096, directories, numberOfDirectories);
MenuDefinition *programLauncher = menu_create_and_register();
MenuDefinition_AddNamedCommand(programLauncher, cmdBuf, FALSE, FALSE);
MenuDefinition_ParseAndAddLoadCommand(programLauncher, "ld", TRUE);
if(isElevated)
{
programLauncher->onSelection = open_program_scratch_callback;
keybinding_create_with_menu_arg("ProgramLauncherMenu", modifiers, virtualKey, menu_run, programLauncher);
}
else
{
programLauncher->onSelection = open_program_scratch_callback_not_elevated;
keybinding_create_with_menu_arg("ProgramLauncherNotElevatedMenu", modifiers, virtualKey, menu_run, programLauncher);
}
}
void mimimize_focused_window(WindowManagerState *self)
{
HWND foregroundHwnd = GetForegroundWindow();
ShowWindow(foregroundHwnd, SW_SHOWMINIMIZED);
workspace_focus_selected_window(self, self->selectedMonitor->workspace);
}
void move_focused_client_next(WindowManagerState *self)
{
HWND foregroundHwnd = GetForegroundWindow();
Client* existingClient = windowManager_find_client_in_workspaces_by_hwnd(self, foregroundHwnd);
if(existingClient)
{
existingClient->workspace->layout->move_client_next(existingClient);
workspace_arrange_windows(existingClient->workspace, self);
workspace_focus_selected_window(self, existingClient->workspace);
}
}
void move_focused_client_previous(WindowManagerState *self)
{
HWND foregroundHwnd = GetForegroundWindow();
Client* existingClient = windowManager_find_client_in_workspaces_by_hwnd(self, foregroundHwnd);
if(existingClient)
{
existingClient->workspace->layout->move_client_previous(existingClient->workspace->selected);
workspace_arrange_windows(existingClient->workspace, self);
workspace_focus_selected_window(self, existingClient->workspace);
}
}
void move_focused_window_to_workspace(WindowManagerState *self, Workspace *workspace)
{
HWND foregroundHwnd = GetForegroundWindow();
windowManager_move_window_to_workspace_and_arrange(self, foregroundHwnd, workspace);
workspace_focus_selected_window(self, self->selectedMonitor->workspace);
}
void move_focused_window_to_selected_monitor_workspace(WindowManagerState *self)
{
UNREFERENCED_PARAMETER(self);
Workspace *workspace = self->selectedMonitor->workspace;
move_focused_window_to_workspace(self, workspace);
}
void move_workspace_to_secondary_monitor_without_focus(WindowManagerState *self, Workspace *workspace)
{
windowManager_move_workspace_to_monitor(self, self->secondaryMonitor, workspace);
if(self->primaryMonitor->workspace)
{
workspace_focus_selected_window(self, self->primaryMonitor->workspace);
}
}
void move_focused_window_to_main(WindowManagerState *self)
{
if(self->selectedMonitor->workspace)
{
Client *client = self->selectedMonitor->workspace->selected;
if(client)
{
if(client == client->workspace->clients)
{
if(client->next)
{
client = client->next;
}
}
client->workspace->layout->move_client_to_main(client);
workspace_arrange_windows(client->workspace, self);
workspace_focus_selected_window(self, client->workspace);
}
}
}
void move_secondary_monitor_focused_window_to_main(WindowManagerState *self)
{
if(self->secondaryMonitor->workspace)
{
Client *client = self->secondaryMonitor->workspace->selected;
if(client)
{
if(client == client->workspace->clients)
{
if(client->next)
{
client = client->next;
}
}
client->workspace->layout->move_client_to_main(client);
workspace_arrange_windows(client->workspace, self);
}
}
}
void toggle_create_window_in_current_workspace(WindowManagerState *self)
{
if(self->currentWindowRoutingMode != FilteredCurrentWorkspace)
{
self->currentWindowRoutingMode = FilteredCurrentWorkspace;
}
else
{
self->currentWindowRoutingMode = FilteredAndRoutedToWorkspace;
}
for(int i = 0; i < self->numberOfMonitors; i++)
{
if(!self->monitors[i]->isHidden)
{
bar_trigger_selected_window_paint(self->monitors[i]->bar);
}
}
}
void toggle_ignore_workspace_filters(WindowManagerState *self)
{
if(self->currentWindowRoutingMode != NotFilteredCurrentWorkspace)
{
self->currentWindowRoutingMode = NotFilteredCurrentWorkspace;
}
else
{
self->currentWindowRoutingMode = FilteredAndRoutedToWorkspace;
}
for(int i = 0; i < self->numberOfMonitors; i++)
{
if(!self->monitors[i]->isHidden)
{
bar_trigger_selected_window_paint(self->monitors[i]->bar);
}
}
}
void toggle_non_filtered_windows_assigned_to_current_workspace(WindowManagerState *self)
{
if(self->currentWindowRoutingMode != FilteredRoutedNonFilteredCurrentWorkspace)
{
self->currentWindowRoutingMode = FilteredRoutedNonFilteredCurrentWorkspace;
}
else
{
self->currentWindowRoutingMode = FilteredAndRoutedToWorkspace;
}
for(int i = 0; i < self->numberOfMonitors; i++)
{
if(!self->monitors[i]->isHidden)
{
bar_trigger_selected_window_paint(self->monitors[i]->bar);
}
}
}
void swap_selected_monitor_to(WindowManagerState *self, Workspace *workspace)
{
windowManager_move_workspace_to_monitor(self, self->selectedMonitor, workspace);
workspace_focus_selected_window(self, workspace);
}
void goto_last_workspace(WindowManagerState *self)
{
Workspace *workspace = self->lastWorkspace;
if(workspace)
{
windowManager_move_workspace_to_monitor(self, self->selectedMonitor, workspace);
workspace_focus_selected_window(self, workspace);
}
}
void close_focused_window(WindowManagerState *self)
{
UNREFERENCED_PARAMETER(self);
HWND foregroundHwnd = GetForegroundWindow();
SendMessage(foregroundHwnd, WM_CLOSE, (WPARAM)NULL, (LPARAM)NULL);
}
void float_window_move_up(WindowManagerState *self, HWND hwnd)
{
RECT currentRect;
GetWindowRect(hwnd, ¤tRect);
int targetLeft = currentRect.left;
int targetTop = currentRect.top - self->floatWindowMovement;
int targetWidth = currentRect.right - currentRect.left;
int targetHeight = currentRect.bottom - currentRect.top;
MoveWindow(hwnd, targetLeft, targetTop, targetWidth, targetHeight, FALSE);
}
void float_window_move_down(WindowManagerState *self, HWND hwnd)
{
RECT currentRect;
GetWindowRect(hwnd, ¤tRect);
int targetLeft = currentRect.left;
int targetTop = currentRect.top + self->floatWindowMovement;
int targetWidth = currentRect.right - currentRect.left;
int targetHeight = currentRect.bottom - currentRect.top;
MoveWindow(hwnd, targetLeft, targetTop, targetWidth, targetHeight, FALSE);
}
void float_window_move_right(WindowManagerState *self, HWND hwnd)
{
RECT currentRect;
GetWindowRect(hwnd, ¤tRect);
int targetLeft = currentRect.left + self->floatWindowMovement;
int targetTop = currentRect.top;
int targetWidth = currentRect.right - currentRect.left;
int targetHeight = currentRect.bottom - currentRect.top;
MoveWindow(hwnd, targetLeft, targetTop, targetWidth, targetHeight, FALSE);
}
void float_window_move_left(WindowManagerState *self, HWND hwnd)
{
RECT currentRect;
GetWindowRect(hwnd, ¤tRect);
int targetLeft = currentRect.left - self->floatWindowMovement;
int targetTop = currentRect.top;
int targetWidth = currentRect.right - currentRect.left;
int targetHeight = currentRect.bottom - currentRect.top;
MoveWindow(hwnd, targetLeft, targetTop, targetWidth, targetHeight, FALSE);
}
void kill_focused_window(WindowManagerState *self)
{
UNREFERENCED_PARAMETER(self);
HWND foregroundHwnd = GetForegroundWindow();
DWORD processId = 0;
GetWindowThreadProcessId(foregroundHwnd, &processId);
HANDLE processHandle = OpenProcess(PROCESS_TERMINATE, FALSE, processId);
TerminateProcess(processHandle, 1);
CloseHandle(processHandle);
}
void redraw_focused_window(WindowManagerState *self)
{
UNREFERENCED_PARAMETER(self);
HWND foregroundHwnd = GetForegroundWindow();
RedrawWindow(foregroundHwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
}
void move_focused_window_right(WindowManagerState *self)
{
if(self->selectedMonitor->scratchWindow)
{
return;
}
HWND foregroundHwnd = GetForegroundWindow();
Client* existingClient = windowManager_find_client_in_workspaces_by_hwnd(self, foregroundHwnd);
if(!existingClient)
{
float_window_move_right(self, foregroundHwnd);
}
else
{
workspace_increase_main_width_selected_monitor(self);
}
}
void move_focused_window_left(WindowManagerState *self)
{
if(self->selectedMonitor->scratchWindow)
{
return;
}
HWND foregroundHwnd = GetForegroundWindow();
Client* existingClient = windowManager_find_client_in_workspaces_by_hwnd(self, foregroundHwnd);
if(!existingClient)
{
float_window_move_left(self, foregroundHwnd);
}
else
{
workspace_decrease_main_width_selected_monitor(self);
}
}
void move_focused_window_up(WindowManagerState *self)
{
if(self->selectedMonitor->scratchWindow)
{
return;
}
HWND foregroundHwnd = GetForegroundWindow();
Client* existingClient = windowManager_find_client_in_workspaces_by_hwnd(self, foregroundHwnd);
if(!existingClient)
{
float_window_move_up(self, foregroundHwnd);
}
}
void move_focused_window_to_monitor(WindowManagerState *self, Monitor *monitor)
{
HWND foregroundHwnd = GetForegroundWindow();
Client *client = windowManager_find_client_in_workspaces_by_hwnd(self, foregroundHwnd);
if(!client)
{
RECT windowRect;
GetWindowRect(foregroundHwnd, &windowRect);
int windowWidth = windowRect.right - windowRect.left;
int windowHeight = windowRect.bottom - windowRect.top;
int centerX = (monitor->w - windowWidth) / 2;
int x = centerX + monitor->xOffset;
int y = (monitor->h - windowHeight) / 2;
SetWindowPos(foregroundHwnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
}
void move_focused_window_down(WindowManagerState *self)
{
if(self->selectedMonitor->scratchWindow)
{
return;
}
HWND foregroundHwnd = GetForegroundWindow();
Client* existingClient = windowManager_find_client_in_workspaces_by_hwnd(self, foregroundHwnd);
if(!existingClient)
{
float_window_move_down(self, foregroundHwnd);
}
}
void select_next_window(WindowManagerState *self)
{
Workspace *workspace = self->selectedMonitor->workspace;
if(self->selectedMonitor->scratchWindow)
{
scratch_window_hide(self, self->selectedMonitor->scratchWindow);
workspace_focus_selected_window(self, workspace);
return;
}
workspace->layout->select_next_window(workspace);
workspace_focus_selected_window(self, workspace);
}
void select_previous_window(WindowManagerState *self)
{
Workspace *workspace = self->selectedMonitor->workspace;
if(self->selectedMonitor->scratchWindow)
{
scratch_window_hide(self, self->selectedMonitor->scratchWindow);
workspace_focus_selected_window(self, workspace);
return;
}
workspace->layout->select_previous_window(workspace);
workspace_focus_selected_window(self, workspace);
}
void toggle_selected_monitor_layout(WindowManagerState *self)
{
Workspace *workspace = self->selectedMonitor->workspace;
if(workspace->layout->next)
{
workspace->selected = workspace->clients;
monitor_set_layout(self, workspace->layout->next);
}
else
{
monitor_set_layout(self, headLayoutNode);
}
}
void swap_selected_monitor_to_monacle_layout(WindowManagerState *self)
{
monitor_set_layout(self, &monacleLayout);
}
void swap_selected_monitor_to_deck_layout(WindowManagerState *self)
{
monitor_set_layout(self, &deckLayout);
}
void swap_selected_monitor_to_horizontaldeck_layout(WindowManagerState *self)
{
monitor_set_layout(self, &horizontaldeckLayout);
}
void swap_selected_monitor_to_tile_layout(WindowManagerState *self)
{
monitor_set_layout(self, &tileLayout);
}
void arrange_clients_in_selected_workspace(WindowManagerState *self)
{
self->selectedMonitor->workspace->mainOffset = 0;
workspace_arrange_windows(self->selectedMonitor->workspace, self);
workspace_focus_selected_window(self, self->selectedMonitor->workspace);
}
int taskbar_get_height(HWND taskbarHwnd)
{
long taskBarStyles = GetWindowLong(taskbarHwnd, GWL_STYLE);
int result = 0;
if(taskBarStyles & WS_VISIBLE)
{
RECT taskBarRect;
GetWindowRect(taskbarHwnd, &taskBarRect);
result = taskBarRect.bottom - taskBarRect.top;
}
return result;
}
void monitor_calculate_height(Monitor *self, HWND taskbarHwnd)
{
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
int taskBarHeight = taskbar_get_height(taskbarHwnd);
self->h = screenHeight - taskBarHeight;
self->bottom = screenHeight - taskBarHeight;
}
void monitors_resize_for_taskbar(WindowManagerState *windowManagerState, HWND taskbarHwnd)
{
for(int i = 0; i < windowManagerState->numberOfMonitors; i++)
{
monitor_calculate_height(windowManagerState->monitors[i], taskbarHwnd);
if(windowManagerState->monitors[i]->workspace)
{
workspace_arrange_windows(windowManagerState->monitors[i]->workspace, windowManagerState);
if(windowManagerState->monitors[i] == windowManagerState->selectedMonitor)
{
workspace_focus_selected_window(windowManagerState, windowManagerState->monitors[i]->workspace);
}
}
}
}
void taskbar_toggle(WindowManagerState *self)
{
UNREFERENCED_PARAMETER(self);
HWND taskBarHandle = FindWindow(TASKBAR_CLASS, NULL);
HWND taskBar2Handle = FindWindow(TASKBAR2_CLASS, NULL);
long taskBarStyles = GetWindowLong(taskBarHandle, GWL_STYLE);
UINT showHideFlag = SW_SHOW;
if(taskBarStyles & WS_VISIBLE)
{
showHideFlag = SW_HIDE;
}
ShowWindow(taskBarHandle, showHideFlag);
ShowWindow(taskBar2Handle, showHideFlag);
}
void quit(WindowManagerState *self)
{
UNREFERENCED_PARAMETER(self);
ExitProcess(0);
}
BOOL has_float_styles(LONG_PTR styles, LONG_PTR exStyles)
{
if(exStyles & WS_EX_APPWINDOW)
{
return FALSE;
}
if(exStyles & WS_EX_TOOLWINDOW)
{
return TRUE;
}
if(!(styles & WS_SIZEBOX))
{
return TRUE;
}
return FALSE;
}
BOOL is_float_window(Client *client, LONG_PTR styles, LONG_PTR exStyles)
{
if(configuration->windowsThatShouldNotFloatFunc)
{
if(!configuration->windowsThatShouldNotFloatFunc(client, styles, exStyles))
{
return FALSE;
}
}
if(wcsstr(client->data->className, UWP_WRAPPER_CLASS))
{
if(configuration->floatUwpWindows)
{
return TRUE;
}
else
{
return FALSE;
}
}
WINDOWPLACEMENT placement = {0};
if(GetWindowPlacement(client->data->hwnd, &placement))
{
int height = placement.rcNormalPosition.bottom - placement.rcNormalPosition.top;
if(height < configuration->nonFloatWindowHeightMinimum)
{
return TRUE;
}
}
if(has_float_styles(styles, exStyles))
{
return TRUE;
}
return FALSE;
}
static BOOL CALLBACK enum_windows_callback(HWND hwnd, LPARAM lparam)
{
WindowManagerState *windowManagerState = (WindowManagerState*)lparam;
LONG styles = GetWindowLong(hwnd, GWL_STYLE);
LONG exStyles = GetWindowLong(hwnd, GWL_EXSTYLE);
BOOL isRootWindow = is_root_window(hwnd, styles, exStyles);
if(!isRootWindow)
{
return TRUE;
}
if(!(styles & WS_VISIBLE))
{
return TRUE;
}
Client *client = clientFactory_create_from_hwnd(hwnd);
ScratchWindow *scratchWindow = scratch_windows_find_from_client(windowManagerState, client);
if(scratchWindow)
{
if(!scratchWindow->client)