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

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