AxWin2 - Working in UDP based IPC, select() for input
[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 <axwin/messages.h>
9 //#include <select.h>
10
11 #define AXWIN_PORT      4101
12
13 #define STATICBUF_SIZE  64
14
15 // === TYPES ===
16 typedef void tMessages_Handle_Callback(void*, size_t, void*);
17
18 // === PROTOTYPES ===
19 void    Messages_PollIPC();
20 void    Messages_RespondIPC(void *Ident, size_t Length, void *Data);
21 void    Messages_Handle(void *Ident, int MsgLen, tAxWin_Message *Msg, tMessages_Handle_Callback *Respond);
22
23 // === GLOBALS ===
24  int    giIPCFileHandle;
25
26 // === CODE ===
27 void IPC_Init(void)
28 {
29          int    tmp;
30         // TODO: Check this
31         giIPCFileHandle = open("/Devices/ip/loop/udp", OPENFLAG_READ);
32         tmp = AXWIN_PORT;       ioctl(giIPCFileHandle, 4, &tmp);        // TODO: Don't hard-code IOCtl number
33 }
34
35 void IPC_FillSelect(int *nfds, fd_set *set)
36 {
37         if( giIPCFileHandle > *nfds )   *nfds = giIPCFileHandle;
38         FD_SET(giIPCFileHandle, set);
39 }
40
41 void IPC_HandleSelect(fd_set *set)
42 {
43         if( FD_ISSET(giIPCFileHandle, set) )
44         {
45                 char    staticBuf[STATICBUF_SIZE];
46                  int    readlen, identlen;
47                 char    *msg;
48                 readlen = read(giIPCFileHandle, sizeof(staticBuf), staticBuf);
49                 
50                 // Assume that all connections are from localhost
51                 identlen = 4 + Net_GetAddressSize( ((uint16_t*)staticBuf)[1] );
52                 msg = staticBuf + identlen;
53
54                 Messages_Handle(staticBuf, readlen - identlen, (void*)msg, Messages_RespondIPC);
55         }
56 }
57
58 #if 0
59 void Messages_PollIPC()
60 {
61          int    len;
62         pid_t   tid = 0;
63         char    staticBuf[STATICBUF_SIZE];
64         tAxWin_Message  *msg;
65         
66         // Wait for a message
67         while( (len = SysGetMessage(&tid, NULL)) == 0 )
68                 sleep();
69         
70         // Allocate the space for it
71         if( len <= STATICBUF_SIZE )
72                 msg = (void*)staticBuf;
73         else {
74                 msg = malloc( len );
75                 if(!msg) {
76                         fprintf(
77                                 stderr,
78                                 "ERROR - Unable to allocate message buffer, ignoring message from %i\n",
79                                 tid);
80                         SysGetMessage(NULL, GETMSG_IGNORE);
81                         return ;
82                 }
83         }
84         
85         // Get message data
86         SysGetMessage(NULL, msg);
87         
88         Messages_Handle(msg, Messages_RespondIPC, tid);
89 }
90 #endif
91
92 void Messages_RespondIPC(void *Ident, size_t Length, void *Data)
93 {
94         SysSendMessage( *(tid_t*)Ident, Length, Data );
95 }
96
97 void Messages_Handle(void *Ident, int MsgLen, tAxWin_Message *Msg, tMessages_Handle_Callback *Respond)
98 {
99         switch(Msg->ID)
100         {
101         #if 0
102         case MSG_SREQ_PING:
103                 Msg->ID = MSG_SRSP_VERSION;
104                 Msg->Size = 2;
105                 Msg->Data[0] = 0;
106                 Msg->Data[1] = 1;
107                 *(uint16_t*)&Msg->Data[2] = -1;
108                 Messages_RespondIPC(ID, sizeof(Msg->ID), Msg);
109                 break;
110         #endif
111         default:
112                 fprintf(stderr, "WARNING: Unknown message %i (%p)\n", Msg->ID, Respond);
113                 _SysDebug("WARNING: Unknown message %i (%p)\n", Msg->ID, Respond);
114                 break;
115         }
116 }
117

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