/
UniMatrix.c
715 lines (607 loc) · 20.3 KB
/
UniMatrix.c
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
/***************************************************************************
* UniMatrix.c - Main program and event handler
*
* Generated: July 2002
* Copyright 2002-2005 Tim Niemueller [www.niemueller.de]
*
* $Id: UniMatrix.c,v 1.6 2005/05/28 12:59:14 tim Exp $
*
****************************************************************************/
/*
* This program 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.
*
* This program 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 Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "UniMatrix.h"
#include "gadget.h"
#include "database.h"
#include "delete.h"
#include "clist.h"
#include "tnglue.h"
#include "edit.h"
#include "beam.h"
#include "prefs.h"
#include "ctype.h"
#include "exams.h"
#include "cache.h"
#include "notes.h"
#include "alarm.h"
Char gCategoryName[dmCategoryLength];
UInt16 gMenuCurrentForm = FORM_main,
// backup of key parameters
gKeyP1,gKeyP2,gKeyP3;
Boolean gKeyP4;
UniMatrixPrefs gPrefs;
UInt32 gMainRepeat=false;
/***********************************************************************
* FUNCTION: GetObjectPtr
* DESCRIPTION: This routine returns a pointer to an object in the current
* form.
* PARAMETERS: formId - id of the form to display
***********************************************************************/
void *
GetObjectPtr(UInt16 objectID)
{
FormPtr frmP = FrmGetActiveForm();
return (FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, objectID)));
}
/***********************************************************************
* function is called at program start
* you can put your own initialization jobs there
***********************************************************************/
static UInt16
StartApplication(void)
{
Err err = 0;
// Initialize the random number seed;
SysRandom( TimGetSeconds() );
// Load prefs
PrefLoadPrefs(&gPrefs);
// Initialize TNglue
err = TNGlueColorInit();
// Initialize Cache
CacheInit();
// Open Database
if (err == errNone) err = OpenDatabase();
if (err == errNone) DatabaseSetCat(gPrefs.curCat);
// Set Alarms
if (err == errNone) AlarmReset(DatabaseGetRefN(DB_MAIN));
return (err);
}
/***********************************************************************
* function is checking ROM version of the palmos operating system
* if ROM version is less then required the result of this function is
* sysErrRomIncompatible
***********************************************************************/
static Err
RomVersionCompatible (UInt32 requiredVersion, UInt16 launchFlags)
{
// See if we're on in minimum required version of the ROM or later.
if (TNPalmOSVersion() < requiredVersion) {
if ((launchFlags & (sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp)) ==
(sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp)) {
FrmAlert (ALERT_ROMIncompatible);
// PalmOS before 3.5 will continuously relaunch this app unless we switch to
// another safe one.
AppLaunchWithCommand(sysFileCDefaultApp, sysAppLaunchCmdNormalLaunch, NULL);
}
return (sysErrRomIncompatible);
}
return (0);
}
/***********************************************************************
* Notification Handle
***********************************************************************/
static Err
HandleNotification(SysNotifyParamType *notifyParams)
{
switch (notifyParams->notifyType) {
case sysNotifyLateWakeupEvent:
if (FrmGetActiveFormID() == FORM_main) {
// We are on the main form, we have a LateWakup notification => Highlight next event
GadgetDrawHintNext();
}
break;
default:
break;
}
return errNone;
}
/***********************************************************************
* handling for the main drop down menu actions
***********************************************************************/
Boolean
HandleMenuEvent (UInt16 command)
{
Boolean handled = false;
MenuEraseStatus(0);
gMenuCurrentForm=FrmGetFormId(FrmGetActiveForm());
switch (command) {
case MENUITEM_about:
FrmDoDialog(FrmInitForm(FORM_about));
handled=true;
break;
case MENUITEM_course:
AddCourse();
handled=true;
break;
case MENUITEM_time:
if (CountCourses() != 0) {
AddTime();
} else {
FrmAlert(ALERT_nocourses);
}
handled=true;
break;
case MENUITEM_courselist:
FrmGotoForm(FORM_courselist);
handled=true;
break;
case MENUITEM_settings:
FrmPopupForm(FORM_settings);
handled=true;
break;
case MENUITEM_alarm:
FrmPopupForm(FORM_alarm_sets);
handled=true;
break;
case MENUITEM_exams:
FrmGotoForm(FORM_exams);
handled=true;
break;
case MENUITEM_beam:
BeamSemester(DatabaseGetCat());
handled=true;
break;
case MENUITEM_chat:
AppLaunchWithCommand(UNICHAT_APP_CREATOR, sysAppLaunchCmdNormalLaunch, NULL);
handled=true;
break;
case MENUITEM_mensa:
AppLaunchWithCommand(UNIMENSA_APP_CREATOR, sysAppLaunchCmdNormalLaunch, NULL);
handled=true;
break;
default:
break;
}
return handled;
}
Boolean
HandleMenuOpenEvent(EventType *event)
{
DmSearchStateType searchState;
UInt16 cardNo;
LocalID dbID;
Boolean needsSeparator=false;
if (DmGetNextDatabaseByTypeCreator(true, &searchState, 0, UNICHAT_APP_CREATOR, true, &cardNo, &dbID) != dmErrCantFind) {
MenuAddItem(MENUITEM_exams, MENUITEM_chat, 'C', "UniChat");
needsSeparator=true;
}
if (DmGetNextDatabaseByTypeCreator(true, &searchState, 0, UNIMENSA_APP_CREATOR, true, &cardNo, &dbID) != dmErrCantFind) {
MenuAddItem(MENUITEM_exams, MENUITEM_mensa, 'M', "UniMensa");
needsSeparator=true;
}
if (needsSeparator) {
Char tmpSep[2];
tmpSep[0]=MenuSeparatorChar;
tmpSep[1]=0;
MenuAddItem(MENUITEM_exams, MENUITEM_addsep, 0, tmpSep);
}
if (TNPalmOSVersion() < 0x04000000) {
// No attention manager available, we do not work around that (yet?)
MenuHideItem(MENUITEM_alarm);
}
return true;
}
/***********************************************************************
* handling for form and control actions
* menu actions are forwarded to MainFormDoCommand
***********************************************************************/
static Boolean
MainFormHandleEvent (EventPtr event)
{
FormType *frm;
Boolean handled = false;
Boolean categoryEdited, reDraw=false;
UInt16 category, numRecords;
ControlType *ctl;
UInt32 *recordList;
if (event->eType == ctlSelectEvent) {
// button handling
handled = true;
switch (event->data.ctlSelect.controlID) {
// the ok button - this leaves the application
case LIST_cat_trigger:
frm = FrmGetActiveForm();
category=DatabaseGetCat();
numRecords=DmNumRecordsInCategory(DatabaseGetRef(), DELETE_CATEGORY);
recordList=(UInt32 *)MemPtrNew(numRecords * sizeof(UInt32));
CatPreEdit(numRecords, recordList);
categoryEdited = CategorySelect(DatabaseGetRef(), frm,
LIST_cat_trigger, LIST_cat, false,
&category, gCategoryName, 0,
STRING_cat_edit); // categoryDefaultEditCategoryString
if (categoryEdited || (category != DatabaseGetCat())) {
reDraw=true;
DatabaseSetCat(category);
ctl = GetObjectPtr(LIST_cat_trigger);
CategoryGetName(DatabaseGetRef(), DatabaseGetCat(), gCategoryName);
CategorySetTriggerLabel(ctl, gCategoryName);
}
CatPostEdit(numRecords, recordList);
if (reDraw) {
GadgetSetNeedsCompleteRedraw(true);
FrmDrawForm(frm);
}
if (recordList != NULL) MemPtrFree((MemPtr)recordList);
break;
case BUTTON_beam:
BeamCourse(GadgetGetHintCourseIndex());
break;
case BUTTON_edit:
gMenuCurrentForm=FORM_main;
EditTime();
break;
case BUTTON_next:
GadgetDrawHintNext();
break;
default:
break;
}
} else if (event->eType == keyDownEvent) {
// We have a hard button assigned and it was pressed
if (TxtCharIsHardKey(event->data.keyDown.modifiers, event->data.keyDown.chr)) {
if (! (event->data.keyDown.modifiers & poweredOnKeyMask)) {
GadgetDrawHintNext();
handled = true;
}
} else if (EvtKeydownIsVirtual(event)) {
// Up or down keys pressed
switch (event->data.keyDown.chr) {
case vchrPageUp:
if (event->data.keyDown.modifiers & autoRepeatKeyMask) {
if (! gMainRepeat) {
GadgetSwitchScreen();
gMainRepeat = true;
}
} else {
GadgetDrawStep(winUp);
gMainRepeat = false;
}
handled=true;
break;
case vchrPageDown:
if (event->data.keyDown.modifiers & autoRepeatKeyMask) {
if (! gMainRepeat) {
GadgetSwitchScreen();
gMainRepeat = true;
}
} else {
GadgetDrawStep(winDown);
gMainRepeat = false;
}
handled=true;
break;
case vchrSendData:
BeamCourse(GadgetGetHintCourseIndex());
handled=true;
break;
default:
break;
}
}
} else if (event->eType == menuEvent) {
// forwarding of menu events
return HandleMenuEvent(event->data.menu.itemID);
} else if (event->eType == menuOpenEvent) {
return HandleMenuOpenEvent(event);
} else if (event->eType == frmUpdateEvent) {
// redraws the form if needed
frm = FrmGetActiveForm();
FrmDrawForm(frm);
// GadgetDrawHintNext();
handled = true;
} else if (event->eType == frmOpenEvent) {
ControlType *ctl;
LocalID dbID;
UInt16 cardNo;
Boolean newKeyP4=false;
UInt16 newKeyP2=0xFFFF;
// initializes and draws the form at program launch
frm = FrmGetActiveForm();
GadgetSet(frm, GADGET_main, GADGET_hint);
FrmSetGadgetHandler(frm, FrmGetObjectIndex(frm, GADGET_main), GadgetHandler);
FrmSetGadgetHandler(frm, FrmGetObjectIndex(frm, GADGET_hint), GadgetHintHandler);
FrmDrawForm(frm);
GadgetDrawHintNext();
ctl = GetObjectPtr(LIST_cat_trigger);
CategoryGetName (DatabaseGetRef(), DatabaseGetCat(), gCategoryName);
CategorySetTriggerLabel (ctl, gCategoryName);
DmOpenDatabaseInfo(DatabaseGetRefN(DB_MAIN), &dbID, NULL, NULL, &cardNo, NULL);
SysNotifyRegister(cardNo, dbID, sysNotifyLateWakeupEvent, HandleNotification, sysNotifyNormalPriority, NULL);
KeyRates(false,&gKeyP1, &gKeyP2, &gKeyP3, &gKeyP4);
KeyRates(true, &gKeyP1, &newKeyP2, &gKeyP3, &newKeyP4);
handled = true;
} else if (event->eType == frmCloseEvent) {
// this is done if program is closed
LocalID dbID;
UInt16 cardNo;
DmOpenDatabaseInfo(DatabaseGetRefN(DB_MAIN), &dbID, NULL, NULL, &cardNo, NULL);
SysNotifyUnregister(cardNo, dbID, sysNotifyLateWakeupEvent, sysNotifyNormalPriority);
// Restore original key rates
KeyRates(true, &gKeyP1, &gKeyP2, &gKeyP3, &gKeyP4);
}
return (handled);
}
/***********************************************************************
* chooses an displays the requested form
***********************************************************************/
static Boolean
AppHandleEvent( EventPtr eventP)
{
UInt16 formId;
FormPtr frmP;
switch (eventP->eType) {
case frmLoadEvent:
// Load the form resource.
formId = eventP->data.frmLoad.formID;
frmP = FrmInitForm(formId);
FrmSetActiveForm(frmP);
// Set the event handler for the form. The handler of the currently
// active form is called by FrmHandleEvent each time is receives an
// event.
switch (formId) {
case FORM_main:
FrmSetEventHandler(frmP, MainFormHandleEvent);
break;
case FORM_courselist:
FrmSetEventHandler(frmP, CourseListHandleEvent);
break;
case FORM_evt_det:
FrmSetEventHandler(frmP, EditTimeFormHandleEvent);
break;
case FORM_course_det:
FrmSetEventHandler(frmP, EditCourseFormHandleEvent);
break;
case FORM_settings:
FrmSetEventHandler(frmP, SettingsFormHandleEvent);
break;
case FORM_coursetypes:
FrmSetEventHandler(frmP, CourseTypeFormHandleEvent);
break;
case FORM_exams:
FrmSetEventHandler(frmP, ExamsFormHandleEvent);
break;
case FORM_exam_details:
FrmSetEventHandler(frmP, ExamDetailsFormHandleEvent);
break;
case NewNoteView:
FrmSetEventHandler(frmP, NoteViewHandleEvent);
break;
case FORM_alarm_sets:
FrmSetEventHandler(frmP, AlarmFormHandleEvent);
break;
default:
ErrNonFatalDisplay("Invalid Form Load Event");
break;
}
break;
default:
return false;
}
return true;
}
/***********************************************************************
* main event loop; loops until appStopEvent is caught or
* QuitApp is set
***********************************************************************/
static void
AppEventLoop(void)
{
UInt16 error;
EventType event;
do {
EvtGetEvent(&event, evtWaitForever);
if (! SysHandleEvent(&event))
if (! MenuHandleEvent(0, &event, &error))
if (! AppHandleEvent(&event))
FrmDispatchEvent(&event);
// Check the heaps after each event
#if EMULATION_LEVEL != EMULATION_NONE
MemHeapCheck(0);
MemHeapCheck(1);
#endif
} while (event.eType != appStopEvent);
}
/***********************************************************************
* application is finished, so we have to clean the desktop behind us
***********************************************************************/
static void
StopApplication (void)
{
gPrefs.curCat = DatabaseGetCat();
PrefSavePrefs(&gPrefs);
FrmCloseAllForms ();
CacheFree();
CloseDatabase();
}
/***********************************************************************
* main function
***********************************************************************/
UInt32
PilotMain(UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags)
{
UInt16 error = RomVersionCompatible (MINVERSION, launchFlags);
if (error) return (error);
/***
* NORMAL STARTUP
****/
if ( cmd == sysAppLaunchCmdNormalLaunch ) {
error = StartApplication ();
if (error) {
// PalmOS before 3.5 will continuously relaunch this app unless we switch to
// another safe one.
if (error != dmErrCorruptDatabase) {
FrmCustomAlert(ALERT_debug, "Please reports this bug! Give your Palm device and PalmOS version, this BadBug(TM) should not happen.", "", "");
}
StopApplication();
AppLaunchWithCommand(sysFileCDefaultApp, sysAppLaunchCmdNormalLaunch, NULL);
return error;
}
FrmGotoForm(FORM_main);
AppEventLoop ();
StopApplication ();
/***
* FIND
****/
/*
} else if (cmd == sysAppLaunchCmdSaveData) {
FrmSaveAllForms();
} else if (cmd == sysAppLaunchCmdFind) {
PalmGlobalFind((FindParamsPtr)cmdPBP);
*/
/***
* GoTo
****/
/*
} else if (cmd == sysAppLaunchCmdGoTo) {
Boolean launched = launchFlags & sysAppLaunchFlagNewGlobals;
if (launched) {
error = StartApplication();
if (! error) {
GoToItem((GoToParamsPtr)cmdPBP, launched);
AppEventLoop();
StopApplication();
}
} else {
GoToItem((GoToParamsPtr)cmdPBP, launched);
}
*/
/***
* BEAMING
****/
} else if (cmd == sysAppLaunchCmdSyncNotify) {
// Register with the Exchange Manager
ExgRegisterData(APP_CREATOR, exgRegExtensionID, "umx");
} else if (cmd == sysAppLaunchCmdExgAskUser) {
// Always assume "Yes" as answer to the accept dialog since we display our
// own on which the user can cancel the data
ExgAskParamType *exgAskParam = (ExgAskParamType *)cmdPBP;
exgAskParam->result=exgAskOk;
} else if (cmd == sysAppLaunchCmdExgReceiveData) {
DmOpenRef cats=NULL, dogs=NULL;
// Is app active?
if (launchFlags & sysAppLaunchFlagSubCall) {
// Quit Forms
FrmSaveAllForms();
cats = DatabaseGetRefN(DB_MAIN);
dogs = DatabaseGetRefN(DB_DATA);
error = BeamReceive(cats, dogs, (ExgSocketPtr) cmdPBP);
CacheReset();
FrmGotoForm(FORM_main);
} else {
// Another app was running when we were called
cats = DmOpenDatabaseByTypeCreator(DATABASE_TYPE, APP_CREATOR, dmModeReadWrite);
dogs = DmOpenDatabaseByTypeCreator(DATABASE_DATA_TYPE, APP_CREATOR, dmModeReadWrite);
if (! (cats && dogs)) {
FrmAlert(ALERT_beamdbfail);
} else {
error=BeamReceive(cats, dogs, (ExgSocketPtr)cmdPBP);
}
if (cats) DmCloseDatabase(cats);
if (dogs) DmCloseDatabase(dogs);
}
/***
* ALARM
****/
} else if (cmd == sysAppLaunchCmdAlarmTriggered) {
// Is app active?
if (launchFlags & sysAppLaunchFlagSubCall) {
AlarmTriggered(DatabaseGetRefN(DB_MAIN), (SysAlarmTriggeredParamType *)cmdPBP);
} else {
DmOpenRef cats = DmOpenDatabaseByTypeCreator(DATABASE_TYPE, APP_CREATOR, dmModeReadWrite);
AlarmTriggered(cats, (SysAlarmTriggeredParamType *)cmdPBP);
DmCloseDatabase(cats);
}
/***
* ATTENTION
****/
} else if (cmd == sysAppLaunchCmdAttention) {
// Is app active?
if (launchFlags & sysAppLaunchFlagSubCall) {
AttentionBottleNeckProc(DatabaseGetRefN(DB_MAIN), (AttnLaunchCodeArgsType *)cmdPBP);
} else {
// Another app was running when we were called
DmOpenRef cats = DmOpenDatabaseByTypeCreator(DATABASE_TYPE, APP_CREATOR, dmModeReadWrite);
AttentionBottleNeckProc(cats, (AttnLaunchCodeArgsType *)cmdPBP);
DmCloseDatabase(cats);
}
/***
* ATTENTION GOTO
****/
} else if (cmd == appLaunchCmdAlarmEventGoto) {
error = StartApplication ();
if (error) {
// PalmOS before 3.5 will continuously relaunch this app unless we switch to
// another safe one.
if (error != dmErrCorruptDatabase) {
FrmCustomAlert(ALERT_debug, "Please reports this bug! Give your Palm device and PalmOS version, this BadBug(TM) should not happen.", "", "");
}
StopApplication();
AppLaunchWithCommand(sysFileCDefaultApp, sysAppLaunchCmdNormalLaunch, NULL);
return error;
}
ExamSetGoto(*(UInt32 *)cmdPBP);
FrmGotoForm(FORM_exams);
AppEventLoop ();
StopApplication ();
/***
* TIME CHANGE
****/
// Launch code sent when the system time is changed.
} else if (cmd == sysAppLaunchCmdTimeChange) {
// reset the trigger for the next alarm to fire
if (launchFlags & sysAppLaunchFlagSubCall) {
AlarmReset(DatabaseGetRefN(DB_MAIN));
// Remove any "future" alarms from the attention manager queue
// (ie alarms that will trigger after the new time setting)
//AlarmUpdatePosted(DeviceTimeChanged);
} else {
// Another app was running when we were called
DmOpenRef cats = DmOpenDatabaseByTypeCreator(DATABASE_TYPE, APP_CREATOR, dmModeReadWrite);
if (cats != 0) {
AlarmReset(cats);
//AlarmUpdatePosted(DeviceTimeChanged);
DmCloseDatabase(cats);
}
}
/***
* RESET
****/
// This action code is sent after the system is reset.
} else if (cmd == sysAppLaunchCmdSystemReset) {
if (! ((SysAppLaunchCmdSystemResetType*)cmdPBP)->hardReset) {
if (launchFlags & sysAppLaunchFlagSubCall) {
AlarmReset(DatabaseGetRefN(DB_MAIN));
} else {
// Another app was running when we were called
DmOpenRef cats = DmOpenDatabaseByTypeCreator(DATABASE_TYPE, APP_CREATOR, dmModeReadWrite);
if (cats != 0) {
AlarmReset(cats);
DmCloseDatabase(cats);
}
}
}
}
return 0;
}