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

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