-
Notifications
You must be signed in to change notification settings - Fork 1
/
SMDEMO.C
479 lines (428 loc) · 13.3 KB
/
SMDEMO.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
/*
* SMDEMO.C - Example of Menu and File Menu Services.
*/
/* System Manager header files */
#include "..\headers\interfac.h"
#include "..\headers\event.h"
#include "..\headers\fmenu.h"
#include "..\headers\menu.h"
#include "..\headers\edit.h"
#include "..\headers\edit.h"
#include "..\headers\smtime.h"
/* C standard header files */
#include <stdlib.h>
/* defines */
typedef int BOOL;
#define TRUE 1
#define FALSE 0
enum { NORM, INV };
enum states { MAIN, MENU, FSEL, COUNT, DONE };
enum strings { TITLEs, MENUs, MAINkey1s, MAINkey2s,
HELPkey1s, HELPkey2s, ESCs, FSELtitles, _last_string };
#define NSTRINGS _last_string
/* s(x) below converts a string number to pointer to the string. */
#define s(x) strptr[x]
/* Menu states */
enum { File, Count, Quit, _last_menu };
#define N_MENU _last_menu
/* Key codes returned by System manager */
#define MENUKEY 0xC800
#define TMENUKEY 0x8500 /* F11 key which acts like menu under tkernel */
#define F1 0x3B00
#define ESC 27
/* File-menu defines */
#define NFILES 50
#define FM_BUF_SIZE ( NFILES * (sizeof (FILEINFO) ) )
#define MAX_PERMS 64
/* function prototypes */
void app_init(void);
void app_term(void);
void app_awake(void);
void app_sleep(void);
void app_break(void);
int app_key(void);
void app_display(void);
void ESCmsg( char * msg );
void disable_light_sleep(void);
void enable_light_sleep(void);
void InterruptibleLoop(BOOL (*RepeatingFunction)(void));
void ResetPermutations(void);
BOOL Permutation(void);
/* global variables */
EVENT event;
FILEINFO fmbuffer[NFILES];
MENUDATA menu;
EDITDATA edit_data;
enum states state = MAIN;
char * strptr[ NSTRINGS ];
FMENU fmenu = { (char far *) "C:\\_DAT",
(char far *) "*.TXT",
( FILEINFO far * ) fmbuffer,
FM_BUF_SIZE, -2, 0, 13, 40, 3
};
/* Variables for computing permutations */
int Perm[MAX_PERMS];
int PermSpots;
long int PermCount;
/*-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
void main(void)
{
m_init(); /* init call to system manager */
app_init(); /* application initialization */
/* event loop */
do {
m_event(&event); /* get next event */
switch (event.kind) {
case E_ACTIV:
app_awake(); /* reactivate app */
break;
case E_DEACT:
app_sleep(); /* prepare for suspension */
break;
case E_TERM:
state = DONE; /* being terminated */
break;
case E_BREAK:
app_break(); /* app ctrl-break handler */
break;
case E_KEY:
app_key(); /* process key */
break;
}
}
while ( state != DONE );
app_term(); /* application termination */
m_fini(); /* terminate call to system manager, never returns */
}
/*-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
void app_init(void)
/*
* Application specific initialization.
*
* Initialize text strings. This routine could be changed later to read
* the strings from a file. This would be required, for example, to
* provide for localization to different languages.
*/
{
int i;
char * sp[ NSTRINGS ] = {
"SM Demo",
"File\0Permutations\0Quit",
" ",
"Help ",
" ",
"Help ",
"Press ESC to continue",
"File to use:"
};
/* templates for function keys */
/*
" ...... ...... ...... ...... ....."
"..... ...... ...... ...... ...... "
*/
for( i = 0; i < NSTRINGS; i++ )
strptr[ i ] = sp[ i ];
app_display();
}
/*-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
void app_term(void)
/*
* Terminate application
*/
{
}
/*-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
void app_awake(void)
/*
* Reactivates application after suspension.
*/
{
app_display();
}
/*-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
void app_display(void)
/*
* Application displayer.
*/
{
switch( state ) {
case MAIN:
drawbox( s( TITLEs ) );
m_telltime( DATE_N_TIME, -3, 19 );
m_disp( 11, 0, s( MAINkey1s ), 40, INV, 0 );
m_disp( 12, 0, s( MAINkey2s ), 40, INV, 0 );
m_setcur( 0, -1 );
break;
case MENU:
drawbox( "" );
menu_dis( &menu );
m_disp( 11, 0, s( HELPkey1s ), 40, INV, 0 );
m_disp( 12, 0, s( HELPkey2s ), 40, INV, 0 );
break;
case FSEL:
fmenu_dis( &fmenu, &edit_data );
m_disp( 11, 0, s( HELPkey1s ), 40, INV, 0 );
m_disp( 12, 0, s( HELPkey2s ), 40, INV, 0 );
break;
case COUNT:
break;
} /* end switch on state */
}
/*-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
void app_sleep(void)
/*
* Prepares application for suspension.
*/
{
}
/*-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
void app_break(void)
/*
* Application control-break handler.
*/
{
}
/*-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
int app_key(void)
/*
* Application keystroke processor.
*/
{
int menuselect;
char txtpattern[] = "*.txt";
switch( state ) {
case MAIN:
switch( event.data ) {
case MENUKEY:
case TMENUKEY: /* menu key under tkernel */
state = MENU;
menu_setup( &menu, s( MENUs ), N_MENU, 1, NULL, 0, NULL );
menu_on( &menu );
app_display();
break;
case F1: // Help
ESCmsg( "Help subsystem would be entered" );
break;
default:
m_beep();
}
break;
case MENU:
if( event.data == ESC ) {
state = MAIN;
menu_off( &menu );
app_display();
}
else if( event.data == F1 )
ESCmsg( "Help subsystem would be entered" );
else {
menu_key( &menu, event.data, &menuselect );
if ( menuselect > -1 )
menu_off( &menu );
switch( menuselect ) {
case File:
state = FSEL;
fmenu.fm_pattern = txtpattern;
edit_data.prompt_window = 1;
edit_data.prompt_line_length = 0;
edit_data.message_line = s( FSELtitles );
edit_data.message_line_length = strlen( s( FSELtitles ) );
fmenu_init( &fmenu, &edit_data, "*.txt", 9, 1 );
app_display();
break;
case Count:
ResetPermutations();
InterruptibleLoop(Permutation);
state=MAIN;
app_display();
break;
case Quit:
state = DONE;
break;
}
}
break;
case FSEL:
if( event.data == F1 )
ESCmsg( "Help subsystem would be entered" );
else
switch( fmenu_key( &fmenu, &edit_data, event.data ) ) {
case RET_OK:
break;
case RET_ACCEPT:
fmenu_off( &fmenu, &edit_data );
state = MAIN;
app_display();
m_disp(5,5,edit_data.edit_buffer,
strlen(edit_data.edit_buffer),0,0);
break;
case RET_ABORT:
fmenu_off( &fmenu, &edit_data );
state = MAIN;
app_display();
break;
default:
m_beep();
}
break;
} /* end switch on state */
}
/*-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
void ESCmsg( char * msg )
/*
* Display a message which requires ESC to continue.
* Lock the user into responding to this message with the m_lock()
* call which will prevent task-switches.
*/
{
message( msg, strlen( msg ), s( ESCs ), strlen( s( ESCs ) ) );
m_lock();
while (1) {
m_event( &event );
if ( event.kind == E_KEY ) {
if ( event.data == ESC)
break;
else
m_beep();
}
}
m_unlock();
msg_off();
}
/*-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
void InterruptibleLoop(BOOL (*RepeatingFunction)(void))
/*
* The only special note to be taken of this process, is that it
* continously calls RepeatingFunction AND allows the user to task-switch
* to other System Manager applications (also allowing alarms to function).
* RepeatingFunction should return a TRUE when it is finished, else a FALSE.
* The equivalent loop above (ESCmsg) cannot task-switch because
* of the m_lock() call.
* NOTE: The calls to disable_light_sleep() and enable_light_sleep()
* MUST BE PAIRED WITHIN THIS PROGRAM. Special precautions are
* taken to prevent a task-switch while light sleep is disabled.
* A switch away while light sleep is disabled can severly limit
* battery life.
*/
{
BOOL Done=FALSE;
disable_light_sleep(); /* Allow process to run at full speed */
while (!Done) {
Done = (*RepeatingFunction)();
m_nevent( &event ); /* Look at next event (to avoid .5 sec timeout) */
switch (event.kind) {
case E_BREAK: /* Allow a break key to terminate */
Done = TRUE;
break;
case E_KEY:
m_event( &event );
if (event.data==ESC) Done=TRUE; /* Or an escape key to terminate */
break;
case E_ACTIV:
app_display();
disable_light_sleep(); /* Coming back from switch--disable l.s. */
break;
case E_DEACT:
enable_light_sleep(); /* Switching away--re-enable l.s. */
break;
}
}
enable_light_sleep(); /* Allow CPU to save as much battery life as possible */
}
/*-----------------------------------------------------------------------*/
void disable_light_sleep(void)
/*
* Use 95's special BIOS routine to disable light sleep
*/
{
_asm {
mov ax,4e00h
int 15h
}
}
/*-----------------------------------------------------------------------*/
void enable_light_sleep(void)
/*
* Use 95's BIOS call
*/
{
_asm {
mov ax,4e01h
int 15h
}
}
/*-----------------------------------------------------------------------*/
void ResetPermutations()
/*
* Start at very first permutation
*/
{
int i;
PermSpots = strlen(edit_data.edit_buffer);
for (i=0; i<PermSpots; i++) Perm[i] = i;
PermCount = 0;
}
/*-----------------------------------------------------------------------*/
void ShowPerm(void)
/*
* Diplay current perm. count and current perm.
*/
{
int i;
char Buffer[MAX_PERMS];
ltoa(PermCount,Buffer,10);
m_disp(3,5,Buffer,strlen(Buffer),0,0);
for (i=0; i<PermSpots; i++) Buffer[i] = edit_data.edit_buffer[Perm[i]];
Buffer[PermSpots] = 0;
m_disp(5,5,Buffer,strlen(Buffer),0,0); /* Show current perm */
m_dirty_sync(); /* And ensure it is displayed */
}
/*-----------------------------------------------------------------------*/
void NextAvailable(int level)
/*
* For the level-th character, find the next available (ie. not already used)
* character beyond the current one.
*/
{
int i;
BOOL AlreadyHit[MAX_PERMS];
for (i=0; i<MAX_PERMS; i++) AlreadyHit[i] = FALSE; /* Zero out array */
for (i=0; i<level; i++) AlreadyHit[Perm[i]] = TRUE; /* Set previous perms */
for (i=Perm[level]+1; i<PermSpots; i++) if (!AlreadyHit[i]) { /* Look for available perm. following current one */
Perm[level] = i;
return;
}
Perm[level] = -1; /* Indicate no more possibilities for this position */
}
/*-----------------------------------------------------------------------*/
BOOL Permutation(void)
/*
* Advance current perm to the next perm.
* Return TRUE if done, otherwise FALSE.
*/
{
int PermLevel=PermSpots-1; /* Start looking at last position */
int i;
PermCount++; /* Count perms. */
ShowPerm(); /* and show last one */
NextAvailable(PermLevel);
while (PermLevel>0 && Perm[PermLevel]==-1) { /* If current level has rolled off end */
PermLevel--; /* Decrease depth and */
NextAvailable(PermLevel); /* Bump up index */
for (i=PermLevel+1; i<PermSpots; i++) Perm[i] = 0; /* Zap all to right */
}
if (PermLevel<0) return TRUE; /* Done. */
/* Fill up all to right of scrolled spot with available chars */
for (i=PermLevel+1; i<PermSpots; i++) NextAvailable(i);
return FALSE;
}