]> git.sesse.net Git - pistorm/blob - platforms/amiga/pistorm-dev/pistorm_dev_amiga/gui_interact.c
f698c900c5cc2355f1ed76235f0e172e9812fd63
[pistorm] / platforms / amiga / pistorm-dev / pistorm_dev_amiga / gui_interact.c
1 // SPDX-License-Identifier: MIT
2
3 #include <proto/intuition.h>
4 #include <proto/dos.h>
5 #include <intuition/intuition.h>
6 #include <proto/exec.h>
7 #include <proto/dos.h>
8 #include <workbench/startup.h>
9 #include <clib/expansion_protos.h>
10
11 #include "pistorm_dev.h"
12 #include "../pistorm-dev-enums.h"
13
14 #include <stdio.h>
15 #include <string.h>
16
17 extern unsigned int pistorm_base_addr;
18
19 #define VERSION "v0.1"
20
21 #define button1w 54
22 #define button1h 20
23
24 #define button2w 87
25 #define button2h 20
26
27 #define button3w 100
28 #define button3h 20
29
30 #define tbox1w 130
31 #define tbox1h 18
32
33 SHORT SharedBordersPairs0[] =
34 {
35     0, 0, 0, button1h - 1, 1, button1h - 2, 1, 0, button1w - 2, 0
36 };
37 SHORT SharedBordersPairs1[] =
38 {
39     1, button1h - 1, button1w - 2, button1h - 1, button1w - 2, 1, button1w - 1, 0, button1w - 1, button1h - 1
40 };
41
42 SHORT SharedBordersPairs2[] =
43 {
44     0, 0, 0, button3h - 1, 1, button3h - 2, 1, 0, button3w - 2, 0
45 };
46 SHORT SharedBordersPairs3[] =
47 {
48     1, button3h - 1, button3w - 2, button3h - 1, button3w - 2, 1, button3w - 1, 0, button3w - 1, button3h - 1
49 };
50
51 SHORT SharedBordersPairs4[] =
52 {
53     0, 0, 0, button2h - 1, 1, button2h - 2, 1, 0, button2w - 2, 0
54 };
55 SHORT SharedBordersPairs5[] =
56 {
57     1, button2h - 1, button2w - 2, button2h - 1, button2w - 2, 1, button2w - 1, 0, button2w - 1, button2h - 1
58 };
59
60 SHORT SharedBordersPairs6[] =
61 {
62     -2, -1, -2, tbox1h - 1, -1, tbox1h - 2, -1, -1, tbox1w - 2, -1
63 };
64
65 SHORT SharedBordersPairs7[] =
66 {
67     -1, tbox1h - 1, tbox1w - 2, tbox1h - 1, tbox1w - 2, 0, tbox1w - 1, -1, tbox1w - 1, tbox1h - 1
68 };
69
70 struct Border SharedBorders[] =
71 {
72     0, 0, 2, 0, JAM2, 5, (SHORT *) &SharedBordersPairs0[0], &SharedBorders[1], // Button 1
73     0, 0, 1, 0, JAM2, 5, (SHORT *) &SharedBordersPairs1[0], NULL,
74     0, 0, 2, 0, JAM2, 5, (SHORT *) &SharedBordersPairs2[0], &SharedBorders[3], // Button 3
75     0, 0, 1, 0, JAM2, 5, (SHORT *) &SharedBordersPairs3[0], NULL,
76     0, 0, 2, 0, JAM2, 5, (SHORT *) &SharedBordersPairs4[0], &SharedBorders[5], // Button 2
77     0, 0, 1, 0, JAM2, 5, (SHORT *) &SharedBordersPairs5[0], NULL,
78     0, 0, 1, 0, JAM2, 5, (SHORT *) &SharedBordersPairs6[0], &SharedBorders[7], // TBox
79     0, 0, 2, 0, JAM2, 5, (SHORT *) &SharedBordersPairs7[0], NULL,
80 };
81
82 struct Border SharedBordersInvert[] =
83 {
84     0, 0, 1, 0, JAM2, 5, (SHORT *) &SharedBordersPairs0[0], &SharedBordersInvert[1], // Button 1
85     0, 0, 2, 0, JAM2, 5, (SHORT *) &SharedBordersPairs1[0], NULL,
86     0, 0, 1, 0, JAM2, 5, (SHORT *) &SharedBordersPairs2[0], &SharedBordersInvert[3], // Button 3
87     0, 0, 2, 0, JAM2, 5, (SHORT *) &SharedBordersPairs3[0], NULL,
88     0, 0, 1, 0, JAM2, 5, (SHORT *) &SharedBordersPairs4[0], &SharedBordersInvert[5], // Button 2
89     0, 0, 2, 0, JAM2, 5, (SHORT *) &SharedBordersPairs5[0], NULL,
90 };
91
92 struct IntuiText ConfigDefault_text =
93 {
94     1, 0, JAM2, 2, 6, NULL, (UBYTE *)"Load Default", NULL
95 };
96
97 #define GADCONFIGDEFAULT 6
98
99 struct Gadget ConfigDefault =
100 {
101     NULL, 304, 39, button3w, button3h,
102     GADGHIMAGE,
103     RELVERIFY,
104     BOOLGADGET,
105     (APTR) &SharedBorders[2], (APTR) &SharedBordersInvert[2],
106     &ConfigDefault_text, 0, NULL, GADCONFIGDEFAULT, NULL
107 };
108
109 struct IntuiText ConfigCommit_text =
110 {
111     1, 0, JAM2, 2, 6, NULL, (UBYTE *)"Commit", NULL
112 };
113
114 #define GADCONFIGCOMMIT 5
115
116 struct Gadget ConfigCommit =
117 {
118     &ConfigDefault, 244, 39, button1w, button1h,
119     GADGHIMAGE,
120     RELVERIFY,
121     BOOLGADGET,
122     (APTR) &SharedBorders[0], (APTR) &SharedBordersInvert[0],
123     &ConfigCommit_text, 0, NULL, GADCONFIGCOMMIT, NULL
124 };
125
126
127 UBYTE ConfigFileValue_buf[255];
128
129 struct StringInfo ConfigFileValue =
130 {
131     ConfigFileValue_buf, NULL, 0, 255, 0, 0, 0, 0, 4, 4, NULL, 0, NULL
132 };
133
134 struct IntuiText ConfigFile_text =
135 {
136     1, 0, JAM2, -98, 4, NULL, "Config file:", NULL
137 };
138
139 #define GADCONFIGFILE 4
140
141 struct Gadget ConfigFile =
142 {
143     &ConfigCommit, 108, 41, tbox1w, tbox1h,
144     GADGHIMAGE,
145     0,
146     STRGADGET,
147     (APTR) &SharedBorders[6], NULL,
148     &ConfigFile_text, 0, (APTR)&ConfigFileValue, GADCONFIGFILE, NULL
149 };
150
151 UBYTE RTGStatus_buf[64] = "RTG status";
152
153 struct IntuiText RTGStatus_text =
154 {
155     1, 0, JAM2, 4, 4, NULL, (UBYTE *)RTGStatus_buf, NULL
156 };
157
158 #define GADRTGSTATUS 3
159
160 struct Gadget RTGStatus =
161 {
162     &ConfigFile, 14, 15, tbox1w, tbox1h,
163     GADGHIMAGE,
164     0,
165     BOOLGADGET,
166     (APTR) &SharedBorders[6], NULL,
167     &RTGStatus_text, 0, NULL, GADRTGSTATUS, NULL
168 };
169
170 UBYTE RTG_buf[64] = "RTG Enable";
171
172 struct IntuiText RTG_text =
173 {
174     1, 0, JAM2, 8, 6, NULL, (UBYTE *)RTG_buf, NULL
175 };
176
177 #define GADRTGBUTTON 2
178
179 struct Gadget RTGButton =
180 {
181     &RTGStatus, 150, 13, button3w, button3h,
182     GADGHIMAGE,
183     RELVERIFY,
184     BOOLGADGET,
185     (APTR) &SharedBorders[2], (APTR) &SharedBordersInvert[2],
186     &RTG_text, 0, NULL, GADRTGBUTTON, NULL
187 };
188
189 struct IntuiText AboutButton_text =
190 {
191     1, 0, JAM2, 8, 6, NULL, (UBYTE *)"About", NULL
192 };
193
194 #define GADABOUT 1
195
196 struct Gadget AboutButton =
197 {
198     &RTGButton, 356, 170, button1w, button1h,
199     GADGHIMAGE,
200     RELVERIFY,
201     BOOLGADGET,
202     (APTR) &SharedBorders[0], (APTR) &SharedBordersInvert[0],
203     &AboutButton_text, 0, NULL, GADABOUT, NULL
204 };
205
206
207 struct IntuiText QuitButton_text =
208 {
209     1, 0, JAM2, 12, 6, NULL, (UBYTE *)"Quit", NULL
210 };
211
212 #define GADQUIT 0
213
214 struct Gadget QuitButton =
215 {
216     &AboutButton, 438, 170, button1w, button1h,
217     GADGHIMAGE,
218     RELVERIFY,
219     BOOLGADGET,
220     (APTR) &SharedBorders[0], (APTR) &SharedBordersInvert[0],
221     &QuitButton_text, 0, NULL, GADQUIT, NULL
222 };
223
224
225 struct NewWindow winlayout =
226 {
227     20, 20,
228     512, 200,
229     -1, -1,
230     CLOSEWINDOW | GADGETUP | GADGETDOWN,
231     ACTIVATE | WINDOWCLOSE | WINDOWDRAG | WINDOWDEPTH,
232     &QuitButton, NULL,
233     (STRPTR)"PiStorm Interaction Tool",
234     NULL, NULL,
235     0, 0,
236     600, 400,
237     WBENCHSCREEN
238 };
239
240 // Pads what we are writing to screen with spaces, otherwise we get bits of
241 // old text still showing
242 static void WriteGadgetText(const char *text, UBYTE *buffer, struct Window *window, struct Gadget *gadget)
243 {
244     ULONG newlen = strlen(text);
245     ULONG oldlen = strlen((char *)buffer);
246
247     if (newlen < oldlen)
248     {
249         snprintf((char *)buffer, 64, "%s%*.*s", text, (int)(oldlen - newlen),
250                  (int)(oldlen - newlen), " ");
251     }
252     else
253     {
254         strncpy((char *)buffer, text, 64);
255     }
256
257     RefreshGadgets(gadget, window, NULL);
258 }
259 static void updateRTG(struct Window *window)
260 {
261     unsigned short rtg = pi_get_rtg_status();
262     if (rtg & 0x01)
263     {
264         WriteGadgetText("Disable RTG", RTG_buf, window, &RTGButton);
265         if (rtg & 0x02)
266         {
267             WriteGadgetText("RTG in use", RTGStatus_buf, window, &RTGStatus);
268         }
269         else
270         {
271             WriteGadgetText("RTG not in use", RTGStatus_buf, window, &RTGStatus);
272         }
273     }
274     else
275     {
276         WriteGadgetText("Enable RTG", RTG_buf, window, &RTGButton);
277         WriteGadgetText("RTG disabled", RTGStatus_buf, window, &RTGStatus);
278     }
279 }
280
281 int main()
282 {
283     struct Window *myWindow;
284
285     IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library", 0);
286     if (IntuitionBase == NULL)
287     {
288         return RETURN_FAIL;
289     }
290
291     pistorm_base_addr = pi_find_pistorm();
292     myWindow = OpenWindow(&winlayout);
293     BOOL no_board = FALSE;
294
295     if (pistorm_base_addr == 0xFFFFFFFF)
296     {
297         static struct IntuiText msg, pos;
298         msg.IText = "Unable to find PiStorm autoconf device.";
299         pos.IText = "OK";
300         AutoRequest(myWindow, &msg, NULL, &pos, 0, 0, 0, 0);
301         no_board = TRUE;
302     }
303     if (!no_board)
304     {
305         updateRTG(myWindow);
306     }
307
308     FOREVER
309     {
310         BOOL closewin = FALSE;
311         struct IntuiMessage *message;
312         Wait(1 << myWindow->UserPort->mp_SigBit);
313
314         while ((message = (struct IntuiMessage*)GetMsg(myWindow->UserPort)))
315         {
316             ULONG class = message->Class;
317             struct Gadget *address = (struct Gadget*)message->IAddress;
318             ReplyMsg((struct Message*)message);
319
320             if (class == CLOSEWINDOW)
321             {
322                 closewin = TRUE;
323             }
324             else if (class == GADGETUP)
325             {
326                 if (no_board && (address->GadgetID != GADQUIT) && (address->GadgetID != GADABOUT))
327                 {
328                     continue;
329                 }
330                 switch (address->GadgetID)
331                 {
332                     case GADQUIT:
333                         closewin = TRUE;
334                         break;
335                     case GADABOUT:
336                         {
337                             static struct IntuiText pos;
338                             UBYTE buf[64], buf2[64], buf3[64];
339                             if (!no_board)
340                             {
341                                 unsigned short hw_rev = pi_get_hw_rev();
342                                 unsigned short sw_rev = pi_get_sw_rev();
343                                 snprintf((char*)buf2, 64, "PiStorm hardware: %d.%d", (hw_rev >> 8), (hw_rev & 0xFF));
344                                 snprintf((char*)buf3, 64, "PiStorm software: %d.%d", (sw_rev >> 8), (sw_rev & 0xFF));
345                             }
346                             else
347                             {
348                                 snprintf((char*)buf2, 64, "PiStorm hardware not found!");
349                             }
350                             struct IntuiText msg[] =
351                             {
352                                 1, 0, JAM2, 0, 0, NULL, (UBYTE *)buf, &msg[1],
353                                 1, 0, JAM2, 0, 10, NULL, "Tool written by beeanyew and LinuxJedi", &msg[2],
354                                 1, 0, JAM2, 0, 20, NULL, (UBYTE*)buf2, &msg[3],
355                                 1, 0, JAM2, 0, 30, NULL, (UBYTE*)buf3, &msg[4],
356                                 1, 0, JAM2, 0, 50, NULL, "Now with 53% more Nibbles!", NULL,
357                             };
358                             snprintf(buf, 64, "PiStorm Interaction Tool %s", VERSION);
359                             pos.IText = "OK";
360                             AutoRequest(myWindow, msg, NULL, &pos, 0, 0, 0, 0);
361                             break;
362                         }
363                     case GADRTGBUTTON:
364                         {
365                             unsigned short rtgStatus = pi_get_rtg_status() & 0x01;
366                             pi_enable_rtg(!rtgStatus);
367                             updateRTG(myWindow);
368                             break;
369                         }
370                     case GADCONFIGCOMMIT:
371                         {
372                             unsigned short ret = pi_handle_config(PICFG_LOAD, ConfigFileValue_buf);
373                             if (ret == PI_RES_FILENOTFOUND)
374                             {
375                                 static struct IntuiText msg, pos;
376                                 msg.IText = "PiStorm says \"file not found\"";
377                                 pos.IText = "OK";
378                                 AutoRequest(myWindow, &msg, NULL, &pos, 0, 0, 0, 0);
379                             }
380                             break;
381                         }
382                     case GADCONFIGDEFAULT:
383                         {
384                             pi_handle_config(PICFG_DEFAULT, NULL);
385                             break;
386                         }
387                 }
388             }
389             if (closewin)
390             {
391                 break;
392             }
393         }
394         if (closewin)
395         {
396             break;
397         }
398     };
399     if (myWindow) CloseWindow(myWindow);
400     return 0;
401 }