This repository has been archived by the owner on Oct 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmayu.cpp
1003 lines (910 loc) · 26.4 KB
/
mayu.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
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// mayu.cpp
#define APSTUDIO_INVOKED
#include "misc.h"
#include "compiler_specific_func.h"
#if defined(WIN32)
#include "dlginvestigate.h"
#include "dlglog.h"
#include "dlgsetting.h"
#include "dlgversion.h"
#endif
#include "engine.h"
#include "errormessage.h"
#if defined(WIN32)
#include "focus.h"
#endif
#include "function.h"
#if defined(WIN32)
#include "hook.h"
#endif
#include "mayu.h"
#if defined(WIN32)
#include "mayurc.h"
#endif
#include "msgstream.h"
#include "multithread.h"
#if defined(WIN32)
#include "registry.h"
#endif
#include "setting.h"
#if defined(WIN32)
#include "target.h"
#include "windowstool.h"
#include <process.h>
#endif
#include <time.h>
#if defined(WIN32)
#include <commctrl.h>
#include <wtsapi32.h>
#endif
///
#define ID_MENUITEM_reloadBegin _APS_NEXT_COMMAND_VALUE
// global variable
int __argc;
_TCHAR** __targv;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Mayu
///
class Mayu
{
#if defined(WIN32)
HWND m_hwndTaskTray; /// tasktray window
HWND m_hwndLog; /// log dialog
HWND m_hwndInvestigate; /// investigate dialog
HWND m_hwndVersion; /// version dialog
UINT m_WM_TaskbarRestart; /** window message sent when
taskber restarts */
NOTIFYICONDATA m_ni; /// taskbar icon data
HICON m_tasktrayIcon[2]; /// taskbar icon
bool m_canUseTasktrayBaloon; ///
HMENU m_hMenuTaskTray; /// tasktray menu
bool m_isSettingDialogOpened; /// is setting dialog opened ?
enum
{
WM_APP_taskTrayNotify = WM_APP + 101, ///
WM_APP_msgStreamNotify = WM_APP + 102, ///
ID_TaskTrayIcon = 1, ///
};
#endif
Engine m_engine; /// engine
tomsgstream m_log; /** log stream (output to log
dialog's edit) */
Setting *m_setting; /// current setting
bool m_usingSN; /// using WTSRegisterSessionNotification() ?
time_t m_startTime; /// mayu started at ...
private:
#if defined(WIN32)
/// register class for tasktray
ATOM Register_tasktray()
{
WNDCLASS wc;
wc.style = 0;
wc.lpfnWndProc = tasktray_wndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = sizeof(Mayu *);
wc.hInstance = g_hInst;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = _T("mayuTasktray");
return RegisterClass(&wc);
}
/// notify handler
BOOL notifyHandler(COPYDATASTRUCT *cd)
{
switch (cd->dwData)
{
case Notify::Type_setFocus:
case Notify::Type_name:
{
NotifySetFocus *n = (NotifySetFocus *)cd->lpData;
n->m_className[NUMBER_OF(n->m_className) - 1] = _T('\0');
n->m_titleName[NUMBER_OF(n->m_titleName) - 1] = _T('\0');
if (n->m_type == Notify::Type_setFocus)
m_engine.setFocus(n->m_hwnd, n->m_threadId,
n->m_className, n->m_titleName, false);
{
Acquire a(&m_log, 1);
m_log << _T("HWND:\t") << std::hex
<< reinterpret_cast<int>(n->m_hwnd)
<< std::dec << std::endl;
m_log << _T("THREADID:") << static_cast<int>(n->m_threadId)
<< std::endl;
}
Acquire a(&m_log, (n->m_type == Notify::Type_name) ? 0 : 1);
m_log << _T("CLASS:\t") << n->m_className << std::endl;
m_log << _T("TITLE:\t") << n->m_titleName << std::endl;
bool isMDI = true;
HWND hwnd = getToplevelWindow(n->m_hwnd, &isMDI);
RECT rc;
if (isMDI)
{
getChildWindowRect(hwnd, &rc);
m_log << _T("MDI Window Position/Size: (")
<< rc.left << _T(", ") << rc.top << _T(") / (")
<< rcWidth(&rc) << _T("x") << rcHeight(&rc) << _T(")")
<< std::endl;
hwnd = getToplevelWindow(n->m_hwnd, NULL);
}
GetWindowRect(hwnd, &rc);
m_log << _T("Toplevel Window Position/Size: (")
<< rc.left << _T(", ") << rc.top << _T(") / (")
<< rcWidth(&rc) << _T("x") << rcHeight(&rc) << _T(")")
<< std::endl;
SystemParametersInfo(SPI_GETWORKAREA, 0, (void *)&rc, FALSE);
m_log << _T("Desktop Window Position/Size: (")
<< rc.left << _T(", ") << rc.top << _T(") / (")
<< rcWidth(&rc) << _T("x") << rcHeight(&rc) << _T(")")
<< std::endl;
m_log << std::endl;
break;
}
case Notify::Type_lockState:
{
NotifyLockState *n = (NotifyLockState *)cd->lpData;
m_engine.setLockState(n->m_isNumLockToggled,
n->m_isCapsLockToggled,
n->m_isScrollLockToggled,
n->m_isKanaLockToggled,
n->m_isImeLockToggled,
n->m_isImeCompToggled);
#if 0
Acquire a(&m_log, 0);
if (n->m_isKanaLockToggled) {
m_log << _T("Notify::Type_lockState Kana on : ");
} else {
m_log << _T("Notify::Type_lockState Kana off : ");
}
m_log << n->m_debugParam << ", "
<< g_hookData->m_correctKanaLockHandling << std::endl;
#endif
break;
}
case Notify::Type_sync:
{
m_engine.syncNot // destroy ify();
break;
}
case Notify::Type_threadDetach:
{
NotifyThreadDetach *n = (NotifyThreadDetach *)cd->lpData;
m_engine.threadDetachNotify(n->m_threadId);
break;
}
case Notify::Type_command:
{
NotifyCommand *n = (NotifyCommand *)cd->lpData;
m_engine.commandNotify(n->m_hwnd, n->m_message,
n->m_wParam, n->m_lParam);
break;
}
case Notify::Type_show:
{
NotifyShow *n = (NotifyShow *)cd->lpData;
switch (n->m_show)
{
case NotifyShow::Show_Maximized:
m_engine.setShow(true, false, n->m_isMDI);
break;
case NotifyShow::Show_Minimized:
m_engine.setShow(false, true, n->m_isMDI);
break;
case NotifyShow::Show_Normal:
default:
m_engine.setShow(false, false, n->m_isMDI);
break;
}
break;
}
case Notify::Type_log:
{
Acquire a(&m_log, 1);
NotifyLog *n = (NotifyLog *)cd->lpData;
m_log << _T("hook log: ") << n->m_msg << std::endl;
break;
}
}
return true;
}
/// window procedure for tasktray
static LRESULT CALLBACK
tasktray_wndProc(HWND i_hwnd, UINT i_message,
WPARAM i_wParam, LPARAM i_lParam)
{
Mayu *This = reinterpret_cast<Mayu *>(GetWindowLong(i_hwnd, 0));
if (!This)
switch (i_message)
{
case WM_CREATE:
This = reinterpret_cast<Mayu *>(
reinterpret_cast<CREATESTRUCT *>(i_lParam)->lpCreateParams);
SetWindowLong(i_hwnd, 0, (long)This);
return 0;
}
else
switch (i_message)
{
case WM_COPYDATA:
{
COPYDATASTRUCT *cd;
cd = reinterpret_cast<COPYDATASTRUCT *>(i_lParam);
return This->notifyHandler(cd);
}
case WM_QUERYENDSESSION:
PostQuitMessage(0);
return TRUE;
#ifndef WM_WTSSESSION_CHANGE // WinUser.h
# define WM_WTSSESSION_CHANGE 0x02B1
#endif
case WM_WTSSESSION_CHANGE:
{
const char *m = "";
switch (i_wParam)
{
#ifndef WTS_CONSOLE_CONNECT // WinUser.h
# define WTS_CONSOLE_CONNECT 0x1
# define WTS_CONSOLE_DISCONNECT 0x2
# define WTS_REMOTE_CONNECT 0x3
# define WTS_REMOTE_DISCONNECT 0x4
# define WTS_SESSION_LOGON 0x5
# define WTS_SESSION_LOGOFF 0x6
# define WTS_SESSION_LOCK 0x7
# define WTS_SESSION_UNLOCK 0x8
#endif
case WTS_CONSOLE_CONNECT:
m = "WTS_CONSOLE_CONNECT";
if (!This->m_engine.resume()) {
PostQuitMessage(0);
}
break;
case WTS_CONSOLE_DISCONNECT:
m = "WTS_CONSOLE_DISCONNECT";
This->m_engine.pause();
break;
case WTS_REMOTE_CONNECT: m = "WTS_REMOTE_CONNECT"; break;
case WTS_REMOTE_DISCONNECT: m = "WTS_REMOTE_DISCONNECT"; break;
case WTS_SESSION_LOGON: m = "WTS_SESSION_LOGON"; break;
case WTS_SESSION_LOGOFF: m = "WTS_SESSION_LOGOFF"; break;
case WTS_SESSION_LOCK: m = "WTS_SESSION_LOCK"; break;
case WTS_SESSION_UNLOCK: m = "WTS_SESSION_UNLOCK"; break;
//case WTS_SESSION_REMOTE_CONTROL: m = "WTS_SESSION_REMOTE_CONTROL"; break;
}
This->m_log << _T("WM_WTSESSION_CHANGE(")
<< i_wParam << ", " << i_lParam << "): "
<< m << std::endl;
return TRUE;
}
case WM_APP_msgStreamNotify:
{
tomsgstream::StreamBuf *log =
reinterpret_cast<tomsgstream::StreamBuf *>(i_lParam);
const tstring &str = log->acquireString();
editInsertTextAtLast(GetDlgItem(This->m_hwndLog, IDC_EDIT_log),
str, 65000);
log->releaseString();
return 0;
}
case WM_APP_taskTrayNotify:
{
if (i_wParam == ID_TaskTrayIcon)
switch (i_lParam)
{
case WM_RBUTTONUP:
{
POINT p;
CHECK_TRUE( GetCursorPos(&p) );
SetForegroundWindow(i_hwnd);
HMENU hMenuSub = GetSubMenu(This->m_hMenuTaskTray, 0);
if (This->m_engine.getIsEnabled())
CheckMenuItem(hMenuSub, ID_MENUITEM_disable,
MF_UNCHECKED | MF_BYCOMMAND);
else
CheckMenuItem(hMenuSub, ID_MENUITEM_disable,
MF_CHECKED | MF_BYCOMMAND);
CHECK_TRUE( SetMenuDefaultItem(hMenuSub,
ID_MENUITEM_investigate, FALSE) );
// create reload menu
HMENU hMenuSubSub = GetSubMenu(hMenuSub, 1);
Registry reg(MAYU_REGISTRY_ROOT);
int mayuIndex;
reg.read(_T(".mayuIndex"), &mayuIndex, 0);
while (DeleteMenu(hMenuSubSub, 0, MF_BYPOSITION))
;
tregex getName(_T("^([^;]*);"));
for (int index = 0; ; index ++)
{
_TCHAR buf[100];
_sntprintf(buf, NUMBER_OF(buf), _T(".mayu%d"), index);
tstringi dot_mayu;
if (!reg.read(buf, &dot_mayu))
break;
tcmatch_results what;
if (boost::regex_search(dot_mayu, what, getName))
{
MENUITEMINFO mii;
std::memset(&mii, 0, sizeof(mii));
mii.cbSize = sizeof(mii);
mii.fMask = MIIM_ID | MIIM_STATE | MIIM_TYPE;
mii.fType = MFT_STRING;
mii.fState =
MFS_ENABLED | ((mayuIndex == index) ? MFS_CHECKED : 0);
mii.wID = ID_MENUITEM_reloadBegin + index;
tstringi name(what.str(1));
mii.dwTypeData = const_cast<_TCHAR *>(name.c_str());
mii.cch = name.size();
InsertMenuItem(hMenuSubSub, index, TRUE, &mii);
}
}
// show popup menu
TrackPopupMenu(hMenuSub, TPM_LEFTALIGN,
p.x, p.y, 0, i_hwnd, NULL);
// TrackPopupMenu may fail (ERROR_POPUP_ALREADY_ACTIVE)
break;
}
case WM_LBUTTONDBLCLK:
SendMessage(i_hwnd, WM_COMMAND,
MAKELONG(ID_MENUITEM_investigate, 0), 0);
break;
}
return 0;
}
case WM_COMMAND:
{
int notify_code = HIWORD(i_wParam);
int id = LOWORD(i_wParam);
if (notify_code == 0) // menu
switch (id)
{
default:
if (ID_MENUITEM_reloadBegin <= id)
{
Registry reg(MAYU_REGISTRY_ROOT);
reg.write(_T(".mayuIndex"), id - ID_MENUITEM_reloadBegin);
This->load();
}
break;
case ID_MENUITEM_reload:
This->load();
break;
case ID_MENUITEM_investigate:
{
ShowWindow(This->m_hwndLog, SW_SHOW);
ShowWindow(This->m_hwndInvestigate, SW_SHOW);
RECT rc1, rc2;
GetWindowRect(This->m_hwndInvestigate, &rc1);
GetWindowRect(This->m_hwndLog, &rc2);
MoveWindow(This->m_hwndLog, rc1.left, rc1.bottom,
rcWidth(&rc1), rcHeight(&rc2), TRUE);
SetForegroundWindow(This->m_hwndLog);
SetForegroundWindow(This->m_hwndInvestigate);
break;
}
case ID_MENUITEM_setting:
if (!This->m_isSettingDialogOpened)
{
This->m_isSettingDialogOpened = true;
if (DialogBox(g_hInst, MAKEINTRESOURCE(IDD_DIALOG_setting),
NULL, dlgSetting_dlgProc))
This->load();
This->m_isSettingDialogOpened = false;
}
break;
case ID_MENUITEM_log:
ShowWindow(This->m_hwndLog, SW_SHOW);
SetForegroundWindow(This->m_hwndLog);
break;
case ID_MENUITEM_version:
ShowWindow(This->m_hwndVersion, SW_SHOW);
SetForegroundWindow(This->m_hwndVersion);
break;
case ID_MENUITEM_help:
{
_TCHAR buf[GANA_MAX_PATH];
CHECK_TRUE( GetModuleFileName(g_hInst, buf, NUMBER_OF(buf)) );
tstringi helpFilename = pathRemoveFileSpec(buf);
helpFilename += _T("\\");
helpFilename += loadString(IDS_helpFilename);
ShellExecute(NULL, _T("open"), helpFilename.c_str(),
NULL, NULL, SW_SHOWNORMAL);
break;
}
case ID_MENUITEM_disable:
This->m_engine.enable(!This->m_engine.getIsEnabled());
This->showTasktrayIcon();
break;
case ID_MENUITEM_quit:
PostQuitMessage(0);
break;
}
return 0;
}
case WM_APP_engineNotify:
{
switch (i_wParam)
{
case EngineNotify_shellExecute:
This->m_engine.shellExecute();
break;
case EngineNotify_loadSetting:
This->load();
break;
case EngineNotify_helpMessage:
This->showHelpMessage(false);
if (i_lParam)
This->showHelpMessage(true);
break;
case EngineNotify_showDlg:
{
// show investigate/log window
int sw = (i_lParam & ~MayuDialogType_mask);
HWND hwnd = NULL;
switch (static_cast<MayuDialogType>(
i_lParam & MayuDialogType_mask))
{
case MayuDialogType_investigate:
hwnd = This->m_hwndInvestigate;
break;
case MayuDialogType_log:
hwnd = This->m_hwndLog;
break;
}
if (hwnd)
{
ShowWindow(hwnd, sw);
switch (sw)
{
case SW_SHOWNORMAL:
case SW_SHOWMAXIMIZED:
case SW_SHOW:
case SW_RESTORE:
case SW_SHOWDEFAULT:
SetForegroundWindow(hwnd);
break;
}
}
break;
}
case EngineNotify_setForegroundWindow:
// FIXME: completely useless. why ?
setForegroundWindow(reinterpret_cast<HWND>(i_lParam));
{
Acquire a(&This->m_log, 1);
This->m_log << _T("setForegroundWindow(0x")
<< std::hex << i_lParam << std::dec << _T(")")
<< std::endl;
}
break;
case EngineNotify_clearLog:
SendMessage(This->m_hwndLog, WM_COMMAND,
MAKELONG(IDC_BUTTON_clearLog, 0), 0);
break;
default:
break;
}
return 0;
}
case WM_APP_dlglogNotify:
{
switch (i_wParam)
{
case DlgLogNotify_logCleared:
This->showBanner(true);
break;
default:
break;
}
return 0;
}
case WM_DESTROY:
if (This->m_usingSN)
{
wtsUnRegisterSessionNotification(i_hwnd);
This->m_usingSN = false;
}
return 0;
default:
if (i_message == This->m_WM_TaskbarRestart)
{
if (This->showTasktrayIcon(true))
{
Acquire a(&This->m_log, 0);
This->m_log << _T("Tasktray icon is updated.") << std::endl;
}
else
{
Acquire a(&This->m_log, 1);
This->m_log << _T("Tasktray icon already exists.") << std::endl;
}
return 0;
}
}
return DefWindowProc(i_hwnd, i_message, i_wParam, i_lParam);
}
#endif
/// load setting
void load()
{
Setting *newSetting = new Setting;
// set symbol
for (int i = 1; i < __argc; ++ i)
{
if (__targv[i][0] == _T('-') && __targv[i][1] == _T('D'))
newSetting->m_symbols.insert(__targv[i] + 2);
}
if (!SettingLoader(&m_log, &m_log).load(newSetting))
{
#if defined(WIN32)
ShowWindow(m_hwndLog, SW_SHOW);
SetForegroundWindow(m_hwndLog);
#endif
delete newSetting;
Acquire a(&m_log, 0);
m_log << _T("error: failed to load.") << std::endl;
return;
}
m_log << _T("successfully loaded.") << std::endl;
while (!m_engine.setSetting(newSetting))
#if defined(WIN32)
Sleep(1000);
#elif defined(__linux__)
sleep(1);
# elif defined(__APPLE__)
sleep(1);
#endif
delete m_setting;
m_setting = newSetting;
}
#if defined(WIN32)
// show message (a baloon from the task tray icon)
void showHelpMessage(bool i_doesShow = true)
{
if (m_canUseTasktrayBaloon)
{
if (i_doesShow)
{
tstring helpMessage, helpTitle;
m_engine.getHelpMessages(&helpMessage, &helpTitle);
tcslcpy(m_ni.szInfo, helpMessage.c_str(), NUMBER_OF(m_ni.szInfo));
tcslcpy(m_ni.szInfoTitle, helpTitle.c_str(),
NUMBER_OF(m_ni.szInfoTitle));
m_ni.dwInfoFlags = NIIF_INFO;
}
else
m_ni.szInfo[0] = m_ni.szInfoTitle[0] = _T('\0');
CHECK_TRUE( Shell_NotifyIcon(NIM_MODIFY, &m_ni) );
}
}
// change the task tray icon
bool showTasktrayIcon(bool i_doesAdd = false)
{
m_ni.hIcon = m_tasktrayIcon[m_engine.getIsEnabled() ? 1 : 0];
m_ni.szInfo[0] = m_ni.szInfoTitle[0] = _T('\0');
if (i_doesAdd) {
// http://support.microsoft.com/kb/418138/JA/
int guard = 60;
for (; !Shell_NotifyIcon(NIM_ADD, &m_ni) && 0 < guard; -- guard) {
if (Shell_NotifyIcon(NIM_MODIFY, &m_ni)) {
return true;
}
Sleep(1000); // 1sec
}
return 0 < guard;
} else {
return !!Shell_NotifyIcon(NIM_MODIFY, &m_ni);
}
}
void showBanner(bool i_isCleared)
{
time_t now;
time(&now);
_TCHAR starttimebuf[1024];
_TCHAR timebuf[1024];
#ifdef __BORLANDC__
#pragma message("\t\t****\tAfter std::ostream() is called, ")
#pragma message("\t\t****\tstrftime(... \"%%#c\" ...) fails.")
#pragma message("\t\t****\tWhy ? Bug of Borland C++ 5.5.1 ? ")
#pragma message("\t\t****\t - nayuta")
_tcsftime(timebuf, NUMBER_OF(timebuf), _T("%Y/%m/%d %H:%M:%S"),
localtime(&now));
_tcsftime(starttimebuf, NUMBER_OF(starttimebuf), _T("%Y/%m/%d %H:%M:%S"),
localtime(&m_startTime));
#else
_tcsftime(timebuf, NUMBER_OF(timebuf), _T("%#c"), localtime(&now));
_tcsftime(starttimebuf, NUMBER_OF(starttimebuf), _T("%#c"),
localtime(&m_startTime));
#endif
Acquire a(&m_log, 0);
m_log << _T("------------------------------------------------------------") << std::endl;
m_log << loadString(IDS_mayu) << _T(" ") _T(VERSION);
#ifndef NDEBUG
m_log << _T(" (DEBUG)");
#endif
#ifdef _UNICODE
m_log << _T(" (UNICODE)");
#endif
m_log << std::endl;
m_log << _T(" built by ")
<< _T(LOGNAME) << _T("@") << toLower(_T(COMPUTERNAME))
<< _T(" (") << _T(__DATE__) << _T(" ")
<< _T(__TIME__) << _T(", ")
<< getCompilerVersionString() << _T(")") << std::endl;
_TCHAR modulebuf[1024];
CHECK_TRUE( GetModuleFileName(g_hInst, modulebuf,
NUMBER_OF(modulebuf)) );
m_log << _T("started at ") << starttimebuf << std::endl;
m_log << modulebuf << std::endl;
m_log << _T("------------------------------------------------------------") << std::endl;
if (i_isCleared) {
m_log << _T("log was cleared at ") << timebuf << std::endl;
} else {
m_log << _T("log begins at ") << timebuf << std::endl;
}
}
#endif
public:
///
Mayu()
:
#if defined(WIN32)
m_hwndTaskTray(NULL),
m_hwndLog(NULL),
m_WM_TaskbarRestart(RegisterWindowMessage(_T("TaskbarCreated"))),
m_canUseTasktrayBaloon(
PACKVERSION(5, 0) <= getDllVersion(_T("shlwapi.dll"))),
m_isSettingDialogOpened(false),
m_log(WM_APP_msgStreamNotify),
#elif defined(__linux__) || defined(__APPLE__)
m_log(),
#endif
m_setting(NULL),
m_engine(m_log)
{
time(&m_startTime);
#if defined(WIN32)
CHECK_TRUE( Register_focus() );
CHECK_TRUE( Register_target() );
CHECK_TRUE( Register_tasktray() );
// change dir
#if 0
HomeDirectories pathes;
getHomeDirectories(&pathes);
for (HomeDirectories::iterator i = pathes.begin(); i != pathes.end(); ++ i)
if (SetCurrentDirectory(i->c_str()))
break;
#endif
// create windows, dialogs
tstringi title = loadString(IDS_mayu);
m_hwndTaskTray = CreateWindow(_T("mayuTasktray"), title.c_str(),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, g_hInst, this);
CHECK_TRUE( m_hwndTaskTray );
// set window handle of tasktray to hooks
g_hookData->m_hwndTaskTray = m_hwndTaskTray;
m_usingSN = wtsRegisterSessionNotification(m_hwndTaskTray,
NOTIFY_FOR_THIS_SESSION);
DlgLogData dld;
dld.m_log = &m_log;
dld.m_hwndTaskTray = m_hwndTaskTray;
m_hwndLog =
CreateDialogParam(g_hInst, MAKEINTRESOURCE(IDD_DIALOG_log), NULL,
dlgLog_dlgProc, (LPARAM)&dld);
CHECK_TRUE( m_hwndLog );
DlgInvestigateData did;
did.m_engine = &m_engine;
did.m_hwndLog = m_hwndLog;
m_hwndInvestigate =
CreateDialogParam(g_hInst, MAKEINTRESOURCE(IDD_DIALOG_investigate), NULL,
dlgInvestigate_dlgProc, (LPARAM)&did);
CHECK_TRUE( m_hwndInvestigate );
m_hwndVersion =
CreateDialogParam(g_hInst, MAKEINTRESOURCE(IDD_DIALOG_version),
NULL, dlgVersion_dlgProc,
(LPARAM)m_engine.getMayudVersion().c_str());
CHECK_TRUE( m_hwndVersion );
// attach log
SendMessage(GetDlgItem(m_hwndLog, IDC_EDIT_log), EM_SETLIMITTEXT, 0, 0);
m_log.attach(m_hwndTaskTray);
// start keyboard handler thread
m_engine.setAssociatedWndow(m_hwndTaskTray);
#endif
m_engine.start();
#if defined(WIN32)
// show tasktray icon
m_tasktrayIcon[0] = loadSmallIcon(IDI_ICON_mayu_disabled);
m_tasktrayIcon[1] = loadSmallIcon(IDI_ICON_mayu);
std::memset(&m_ni, 0, sizeof(m_ni));
m_ni.uID = ID_TaskTrayIcon;
m_ni.hWnd = m_hwndTaskTray;
m_ni.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
m_ni.hIcon = m_tasktrayIcon[1];
m_ni.uCallbackMessage = WM_APP_taskTrayNotify;
tstring tip = loadString(IDS_mayu);
tcslcpy(m_ni.szTip, tip.c_str(), NUMBER_OF(m_ni.szTip));
if (m_canUseTasktrayBaloon)
{
m_ni.cbSize = sizeof(m_ni);
m_ni.uFlags |= NIF_INFO;
}
else
m_ni.cbSize = NOTIFYICONDATA_V1_SIZE;
showTasktrayIcon(true);
// create menu
m_hMenuTaskTray = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MENU_tasktray));
ASSERT(m_hMenuTaskTray);
// set initial lock state
notifyLockState();
#endif
}
///
~Mayu()
{
#if defined(WIN32)
// first, detach log from edit control to avoid deadlock
m_log.detach();
// stop notify from mayu.dll
g_hookData->m_hwndTaskTray = NULL;
// destroy windows
CHECK_TRUE( DestroyWindow(m_hwndVersion) );
CHECK_TRUE( DestroyWindow(m_hwndInvestigate) );
CHECK_TRUE( DestroyWindow(m_hwndLog) );
CHECK_TRUE( DestroyWindow(m_hwndTaskTray) );
// destroy menu
DestroyMenu(m_hMenuTaskTray);
// delete tasktray icon
CHECK_TRUE( Shell_NotifyIcon(NIM_DELETE, &m_ni) );
CHECK_TRUE( DestroyIcon(m_tasktrayIcon[1]) );
CHECK_TRUE( DestroyIcon(m_tasktrayIcon[0]) );
#endif
// stop keyboard handler thread
m_engine.stop();
// remove setting;
delete m_setting;
}
#if defined(WIN32)
/// message loop
WPARAM messageLoop()
{
showBanner(false);
load();
MSG msg;
while (0 < GetMessage(&msg, NULL, 0, 0))
{
if (IsDialogMessage(m_hwndLog, &msg))
continue;
if (IsDialogMessage(m_hwndInvestigate, &msg))
continue;
if (IsDialogMessage(m_hwndVersion, &msg))
continue;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
#elif defined(__linux__)
// TODO:
# elif defined(__APPLE__)
#endif
};
#if defined(WIN32)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Functions
/// convert registry
void convertRegistry()
{
Registry reg(MAYU_REGISTRY_ROOT);
tstringi dot_mayu;
bool doesAdd = false;
DWORD index;
if (reg.read(_T(".mayu"), &dot_mayu))
{
reg.write(_T(".mayu0"), _T(";") + dot_mayu + _T(";"));
reg.remove(_T(".mayu"));
doesAdd = true;
index = 0;
}
else if (!reg.read(_T(".mayu0"), &dot_mayu))
{
reg.write(_T(".mayu0"), loadString(IDS_readFromHomeDirectory) + _T(";;"));
doesAdd = true;
index = 1;
}
if (doesAdd)
{
Registry commonreg(HKEY_LOCAL_MACHINE, _T("Software\\GANAware\\mayu"));
tstringi dir, layout;
if (commonreg.read(_T("dir"), &dir) &&
commonreg.read(_T("layout"), &layout))
{
tstringi tmp = _T(";") + dir + _T("\\dot.mayu");
if (layout == _T("109"))
{
reg.write(_T(".mayu1"), loadString(IDS_109Emacs) + tmp
+ _T(";-DUSE109") _T(";-DUSEdefault"));
reg.write(_T(".mayu2"), loadString(IDS_104on109Emacs) + tmp
+ _T(";-DUSE109") _T(";-DUSEdefault") _T(";-DUSE104on109"));
reg.write(_T(".mayu3"), loadString(IDS_109) + tmp
+ _T(";-DUSE109"));
reg.write(_T(".mayu4"), loadString(IDS_104on109) + tmp
+ _T(";-DUSE109") _T(";-DUSE104on109"));
}
else
{
reg.write(_T(".mayu1"), loadString(IDS_104Emacs) + tmp
+ _T(";-DUSE104") _T(";-DUSEdefault"));
reg.write(_T(".mayu2"), loadString(IDS_109on104Emacs) + tmp
+ _T(";-DUSE104") _T(";-DUSEdefault") _T(";-DUSE109on104"));
reg.write(_T(".mayu3"), loadString(IDS_104) + tmp
+ _T(";-DUSE104"));
reg.write(_T(".mayu4"), loadString(IDS_109on104) + tmp
+ _T(";-DUSE104") _T(";-DUSE109on104"));
}
reg.write(_T(".mayuIndex"), index);
}
}
}
#endif
#if defined(WIN32)
/// main
int WINAPI _tWinMain(HINSTANCE i_hInstance, HINSTANCE /* i_hPrevInstance */,
LPTSTR /* i_lpszCmdLine */, int /* i_nCmdShow */)
{
g_hInst = i_hInstance;
// set locale
CHECK_TRUE( _tsetlocale(LC_ALL, _T("")) );
// common controls
#if defined(_WIN95)
InitCommonControls();
#else
INITCOMMONCONTROLSEX icc;
icc.dwSize = sizeof(icc);
icc.dwICC = ICC_LISTVIEW_CLASSES;
CHECK_TRUE( InitCommonControlsEx(&icc) );
#endif
// convert old registry to new registry
convertRegistry();
// is another mayu running ?
HANDLE mutex = CreateMutex(
(SECURITY_ATTRIBUTES *)NULL, TRUE,
addSessionId(MUTEX_MAYU_EXCLUSIVE_RUNNING).c_str());
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
// another mayu already running
tstring text = loadString(IDS_mayuAlreadyExists);
tstring title = loadString(IDS_mayu);
if (g_hookData) {
UINT WM_TaskbarRestart = RegisterWindowMessage(_T("TaskbarCreated"));
PostMessage(g_hookData->m_hwndTaskTray, WM_TaskbarRestart, 0, 0);
}
MessageBox((HWND)NULL, text.c_str(), title.c_str(), MB_OK | MB_ICONSTOP);
return 1;
}
// check remote desktop
DWORD sessionId;
if (!ProcessIdToSessionId(GetCurrentProcessId(), &sessionId) ||
wtsGetActiveConsoleSessionId() != sessionId)
{
tstring text = loadString(IDS_executedInRemoteDesktop);
tstring title = loadString(IDS_mayu);
MessageBox((HWND)NULL, text.c_str(), title.c_str(), MB_OK | MB_ICONSTOP);
return 1;
}
CHECK_FALSE( installHooks() );
try
{
Mayu().messageLoop();
}
catch (ErrorMessage &i_e)
{
tstring title = loadString(IDS_mayu);
MessageBox((HWND)NULL, i_e.getMessage().c_str(), title.c_str(),
MB_OK | MB_ICONSTOP);
}
CHECK_FALSE( uninstallHooks() );
CHECK_TRUE( CloseHandle(mutex) );