Usermode/AxWin3 - Added menu render code (with hilights)
[tpg/acess2.git] / Usermode / Applications / axwin3_src / WM / ipc.c
1 /*
2  * Acess2 GUI (AxWin) Version 3
3  * - By John Hodge (thePowersGang)
4  * 
5  * ipc.c
6  * - Interprocess communication
7  */
8 #include <common.h>
9 #include <acess/sys.h>
10 #include <net.h>
11 #include <string.h>
12 #include <ipcmessages.h>
13 #include <stdio.h>
14 #include <wm.h>
15
16 #define AXWIN_PORT      4101
17
18 #define STATICBUF_SIZE  64
19 #define MAX_WINDOWS_PER_APP     128
20
21 // === TYPES ===
22 typedef struct sIPC_Type        tIPC_Type;
23
24 struct sIPC_Type
25 {
26          int    (*GetIdentSize)(const void *Ident);
27          int    (*CompareIdent)(const void *Ident1, const void *Ident2);
28         void    (*SendMessage)(const void *Ident, size_t Length, const void *Data);
29 };
30
31 struct sIPC_Client
32 {
33         const tIPC_Type *IPCType;
34         const void      *Ident; // Stored after structure
35
36          int    nWindows;
37         tWindow **Windows;
38 };
39
40
41 // === PROTOTYPES ===
42 void    IPC_Init(void);
43 void    IPC_FillSelect(int *nfds, fd_set *set);
44 void    IPC_HandleSelect(fd_set *set);
45  int    IPC_Type_Datagram_GetSize(const void *Ident);
46  int    IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2);
47 void    IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data);
48  int    IPC_Type_Sys_GetSize(const void *Ident);
49  int    IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2);
50 void    IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data);
51 void    IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg);
52
53 // === GLOBALS ===
54 const tIPC_Type gIPC_Type_Datagram = {
55         IPC_Type_Datagram_GetSize,
56         IPC_Type_Datagram_Compare, 
57         IPC_Type_Datagram_Send
58 };
59 const tIPC_Type gIPC_Type_SysMessage = {
60         IPC_Type_Sys_GetSize,
61         IPC_Type_Sys_Compare,
62         IPC_Type_Sys_Send
63 };
64  int    giNetworkFileHandle = -1;
65  int    giMessagesFileHandle = -1;
66  int    giIPC_ClientCount;
67 tIPC_Client     **gIPC_Clients;
68
69 // === CODE ===
70 void IPC_Init(void)
71 {
72          int    tmp;
73         // TODO: Check this
74         giNetworkFileHandle = open("/Devices/ip/loop/udp", OPENFLAG_READ);
75         tmp = AXWIN_PORT;       ioctl(giNetworkFileHandle, 4, &tmp);    // TODO: Don't hard-code IOCtl number
76 }
77
78 void IPC_FillSelect(int *nfds, fd_set *set)
79 {
80         if( giNetworkFileHandle > *nfds )       *nfds = giNetworkFileHandle;
81         FD_SET(giNetworkFileHandle, set);
82 }
83
84 void IPC_HandleSelect(fd_set *set)
85 {
86         if( FD_ISSET(giNetworkFileHandle, set) )
87         {
88                 char    staticBuf[STATICBUF_SIZE];
89                  int    readlen, identlen;
90                 char    *msg;
91
92                 readlen = read(giNetworkFileHandle, staticBuf, sizeof(staticBuf));
93                 
94                 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
95                 msg = staticBuf + identlen;
96
97                 IPC_Handle(&gIPC_Type_Datagram, staticBuf, readlen - identlen, (void*)msg);
98 //              _SysDebug("IPC_HandleSelect: UDP handled");
99         }
100
101         while(SysGetMessage(NULL, NULL))
102         {
103                 pid_t   tid;
104                  int    len = SysGetMessage(&tid, NULL);
105                 char    data[len];
106                 SysGetMessage(NULL, data);
107
108                 IPC_Handle(&gIPC_Type_SysMessage, &tid, len, (void*)data);
109 //              _SysDebug("IPC_HandleSelect: Message handled");
110         }
111 }
112
113 int IPC_Type_Datagram_GetSize(const void *Ident)
114 {
115         return 4 + Net_GetAddressSize( ((const uint16_t*)Ident)[1] );
116 }
117
118 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2)
119 {
120         // Pass the buck :)
121         // - No need to worry about mis-matching sizes, as the size is computed
122         //   from the 3rd/4th bytes, hence it will differ before the size is hit.
123         return memcmp(Ident1, Ident2, IPC_Type_Datagram_GetSize(Ident1));
124 }
125
126 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data)
127 {
128          int    identlen = IPC_Type_Datagram_GetSize(Ident);
129         char    tmpbuf[ identlen + Length ];
130         memcpy(tmpbuf, Ident, identlen);        // Header
131         memcpy(tmpbuf + identlen, Data, Length);        // Data
132         // TODO: Handle fragmented packets
133         write(giNetworkFileHandle, tmpbuf, sizeof(tmpbuf));
134 }
135
136 int IPC_Type_Sys_GetSize(const void *Ident)
137 {
138         return sizeof(pid_t);
139 }
140
141 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2)
142 {
143         return *(const tid_t*)Ident1 - *(const tid_t*)Ident2;
144 }
145
146 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data)
147 {
148         SysSendMessage( *(const tid_t*)Ident, Length, Data );
149 }
150
151 // --- Client -> Window Mappings
152 int _CompareClientPtrs(const void *_a, const void *_b)
153 {
154         tIPC_Client     *a = *(tIPC_Client**)_a;
155         tIPC_Client     *b = *(tIPC_Client**)_b;
156         
157         if(a->IPCType < b->IPCType)     return -1;
158         if(a->IPCType > b->IPCType)     return 1;
159         
160         return a->IPCType->CompareIdent(a->Ident, b->Ident);
161 }
162
163 tIPC_Client *IPC_int_GetClient(const tIPC_Type *IPCType, const void *Ident)
164 {
165          int    pos = 0;        // Position where the new client will be inserted
166          int    ident_size;
167         tIPC_Client     *ret;
168
169         // - Search list of registered clients
170         if(giIPC_ClientCount > 0)
171         {
172                 tIPC_Client     target;
173                  int    div;
174                  int    cmp = -1;
175         
176                 target.IPCType = IPCType;
177                 target.Ident = Ident;
178                 ret = &target;  // Abuse ret to get a pointer
179                 
180                 div = giIPC_ClientCount;
181                 pos = div/2;
182                 while(div > 0)
183                 {
184                         div /= 2;
185                         cmp = _CompareClientPtrs(&ret, &gIPC_Clients[pos]);
186 //                      _SysDebug("Checking against %i gives %i", pos, cmp);
187                         if(cmp == 0)    break;
188                         if(cmp < 0)
189                                 pos -= div;
190                         else
191                                 pos += div;
192                 }
193                 
194                 // - Return if found    
195                 if(cmp == 0)
196                         return gIPC_Clients[pos];
197         
198                 // Adjust pos to be the index where the new client will be placed
199                 if(cmp > 0)     pos ++;
200         }
201
202
203         // - Create a new client entry
204         ident_size = IPCType->GetIdentSize(Ident);
205 //      _SysDebug("ident_size = %i", ident_size);
206         ret = malloc( sizeof(tIPC_Client) + ident_size );
207         if(!ret)        return NULL;
208         ret->IPCType = IPCType;
209         ret->Ident = ret + 1;   // Get the end of the structure
210         memcpy( (void*)ret->Ident, Ident, ident_size );
211         ret->nWindows = 0;
212         ret->Windows = NULL;
213         
214         // TODO: Register some way of detecting the client disconnecting
215         //       > Wait on the thread / register with kernel somehow
216         //       > Sockets are easier, but UDP is harder. Might get rid of it
217
218         // - Insert
219         giIPC_ClientCount ++;
220         gIPC_Clients = realloc(gIPC_Clients, giIPC_ClientCount*sizeof(tIPC_Client*));
221         memmove(&gIPC_Clients[pos+1], &gIPC_Clients[pos], (giIPC_ClientCount-pos-1) * sizeof(tIPC_Client*));
222         gIPC_Clients[pos] = ret;
223
224         return ret;
225 }
226
227 tWindow *IPC_int_GetWindow(tIPC_Client *Client, uint32_t WindowID)
228 {
229         if( WindowID == -1 )
230                 return NULL;
231
232         if( WindowID >= Client->nWindows ) {
233 //              _SysDebug("Window %i out of range for %p (%i)", WindowID, Client, Client->nWindows);
234                 return NULL;
235         }
236
237         return Client->Windows[WindowID];
238 }
239
240 void IPC_int_SetWindow(tIPC_Client *Client, uint32_t WindowID, tWindow *WindowPtr)
241 {
242         if( WindowID >= MAX_WINDOWS_PER_APP )
243                 return ;
244
245         if( WindowID >= Client->nWindows )
246         {
247                  int    oldCount = Client->nWindows;
248                 Client->nWindows = WindowID + 1;
249                 Client->Windows = realloc(Client->Windows, Client->nWindows*sizeof(tWindow*));
250                 memset( &Client->Windows[oldCount],  0, (Client->nWindows-oldCount)*sizeof(tWindow*) );
251                 _SysDebug("Expanded %p's window list from %i to %i", Client, oldCount, Client->nWindows);
252         }
253
254         _SysDebug("Assigned %p to window %i for %p", WindowPtr, WindowID, Client);      
255         Client->Windows[WindowID] = WindowPtr;
256 }
257
258 // --- IPC Message Handlers ---
259 int IPC_Msg_SendMsg(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
260 {
261         tIPCMsg_SendMsg *info = (void*)Msg->Data;
262         tWindow *src, *dest;
263         
264         // - Sanity checks
265         if( Msg->Size < sizeof(tIPCMsg_SendMsg) )
266                 return -1;
267         if( Msg->Size < sizeof(tIPCMsg_SendMsg) + info->Length )
268                 return -1;
269         
270         src = IPC_int_GetWindow(Client, Msg->Window);
271         if(!src)        return 1;
272         dest = IPC_int_GetWindow(Client, info->Remote);
273         if(!dest)       return 1;
274
275         WM_SendMessage(src, dest, info->ID, info->Length, info->Data);  
276
277         return 0;
278 }
279
280 int IPC_Msg_CreateWin(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
281 {
282         tIPCMsg_CreateWin       *info = (void*)Msg->Data;
283         tWindow *newwin, *parent;
284
285         // - Sanity checks
286         //  > +1 is for NULL byte on string
287         if( Msg->Size < sizeof(*info) + 1 ) {
288                 _SysDebug("IPC_Msg_CreateWin: Size check 1 failed");
289                 return -1;
290         }
291         if( info->Renderer[Msg->Size - sizeof(*info) - 1] != '\0' ) {
292                 _SysDebug("IPC_Msg_CreateWin: Size check 2 failed");
293                 _SysDebug("info = {");
294                 _SysDebug("  .NewWinID = %i", info->NewWinID);
295                 _SysDebug("  .RendererArg = %i", info->RendererArg);
296                 _SysDebug("  .Renderer = '%.*s'", Msg->Size - sizeof(*info), info->Renderer);
297                 _SysDebug("}");
298                 return -1;
299         }
300         
301         // - Get the parent window ID
302         parent = IPC_int_GetWindow(Client, Msg->Window);
303
304         // Catch creating a window with an existing ID
305         if( IPC_int_GetWindow(Client, info->NewWinID) )
306                 return 1;
307
308         // - Create the new window, and save its pointer
309         newwin = WM_CreateWindow(parent, Client, info->NewWinID, info->RendererArg, info->Renderer);
310         IPC_int_SetWindow(Client, info->NewWinID, newwin);
311
312         return 0;
313 }
314
315 int IPC_Msg_ShowWindow(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
316 {
317         tIPCMsg_ShowWindow      *info = (void*)Msg->Data;
318         tWindow *win;
319         
320         if( Msg->Size < sizeof(*info) ) return -1;
321         
322         win = IPC_int_GetWindow(Client, Msg->Window);
323         if(!win)        return 1;
324
325         WM_ShowWindow(win, !!info->bShow);
326         
327         return 0;
328 }
329
330 int IPC_Msg_SetWinPos(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
331 {
332         tIPCMsg_SetWindowPos    *info = (void*)Msg->Data;
333         tWindow *win;
334         
335         if(Msg->Size < sizeof(*info))   return -1;
336         
337         win = IPC_int_GetWindow(Client, Msg->Window);
338         if(!win)        return 1;
339         
340         _SysDebug("info = {..., bSetPos=%i,bSetDims=%i}", info->bSetPos, info->bSetDims);
341         
342         if(info->bSetPos)
343                 WM_MoveWindow(win, info->X, info->Y);
344         if(info->bSetDims)
345                 WM_ResizeWindow(win, info->W, info->H);
346         
347         return 0;
348 }
349
350 int IPC_Msg_GetDisplayCount(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
351 {
352         tAxWin_IPCMessage       *ret_hdr;
353         tIPCMsg_ReturnInt       *ret;
354         char    buf[sizeof(*ret_hdr)+sizeof(*ret)];
355         
356         if( !(Msg->Flags & IPCMSG_FLAG_RETURN) )        return 0;
357         
358         ret_hdr = (void*)buf;
359         ret_hdr->ID = IPCMSG_GETDISPLAYCOUNT;
360         ret_hdr->Flags = 0;
361         ret_hdr->Window = -1;
362         ret_hdr->Size = sizeof(*ret);
363         ret = (void*)ret_hdr->Data;
364         ret->Value = 1; // HARD CODE - Current version only supports one display
365         
366         Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
367         return 0;
368 }
369
370 int IPC_Msg_GetDisplayDims(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
371 {
372         tIPCMsg_GetDisplayDims  *info;
373         tAxWin_IPCMessage       *ret_hdr;
374         tIPCMsg_RetDisplayDims  *ret;
375         char    buf[sizeof(*ret_hdr)+sizeof(*ret)];
376         
377         if( !(Msg->Flags & IPCMSG_FLAG_RETURN) )        return 0;
378
379         info = (void*)Msg->Data;        
380
381         ret_hdr = (void*)buf;
382         ret_hdr->ID = IPCMSG_GETDISPLAYDIMS;
383         ret_hdr->Flags = 0;
384         ret_hdr->Window = -1;
385         ret_hdr->Size = sizeof(*ret);
386         ret = (void*)ret_hdr->Data;
387         
388         // HARD CODE! Only one display supported
389         if( info->DisplayID == 0 )
390         {
391                 ret->X = 0;
392                 ret->Y = 0;
393                 ret->W = giScreenWidth;
394                 ret->H = giScreenHeight;
395         }
396         else
397         {
398                 ret->X = 0;     ret->Y = 0;
399                 ret->W = 0;     ret->H = 0;
400         }
401         
402         Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
403         return 0;
404 }
405
406 void IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg)
407 {
408         tIPC_Client     *client;
409          int    rv = 0;
410         
411         _SysDebug("IPC_Handle: (IPCType=%p, Ident=%p, MsgLen=%i, Msg=%p)",
412                 IPCType, Ident, MsgLen, Msg);
413         
414         if( MsgLen < sizeof(tAxWin_IPCMessage) )
415                 return ;
416         if( MsgLen < sizeof(tAxWin_IPCMessage) + Msg->Size )
417                 return ;
418         
419         client = IPC_int_GetClient(IPCType, Ident);
420
421         switch((enum eAxWin_IPCMessageTypes) Msg->ID)
422         {
423         // --- Ping message (reset timeout and get server version)
424         case IPCMSG_PING:
425                 _SysDebug(" IPC_Handle: IPCMSG_PING");
426                 if( Msg->Size < 4 )     return;
427                 if( Msg->Flags & IPCMSG_FLAG_RETURN )
428                 {
429                         tIPCMsg_ReturnInt       *ret = (void*)Msg->Data;
430                         Msg->ID = IPCMSG_PING;
431                         Msg->Size = sizeof(*ret);
432                         ret->Value = AXWIN_VERSION;
433                         IPCType->SendMessage(Ident, sizeof(*Msg)+sizeof(*ret), Msg);
434                 }
435                 break;
436
437         // -- Get display count
438         case IPCMSG_GETDISPLAYCOUNT:
439                 rv = IPC_Msg_GetDisplayCount(client, Msg);
440                 break;
441         
442         // --- Get display dimensions
443         case IPCMSG_GETDISPLAYDIMS:
444                 rv = IPC_Msg_GetDisplayDims(client, Msg);
445                 break;
446
447         // --- Send a message
448         case IPCMSG_SENDMSG:
449                 _SysDebug(" IPC_Handle: IPCMSG_SENDMSG %i", ((tIPCMsg_SendMsg*)Msg->Data)->ID);
450                 rv = IPC_Msg_SendMsg(client, Msg);
451                 break;
452
453         // --- Create window
454         case IPCMSG_CREATEWIN:
455                 _SysDebug(" IPC_Handle: IPCMSG_CREATEWIN");
456                 rv = IPC_Msg_CreateWin(client, Msg);
457                 break;
458         // --- Show/Hide a window
459         case IPCMSG_SHOWWINDOW:
460                 _SysDebug(" IPC_Handle: IPCMSG_SHOWWINDOW");
461                 rv = IPC_Msg_ShowWindow(client, Msg);
462                 break;
463         // --- Move/Resize a window
464         case IPCMSG_SETWINPOS:
465                 _SysDebug(" IPC_Handle: IPCMSG_SETWINPOS");
466                 rv = IPC_Msg_SetWinPos(client, Msg);
467                 break;
468
469         // --- Unknown message
470         default:
471                 fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, IPCType);
472                 _SysDebug("WARNING: Unknown message %i (%p)", Msg->ID, IPCType);
473                 break;
474         }
475         if(rv)
476                 _SysDebug("IPC_Handle: rv = %i", rv);
477 }
478
479 // --- Server->Client replies
480 void IPC_SendWMMessage(tIPC_Client *Client, uint32_t Src, uint32_t Dst, int MsgID, int Len, void *Data)
481 {
482         tAxWin_IPCMessage       *hdr;
483         tIPCMsg_SendMsg         *msg;
484         char    buf[sizeof(*hdr)+sizeof(*msg)+Len];
485         
486         hdr = (void*)buf;
487         msg = (void*)hdr->Data;
488         
489         hdr->ID = IPCMSG_SENDMSG;
490         hdr->Flags = 0;
491         hdr->Size = sizeof(*msg) + Len;
492         hdr->Window = Dst;
493         
494         msg->Remote = Src;
495         msg->ID = MsgID;
496         msg->Length = Len;
497         memcpy(msg->Data, Data, Len);
498         
499         Client->IPCType->SendMessage(Client->Ident, sizeof(buf), buf);
500 }
501

UCC git Repository :: git.ucc.asn.au