-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtu56.c
498 lines (426 loc) · 14.2 KB
/
tu56.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
/*
* tu56.c
*
* A visual display of the DECtape TU56 front panel
*
* for the Raspberry Pi and other Linux systems
*
* Copyright 2019 rricharz
*
* 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 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., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
#define TSTATE_ONLINE 1
#define TSTATE_DRIVE1 2
#define TSTATE_BACKWARDS 4
#define TSTATE_SEEK 8
#define TSTATE_READ 16
#define TSTATE_WRITE 32
#define GDK_DISABLE_DEPRECATION_WARNINGS
#include <cairo.h>
#include <math.h>
#include <gtk/gtk.h>
// The TU56 tape status is checked every TIME_INTERVAL milliseconds
// If the status changes, the display is updated immediately
//
// Blurred rotational motion pictures of the reels are only updated
// every TIME_INTERVAL * C_ANIMATION, to reduce CPU usage
//
// Tape motion on and off times are extended to a minimal time of
// TIME_INTERVAL * C_TAPE to make them visible
#define TIME_INTERVAL 10 // timer interval in msec
#define DRAW_MULTIPLIER 10
#define C_ANIMATION 4 // blurred tape animation cycles
#define C_TAPE 8 // tape on off minimum cycles
#define REELAX 52
#define REELAY 524
#define REELBX 513
#define REELBY 525
#define REELCX 979
#define REELCY 524
#define REELDX 1439
#define REELDY 526
int lightx[4] = {131, 812, 1037, 1725};
int lighty[4] = {79, 77, 77, 77};
int buttonx[6] = {198, 546, 741, 1099, 1453, 1649};
int buttony[6] = {78, 78, 78, 78, 78, 78};
#define BUTTONXSIZE 68
#define BUTTONYSIZE 150
struct {
cairo_surface_t *image;
cairo_surface_t *light1on, *lightoff, *light2on;
cairo_surface_t *reelA[3], *reelB[3], *reelC[3], *reelD[3], *button[3];
int light[4], tape1, tape2, buttonstate[6];
int reelAindex, reelCindex;
double scale;
int remote_status, last_remote_status, reelsound, updated;
int argFullscreen, argAudio;
} glob;
int getStatus()
{
static FILE *statusFile = 0;
char *fname = "/tmp/tu56status";
int st;
// file needs to be opened again each time to read new status
statusFile = fopen(fname, "r");
if (statusFile != 0) {
st = getc(statusFile) - 32;
fclose(statusFile);
if (st >= 0)
return st;
else
return 0;
}
else return 0;
}
static void do_drawing(cairo_t *);
static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr,
gpointer user_data)
{
do_drawing(cr);
return FALSE;
}
static void do_drawing(cairo_t *cr)
{
static int animation_counter = 0;
int index;
animation_counter++;
// printf("do_drawing, animation_counter = %ld\n", animation_counter);
cairo_scale(cr,glob.scale,glob.scale);
cairo_set_source_surface(cr, glob.image, 0, 0);
cairo_paint(cr);
if (glob.light[0]) {
cairo_set_source_surface(cr, glob.light1on, lightx[0], lighty[0]);
}
else {
cairo_set_source_surface(cr, glob.lightoff, lightx[0], lighty[0]);
}
cairo_paint(cr);
if (glob.light[1]) {
cairo_set_source_surface(cr, glob.light2on, lightx[1], lighty[1]);
}
else {
cairo_set_source_surface(cr, glob.lightoff, lightx[1], lighty[1]);
}
cairo_paint(cr);
if (glob.light[2]) {
cairo_set_source_surface(cr, glob.light1on, lightx[2], lighty[2]);
}
else {
cairo_set_source_surface(cr, glob.lightoff, lightx[2], lighty[2]);
}
cairo_paint(cr);
if (glob.light[3]) {
cairo_set_source_surface(cr, glob.light2on, lightx[3], lighty[3]);
}
else {
cairo_set_source_surface(cr, glob.lightoff, lightx[3], lighty[3]);
}
cairo_paint(cr);
if (glob.tape1) {
index = 1 + (animation_counter % 2);
}
else {
index = 0;
}
if (index != glob.reelAindex) {
cairo_set_source_surface(cr, glob.reelA[index], REELAX, REELAY);
cairo_paint(cr);
cairo_set_source_surface(cr, glob.reelB[index], REELBX, REELBY);
cairo_paint(cr);
}
glob.reelAindex = index;
if (glob.tape2) {
index = 1 + (animation_counter % 2);
}
else {
index = 0;
}
if (index != glob.reelCindex) {
cairo_set_source_surface(cr, glob.reelC[index], REELCX, REELCY);
cairo_paint(cr);
cairo_set_source_surface(cr, glob.reelD[index], REELDX, REELDY);
cairo_paint(cr);
}
glob.reelCindex = index;
for (int i = 0; i < 6; i++) {
cairo_set_source_surface(cr, glob.button[glob.buttonstate[i]], buttonx[i], buttony[i]);
cairo_paint(cr);
}
if (((glob.tape1 == 0) && (glob.tape2 == 0)) && (glob.reelsound != 0)) {
glob.reelsound = 0;
// printf("turn reel sound off , tape1=%d, tape2=%d\n", glob.tape1, glob.tape2);
if (glob.argAudio) system("pkill mpg321");
}
}
static void do_logic()
{
glob.light[0] = 0;
glob.light[1] = 0;
glob.light[2] = 0;
glob.light[3] = 0;
glob.tape1 = (glob.buttonstate[1] != 0) && (glob.buttonstate[2] == 2);
glob.tape2 = (glob.buttonstate[4] != 0) && (glob.buttonstate[5] == 2);
if ((glob.buttonstate[2] == 1) || (glob.buttonstate[5] == 1)) {
// if remote is set in a drive
// if (glob.last_remote_status != glob.remote_status) printf("remote state = 0x%02x\n", glob.remote_status);
// set the lights and motors
// for now it also flips the write enable button if necessary
if ((glob.remote_status & TSTATE_DRIVE1) && (glob.buttonstate[5] == 1)) {
if (glob.remote_status & TSTATE_WRITE)
glob.buttonstate[3] = 1;
glob.light[2] = (glob.remote_status & TSTATE_WRITE);
glob.light[3] = (glob.remote_status & TSTATE_ONLINE);
glob.tape2 = (glob.remote_status & (TSTATE_READ | TSTATE_WRITE | TSTATE_SEEK));
}
else if (glob.buttonstate[2] == 1) {
if (glob.remote_status & TSTATE_WRITE)
glob.buttonstate[0] = 1;
glob.light[0] = (glob.remote_status & TSTATE_WRITE);
glob.light[1] = (glob.remote_status & TSTATE_ONLINE);
glob.tape1 = (glob.remote_status & (TSTATE_READ | TSTATE_WRITE | TSTATE_SEEK));
}
}
else
glob.remote_status = 0;
// stop tape for one frame if direction has changed
if ((glob.last_remote_status & TSTATE_BACKWARDS) != (glob.remote_status & TSTATE_BACKWARDS)) {
glob.tape1 = 0;
glob.tape2 = 0;
}
if (((glob.tape1 != 0) || (glob.tape2 != 0)) && (glob.reelsound == 0)) {
glob.reelsound = 1;
// printf("turn reel sound on , tape1=%d, tape2=%d\n", glob.tape1, glob.tape2);
if (glob.argAudio) system("/usr/bin/mpg321 -q -l 0 sound/reels.mp3 2> /dev/null &");
}
}
static gboolean on_timer_event(GtkWidget *widget)
{
static int timer_counter = 0;
timer_counter++;
if (timer_counter < DRAW_MULTIPLIER) {
if ((glob.buttonstate[2] == 1) || (glob.buttonstate[5] == 1)) {
// if remote is set in a drive
glob.remote_status |= getStatus();
}
else
glob.remote_status = 0;
return TRUE;
}
timer_counter = 0;
do_logic();
glob.updated = 0;
if ((glob.last_remote_status != glob.remote_status) || glob.updated)
gtk_widget_queue_draw(widget);
else if (glob.tape1 || glob.tape2)
gtk_widget_queue_draw(widget);
glob.last_remote_status = glob.remote_status;
glob.remote_status = 0;
return TRUE;
}
static gboolean on_button_release_event(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
{
// event-button = 1: means left mouse button; button = 3 means right mouse button
// printf("on_button_release_event called, button %d, x = %d, y= %d\n", (int)event->button, (int)event->x, (int)event->y);
int x = (int)((double)event->x / glob.scale);
int y = (int)((double)event->y / glob.scale);
if (event->button == 1) {
if ((x >= buttonx[1]) && (x <= buttonx[1] + BUTTONXSIZE) &&
(y >= buttony[1]) && (y <= buttony[1] + BUTTONYSIZE)) {
glob.buttonstate[1] = 0;
do_logic();
if (glob.argAudio) system("/usr/bin/mpg321 -q sound/switch.mp3 2> /dev/null &");
gtk_widget_queue_draw(widget);
return TRUE;
}
if ((x >= buttonx[4]) && (x <= buttonx[4] + BUTTONXSIZE) &&
(y >= buttony[4]) && (y <= buttony[4] + BUTTONYSIZE)) {
glob.buttonstate[4] = 0;
do_logic();
if (glob.argAudio) system("/usr/bin/mpg321 -q sound/switch.mp3 2> /dev/null &");
gtk_widget_queue_draw(widget);
return TRUE;
}
}
}
static gboolean on_button_click_event(GtkWidget *widget, GdkEventButton *event, gpointer user_data)
{
// event-button = 1: means left mouse button; button = 3 means right mouse button
// printf("on_button_click_event called, button %d, x = %d, y= %d\n", (int)event->button, (int)event->x, (int)event->y);
int x = (int)((double)event->x / glob.scale);
int y = (int)((double)event->y / glob.scale);
if (event->button == 1) {
for (int i = 0; i < 6; i++) {
if ((x >= buttonx[i]) && (x <= buttonx[i] + BUTTONXSIZE) &&
(y >= buttony[i]) && (y <= buttony[i] + BUTTONYSIZE)) {
if (y <= buttony[i] + (BUTTONYSIZE / 2)) {
if (glob.argAudio) system("/usr/bin/mpg321 -q sound/switch.mp3 2> /dev/null &");
if ((i == 0) || (i == 3)) // 2 state buttons
glob.buttonstate[i] = 1;
else if (glob.buttonstate[i] != 0) // 3 state button
glob.buttonstate[i] = 0;
else
glob.buttonstate[i] = 1;
}
else {
if (glob.argAudio) system("/usr/bin/mpg321 -q sound/switch.mp3 2> /dev/null &");
if ((i == 0) || (i == 3)) // 2 state buttons
glob.buttonstate[i] = 2;
else if (glob.buttonstate[i] != 0) // 3 state button
glob.buttonstate[i] = 0;
else
glob.buttonstate[i] = 2;
}
do_logic();
gtk_widget_queue_draw(widget);
// printf("button state %d: %d\n",i,glob.buttonstate[i]);
return TRUE;
}
}
}
return TRUE;
}
static void on_quit_event()
{
system("pkill mpg321");
gtk_main_quit();
exit(0);
}
static void on_key_press(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
{
int ch;
// printf("key pressed, state =%04X, keyval =%04X\n", event->state, event->keyval);
if ((event->state == 0x4) && (event->keyval == 0x0063)) // ctrl-c
on_quit_event();
else if ((event->state == 0x4) && (event->keyval == 0x0071)) // ctrl-q
on_quit_event();
else if ((event->state == 0x0) && (event->keyval == 0xFF1B)) // esc
on_quit_event();
}
cairo_surface_t* readpng(char* s)
{
cairo_surface_t *t = cairo_image_surface_create_from_png(s);
if ((t == 0) || cairo_surface_status(t)) {
printf("Cannot load %s\n",s);
exit(1);
}
return t;
}
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *darea;
glob.argFullscreen = 0;
glob.argAudio = 0;
int firstArg = 1;
printf("tu56 version 0.8\n");
system("pkill mpg321");
while (firstArg < argc) {
if (strcmp(argv[firstArg],"-full") == 0)
glob.argFullscreen = 1;
else if (strcmp(argv[firstArg],"-audio") == 0)
glob.argAudio = 1;
else {
printf("tu56: unknown argument %s\n", argv[firstArg]);
exit(1);
}
firstArg++;
}
glob.image = readpng("front.png");
glob.light1on = readpng("light1on.png");
glob.light2on = readpng("light2on.png");
glob.lightoff = readpng("lightoff.png");
glob.reelA[0] = readpng("reelA0.png");
glob.reelA[1] = readpng("reelA1.png");
glob.reelA[2] = readpng("reelA2.png");
glob.reelB[0] = readpng("reelB0.png");
glob.reelB[1] = readpng("reelB1.png");
glob.reelB[2] = readpng("reelB2.png");
glob.reelC[0] = readpng("reelC0.png");
glob.reelC[1] = readpng("reelC1.png");
glob.reelC[2] = readpng("reelC2.png");
glob.reelD[0] = readpng("reelD0.png");
glob.reelD[1] = readpng("reelD1.png");
glob.reelD[2] = readpng("reelD2.png");
glob.button[0] = readpng("button0.png");
glob.button[1] = readpng("button1.png");
glob.button[2] = readpng("button2.png");
glob.light[0] = 0;
glob.light[1] = 0;
glob.light[2] = 0;
glob.light[3] = 0;
glob.tape1 = 0;
glob.tape2 = 0;
glob.reelAindex = -1;
glob.reelCindex = -1;
glob.buttonstate[0] = 0;
glob.buttonstate[1] = 0;
glob.buttonstate[2] = 1;
glob.buttonstate[3] = 0;
glob.buttonstate[4] = 0;
glob.buttonstate[5] = 1;
glob.last_remote_status = 0;
glob.last_remote_status = 0;
glob.reelsound = 0;
glob.updated = 0;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
// set the background color
GdkColor color;
color.red = 0;
color.green = 0;
color.blue = 0;
gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &color);
darea = gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER (window), darea);
g_signal_connect(G_OBJECT(darea), "draw",
G_CALLBACK(on_draw_event), NULL);
g_signal_connect(window, "destroy",
G_CALLBACK (gtk_main_quit), NULL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
GdkScreen *screen = gtk_window_get_screen(GTK_WINDOW(window));
int screenWidth = gdk_screen_get_width(screen);
int screenHeight = gdk_screen_get_height(screen);
printf("Screen dimensions: %d x %d\n", screenWidth, screenHeight);
if (glob.argFullscreen) {
// DISPLAY UNDECORATED FULL SCREEN WINDOW
gtk_window_set_decorated(GTK_WINDOW(window), FALSE);
gtk_window_fullscreen(GTK_WINDOW(window));
gtk_window_set_keep_above(GTK_WINDOW(window), FALSE);
glob.scale = screenWidth / 1920.0;
}
else {
// DISPLAY DECORATED WINDOW
gtk_window_set_decorated(GTK_WINDOW(window), TRUE);
gtk_window_set_default_size(GTK_WINDOW(window), 960, 464);
glob.scale = 0.5;
}
gtk_window_set_title(GTK_WINDOW(window), "tu56");
g_signal_connect(G_OBJECT(window), "button-press-event", G_CALLBACK(on_button_click_event), NULL);
g_signal_connect(G_OBJECT(window), "button-release-event", G_CALLBACK(on_button_release_event), NULL);
g_signal_connect(G_OBJECT(window), "key_press_event", G_CALLBACK(on_key_press), NULL);
if (TIME_INTERVAL > 0) {
// Add timer event
// Register the timer and set time in mS.
// The timer_event() function is called repeatedly until it returns FALSE.
g_timeout_add(TIME_INTERVAL, (GSourceFunc) on_timer_event, (gpointer) window);
}
gtk_widget_show_all(window);
gtk_main();
cairo_surface_destroy(glob.image);
return 0;
}