-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDesktop.cpp
More file actions
747 lines (645 loc) · 28.1 KB
/
Desktop.cpp
File metadata and controls
747 lines (645 loc) · 28.1 KB
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
#include <windows.h>
#include <atlbase.h>
#include <shlobj.h>
#include <winstring.h>
#include <assert.h>
#include <tchar.h>
#include "Win10Desktops.h"
#include "ComUtils.h"
#include "VDNotification.h"
#include <combaseapi.h>
#include <inttypes.h>
#include <cctype>
#include <cwctype>
#include <cstdlib>
#include <cstdio>
template <class T>
typename ATL::CComPtr<T>::_PtrClass* operator-(const ATL::CComPtr<T>& p)
{
return static_cast<typename ATL::CComPtr<T>::_PtrClass*>(p);
}
template <class VD>
void PrintDesktop(VD* pDesktop, UINT dn)
{
GUID guid;
CHECK(pDesktop->GetID(&guid), _T("GetID"), void());
OLECHAR* guidString;
CHECK(StringFromCLSID(guid, &guidString), _T("StringFromCLSID"), void());
HSTRING s = NULL;
CHECK(pDesktop->GetName(&s), _T("GetName"), void());
if (s != nullptr)
_tprintf(_T("%ls \"%ls\"\n"), guidString, WindowsGetStringRawBuffer(s, nullptr));
else
_tprintf(_T("%ls \"Desktop %u\"\n"), guidString, dn);
::CoTaskMemFree(guidString);
CHECK(WindowsDeleteString(s), _T("WindowsDeleteString"), void());
}
template <class VD, class VD2 = VD, class VDMI>
int ShowCurrentDesktop(VDMI* pDesktopManagerInternal)
{
CComPtr<VD> pCurrentDesktop;
CHECK(pDesktopManagerInternal->GetCurrentDesktop(&pCurrentDesktop), _T("GetCurrentDesktop"), EXIT_FAILURE);
CComPtr<IObjectArray> pDesktopArray;
CHECK(pDesktopManagerInternal->GetDesktops(&pDesktopArray), _T("GetDesktops"), EXIT_FAILURE);
UINT dn = 0;
for (CComPtr<VD2> pDesktop : ObjectArrayRange<VD2>(pDesktopArray))
{
if (pDesktop.IsEqualObject(pCurrentDesktop))
{
++dn;
PrintDesktop(-pDesktop, dn);
}
}
return EXIT_SUCCESS;
}
int ShowCurrentDesktop(IServiceProvider* pServiceProvider)
{
CComPtr<Win10::IVirtualDesktopManagerInternal> pDesktopManagerInternal10;
CComPtr<Win11::IVirtualDesktopManagerInternal> pDesktopManagerInternal11;
if (SUCCEEDED(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal10)))
return ShowCurrentDesktop<Win10::IVirtualDesktop, Win10::IVirtualDesktop2>(-pDesktopManagerInternal10);
else if (SUCCEEDED(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal11)))
return ShowCurrentDesktop<Win11::IVirtualDesktop>(-pDesktopManagerInternal11);
else
{
CHECK(false, _T("obtaining CLSID_VirtualDesktopManagerInternal"), EXIT_FAILURE);
return EXIT_SUCCESS;
}
}
int ListViews(IServiceProvider* pServiceProvider)
{
CComPtr<IVirtualDesktopPinnedApps> pVirtualDesktopPinnedApps;
CHECK(pServiceProvider->QueryService(CLSID_VirtualDesktopPinnedApps, &pVirtualDesktopPinnedApps), _T("obtaining IID_IVirtualDesktopPinnedApps"), EXIT_FAILURE);
CComPtr<IApplicationViewCollection> pApplicationViewCollection;
CHECK(pServiceProvider->QueryService(IID_IApplicationViewCollection, &pApplicationViewCollection), _T("obtaining IID_IApplicationViewCollection"), EXIT_FAILURE);
CComPtr<IObjectArray> pViewArray;
CHECK(pApplicationViewCollection->GetViewsByZOrder(&pViewArray), _T("GetViewsByZOrder"), EXIT_FAILURE);
for (CComPtr<IApplicationView> pView : ObjectArrayRange<IApplicationView>(pViewArray))
{
BOOL bShowInSwitchers = FALSE;
CHECK(pView->GetShowInSwitchers(&bShowInSwitchers), _T("GetShowInSwitchers"), EXIT_FAILURE);
if (!bShowInSwitchers)
continue;
HWND hWnd;
CHECK(pView->GetThumbnailWindow(&hWnd), _T("GetThumbnailWindow"), EXIT_FAILURE);
GUID guid;
CHECK(pView->GetVirtualDesktopId(&guid), _T("GetVirtualDesktopId"), EXIT_FAILURE);
OLECHAR* guidString;
CHECK(StringFromCLSID(guid, &guidString), _T("StringFromCLSID"), EXIT_FAILURE);
BOOL bPinned = FALSE;
CHECK(pVirtualDesktopPinnedApps->IsViewPinned(pView, &bPinned), _T("IsViewPinned"), EXIT_FAILURE);
TCHAR title[256];
GetWindowText(hWnd, title, ARRAYSIZE(title));
_tprintf(_T("0x%08") _T(PRIXPTR) _T(" %c %ls %s\n"), reinterpret_cast<uintptr_t>(hWnd), bPinned ? _T('*') : _T(' '), guidString, title);
::CoTaskMemFree(guidString);
}
return EXIT_SUCCESS;
}
template <class VD, class VDMI>
int ListDesktops(IServiceProvider* pServiceProvider, VDMI* pDesktopManagerInternal, bool bShowViews)
{
CComPtr<IObjectArray> pDesktopArray;
CHECK(pDesktopManagerInternal->GetDesktops(&pDesktopArray), _T("GetDesktops"), EXIT_FAILURE);
if (bShowViews)
{
CComPtr<IApplicationViewCollection> pApplicationViewCollection;
CHECK(pServiceProvider->QueryService(IID_IApplicationViewCollection, &pApplicationViewCollection), _T("obtaining IID_IApplicationViewCollection"), EXIT_FAILURE);
CComPtr<IObjectArray> pViewArray;
CHECK(pApplicationViewCollection->GetViewsByZOrder(&pViewArray), _T("GetViewsByZOrder"), EXIT_FAILURE);
CComPtr<IVirtualDesktopPinnedApps> pVirtualDesktopPinnedApps;
CHECK(pServiceProvider->QueryService(CLSID_VirtualDesktopPinnedApps, &pVirtualDesktopPinnedApps), _T("obtaining IID_IVirtualDesktopPinnedApps"), EXIT_FAILURE);
UINT dn = 0;
for (CComPtr<VD> pDesktop : ObjectArrayRange<VD>(pDesktopArray))
{
++dn;
PrintDesktop(-pDesktop, dn);
if (pViewArray)
{
for (CComPtr<IApplicationView> pView : ObjectArrayRangeRev<IApplicationView>(pViewArray))
{
BOOL bShowInSwitchers = FALSE;
CHECK(pView->GetShowInSwitchers(&bShowInSwitchers), _T("GetShowInSwitchers"), EXIT_FAILURE);
if (!bShowInSwitchers)
continue;
BOOL bVisible = FALSE;
CHECK(pDesktop->IsViewVisible(pView, &bVisible), _T("IsViewVisible"), EXIT_FAILURE);
if (!bVisible)
continue;
HWND hWnd;
CHECK(pView->GetThumbnailWindow(&hWnd), _T("GetThumbnailWindow"), EXIT_FAILURE);
BOOL bPinned = FALSE;
CHECK(pVirtualDesktopPinnedApps->IsViewPinned(pView, &bPinned), _T("IsViewPinned"), EXIT_FAILURE);
TCHAR title[256];
GetWindowText(hWnd, title, ARRAYSIZE(title));
_tprintf(_T(" 0x%08") _T(PRIXPTR) _T(" %c %s\n"), reinterpret_cast<uintptr_t>(hWnd), bPinned ? _T('*') : _T(' '), title);
}
}
}
}
else
{
UINT dn = 0;
for (CComPtr<VD> pDesktop : ObjectArrayRange<VD>(pDesktopArray))
{
++dn;
PrintDesktop(-pDesktop, dn);
}
}
return EXIT_SUCCESS;
}
int ListDesktops(IServiceProvider* pServiceProvider, bool bShowViews)
{
CComPtr<Win10::IVirtualDesktopManagerInternal> pDesktopManagerInternal10;
CComPtr<Win11::IVirtualDesktopManagerInternal> pDesktopManagerInternal11;
if (SUCCEEDED(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal10)))
return ListDesktops<Win10::IVirtualDesktop2>(pServiceProvider, -pDesktopManagerInternal10, bShowViews);
else if (SUCCEEDED(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal11)))
return ListDesktops<Win11::IVirtualDesktop>(pServiceProvider, -pDesktopManagerInternal11, bShowViews);
else
{
CHECK(false, _T("obtaining CLSID_VirtualDesktopManagerInternal"), EXIT_FAILURE);
return EXIT_SUCCESS;
}
}
int PinView(IServiceProvider* pServiceProvider, HWND hWnd, BOOL bPin)
{
CComPtr<IVirtualDesktopPinnedApps> pVirtualDesktopPinnedApps;
CHECK(pServiceProvider->QueryService(CLSID_VirtualDesktopPinnedApps, &pVirtualDesktopPinnedApps), _T("obtaining IID_IVirtualDesktopPinnedApps"), EXIT_FAILURE);
CComPtr<IApplicationViewCollection> pApplicationViewCollection;
CHECK(pServiceProvider->QueryService(IID_IApplicationViewCollection, &pApplicationViewCollection), _T("obtaining IID_IApplicationViewCollection"), EXIT_FAILURE);
CComPtr<IApplicationView> pView;
CHECK(pApplicationViewCollection->GetViewForHwnd(hWnd, &pView), _T("obtaining GetViewForHwnd"), EXIT_FAILURE);
if (bPin)
{
CHECK(pVirtualDesktopPinnedApps->PinView(pView), _T("PinView"), EXIT_FAILURE);
}
else
{
CHECK(pVirtualDesktopPinnedApps->UnpinView(pView), _T("UnpinView"), EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
HWND GetWindow(const TCHAR* s)
{
// TODO Support
// by title
if (s == nullptr)
return NULL;
else if (_tcsicmp(s, _T("{cursor}")) == 0)
{
POINT pt;
GetCursorPos(&pt);
return WindowFromPoint(pt);
}
else if (_tcsicmp(s, _T("{console}")) == 0)
return GetConsoleWindow();
else if (_tcsicmp(s, _T("{foreground}")) == 0)
return GetForegroundWindow();
else
return static_cast<HWND>(UlongToHandle(_tcstoul(s, nullptr, 0)));
}
template <class VD, class VDMI>
CComPtr<VD> GetAdjacentDesktop(VDMI* pDesktopManagerInternal, AdjacentDesktop uDirection)
{
CComPtr<VD> pDesktop;
CHECK(pDesktopManagerInternal->GetCurrentDesktop(&pDesktop), _T("GetCurrentDesktop"), {});
CComPtr<VD> pAdjacentDesktop;
CHECK(pDesktopManagerInternal->GetAdjacentDesktop(pDesktop, uDirection, &pAdjacentDesktop), _T("GetAdjacentDesktop"), {});
return pAdjacentDesktop;
}
template <class VD, class VD2 = VD, class VDMI>
CComPtr<VD> GetDesktop(VDMI* pDesktopManagerInternal, const LPCTSTR sDesktop)
{
// TODO Support
// by guid
if (sDesktop == nullptr)
return {};
else if (_tcsicmp(sDesktop, _T("{current}")) == 0)
{
CComPtr<VD> pDesktop;
CHECK(pDesktopManagerInternal->GetCurrentDesktop(&pDesktop), _T("GetCurrentDesktop"), {});
return pDesktop;
}
else if (_tcsicmp(sDesktop, _T("{prev}")) == 0)
return GetAdjacentDesktop<VD>(pDesktopManagerInternal, LeftDirection);
else if (_tcsicmp(sDesktop, _T("{next}")) == 0)
return GetAdjacentDesktop<VD>(pDesktopManagerInternal, RightDirection);
else if (std::_istdigit(sDesktop[0]))
{
int i = _tstoi(sDesktop) - 1;
CComPtr<IObjectArray> pDesktopArray;
CHECK(pDesktopManagerInternal->GetDesktops(&pDesktopArray), _T("GetDesktops"), {});
CComPtr<VD> pDesktop;
CHECK(pDesktopArray->GetAt(i, IID_PPV_ARGS(&pDesktop)), _T("GetAt"), {});
return pDesktop;
}
else
{
#ifdef UNICODE
LPCWSTR wDesktop = sDesktop;
#else
WCHAR wDesktop[100];
MultiByteToWideChar(CP_UTF8, 0, sDesktop, -1, wDesktop, ARRAYSIZE(wDesktop));
#endif
CComPtr<VD> pRetDesktop;
CComPtr<IObjectArray> pDesktopArray;
CHECK(pDesktopManagerInternal->GetDesktops(&pDesktopArray), _T("GetDesktops"), {});
UINT dn = 0;
for (CComPtr<VD2> pDesktop : ObjectArrayRange<VD2>(pDesktopArray))
{
++dn;
HSTRING s = NULL;
CHECK(pDesktop->GetName(&s), _T("GetName"), {});
if (s == nullptr)
{
WCHAR wNumDesktop[100];
swprintf_s(wNumDesktop, L"Desktop %d", dn);
if (_wcsicmp(wNumDesktop, wDesktop) == 0)
{
pRetDesktop = pDesktop;
break;
}
}
else if (_wcsicmp(WindowsGetStringRawBuffer(s, nullptr), wDesktop) == 0)
{
pRetDesktop = pDesktop;
break;
}
}
return pRetDesktop;
}
}
template <class VD, class VDMI>
int DoSwitchDesktop(VDMI* pDesktopManagerInternal, VD* pDesktop)
{
if (pDesktop)
{
CHECK(pDesktopManagerInternal->SwitchDesktop(pDesktop), _T("SwitchDesktop"), EXIT_FAILURE);
return EXIT_SUCCESS;
}
else
{
_ftprintf(stderr, _T("Unknown desktop\n"));
return EXIT_FAILURE;
}
}
int SwitchDesktop(IServiceProvider* pServiceProvider, LPCTSTR strDesktop)
{
CComPtr<Win10::IVirtualDesktopManagerInternal> pDesktopManagerInternal10;
CComPtr<Win11::IVirtualDesktopManagerInternal> pDesktopManagerInternal11;
//CHECK(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal), _T("obtaining CLSID_VirtualDesktopManagerInternal"), EXIT_FAILURE);
if (SUCCEEDED(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal10)))
return DoSwitchDesktop(-pDesktopManagerInternal10, -GetDesktop<Win10::IVirtualDesktop, Win10::IVirtualDesktop2>(-pDesktopManagerInternal10, strDesktop));
else if (SUCCEEDED(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal11)))
return DoSwitchDesktop(-pDesktopManagerInternal11, -GetDesktop<Win11::IVirtualDesktop>(-pDesktopManagerInternal11, strDesktop));
else
{
CHECK(false, _T("obtaining CLSID_VirtualDesktopManagerInternal"), EXIT_FAILURE);
return EXIT_SUCCESS;
}
}
template <class VD, class VDMI>
int RenameDesktop(VDMI* pDesktopManagerInternal, VD* pDesktop, LPCTSTR strName)
{
if (pDesktop)
{
HSTRING hName = NULL;
CHECK(WindowsCreateString(strName, UINT32(_tcslen(strName)), &hName), _T("WindowsCreateString"), EXIT_FAILURE);
CHECK(pDesktopManagerInternal->SetDesktopName(pDesktop, hName), _T("SwitchDesktop"), EXIT_FAILURE);
CHECK(WindowsDeleteString(hName), _T("WindowsDeleteString"), EXIT_FAILURE);
return EXIT_SUCCESS;
}
else
{
_ftprintf(stderr, _T("Unknown desktop\n"));
return EXIT_FAILURE;
}
}
int RenameDesktop(IServiceProvider* pServiceProvider, LPCTSTR strDesktop, LPCTSTR strName)
{
CComPtr<Win10::IVirtualDesktopManagerInternal2> pDesktopManagerInternal10;
CComPtr<Win11::IVirtualDesktopManagerInternal> pDesktopManagerInternal11;
//CHECK(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal), _T("obtaining CLSID_VirtualDesktopManagerInternal"), EXIT_FAILURE);
if (SUCCEEDED(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal10)))
return RenameDesktop(-pDesktopManagerInternal10, -GetDesktop<Win10::IVirtualDesktop, Win10::IVirtualDesktop2>(-pDesktopManagerInternal10, strDesktop), strName);
else if (SUCCEEDED(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal11)))
return RenameDesktop(-pDesktopManagerInternal11, -GetDesktop<Win11::IVirtualDesktop>(-pDesktopManagerInternal11, strDesktop), strName);
else
{
CHECK(false, _T("obtaining CLSID_VirtualDesktopManagerInternal"), EXIT_FAILURE);
return EXIT_SUCCESS;
}
}
template <class VD, class VD2 = VD, class VDMI>
int CreateDesktop(VDMI* pDesktopManagerInternal, LPCTSTR strName)
{
CComPtr<VD> pNewDesktop;
CComPtr<VD> pOtherDesktop;
CHECK(pDesktopManagerInternal->CreateDesktop(&pNewDesktop), _T("CreateDesktop"), EXIT_FAILURE);
if (strName != nullptr)
{
CComQIPtr<VD2> pNewDesktop2(pNewDesktop);
return RenameDesktop(pDesktopManagerInternal, -pNewDesktop2, strName);
}
else
return EXIT_SUCCESS;
}
int CreateDesktop(IServiceProvider* pServiceProvider, LPCTSTR strName)
{
CComPtr<Win10::IVirtualDesktopManagerInternal2> pDesktopManagerInternal10;
CComPtr<Win11::IVirtualDesktopManagerInternal> pDesktopManagerInternal11;
//CHECK(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal), _T("obtaining CLSID_VirtualDesktopManagerInternal"), EXIT_FAILURE);
if (SUCCEEDED(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal10)))
return CreateDesktop<Win10::IVirtualDesktop, Win10::IVirtualDesktop2>(-pDesktopManagerInternal10, strName);
else if (SUCCEEDED(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal11)))
return CreateDesktop<Win11::IVirtualDesktop>(-pDesktopManagerInternal11, strName);
else
{
CHECK(false, _T("obtaining CLSID_VirtualDesktopManagerInternal"), EXIT_FAILURE);
return EXIT_SUCCESS;
}
}
template <class VD, class VDMI>
int RemoveDesktop(VDMI* pDesktopManagerInternal, VD* pDesktop)
{
if (pDesktop)
{
CComPtr<VD> pFallbackDesktop;
CHECK(pDesktopManagerInternal->GetCurrentDesktop(&pFallbackDesktop), _T("GetCurrentDesktop"), EXIT_FAILURE);
if (pFallbackDesktop.IsEqualObject(pDesktop))
{
if (FAILED(pDesktopManagerInternal->GetAdjacentDesktop(pDesktop, LeftDirection, &pFallbackDesktop)))
pDesktopManagerInternal->GetAdjacentDesktop(pDesktop, RightDirection, &pFallbackDesktop);
}
CHECK(pDesktopManagerInternal->RemoveDesktop(pDesktop, pFallbackDesktop), _T("RemoveDesktop"), EXIT_FAILURE);
return EXIT_SUCCESS;
}
else
{
_ftprintf(stderr, _T("Unknown desktop\n"));
return EXIT_FAILURE;
}
}
int RemoveDesktop(IServiceProvider* pServiceProvider, LPCTSTR strDesktop)
{
CComPtr<Win10::IVirtualDesktopManagerInternal> pDesktopManagerInternal10;
CComPtr<Win11::IVirtualDesktopManagerInternal> pDesktopManagerInternal11;
//CHECK(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal), _T("obtaining CLSID_VirtualDesktopManagerInternal"), EXIT_FAILURE);
if (SUCCEEDED(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal10)))
return RemoveDesktop(-pDesktopManagerInternal10, -GetDesktop<Win10::IVirtualDesktop, Win10::IVirtualDesktop2>(-pDesktopManagerInternal10, strDesktop));
else if (SUCCEEDED(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal11)))
return RemoveDesktop(-pDesktopManagerInternal11, -GetDesktop<Win11::IVirtualDesktop>(-pDesktopManagerInternal11, strDesktop));
else
{
CHECK(false, _T("obtaining CLSID_VirtualDesktopManagerInternal"), EXIT_FAILURE);
return EXIT_SUCCESS;
}
}
class VDNotification: public IVDNotification
{
public:
// Win10::IVirtualDesktopNotification
virtual void VirtualDesktopCreated(Win10::IVirtualDesktop* pDesktop) override
{
CComQIPtr<Win10::IVirtualDesktop2> pDesktop2(pDesktop);
_tprintf(_T("VirtualDesktopCreated\n "));
PrintDesktop(-pDesktop2, 0);
}
virtual void VirtualDesktopDestroyed(Win10::IVirtualDesktop* pDesktopDestroyed, Win10::IVirtualDesktop* pDesktopFallback) override
{
CComQIPtr<Win10::IVirtualDesktop2> pDesktopDestroyed2(pDesktopDestroyed);
CComQIPtr<Win10::IVirtualDesktop2> pDesktopFallback2(pDesktopFallback);
_tprintf(_T("VirtualDesktopDestroyed\n Destroyed: "));
PrintDesktop(-pDesktopDestroyed2, 0);
_tprintf(_T(" Fallback: "));
PrintDesktop(-pDesktopFallback2, 0);
}
virtual void CurrentVirtualDesktopChanged(Win10::IVirtualDesktop* pDesktopOld, Win10::IVirtualDesktop* pDesktopNew) override
{
CComQIPtr<Win10::IVirtualDesktop2> pDesktopOld2(pDesktopOld);
CComQIPtr<Win10::IVirtualDesktop2> pDesktopNew2(pDesktopNew);
_tprintf(_T("CurrentVirtualDesktopChanged\n Old: "));
PrintDesktop(-pDesktopOld2, 0);
_tprintf(_T(" New: "));
PrintDesktop(-pDesktopNew2, 0);
}
// Win10::IVirtualDesktopNotification2
virtual void VirtualDesktopNameChanged(Win10::IVirtualDesktop* pDesktop, HSTRING name) override
{
CComQIPtr<Win10::IVirtualDesktop2> pDesktop2(pDesktop);
_tprintf(_T("VirtualDesktopNameChanged\n "));
PrintDesktop(-pDesktop2, 0);
_tprintf(_T(" Name:s %ls\n"), WindowsGetStringRawBuffer(name, nullptr));
}
// Win11::IVirtualDesktopNotification
virtual void VirtualDesktopCreated(Win11::IVirtualDesktop* pDesktop) override
{
_tprintf(_T("VirtualDesktopCreated\n "));
PrintDesktop(pDesktop, 0);
}
virtual void VirtualDesktopDestroyed(Win11::IVirtualDesktop* pDesktopDestroyed, Win11::IVirtualDesktop* pDesktopFallback) override
{
_tprintf(_T("VirtualDesktopDestroyed\n Destroyed: "));
PrintDesktop(pDesktopDestroyed, 0);
_tprintf(_T(" Fallback: "));
PrintDesktop(pDesktopFallback, 0);
}
virtual void VirtualDesktopMoved(Win11::IVirtualDesktop* pDesktop, int64_t oldIndex, int64_t newIndex) override
{
_tprintf(_T("VirtualDesktopMoved\n "));
PrintDesktop(pDesktop, 0);
_tprintf(_T(" From: %d To: %d\n"), static_cast<int>(oldIndex), static_cast<int>(newIndex));
}
virtual void VirtualDesktopNameChanged(Win11::IVirtualDesktop* pDesktop, HSTRING name) override
{
_tprintf(_T("VirtualDesktopNameChanged\n "));
PrintDesktop(pDesktop, 0);
_tprintf(_T(" Name:s %ls\n"), WindowsGetStringRawBuffer(name, nullptr));
}
virtual void CurrentVirtualDesktopChanged(Win11::IVirtualDesktop* pDesktopOld, Win11::IVirtualDesktop* pDesktopNew) override
{
_tprintf(_T("CurrentVirtualDesktopChanged\nOld: "));
PrintDesktop(pDesktopOld, 0);
_tprintf(_T(" New: "));
PrintDesktop(pDesktopNew, 0);
}
virtual void VirtualDesktopWallpaperChanged(Win11::IVirtualDesktop* pDesktop, HSTRING name) override
{
_tprintf(_T("VirtualDesktopWallpaperChanged\n "));
PrintDesktop(pDesktop, 0);
_tprintf(_T(" Path:s %ls\n"), WindowsGetStringRawBuffer(name, nullptr));
}
virtual void VirtualDesktopSwitched(Win11::IVirtualDesktop* pDesktop, enum VirtualDesktopSwitchType type) override
{
_tprintf(_T("VirtualDesktopSwitched\n "));
PrintDesktop(pDesktop, 0);
_tprintf(_T(" Type: %d\n"), static_cast<int>(type));
}
virtual void RemoteVirtualDesktopConnected(Win11::IVirtualDesktop* pDesktop) override
{
_tprintf(_T("RemoteVirtualDesktopConnected\n "));
PrintDesktop(pDesktop, 0);
}
};
UINT DoMessageLoop()
{
MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.message;
}
int WatchDesktops(IServiceProvider* pServiceProvider)
{
CComPtr<Win10::IVirtualDesktopManagerInternal> pDesktopManagerInternal10;
CComPtr<Win11::IVirtualDesktopManagerInternal> pDesktopManagerInternal11;
//CHECK(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal), _T("obtaining CLSID_VirtualDesktopManagerInternal"), EXIT_FAILURE);
VDNotification TheVDNotification;
DWORD idVirtualDesktopNotification = 0;
CComPtr<VirtualDesktopNotification> pNotify = new VirtualDesktopNotification(&TheVDNotification);
if (SUCCEEDED(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal10)))
{
CComPtr<Win10::IVirtualDesktopNotificationService> pVirtualNotificationService;
CHECK(pServiceProvider->QueryService(CLSID_VirtualNotificationService, &pVirtualNotificationService), _T("creating CLSID_IVirtualNotificationService"), EXIT_FAILURE);
if (pVirtualNotificationService)
CHECK(pVirtualNotificationService->Register(pNotify, &idVirtualDesktopNotification), _T("Register DesktopNotificationService"), EXIT_FAILURE);
DoMessageLoop();
CHECK(pVirtualNotificationService->Unregister(idVirtualDesktopNotification), _T("Unregister DesktopNotificationService"), EXIT_FAILURE);
return EXIT_SUCCESS;
}
else if (SUCCEEDED(pServiceProvider->QueryService(CLSID_VirtualDesktopManagerInternal, &pDesktopManagerInternal11)))
{
CComPtr<Win11::IVirtualDesktopNotificationService> pVirtualNotificationService;
CHECK(pServiceProvider->QueryService(CLSID_VirtualNotificationService, &pVirtualNotificationService), _T("creating CLSID_IVirtualNotificationService"), EXIT_FAILURE);
if (pVirtualNotificationService)
CHECK(pVirtualNotificationService->Register(pNotify, &idVirtualDesktopNotification), _T("Register DesktopNotificationService"), EXIT_FAILURE);
DoMessageLoop();
CHECK(pVirtualNotificationService->Unregister(idVirtualDesktopNotification), _T("Unregister DesktopNotificationService"), EXIT_FAILURE);
return EXIT_SUCCESS;
}
else
{
CHECK(false, _T("obtaining CLSID_VirtualDesktopManagerInternal"), EXIT_FAILURE);
return EXIT_SUCCESS;
}
}
const TCHAR* GetNextArg(int argc, const TCHAR* const argv[], int* arg)
{
if (argc > *arg)
return argv[++(*arg)];
else
return nullptr;
}
template <class T>
class AutoCall
{
public:
AutoCall(T* f)
: f(f)
{
}
~AutoCall()
{
f();
}
private:
T* f;
};
template <class T>
AutoCall<T> MakeAutoCall(T* f)
{
return AutoCall<T>(f);
};
int _tmain(const int argc, const TCHAR* const argv[])
{
CHECK(CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED), _T("CoInitialize"), EXIT_FAILURE);
auto AutoCoUninitialize(MakeAutoCall(CoUninitialize));
CComPtr<IServiceProvider> pServiceProvider;
CHECK(pServiceProvider.CoCreateInstance(CLSID_ImmersiveShell, NULL, CLSCTX_LOCAL_SERVER), _T("creating CLSID_ImmersiveShell"), EXIT_FAILURE);
int arg = 0;
const TCHAR* cmd = GetNextArg(argc, argv, &arg);
bool showusage = true;
if (cmd != nullptr)
{
if (_tcsicmp(cmd, _T("current")) == 0)
return ShowCurrentDesktop(pServiceProvider);
else if (_tcsicmp(cmd, _T("views")) == 0)
return ListViews(pServiceProvider);
else if (_tcsicmp(cmd, _T("list")) == 0)
{
const TCHAR* showviews = GetNextArg(argc, argv, &arg);
bool bshowviews = showviews != nullptr && _tcsicmp(showviews, _T("/views")) == 0;
return ListDesktops(pServiceProvider, bshowviews);
}
else if (_tcsicmp(cmd, _T("pin")) == 0)
{
const TCHAR* wnd = GetNextArg(argc, argv, &arg);
if (wnd != nullptr)
return PinView(pServiceProvider, GetWindow(wnd), TRUE);
}
else if (_tcsicmp(cmd, _T("unpin")) == 0)
{
const TCHAR* wnd = GetNextArg(argc, argv, &arg);
if (wnd != nullptr)
return PinView(pServiceProvider, GetWindow(wnd), FALSE);
}
else if (_tcsicmp(cmd, _T("switch")) == 0)
{
const TCHAR* desktop = GetNextArg(argc, argv, &arg);
if (desktop != nullptr)
return SwitchDesktop(pServiceProvider, desktop);
}
else if (_tcsicmp(cmd, _T("create")) == 0)
{
const TCHAR* name = GetNextArg(argc, argv, &arg);
return CreateDesktop(pServiceProvider, name);
}
else if (_tcsicmp(cmd, _T("remove")) == 0)
{
const TCHAR* desktop = GetNextArg(argc, argv, &arg);
if (desktop != nullptr)
return RemoveDesktop(pServiceProvider, desktop);
}
else if (_tcsicmp(cmd, _T("rename")) == 0)
{
const TCHAR* desktop = GetNextArg(argc, argv, &arg);
const TCHAR* name = GetNextArg(argc, argv, &arg);
if (desktop != nullptr && name != nullptr)
return RenameDesktop(pServiceProvider, desktop, name);
}
else if (_tcsicmp(cmd, _T("watch")) == 0)
{
return WatchDesktops(pServiceProvider);
}
else
{
_ftprintf(stderr, _T("Unknown cmd: %s\n"), cmd);
return EXIT_FAILURE;
}
}
if (showusage)
{
_tprintf(_T("Desktop [cmd] <options>\n\n"));
_tprintf(_T("where [cmd] is one of:\n"));
_tprintf(_T(" current\n"));
_tprintf(_T(" list [/views]\n"));
_tprintf(_T(" views\n"));
_tprintf(_T(" pin <HWND>\n"));
_tprintf(_T(" unpin <HWND>\n"));
_tprintf(_T(" switch <desktop>\n"));
_tprintf(_T(" rename <desktop> <name>\n"));
_tprintf(_T(" create <name>\n"));
_tprintf(_T(" remove <desktop>\n"));
_tprintf(_T(" watch\n"));
_tprintf(_T("\n"));
_tprintf(_T("where <desktop> is one of:\n"));
_tprintf(_T(" {current}\n"));
_tprintf(_T(" {prev}\n"));
_tprintf(_T(" {next}\n"));
_tprintf(_T(" \"desktop number\"\n"));
_tprintf(_T(" \"desktop name\"\n"));
}
return EXIT_SUCCESS;
}