-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
2168 lines (1731 loc) · 53.8 KB
/
main.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
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
/*
* Teradesk. Copyright (c) 1993 - 2002 W. Klaren.
* 2002 - 2003 H. Robbers,
* 2003 - 2014 Dj. Vukovic
*
* This file is part of Teradesk.
*
* Teradesk 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.
*
* Teradesk 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Teradesk; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA
*/
#include <library.h>
#include <xdialog.h>
#include <xscncode.h>
#include <mint/cookie.h>
#include "resource.h"
#include "desk.h"
#include "environm.h"
#include "error.h"
#include "events.h"
#include "font.h"
#include "open.h"
#include "version.h"
#include "xfilesys.h"
#include "config.h"
#include "window.h"
#include "copy.h"
#include "dir.h"
#include "file.h"
#include "lists.h"
#include "slider.h"
#include "filetype.h"
#include "icon.h"
#include "prgtype.h"
#include "screen.h"
#include "icontype.h"
#include "applik.h"
#include "va.h"
#include "video.h"
#include "startprg.h"
#include "main.h"
#undef os_start
#define os_start ( * (long *) 0x4F2L )
#define memval ( * (long *) 0x420L )
#define memval2 ( * (long *) 0x43AL )
#define resvector ( * ( void (**)( void ) ) 0x42AL )
#define resvalid ( * (long *) 0x426L )
#define RSRCNAME "desktop.rsc" /* Name of te program's resource file */
#define EVNT_FLAGS (MU_MESAG | MU_BUTTON | MU_KEYBD) /* For the main loop */
#define MAX_PLINE 132 /* maximum printer line length */
#define MIN_PLINE 32 /* minimum printer line length */
#define DEF_PLINE 80 /* default printer line length */
Options options; /* configuration options */
XDFONT def_font; /* Data for the default (system) font */
static const char *teraenv; /* pointer to value of TERAENV environment variable */
const char *fsdefext; /* default filename extension in current OS */
char *infname; /* name of V3 configuration file (teradesk.inf) */
char *palname; /* name of V3 colour palette file (teradesk.pal) */
char *global_memory; /* Globally available buffer for passing params */
static char *definfname; /* name of the initial configuration file at startup */
static const char *infide = "TeraDesk-inf"; /* File identifier header */
const char *empty = "\0"; /* often used string */
const char *bslash = "\\"; /* often used string */
const char *adrive = "A:\\"; /* often used string */
const char *prevdir = ".."; /* often used string */
long global_mem_size; /* size of global_memory */
_WORD tos_version; /* detected version of TOS; interpret in hex format */
_WORD aes_version; /* detected version of AES; interpret in hex format */
_WORD ap_id; /* application id. of this program (TeraDesk) */
_WORD vdi_handle; /* current VDI station handle */
_WORD nfonts; /* number of available fonts */
static _WORD shutopt = 0; /* shutdown type: system halt/poweroff */
/* Names of menu boxes in the main menu */
static const _WORD menu_items[] = { MINFO, TDESK, TLFILE, TVIEW, TWINDOW, TOPTIONS };
#if _MINT_
bool have_ssystem = FALSE;
bool mint; /* True if Mint is present */
bool magx; /* True if MagiC is present */
bool naes; /* True if N.AES is present */
bool geneva; /* True if Geneva is present */
#endif
bool shutdown = FALSE; /* true if system shutdown is required */
bool startup = TRUE; /* true if desktop is starting */
static bool ct60; /* True if this is a CT60 machine */
static bool chrez = FALSE; /* true if resolution should be changed */
static bool quit = FALSE; /* true if teradesk should finish */
static bool shutting = FALSE; /* true if started shutting down */
bool onekey_shorts; /* true if any single-key menu shortcuts are defined */
#if _LOGFILE
FILE *logfile = NULL;
char *logname;
#endif
/*
* Below is supposed to be the only text embedded in the code:
* information (in several languages) that a resource file
* can not be found. It is shown in an alert box.
*/
static char const msg_resnfnd[] = "[1][Can not find the resource file.|"
"Resource Datei nicht gefunden.|"
"Resource file niet gevonden.|"
"Impossible de trouver le|fichier resource.][ OK ]";
static XDEVENT loopevents; /* events awaited for in the main loop */
/*
* Configuration table for desktop options.
* Take care to keep "infv" first in the list.
* Bit flags are written in hex format for easier
* manipulation by humans.
*/
static CfgEntry const Options_table[] = {
CFG_HDR("options"),
CFG_BEG(),
/* file version */
CFG_X("infv", options.version), /* file version */
/* desktop preferences */
CFG_X("save", options.sexit), /* what to save at exit */
CFG_X("dial", options.dial_mode), /* bit flags !!! dialog mode and position */
CFG_X("xpre", options.xprefs), /* bit flags !!! diverse prefs */
/* Copy preferences */
CFG_X("pref", options.cprefs), /* bit flags !!! copy prefs */
/* sizes of diverse items */
CFG_D("buff", options.bufsize), /* copy buffer size */
CFG_L("maxd", options.max_dir), /* initial dir size */
CFG_D("plin", options.plinelen), /* printer line length */
CFG_D("tabs", options.tabsize), /* tab size */
CFG_D("cwin", options.cwin), /* compare match size */
/* settings of the View menu */
CFG_D("mode", options.mode), /* text/icon mode */
CFG_D("sort", options.sort), /* sorting key */
CFG_D("aarr", options.aarr), /* auto arrange */
CFG_X("attr", options.attribs), /* Bit flags !!! global attributes to show */
CFG_X("flds", options.fields), /* Bit flags !!! dir. fields to show */
/* video options */
CFG_X("vidp", options.vprefs), /* Bit flags ! */
CFG_D("vres", options.vrez), /* video resolution (currently unused) */
/* patterns and colours */
CFG_D("dpat", options.dsk_pattern), /* desk pattern */
CFG_D("dcol", options.dsk_colour), /* desk colour */
CFG_D("wpat", options.win_pattern), /* window pattern */
CFG_D("wcol", options.win_colour), /* window colour */
CFG_ENDG(),
CFG_LAST()
};
/*
* Configuration table for menu shortcuts. If the main menu of
* TeraDesk is changed, this table should always be updated to
* match the actual state.
*/
static CfgEntry const Shortcut_table[] = {
CFG_HDR("shortcuts"),
CFG_BEG(),
/* File menu */
CFG_X("open", options.kbshort[MOPEN - MFIRST]),
CFG_X("show", options.kbshort[MSHOWINF - MFIRST]),
CFG_X("newd", options.kbshort[MNEWDIR - MFIRST]),
CFG_X("comp", options.kbshort[MCOMPARE - MFIRST]),
CFG_X("srch", options.kbshort[MSEARCH - MFIRST]),
CFG_X("prin", options.kbshort[MPRINT - MFIRST]),
CFG_X("dele", options.kbshort[MDELETE - MFIRST]),
CFG_X("sela", options.kbshort[MSELALL - MFIRST]),
#ifdef MFCOPY
CFG_X("copy", options.kbshort[MFCOPY - MFIRST]),
CFG_X("form", options.kbshort[MFFORMAT - MFIRST]),
#else
CFG_XI("copy", inhibit),
CFG_XI("form", inhibit),
#endif
CFG_X("quit", options.kbshort[MQUIT - MFIRST]),
/* View menu */
#ifdef MSHOWTXT
CFG_X("shtx", options.kbshort[MSHOWTXT - MFIRST]),
#endif
CFG_X("shic", options.kbshort[MSHOWICN - MFIRST]),
CFG_X("sarr", options.kbshort[MAARNG - MFIRST]),
CFG_X("snam", options.kbshort[MSNAME - MFIRST]),
CFG_X("sext", options.kbshort[MSEXT - MFIRST]),
CFG_X("sdat", options.kbshort[MSDATE - MFIRST]),
CFG_X("ssiz", options.kbshort[MSSIZE - MFIRST]),
CFG_X("suns", options.kbshort[MSUNSORT - MFIRST]),
CFG_X("revo", options.kbshort[MREVS - MFIRST]),
#if _MINT_
CFG_X("noca", options.kbshort[MNOCASE - MFIRST]),
#endif
CFG_X("asiz", options.kbshort[MSHSIZ - MFIRST]),
CFG_X("adat", options.kbshort[MSHDAT - MFIRST]),
CFG_X("atim", options.kbshort[MSHTIM - MFIRST]),
CFG_X("aatt", options.kbshort[MSHATT - MFIRST]),
#if _MINT_
CFG_X("aown", options.kbshort[MSHOWN - MFIRST]),
#endif
CFG_X("smsk", options.kbshort[MSETMASK - MFIRST]),
/* Window menu */
CFG_X("wico", options.kbshort[MICONIF - MFIRST]),
CFG_X("wful", options.kbshort[MFULL - MFIRST]),
CFG_X("clos", options.kbshort[MCLOSE - MFIRST]),
CFG_X("wclo", options.kbshort[MCLOSEW - MFIRST]),
CFG_X("cloa", options.kbshort[MCLOSALL - MFIRST]),
CFG_X("wdup", options.kbshort[MDUPLIC - MFIRST]),
CFG_X("cycl", options.kbshort[MCYCLE - MFIRST]),
/* Options menu */
CFG_X("appl", options.kbshort[MAPPLIK - MFIRST]),
CFG_X("ptyp", options.kbshort[MPRGOPT - MFIRST]),
CFG_X("dski", options.kbshort[MIDSKICN - MFIRST]),
CFG_X("wini", options.kbshort[MIWDICN - MFIRST]),
CFG_X("pref", options.kbshort[MOPTIONS - MFIRST]),
CFG_X("copt", options.kbshort[MCOPTS - MFIRST]),
CFG_X("wopt", options.kbshort[MWDOPT - MFIRST]),
CFG_X("vopt", options.kbshort[MVOPTS - MFIRST]),
CFG_X("ldop", options.kbshort[MLOADOPT - MFIRST]),
CFG_X("svop", options.kbshort[MSAVESET - MFIRST]),
CFG_X("svas", options.kbshort[MSAVEAS - MFIRST]),
CFG_ENDG(),
CFG_LAST()
};
/*
* Try to allocate some memory and check success. Display an alert if failed.
* There will generally be some loss in speed, so use with discretion.
*/
void *malloc_chk(size_t size)
{
void *address = malloc(size);
if (address == NULL)
xform_error(ENOMEM);
return address;
}
/*
* Display a dialog, but check for errors when opening and display an alert.
* This routine ends only when the dialog is closed.
*/
_WORD chk_xd_dialog(OBJECT *tree, _WORD start)
{
_WORD error = xd_dialog(tree, start);
xform_error(error);
return error;
}
/*
* Open a dialog, but check for errors and display an alert.
* In some dialogs it is not convenient to use this routine :(
*/
_WORD chk_xd_open(OBJECT *tree, XDINFO *info)
{
_WORD error;
arrow_mouse();
error = xd_open(tree, info);
xform_error(error);
return error;
}
/*
* Inquire about the size of the largest block of free memory.
* At least TOS 2.06 is needed in order to inquire about TT/Alt memory.
* Note that MagiC reports as TOS 2.00 only but it is assumed that
* Mint and Magic can always handle Alt/TT-RAM
*/
static void get_freemem(long *stsize, long *ttsize)
{
#if _MINT_
if (mint || (tos_version >= 0x206))
#else
if (tos_version >= 0x206)
#endif
{
*stsize = (long) Mxalloc(-1L, 0);
*ttsize = (long) Mxalloc(-1L, 1);
} else
{
*stsize = (long) Malloc(-1L);
*ttsize = 0L;
}
}
/*
* Show information on current versions of TeraDesk, TOS, AES...
* Note: constant part of info for this dialog is handled in resource.c
* This involves setting TeraDesk name and version, etc.
*/
static void show_osinfo(void)
{
long stsize;
long ttsize; /* sizes of ST-RAM and TT/ALT-RAM */
/* Inquire about the size of the largest free memory block */
get_freemem(&stsize, &ttsize);
/*
* Display currently available memory.
* Maximum correctly displayed size is 2GB.
* If display of larger sizes (or a formatted display)
* is desired it is necessary to change the signs
* of stsize and ttsize, and to divide them by KBMB
*/
rsc_ltoftext(infobox, INFSTMEM, stsize);
rsc_ltoftext(infobox, INFTTMEM, ttsize);
chk_xd_dialog(infobox, 0);
}
/*
* If HELP is pressed, display consecutive boxes of text in a dialog,
* or, if <Shift><Help> is pressed, try to call the .HYP file viewer
* (usually this is ST-GUIDE program or accessory).
* Currently, there is no notification if the call to the viewer
* is successful.
*/
static void showhelp(unsigned short key)
{
static const unsigned char hbox[] = { HLPBOX1, HLPBOX2, HLPBOX3, HLPBOX4 };
hyppage = empty;
if (key & XD_SHIFT)
{
opn_hyphelp();
} else
{
XDINFO info;
_WORD i = 0;
_WORD button;
obj_unhide(helpno1[HELPOK]);
obj_unhide(helpno1[HLPBOX1]);
if (chk_xd_open(helpno1, &info) >= 0)
{
do
{
button = xd_form_do(&info, ROOT);
xd_buttnorm(&info, button);
obj_hide(helpno1[hbox[i]]);
/* At i == 3 one must and can exit this way only */
if (button == HELPCANC)
break;
i++;
obj_unhide(helpno1[hbox[i]]);
if (i == 3)
obj_hide(helpno1[HELPOK]);
xd_drawdeep(&info, ROOT);
} while (TRUE);
xd_close(&info);
} /* open ? */
}
}
/*
* Generalized set_opt() to change display of any options button from bitflags
* In fact it can set option buttons from bool variables as well
* (then set "opt" to 1, and "button" is a bool variable)
*/
void set_opt(OBJECT *tree, /* pointer to the object tree */
_WORD flags, /* variable containing bit flags */
_WORD opt, /* mask for the bitflag */
_WORD button /* button object index */
)
{
if (flags & opt)
obj_select(tree[button]);
else
obj_deselect(tree[button]);
}
/*
* Inverse function to the above- set bit flag from button id.
*/
void get_opt(OBJECT *tree, /* pointer to the object tree */
_WORD *flags, /* variable containing the bit flags */
_WORD opt, /* mask of the bit flag */
_WORD button /* button object index */
)
{
if (tree[button].ob_state & OS_SELECTED)
*flags |= opt;
else
*flags &= ~opt;
}
/*
* Set dialogs to a specific display mode, without too many arguments
*/
static void set_dialmode(void)
{
xd_setdialmode((options.dial_mode & DIAL_MODE), hndlmessage, menu,
(_WORD) (sizeof(menu_items) / sizeof(menu_items[0])), menu_items);
}
/* See also desk.h */
/*
* A routine for displaying a keyboard shortcut in a human-readable form;
* <DEL>, <BS>, <TAB> and <SP> are displayed as "DEL", "BS", "TAB" and "SP",
* other single characters are represented by that character;
* "control" is represented by "^";
* Uppercase or ctrl-uppercase are always assumed;
* resultant string is never more than 4 characters long;
* XD_CTRL is used for convenience; no need to define a new flag
*/
static void disp_short(char *string, /* resultant string */
_WORD kbshort, /* keyboard shortcut to be displayed */
_WORD left /* left-justify and 0-terminate string if true */
)
{
char k = (char) (kbshort & 0x00FF);
char *strp = &string[3];
switch (k)
{
case BACKSPC:
*strp-- = 'S';
*strp-- = 'B';
break;
case TAB:
*strp-- = 'B';
*strp-- = 'A';
*strp-- = 'T';
break;
case SPACE:
*strp-- = 'P';
*strp-- = 'S';
break;
case DELETE:
*strp-- = 'L';
*strp-- = 'E';
*strp-- = 'D';
break;
default: /* Other */
if (kbshort != 0)
*strp-- = k;
break;
}
if (kbshort & XD_CTRL) /* Preceded by ^ ? */
*strp-- = '^';
while (strp >= string)
*strp-- = ' ';
if (left) /* if needed- left justify, terminate with 0 */
{
string[4] = 0;
strip_name(string, string);
}
}
/*
* A routine for inserting keyboard menu shortcuts into menu texts;
* it also marks where the domain of a new menu title begins;
* for this, bit-flag XD_ALT is used (just convenient, no need to define
* something else)
*/
static void ins_shorts(void)
{
_WORD menui; /* menu item counter */
char *where; /* address of location of shortcut in a menu string */
char *str;
onekey_shorts = FALSE;
for (menui = MFIRST; menui <= MLAST; menui++) /* for each menu item */
{
if (menu[menui].ob_type == G_STRING) /* which is a string... */
{
if (menu[menui].ob_spec.free_string[1] == ' ') /* and not a separator line */
{
_WORD shortcut = options.kbshort[menui - MFIRST];
if (shortcut == BACKSPC || (shortcut >= ' ' && shortcut <= '~'))
onekey_shorts = TRUE;
str = menu[menui].ob_spec.free_string;
where = str + strlen(str); /* includes trailing spaces */
/* beware: assumes exactly 4 '_' in initial resource
(but we cannot search for it anymore once shortcuts had been inserted)
*/
if (where > str + 4)
{
where -= 4;
disp_short(where, shortcut, FALSE);
}
}
} else
{
options.kbshort[menui - MFIRST] = XD_ALT; /* under new title from now on */
}
}
}
/*
* Handle the "Preferences" dialog for setting some
* TeraDesk configuration aspects
*/
static void setpreferences(void)
{
XDINFO prefinfo; /* dialog handling structure */
static _WORD menui = MFIRST; /* .rsc index of currently displayed menu item */
_WORD button = OPTMNEXT; /* current button index */
_WORD mi; /* menui - MFIRST */
_WORD redraw = TRUE; /* true if to redraw menu item and key def */
_WORD lm; /* length of text field in current menu item */
_WORD lf; /* length of form for menu item text */
_WORD i;
_WORD j; /* counters */
unsigned short *tmpmi;
unsigned short tmp[NITEM + 2]; /* temporary kbd shortcuts (until OK'd) */
bool shok; /* TRUE if a shortcut is acceptable */
char aux[5]; /* temp. buffer for string manipulation */
/* Set state of radio buttons and checkbox button(s) */
xd_set_rbutton(setprefs, OPTPAR2, (options.dial_mode & DIALPOS_MODE) ? DMOUSE : DCENTER);
xd_set_rbutton(setprefs, OPTPAR1, DBUFFERD + (options.dial_mode & DIAL_MODE) - 1);
set_opt(setprefs, options.sexit, SAVE_CFG, SEXIT);
lf = (_WORD) strlen(setprefs[OPTMTEXT].ob_spec.free_string); /* get length of space for displaying menu items */
/* Copy shortcuts to temporary storage */
memcpy(&tmp[0], &options.kbshort[0], (NITEM + 1) * sizeof(tmp[0]));
if (chk_xd_open(setprefs, &prefinfo) >= 0) /* Open dialog; then loop until OK or Cancel */
{
/*
* Handle setting of keyboard shortcuts; note: because of limitations in
* keyboard scancodes, distinction of some key combinations is impossible
* Because of that, e.g. ^ESC, ^BS and ^TAB are not permitted.
* Also, characters like SP and CR which are used for other purposes
* are not permitted as shortcuts.
*/
while (button != OPTOK && button != OPTCANC)
{
/* Display text of current menu item */
mi = menui - MFIRST;
tmpmi = &tmp[mi];
if (redraw)
{
lm = (_WORD) strlen(menu[menui].ob_spec.free_string); /* How long? Assumed always to be lm > 5 */
/* Copy menu text to dialog, remove shortcut text */
strncpy(setprefs[OPTMTEXT].ob_spec.free_string, menu[menui].ob_spec.free_string + 1L, min(lm, lf) - 1);
for (i = min(lf, lm - 6); i < lf; i++)
setprefs[OPTMTEXT].ob_spec.free_string[i] = ' ';
/* Display defined shortcut and assigned key in ASCII form */
disp_short(setprefs[OPTKKEY].ob_spec.tedinfo->te_ptext, *tmpmi, TRUE);
xd_drawthis(&prefinfo, OPTMTEXT);
xd_drawthis(&prefinfo, OPTKKEY);
redraw = FALSE;
}
do /* again: */
{
shok = TRUE;
button = xd_form_do(&prefinfo, ROOT);
/* Interpret shortcut from the dialog */
strip_name(aux, setprefs[OPTKKEY].ob_spec.tedinfo->te_ptext);
strcpy(setprefs[OPTKKEY].ob_spec.tedinfo->te_ptext, aux);
i = (_WORD) strlen(aux);
*tmpmi = 0;
switch (i)
{
case 0: /* nothing defined */
break;
case 1: /* single-character shortcut */
*tmpmi = aux[0] & 0x00FF;
break;
case 2: /* two-character ^something shortcut or BS */
if ((aux[0] == '^') && (isupper(aux[1]) || isdigit(aux[1]) || ((aux[1] & 0x80) != 0)))
*tmpmi = (aux[1] & 0x00FF) | XD_CTRL;
else if (strcmp(aux, "BS") == 0) /* BS */
*tmpmi = BACKSPC;
else
*tmpmi = XD_CTRL; /* illegal/ignored menu object */
break;
default: /* longer shortcuts */
if (aux[0] == '^')
{
*tmpmi = XD_CTRL;
aux[0] = ' ';
strip_name(aux, aux);
if (strcmp(aux, "SP") == 0) /* ^SP */
*tmpmi |= SPACE;
} else
{
if (strcmp(aux, "TAB") == 0) /* TAB */
*tmpmi = TAB;
}
if (strcmp(aux, "DEL") == 0) /* DEL & ^DEL */
*tmpmi |= DELETE;
if (*tmpmi == 0)
*tmpmi = XD_CTRL;
break;
}
/* Now check for duplicate keys */
if (button != OPTCANC && button != OPTKCLR)
{
/* Note: this tests tmp[mi] many times repeatedly, but who cares */
for (j = 0; j <= NITEM; j++)
{
/* XD_ALT below in order to skip items related to menu boxes */
if ((((*tmpmi & XD_SCANCODE) != 0) && ((*tmpmi & 0xFF) < 128)) || ((*tmpmi & ~XD_ALT) != 0 && (mi != j) && *tmpmi == tmp[j]) /* duplicate */
|| *tmpmi == XD_CTRL /* ^ only, illegal */
)
{
alert_printf(1, ADUPKEY, setprefs[OPTKKEY].ob_spec.tedinfo->te_ptext);
shok = FALSE;
break;
}
}
}
} while (!shok);
/*
* Only menu items which lay between MFIRST and MLAST are considered;
* if menu structure is changed, this interval should be redefined too;
* only those menu items with a space in the second char position
* are considered; other items are assumed not to be valid menu texts
* note: this code will crash in the (ridiculous) situation when the
* first or the last menu item is not a good text
*/
switch (button)
{
case OPTMPREV:
while (menui > MFIRST && menu[--menui].ob_type != G_STRING) ;
if (menu[menui].ob_spec.free_string[1] != ' ')
menui--;
redraw = TRUE;
break;
case OPTMNEXT:
while (menui < MLAST && menu[++menui].ob_type != G_STRING) ;
if (menu[menui].ob_spec.free_string[1] != ' ')
menui++;
redraw = TRUE;
break;
case OPTKCLR:
memclr(&tmp[0], (NITEM + 2) * sizeof(tmp[0]));
redraw = TRUE;
break;
default:
break;
}
}
/* Here we come only with OK or Cancel */
xd_buttnorm(&prefinfo, button);
xd_close(&prefinfo);
if (button == OPTOK)
{
_WORD posmode = XD_CENTERED;
/*
* Move kbd. shortcuts into permanent storage,
* then insert them into menu texts
*/
memcpy(&options.kbshort[0], &tmp[0], (NITEM + 1) * sizeof(tmp[0]));
ins_shorts();
/* Get and then set dialog display mode */
options.dial_mode = xd_get_rbutton(setprefs, OPTPAR1) - DBUFFERD + 1;
if (xd_get_rbutton(setprefs, OPTPAR2) == DMOUSE)
{
options.dial_mode |= DIALPOS_MODE;
posmode = XD_MOUSE;
} else
{
options.dial_mode &= ~DIALPOS_MODE;
}
get_opt(setprefs, &options.sexit, SAVE_CFG, SEXIT);
set_dialmode();
xd_setposmode(posmode);
}
}
}
/*
* Handle the dialog for setting options related to copying and printing
*/
static void copyprefs(void)
{
char *copybuffer = copyoptions[COPYBUF].ob_spec.tedinfo->te_ptext;
_WORD i, button;
static const _WORD bitflags[] = { CF_COPY, CF_OVERW, CF_DEL, CF_PRINT, CF_SHOWD, CF_KEEPS, CF_TRUNN, P_HEADER };
/* Set states of appropriate options buttons and copy buffer field */
for (i = CCOPY; i <= CPPHEAD; i++)
set_opt(copyoptions, options.cprefs, bitflags[i - CCOPY], i);
itoa(options.bufsize, copybuffer, 10);
itoa(options.plinelen, copyoptions[CPPLINE].ob_spec.tedinfo->te_ptext, 10);
/* The dialog itself */
button = chk_xd_dialog(copyoptions, 0);
/* If OK is selected... */
if (button == COPTOK)
{
/* Get new states of options buttons and new copy buffer size */
for (i = CCOPY; i <= CPPHEAD; i++)
get_opt(copyoptions, &options.cprefs, bitflags[i - CCOPY], i);
if ((options.bufsize = atoi(copybuffer)) < 1)
options.bufsize = 1;
options.plinelen = atoi(copyoptions[CPPLINE].ob_spec.tedinfo->te_ptext);
if ((options.plinelen < MIN_PLINE) || (options.plinelen > MAX_PLINE))
options.plinelen = DEF_PLINE;
}
}
/*
* Set default options to be in effect without a configuration file.
* Also set desktop and window background.
*/
static void opt_default(void)
{
get_set_video(0); /* get current video mode for default */
memclr(&options, sizeof(options));
options.version = CFG_VERSION;
options.max_dir = 256;
options.dial_mode = XD_BUFFERED;
options.bufsize = 512;
options.tabsize = 8;
#if _PREDEF
options.cprefs = CF_COPY | CF_DEL | CF_OVERW | CF_PRINT | CF_TOUCH | CF_SHOWD;
options.fields = WD_SHSIZ | WD_SHDAT | WD_SHTIM | WD_SHATT | WD_SHOWN;
options.plinelen = DEF_PLINE;
options.attribs = FA_DIR | FA_SYSTEM;
#endif
options.aarr = 1;
/*
* There is no need to set options.sort, .mode, .sexit, .dsk_pattern,
* .dsk_colour, .win_pattern and .win_colour because all of options
* is set to 0 in memclr() above
*/
}
/*
* Define some default keyboard shortcuts ...
* those offered here have become a de-facto standard in other applications.
* On the other hand, they are different from those which Atari has adopted
* for the in-built TOS desktop.Personally, I would prefer some other ones,
* e.g. "?" for "Info..." and "*" for "Select all"
*/
static void short_default(void)
{
memclr(&options.kbshort[MOPEN - MFIRST], (MSAVESET - MFIRST + 1) * sizeof(options.kbshort[0]));
#if _PREDEF
options.kbshort[MOPEN - MFIRST] = XD_CTRL | 'O'; /* ^O, etc. */
options.kbshort[MSHOWINF - MFIRST] = XD_CTRL | 'I';
options.kbshort[MSEARCH - MFIRST] = XD_CTRL | 'F';
options.kbshort[MPRINT - MFIRST] = XD_CTRL | 'P';
options.kbshort[MSELALL - MFIRST] = XD_CTRL | 'A';
options.kbshort[MQUIT - MFIRST] = XD_CTRL | 'Q';
options.kbshort[MDELETE - MFIRST] = XD_CTRL | DELETE;
options.kbshort[MSAVESET - MFIRST] = XD_CTRL | 'S';
options.kbshort[MCYCLE - MFIRST] = XD_CTRL | 'W';
#endif
ins_shorts();
}
/*
* Configuration routine for basic desktop options
*/
static void opt_config(XFILE *file, int lvl, int io, int *error)
{
if (io == CFG_SAVE)
{
/* Save options */
options.version = CFG_VERSION;
get_set_video(0); /* get current video mode */
*error = CfgSave(file, Options_table, lvl, TRUE); /* save empty/0 fields as well */
} else
{
/* Initialize options structure to zero, then default, then load options */
opt_default();
*error = CfgLoad(file, Options_table, MAX_KEYLEN, lvl);
if (*error >= 0)
{
/*
* Check some critical values against limits; if not checked and
* some illegal value happened to be in the file, these variables
* might crash the program or have other ugly consequences
*/
if (options.plinelen < MIN_PLINE) /* probably not entered in the dialog */
options.plinelen = DEF_PLINE;
if (options.version < MIN_VERSION ||
options.version > CFG_VERSION ||
(options.sort & ~(WD_REVSORT | WD_NOCASE)) > WD_NOSORT ||
options.plinelen > MAX_PLINE ||
options.max_dir < 32 ||
(options.dial_mode & DIAL_MODE) > XD_WINDOW)
{
*error = EFRVAL;
}
if (*error >= 0)
{
/* Block possible junk from some fields in config file */
options.attribs &= 0x0077;
options.dsk_pattern = limpattern(options.dsk_pattern);
options.win_pattern = limpattern(options.win_pattern);
#if 0 /* currently not used */
options.vrez &= 0x0007;
#endif
/* Currently it makes no sense NOT to confirm touch */
options.cprefs |= CF_TOUCH;
options.cprefs &= ~(CF_CTIME | CF_CATTR);
#if PALETTES
/* Load palette. Ignore errors */
handle_colours(CFG_LOAD);
#endif
/* Set video state but do not change resolution */
get_set_video(1);
/* If all is OK so far, start setting TeraDesk */
set_dsk_background(options.dsk_pattern, options.dsk_colour);
}
}
}
}
/*
* Configure (load or save) keyboard shortcuts
* Note: for loading, this is initialized to zero earlier in opt_config.
*/
static void short_config(XFILE *file, int lvl, int io, int *error)
{
*error = handle_cfg(file, Shortcut_table, lvl, CFGEMP, io, NULL, short_default);
if (io == CFG_LOAD)
{
if (*error == 0)
ins_shorts();
}
}
/* Root level of configuration data */
static CfgEntry const Config_table[] = {
CFG_NEST("options", opt_config),
CFG_NEST("shortcuts", short_config),
CFG_NEST("filetypes", ft_config),
CFG_NEST("apptypes", prg_config), /* must be before icons and apps */
CFG_NEST("icontypes", icnt_config),
CFG_NEST("deskicons", dsk_config),
CFG_NEST("applications", app_config),
CFG_NEST("windows", wd_config),
CFG_NEST("avstats", va_config),
CFG_FINAL(), /* file completness check */
CFG_LAST()
};
/*
* Read configuration from the configuration file