tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / kern / amiga_gui.c
blobc4451d796cdf781d5e6f30cbf220401ea9bbc8cf
1 #include "conf.h"
3 #include <proto/intuition.h>
5 #include <devices/timer.h>
6 #include <dos/dos.h>
7 #include <dos/dosextens.h>
8 #include <dos/dostags.h>
9 #if !defined(__AROS__)
10 #include <emul/emulinterface.h>
11 #endif
12 #include <exec/libraries.h>
13 #include <intuition/intuition.h>
14 #include <libraries/miamipanel.h>
15 #include <proto/dos.h>
16 #include <proto/exec.h>
17 #include <proto/miamipanel.h>
18 #include <kern/amiga_dhcp.h>
19 #include <kern/amiga_gui.h>
20 #include <kern/amiga_log.h>
21 #include <sys/synch.h>
22 #include <stdio.h>
23 #include <syslog.h>
25 /* Panel preferences */
26 struct gui_cnf gui_cnf = {NULL};
27 LONG gui_show[6] = {TRUE, TRUE, TRUE, TRUE, TRUE, TRUE};
28 ULONG gui_refresh = 1;
29 long panelx = 50;
30 long panely = 50;
32 UBYTE GUI_Running = 0;
33 ULONG guimask = 0;
34 struct Library *MiamiPanelBase = NULL;
35 struct MsgPort *gui_msgport = NULL;
36 struct timerequest *gui_timerio = NULL;
37 ULONG InternalProc = 0;
39 extern struct ifnet *ifnet;
40 extern struct Task *AmiTCP_Task;
41 extern TEXT panels_path[];
43 STRPTR strings[] =
45 ">On",
46 ">Of",
47 ">Su",
48 "Onl",
49 "Off",
50 "Sus",
51 "Show",
52 "Hide",
53 "Quit",
54 "Onl",
55 "Off"
58 void SAVEDS gui_async_op(ULONG code, ULONG unit)
60 struct ifnet *ifp;
62 DGUI(log(LOG_DEBUG,"gui_async_op(%lu, %lu) called", code, unit);)
63 for (ifp = ifnet; ifp; ifp = ifp->if_next) {
64 if (ifp->if_index == unit) {
65 DGUI(log(LOG_DEBUG,"Found ifp = 0x%08lx", ifp);)
66 ifupdown(ifp, (code == MIAMIPANELV_CallBack_Code_UnitOffline));
69 Forbid();
70 InternalProc--;
73 #ifdef __MORPHOS__
74 #define getarg(x, y) (y)*x++
75 long callback_function(void)
77 long *m68k_stack = (ULONG *)REG_A7;
78 long code = m68k_stack[1];
79 long count = m68k_stack[2];
80 ULONG *args = (ULONG *)m68k_stack[3];
81 #else
82 #define getarg va_arg
83 long callback(long code, long count, va_list args)
85 #endif
86 ULONG nstr;
87 struct SysLogPacket *msg;
89 switch (code) {
90 case MIAMIPANELV_CallBack_Code_Localize:
91 nstr = getarg(args, ULONG) - 5000;
92 DGUI(KPrintF("Callback: localize(%lu), result: %s", nstr, strings[nstr]);)
93 /* TODO: full localization */
94 return (long)strings[nstr];
95 case MIAMIPANELV_CallBack_Code_UnitOnline:
96 case MIAMIPANELV_CallBack_Code_UnitOffline:
97 nstr = getarg(args, ULONG);
98 InternalProc++;
99 if (!(CreateNewProcTags(NP_Entry, (IPTR)&gui_async_op,
100 NP_Name, (IPTR)"AROSTCP interface control",
101 #ifdef __MORPHOS__
102 NP_CodeType, CODETYPE_PPC,
103 NP_PPC_Arg1, code,
104 NP_PPC_Arg2, nstr,
105 #else
106 /* TODO: Implement passing arguments to gui_async_op() */
107 #endif
108 TAG_DONE)))
109 InternalProc--;
110 break;
111 case MIAMIPANELV_CallBack_Code_ShowMainGUI:
112 /* TODO: Run configuration editor */
113 break;
114 case MIAMIPANELV_CallBack_Code_HideMainGUI:
115 /* In fact we don't have main GUI so we have nothing to hide here.
116 However many panels (MUI.MiamiPanel for example) have "Hide"
117 button and in order to make it usable we simply make it doing
118 the same as "Close panel" */
119 case MIAMIPANELV_CallBack_Code_ClosePanel:
120 if (msg = (struct SysLogPacket *)GetLogMsg(&logReplyPort)) {
121 msg->Level = LOG_GUIMSG | GUICMD_CLOSE;
122 PutMsg(logPort, (struct Message *)msg);
124 break;
125 case MIAMIPANELV_CallBack_Code_QuitMiami:
126 Signal(AmiTCP_Task, SIGBREAKF_CTRL_C);
127 break;
128 default:
129 DGUI(KPrintF("Bad callback code %ld from panel\n", code));
131 return 0;
134 #ifdef __MORPHOS__
135 struct EmulLibEntry callback =
137 TRAP_LIB, 0, (void (*)(void)) callback_function
139 #endif
141 TEXT panel_path[FILENAME_MAX];
143 void gui_open()
145 struct ifnet *ifp;
146 DGUI(long PanelVersion;)
147 long showflags = 0;
148 char ifname[IFNAMSIZ+2];
149 long ifstate;
150 char ifspeed[7];
151 int i;
153 #if defined(__AROS__)
154 D(bug("[AROSTCP](amiga_gui.c) gui_open()\n"));
155 #endif
157 if (GUI_Running) {
158 DGUI(KPrintF("GUI is already opened\n");)
159 return;
161 if (!gui_cnf.PanelName) {
162 DGUI(KPrintF("GUI is disabled\n");)
163 return;
165 gui_timerio = CreateIORequest(logPort, sizeof(struct timerequest));
166 if (gui_timerio) {
167 if (!OpenDevice("timer.device", UNIT_VBLANK, (struct IORequest *)gui_timerio, 0)) {
168 // NameFromLock(GetProgramDir(), panel_path, FILENAME_MAX);
169 AddPart(panel_path,"LIBS:", FILENAME_MAX);
170 AddPart(panel_path,gui_cnf.PanelName, FILENAME_MAX);
171 strcat(panel_path, ".MiamiPanel");
172 DGUI(KPrintF("Opening GUI: %s...\n", panel_path);)
173 #if defined(__AROS__)
174 D(bug("[AROSTCP](amiga_gui.c) gui_open: Attempting to use '%s'\n", panel_path));
175 #endif
176 MiamiPanelBase = OpenLibrary(panel_path, 0);
177 DGUI(KPrintF("Panel library opened, base = 0x%08lx\n", MiamiPanelBase);)
178 if (MiamiPanelBase) {
179 DGUI(PanelVersion = MiamiPanelGetVersion();)
180 DGUI(KPrintF("Panel API version: %lu\n", PanelVersion);)
181 MiamiPanelInhibitRefresh(TRUE);
182 DGUI(KPrintF("Panel inhibited\n");)
183 for (i=0; i<6; i++)
184 if (gui_show[i])
185 showflags |= 1<<i;
186 /* Screen is always default */
187 if (MiamiPanelInit((IPTR)&callback, (IPTR)&callback, showflags, gui_cnf.PanelFont, NULL, panelx, panely, (IPTR)&guimask)) {
188 DGUI(KPrintF("Panel initialized, signals = 0x%08lx\n", guimask);)
189 GUI_Running = 1;
190 for (ifp = ifnet; ifp; ifp = ifp->if_next) {
191 if (!(ifp->if_flags & IFF_LOOPBACK)) {
192 sprintf(ifname, "%s%d", ifp->if_name, ifp->if_unit);
193 DGUI(KPrintF("Adding interface %s, index %lu, baud rate %lu\n", ifname, ifp->if_index, ifp->if_baudrate);)
194 if (ifp->if_flags & IFF_UP) {
195 ifstate = MIAMIPANELV_AddInterface_State_Online;
196 if (ifp->if_baudrate > 1000000)
197 sprintf(ifspeed,"%luM", ifp->if_baudrate / 1000000);
198 else
199 sprintf(ifspeed,"%lu", ifp->if_baudrate);
200 } else {
201 ifstate = MIAMIPANELV_AddInterface_State_Offline;
202 ifspeed[0] = 0;
204 MiamiPanelAddInterface(ifp->if_index, ifname, ifstate, ifp->if_data.ifi_aros_ontime.tv_secs, ifspeed);
207 DGUI(KPrintF("Opening the panel...\n");)
208 MiamiPanelInhibitRefresh(FALSE);
209 DGUI(KPrintF("Panel opened\n");)
210 gui_timerio->tr_node.io_Command = TR_ADDREQUEST;
211 gui_timerio->tr_time.tv_secs = gui_refresh;
212 gui_timerio->tr_time.tv_micro = 0;
213 SendIO ((struct IORequest *)gui_timerio);
214 return;
216 CloseLibrary(MiamiPanelBase);
217 MiamiPanelBase = NULL;
219 CloseDevice((struct IORequest *)gui_timerio);
221 DeleteIORequest((struct IORequest *)gui_timerio);
222 gui_timerio = NULL;
226 void gui_close()
228 #if defined(__AROS__)
229 D(bug("[AROSTCP](amiga_gui.c) gui_close()\n"));
230 #endif
231 DGUI(KPrintF("Closing GUI...\n");)
232 if (gui_timerio) {
233 if (MiamiPanelBase) {
234 if (GUI_Running) {
235 DGUI(KPrintF("Cleaning up the panel\n");)
236 AbortIO((struct IORequest *)gui_timerio);
237 WaitIO((struct IORequest *)gui_timerio);
238 GUI_Running = 0;
239 guimask = 0;
240 MiamiPanelCleanup();
242 CloseLibrary(MiamiPanelBase);
243 MiamiPanelBase = NULL;
244 DGUI(KPrintF("Panel library closed\n");)
246 CloseDevice((struct IORequest *)gui_timerio);
247 DeleteIORequest((struct IORequest *)gui_timerio);
248 gui_timerio = NULL;
252 void gui_snapshot()
254 #if defined(__AROS__)
255 D(bug("[AROSTCP](amiga_gui.c) gui_snapshot()\n"));
256 #endif
257 /* TODO */
260 void gui_process_refresh()
262 struct ifnet *ifp;
263 struct timeval now;
264 unsigned long rate;
265 union {
266 unsigned long long q;
267 struct {
268 unsigned long hi;
269 unsigned long lo;
270 } l;
271 } total;
273 #if defined(__AROS__)
274 D(bug("[AROSTCP](amiga_gui.c) gui_process_refresh()\n"));
275 #endif
277 if (GUI_Running) {
278 WaitIO ((struct IORequest *)gui_timerio);
279 GetSysTime(&now);
280 MiamiPanelInterfaceReport(-1, 0, now.tv_secs, 0, 0);
281 for (ifp = ifnet; ifp; ifp = ifp->if_next) {
282 if ((ifp->if_flags & (IFF_LOOPBACK | IFF_UP)) == IFF_UP) {
283 total.q = ifp->if_ibytes + ifp->if_obytes;
284 rate = (total.q - ifp->if_data.ifi_aros_lasttotal)/gui_refresh;
285 ifp->if_data.ifi_aros_lasttotal = total.q;
286 MiamiPanelInterfaceReport(ifp->if_index, rate, now.tv_secs, total.l.hi, total.l.lo);
289 gui_timerio->tr_node.io_Command = TR_ADDREQUEST;
290 gui_timerio->tr_time.tv_secs = gui_refresh;
291 gui_timerio->tr_time.tv_micro = 0;
292 SendIO ((struct IORequest *)gui_timerio);
296 void gui_process_msg(struct SysLogPacket *msg)
298 struct ifnet *ifp;
299 long state;
300 char ifspeed[7];
302 #if defined(__AROS__)
303 D(bug("[AROSTCP](amiga_gui.c) gui_process_msg()\n"));
304 #endif
306 if (GUI_Running) {
307 switch (msg->Level & GUICMD_MASK) {
308 case GUICMD_SET_INTERFACE_STATE:
309 ifp = (struct ifnet *)msg->Time;
310 state = (long)msg->Tag;
311 DGUI(KPrintF("Setting interface state for %s%u: 0x%08lx\n", ifp->if_name, ifp->if_unit, state);)
312 MiamiPanelSetInterfaceState(ifp->if_index, state, ifp->if_data.ifi_aros_ontime.tv_secs);
313 DGUI(KPrintF("Interface state set\n");)
314 if (state & (MIAMIPANELV_AddInterface_State_Online | MIAMIPANELV_AddInterface_State_Offline)) {
315 if (state == MIAMIPANELV_AddInterface_State_Online) {
316 if (ifp->if_baudrate > 1000000)
317 sprintf(ifspeed,"%luM", ifp->if_baudrate / 1000000);
318 else
319 sprintf(ifspeed,"%lu", ifp->if_baudrate);
320 } else
321 ifspeed[0] = 0;
322 MiamiPanelSetInterfaceSpeed(ifp->if_index, ifspeed);
324 break;
325 case GUICMD_CLOSE:
326 gui_close();
327 break;
330 if ((msg->Level & GUICMD_MASK) == GUICMD_SET_INTERFACE_STATE) {
331 if (state & (MIAMIPANELV_AddInterface_State_Online | MIAMIPANELV_AddInterface_State_GoingOffline)) {
332 if ((state == MIAMIPANELV_AddInterface_State_Online) && (ifp->if_data.ifi_aros_usedhcp))
333 run_dhclient(ifp);
334 if (state == MIAMIPANELV_AddInterface_State_GoingOffline)
335 kill_dhclient(ifp);
340 void gui_set_interface_state(struct ifnet *ifp, long state)
342 struct SysLogPacket *msg;
344 #if defined(__AROS__)
345 D(bug("[AROSTCP](amiga_gui.c) gui_set_interface_state()\n"));
346 #endif
348 if (GUI_Running) {
349 if (msg = (struct SysLogPacket *)GetLogMsg(&logReplyPort)) {
350 msg->Level = LOG_GUIMSG | GUICMD_SET_INTERFACE_STATE;
351 msg->Time = (IPTR)ifp;
352 msg->Tag = (STRPTR)state;
353 PutMsg(logPort, (struct Message *)msg);
358 void error_request(STRPTR Text, ...)
360 struct EasyStruct es = {
361 sizeof(struct EasyStruct),
363 "AROSTCP error",
364 NULL,
365 "Ok"
368 es.es_TextFormat = Text;
369 AROS_SLOWSTACKFORMAT_PRE(Text);
370 EasyRequestArgs(NULL, &es, NULL, AROS_SLOWSTACKFORMAT_ARG(Text));
371 AROS_SLOWSTACKFORMAT_POST(Text);