-
Notifications
You must be signed in to change notification settings - Fork 17
/
trs_gtkinterface.c
1913 lines (1705 loc) · 52.9 KB
/
trs_gtkinterface.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
/*
* Copyright (c) 2009-2020, Timothy P. Mann
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* XXX TODO
* - design more menus, toolbar, and dialogs. remove any unused menu items.
* - create a window for zbx instead of using the launch xterm.
* - save/load settings to a file (file selectable on command line). useful to
* replace old way of pre-configured directories with disk?-? symlinks.
* - implement copy/paste. can maybe steal some code from sdltrs.
* - generally, look at sdltrs for ideas and code to steal.
* - split up this file more
* - fix any remaining XXX's
*/
/*XXX for command line parsing, should move? */
#define _XOPEN_SOURCE 500 /* string.h: strdup() */
#include <getopt.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
/*XXX end */
#define GDK_ENABLE_BROKEN 1 // needed for gdk_image_new_bitmap
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include "trs.h"
#include "trs_iodefs.h"
#include "trs_disk.h"
#include "trs_uart.h"
#include "keyrepeat.h"
/*#define MOUSEDEBUG 6*/
/*#define KDEBUG 1*/
#define MAX_SCALE 4
/* currentmode values */
#define NORMAL 0
#define EXPANDED 1
#define INVERSE 2
#define ALTERNATE 4
/* Private data */
static unsigned char trs_screen[2048];
static int screen_chars = 1024;
static int row_chars = 64;
static int col_chars = 16;
static int resize = -1;
static int top_margin = 0;
static int left_margin = 0;
static int border_width = 2;
static int currentmode = NORMAL;
static int cur_screen_width, cur_screen_height;
static int cur_char_height, cur_char_width;
static int text80x24 = 0, screen640x240 = 0;
static int trs_charset;
static int scale_x = 1;
static int scale_y = 0;
GtkWidget *main_window;
GtkWidget *about_dialog;
GtkWidget *keys_window;
GtkWidget *quit_dialog;
GtkWidget *drawing_area;
GdkPixmap *trs_screen_pixmap;
GdkGC *gc;
GdkGC *gc_inv;
GdkGC *gc_xor;
GdkColor fore_color, back_color, xor_color, null_color;
static GdkPixmap *trs_char[2][2][MAXCHARS];
static GdkPixmap *trs_box[2][64];
/* Support for Micro Labs Grafyx Solution and Radio Shack hi-res card */
/* True size of graphics memory -- some is offscreen */
#define G_XSIZE 128
#define G_YSIZE 256
unsigned char grafyx_unscaled[G_YSIZE][G_XSIZE];
GdkImage *grafyx_image;
int grafyx_microlabs = 0;
unsigned char grafyx_x = 0, grafyx_y = 0, grafyx_mode = 0;
unsigned char grafyx_enable = 0;
unsigned char grafyx_overlay = 0;
unsigned char grafyx_xoffset = 0, grafyx_yoffset = 0;
/* Port 0x83 (grafyx_mode) bits */
#define G_ENABLE 1
#define G_UL_NOTEXT 2 /* Micro Labs only */
#define G_RS_WAIT 2 /* Radio Shack only */
#define G_XDEC 4
#define G_YDEC 8
#define G_XNOCLKR 16
#define G_YNOCLKR 32
#define G_XNOCLKW 64
#define G_YNOCLKW 128
/* Port 0xFF (grafyx_m3_mode) bits */
#define G3_COORD 0x80
#define G3_ENABLE 0x40
#define G3_COMMAND 0x20
#define G3_YLOW(v) (((v)&0x1e)>>1)
void grafyx_init(void);
#define HRG_MEMSIZE (1024 * 12) /* 12k * 8 bit graphics memory */
static unsigned char hrg_screen[HRG_MEMSIZE];
static int hrg_pixel_x[2][6+1];
static int hrg_pixel_y[12+1];
static int hrg_pixel_width[2][6];
static int hrg_pixel_height[12];
static int hrg_enable = 0;
static int hrg_addr = 0;
static void hrg_update_char(int position);
/*
* Create a pixmap from xbm data, scaled by the given scale_x, scale_y.
*/
GdkPixmap*
pixmap_create_from_data_scale(GdkDrawable *drawable,
const gchar *data,
gint width, gint height,
gint scale_x, gint scale_y,
const GdkColor *fore_color,
const GdkColor *back_color)
{
gchar *mydata;
gchar *mypixels;
int i, j, k;
int width8; // width rounded up to a multiple of 8
int scaled_width, scaled_width8, scaled_height;
GdkPixmap *pm;
width8 = (width + 7) / 8 * 8;
scaled_width = width * scale_x;
scaled_width8 = (scaled_width + 7) / 8 * 8;
scaled_height = height * scale_y;
mydata = g_malloc(scaled_width8 * scaled_height * 8);
mypixels= g_malloc(width8 * height * 8);
/* Read the character data */
for (j = 0; j < width8 * height; j += 8) {
for (i = j + 7; i >= j; i--) {
*(mypixels + i) = (*(data + (j >> 3)) >> (i - j)) & 1;
}
}
/* Clear out the rescaled character array */
memset(mydata, 0, scaled_width8 * scaled_height * 4);
/* And prepare our rescaled character. */
k= 0;
for (j = 0; j < scaled_height; j++) {
for (i = 0; i < scaled_width8; i++) {
*(mydata + (k >> 3)) |=
(*(mypixels + ((int)(j / scale_y) * width8) +
(int)(i / scale_x)) << (k & 7));
k++;
}
}
pm = gdk_pixmap_create_from_data(drawable, mydata,
scaled_width, scaled_height, -1,
fore_color, back_color);
g_free(mydata);
g_free(mypixels);
return pm;
}
void
font_init(void)
{
/* Initialize from built-in font bitmaps. */
int i;
for (i = 0; i < MAXCHARS; i++) {
trs_char[0][0][i] =
pixmap_create_from_data_scale(trs_screen_pixmap,
trs_char_data[trs_charset][i],
cur_char_width / scale_x,
cur_char_height / scale_y,
scale_x, scale_y,
&fore_color, &back_color);
trs_char[1][0][i] =
pixmap_create_from_data_scale(trs_screen_pixmap,
trs_char_data[trs_charset][i],
cur_char_width / scale_x,
cur_char_height / scale_y,
scale_x * 2, scale_y,
&fore_color, &back_color);
if (trs_model >= 4) {
trs_char[0][1][i] =
pixmap_create_from_data_scale(trs_screen_pixmap,
trs_char_data[trs_charset][i],
cur_char_width / scale_x,
cur_char_height / scale_y,
scale_x, scale_y,
&back_color, &fore_color);
trs_char[1][1][i] =
pixmap_create_from_data_scale(trs_screen_pixmap,
trs_char_data[trs_charset][i],
cur_char_width / scale_x,
cur_char_height / scale_y,
scale_x * 2, scale_y,
&back_color, &fore_color);
}
}
}
/*
* Create a pixmap for each TRS-80 2x3 box graphics character.
*/
void
boxes_init(int width, int height, int expanded)
{
int graphics_char, bit;
GdkRectangle bits[6];
/*
* Calculate what the 2x3 boxes look like.
*/
bits[0].x = bits[2].x = bits[4].x = 0;
bits[0].width = bits[2].width = bits[4].width =
bits[1].x = bits[3].x = bits[5].x = width / 2;
bits[1].width = bits[3].width = bits[5].width = width - bits[1].x;
bits[0].y = bits[1].y = 0;
bits[0].height = bits[1].height =
bits[2].y = bits[3].y = height / 3;
bits[4].y = bits[5].y = (height * 2) / 3;
bits[2].height = bits[3].height = bits[4].y - bits[2].y;
bits[4].height = bits[5].height = height - bits[4].y;
for (graphics_char = 0; graphics_char < 64; ++graphics_char) {
trs_box[expanded][graphics_char] =
gdk_pixmap_new(trs_screen_pixmap, width, height, -1);
/* Clear everything */
gdk_draw_rectangle(trs_box[expanded][graphics_char],
gc_inv, TRUE, 0, 0, width, height);
/* Set the bits */
for (bit = 0; bit < 6; ++bit) {
if (graphics_char & (1 << bit)) {
gdk_draw_rectangle(trs_box[expanded][graphics_char], gc, TRUE,
bits[bit].x, bits[bit].y,
bits[bit].width, bits[bit].height);
}
}
}
}
/*
* Command line parsing. In trs_gtkinterface mostly for historical
* reasons (the old trs_xinterface looked in the X resource database)
* and partly because it sets a lot of values that are used here. Ugh.
*/
int opt_iconic = FALSE;
char *opt_background = NULL;
char *opt_foreground = NULL;
int opt_debug = FALSE;
char *opt_title;
int opt_shiftbracket = -1;
char *opt_charset = NULL;
char *opt_scale = NULL;
int opt_stepdefault = 1;
char *opt_stepmap = NULL;
char *opt_sizemap = NULL;
struct option options[] = {
/* Name, takes argument?, store int value at, value to store */
{"iconic", FALSE, &opt_iconic, TRUE },
{"noiconic", FALSE, &opt_iconic, FALSE },
{"background", TRUE, NULL, 0 },
{"bg", TRUE, NULL, 0 },
{"foreground", TRUE, NULL, 0 },
{"fg", TRUE, NULL, 0 },
{"title", TRUE, NULL, 0 },
{"borderwidth", TRUE, NULL, 0 },
{"scale", TRUE, NULL, 0 },
{"scale1", FALSE, &scale_x, 1 },
{"scale2", FALSE, &scale_x, 2 },
{"scale3", FALSE, &scale_x, 3 },
{"scale4", FALSE, &scale_x, 4 },
{"resize", FALSE, &resize, TRUE },
{"noresize", FALSE, &resize, FALSE },
{"charset", TRUE, NULL, 0 },
{"microlabs", FALSE, &grafyx_microlabs, TRUE },
{"nomicrolabs", FALSE, &grafyx_microlabs, FALSE },
{"debug", FALSE, &opt_debug, TRUE },
{"nodebug", FALSE, &opt_debug, FALSE },
{"romfile1", TRUE, NULL, 0 },
{"romfile1x", TRUE, NULL, 0 },
{"romfile3", TRUE, NULL, 0 },
{"romfile4p", TRUE, NULL, 0 },
{"model", TRUE, NULL, 0 },
{"model1", FALSE, &trs_model, 1 },
{"model3", FALSE, &trs_model, 3 },
{"model4", FALSE, &trs_model, 4 },
{"model4p", FALSE, &trs_model, 5 },
{"delay", TRUE, NULL, 0 },
{"autodelay", FALSE, &trs_autodelay, TRUE },
{"noautodelay", FALSE, &trs_autodelay, FALSE },
{"keystretch", TRUE, NULL, 0 },
{"keydelay", TRUE, NULL, 0 },
{"shiftbracket", FALSE, &opt_shiftbracket, TRUE },
{"noshiftbracket", FALSE, &opt_shiftbracket, FALSE },
{"diskdir", TRUE, NULL, 0 },
{"doubler", TRUE, NULL, 0 },
{"doublestep", FALSE, &opt_stepdefault, 2 },
{"nodoublestep", FALSE, &opt_stepdefault, 1 },
{"stepmap", TRUE, NULL, 0 },
{"sizemap", TRUE, NULL, 0 },
{"truedam", FALSE, &trs_disk_truedam, TRUE },
{"notruedam", FALSE, &trs_disk_truedam, FALSE },
{"samplerate", TRUE, NULL, 0 },
{"serial", TRUE, NULL, 0 },
{"switches", TRUE, NULL, 0 },
{"emtsafe", FALSE, &trs_emtsafe, TRUE },
{"noemtsafe", FALSE, &trs_emtsafe, FALSE },
{"lowercase", FALSE, &trs_lowercase, TRUE },
{"nolowercase", FALSE, &trs_lowercase, FALSE },
{"year", TRUE, NULL, 0 },
{NULL, 0, 0, 0}
};
int
trs_parse_command_line(int argc, char **argv, int *debug)
{
int i;
int s[8];
gtk_init(&argc, &argv);
opterr = 0;
for (;;) {
int c;
int option_index = 0;
const char *name;
c = getopt_long_only(argc, argv, "", options, &option_index);
if (c == -1) break;
if (c == '?') {
fatal("unrecognized option %s", argv[optind - 1]);
}
name = options[option_index].name;
if (strcmp(name, "background") == 0 ||
strcmp(name, "bg") == 0) {
opt_background = optarg;
} else if (strcmp(name, "foreground") == 0 ||
strcmp(name, "fg") == 0) {
opt_foreground = optarg;
} else if (strcmp(name, "title") == 0) {
opt_title = optarg;
} else if (strcmp(name, "borderwidth") == 0) {
border_width = strtoul(optarg, NULL, 0);
} else if (strcmp(name, "scale") == 0) {
sscanf(optarg, "%u,%u", &scale_x, &scale_y);
} else if (strcmp(name, "charset") == 0) {
opt_charset = optarg;
} else if (strcmp(name, "romfile1") == 0) {
romfile1 = optarg;
} else if (strcmp(name, "romfile1x") == 0) {
romfile1x = optarg;
} else if (strcmp(name, "romfile3") == 0) {
romfile3 = optarg;
} else if (strcmp(name, "romfile4p") == 0) {
romfile4p = optarg;
} else if (strcmp(name, "model") == 0) {
if (strcmp(optarg, "1") == 0 ||
strcasecmp(optarg, "I") == 0) {
trs_model = 1;
} else if (strcmp(optarg, "3") == 0 ||
strcasecmp(optarg, "III") == 0) {
trs_model = 3;
} else if (strcmp(optarg, "4") == 0 ||
strcasecmp(optarg, "IV") == 0) {
trs_model = 4;
} else if (strcasecmp(optarg, "4P") == 0 ||
strcasecmp(optarg, "IVp") == 0) {
trs_model = 5;
} else {
fatal("TRS-80 Model %s not supported", optarg);
}
} else if (strcmp(name, "delay") == 0) {
z80_state.delay = strtol(optarg, NULL, 0);
} else if (strcmp(name, "keydelay") == 0) {
trs_keydelay = strtol(optarg, NULL, 0);
} else if (strcmp(name, "keystretch") == 0) {
stretch_amount = strtol(optarg, NULL, 0);
} else if (strcmp(name, "diskdir") == 0) {
trs_disk_dir = strdup(optarg);
if (trs_disk_dir[0] == '~' &&
(trs_disk_dir[1] == '/' || trs_disk_dir[1] == '\0')) {
char* home = getenv("HOME");
if (home) {
char *p = (char*)malloc(strlen(home) + strlen(trs_disk_dir) + 1);
sprintf(p, "%s/%s", home, trs_disk_dir+1);
trs_disk_dir = p;
}
}
} else if (strcmp(name, "doubler") == 0) {
switch (optarg[0]) {
case 'p':
case 'P':
trs_disk_doubler = TRSDISK_PERCOM;
break;
case 'r':
case 'R':
case 't':
case 'T':
trs_disk_doubler = TRSDISK_TANDY;
break;
case 'b':
case 'B':
trs_disk_doubler = TRSDISK_BOTH;
break;
case 'n':
case 'N':
trs_disk_doubler = TRSDISK_NODOUBLER;
break;
default:
fatal("unrecognized doubler type %s\n", optarg);
}
} else if (strcmp(name, "stepmap") == 0) {
opt_stepmap = optarg;
} else if (strcmp(name, "sizemap") == 0) {
opt_sizemap = optarg;
} else if (strcmp(name, "samplerate") == 0) {
cassette_default_sample_rate = strtol(optarg, NULL, 0);
} else if (strcmp(name, "serial") == 0) {
trs_uart_name = strdup(optarg);
} else if (strcmp(name, "switches") == 0) {
trs_uart_switches = strtol(optarg, NULL, 0);
} else if (strcmp(name, "year") == 0) {
trs_inityear(strtol(optarg, NULL, 0));
}
}
if (optind != argc) {
fatal("unrecognized argument %s", argv[optind]);
}
/*
* Some additional processing needed after all options are parsed.
* In some cases the order is important; e.g., trs_model must be known.
*/
*debug = opt_debug;
if (resize == -1) {
resize = (trs_model == 3);
}
if (opt_shiftbracket == -1) {
opt_shiftbracket = trs_model >= 4;
}
trs_kb_bracket(opt_shiftbracket);
if (scale_y == 0) scale_y = 2 * scale_x;
/* Note: charset numbers must match trs_chars.c */
if (trs_model == 1) {
if (opt_charset == NULL) {
opt_charset = "wider"; /* default */
}
if (isdigit(*opt_charset)) {
trs_charset = strtol(opt_charset, NULL, 0);
cur_char_width = 8 * scale_x;
} else {
if (opt_charset[0] == 'e'/*early*/) {
trs_charset = 0;
cur_char_width = 6 * scale_x;
} else if (opt_charset[0] == 's'/*stock*/) {
trs_charset = 1;
cur_char_width = 6 * scale_x;
} else if (opt_charset[0] == 'l'/*lcmod*/) {
trs_charset = 2;
cur_char_width = 6 * scale_x;
} else if (opt_charset[0] == 'w'/*wider*/) {
trs_charset = 3;
cur_char_width = 8 * scale_x;
} else if (opt_charset[0] == 'g'/*genie or german*/) {
trs_charset = 10;
cur_char_width = 8 * scale_x;
} else {
fatal("unknown charset name %s", opt_charset);
}
}
cur_char_height = TRS_CHAR_HEIGHT * scale_y;
} else /* trs_model > 1 */ {
if (opt_charset == NULL) {
/* default */
opt_charset = (trs_model == 3) ? "katakana" : "international";
}
if (isdigit(*opt_charset)) {
trs_charset = strtol(opt_charset, NULL, 0);
} else {
if (opt_charset[0] == 'k'/*katakana*/) {
trs_charset = 4 + 3*(trs_model > 3);
} else if (opt_charset[0] == 'i'/*international*/) {
trs_charset = 5 + 3*(trs_model > 3);
} else if (opt_charset[0] == 'b'/*bold*/) {
trs_charset = 6 + 3*(trs_model > 3);
} else {
fatal("unknown charset name %s", opt_charset);
}
}
cur_char_width = TRS_CHAR_WIDTH * scale_x;
cur_char_height = TRS_CHAR_HEIGHT * scale_y;
}
for (i = 0; i <= 7; i++) {
s[i] = opt_stepdefault;
}
if (opt_stepmap) {
sscanf(opt_stepmap, "%d,%d,%d,%d,%d,%d,%d,%d",
&s[0], &s[1], &s[2], &s[3], &s[4], &s[5], &s[6], &s[7]);
}
for (i = 0; i <= 7; i++) {
if (s[i] != 1 && s[i] != 2) {
fatal("bad value %d for disk %d single/double step\n", s[i], i);
} else {
trs_disk_setstep(i, s[i]);
}
}
/* Defaults for sizemap */
s[0] = 5;
s[1] = 5;
s[2] = 5;
s[3] = 5;
s[4] = 8;
s[5] = 8;
s[6] = 8;
s[7] = 8;
if (opt_sizemap) {
sscanf(opt_sizemap, "%d,%d,%d,%d,%d,%d,%d,%d",
&s[0], &s[1], &s[2], &s[3], &s[4], &s[5], &s[6], &s[7]);
}
for (i = 0; i <= 7; i++) {
if (s[i] != 5 && s[i] != 8) {
fatal("bad value %d for disk %d size", s[i], i);
} else {
trs_disk_setsize(i, s[i]);
}
}
return 1;
}
void trs_exit(void)
{
gtk_exit(0);
}
void
trs_screen_init(void)
{
const char *glade_file = GLADE_FILE;
GtkBuilder *builder;
GError *err = NULL;
builder = gtk_builder_new();
if (gtk_builder_add_from_file(builder, glade_file, &err) == 0) {
fatal(err->message);
}
main_window = GTK_WIDGET(gtk_builder_get_object(builder,
"main_window"));
drawing_area = GTK_WIDGET(gtk_builder_get_object(builder,
"drawing_area"));
about_dialog = GTK_WIDGET(gtk_builder_get_object(builder,
"about_dialog"));
keys_window = GTK_WIDGET(gtk_builder_get_object(builder,
"keys_window"));
quit_dialog = GTK_WIDGET(gtk_builder_get_object(builder,
"quit_dialog"));
/*
* Work around glade-3 not letting me connect to the delete-event
* signal for dialogs, instead leaving it with the default
* that destroys the dialog. XXX is this still a problem?
*/
g_signal_connect(GTK_OBJECT(about_dialog), "delete-event",
GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete), NULL);
g_signal_connect(GTK_OBJECT(keys_window), "delete-event",
GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete), NULL);
g_signal_connect(GTK_OBJECT(quit_dialog), "delete-event",
GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete), NULL);
gtk_builder_connect_signals(builder, NULL);
g_object_unref(G_OBJECT(builder));
if (trs_model >= 3 && !resize) {
cur_screen_width = cur_char_width * 80 + 2 * border_width;
left_margin = cur_char_width * (80 - row_chars) / 2 + border_width;
cur_screen_height = TRS_CHAR_HEIGHT4 * scale_y * 24 + 2 * border_width;
top_margin = (TRS_CHAR_HEIGHT4 * scale_y * 24 -
cur_char_height * col_chars) / 2 + border_width;
} else {
cur_screen_width = cur_char_width * row_chars + 2 * border_width;
left_margin = border_width;
cur_screen_height = cur_char_height * col_chars + 2 * border_width;
top_margin = border_width;
}
gtk_widget_set_size_request(drawing_area,
cur_screen_width, cur_screen_height);
if (opt_background) {
if (!gdk_color_parse(opt_background, &back_color)) {
fatal("unrecognized color %s", opt_background);
}
if (!gdk_colormap_alloc_color(gdk_colormap_get_system(),
&back_color, FALSE, TRUE)) {
fatal("failed to allocate color %s", opt_background);
}
} else {
gdk_color_black(gdk_colormap_get_system(), &back_color);
}
if (opt_foreground) {
if (!gdk_color_parse(opt_foreground, &fore_color)) {
fatal("unrecognized color %s", opt_foreground);
}
if (!gdk_colormap_alloc_color(gdk_colormap_get_system(),
&fore_color, FALSE, TRUE)) {
fatal("failed to allocate color %s", opt_foreground);
}
} else {
gdk_color_white(gdk_colormap_get_system(), &fore_color);
}
/*
* The xor_color is a magic color that swaps fore_color and
* back_color when it is xored in, and null_color is a color that
* causes no change when xor'ed in. It is the actual pixel value
* that gets xor'ed, so we must construct the xor_color pixel value
* here by xor'ing the fore_color and back_color pixel values, not
* by xor'ing the RGB components and asking gdk_colormap_alloc_color
* to allocate the closest matching color. Similarly, the null_color
* simply has pixel value 0.
*/
xor_color.pixel = fore_color.pixel ^ back_color.pixel;
null_color.pixel = 0;
if (opt_iconic) {
gtk_window_iconify(GTK_WINDOW(main_window));
}
gtk_widget_realize(main_window);
gdk_window_set_title(main_window->window,
opt_title ? opt_title : program_name);
trs_screen_pixmap =
gdk_pixmap_new(main_window->window,
640 * scale_x + 2 * border_width,
240 * scale_y + 2 * border_width, -1);
gc = gdk_gc_new(trs_screen_pixmap);
gdk_gc_set_foreground(gc, &fore_color);
gdk_gc_set_background(gc, &back_color);
gdk_gc_set_function(gc, GDK_COPY);
gc_inv = gdk_gc_new(trs_screen_pixmap);
gdk_gc_set_foreground(gc_inv, &back_color);
gdk_gc_set_background(gc_inv, &fore_color);
gdk_gc_set_function(gc_inv, GDK_COPY);
gc_xor = gdk_gc_new(trs_screen_pixmap);
gdk_gc_set_foreground(gc_xor, &xor_color);
gdk_gc_set_background(gc_xor, &null_color);
gdk_gc_set_function(gc_xor, GDK_XOR);
gdk_draw_rectangle(trs_screen_pixmap, gc_inv, TRUE,
0, 0, cur_screen_width, cur_screen_height);
font_init();
boxes_init(cur_char_width, TRS_CHAR_HEIGHT * scale_y, 0);
boxes_init(cur_char_width * 2, TRS_CHAR_HEIGHT * scale_y, 1);
grafyx_init();
gtk_settings_set_string_property (gtk_settings_get_default (),
"gtk-menu-bar-accel", "F12", NULL);
gtk_widget_show(main_window);
}
void
trs_screen_write_char(int position, int char_index)
{
int row, col, destx, desty, expanded, width, height;
trs_screen[position] = char_index;
if (position >= screen_chars) {
return;
}
if ((currentmode & EXPANDED) && (position & 1)) {
return;
}
if (grafyx_enable && !grafyx_overlay) {
return;
}
row = position / row_chars;
col = position - (row * row_chars);
destx = col * cur_char_width + left_margin;
desty = row * cur_char_height + top_margin;
expanded = (currentmode & EXPANDED) != 0;
width = cur_char_width * (expanded + 1);
height = cur_char_height;
if (trs_model == 1 && char_index >= 0xc0) {
/* On Model I, 0xc0-0xff is another copy of 0x80-0xbf */
char_index -= 0x40;
}
if (char_index >= 0x80 && char_index <= 0xbf && !(currentmode & INVERSE)) {
/* Use box graphics character bitmap */
gdk_draw_drawable(trs_screen_pixmap, gc,
trs_box[expanded][char_index - 0x80],
0, 0, destx, desty, width, height);
} else {
int inverse = 0;
/* Use regular character bitmap */
if (trs_model > 1 && char_index >= 0xc0 &&
(currentmode & (ALTERNATE+INVERSE)) == 0) {
char_index -= 0x40;
}
if ((currentmode & INVERSE) && (char_index & 0x80)) {
inverse = 1;
char_index &= 0x7f;
}
gdk_draw_drawable(trs_screen_pixmap, gc,
trs_char[expanded][inverse][char_index],
0, 0, destx, desty, width, height);
}
/* Overlay grafyx on character */
if (grafyx_enable) {
/* assert(grafyx_overlay); */
int srcx, srcy, duny;
srcx = ((col + grafyx_xoffset) % G_XSIZE) * cur_char_width;
srcy = (row * cur_char_height + grafyx_yoffset * scale_y)
% (G_YSIZE * scale_y);
duny = G_YSIZE * scale_y - srcy;
gdk_draw_image(trs_screen_pixmap, gc_xor, grafyx_image,
srcx, srcy, destx, desty,
cur_char_width, MIN(duny, cur_char_height));
/* Draw wrapped portion if any */
if (duny < cur_char_height) {
gdk_draw_image(trs_screen_pixmap, gc_xor, grafyx_image, srcx, 0,
destx, desty + duny,
cur_char_width, cur_char_height - duny);
}
}
if (hrg_enable) {
hrg_update_char(position);
}
gtk_widget_queue_draw_area(drawing_area,
destx, desty, width, height);
}
void
trs_screen_refresh(void)
{
int i;
int srcx, srcy, dunx, duny;
if (grafyx_enable && !grafyx_overlay) {
srcx = cur_char_width * grafyx_xoffset;
srcy = scale_y * grafyx_yoffset;
dunx = (G_XSIZE * scale_x * 8) - srcx;
duny = (G_YSIZE * scale_y) - srcy;
gdk_draw_image(trs_screen_pixmap, gc, grafyx_image,
srcx, srcy,
left_margin, top_margin,
MIN(dunx, cur_char_width * row_chars),
MIN(duny, cur_char_height * col_chars));
/* Draw wrapped portions if any */
if (dunx < cur_char_width * row_chars) {
gdk_draw_image(trs_screen_pixmap, gc, grafyx_image,
0, srcy,
left_margin + dunx, top_margin,
cur_char_width * row_chars - dunx,
MIN(duny, cur_char_height * col_chars));
}
if (duny < cur_char_height * col_chars) {
gdk_draw_image(trs_screen_pixmap, gc, grafyx_image,
srcx, 0,
left_margin, top_margin + duny,
MIN(dunx, cur_char_width * row_chars),
cur_char_height * col_chars - duny);
if (dunx < cur_char_width * row_chars) {
gdk_draw_image(trs_screen_pixmap, gc, grafyx_image,
0, 0,
left_margin + dunx, top_margin + duny,
cur_char_width * row_chars - dunx,
cur_char_height * col_chars - duny);
}
}
gtk_widget_queue_draw_area(drawing_area, 0, 0,
cur_screen_width, cur_screen_height);
} else {
for (i = 0; i < screen_chars; i++) {
trs_screen_write_char(i, trs_screen[i]);
}
}
}
/*
* Copies lines 1 to col_chars - 1 to lines 0 to col_chars - 2.
* Does not clear line col_chars - 1.
*/
void trs_screen_scroll(void)
{
int i = 0;
for (i = row_chars; i < screen_chars; i++)
trs_screen[i - row_chars] = trs_screen[i];
if (grafyx_enable) {
if (grafyx_overlay) {
trs_screen_refresh();
}
} else if (hrg_enable) {
trs_screen_refresh();
} else {
gdk_draw_drawable(trs_screen_pixmap, gc, trs_screen_pixmap,
0, top_margin + cur_char_height, 0, top_margin,
cur_screen_width,
cur_screen_height - cur_char_height - top_margin * 2);
gtk_widget_queue_draw(drawing_area);
}
}
void trs_screen_expanded(int flag)
{
int bit = flag ? EXPANDED : 0;
if ((currentmode ^ bit) & EXPANDED) {
currentmode ^= EXPANDED;
trs_screen_refresh();
}
}
void trs_screen_inverse(int flag)
{
int bit = flag ? INVERSE : 0;
int i;
if ((currentmode ^ bit) & INVERSE) {
currentmode ^= INVERSE;
for (i = 0; i < screen_chars; i++) {
if (trs_screen[i] & 0x80)
trs_screen_write_char(i, trs_screen[i]);
}
}
}
void trs_screen_alternate(int flag)
{
int bit = flag ? ALTERNATE : 0;
int i;
if ((currentmode ^ bit) & ALTERNATE) {
currentmode ^= ALTERNATE;
for (i = 0; i < screen_chars; i++) {
if (trs_screen[i] >= 0xc0)
trs_screen_write_char(i, trs_screen[i]);
}
}
}
void trs_screen_640x240(int flag)
{
if (flag == screen640x240) return;
screen640x240 = flag;
if (flag) {
row_chars = 80;
col_chars = 24;
cur_char_height = TRS_CHAR_HEIGHT4 * scale_y;
} else {
row_chars = 64;
col_chars = 16;
cur_char_height = TRS_CHAR_HEIGHT * scale_y;
}
screen_chars = row_chars * col_chars;
if (resize) {
cur_screen_width = cur_char_width * row_chars + 2 * border_width;
left_margin = border_width;
cur_screen_height = cur_char_height * col_chars + 2 * border_width;
top_margin = border_width;
gtk_widget_set_size_request(drawing_area,
cur_screen_width, cur_screen_height);
} else {
left_margin = cur_char_width * (80 - row_chars) / 2 + border_width;
top_margin = (TRS_CHAR_HEIGHT4 * scale_y * 24 -
cur_char_height * col_chars) / 2 + border_width;
if (left_margin > border_width || top_margin > border_width) {
gdk_draw_rectangle(trs_screen_pixmap, gc_inv, TRUE,
0, 0, cur_screen_width, cur_screen_height);
gtk_widget_queue_draw(drawing_area);
}
}
trs_screen_refresh();
}
void trs_screen_80x24(int flag)
{
if (!grafyx_enable || grafyx_overlay) {
trs_screen_640x240(flag);
}
text80x24 = flag;
}
/*
* Get and process GTK event(s).
*
* If wait is true, we think there's nothing to do right now and
* want to give up the CPU until there is something to do.
* Unfortunately we can't simply call gtk_main_iteration_do in
* blocking mode, because (currently) we don't timer ticks via GTK.
* Instead, trs_interrupt.c uses an itimer and gets a SIGALRM
* callback, and that doesn't cause gtk_main_iteration_do to
* unblock. So instead we pause() and then call
* gtk_main_iteration_do in nonblocking mode.
*
* If wait is false we definitely don't want to block for events.
*
* Handle interrupt-driven uart input here too.
*
*/
void trs_get_event(int wait)
{
if (trs_model > 1) {
(void)trs_uart_check_avail();
}
if (wait) {
pause();
trs_paused = 1;
}
do {
gtk_main_iteration_do(FALSE);
} while (gtk_events_pending());
}
void
on_reset_menu_item_activate(GtkMenuItem *menuitem,
gpointer user_data)
{
trs_reset(0);
}