61ffc53042d94b4cb2dee1ae7aeb2ff15fe31a1a
[tpg/acess2.git] / Usermode / Applications / axwin2_src / WM / messages.c
1 /*
2  * Acess GUI (AxWin) Version 2
3  * By John Hodge (thePowersGang)
4  */
5 #include "common.h"
6 #include <acess/sys.h>
7 #include <net.h>
8 #include <axwin2/messages.h>
9 #include <string.h>
10
11 #define AXWIN_PORT      4101
12
13 #define STATICBUF_SIZE  64
14
15 // === TYPES ===
16
17 // === PROTOTYPES ===
18 void    IPC_Init(void);
19 void    IPC_FillSelect(int *nfds, fd_set *set);
20 void    IPC_HandleSelect(fd_set *set);
21 void    Messages_RespondDatagram(void *Ident, size_t Length, void *Data);
22 void    Messages_RespondIPC(void *Ident, size_t Length, void *Data);
23 void    Messages_Handle(size_t IdentLen, void *Ident, size_t MsgLen, tAxWin_Message *Msg, tMessages_Handle_Callback *Respond);
24
25 // === GLOBALS ===
26  int    giNetworkFileHandle = -1;
27  int    giMessagesFileHandle = -1;
28
29 // === CODE ===
30 void IPC_Init(void)
31 {
32          int    tmp;
33         // TODO: Check this
34         giNetworkFileHandle = open("/Devices/ip/loop/udp", OPENFLAG_READ);
35         tmp = AXWIN_PORT;       ioctl(giNetworkFileHandle, 4, &tmp);    // TODO: Don't hard-code IOCtl number
36 }
37
38 void IPC_FillSelect(int *nfds, fd_set *set)
39 {
40         if( giNetworkFileHandle > *nfds )       *nfds = giNetworkFileHandle;
41         FD_SET(giNetworkFileHandle, set);
42 }
43
44 void IPC_HandleSelect(fd_set *set)
45 {
46         if( FD_ISSET(giNetworkFileHandle, set) )
47         {
48                 char    staticBuf[STATICBUF_SIZE];
49                  int    readlen, identlen;
50                 char    *msg;
51
52                 readlen = read(giNetworkFileHandle, sizeof(staticBuf), staticBuf);
53                 
54                 // Assume that all connections are from localhost
55                 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
56                 msg = staticBuf + identlen;
57
58                 Messages_Handle(identlen, staticBuf, readlen - identlen, (void*)msg, Messages_RespondDatagram);
59         }
60
61         while(SysGetMessage(NULL, NULL))
62         {
63                 pid_t   tid;
64                  int    len = SysGetMessage(&tid, NULL);
65                 char    data[len];
66                 SysGetMessage(NULL, data);
67
68                 Messages_Handle(sizeof(tid), &tid, len, (void*)data, Messages_RespondIPC);
69         }
70 }
71
72 void Messages_RespondDatagram(void *Ident, size_t Length, void *Data)
73 {
74          int    addrSize = Net_GetAddressSize( ((uint16_t*)Ident)[1] );
75         char    tmpbuf[ 4 + addrSize + Length ];
76         memcpy(tmpbuf, Ident, 4 + addrSize);
77         memcpy(tmpbuf + 4 + addrSize, Data, Length);
78         // TODO: Handle fragmented packets
79         write(giNetworkFileHandle, sizeof(tmpbuf), tmpbuf);
80 }
81
82 void Messages_RespondIPC(void *Ident, size_t Length, void *Data)
83 {
84         SysSendMessage( *(tid_t*)Ident, Length, Data );
85 }
86
87 void Messages_Handle(size_t IdentLen, void *Ident, size_t MsgLen, tAxWin_Message *Msg, tMessages_Handle_Callback *Respond)
88 {
89         if( MsgLen < sizeof(tAxWin_Message) )
90                 return ;
91         if( MsgLen < sizeof(tAxWin_Message) + Msg->Size )
92                 return ;
93
94         switch(Msg->ID)
95         {
96         case MSG_SREQ_PING:
97                 if( MsgLen < sizeof(tAxWin_Message) + 4 )       return;
98                 Msg->ID = MSG_SRSP_VERSION;
99                 Msg->Size = 4;
100                 Msg->Data[0] = 0;
101                 Msg->Data[1] = 1;
102                 *(uint16_t*)&Msg->Data[2] = -1;
103                 Respond(Ident, sizeof(Msg->ID), Msg);
104                 break;
105
106         case MSG_SREQ_REGISTER:
107                 if( Msg->Data[Msg->Size-1] != '\0' ) {
108                         // Invalid message
109                         return ;
110                 }
111                 
112                 AxWin_RegisterClient(IdentLen, Ident, Respond, Msg->Data);
113
114                 break;
115
116         default:
117                 fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, Respond);
118                 _SysDebug("WARNING: Unknown message %i (%p)\n", Msg->ID, Respond);
119                 break;
120         }
121 }
122

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