Usermode/AxWin3 - Workign on interface lib, adding basic UI app
[tpg/acess2.git] / Usermode / Libraries / libaxwin3.so_src / msg.c
1 /*
2  * AxWin3 Interface Library
3  * - By John Hodge (thePowersGang)
4  *
5  * msg.c
6  * - Message handling / IPC
7  */
8 #include <axwin3/axwin.h>
9 #include <acess/sys.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include "include/ipc.h"
13 #include "include/internal.h"
14
15 // === CONSTANTS ===
16 enum eConnectionType
17 {
18         CONNTYPE_SENDMESSAGE,
19         CONNTYPE_UDP,
20         CONNTYPE_TCP
21 };
22
23 // === GLOBALS ===
24 enum eConnectionType    giConnectionType;
25 int     giConnectionNum;        // FD or PID
26 char    gaAxWin3_int_UDPHeader[] = {5,16,0,0};  // Port 4101
27  int    giAxWin3_int_UDPHeaderLen = sizeof(gaAxWin3_int_UDPHeader);
28 const char      *gsAxWin3_int_ServerDesc;
29 tAxWin3_MessageCallback gAxWin3_MessageCallback;
30
31 // === CODE ===
32 void AxWin3_Connect(const char *ServerDesc)
33 {
34         if( !ServerDesc )
35         {
36                 ServerDesc = gsAxWin3_int_ServerDesc;
37         }
38         if( !ServerDesc )
39         {
40                 // TODO: Error out
41                 return ;
42         }
43         switch(ServerDesc[0])
44         {
45         case '1': case '2': case '3': case '4': case '5':
46         case '6': case '7': case '8': case '9': case '0':
47                 giConnectionType = CONNTYPE_SENDMESSAGE;
48                 giConnectionNum = atoi(ServerDesc);
49                 break;
50         case 'u':
51                 while(*ServerDesc && *ServerDesc != ':')        ServerDesc ++;
52                 ServerDesc ++;
53                 // TODO: Open socket and create UDP header
54                 break;
55         case 't':
56                 while(*ServerDesc && *ServerDesc != ':')        ServerDesc ++;
57                 ServerDesc ++;
58                 // TODO: Open socket
59                 break;
60         }
61 }
62
63 tAxWin3_MessageCallback AxWin3_SetMessageCallback(tAxWin3_MessageCallback Callback)
64 {
65         tAxWin3_MessageCallback old = gAxWin3_MessageCallback;
66         gAxWin3_MessageCallback = Callback;
67         return old;
68 }
69
70 tAxWin_IPCMessage *AxWin3_int_AllocateIPCMessage(tHWND Window, int Message, int Flags, int ExtraBytes)
71 {
72         tAxWin_IPCMessage       *ret;
73
74         ret = malloc( sizeof(tAxWin_IPCMessage) + ExtraBytes );
75         ret->Flags = Flags;
76         ret->ID = Message;
77         if(Window)
78                 ret->Window = Window->ServerID;
79         else
80                 ret->Window = -1;
81         ret->Size = ExtraBytes;
82         return ret;
83 }
84
85 void AxWin3_int_SendIPCMessage(tAxWin_IPCMessage *Msg)
86 {
87          int    size = sizeof(tAxWin_IPCMessage) + Msg->Size;
88         switch(giConnectionType)
89         {
90         case CONNTYPE_SENDMESSAGE:
91                 SysSendMessage(giConnectionNum, size, Msg);
92                 break;
93         case CONNTYPE_UDP: {
94                 // Create UDP header
95                 char    tmpbuf[giAxWin3_int_UDPHeaderLen + size];
96                 memcpy(tmpbuf, gaAxWin3_int_UDPHeader, giAxWin3_int_UDPHeaderLen);
97                 memcpy(tmpbuf + giAxWin3_int_UDPHeaderLen, Msg, size);
98                 write(giConnectionNum, tmpbuf, sizeof(tmpbuf));
99                 }
100                 break;
101         case CONNTYPE_TCP:
102                 write(giConnectionNum, Msg, size);
103                 break;
104         default:
105                 break;
106         }
107 }
108
109 tAxWin_IPCMessage *AxWin3_int_GetIPCMessage(void)
110 {
111          int    len;
112         tAxWin_IPCMessage       *ret = NULL;
113         switch(giConnectionType)
114         {
115         case CONNTYPE_SENDMESSAGE:
116                 while(SysGetMessage(NULL, NULL))
117                 {
118                         pid_t   tid;
119                         len = SysGetMessage(&tid, NULL);
120                         // Check if the message came from the server
121                         if(tid != giConnectionNum)
122                         {
123                                 // If not, pass the buck (or ignore)
124                                 if( gAxWin3_MessageCallback )
125                                         gAxWin3_MessageCallback(tid, len);
126                                 else
127                                         SysGetMessage(NULL, GETMSG_IGNORE);
128                                 continue ;
129                         }
130                         
131                         // If it's from the server, allocate a buffer and return it
132                         ret = malloc(len);
133                         if(ret == NULL) return NULL;
134                         SysGetMessage(NULL, ret);
135                         break;
136                 }
137                 break;
138         default:
139                 // TODO: Implement
140                 _SysDebug("TODO: Implement AxWin3_int_GetIPCMessage for TCP/UDP");
141                 break;
142         }
143
144         // No message?
145         if( ret == NULL )
146                 return NULL;
147
148         // TODO: Sanity checks, so a stupid server can't crash us
149
150         return ret;
151 }
152

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