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

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