-
Notifications
You must be signed in to change notification settings - Fork 1
/
11globalForegroundWindow.c
2390 lines (1840 loc) · 64.2 KB
/
11globalForegroundWindow.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
/**
Max object: OS-wide foreground window get / set
-- 11oLsen.de
*/
#include "ext.h"
#include "ext_obex.h"
#include <tlhelp32.h>
#include <dwmapi.h> // add dwmapi.lib at linker additional dependencies
////////////////////////// object struct
typedef struct _globalForegroundWindow
{
t_object ob; // the object itself (must be first)
t_symbol *name;
void *outa; //outlets
void *outb;
void *outc;
void *outd;
void *outdump;
t_systhread x_systhread; // thread reference
t_systhread_mutex x_mutex; // mutual exclusion lock for threadsafety
int x_systhread_cancel; // thread cancel flag
void *x_qelem; // for message passing between threads
void *clock;
int x_outputmode;
int x_mask;
int x_rot;
//int consume;
// Thread and hook handles.
DWORD hook_thread_id;
HWINEVENTHOOK win_event_hhook;
t_atom winLoc[4];
int screenCount;
} t_globalForegroundWindow;
///////////////////////// function prototypes
void *globalForegroundWindow_new(t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_free(t_globalForegroundWindow *x);
void globalForegroundWindow_assist(t_globalForegroundWindow *x, void *b, long m, long a, char *s);
void globalForegroundWindow_bang(t_globalForegroundWindow *x);
void globalForegroundWindow_do_bang(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_cons(t_globalForegroundWindow *x, long m);
void globalForegroundWindow_start(t_globalForegroundWindow *x);
void globalForegroundWindow_stop(t_globalForegroundWindow *x);
void *globalForegroundWindow_threadproc(t_globalForegroundWindow *x);
//void *dispatch_proc(uiohook_event * const event);
t_globalForegroundWindow *thisobject();
void globalForegroundWindow_int(t_globalForegroundWindow *x, long m);
void unregister_running_hooks();
void CALLBACK win_hook_event_proc(HWINEVENTHOOK hook, DWORD event, HWND hWnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime);
void globalForegroundWindow_do_size(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_size(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_do_pos(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_pos(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_do_getwindows(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_getwindows(t_globalForegroundWindow *x);
void globalForegroundWindow_set(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_do_set(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_do_setWithNum(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_isrunning(t_globalForegroundWindow* x, t_symbol* s, long argc, t_atom* argv);
void globalForegroundWindow_do_isrunning(t_globalForegroundWindow* x, t_symbol* s, long argc, t_atom* argv);
void globalForegroundWindow_do_topmost(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_topmost(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_do_fullscreen(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_fullscreen(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_do_minimize(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_minimize(t_globalForegroundWindow *x);
void globalForegroundWindow_do_close(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_close(t_globalForegroundWindow *x);
void globalForegroundWindow_do_quitapp(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_quitapp(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_do_killapp(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_killapp(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_do_agent(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_agent(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_do_getscreens(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_getscreens(t_globalForegroundWindow *x);
void globalForegroundWindow_do_maximize(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_maximize(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_test(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
void globalForegroundWindow_testoutput(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv);
BOOL CALLBACK globalForegroundWindow_enumWindowCallback(HWND hWnd, LPARAM lparam);
BOOL CALLBACK globalForegroundWindow_closeWindowsWithPidCallback(HWND hWnd, LPARAM lparam);
BOOL CALLBACK globalForegroundWindow_closeWindowsWithProcNameCallback(HWND hWnd, LPARAM lparam);
BOOL CALLBACK globalForegroundWindow_theMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData);
BOOL CALLBACK globalForegroundWindow_setWindowWithProcNameCallback(HWND hWnd, LPARAM lparam);
BOOL CALLBACK globalForegroundWindow_setWindowWithProcNameAndTitleCallback(HWND hWnd, LPARAM lparam);
BOOL CALLBACK globalForegroundWindow_findWindowWithProcNameCallback(HWND hWnd, LPARAM lparam);
//Helper
void globalForegroundWindow_activateWindow(t_globalForegroundWindow *x, HWND hWnd);
int globalForegroundWindow_getPathAndFilenameForPid(DWORD dwPID, char **path, char **filename);
int globalForegroundWindow_TerminateProcessWithPid(DWORD ProcessId);
int globalForegroundWindow_storeBgWindow(t_globalForegroundWindow *x, DWORD dwPID, HWND hWnd);
void globalForegroundWindow_resetBgWindowWithHwnd(t_globalForegroundWindow *x, HWND hWnd);
void globalForegroundWindow_resetBgWindowsWithPid(t_globalForegroundWindow *x, DWORD dwPID);
void globalForegroundWindow_resetBgWindowsWithProcName(t_globalForegroundWindow *x, char* appNameToMatch);
void globalForegroundWindow_resetAllBgWindows(t_globalForegroundWindow *x);
int globalForegroundWindow_storeFsWindow(t_globalForegroundWindow *x, HWND hWnd);
void globalForegroundWindow_resetFsWindowWithHwnd(t_globalForegroundWindow *x, HWND hWnd);
void globalForegroundWindow_resetAllFsWindows(t_globalForegroundWindow *x);
int globalForegroundWindow_getRectOfMinimizedWindow(HWND hWnd, RECT* rectPtr);
int globalForegroundWindow_getFullscreenRectForWindow(HWND hWnd, RECT * rectPtr);
int globalForegroundWindow_windowTool(t_globalForegroundWindow *x, HWND hWnd, int CaSe);
//////////////////////// global class pointer variable
void *globalForegroundWindow_class;
t_globalForegroundWindow *t_globalForegroundWindow_temp[256]; // object pointer
DWORD globalForegroundWindow_thread_ids[256]; // thread ids
// need Pid+window+oldParent to store the bg (topmost -1) windows
DWORD bgWndPids[256];
HWND bgWndHandles[256];
HWND bgWndParentHandles[256];
// fullscreen windows
HWND fsWndHandles[256];
DWORD fsWndStyles[256];
// The handle to the DLL module pulled in DllMain on DLL_PROCESS_ATTACH.
HINSTANCE hInst;
static t_symbol *symNameChange;
static t_symbol *emptySym;
static t_symbol *SYMnoProcName;
static t_symbol *SYMnoTitle;
#pragma region MAIN and New
void ext_main(void *r)
{
t_class *c;
int i;
c = class_new("11globalForegroundWindow", (method)globalForegroundWindow_new, (method)globalForegroundWindow_free,
(long)sizeof(t_globalForegroundWindow), 0L /* leave NULL!! */, A_GIMME, 0);
class_addmethod(c, (method)globalForegroundWindow_assist, "assist", A_CANT, 0); /* you CAN'T call this from the patcher */
class_addmethod(c, (method)globalForegroundWindow_bang, "bang", 0);
class_addmethod(c, (method)globalForegroundWindow_int, "int", A_LONG, 0); // the method for an int in the right inlet (inlet 1)
class_addmethod(c, (method)globalForegroundWindow_getscreens, "getscreens", 0);
class_addmethod(c, (method)globalForegroundWindow_getwindows, "getwindows", 0);
class_addmethod(c, (method)globalForegroundWindow_minimize, "minimize", 0);
class_addmethod(c, (method)globalForegroundWindow_close, "close", 0);
class_addmethod(c, (method)globalForegroundWindow_isrunning, "isrunning", A_GIMME, 0);
class_addmethod(c, (method)globalForegroundWindow_set, "set", A_GIMME, 0);
class_addmethod(c, (method)globalForegroundWindow_size, "size", A_GIMME, 0);
class_addmethod(c, (method)globalForegroundWindow_pos, "pos", A_GIMME, 0);
class_addmethod(c, (method)globalForegroundWindow_topmost, "topmost", A_GIMME, 0);
class_addmethod(c, (method)globalForegroundWindow_fullscreen, "fullscreen", A_GIMME, 0);
class_addmethod(c, (method)globalForegroundWindow_killapp, "killapp", A_GIMME, 0);
class_addmethod(c, (method)globalForegroundWindow_quitapp, "quitapp", A_GIMME, 0);
class_addmethod(c, (method)globalForegroundWindow_agent, "agent", A_GIMME, 0);
class_addmethod(c, (method)globalForegroundWindow_maximize, "maximize", A_GIMME, 0);
class_addmethod(c, (method)globalForegroundWindow_test, "test", A_GIMME, 0);
CLASS_METHOD_ATTR_PARSE(c, "test", "undocumented", gensym("long"), 0, "1");
CLASS_METHOD_ATTR_PARSE(c, "agent", "undocumented", gensym("long"), 0, "1");
class_register(CLASS_BOX, c); /* CLASS_NOBOX */
globalForegroundWindow_class = c;
/*for (i = 0; i < 256; i++)
{
t_globalForegroundWindow_temp[i] = NULL;
globalForegroundWindow_thread_ids[i] = NULL;
}*/
symNameChange = gensym("namechange");
emptySym = gensym("");
SYMnoProcName = gensym("<noProcName>");
SYMnoTitle = gensym("<noTitle>");
object_post(NULL,"11globalForegroundWindow 2022/03/10 11OLSEN.DE");
return 0;
}
void *globalForegroundWindow_new(t_symbol *s, long argc, t_atom *argv) // NEW //////////////////////////
{
t_globalForegroundWindow *x = NULL;
if (x = (t_globalForegroundWindow *)object_alloc(globalForegroundWindow_class))
{
x->x_systhread = NULL;
//systhread_mutex_new(&x->x_mutex, 0);
x->outdump = outlet_new(x, NULL);
x->outd = outlet_new(x, NULL);
x->outc = outlet_new(x, NULL);
x->outb = outlet_new(x, NULL);
x->outa = listout(x);
// Thread and hook handles.
x->hook_thread_id = 0;
x->win_event_hhook = NULL;
x->name = s;
x->x_qelem = qelem_new(x, (method)globalForegroundWindow_do_bang);
}
return (x);
}
#pragma endregion
#pragma region BANG
void globalForegroundWindow_bang(t_globalForegroundWindow *x)
{
defer_low(x, (method)globalForegroundWindow_do_bang, NULL, 0, NULL);
}
void globalForegroundWindow_do_bang(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv)
{
//HWND
HWND hWnd = GetForegroundWindow(); // get handle of currently active window;
int length = 0;
LPWSTR wnd_title = NULL;
length = GetWindowTextLengthW(hWnd); // length of title
length++;// +1 for termination
DWORD dwPID;
GetWindowThreadProcessId(hWnd, &dwPID);
char *path = NULL;
char *filename = NULL; // without extension
int success = globalForegroundWindow_getPathAndFilenameForPid(dwPID, &path, &filename);
if (!success)
{
outlet_anything(x->outd, SYMnoProcName, 0, NULL);
outlet_anything(x->outc, SYMnoProcName, 0, NULL);
}
else
{
//object_post((t_object*)x, "filename: %s path: %s", filename, path);
outlet_anything(x->outd, gensym(filename), 0, NULL);
outlet_anything(x->outc, gensym(path), 0, NULL);
}
if (filename) sysmem_freeptr(filename);
if (path) sysmem_freeptr(path);
//if (wnd_title) free(wnd_title); wnd_title = NULL;
wnd_title = (wchar_t *)sysmem_resizeptr(wnd_title, length*sizeof(wchar_t));
// get window title
if (!GetWindowTextW(hWnd, wnd_title, length))
{
outlet_anything(x->outb, emptySym, 0, NULL);
}
else
{
int i;
char *maxConform = charset_unicodetoutf8(wnd_title, wcslen(wnd_title), NULL);
// replace all backslashes in title
for (i = 0; i <= strlen(maxConform); i++)
{
if (maxConform[i] == '\\')
{
maxConform[i] = '/';
}
}
outlet_anything(x->outb, gensym(maxConform), 0, NULL);
sysmem_freeptr(maxConform);
}
if (wnd_title) sysmem_freeptr(wnd_title);
//get window rect
RECT myrect;
RECT *rect = &myrect;
GetWindowRect(hWnd, rect);
atom_setlong(x->winLoc + 0, rect->left);
atom_setlong(x->winLoc + 1, rect->top);
atom_setlong(x->winLoc + 2, rect->right - rect->left);
atom_setlong(x->winLoc + 3, rect->bottom - rect->top);
outlet_list(x->outa, NULL, 4, x->winLoc);
//object_post((t_object*)x, "%i %i %i %i ", rect->left, rect->right, rect->top, rect->bottom);
}
#pragma endregion
#pragma region SIZE
void globalForegroundWindow_size(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv)
{
defer_low(x, (method)globalForegroundWindow_do_size, s, argc, argv);
}
void globalForegroundWindow_do_size(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv)
{
int success = false;
if (argc != 2)
{
object_error((t_object*)x, "you need 2 integer arguments");
return;
}
//HWND // get handle of currently active window
HWND hWnd = GetForegroundWindow();
success = SetWindowPos(hWnd, NULL, 0, 0, atom_getlong(argv + 0), atom_getlong(argv + 1), SWP_ASYNCWINDOWPOS | SWP_NOMOVE | SWP_NOZORDER);
if (!success)
object_error((t_object*)x, "Error setting window size!");
}
#pragma endregion
#pragma region POSITION
void globalForegroundWindow_pos(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv)
{
defer_low(x, (method)globalForegroundWindow_do_pos, s, argc, argv);
}
void globalForegroundWindow_do_pos(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv)
{
int success = false;
if (argc != 2)
{
object_error((t_object*)x, "you need 2 integer arguments");
return;
}
POINT point;
point.x = atom_getlong(argv + 0);
point.y = atom_getlong(argv + 1);
HWND hWnd = GetForegroundWindow();
HWND parent = GetAncestor(hWnd, GA_PARENT); // GetParent(hWnd); //
HWND desktop = GetDesktopWindow();
//check if window is child of another window
if (parent != desktop)
{
//object_post((t_object*)x, "positioning a child window");
MapWindowPoints(desktop, parent, (LPPOINT)&point, 1);
success = SetWindowPos(hWnd, NULL, point.x, point.y, 0, 0, SWP_ASYNCWINDOWPOS | SWP_NOSIZE | SWP_NOZORDER);
if (!success)
object_error((t_object*)x, "Error setting window position!");
}
else
{
success = SetWindowPos(hWnd, NULL, point.x, point.y, 0, 0, SWP_ASYNCWINDOWPOS | SWP_NOSIZE | SWP_NOZORDER);
if (!success)
object_error((t_object*)x, "Error setting window position!");
}
}
#pragma endregion
#pragma region GETWINDOWS
/*
getting real path does not work with the newer modern/universal apps because it returns the name of a helper process
WWAHost.exe on Windows 8 and ApplicationFrameHost.exe on Windows 10 rather than the name of the app
https://android.developreference.com/article/17892596/Name+of+process+for+active+window+in+Windows+8+10
*/
void globalForegroundWindow_getwindows(t_globalForegroundWindow *x)
{
defer_low(x, (method)globalForegroundWindow_do_getwindows, NULL, NULL, NULL);
}
void globalForegroundWindow_do_getwindows(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv)
{
outlet_anything(x->outdump, gensym("getwindows"), 0, NULL);
EnumWindows(globalForegroundWindow_enumWindowCallback, x);
outlet_anything(x->outdump, gensym("getwindows_end"), 0, NULL);
}
BOOL CALLBACK globalForegroundWindow_enumWindowCallback(HWND hWnd, LPARAM lparam)
{
t_globalForegroundWindow *x = (t_globalForegroundWindow *)lparam;
t_atom result[8];
int i;
// STATE
if (!IsWindowVisible(hWnd))
{
return TRUE; // we don't list hidden windows
}
else if (IsIconic(hWnd))
atom_setlong(result + 7, 0); // 0 minimized
else if (IsZoomed(hWnd))
atom_setlong(result + 7, 2); // 2 fullscreen
else
{
atom_setlong(result + 7, 1); // 1 visible normal
}
// window class name
/*LPWSTR lpClassName = (wchar_t *)calloc(1024, sizeof(wchar_t));
if (GetClassNameW(hWnd, lpClassName, 1024) )
{
char *maxConform = charset_unicodetoutf8((unsigned short*)lpClassName, wcslen(lpClassName), NULL);
atom_setsym(result + 3, gensym(maxConform));
sysmem_freeptr(maxConform);
}
else
{
atom_setsym(result + 3, gensym("<noClassName>") );
}
free(lpClassName);*/
// ID
//if(id=GetWindowLongPtr(hWnd, GWL_ID))
long id = (long)hWnd;
atom_setlong(result + 0, id);
// TITLE
int length = GetWindowTextLengthW(hWnd);
if (length)
{
length++;// +1 for terminator
wchar_t *buffer = (wchar_t *)sysmem_newptrclear(length*sizeof(wchar_t)); //newptr
GetWindowTextW(hWnd, buffer, length);
char *maxConform = charset_unicodetoutf8(buffer, wcslen(buffer), NULL);
sysmem_freeptr(buffer);
// replace all backslashes in title
for (i = 0; i <= strlen(maxConform); i++)
{
if (maxConform[i] == '\\')
{
maxConform[i] = '/';
}
}
atom_setsym(result + 2, gensym(maxConform));
sysmem_freeptr(maxConform);
}else
atom_setsym(result + 2, SYMnoTitle);
DWORD dwPID;
GetWindowThreadProcessId(hWnd, &dwPID);
// PROCESS NAME
char *path = NULL;
char *filename = NULL; // without extension
int success = globalForegroundWindow_getPathAndFilenameForPid(dwPID, &path, &filename); // newptr
if (success)
{
atom_setsym(result + 1, gensym(filename));
}
else
atom_setsym(result + 1, SYMnoProcName);
if (filename) sysmem_freeptr(filename);
if (path) sysmem_freeptr(path);
// WINDOW COORDINATES
RECT thisWndRect = { 0, 0, 0, 0 };
if (IsIconic(hWnd))
{
// special case: window is minimized
globalForegroundWindow_getRectOfMinimizedWindow(hWnd, &thisWndRect);
}
else
{
GetWindowRect(hWnd, &thisWndRect);
}
atom_setlong(result + 3, thisWndRect.left);
atom_setlong(result + 4, thisWndRect.top);
atom_setlong(result + 5, thisWndRect.right - thisWndRect.left);
atom_setlong(result + 6, thisWndRect.bottom - thisWndRect.top);
//CopyRect(dst,src);
// now output all the infos for this window as a list
outlet_list( x->outdump, NULL, 8, result);
return TRUE;
}
#pragma endregion
#pragma region GETSCREENS
void globalForegroundWindow_getscreens(t_globalForegroundWindow *x)
{
defer_low(x, (method)globalForegroundWindow_do_getscreens, NULL, NULL, NULL);
}
BOOL CALLBACK globalForegroundWindow_theMonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
t_globalForegroundWindow *x = (t_globalForegroundWindow *)dwData;
t_atom as[6];
RECT desktopRect;
MONITORINFO moninfo;
moninfo.cbSize = sizeof(MONITORINFO); // !!
x->screenCount++;
// this always returns the primary screen rect
// GetWindowRect(GetDesktopWindow(), &desktopRect);
GetMonitorInfo(hMonitor, &moninfo);
if (moninfo.dwFlags == MONITORINFOF_PRIMARY)
{
atom_setlong(as + 5, 1);
}
else
atom_setlong(as + 5, 0);
atom_setlong(as + 0, x->screenCount);
atom_setlong(as + 1, lprcMonitor->left);
atom_setlong(as + 2, lprcMonitor->top);
atom_setlong(as + 3, lprcMonitor->right - lprcMonitor->left);
atom_setlong(as + 4, lprcMonitor->bottom - lprcMonitor->top);
// output
outlet_list(x->outdump, NULL, 6, as);
return TRUE;
}
void globalForegroundWindow_do_getscreens(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv)
{
x->screenCount = 0;
//signal start of list
outlet_anything(x->outdump, gensym("getscreens"), 0, NULL);
EnumDisplayMonitors(NULL, NULL, globalForegroundWindow_theMonitorEnumProc, x);
//signal end of list
outlet_anything(x->outdump, gensym("getscreens_end"), 0, NULL);
return;
}
#pragma endregion
#pragma region SET
struct appNameAndTitle {
char * appNameToMatch;
char * titleToMatch;
};
void globalForegroundWindow_set(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv)
{
// check if we have argument/s
if (!argc)
{
object_error((t_object*)x, "you need at least one arg");
object_error((t_object*)x, "'set <window number>' or");
object_error((t_object*)x, "'set <app name>' or");
object_error((t_object*)x, "'set <app name> <window title>'");
return;
}
if (atom_gettype(argv) == A_LONG)
{
defer_low(x, (method)globalForegroundWindow_do_setWithNum, s, argc, argv);
}
else
{
defer_low(x, (method)globalForegroundWindow_do_set, s, argc, argv);
}
}
void globalForegroundWindow_do_set(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv)
{
int titleInput = false;
if (argc>1) titleInput = true;
struct appNameAndTitle matchStruct;
matchStruct.appNameToMatch = NULL;
matchStruct.titleToMatch = NULL;
// get the string from arg 1
matchStruct.appNameToMatch = calloc(strlen(atom_getsym(argv)->s_name) + 1, sizeof(char));
strcpy(matchStruct.appNameToMatch, atom_getsym(argv)->s_name);
if (titleInput)
{
// get the string from arg 2
matchStruct.titleToMatch = calloc(strlen(atom_getsym(argv + 1)->s_name) + 1, sizeof(char));
strcpy(matchStruct.titleToMatch, atom_getsym(argv + 1)->s_name);
int result = EnumWindows(globalForegroundWindow_setWindowWithProcNameAndTitleCallback, &matchStruct);
if (result)
object_error((t_object*)x, "can't find app with name: %s and title: %s", matchStruct.appNameToMatch, matchStruct.titleToMatch);
}
else
{
// enum windows and use first window matching proc name
// it's the highest in z-order, so probably the last used one of this process
int result = EnumWindows(globalForegroundWindow_setWindowWithProcNameCallback, matchStruct.appNameToMatch);
if (result)
object_error((t_object*)x, "can't find app with name: %s", matchStruct.appNameToMatch);
// progman windows?
}
if (matchStruct.appNameToMatch)
free(matchStruct.appNameToMatch);
if (matchStruct.titleToMatch)
free(matchStruct.titleToMatch);
}
void globalForegroundWindow_activateWindow(t_globalForegroundWindow *x, HWND hWnd)
{
//SwitchToThisWindow(hWnd, FALSE); // side effect: puts the current foregr.wind to the back
DWORD fgPID;
if (IsIconic(hWnd))//if minimized
{
ShowWindow(hWnd, SW_RESTORE);
SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
}
GetWindowThreadProcessId(GetForegroundWindow(), &fgPID);
if (fgPID != GetCurrentProcessId())
{
//post("not a window of our process");
HWND hCurWnd = GetForegroundWindow();
DWORD dwMyID = GetCurrentThreadId();
DWORD dwCurID = GetWindowThreadProcessId(hCurWnd, NULL);
AttachThreadInput(dwCurID, dwMyID, TRUE);
SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
SetForegroundWindow(hWnd);
SetFocus(hWnd);
SetActiveWindow(hWnd);
AttachThreadInput(dwCurID, dwMyID, FALSE);
}
else
{
//SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
//SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
SetWindowPos(hWnd, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
SetForegroundWindow(hWnd);
SetFocus(hWnd);
SetActiveWindow(hWnd);
}
}
void globalForegroundWindow_do_setWithNum(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv)
{
HWND hWnd = (HWND)atom_getlong(argv);
if (!IsWindow(hWnd))
{
object_error((t_object *)x, "Could not find the window for num: %i", atom_getlong(argv));
return;
}
globalForegroundWindow_activateWindow(x, hWnd);
}
BOOL CALLBACK globalForegroundWindow_setWindowWithProcNameAndTitleCallback(HWND hWnd, LPARAM lparam)
{
bool result = TRUE;
if (!IsWindowVisible(hWnd))
{
return result; // we don't list hidden windows
}
struct appNameAndTitle* matchStructPtr = lparam;
DWORD dwPID;
GetWindowThreadProcessId(hWnd, &dwPID);
char *path = NULL;
char *filename = NULL; // without extension
int success = globalForegroundWindow_getPathAndFilenameForPid(dwPID, &path, &filename);
if (success)
{
if (!strcmp(matchStructPtr->appNameToMatch, filename))
{
// it's a match
//object_post(NULL, "found visible window of: %s", lparam);
if (GetWindow(hWnd, GW_OWNER) == (HWND)0)
{
// TITLE
int length = GetWindowTextLengthW(hWnd);
if (length)
{
int i;
length++;// +1 for terminator
wchar_t *buffer = (wchar_t *)sysmem_newptrclear(length*sizeof(wchar_t)); //newptr
GetWindowTextW(hWnd, buffer, length);
char *maxConform = charset_unicodetoutf8(buffer, wcslen(buffer), NULL);
sysmem_freeptr(buffer);
// replace all backslashes in title
for (i = 0; i <= strlen(maxConform); i++)
{
if (maxConform[i] == '\\')
{
maxConform[i] = '/';
}
}
// compare
if (!strcmp(matchStructPtr->titleToMatch, maxConform))
{
// it's a match
globalForegroundWindow_activateWindow(NULL, hWnd);
result = FALSE;
}
sysmem_freeptr(maxConform);
}
}
}
}
if (filename) sysmem_freeptr(filename);
if (path) sysmem_freeptr(path);
return result;
}
BOOL CALLBACK globalForegroundWindow_setWindowWithProcNameCallback(HWND hWnd, LPARAM lparam)
{
bool result = TRUE;
if (!IsWindowVisible(hWnd))
{
return result; // we don't list hidden windows
}
DWORD dwPID;
GetWindowThreadProcessId(hWnd, &dwPID);
char *path = NULL;
char *filename = NULL; // without extension
int success = globalForegroundWindow_getPathAndFilenameForPid(dwPID, &path, &filename);
if (success)
{
if (!strcmp(lparam, filename))
{
// it's a match
//object_post(NULL, "found visible window of: %s", lparam);
if (GetWindow(hWnd, GW_OWNER) == (HWND)0)
{
globalForegroundWindow_activateWindow(NULL, hWnd);
result = FALSE;
}
}
}
if (filename) sysmem_freeptr(filename);
if (path) sysmem_freeptr(path);
return result;
}
#pragma endregion
#pragma region ISRUNNING
void globalForegroundWindow_isrunning(t_globalForegroundWindow* x, t_symbol* s, long argc, t_atom* argv)
{
defer_low(x, (method)globalForegroundWindow_do_isrunning, s, argc, argv);
}
void globalForegroundWindow_do_isrunning(t_globalForegroundWindow* x, t_symbol* s, long argc, t_atom* argv)
{
t_atom as[3];
struct appNameAndTitle matchStruct;
if (!argc) return;
matchStruct.appNameToMatch = NULL;
// get the string from arg 1
matchStruct.appNameToMatch = calloc(strlen(atom_getsym(argv)->s_name) + 1, sizeof(char));
strcpy(matchStruct.appNameToMatch, atom_getsym(argv)->s_name);
int result = EnumWindows(globalForegroundWindow_findWindowWithProcNameCallback, matchStruct.appNameToMatch);
if (result)
atom_setlong(as + 2, 0);
else
atom_setlong(as + 2, 1);
atom_setsym(as, gensym("isrunning"));
atom_setsym(as + 1, atom_getsym(argv)); //use input appname sym
outlet_list(x->outdump, 0L, 3, as);
if (matchStruct.appNameToMatch)
free(matchStruct.appNameToMatch);
}
BOOL CALLBACK globalForegroundWindow_findWindowWithProcNameCallback(HWND hWnd, LPARAM lparam)
{
bool result = TRUE;
DWORD dwPID;
GetWindowThreadProcessId(hWnd, &dwPID);
char* path = NULL;
char* filename = NULL; // without extension
int success = globalForegroundWindow_getPathAndFilenameForPid(dwPID, &path, &filename);
if (success)
{
if (!strcmp(lparam, filename))
{
// it's a match
result = FALSE;
}
}
if (filename) sysmem_freeptr(filename);
if (path) sysmem_freeptr(path);
return result;
}
#pragma endregion
#pragma region QUITAPP*
void globalForegroundWindow_quitapp(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv)
{
defer_low(x, (method)globalForegroundWindow_do_quitapp, s, argc, argv);
}
void globalForegroundWindow_do_quitapp(t_globalForegroundWindow *x, t_symbol *s, long argc, t_atom *argv)
{
HWND hWnd = GetForegroundWindow();
if (!argc)
{
DWORD dwPID;
GetWindowThreadProcessId(hWnd, &dwPID);
// reset bg windows of this proc or they won't be visible in enumWindows list
globalForegroundWindow_resetBgWindowsWithPid(x, dwPID);
//the softest way may be to send wm_close to all visible windows of this proc
EnumWindows(globalForegroundWindow_closeWindowsWithPidCallback, dwPID);
return;
}
// we have an arg
char * appNameToMatch = NULL;
// get the string from arg 1
appNameToMatch = calloc(strlen(atom_getsym(argv)->s_name) + 1, sizeof(char));
strcpy(appNameToMatch, atom_getsym(argv)->s_name);
// reset bg windows of this proc or they won't be visible in enumWindows list
globalForegroundWindow_resetBgWindowsWithProcName(x, appNameToMatch);
EnumWindows(globalForegroundWindow_closeWindowsWithProcNameCallback, appNameToMatch);
if (appNameToMatch)
free(appNameToMatch);
return;
}
BOOL CALLBACK globalForegroundWindow_closeWindowsWithPidCallback(HWND hWnd, LPARAM lparam)
{
//HWND desktop = GetDesktopWindow();
//HWND parent = GetAncestor(hWnd, GA_ROOT);
DWORD dwPID;
GetWindowThreadProcessId(hWnd, &dwPID);
if (dwPID == lparam)
{
if (IsWindowVisible(hWnd))
{
//object_post((t_object*)x, "this window is visible");
int success;
if (GetWindow(hWnd, GW_OWNER) == (HWND)0) //better only close the top windows without owner
{
int success = PostMessage(hWnd, WM_SYSCOMMAND, SC_CLOSE, NULL);
if (!success) object_error(NULL, "failed to send close message to window");
}
else
{
;// object_post(NULL, "not closing owned window");
}
}
}
return TRUE;
}
BOOL CALLBACK globalForegroundWindow_closeWindowsWithProcNameCallback(HWND hWnd, LPARAM lparam)
{
if (IsWindowVisible(hWnd))
{
DWORD dwPID;
GetWindowThreadProcessId(hWnd, &dwPID);
char *path = NULL;
char *filename = NULL; // without extension
int success = globalForegroundWindow_getPathAndFilenameForPid(dwPID, &path, &filename);