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

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