d8420710a83bae2b43a03bdf9dccf72b4e7662ba
[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 typedef struct sIPC_Client      tIPC_Client;
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
42 // === PROTOTYPES ===
43 void    IPC_Init(void);
44 void    IPC_FillSelect(int *nfds, fd_set *set);
45 void    IPC_HandleSelect(fd_set *set);
46  int    IPC_Type_Datagram_GetSize(const void *Ident);
47  int    IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2);
48 void    IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data);
49  int    IPC_Type_Sys_GetSize(const void *Ident);
50  int    IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2);
51 void    IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data);
52 void    IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg);
53
54 // === GLOBALS ===
55 const tIPC_Type gIPC_Type_Datagram = {
56         IPC_Type_Datagram_GetSize,
57         IPC_Type_Datagram_Compare, 
58         IPC_Type_Datagram_Send
59 };
60 const tIPC_Type gIPC_Type_SysMessage = {
61         IPC_Type_Sys_GetSize,
62         IPC_Type_Sys_Compare,
63         IPC_Type_Sys_Send
64 };
65  int    giNetworkFileHandle = -1;
66  int    giMessagesFileHandle = -1;
67  int    giIPC_ClientCount;
68 tIPC_Client     **gIPC_Clients;
69
70 // === CODE ===
71 void IPC_Init(void)
72 {
73          int    tmp;
74         // TODO: Check this
75         giNetworkFileHandle = open("/Devices/ip/loop/udp", OPENFLAG_READ);
76         tmp = AXWIN_PORT;       ioctl(giNetworkFileHandle, 4, &tmp);    // TODO: Don't hard-code IOCtl number
77 }
78
79 void IPC_FillSelect(int *nfds, fd_set *set)
80 {
81         if( giNetworkFileHandle > *nfds )       *nfds = giNetworkFileHandle;
82         FD_SET(giNetworkFileHandle, set);
83 }
84
85 void IPC_HandleSelect(fd_set *set)
86 {
87         if( FD_ISSET(giNetworkFileHandle, set) )
88         {
89                 char    staticBuf[STATICBUF_SIZE];
90                  int    readlen, identlen;
91                 char    *msg;
92
93                 readlen = read(giNetworkFileHandle, staticBuf, sizeof(staticBuf));
94                 
95                 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
96                 msg = staticBuf + identlen;
97
98                 IPC_Handle(&gIPC_Type_Datagram, staticBuf, readlen - identlen, (void*)msg);
99                 _SysDebug("IPC_HandleSelect: UDP handled");
100         }
101
102         while(SysGetMessage(NULL, NULL))
103         {
104                 pid_t   tid;
105                  int    len = SysGetMessage(&tid, NULL);
106                 char    data[len];
107                 SysGetMessage(NULL, data);
108
109                 IPC_Handle(&gIPC_Type_SysMessage, &tid, len, (void*)data);
110                 _SysDebug("IPC_HandleSelect: Message handled");
111         }
112 }
113
114 int IPC_Type_Datagram_GetSize(const void *Ident)
115 {
116         return 4 + Net_GetAddressSize( ((const uint16_t*)Ident)[1] );
117 }
118
119 int IPC_Type_Datagram_Compare(const void *Ident1, const void *Ident2)
120 {
121         // Pass the buck :)
122         // - No need to worry about mis-matching sizes, as the size is computed
123         //   from the 3rd/4th bytes, hence it will differ before the size is hit.
124         return memcmp(Ident1, Ident2, IPC_Type_Datagram_GetSize(Ident1));
125 }
126
127 void IPC_Type_Datagram_Send(const void *Ident, size_t Length, const void *Data)
128 {
129          int    identlen = IPC_Type_Datagram_GetSize(Ident);
130         char    tmpbuf[ identlen + Length ];
131         memcpy(tmpbuf, Ident, identlen);        // Header
132         memcpy(tmpbuf + identlen, Data, Length);        // Data
133         // TODO: Handle fragmented packets
134         write(giNetworkFileHandle, tmpbuf, sizeof(tmpbuf));
135 }
136
137 int IPC_Type_Sys_GetSize(const void *Ident)
138 {
139         return sizeof(pid_t);
140 }
141
142 int IPC_Type_Sys_Compare(const void *Ident1, const void *Ident2)
143 {
144         return *(const int*)Ident1 - *(const int*)Ident2;
145 }
146
147 void IPC_Type_Sys_Send(const void *Ident, size_t Length, const void *Data)
148 {
149         SysSendMessage( *(const tid_t*)Ident, Length, Data );
150 }
151
152 // --- Client -> Window Mappings
153 int _CompareClientPtrs(const void *_a, const void *_b)
154 {
155         tIPC_Client     *a = *(tIPC_Client**)_a;
156         tIPC_Client     *b = *(tIPC_Client**)_b;
157         
158         if(a->IPCType < b->IPCType)     return -1;
159         if(a->IPCType > b->IPCType)     return 1;
160         
161         return a->IPCType->CompareIdent(a->Ident, b->Ident);
162 }
163
164 tIPC_Client *IPC_int_GetClient(const tIPC_Type *IPCType, const void *Ident)
165 {
166          int    pos;    // Position where the new client will be inserted
167          int    ident_size;
168         tIPC_Client     *ret;
169
170         // - Search list of registered clients
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                         if(cmp == 0)    break;
187                         if(cmp < 0)
188                                 pos -= div;
189                         else
190                                 pos += div;
191                 }
192                 
193                 // - Return if found    
194                 if(cmp == 0)
195                         return gIPC_Clients[pos];
196         
197                 // Adjust pos to be the index where the new client will be placed
198                 if(cmp > 0)     pos ++;
199         }
200
201
202         // - Create a new client entry
203         ident_size = IPCType->GetIdentSize(Ident);
204         ret = malloc( sizeof(tIPC_Client) + ident_size );
205         if(!ret)        return NULL;
206         ret->IPCType = IPCType;
207         ret->Ident = &ret + 1;  // Get the end of the structure
208         memcpy( (void*)ret->Ident, Ident, ident_size );
209         ret->nWindows = 0;
210         ret->Windows = NULL;
211         
212         // TODO: Register some way of detecting the client disconnecting
213         //       > Wait on the thread / register with kernel somehow
214         //       > Sockets are easier, but UDP is harder. Might get rid of it
215
216         // - Insert
217         giIPC_ClientCount ++;
218         gIPC_Clients = realloc(gIPC_Clients, giIPC_ClientCount*sizeof(tIPC_Client*));
219         memmove(&gIPC_Clients[pos+1], &gIPC_Clients[pos], (giIPC_ClientCount-pos-1) * sizeof(tIPC_Client*));
220         gIPC_Clients[pos] = ret;
221
222         return ret;
223 }
224
225 tWindow *IPC_int_GetWindow(tIPC_Client *Client, uint32_t WindowID)
226 {
227         if( WindowID == -1 )
228                 return NULL;
229
230         if( WindowID >= Client->nWindows )
231                 return NULL;
232
233         return Client->Windows[WindowID];
234 }
235
236 void IPC_int_SetWindow(tIPC_Client *Client, uint32_t WindowID, tWindow *WindowPtr)
237 {
238         if( WindowID >= MAX_WINDOWS_PER_APP )
239                 return ;
240
241         if( WindowID >= Client->nWindows )
242         {
243                  int    oldCount = Client->nWindows;
244                 Client->nWindows = WindowID + 1;
245                 Client->Windows = realloc(Client->Windows, Client->nWindows*sizeof(tWindow*));
246                 memset( &Client->Windows[oldCount],  0, (Client->nWindows-oldCount)*sizeof(tWindow*) );
247         }
248         
249         Client->Windows[WindowID] = WindowPtr;
250 }
251
252 // --- IPC Message Handlers ---
253 int IPC_Msg_CreateWin(tIPC_Client *Client, tAxWin_IPCMessage *Msg)
254 {
255         tIPCMsg_CreateWin       *info = (void*)Msg->Data;
256         tWindow *newwin, *parent;
257
258         // - Sanity checks
259         //  > +1 is for NULL byte on string
260         if( Msg->Size < sizeof(tIPCMsg_CreateWin) + 1 )
261                 return -1;
262         if( info->Renderer[Msg->Size - sizeof(tIPCMsg_CreateWin)] != '\0' )
263                 return -1;
264         
265         // - Get the parent window ID
266         parent = IPC_int_GetWindow(Client, Msg->Window);
267
268         // Catch creating a window with an existing ID
269         if( IPC_int_GetWindow(Client, info->NewWinID) )
270                 return 1;
271
272         // - Create the new window, and save its pointer
273         newwin = WM_CreateWindow(parent, info->Flags, info->Renderer);
274         IPC_int_SetWindow(Client, info->NewWinID, newwin);
275
276         return 0;
277 }
278
279 void IPC_Handle(const tIPC_Type *IPCType, const void *Ident, size_t MsgLen, tAxWin_IPCMessage *Msg)
280 {
281         tIPC_Client     *client;
282         
283         _SysDebug("IPC_Handle: (IPCType=%p, Ident=%p, MsgLen=%i, Msg=%p)",
284                 IPCType, Ident, MsgLen, Msg);
285         
286         if( MsgLen < sizeof(tAxWin_IPCMessage) )
287                 return ;
288         if( MsgLen < sizeof(tAxWin_IPCMessage) + Msg->Size )
289                 return ;
290         
291         client = IPC_int_GetClient(IPCType, Ident);
292
293         switch((enum eAxWin_IPCMessageTypes) Msg->ID)
294         {
295         // --- Ping message (reset timeout and get server version)
296         case IPCMSG_PING:
297                 _SysDebug(" IPC_Handle: IPCMSG_PING");
298                 if( Msg->Size < 4 )     return;
299                 if( Msg->Flags & IPCMSG_FLAG_RETURN )
300                 {
301                         Msg->ID = IPCMSG_PING;
302                         Msg->Size = sizeof(tIPCMsg_Return);
303                         ((tIPCMsg_Return*)Msg->Data)->Value = AXWIN_VERSION;
304                         IPCType->SendMessage(Ident, sizeof(tIPCMsg_Return), Msg);
305                 }
306                 break;
307
308         // ---  Create window
309         case IPCMSG_CREATEWIN:
310                 _SysDebug(" IPC_Handle: IPCMSG_CREATEWIN");
311                 IPC_Msg_CreateWin(client, Msg);
312                 break;
313
314 //      case IPCMSG_SHOWWINDOW:
315
316         // --- Unknown message
317         default:
318                 fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, IPCType);
319                 _SysDebug("WARNING: Unknown message %i (%p)", Msg->ID, IPCType);
320                 break;
321         }
322 }
323

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