-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathglobals.cpp
1714 lines (1386 loc) · 45.5 KB
/
globals.cpp
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
/*!=========================================================================
//
// Program: ZMatrix
// Module: $RCSfile: globals.cpp,v $
// Language: C/C++
// Date: $Date: 2003/04/13 22:01:43 $
// Version: $Revision: 1.29 $
//
// Copyright (c) 2001-2002 Z. Shaker
// All rights reserved.
// See License.txt for details.
//
// This file is part of ZMatrix.
//
// ZMatrix is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// ZMatrix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ZMatrix; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//=========================================================================*/
#include "globals.h"
#include "RegistryListenerThread.h"
#include "TopLevelListenerWindow.h"
#include <wininet.h>
#include <shlguid.h>
#include <shlobj.h>
unsigned int RefreshTime = 50;
HANDLE DESKTOPREDRAW_Event = CreateEvent(NULL,FALSE,FALSE,DESKTOPREDRAW_EVENT_ID);
_TCHAR szWinName[] = _TEXT("ZMatrix");
_TCHAR DummyMatrixWindowClassName[] = _TEXT("DummyZMatrixWindowClass");
unsigned int gscreenWidth = 0;
unsigned int gscreenHeight = 0;
int gscreenTop = 0;
int gscreenLeft = 0;
HDC ghdc;
HMENU gSysTrayMenu;
HMENU gSysTrayPopup;
HINSTANCE ghInstance;
HWND ghWnd = NULL;
HWND ghProgman = NULL;
HWND WorkerW = NULL;
HWND ghShellDLL = NULL;
HWND ghSysListView = NULL;
bool LiteStepMode = false;
NOTIFYICONDATA IconData;
HRGN ValidRGN = NULL;
IzsMatrix *PreScreenSaveMatrixObject = NULL;
bool PreScreenSavePauseState = false;
unsigned int PreScreenSaveRefreshTime = 50;
DWORD PreScreenSavePriorityClass = IDLE_PRIORITY_CLASS;
WNDCLASSEX ScreenSaverDummyClass = {
/* Define a window class */
sizeof(WNDCLASSEX),
0,
DefWindowProc,
0,
0,
ghInstance,
LoadIcon(NULL, IDI_WINLOGO),
LoadCursor(NULL,IDC_ARROW),
(HBRUSH)GetStockObject(NULL_BRUSH),
NULL,
_TEXT("DummyZMatrixWindowClass"),
LoadIcon(NULL, IDI_WINLOGO)
};
HWND ScreenSaverDummyWindow = NULL;
IzsMatrix *MatrixObject = NULL;
bool Paused = false;
bool DesktopIsCleared = false;
bool WallpaperIsCleared = false;
bool DesktopColorIsCleared = false;
bool OrigDesktopColorIsValid = false;
COLORREF OrigDesktopColor = GetSysColor(COLOR_DESKTOP);
bool OrigWallpaperIsValid = false;
_tstring OrigWallpaperString;
bool OrigActiveDesktopWallpaperIsValid = false;
widestring OrigActiveDesktopWallpaperString;
TCHAR AllUsersStartupDirectoryPath[MAX_PATH];
_tstring AllUsersStartupShortcutPath;
TCHAR CurrentUserStartupDirectoryPath[MAX_PATH];
_tstring CurrentUserStartupShortcutPath;
_tstring MatrixCommandLine;
TCHAR CurrentUserAppDataDirectoryPath[MAX_PATH];
_tstring AppConfigDirectoryPath;
_tstring AppConfigFilePath;
_tstring AppScreenSaverConfigFilePath;
_tstring AppMiscConfigFilePath;
bool ReadyToScreenSave = false;
bool OutstandingScreenSaveRequest = false;
bool InScreenSaveMode = false;
bool UnSetSSWhenDone = false;
vector<OutstandingScreenSaveRequestPair> OutstandingScreenSaveRequestParams;
POINT MouseScreenPointAtScreenSaveStart = {0,0};
bool AlwaysSetAsScreenSaverWhileRunning = false;
bool BlendScreenSaverWithBGOnly = false;
typedef pair<HWND,UINT> NotificationPair;
vector<NotificationPair> WindowsToNotifyWhenSSDone;
deque<IgnoredWallpaperChangeElement> WallpaperChangesToIgnore;
deque<IgnoredBGColorChangeElement> BGColorChangesToIgnore;
int SelfPostedDesktopChildAttached = 0;
vector<HWND> MinimizedWindows;
static bool AlreadyInConfig = false;
//===========================================================================
//===========================================================================
static int getNumColors( int nBits )
{
switch ( nBits ) {
case 1 : return 2;
case 4 : return 16;
case 8 : return 256;
case 24: return 0;
// default:
// assert( false );
} return 0;
}
//===========================================================================
//===========================================================================
static BITMAP *getInfo( HBITMAP hbmp )
{
static BITMAP bitmap = { { 0 } };
GetObject( hbmp, sizeof bitmap, &bitmap );
return &bitmap;
}
//===========================================================================
//===========================================================================
bool SaveBitmap( HBITMAP hbmp, LPCTSTR pszFile )
{
const BITMAP *pBitmap = getInfo( hbmp );
const int nNumColors = getNumColors( pBitmap->bmBitsPixel );
const UINT nPalSize = nNumColors * sizeof( RGBQUAD );
const long lBytes = pBitmap->bmHeight *
MulDiv( 4, pBitmap->bmWidthBytes + 3, 4 );
BYTE *pImage = (BYTE *) malloc( lBytes );
if ( 0 == pImage ) {
return false;
}
BITMAPINFOHEADER bitmapInfoHeader = {
sizeof( BITMAPINFOHEADER ),
pBitmap->bmWidth,
pBitmap->bmHeight,
1,
pBitmap->bmBitsPixel,
BI_RGB,
lBytes,
0, 0,
//1 << pBitmap->bmBitsPixel,
};
const int nHeaderSize = sizeof( BITMAPFILEHEADER ) +
sizeof( BITMAPINFOHEADER ) + nPalSize;
const BITMAPFILEHEADER bitmapFileHeader = {
MAKEWORD( 'B', 'M' ), nHeaderSize + lBytes, 0, 0, nHeaderSize,
};
HDC hdc = GetDC( 0 );
const BOOL bOK = GetDIBits(
hdc, hbmp, 0, (WORD) pBitmap->bmHeight, pImage,
(BITMAPINFO *) &bitmapInfoHeader, DIB_RGB_COLORS );
ReleaseDC( 0, hdc );
if ( !bOK ) {
return false;
}
HANDLE h = CreateFile( pszFile,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ, 0, CREATE_ALWAYS, 0, 0 );
if ( INVALID_HANDLE_VALUE == h ) {
return false;
}
DWORD dwBytesWritten = 0;
WriteFile( h, &bitmapFileHeader, sizeof bitmapFileHeader,
&dwBytesWritten, 0 );
WriteFile( h, &bitmapInfoHeader, sizeof bitmapInfoHeader,
&dwBytesWritten, 0 );
if ( 0 < nPalSize ) { // Note -- this has never been tested!
return false;
MB("Palette will be wrong!");
const BOOL hasPalette = IsClipboardFormatAvailable( CF_PALETTE );
RGBQUAD rgb[ 256 ] = { 0 };
PALETTEENTRY pe [ 256 ] = { 0 };
// assert( nPalSize <= 256 );
HPALETTE hpalette = 0;
const UINT nColors = GetPaletteEntries( hpalette, 0, nPalSize, pe );
// assert( nColors == nPalSize );
for ( UINT iColor = 0; iColor < nColors; iColor++) {
rgb[ iColor ].rgbRed = pe[ iColor ].peRed ;
rgb[ iColor ].rgbGreen = pe[ iColor ].peGreen;
rgb[ iColor ].rgbBlue = pe[ iColor ].peBlue ;
rgb[ iColor ].rgbReserved = 0;
}
// Save color table:
WriteFile( h, rgb, nPalSize, &dwBytesWritten, 0 );
// assert( dwBytesWritten == nPalSize );
}
// Save image:
WriteFile( h, pImage, lBytes, &dwBytesWritten, 0 );
// assert( dwBytesWritten == lBytes );
CloseHandle( h );
free( pImage );
return true;
}
//===========================================================================
//===========================================================================
void LogToFile(const _TCHAR *format, ...)
{
static bool FirstRun = true;
static _TCHAR buffer[2048];
va_list ap;
va_start(ap, format);
_vsntprintf(buffer,sizeof(buffer)/sizeof(_TCHAR), format, ap);
va_end(ap);
// Cleanup the log?
if (FirstRun)
{
_tunlink(_T("ZMatrixLog.txt"));
FirstRun = false;
}
// Open the log file
FILE *fp = _tfopen(_T("ZMatrixLog.txt"),_T("ab"));
if (!fp) return;
// Spit out the data to the log
_ftprintf(fp,_T("%s\r\n"), buffer);
fclose(fp);
}
//===========================================================================
//===========================================================================
BOOL CALLBACK MinimizeWindowsProc(HWND hWnd,LPARAM lParam)
{
if ( !IsWindowVisible( hWnd ) )
return TRUE;
if ( GetWindow( hWnd, GW_OWNER ) )
return TRUE;
LONG styleEx = GetWindowLong( hWnd, GWL_EXSTYLE );
if (!( styleEx & WS_EX_APPWINDOW ))
{
if ( styleEx & WS_EX_TOOLWINDOW )
return TRUE;
}
if(!IsIconic(hWnd))
{
SendMessage(hWnd,WM_SETREDRAW,FALSE,0);
ShowWindow(hWnd,SW_SHOWMINNOACTIVE);
SendMessage(hWnd,WM_SETREDRAW,TRUE,0);
MinimizedWindows.push_back(hWnd);
}
return TRUE;
}
//===========================================================================
//===========================================================================
void MinimizeAll(void)
{
MinimizedWindows.clear();
EnumWindows(MinimizeWindowsProc,NULL);
}
//===========================================================================
//===========================================================================
void UndoMinimizeAll(void)
{
vector<HWND>::reverse_iterator Iter = MinimizedWindows.rbegin();
for(;Iter != MinimizedWindows.rend();++Iter)
{
SendMessage(*Iter,WM_SETREDRAW,FALSE,0);
ShowWindow(*Iter,SW_RESTORE);
SendMessage(*Iter,WM_SETREDRAW,TRUE,0);
}
MinimizedWindows.clear();
}
//===========================================================================
//===========================================================================
void StoreOrigWallpaper(const _TCHAR *NewPaper)
{
if(NewPaper != NULL)
{
OrigWallpaperString = NewPaper;
OrigWallpaperIsValid = true;
}
else
{
#ifdef WIN9X
HKEY hKey;
if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER,_T("Control Panel\\Desktop"),0,KEY_QUERY_VALUE,&hKey))
{
_TCHAR WallpaperBuffer[MAX_PATH + 1];
DWORD BufferPathInBytes = sizeof(_TCHAR)*(MAX_PATH + 1);
DWORD BufferPathInTCHARs = (MAX_PATH + 1);
memset(WallpaperBuffer,0,BufferPathInBytes);
DWORD TypeQueried = 0;
DWORD QueriedSize = BufferPathInBytes;
if(ERROR_SUCCESS == RegQueryValueEx(hKey,_T("Wallpaper"),0,&TypeQueried,(LPBYTE)WallpaperBuffer,&QueriedSize))
{
if(TypeQueried == REG_SZ)
{
WallpaperBuffer[QueriedSize/sizeof(_TCHAR) - 1] = _T(0);
OrigWallpaperString = WallpaperBuffer;
OrigWallpaperIsValid = true;
}
else
{
MB("Wallpaper not stored because of bizzare queried type when reading current screensaver.");
}
}
RegCloseKey(hKey);
}
else
{
MB("Failed to store the original wallpaper.(RegOpenKeyEx)");
}
#else
_TCHAR TempBuff[MAX_PATH + 1];
SystemParametersInfo(SPI_GETDESKWALLPAPER,MAX_PATH+1,TempBuff,0);
OrigWallpaperString = TempBuff;
OrigWallpaperIsValid = true;
#endif
}
}
//===========================================================================
//===========================================================================
void StoreOrigActiveDesktopWallpaper(const WCHAR *NewActiveDesktopPaper)
{
if(NewActiveDesktopPaper != NULL)
{
OrigActiveDesktopWallpaperString = NewActiveDesktopPaper;
OrigActiveDesktopWallpaperIsValid = true;
}
else
{
WCHAR TempBuff[MAX_PATH + 1];
if(HWND IESrv = FindWindowEx(ghShellDLL,0,_TEXT("Internet Explorer_Server"),NULL))
{
IActiveDesktop *pActiveDesktop = NULL;
CoCreateInstance( CLSID_ActiveDesktop, NULL, CLSCTX_SERVER,
IID_IActiveDesktop, (LPVOID *) &pActiveDesktop );
if(pActiveDesktop)
{
if(S_OK == pActiveDesktop->GetWallpaper(TempBuff,MAX_PATH+1,0))
{
OrigActiveDesktopWallpaperString = TempBuff;
OrigActiveDesktopWallpaperIsValid = true;
}
pActiveDesktop->Release();
}
}
}
}
//===========================================================================
//===========================================================================
void StoreOrigDesktopColor(const COLORREF *NewColor)
{
if(NewColor != NULL)
{
OrigDesktopColor = *NewColor;
OrigDesktopColorIsValid = true;
}
else
{
OrigDesktopColor = GetSysColor(COLOR_DESKTOP);
OrigDesktopColorIsValid = true;
}
}
//===========================================================================
//===========================================================================
void StoreOrigDesktop(const _TCHAR *NewPaper, const WCHAR *NewActiveDesktopPaper, const COLORREF *NewColor)
{
StoreOrigWallpaper(NewPaper);
StoreOrigActiveDesktopWallpaper(NewActiveDesktopPaper);
StoreOrigDesktopColor(NewColor);
}
//===========================================================================
//===========================================================================
void RestoreOrigWallpaper(void)
{
if(OrigWallpaperIsValid)
{
//LogToFile(_T("Restoring wallpaper to %s\n"),OrigWallpaperString.c_str());
//SystemParametersInfo(SPI_SETDESKWALLPAPER,0,NULL,0);
SystemParametersInfo(SPI_SETDESKWALLPAPER,0,const_cast<_TCHAR *>(OrigWallpaperString.c_str()),0);
WallpaperIsCleared = false;
DesktopIsCleared = (WallpaperIsCleared && DesktopColorIsCleared);
}
if(OrigActiveDesktopWallpaperIsValid)
{
if(HWND IESrv = FindWindowEx(ghShellDLL,0,_TEXT("Internet Explorer_Server"),NULL))
{
//SelfPostedDesktopStyleChanges++;
//SendMessage(ghSysListView,LVM_SETEXTENDEDLISTVIEWSTYLE,LVS_EX_REGIONAL,0xFFFF);
IActiveDesktop *pActiveDesktop = NULL;
CoCreateInstance( CLSID_ActiveDesktop, NULL, CLSCTX_SERVER,
IID_IActiveDesktop, (LPVOID *) &pActiveDesktop );
if(pActiveDesktop)
{
if(S_OK != pActiveDesktop->SetWallpaper(OrigActiveDesktopWallpaperString.c_str(),0))
{
MB("Failed to restore active desktop wallpaper");
}
SelfPostedDesktopChildAttached++;
pActiveDesktop->ApplyChanges(AD_APPLY_HTMLGEN|AD_APPLY_REFRESH|AD_APPLY_FORCE);
pActiveDesktop->Release();
}
#ifdef WIN9X
/*This magic message is supposed to force Win9x to refresh the background... Don't ask me why*/
//RedrawWindow(ghProgman,NULL,NULL,RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW|RDW_ALLCHILDREN);
SelfPostedDesktopChildAttached++;
PostMessage(ghProgman,WM_COMMAND,0x1A220,NULL);
//RedrawWindow(GetDesktopWindow(),NULL,NULL,RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW|RDW_ALLCHILDREN);
//SHChangeNotify(SHCNE_ALLEVENTS,SHCNF_IDLIST,NULL,NULL);
//PostMessage(ghProgman,WM_KEYDOWN,VK_F5,0);
//PostMessage(ghProgman,WM_KEYUP,VK_F5,0);
#endif
}
}
}
//===========================================================================
//===========================================================================
void RestoreOrigDesktopColor(void)
{
if(OrigDesktopColorIsValid)
{
int DesktopColorID = COLOR_DESKTOP;
BGColorChangesToIgnore.push_back(OrigDesktopColor);
SetSysColors(1,&DesktopColorID,&OrigDesktopColor);
DesktopColorIsCleared = false;
DesktopIsCleared = (WallpaperIsCleared && DesktopColorIsCleared);
}
}
//===========================================================================
//===========================================================================
void RestoreOrigDesktop(void)
{
RestoreOrigDesktopColor();
RestoreOrigWallpaper();
}
//===========================================================================
//===========================================================================
void ClearWallpaper(void)
{
if(HWND IESrv = FindWindowEx(ghShellDLL,0,_TEXT("Internet Explorer_Server"),NULL))
{
IActiveDesktop *pActiveDesktop = NULL;
CoCreateInstance( CLSID_ActiveDesktop, NULL, CLSCTX_SERVER,
IID_IActiveDesktop, (LPVOID *) &pActiveDesktop );
if(pActiveDesktop)
{
if(S_OK != pActiveDesktop->SetWallpaper(L"",0))
{
MB("Failed to set active desktop wallpaper");
}
SelfPostedDesktopChildAttached++;
pActiveDesktop->ApplyChanges(AD_APPLY_HTMLGEN|AD_APPLY_REFRESH|AD_APPLY_FORCE);
pActiveDesktop->Release();
SetWindowPos(ghWnd,IESrv,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
}
#ifdef WIN9X
/*This magic message is supposed to force Win9x to refresh the background... Don't ask me why*/
//RedrawWindow(ghProgman,NULL,NULL,RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW|RDW_ALLCHILDREN);
SelfPostedDesktopChildAttached++;
PostMessage(ghProgman,WM_COMMAND,0x1A220,NULL);
//SHChangeNotify(SHCNE_ALLEVENTS,SHCNF_IDLIST,NULL,NULL);
//PostMessage(ghProgman,WM_KEYDOWN,VK_F5,0);
//PostMessage(ghProgman,WM_KEYUP,VK_F5,0);
#endif
//SelfPostedDesktopStyleChanges++;
//SendMessage(ghSysListView,LVM_SETEXTENDEDLISTVIEWSTYLE,LVS_EX_REGIONAL,0x0000);
}
SystemParametersInfo(SPI_SETDESKWALLPAPER,0,(void *)_T(""),0);
WallpaperIsCleared = true;
DesktopIsCleared = (WallpaperIsCleared && DesktopColorIsCleared);
}
//===========================================================================
//===========================================================================
void ClearDesktopColor(void)
{
INT DesktopColorID = COLOR_DESKTOP;
COLORREF BlackColor = 0;
BGColorChangesToIgnore.push_back(BlackColor);
SetSysColors(1,&DesktopColorID,&BlackColor);
DesktopColorIsCleared = true;
DesktopIsCleared = (WallpaperIsCleared && DesktopColorIsCleared);
}
//===========================================================================
//===========================================================================
void ClearDesktop(void)
{
ClearDesktopColor();
ClearWallpaper();
}
//===========================================================================
//===========================================================================
void EnforceDesktop(void)
{
BYTE CurrentBGAlpha = GetBGAlpha();
if( !DesktopIsCleared && ((CurrentBGAlpha < 128) || (MatrixObject->GetBGMode() == bgmodeColor)))
{
ClearDesktop();
}
else if(DesktopIsCleared && ((CurrentBGAlpha >= 128) && (MatrixObject->GetBGMode() != bgmodeColor)))
{
RestoreOrigDesktop();
}
}
//===========================================================================
//===========================================================================
BYTE GetBGAlpha(void)
{
BYTE R,G,B,A;
if(MatrixObject != NULL)
{
MatrixObject->GetBGColor(R,G,B,A);
return A;
}
return 0;
}
//===========================================================================
//===========================================================================
HBITMAP UpdateBG(void)
{
if(HWND IESrv = FindWindowEx(ghShellDLL,0,_TEXT("Internet Explorer_Server"),NULL))
{
MinimizeAll();
BringWindowToTop(IESrv);
RedrawWindow(IESrv,NULL,NULL,RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW|RDW_ALLCHILDREN);
HDC source = GetDC(IESrv);
HBITMAP hBGBitmap = CreateCompatibleBitmap(source,gscreenWidth,gscreenHeight);
HDC TempDC = CreateCompatibleDC(source);
SelectObject(TempDC,hBGBitmap);
BitBlt(TempDC,0,0,gscreenWidth,gscreenHeight,source,0,0,SRCCOPY);
//SaveBitmap(hBGBitmap,_T("Test.bmp"));
DeleteDC(TempDC);
ReleaseDC(IESrv,source);
SetWindowPos(IESrv,ghSysListView,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);
//RedrawWindow(ghSysListView,NULL,NULL,RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW|RDW_ALLCHILDREN);
UndoMinimizeAll();
return hBGBitmap;
}
else
{
HDC target = GetDC(0);
SetViewportOrgEx(target,gscreenLeft,gscreenTop,NULL);
RECT ClientRect;
GetClientRect(ghWnd,&ClientRect);
int Width = ClientRect.right - ClientRect.left;
int Height = ClientRect.bottom - ClientRect.top;
HBITMAP hBGBitmap = CreateCompatibleBitmap(target,Width,Height);
if(!PaintDesktop(target))
{
MB("Failed to PaintDesktop");
}
HDC TempDC = CreateCompatibleDC(target);
SelectObject(TempDC,hBGBitmap);
BitBlt(TempDC,ClientRect.left,ClientRect.top,Width,Height,target,ClientRect.left,ClientRect.top,SRCCOPY);
//SaveBitmap(hBGBitmap,_T("UpdatedBG.bmp"));
DeleteDC(TempDC);
SetViewportOrgEx(target,-gscreenLeft,-gscreenTop,NULL);
ReleaseDC(0,target);
InvalidateRect(NULL,NULL,TRUE);
//if(MatrixObject != NULL)
// MatrixObject->SetBGBitmap(hBGBitmap);
return hBGBitmap;
}
}
//===========================================================================
//===========================================================================
void UpdateRegions(void)
{
if(ghSysListView != NULL)
{
DWORD ListViewStyles;
HRGN hListViewRGN = CreateRectRgn(0,0,0,0);
HRGN hWndRGN = CreateRectRgn(0,0,gscreenWidth,gscreenHeight);
const unsigned int MaxRgnAttempts = 2;
ListViewStyles = ListView_GetExtendedListViewStyle(ghSysListView);
//SelfPostedDesktopStyleChanges++;
SendMessage(ghSysListView,LVM_SETEXTENDEDLISTVIEWSTYLE,LVS_EX_REGIONAL,0xFFFF);
RedrawWindow(ghSysListView,NULL,NULL,RDW_ERASE|RDW_INVALIDATE|RDW_UPDATENOW|RDW_ALLCHILDREN);
//hListViewRGN = CreateRectRgn(0,0,0,0);
unsigned int RgnAttempts = 0;
while((RgnAttempts < MaxRgnAttempts) &&
(GetWindowRgn(ghSysListView,hListViewRGN) == ERROR)
)
{
RgnAttempts++;
}
if(RgnAttempts >= MaxRgnAttempts)
{
//This is not neccessarily an error condition, because
//it may arise if there are no icons on the desktop.
//MB("Failed to get RGN for SysListView");
DeleteObject(hListViewRGN);
DeleteObject(hWndRGN);
return;
}
//SelfPostedDesktopStyleChanges++;
SendMessage(ghSysListView,LVM_SETEXTENDEDLISTVIEWSTYLE,LVS_EX_REGIONAL,ListViewStyles);
//if(GetWindowRgn(hWnd,hWndRGN) == ERROR) MB("Error, couldn't get RGN for hWnd");
if(CombineRgn(ValidRGN,hWndRGN,hListViewRGN,RGN_DIFF) == ERROR) MB("Error, couldn't calculate the diff region");
DeleteObject(hListViewRGN);
DeleteObject(hWndRGN);
}
}
//===========================================================================
//===========================================================================
HBITMAP Refresh(void)
{
if(DesktopIsCleared)
{
RestoreOrigDesktop();
UpdateRegions();
HBITMAP RetVal = UpdateBG();
ClearDesktop();
return RetVal;
}
else
{
UpdateRegions();
HBITMAP RetVal = UpdateBG();
return RetVal;
}
}
//===========================================================================
//===========================================================================
bool DirectoryExists(const _TCHAR *DirName)
{
if(DirName == NULL) return false;
DWORD Attrib = GetFileAttributes((_TCHAR *)DirName);
return ((Attrib != INVALID_FILE_ATTRIBUTES) && (FILE_ATTRIBUTE_DIRECTORY & Attrib));
}
//===========================================================================
//===========================================================================
bool FileExists(const _TCHAR *DirName)
{
if(DirName == NULL) return false;
DWORD Attrib = GetFileAttributes((_TCHAR *)DirName);
return ((Attrib != INVALID_FILE_ATTRIBUTES) && (!(FILE_ATTRIBUTE_DIRECTORY & Attrib)));
}
//===========================================================================
//===========================================================================
HRESULT CreateLink(const TCHAR *LinkTarget, const TCHAR *LinkLocation)
{
if((LinkTarget == NULL) || (LinkLocation == NULL))
{
return -1;
}
HRESULT hres;
IShellLink* psl;
// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID *) &psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;
// Set the path to the shortcut target and add the
// description.
psl->SetPath(LinkTarget);
// Query IShellLink for the IPersistFile interface for saving the
// shortcut in persistent storage.
hres = psl->QueryInterface(IID_IPersistFile,(LPVOID*)&ppf);
if (SUCCEEDED(hres))
{
unsigned int Length = _tcslen(LinkLocation);
WCHAR *TempBuffer = new WCHAR[_tcslen(LinkLocation) + 1];
for(unsigned int i = 0; i < Length; i++)
{
TempBuffer[i] = LinkLocation[i];
}
TempBuffer[Length] = (WCHAR)0;
// Save the link by calling IPersistFile::Save.
hres = ppf->Save(TempBuffer, TRUE);
delete[] TempBuffer;
ppf->Release();
}
psl->Release();
}
return hres;
}
//===========================================================================
//===========================================================================
void RemoveLink(const TCHAR *LinkLocation)
{
_tunlink(LinkLocation);
}
//===========================================================================
//===========================================================================
bool GetMatrixCommandLine(_tstring &Target)
{
unsigned int RetStrSize = 128;
_TCHAR *RetStr = (_TCHAR *)malloc(sizeof(_TCHAR)*RetStrSize);
while((RetStrSize - 1) == GetPrivateProfileString(_T("ZMatrixSS"),_T("MatrixCommandLine"),_T("matrix.exe"),RetStr,RetStrSize,_T("ZMatrixSS.ini")))
{
RetStrSize += 128;
RetStr = (_TCHAR *)realloc(RetStr,sizeof(_TCHAR)*RetStrSize);
}
Target = RetStr;
free(RetStr);
return false;
}
//===========================================================================
//===========================================================================
void BeginScreenSaveMode(HWND WindowToNotify,UINT MessageToSend)
{
if(WindowToNotify != NULL)
{
NotificationPair NewPair(WindowToNotify,MessageToSend);
if(WindowsToNotifyWhenSSDone.end() == find(WindowsToNotifyWhenSSDone.begin(),WindowsToNotifyWhenSSDone.end(),NewPair))
{
WindowsToNotifyWhenSSDone.push_back(NewPair);
}
}
if(!InScreenSaveMode)
{
if(MatrixObject == NULL) return;
HRESULT Result = NO_ERROR;
MULTI_QI Qi;
Qi.pIID = &IID_IZSMATRIX;
Qi.pItf = NULL;
Qi.hr = 0;
if(FAILED(Result = CoCreateInstanceEx(CLSID_ZSMATRIX,NULL,CLSCTX_ALL,NULL,1,&Qi) ) )
{
MB("Failed to create BackupObjectToConfig");
return;
}
PreScreenSaveMatrixObject = (IzsMatrix *)Qi.pItf;
PreScreenSaveMatrixObject->CopyFrom(*MatrixObject);
PreScreenSaveRefreshTime = RefreshTime;
PreScreenSavePriorityClass = GetPriorityClass(GetCurrentProcess());
PreScreenSavePauseState = Paused;
Paused = false;
if(BlendScreenSaverWithBGOnly)
{
if(FileExists(AppScreenSaverConfigFilePath.c_str()))
{
LoadConfig(MatrixObject,RefreshTime,AppScreenSaverConfigFilePath.c_str());
SetTimer(ghWnd,REFRESH_TIMER_ID,RefreshTime,0);
}
}
else
{
if(DesktopIsCleared)
{
RestoreOrigDesktop();
}
HDC target = GetDC(0);
SetViewportOrgEx(target,gscreenLeft,gscreenTop,NULL);
HDC TempDC = CreateCompatibleDC(target);
RECT ClientRect;
GetClientRect(ghWnd,&ClientRect);
int Width = ClientRect.right - ClientRect.left;
int Height = ClientRect.bottom - ClientRect.top;
HBITMAP hBGBitmap = CreateCompatibleBitmap(target,Width,Height);
SelectObject(TempDC,hBGBitmap);
BitBlt(TempDC,0,0,Width,Height,target,ClientRect.left,ClientRect.top,SRCCOPY);
DeleteDC(TempDC);
SetViewportOrgEx(target,-gscreenLeft,-gscreenTop,NULL);
ReleaseDC(0,target);
if(FileExists(AppScreenSaverConfigFilePath.c_str()))
{
LoadConfig(MatrixObject,RefreshTime,AppScreenSaverConfigFilePath.c_str());
SetTimer(ghWnd,REFRESH_TIMER_ID,RefreshTime,0);
}
MatrixObject->SetBGBitmap(hBGBitmap);
if(!DeleteObject(hBGBitmap))
{
DWORD Temp = GetLastError();
MB("Failed to delete old BG bitmap after starting a screensave");
}
BYTE CurrentBGAlpha = GetBGAlpha();
if( !DesktopIsCleared && ((CurrentBGAlpha < 128) || (MatrixObject->GetBGMode() == bgmodeColor)))
{
ClearDesktop();
}
}
if (!RegisterClassEx(&ScreenSaverDummyClass))
{
MB("Failed to register dummy matrix window class");
}
else
{
ScreenSaverDummyWindow = CreateWindowEx(WS_EX_TOPMOST|WS_EX_TOOLWINDOW,DummyMatrixWindowClassName,_T("DummyMatrixWindow"),WS_POPUP,gscreenLeft,gscreenTop,gscreenWidth,gscreenHeight,
NULL,NULL,ghInstance,NULL);
ShowWindow(ScreenSaverDummyWindow,SW_SHOW);
}
GetCursorPos(&MouseScreenPointAtScreenSaveStart);
ShowCursor(FALSE);
InScreenSaveMode = true;
SetMouseMessageHook(ghWnd);
CheckMenuItem(gSysTrayPopup,ID_SCREENSAVE,MF_CHECKED);
}
}
//===========================================================================
//===========================================================================
void EndScreenSaveMode(void)
{
if(InScreenSaveMode)
{
if((MatrixObject != NULL) && (PreScreenSaveMatrixObject != NULL))
{
MatrixObject->CopyFrom(*PreScreenSaveMatrixObject);
PreScreenSaveMatrixObject->Release();
PreScreenSaveMatrixObject = NULL;
RefreshTime = PreScreenSaveRefreshTime;
if(PreScreenSavePauseState)
{
KillTimer(ghWnd,REFRESH_TIMER_ID);
Paused = true;
}
else
{
SetTimer(ghWnd,REFRESH_TIMER_ID,RefreshTime,0);
}
SetPriorityClass(GetCurrentProcess(),PreScreenSavePriorityClass);