-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsysmenu.cpp
730 lines (586 loc) · 17.7 KB
/
sysmenu.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
/*
2018 Anthony Super.
System Menu INIT code based on "SysMenu" shell project by Matt Slot, circa 1995.
Password field based on code snippet by Phil Kearney, circa 1994.
*/
#include <Gestalt.h>
#include <Icons.h>
#include <Traps.h>
#include <Resources.h>
#include <MacMemory.h>
#include <Sound.h>
#include <string.h>
#include "sysmenu.h"
#include "Util.h"
#include "ShowInitIcon.h"
extern "C"
{
#include "Retro68Runtime.h"
GlobalsRec glob;
WifiData sharedData;
Str255 _password = "\p";
bool _showPassword;
void _start()
{
RETRO68_RELOCATE();
ShowInitIcon(128, true);
Handle myHandle;
// Setup Global Data
SetZone(SystemZone());
// Save memory location of shared data so the background app can find it
InitSharedData();
SaveSharedMemoryLocation();
// Setup Handlers
glob.saveInsertMenu = (InsertMenuProcPtr)ApplyTrapPatch(_InsertMenu, (ProcPtr)Patched_InsertMenu);
glob.saveDrawMenuBar = (DrawMenuProcPtr)ApplyTrapPatch(_DrawMenuBar, (ProcPtr)Patched_DrawMenuBar);
glob.saveMenuSelect = (MenuSelectProcPtr)ApplyTrapPatch(_MenuSelect, (ProcPtr)Patched_MenuSelect);
glob.saveSystemMenu = (SystemMenuProcPtr)ApplyTrapPatch(_SystemMenu, (ProcPtr)Patched_SystemMenu);
glob.mHdl = 0;
glob.menuID = 0;
glob.menuIcon = 0;
glob.sysMenus = (short **)NewHandle(0);
// Load up our icon suite for the menu "title" (if the iconID is non-zero)
// Get *all* icons in case the desired size (12x12) isn't available.
if (kPrefIconID && !GetIconSuite(&myHandle, kPrefIconID, svAllAvailableData) &&
!ForEachIconDo(myHandle, svAllAvailableData, NewIconActionProc((IconActionProcPtr)DetachIcons), 0))
glob.menuIcon = myHandle;
// Remember the location of our home rsrc file, in case we want to
// load an dialog or other resource at run-time. Note: this doesn't provide
// any guarantee that the file will be there later, so check your errors!
if (CurResFileAsFSSpec(&glob.homeFile)) glob.homeFile.vRefNum = 0;
SetZone(ApplicationZone());
// Detach the INIT resource to prevent it from being purged
Handle h = GetResource('INIT', 128);
DetachResource(h);
}
void InitSharedData()
{
sharedData.Status = ScanRequest;
sharedData.Error = false;
sharedData.UpdateUI = true;
}
void SaveSharedMemoryLocation()
{
MemLocHandle memHandle = (MemLocHandle)Get1Resource('memr', 128);
if (memHandle != nil)
{
**memHandle = (int)&sharedData;
ChangedResource((Handle)memHandle);
}
else
{
memHandle = MemLocHandle(NewHandle(sizeof(MemLoc)));
**memHandle = (int)&sharedData;
AddResource((Handle)memHandle, 'memr', 128, "\pshared memory location");
}
UpdateResFile(CurResFile());
ReleaseResource((Handle)memHandle);
}
short CurResFileAsFSSpec(FSSpec *fileSpec) {
short i, err = 0;
Str63 textBuff;
FCBPBRec fcbPB;
for (i = 0; i<sizeof(FSSpec); ((char *)fileSpec)[i++] = 0);
fcbPB.ioCompletion = 0;
fcbPB.ioFCBIndx = 0;
fcbPB.ioVRefNum = 0;
fcbPB.ioRefNum = CurResFile();
fcbPB.ioNamePtr = textBuff;
if (err = PBGetFCBInfoSync(&fcbPB)) return(err);
fileSpec->vRefNum = fcbPB.ioFCBVRefNum;
fileSpec->parID = fcbPB.ioFCBParID;
BlockMove(textBuff, fileSpec->name, textBuff[0] + 1);
return(err);
}
ProcPtr ApplyTrapPatch(short trap, ProcPtr patchPtr)
{
ProcPtr trapPtr;
if (!patchPtr) return(0);
trapPtr = (ProcPtr)NGetTrapAddress(trap, (trap & 0x0800) ? ToolTrap : OSTrap);
NSetTrapAddress(patchPtr, trap, (trap & 0x0800) ? ToolTrap : OSTrap);
return(trapPtr);
}
pascal short DetachIcons(long iconType, Handle *iconHdl, void *data)
{
short err = 0;
if (*iconHdl)
{
DetachResource(*iconHdl);
HNoPurge(*iconHdl);
err = ResError();
}
return(err);
}
pascal void Patched_InsertMenu(MenuHandle mHdl, short beforeID) {
short menuID = (*mHdl)->menuID;
InsertMenuProcPtr proc;
// As suggested to me, we need a reliable way to track what menus
// have been installed, so we can pick a unique ID for ours. This
// function simply tracks the ID's to a global variable.
if (!glob.menuID && glob.sysMenus && (menuID < 0))
PtrAndHand((Ptr)&menuID, (Handle)glob.sysMenus, sizeof(menuID));
proc = glob.saveInsertMenu;
(*proc) (mHdl, beforeID);
}
pascal void Patched_DrawMenuBar()
{
short i, found;
DrawMenuProcPtr proc;
Str32 textBuff;
THz saveZone;
if (!glob.mHdl)
{
// On the first chance, we create and install the new menu
// (We need to wait for the Menu Mgr to be initialized to setup)
// Let's not create the menu in this App's heap
saveZone = GetZone();
SetZone(SystemZone());
// Search for an unused Menu ID, then install the menu. We have tracked
// the InsertMenu calls, so now we pick an ID not in that list. When done
// we set the gMenuID and dispose (and clear) the gSysMenus handle.
for (glob.menuID = kPrefMenuID; ; glob.menuID++)
{
i = GetHandleSize((Handle)glob.sysMenus) / sizeof(**glob.sysMenus);
for (found = 0; !found && (i > 0);
found = ((*glob.sysMenus)[--i] == glob.menuID));
if (!found)
{
DisposeHandle((Handle)glob.sysMenus);
glob.sysMenus = 0;
break;
}
}
// Setup our new menu with its (optional) cool icon
if (glob.menuIcon)
{
textBuff[0] = 5;
textBuff[1] = 1;
BlockMove(&glob.menuIcon, textBuff + 2, sizeof(glob.menuIcon));
}
else BlockMove("\pMenu", textBuff, sizeof("\pMenu"));
glob.mHdl = NewMenu(glob.menuID, textBuff);
InsertMenu(glob.mHdl, 0);
SetZone(saveZone);
}
proc = glob.saveDrawMenuBar;
(*proc) ();
}
pascal long Patched_MenuSelect(Point where)
{
long value;
MenuSelectProcPtr proc;
THz saveZone;
short itemCount;
// This function is called when the user first clicks in the menubar.
// Its an ideal place to update the contents of your menu on the fly
// to reflect current settings or conditions.
if (glob.mHdl && sharedData.UpdateUI)
{
saveZone = GetZone();
SetZone(SystemZone());
itemCount = CountMItems(glob.mHdl);
// Clear existing menu items
while (itemCount > 0) {
DeleteMenuItem(glob.mHdl, 1);
itemCount--;
}
if (sharedData.Error)
{
AppendMenu(glob.mHdl, "\p ");
itemCount++;
SetMenuItemText(glob.mHdl, itemCount, "\pError");
}
else
{
// Append any known networks
for (std::vector<Network>::iterator it = sharedData.Networks.begin(); it != sharedData.Networks.end(); ++it)
{
AppendMenu(glob.mHdl, "\p ");
itemCount++;
std::string ssid = it->Name.c_str();
SetMenuItemText(glob.mHdl, itemCount, Util::StrToPStr(ssid));
if (it->Connected)
SetItemMark(glob.mHdl, itemCount, checkMark);
if (sharedData.Status != Idle)
{
DisableItem(glob.mHdl, itemCount);
}
}
}
if (itemCount > 0)
{
// Add separator
AppendMenu(glob.mHdl, "\p-");
itemCount++;
}
AppendMenu(glob.mHdl, "\p ");
itemCount++;
switch (sharedData.Status)
{
case ScanRequest:
case Scanning:
SetMenuItemText(glob.mHdl, itemCount, "\pScanning networks...");
DisableItem(glob.mHdl, itemCount);
break;
case ConnectRequest:
case Connecting:
SetMenuItemText(glob.mHdl, itemCount, Util::StrToPStr("Connecting to " + std::string(sharedData.ConnectName) + "..."));
DisableItem(glob.mHdl, itemCount);
break;
case RestartRequired:
SetMenuItemText(glob.mHdl, itemCount, "\pRestart required");
break;
case RestartRequest:
case Restarting:
SetMenuItemText(glob.mHdl, itemCount, "\pRestarting...");
DisableItem(glob.mHdl, itemCount);
break;
default:
SetMenuItemText(glob.mHdl, itemCount, "\pRefresh networks");
AppendMenu(glob.mHdl, "\p ");
itemCount++;
SetMenuItemText(glob.mHdl, itemCount, "\pSettings...");
break;
}
SetZone(saveZone);
sharedData.UpdateUI = false;
}
proc = glob.saveMenuSelect;
value = (*proc) (where);
return(value);
}
pascal void Patched_SystemMenu(long result)
{
short menuID, itemID, iconID;
SystemMenuProcPtr proc;
Str255 pName;
string name;
menuID = (result & 0xFFFF0000) >> 16;
itemID = result & 0x0000FFFF;
// When the user has selected a menu item (by mouse or cmd-key)
// we scan the event and handle it if it is from our menu.
if (menuID == glob.menuID)
{
// Some apps aren't careful about giving us the arrow.
InitCursor();
GetMenuItemText(glob.mHdl, itemID, pName);
name = Util::PtoStr(pName);
if (name == "Error")
{
ShowError();
}
else if (name == "Settings...")
{
ShowSettings();
}
else if (name == "Refresh networks")
{
sharedData.Status = ScanRequest;
sharedData.UpdateUI = true;
}
else if (name == "Restart required")
{
sharedData.Status = RestartRequest;
sharedData.UpdateUI = true;
}
else
{
// Network selected
Network& network = sharedData.Networks.at(itemID - 1);
if (network.Mode == Open)
{
// Open network, so initiate connect request immediately
memset(&sharedData.ConnectName, 0, sizeof(sharedData.ConnectName));
memset(&sharedData.ConnectId, 0, sizeof(sharedData.ConnectId));
memset(&sharedData.ConnectPwd, 0, sizeof(sharedData.ConnectPwd));
strcpy(sharedData.ConnectName, network.Name.c_str());
strcpy(sharedData.ConnectId, network.Id.c_str());
sharedData.ConnectMode = network.Mode;
sharedData.ConnectEncryption = network.Encryption;
sharedData.Status = ConnectRequest;
sharedData.UpdateUI = true;
}
else
{
ShowConnectDialog(network);
}
}
// I don't think this is vital, but may be helpful for handlers
// that go into a GNE loop (such as Alert() or ModalDialog())
HiliteMenu(0);
}
proc = glob.saveSystemMenu;
// Otherwise, pass it on!
if (menuID != glob.menuID)
(*proc) (result);
}
void ShowConnectDialog(Network& network)
{
DialogItemType type;
Handle itemH;
Rect box;
ControlHandle pwdCtrl;
short curResFile = CurResFile();
short homeResFile = FSpOpenResFile(&glob.homeFile, fsRdPerm);
_password[0] = 0;
// Set ssid label in dialog
std::string ssid = network.Name.c_str();
ParamText(Util::StrToPStr(ssid), Util::StrToPStr(GetWifiModeLabel(network.Mode)), nil, nil);
DialogPtr dialog = GetNewDialog(128, 0, (WindowPtr)-1);
MacSetPort(dialog);
Util::FrameDefaultButton(dialog, 7, false);
GetDialogItem(dialog, 5, &type, &itemH, &box);
pwdCtrl = (ControlHandle)itemH;
_showPassword = GetControlValue(pwdCtrl);
ModalFilterUPP filterProc = NewModalFilterProc(PSWDModalFilter);
short item;
bool dialogActive = true;
while (dialogActive)
{
ModalDialog(filterProc, &item);
switch (item)
{
case 5:
_showPassword = !GetControlValue(pwdCtrl);
SetControlValue(pwdCtrl, _showPassword);
PasswordKey(((DialogPeek)dialog)->textH, 0);
break;
case 6:
dialogActive = false;
break;
case 7:
if (_password[0] > 0)
{
memset(&sharedData.ConnectName, 0, sizeof(sharedData.ConnectName));
memset(&sharedData.ConnectId, 0, sizeof(sharedData.ConnectId));
memset(&sharedData.ConnectPwd, 0, sizeof(sharedData.ConnectPwd));
strcpy(sharedData.ConnectName, network.Name.c_str());
strcpy(sharedData.ConnectId, network.Id.c_str());
strcpy(sharedData.ConnectPwd, Util::PtoStr(_password).c_str());
sharedData.ConnectMode = network.Mode;
sharedData.ConnectEncryption = network.Encryption;
sharedData.Status = ConnectRequest;
sharedData.UpdateUI = true;
dialogActive = false;
}
break;
}
}
DisposeDialog(dialog);
CloseResFile(homeResFile);
UseResFile(curResFile);
}
void ShowError()
{
short curResFile = CurResFile();
short homeResFile = FSpOpenResFile(&glob.homeFile, fsRdPerm);
std::string errMsg = sharedData.ErrorMsg;
ParamText(Util::StrToPStr(errMsg), nil, nil, nil);
StopAlert(129, nil);
CloseResFile(homeResFile);
UseResFile(curResFile);
}
void ShowSettings()
{
DialogItemType type;
Handle itemH;
Rect box;
Str255 pHostname, pUsername, pPassword;
short curResFile = CurResFile();
short homeResFile = FSpOpenResFile(&glob.homeFile, fsRdPerm);
DialogPtr dialog = GetNewDialog(130, 0, (WindowPtr)-1);
MacSetPort(dialog);
MoveTo(82, 29);
PenSize(1, 1);
PenNormal();
MacLineTo(352, 29);
// Set device
GetDialogItem(dialog, 4, &type, &itemH, &box);
SetControlValue((ControlHandle)itemH, sharedData.Device);
// Set hostname
std::string hostname = sharedData.Hostname;
GetDialogItem(dialog, 6, &type, &itemH, &box);
SetDialogItemText(itemH, Util::StrToPStr(hostname));
// Set username
std::string username = sharedData.Username;
GetDialogItem(dialog, 8, &type, &itemH, &box);
SetDialogItemText(itemH, Util::StrToPStr(username));
// Set password
std::string password = sharedData.Password;
GetDialogItem(dialog, 10, &type, &itemH, &box);
SetDialogItemText(itemH, Util::StrToPStr(password));
Util::FrameDefaultButton(dialog, 12, true);
ModalFilterUPP filterProc = NewModalFilterProc(SettingsModalFilter);
short item;
bool dialogActive = true;
while (dialogActive)
{
ModalDialog(filterProc, &item);
// Get hostname
GetDialogItem(dialog, 6, &type, &itemH, &box);
GetDialogItemText(itemH, pHostname);
// Get username
GetDialogItem(dialog, 8, &type, &itemH, &box);
GetDialogItemText(itemH, pUsername);
// Get password
GetDialogItem(dialog, 10, &type, &itemH, &box);
GetDialogItemText(itemH, pPassword);
Util::FrameDefaultButton(dialog, 12, (pHostname[0] > 0 && pUsername[0] > 0 && pPassword[0] > 0));
switch (item)
{
case 11:
dialogActive = false;
break;
case 12:
memset(&sharedData.Hostname, 0, sizeof(sharedData.Hostname));
memset(&sharedData.Username, 0, sizeof(sharedData.Username));
memset(&sharedData.Password, 0, sizeof(sharedData.Password));
// Set device
GetDialogItem(dialog, 4, &type, &itemH, &box);
sharedData.Device = GetControlValue((ControlHandle)itemH);
// Set hostname
strcpy(sharedData.Hostname, Util::PtoCStr(pHostname));
// Set username
strcpy(sharedData.Username, Util::PtoCStr(pUsername));
// Set password
strcpy(sharedData.Password, Util::PtoCStr(pPassword));
sharedData.Status = SavePrefsRequest;
dialogActive = false;
break;
}
}
DisposeDialog(dialog);
CloseResFile(homeResFile);
UseResFile(curResFile);
}
pascal Boolean PSWDModalFilter(DialogPtr dialog, EventRecord *theEvent, short *itemHit)
{
char key;
ModalFilterProcPtr theModalProc;
if (theEvent->what == keyDown) {
key = (char)(theEvent->message & charCodeMask);
switch (key)
{
case kEnterCharCode:
case kReturnCharCode:
if (Util::IsControlHilited(dialog, 7))
{
*itemHit = 7;
return true;
}
else
{
theEvent->what = 0;
*itemHit = 0;
return false;
}
case kEscapeCharCode:
*itemHit = 6;
return true;
default:
PasswordKey(((DialogPeek)dialog)->textH, key);
theEvent->what = 0;
*itemHit = 0;
// Set enabled state of Join button based on password length
Util::FrameDefaultButton(dialog, 7, (_password[0] > 0));
return false;
}
}
GetStdFilterProc(&theModalProc);
return (theModalProc(dialog, theEvent, itemHit));
}
pascal Boolean SettingsModalFilter(DialogPtr dialog, EventRecord *theEvent, short *itemHit)
{
char key;
ModalFilterProcPtr theModalProc;
switch (theEvent->what)
{
case keyDown:
{
key = (char)(theEvent->message & charCodeMask);
switch (key)
{
case kEnterCharCode:
case kReturnCharCode:
if (Util::IsControlHilited(dialog, 12))
{
*itemHit = 12;
return true;
}
else
{
theEvent->what = 0;
*itemHit = 0;
return false;
}
case kEscapeCharCode:
*itemHit = 11;
return true;
}
break;
}
}
GetStdFilterProc(&theModalProc);
return (theModalProc(dialog, theEvent, itemHit));
}
void PasswordKey(TEHandle teHndl, char theKey)
{
short start, end, len;
Rect hiddenRect;
Handle txtHndl;
TEHandle tmpTE;
Str255 tmpStr;
GrafPtr currPort;
GetPort(&currPort);
// Create a temporary TERec offscreen
hiddenRect = (**teHndl).viewRect;
OffsetRect(&hiddenRect, currPort->portRect.right + 5, currPort->portRect.bottom + 5);
tmpTE = TENew(&hiddenRect, &hiddenRect);
if (!tmpTE)
return;
// Set the current password into the hidden TERec
TESetText(&_password[1], _password[0], tmpTE);
// Set the selection range of our hidden TERec to that
// of the visible password TERec
TESetSelect((**teHndl).selStart, (**teHndl).selEnd, tmpTE);
// Process the key in the hidden TERec
if(theKey > 0)
TEKey(theKey, tmpTE);
// Grab the new password out of the hidden TERec
_password[0] = (**tmpTE).teLength;
BlockMove(*((**tmpTE).hText), &_password[1], _password[0]);
// Create a string of bullets to match the real password
tmpStr[0] = _password[0];
for (len = 1; len <= tmpStr[0]; len++)
tmpStr[len] = _showPassword ? _password[len] :'¥';
// Deactivate the visible TERec so no phantom cursors pop up.
TEDeactivate(teHndl);
// Dump the bullets into the visible TERec
TESetText(&tmpStr[1], tmpStr[0], teHndl);
// Set the selection to match that of the hidden TERec
TESetSelect((**tmpTE).selStart, (**tmpTE).selEnd, teHndl);
// Dispose of the hidden TERec
TEDispose(tmpTE);
// We need to call InvalRect if the visible TEField is empty because
// of some wierdness I didn't bother to figure out with TextEdit.
// In all other cases, we just need to update the visible TERec.
if (!tmpStr[0])
InvalRect(&((**teHndl).viewRect));
else
TEUpdate(&((**teHndl).viewRect), teHndl);
// Turn the visible TERec back on.
TEActivate(teHndl);
}
string GetWifiModeLabel(WifiMode mode)
{
switch (mode)
{
case WPA2:
return "WPA2";
case WPA:
return "WPA";
default:
return "";
}
}
}