Repository navigation
Expand file tree
/
Copy pathderbar.cpp
More file actions
991 lines (871 loc) · 34.1 KB
/
Copy pathderbar.cpp
File metadata and controls
991 lines (871 loc) · 34.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
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
//**************************************************************************************
// Copyright (c) 2009-2026 Derell Licht
// derbar.exe - Another WinBar application
//
// This program is licenced under Creative Commons CC0 1.0 Universal
//
// The person who associated a work with this deed has dedicated the work to the public domain
// by waiving all of his or her rights to the work worldwide under copyright law, including all
// related and neighboring rights, to the extent allowed by law.
//
// You can copy, modify, distribute and perform the work, even for commercial purposes,
// all without asking permission.
//
//**************************************************************************************
#include <windows.h>
#include <stdio.h> // for sprintf, for %f support
#include <time.h>
#include <tchar.h>
#include "iface_32_64.h"
#include "resource.h"
#include "version.h"
#include "common.h"
// #include "commonw.h"
#include "derbar.h"
#include "images.h"
#include "winmsgs.h"
#include "systray.h"
#include "tooltips.h"
//lint -e592 Non-literal format specifier used without arguments
//lint -esym(844, hwndSFree, hwndSCPU, hwndSMem, hwndSRX, hwndSTX, hwndSTotal, hwndSUpTime)
#define USE_TIMER_TEST
#define USE_CPU_UTIL 1
static TCHAR szClassName[] = _T("DerBar") ;
// system.cpp
extern void update_memory_readings(void);
extern void build_iface_tables(void);
extern void update_iface_counters(void);
extern void cpu_usage_setup(void);
extern double cpu_usage_report(void);
extern void cpu_usage_release(void);
extern DWORD SampleMsec ;
// about.cpp
extern BOOL CmdAbout(HWND hwnd);
// options.cpp
extern void open_options_dialog(HWND hwnd);
// lv_ifaces.cpp
extern void spawn_ifaces_lview(HWND hwnd);
// login_lsa.cpp
//lint -esym(752, get_max_logon_time)
time_t get_max_logon_time(void);
time_t get_logon_time(void);
// dialog color settings 0Oo 1lIi|
static HBRUSH hbLogo = 0 ;
static HBRUSH hbStatic = 0 ;
HBRUSH hbEdit = 0 ;
COLORREF fgnd_edit = WIN_BGREEN ;
COLORREF bgnd_edit = WIN_GREY ;
COLORREF fgnd_static = WIN_BLACK ;
static COLORREF bgnd_static = WIN_GREEN ;
//********************************************************
static const unsigned SZ1GB = (unsigned) (1024 * 1024 * 1024) ;
static const unsigned SZ1MB = (unsigned) (1024 * 1024) ;
// system.cpp
extern u64 freemem ;
extern u64 totalmem ;
extern uint RxBytesPerSec ;
extern uint TxBytesPerSec ;
//***********************************************************************
HINSTANCE g_hinst = 0;
static UINT timerID = 0 ;
bool isMemoryLow = false ;
// definitions for dialog controls
//lint -esym(844, hwndDerBar)
static HWND hwndMainDialog = 0 ;
static HWND hwndDerBar ;
static HWND hwndFreeMem ;
static HWND hwndTotalMem ;
static HWND hwndUptime ;
static HWND hwndSUptime ;
static HWND hwndRxBytes ;
static HWND hwndTxBytes ;
static HWND hwndCpuTime ;
static HWND hwndSMem = NULL ;
static HWND hwndSFree = NULL ;
static HWND hwndSTotal = NULL ;
static HWND hwndSUpTime = NULL ;
static HWND hwndSRX = NULL ;
static HWND hwndSTX = NULL ;
static HWND hwndSCPU = NULL ;
static HWND hwndKbdCaps ;
static HWND hwndKbdNum ;
static HWND hwndKbdScrl ;
//****************************************************************************
// Main dialog tooltips
//****************************************************************************
static tooltip_data const main_tooltips[] = {
{ IDS_MEMORY, _T("Show Memory info" )},
{ IDS_FREEMEM, _T("Show Free Memory" )},
{ IDC_FREEMEM, _T("Show Free Memory" )},
{ IDS_TOTALMEM, _T("Show Total Memory" )},
{ IDC_TOTALMEM, _T("Show Total Memory" )},
{ IDS_UPTIME, _T("Show Uptime/Login Time" )},
{ IDC_UPTIME, _T("Show Uptime/Login Time" )},
{ IDS_RXBYTES, _T("Show Network Receive rate (KBytes/second)" )},
{ IDC_RXBYTES, _T("Show Network Receive rate (KBytes/second)" )},
{ IDS_TXBYTES, _T("Show Network Transmit rate (KBytes/second)" )},
{ IDC_TXBYTES, _T("Show Network Transmit rate (KBytes/second)" )},
{ IDS_CPUTIME, _T("Show CPU Utilization" )},
{ IDC_CPUTIME, _T("Show CPU Utilization" )},
{ IDC_KBD_CAPS, _T("Show/toggle CapsLock state" )},
{ IDC_KBD_NUM , _T("Show/toggle NumLock state" )},
{ IDC_KBD_SCRL, _T("Show/toggle ScrollLock state" )},
{ 0, NULL }} ;
//*******************************************************************
static uint screen_width = 0 ;
static uint screen_height = 0 ;
static void get_monitor_dimens(HWND hwnd)
{
HMONITOR currentMonitor; // Handle to monitor where fullscreen should go
MONITORINFO mi; // Info of that monitor
currentMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
mi.cbSize = sizeof(MONITORINFO);
if (GetMonitorInfo(currentMonitor, &mi) != FALSE) {
screen_width = mi.rcMonitor.right - mi.rcMonitor.left ;
screen_height = mi.rcMonitor.bottom - mi.rcMonitor.top ;
}
}
//*******************************************************************
// if current dialog position is offscreen, move it safely onscreen.
// The actual move will be done later by set_window_position()
//
// players: x_pos, y_pos, hwndMainDialog
//*******************************************************************
static void verify_screen_position(void)
{
if (x_pos > screen_width || y_pos > screen_height) {
x_pos = 0 ;
y_pos = screen_height - 100 ;
}
}
//*******************************************************************
static void save_window_position(HWND hwnd)
{
RECT rect ;
GetWindowRect(hwnd, &rect) ;
x_pos = rect.left ;
y_pos = rect.top ;
save_cfg_file() ;
}
// use 20 for WS_EX_APPWINDOW
// use 16 for WS_EX_TOOLWINDOW
// #define TBAR_DY 16
//******************************************************************
void update_uptime_label(void)
{
if (use_logon_time_for_uptime) {
SetWindowText(hwndSUptime, _T("Login")) ;
}
else {
SetWindowText(hwndSUptime, _T("Uptime")) ;
}
}
//*******************************************************************
static void relocate_main_dialog(HWND hwnd)
{
SetWindowPos(hwnd, NULL, 0, 100, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_DRAWFRAME);
x_pos = 0 ;
y_pos = 100 ;
save_cfg_file() ;
}
//*******************************************************************
static void toggle_title_bar(HWND hwnd)
{
DWORD CurrentStyles = GetWindowLong( hwnd, GWL_STYLE );
CurrentStyles ^= WS_CAPTION ;
SetWindowLong( hwnd, GWL_STYLE, CurrentStyles);
// bool is_caption_visible = (CurrentStyles & WS_CAPTION) ? true : false ;
bool is_caption_visible = ((CurrentStyles & WS_CAPTION) != 0);
RECT rect ;
// SM_CYCAPTION gives the caption height for WS_EX_APPWINDOW.
// SM_CYSMCAPTION gives the caption height for WS_EX_TOOLWINDOW.
// not for WS_EX_TOOLWINDOW, which I'm using.
DWORD ExStyles = GetWindowLong( hwnd, GWL_EXSTYLE );
uint caption_cy ;
if (ExStyles & WS_EX_APPWINDOW) {
caption_cy = (uint) GetSystemMetrics(SM_CYCAPTION) ;
// syslog("WS_EX_APPWINDOW is active\n") ;
}
else {
caption_cy = (uint) GetSystemMetrics(SM_CYSMCAPTION) ;
// syslog("WS_EX_TOOLWINDOW is active\n") ;
}
// syslog("Caption CY=%d\n", caption_cy) ;
GetWindowRect(hwnd, &rect) ;
uint dy = rect.bottom - rect.top ;
uint y0 = rect.top ;
uint dx = rect.right - rect.left ;
if (is_caption_visible) {
y0 -= caption_cy ;
dy += caption_cy ;
tbar_on = 1 ;
} else {
y0 += caption_cy ;
dy -= caption_cy ;
tbar_on = 0 ;
}
// SetWindowPos( hwnd, NULL, rect.left, y0, dx, dy, SWP_NOSIZE | SWP_NOZORDER | SWP_NOMOVE | SWP_DRAWFRAME );
SetWindowPos( hwnd, NULL, rect.left, y0, dx, dy, SWP_NOZORDER | SWP_DRAWFRAME );
x_pos = rect.left ;
y_pos = y0 ;
save_cfg_file() ;
}
//*******************************************************************
// used *only* in WM_INITDIALOG
//*******************************************************************
static void set_window_position(HWND hwnd)
{
RECT rect ;
GetWindowRect(hwnd, &rect) ;
uint dy = rect.bottom - rect.top ;
uint dx = rect.right - rect.left ;
uint caption_cy = (uint) GetSystemMetrics(SM_CYSMCAPTION) ;
if (tbar_on) {
SetWindowPos(hwnd, NULL, x_pos, y_pos, dx, dy, SWP_NOZORDER | SWP_DRAWFRAME );
} else {
// well, this sequence is not very efficient, but it works,
// and it's only run once when program is started...
SetWindowPos(hwnd, NULL, x_pos, y_pos, dx, dy, SWP_NOZORDER | SWP_DRAWFRAME );
toggle_title_bar(hwnd);
y_pos -= caption_cy ;
SetWindowPos( hwnd, NULL, x_pos, y_pos, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_DRAWFRAME );
}
save_cfg_file() ;
}
//***********************************************************************
// this is a debug function to verify the 1000-msec main-loop timer
//***********************************************************************
#ifdef USE_TIMER_TEST
// if timer debugging was actually active,
// we would want to be monitoring these two variables somewhere
static uint min_second_ticks = 0 ;
static uint max_second_ticks = 0 ;
static void update_time_count(void)
{
//lint -esym(843, cpu_freq_hz) // Variable could be declared as const
static uint cpu_freq_hz = 0 ;
static u64 prev_ticks = 0 ;
LARGE_INTEGER li_value ;
if (cpu_freq_hz == 0) {
QueryPerformanceFrequency(&li_value) ;
cpu_freq_hz = (uint) li_value.LowPart ; //lint !e1013 !e40
// cpu_freq_mhz = cpu_freq_hz / 1000 ;
// if (dbg_flags & DBG_VERBOSE)
// syslog("CPU freq=%u hz\n", cpu_freq_hz) ;
}
QueryPerformanceCounter(&li_value) ;
u64 curr_ticks = li_value.QuadPart ;
// runtime_seconds++ ;
// DWORD curr_ticks = GetTickCount() ;
// check for counter wrap
// if (prev_ticks == 0 || curr_ticks < prev_ticks) {
if (prev_ticks == 0) {
// for now, do nothing here
} else {
DWORD diff_ticks = (uint) (curr_ticks - prev_ticks) ;
if (diff_ticks == 0) {
// zero_ticks_count++ ;
} else if (max_second_ticks < diff_ticks) {
// if (dbg_flags & DBG_VERBOSE)
// syslog("new max ticks: %u (%u msec)\n", diff_ticks, diff_ticks/cpu_freq_mhz) ;
max_second_ticks = diff_ticks ;
} else if (min_second_ticks == 0 || min_second_ticks > diff_ticks) {
// if (dbg_flags & DBG_VERBOSE)
// syslog("new min ticks: %u (%u msec)\n", diff_ticks, diff_ticks/cpu_freq_mhz) ;
min_second_ticks = diff_ticks ;
}
}
prev_ticks = curr_ticks ;
}
#endif
//*******************************************************************
static TCHAR uptime_str[81] = _T("0") ;
static void update_uptime(void)
{
DWORD gtc ;
if (use_logon_time_for_uptime) {
gtc = (DWORD) get_logon_time();
}
else {
gtc = GetTickCount() ; // get uptime in msec
gtc /= 1000 ; // convert to seconds
}
DWORD secs = gtc % 60 ;
gtc /= 60 ; // convert to minutes
DWORD mins = gtc % 60 ;
gtc /= 60 ; // convert to hours
DWORD hours = gtc % 24 ;
gtc /= 24 ; // convert to days
// int slen = wsprintf(uptime_str, "Uptime: ") ;
int slen = 0 ;
if (gtc > 0) {
slen += wsprintf(&uptime_str[slen], _T("%ud %uh %um"), gtc, hours, mins) ;
}
else if (hours > 0) {
slen += wsprintf(&uptime_str[slen], _T("%uh %um"), hours, mins) ;
}
else {
slen += wsprintf(&uptime_str[slen], _T("%um"), mins) ;
}
if (show_seconds_for_uptime) {
// slen +=
wsprintf(&uptime_str[slen], _T(" %us"), secs) ;
}
}
//*******************************************************************
static unsigned kbd_state = 0 ;
#define KEY_ACTIVE 1
static void update_keyboard_state(void)
{
kbd_state = 0 ;
SHORT kbd_status = GetKeyState(VK_CAPITAL) ;
if (kbd_status & KEY_ACTIVE) kbd_state |= 4 ;
kbd_status = GetKeyState(VK_NUMLOCK) ;
if (kbd_status & KEY_ACTIVE) kbd_state |= 2 ;
kbd_status = GetKeyState(VK_SCROLL) ;
if (kbd_status & KEY_ACTIVE) kbd_state |= 1 ;
// SetWindowText(hwndKbdState, kbd_state_str[kbd_state]) ;
SetWindowText(hwndKbdCaps, (kbd_state & 4) ? _T("C") : _T("c")) ;
SetWindowText(hwndKbdNum , (kbd_state & 2) ? _T("N") : _T("n")) ;
SetWindowText(hwndKbdScrl, (kbd_state & 1) ? _T("S") : _T("s")) ;
}
//*******************************************************************
static double CpuTime = 0.0 ;
static void read_system_data(void)
{
static unsigned sub_count = 0 ;
if (++sub_count == 4) {
sub_count = 0 ;
update_uptime() ;
update_memory_readings() ;
update_iface_counters() ;
#ifdef USE_CPU_UTIL
CpuTime = cpu_usage_report() ;
#endif
}
update_keyboard_state() ;
}
//*******************************************************************
static void update_data_fields(void)
{
TCHAR msgstr[81] ;
// update editable fields
isMemoryLow = (freemem < (totalmem / 10)) ;
if (freemem < SZ1GB)
_stprintf(msgstr, _T("%u MB"), (unsigned) (freemem / SZ1MB)) ;
else
_stprintf(msgstr, _T("%.2f GB"), (double) freemem / SZ1GB) ;
SetWindowText(hwndFreeMem, msgstr) ;
if (totalmem < 1000000000U)
_stprintf(msgstr, _T("%u MB"), (unsigned) (totalmem / SZ1MB)) ;
else
_stprintf(msgstr, _T("%.2f GB"), (double) totalmem / SZ1GB) ;
SetWindowText(hwndTotalMem, msgstr) ;
SetWindowText(hwndUptime, uptime_str) ;
double rxBps = (double) RxBytesPerSec / SampleMsec ;
if (rxBps < 20000.0) {
_stprintf(msgstr, _T("%.1f"), rxBps) ;
}
else {
_stprintf(msgstr, _T("%.0f"), rxBps) ;
}
SetWindowText(hwndRxBytes, msgstr) ;
double txBps = (double) TxBytesPerSec / SampleMsec ;
if (txBps < 1000.0) {
_stprintf(msgstr, _T("%.1f"), txBps) ;
}
else {
_stprintf(msgstr, _T("%.0f"), txBps) ;
}
SetWindowText(hwndTxBytes, msgstr) ;
_stprintf(msgstr, _T("%u"), (unsigned) (CpuTime + 0.5)) ;
SetWindowText(hwndCpuTime, msgstr) ;
}
//*******************************************************************
void update_static_fields(void)
{
// redraw the static data fields
SetWindowText(hwndSMem , L"Mem:") ;
SetWindowText(hwndSFree , L"Free") ;
SetWindowText(hwndSTotal , L"Total") ;
// update_uptime_label() ;
SetWindowText(hwndSRX , L"RX:") ;
SetWindowText(hwndSTX , L"TX:") ;
SetWindowText(hwndSCPU , L"CPU:") ;
}
//*******************************************************************
void update_keep_on_top(void)
{
if (keep_on_top) {
SetWindowPos (hwndMainDialog, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
} else {
SetWindowPos (hwndMainDialog, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
}
}
//*************************************************************************************
// info on toggling keyboard states was obtained from:
// https://support.microsoft.com/en-us/help/127190/
// howto-toggle-the-num-lock-caps-lock-and-scroll-lock-keys
//
// Later note: just be aware that this function is somewhat misnamed,
// now that it is also responsible for clicks in the Uptime/Logon field
//*************************************************************************************
#define PROP_KBD_PROC TEXT("_DerBarKeys_Original_Proc_")
static LRESULT CALLBACK KbdFlagsProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
BYTE keyState[256]; // 256-byte array that will receive the status data for each virtual key
// debug function: see what messages are being received
if (show_winmsgs) {
switch (message) {
// list messages to be ignored
case WM_TIMER:
case WM_SETTEXT:
case WM_NCHITTEST:
case WM_CTLCOLORSTATIC:
case WM_SETCURSOR:
case WM_NCMOUSEMOVE:
case WM_MOUSEMOVE:
break;
default:
syslog(_T("DerBarSC [%X]: [%s]\n"), hwnd, lookup_winmsg_name(message)) ;
break;
}
}
// kbd hwnd: C0013023E N0013023C S001906D6
WNDPROC pfnOrigProc = (WNDPROC) GetProp(hwnd, PROP_KBD_PROC); //lint !e611
switch (message)
{
case WM_LBUTTONUP:
GetKeyboardState((PBYTE)&keyState[0]);
// syslog("kbd hwnd: hwnd:%08X C:%08X N:%08X S:%08X\n", hwnd, hwndKbdCaps, hwndKbdNum, hwndKbdScrl);
if (hwnd == hwndKbdCaps) {
// Simulate a key press
keybd_event( VK_CAPITAL, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0 );
// Simulate a key release
keybd_event( VK_CAPITAL, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
} else
if (hwnd == hwndKbdNum) {
// Simulate a key press
keybd_event( VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0 );
// Simulate a key release
keybd_event( VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
} else
if (hwnd == hwndKbdScrl) {
// Simulate a key press
keybd_event( VK_SCROLL, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0 );
// Simulate a key release
keybd_event( VK_SCROLL, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
} else
if (hwnd == hwndUptime) {
use_logon_time_for_uptime = !(use_logon_time_for_uptime) ;
update_uptime_label();
save_cfg_file() ;
} // NOLINT(readability-misleading-indentation)
break; // NOLINT(readability-misleading-indentation)
default:
break;
} //lint !e744
return CallWindowProc(pfnOrigProc, hwnd, message, wParam, lParam);
}
//*******************************************************************
static LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// int result ;
// static bool show_winmsgs = true ;
// static bool lbutton_active = false ;
TCHAR msgstr[81] ;
static bool first_pass_done = false ;
static bool moving_via_title_bar = false ;
HDC hdc ;
// debug function: see what messages are being received
if (show_winmsgs) {
switch (message) {
// list messages to be ignored
case WM_CTLCOLORSTATIC:
case WM_NCHITTEST:
case WM_SETCURSOR:
case WM_TIMER:
case WM_NCMOUSEMOVE:
case WM_MOUSEWHEEL:
case WM_ENTERIDLE:
break;
default:
// syslog("MON: [%s]\n", get_winmsg_name(result));
syslog(_T("DerBar: [%s] %08X\n"), lookup_winmsg_name(message), message) ;
break;
}
}
switch (message) {
// case WM_CREATE:
case WM_INITDIALOG:
_stprintf(msgstr, _T("DerBar %s"), VerNum) ; //lint !e719
SetWindowText(hwnd, msgstr) ;
hwndMainDialog = hwnd ;
get_monitor_dimens(hwnd);
// read configuration *before* creating edit fields
verify_screen_position() ;
read_system_data() ;
update_keep_on_top() ;
// edit fields
hwndDerBar = GetDlgItem(hwnd, IDC_DERBAR) ;
hwndFreeMem = GetDlgItem(hwnd, IDC_FREEMEM) ;
hwndTotalMem = GetDlgItem(hwnd, IDC_TOTALMEM) ;
hwndSUptime = GetDlgItem(hwnd, IDS_UPTIME) ;
hwndUptime = GetDlgItem(hwnd, IDC_UPTIME) ;
hwndRxBytes = GetDlgItem(hwnd, IDC_RXBYTES) ;
hwndTxBytes = GetDlgItem(hwnd, IDC_TXBYTES) ;
hwndCpuTime = GetDlgItem(hwnd, IDC_CPUTIME) ;
hwndKbdCaps = GetDlgItem(hwnd, IDC_KBD_CAPS) ;
hwndKbdNum = GetDlgItem(hwnd, IDC_KBD_NUM ) ;
hwndKbdScrl = GetDlgItem(hwnd, IDC_KBD_SCRL) ;
// static controls
hwndSMem = GetDlgItem(hwnd, IDS_MEMORY) ;
hwndSFree = GetDlgItem(hwnd, IDS_FREEMEM) ;
hwndSTotal = GetDlgItem(hwnd, IDS_TOTALMEM) ;
hwndSUpTime = GetDlgItem(hwnd, IDS_UPTIME) ;
hwndSRX = GetDlgItem(hwnd, IDS_RXBYTES) ;
hwndSTX = GetDlgItem(hwnd, IDS_TXBYTES) ;
hwndSCPU = GetDlgItem(hwnd, IDS_CPUTIME) ;
// fgnd_static = GetSysColor(COLOR_WINDOWTEXT) ;
bgnd_static = GetSysColor(COLOR_3DFACE) ;
// LTEXT "Mem:", IDS_MEMORY, 40, Y0, 20, 9,SS_NOTIFY
// LTEXT "Free", IDS_FREEMEM, 64, Y0, 16, 9,SS_NOTIFY
// LTEXT "Total", IDS_TOTALMEM, 118, Y0, 20, 9,SS_NOTIFY
// LTEXT "Uptime:", IDS_UPTIME, 181, Y0, 25, 9,SS_NOTIFY
// LTEXT "RX:", IDS_RXBYTES, 273, Y0, 12, 9,SS_NOTIFY
// LTEXT "TX:", IDS_TXBYTES, 321, Y0, 12, 9,SS_NOTIFY
// LTEXT "CPU:", IDS_CPUTIME, 373, Y0, 18, 9,SS_NOTIFY
// Subclass the existing key-state controls,
// to allow toggling their states
{
WNDPROC pfnOrigProcCaps = (WNDPROC) GetWindowLongIF(hwndKbdCaps, GWx_WNDPROC);
SetProp(hwndKbdCaps, PROP_KBD_PROC, (HANDLE) pfnOrigProcCaps); //lint !e611
SetWindowLongIF(hwndKbdCaps, GWx_WNDPROC, (LONG_PTR) (WNDPROC) KbdFlagsProc);
WNDPROC pfnOrigProcNum = (WNDPROC) GetWindowLongIF(hwndKbdNum, GWx_WNDPROC);
SetProp(hwndKbdNum, PROP_KBD_PROC, (HANDLE) pfnOrigProcNum); //lint !e611
SetWindowLongIF(hwndKbdNum, GWx_WNDPROC, (LONG_PTR) (WNDPROC) KbdFlagsProc);
WNDPROC pfnOrigProcScrl = (WNDPROC) GetWindowLongIF(hwndKbdScrl, GWx_WNDPROC);
SetProp(hwndKbdScrl, PROP_KBD_PROC, (HANDLE) pfnOrigProcScrl); //lint !e611
SetWindowLongIF(hwndKbdScrl, GWx_WNDPROC, (LONG_PTR) (WNDPROC) KbdFlagsProc);
// try this for uptime field as well
// NOTE: this requires SS_NOTIFY set in control definition (derbar.rc)
WNDPROC pfnOrigProcUptime = (WNDPROC) GetWindowLongIF(hwndUptime, GWx_WNDPROC);
SetProp(hwndUptime, PROP_KBD_PROC, (HANDLE) pfnOrigProcUptime); //lint !e611
SetWindowLongIF(hwndUptime, GWx_WNDPROC, (LONG_PTR) (WNDPROC) KbdFlagsProc);
}
update_data_fields() ;
//**********************************************************
// do other config tasks *after* creating fields,
// so we can display status messages.
//**********************************************************
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon(g_hinst, MAKEINTRESOURCE(IDI_MAINICON)));
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon(g_hinst, MAKEINTRESOURCE(IDI_MAINICON)));
set_window_position(hwnd) ;
// put the icon into a system tray
load_tray_menu(IDM_POPMENU);
attach_tray_icon(hwnd, szClassName, IDI_MAINICON);
update_uptime_label();
create_and_add_tooltips(hwnd, 150, 100, 10000, main_tooltips);
// start timer for program update
// Note that Windows timers do not actually count 1000 msec per second,
// they count 1024 msec per second; thus, they are off by about 1.5%
// timerID = SetTimer(hwnd, IDT_TIMER, 1000, (TIMERPROC) NULL) ;
// timerID = SetTimer(hwnd, IDT_TIMER, 977, (TIMERPROC) NULL) ;
// Later note: I eventually switched to 1/4-second timing,
// to improve responsiveness in data updates.
timerID = SetTimer(hwnd, IDT_TIMER, 244, (TIMERPROC) NULL) ;
return TRUE;
case WM_USER:
respond_to_tray_clicks(hwnd, lParam);
break;
case WM_NCRBUTTONUP:
respond_to_tray_clicks(hwnd, WM_RBUTTONUP);
break;
case WM_SYSCOMMAND:
// syslog("SYSCOMMAND>: wParam=%04X\n", wParam) ;
switch (wParam) {
// case SC_MINIMIZE:
// ShowWindow (hwnd, SW_HIDE);
// break;
// detect close from System menu
// case SC_CLOSE:
// DestroyWindow (hwnd);
// break;
// In case you're wondering, NO, I have no idea why I put
// the "OR 2" option in here. Presumably I was seeing these messages
// pop up in the winmsgs display, and couldn't find documentation
// on precisely what SC_MOVE is supposed to do... (??)
//
// Later note: Actually, SC_MOVE does not occur on its own,
// it is *always* accompanies by 0x0002
// This makes it difficult to specifically detect left-click...
case (SC_MOVE | 2): // mouse move
// case SC_MOVE:
// syslog("we be moving!!\n") ;
moving_via_title_bar = true ;
break;
// any WM_SYSCOMMAND that we don't handle ourselves,
// needs to be passed on to the system.
// Otherwise, the window isn't movable!!
default:
// syslog("SYSCOMMAND: wParam=%04X\n", wParam) ;
// return DefWindowProc (hwnd, message, wParam, lParam);
break;
}
break;
case WM_EXITSIZEMOVE:
if (moving_via_title_bar) {
save_window_position(hwnd) ;
moving_via_title_bar = false ;
}
return TRUE;
// technique to drag dialog by its client area.
// Once the title bar is enabled, though, the popup menu will no longer work!!
case WM_NCHITTEST:
if (!tbar_on && DefWindowProc(hwnd, WM_NCHITTEST, wParam, lParam) == HTCLIENT) {
// SetWindowLong(hwnd, DWL_MSGRESULT, HTCAPTION);
SetWindowLong(hwnd, 0, HTCAPTION); // change for 64-bit build
return TRUE;
}
break;
// huh... CLANG defines this in winuser.h, but tdm32 does not...
// So clang-tidy complains, but I cannot remove this
// #define WM_DWMCOLORIZATIONCOLORCHANGED 0x320 // NOLINT
// DerBar: [WM_NCLBUTTONDBLCLK]
case WM_DWMCOLORIZATIONCOLORCHANGED:
case WM_NCLBUTTONDBLCLK:
reset_icon_colors(false);
break;
case WM_COMMAND:
{
DWORD cmd = HIWORD(wParam) ;
DWORD target = LOWORD(wParam) ;
if (show_winmsgs) {
syslog(_T("WM_COMMAND: cmd: %08X, target: %08X\n"), cmd, target);
}
// If a button is clicked...
if (cmd == BN_CLICKED) {
switch (target) {
// Open Network Ifaces listview
case IDM_IFACES:
spawn_ifaces_lview(hwnd) ;
return TRUE;
case IDM_TOGGLE_TBAR:
toggle_title_bar(hwnd) ;
return TRUE;
case IDM_RELOCATE:
relocate_main_dialog(hwnd) ;
return TRUE;
case IDM_SET_NOW:
reset_icon_colors(false);
return TRUE;
case IDM_SET_COLOR:
reset_icon_colors(true);
return TRUE;
case IDM_ABOUT:
CmdAbout(hwnd);
return TRUE;
case IDM_OPTIONS:
open_options_dialog(hwnd);
return TRUE;
case IDM_APP_EXIT:
DestroyWindow(hwnd);
return TRUE;
default:
return FALSE;
} //lint !e744
}
}
break;
case WM_CTLCOLORSTATIC:
hdc = (HDC) wParam;
if ((HWND) lParam == hwndDerBar) {
SetTextColor(hdc, WIN_BCYAN);
SetBkColor(hdc, WIN_BLUE);
if (hbLogo == 0)
hbLogo = CreateSolidBrush(WIN_BLUE) ;
return (LRESULT) hbLogo ; // hilight colour
}
if ((HWND) lParam == hwndSMem ||
(HWND) lParam == hwndSFree ||
(HWND) lParam == hwndSTotal ||
(HWND) lParam == hwndSUpTime ||
(HWND) lParam == hwndSRX ||
(HWND) lParam == hwndSTX ||
(HWND) lParam == hwndSCPU ) {
SetTextColor(hdc, fgnd_static);
SetBkColor(hdc, bgnd_static);
if (hbStatic == 0)
hbStatic = CreateSolidBrush(bgnd_static) ;
return (LRESULT) hbStatic ;
}
if ((HWND) lParam == hwndFreeMem ||
(HWND) lParam == hwndTotalMem ||
(HWND) lParam == hwndUptime ||
(HWND) lParam == hwndRxBytes ||
(HWND) lParam == hwndTxBytes ||
(HWND) lParam == hwndCpuTime) {
if (isMemoryLow && (HWND) lParam == hwndFreeMem) {
SetTextColor(hdc, WIN_WHITE);
SetBkColor(hdc, WIN_RED);
hbEdit = CreateSolidBrush(WIN_RED) ;
} else {
SetTextColor(hdc, fgnd_edit);
SetBkColor(hdc, bgnd_edit);
if (hbEdit == 0)
hbEdit = CreateSolidBrush(bgnd_edit) ;
}
return (LRESULT) hbEdit ; // hilight colour
}
if ((HWND) lParam == hwndKbdCaps) {
if (kbd_state & 4) {
SetTextColor(hdc, WIN_BCYAN);
SetBkColor(hdc, bgnd_edit);
if (hbEdit == 0)
hbEdit = CreateSolidBrush(bgnd_edit) ;
return (LRESULT) hbEdit ; // hilight colour
}
}
if ((HWND) lParam == hwndKbdNum) {
if (kbd_state & 2) {
SetTextColor(hdc, WIN_BCYAN);
SetBkColor(hdc, bgnd_edit);
if (hbEdit == 0)
hbEdit = CreateSolidBrush(bgnd_edit) ;
return (LRESULT) hbEdit ; // hilight colour
}
}
if ((HWND) lParam == hwndKbdScrl) {
if (kbd_state & 1) {
SetTextColor(hdc, WIN_BCYAN);
SetBkColor(hdc, bgnd_edit);
if (hbEdit == 0)
hbEdit = CreateSolidBrush(bgnd_edit) ;
return (LRESULT) hbEdit ; // hilight colour
}
}
break;
case WM_TIMER:
switch (wParam) {
case IDT_TIMER:
#ifdef USE_TIMER_TEST
update_time_count() ;
#endif
read_system_data() ;
update_data_fields() ;
if (!first_pass_done) {
first_pass_done = true ;
reset_icon_colors(false);
}
return TRUE;
} //lint !e744
break;
// We cannot trigger on WM_ERASEBKGND or WM_CTLCOLORDLG,
// because we want to hide the main window.
// Later: now that I've attached ClearIcon to DerBar,
// which is always displayed, the above restrictions do not matter.
// Thus, we trigger on WM_SYSCOLORCHANGE instead.
// 00000002 11:37:16.650 [6380] SWMsg: [WM_SYSCOLORCHANGE]
// 00000003 11:37:16.650 [6380] SWMsg: [WM_PAINT]
// 00000004 11:37:16.650 [6380] SWMsg: [WM_NCPAINT]
// 00000005 11:37:16.655 [6380] SWMsg: [WM_ERASEBKGND]
// 00000006 11:37:16.655 [6380] SWMsg: [WM_CTLCOLORDLG]
// case WM_SYSCOLORCHANGE:
// 00000826 13:10:04.124 [5108] SWMsg: [WM_DISPLAYCHANGE]
// 00000827 13:10:04.125 [4104] 2017-04-07 13:10:10.824 ( 87756.863) |
// DEBUG: [UXDriver.ApiX.MessageTranslator] 325@Nvidia::UXDriver::ApiX::MessageTranslator::Translate :
// message(0x7e) wparam(0x20) lParam(0x6400a00): 9, translated(1).
// case WM_DISPLAYCHANGE:
// Messages rcvd when exiting from Witcher 3
// 00000042 08:28:48.089 [10444] DBGHELP:
// 00000043 08:28:48.108 [5336] DerBar: [WM_PAINT]
// 00000044 08:28:48.108 [5336] DerBar: [WM_NCPAINT]
// 00000045 08:28:48.108 [5336] DerBar: [WM_ERASEBKGND]
// 00000046 08:28:48.108 [5336] DerBar: [WM_CTLCOLORDLG]
// Since I'm not receiving WM_SYSCOLORCHANGE in this case,
// I'll try switching to one of the others instead.
//
// 12/09/17 - From Divinity: Original Sin 2, I receive:
// 00000071 13:54:33.658 [3692] DerBar: [WM_DISPLAYCHANGE]
// 00000072 13:54:34.227 [3692] DerBar: [WM_SYNCPAINT ]
// 00000073 13:54:34.229 [3692] DerBar: [WM_NCPAINT]
// 00000074 13:54:34.231 [3692] DerBar: [WM_ERASEBKGND]
// 00000075 13:54:34.239 [3692] DerBar: [WM_SYNCPAINT ]
// 00000076 13:54:34.246 [3692] DerBar: [WM_CTLCOLORDLG]
// 00000077 13:54:34.246 [3692] DerBar: [WM_PAINT]
// but trapping WM_PAINT did not work either
case WM_ERASEBKGND:
reset_icon_colors(false);
break;
//********************************************************************
// application shutdown handlers
//********************************************************************
case WM_CLOSE:
DestroyWindow(hwnd);
return TRUE;
case WM_DESTROY:
if (timerID != 0) {
KillTimer(hwnd, timerID) ;
timerID = 0 ;
}
#ifdef USE_CPU_UTIL
cpu_usage_release();
#endif
// remove the icon from a system tray and free .dll handle
release_systray_resource();
release_led_images();
PostQuitMessage(0);
return TRUE;
default:
return FALSE ;
} //lint !e744
return FALSE;
} //lint !e715
//***********************************************************************
static BOOL WeAreAlone(TCHAR *szName)
{
HANDLE hMutex = CreateMutex (NULL, TRUE, szName);
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
CloseHandle(hMutex);
return FALSE;
}
return TRUE;
}
//*********************************************************************
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpszArgument, int nShowCmd)
{
if (!WeAreAlone (szClassName)) {
MessageBox(NULL, _T("DerBar is already running!!"), _T("collision"), MB_OK | MB_ICONEXCLAMATION) ;
return 0;
}
g_hinst = hInstance;
load_exec_filename() ; // get our executable name
// build one-time network tables
build_iface_tables() ;
update_iface_counters() ;
// do this *after* building iface tables
read_config_file() ; // read current screen position, etc
#ifdef USE_CPU_UTIL
cpu_usage_setup();
#endif
load_led_images() ; // load our image list
// create the main application
HWND hwnd = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, (DLGPROC) WndProc);
if (hwnd == NULL) {
// Notified your about the failure
syslog(_T("CreateDialog (main): %s [%u]\n"), get_system_message(), GetLastError()) ;
// Set the return value
return FALSE;
}
ShowWindow (hwnd, SW_SHOW) ;
UpdateWindow(hwnd);
MSG msg ;
while (GetMessage (&msg, NULL, 0, 0)) {
if (!IsDialogMessage(hwnd, &msg)) {
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
return (int) msg.wParam ;
} //lint !e715