3 * - By John Hodge (thePowersGang)
6 * - Client-Server communication (dispatch)
8 #define __STDC_LIMIT_MACROS
11 #include <IIPCChannel.hpp>
13 #include <CClient.hpp>
14 #include <serialisation.hpp>
15 #include <ipc_proto.hpp>
16 #include <CCompositor.hpp>
20 #include <CIPCChannel_AcessIPCPipe.hpp>
21 #include <draw_control.hpp>
26 CCompositor* gpCompositor;
27 ::std::list<IIPCChannel*> glChannels;
28 ::std::map<uint16_t,CClient*> glClients;
29 uint16_t giNextClient = 1;
31 void Initialise(const CConfigIPC& config, CCompositor& compositor)
33 gpCompositor = &compositor;
35 ::std::string pipe_basepath = "axwin4";
36 glChannels.push_back( new CIPCChannel_AcessIPCPipe( pipe_basepath ) );
38 //glChannels.push_back( new CIPCChannel_TCP("0.0.0.0:2100") );
40 //for( auto channel : config.m_channels )
42 // channels.push_back( );
46 int FillSelect(fd_set& rfds)
49 for( const auto channel : glChannels )
52 ret = ::std::max(ret, channel->FillSelect(rfds));
57 void HandleSelect(const fd_set& rfds)
59 for( const auto channel : glChannels )
62 channel->HandleSelect(rfds);
66 void RegisterClient(CClient& client)
68 // allocate a client ID, and save
69 for( int i = 0; i < 100; i ++ )
71 uint16_t id = giNextClient++;
72 if(giNextClient == 0) giNextClient = 1;
73 auto r = glClients.insert( ::std::pair<uint16_t,CClient*>(id, &client) );
74 if( r.second == true )
80 // Wut? 100 attempts and fail!
81 assert(!"Todo - Better way of handling client ID reuse");
84 CClient* GetClientByID(uint16_t id)
86 auto it = glClients.find(id);
87 return (it == glClients.end() ? nullptr : it->second);
90 void DeregisterClient(CClient& client)
92 glClients.erase( client.id() );
96 void SendMessage_NotifyDims(CClient& client, unsigned int NewW, unsigned int NewH)
98 _SysDebug("TODO: CClient::SendNotify_Dims");
102 void HandleMessage_Nop(CClient& client, CDeserialiser& message)
106 void HandleMessage_Reply(CClient& client, CDeserialiser& message)
108 // Reply to a sent message
109 // - Not many messages need server-bound replies
110 int orig_command = message.ReadU8();
114 // Ping reply, mark client as still responding
122 void HandleMessage_Ping(CClient& client, CDeserialiser& message)
124 // A client has asked for a ping, we pong them back
126 reply.WriteU8(IPCMSG_REPLY);
127 reply.WriteU8(IPCMSG_PING);
128 client.SendMessage(reply);
131 void HandleMessage_GetGlobalAttr(CClient& client, CDeserialiser& message)
133 uint16_t attr_id = message.ReadU16();
136 reply.WriteU8(IPCMSG_REPLY);
137 reply.WriteU8(IPCMSG_GETGLOBAL);
138 reply.WriteU16(attr_id);
142 case IPC_GLOBATTR_SCREENDIMS: {
143 uint8_t screen_id = message.ReadU8();
145 gpCompositor->GetScreenDims(screen_id, &w, &h);
146 reply.WriteU16( (w <= UINT16_MAX ? w : UINT16_MAX) );
147 reply.WriteU16( (h <= UINT16_MAX ? h : UINT16_MAX) );
149 case IPC_GLOBATTR_MAXAREA:
150 assert(!"TODO: IPC_GLOBATTR_MAXAREA");
153 throw IPC::CClientFailure("Bad global attribute ID");
156 client.SendMessage(reply);
159 void HandleMessage_SetGlobalAttr(CClient& client, CDeserialiser& message)
161 uint16_t attr_id = message.ReadU16();
165 case IPC_GLOBATTR_SCREENDIMS:
168 case IPC_GLOBATTR_MAXAREA:
169 assert(!"TODO: IPC_GLOBATTR_MAXAREA");
172 throw IPC::CClientFailure("Bad global attribute ID");
176 void HandleMessage_CreateWindow(CClient& client, CDeserialiser& message)
178 uint16_t new_id = message.ReadU16();
179 //uint16_t parent_id = message.ReadU16();
180 //CWindow* parent = client.GetWindow( parent_id );
181 ::std::string name = message.ReadString();
183 ::_SysDebug("_CreateWindow: (%i, '%s')", new_id, name.c_str());
184 client.SetWindow( new_id, new CWindow(*gpCompositor, client, name, new_id) );
187 void HandleMessage_DestroyWindow(CClient& client, CDeserialiser& message)
189 uint16_t win_id = message.ReadU16();
190 _SysDebug("_DestroyWindow: (%i)", win_id);
192 CWindow* win = client.GetWindow(win_id);
194 throw IPC::CClientFailure("_DestroyWindow: Bad window");
196 client.SetWindow(win_id, 0);
198 // TODO: Directly inform compositor?
202 void HandleMessage_SetWindowAttr(CClient& client, CDeserialiser& message)
204 uint16_t win_id = message.ReadU16();
205 uint16_t attr_id = message.ReadU16();
206 _SysDebug("_SetWindowAttr: (Win=%i, ID=%i)", win_id, attr_id);
208 CWindow* win = client.GetWindow(win_id);
210 throw IPC::CClientFailure("_SetWindowAttr - Bad window");
215 case IPC_WINATTR_DIMENSIONS: {
216 uint16_t new_w = message.ReadU16();
217 uint16_t new_h = message.ReadU16();
218 win->Resize(new_w, new_h);
220 case IPC_WINATTR_POSITION: {
221 int16_t new_x = message.ReadS16();
222 int16_t new_y = message.ReadS16();
223 win->Move(new_x, new_y);
225 case IPC_WINATTR_SHOW:
226 win->Show( message.ReadU8() != 0 );
228 case IPC_WINATTR_FLAGS:
229 win->SetFlags( message.ReadU8() ); // TODO: U8? why so small?
231 case IPC_WINATTR_TITLE:
232 assert(!"TODO: IPC_WINATTR_TITLE");
235 _SysDebug("HandleMessage_SetWindowAttr - Bad attr %u", attr_id);
236 throw IPC::CClientFailure("Bad window attr");
240 void HandleMessage_GetWindowAttr(CClient& client, CDeserialiser& message)
242 assert(!"TODO HandleMessage_GetWindowAttr");
245 void HandleMessage_SendIPC(CClient& client, CDeserialiser& message)
247 assert(!"TODO HandleMessage_SendIPC");
250 void HandleMessage_GetWindowBuffer(CClient& client, CDeserialiser& message)
252 uint16_t win_id = message.ReadU16();
253 _SysDebug("_GetWindowBuffer: (%i)", win_id);
255 CWindow* win = client.GetWindow(win_id);
257 throw IPC::CClientFailure("_PushData: Bad window");
260 uint64_t handle = win->m_surface.GetSHMHandle();
263 reply.WriteU8(IPCMSG_REPLY);
264 reply.WriteU8(IPCMSG_GETWINBUF);
265 reply.WriteU16(win_id);
266 reply.WriteU64(handle);
267 client.SendMessage(reply);
270 void HandleMessage_DamageRect(CClient& client, CDeserialiser& message)
272 uint16_t winid = message.ReadU16();
273 uint16_t x = message.ReadU16();
274 uint16_t y = message.ReadU16();
275 uint16_t w = message.ReadU16();
276 uint16_t h = message.ReadU16();
278 _SysDebug("_DamageRect: (%i %i,%i %ix%i)", winid, x, y, w, h);
280 CWindow* win = client.GetWindow(winid);
282 throw IPC::CClientFailure("_PushData: Bad window");
290 void HandleMessage_PushData(CClient& client, CDeserialiser& message)
292 uint16_t win_id = message.ReadU16();
293 uint16_t x = message.ReadU16();
294 uint16_t y = message.ReadU16();
295 uint16_t w = message.ReadU16();
296 uint16_t h = message.ReadU16();
297 _SysDebug("_PushData: (%i, (%i,%i) %ix%i)", win_id, x, y, w, h);
299 CWindow* win = client.GetWindow(win_id);
301 throw IPC::CClientFailure("_PushData: Bad window");
304 for( unsigned int row = 0; row < h; row ++ )
306 const ::std::vector<uint8_t> scanline_data = message.ReadBuffer();
307 if( scanline_data.size() != w * 4 ) {
308 _SysDebug("ERROR _PushData: Scanline buffer size mismatch (%i,%i)",
309 scanline_data.size(), w*4);
312 win->DrawScanline(y+row, x, w, scanline_data.data());
315 void HandleMessage_Blit(CClient& client, CDeserialiser& message)
317 assert(!"TODO HandleMessage_Blit");
319 void HandleMessage_DrawCtl(CClient& client, CDeserialiser& message)
321 uint16_t win_id = message.ReadU16();
322 uint16_t x = message.ReadU16();
323 uint16_t y = message.ReadU16();
324 uint16_t w = message.ReadU16();
325 uint16_t h = message.ReadU16();
326 uint16_t ctrl_id = message.ReadU16();
327 uint16_t frame = message.ReadU16();
328 _SysDebug("_DrawCtl: (%i, (%i,%i) %ix%i Ctl%i frame?=0x%04x)", win_id, x, y, w, h, ctrl_id, frame);
330 CWindow* win = client.GetWindow(win_id);
332 throw IPC::CClientFailure("_DrawCtl: Bad window");
335 const CControl* ctrl = CControl::GetByID(ctrl_id);
337 throw IPC::CClientFailure("_DrawCtl: Invalid control ID");
341 ctrl->Render(win->m_surface, area);
343 void HandleMessage_DrawText(CClient& client, CDeserialiser& message)
345 uint16_t win_id = message.ReadU16();
346 uint16_t x = message.ReadU16();
347 uint16_t y = message.ReadU16();
348 uint16_t w = message.ReadU16();
349 uint16_t h = message.ReadU16();
350 uint16_t font = message.ReadU16();
351 ::std::string str = message.ReadString();
352 _SysDebug("_DrawText: (%i (%i,%i) %ix%i Font%i \"%s\")", win_id, x, y, w, h, font, str.c_str());
354 CWindow* win = client.GetWindow(win_id);
356 throw IPC::CClientFailure("_DrawText: Bad window");
359 // 1. Get font from client structure
360 //CFont& font = client.GetFont(font_id);
363 //CRect area(x, y, w, h);
364 //font->Render(win->m_surface, area, str, h);
366 _SysDebug("TODO: HandleMessage_DrawText");
369 typedef void MessageHandler_op_t(CClient& client, CDeserialiser& message);
371 MessageHandler_op_t *message_handlers[] = {
372 [IPCMSG_NULL] = &HandleMessage_Nop,
373 [IPCMSG_REPLY] = &HandleMessage_Reply,
374 [IPCMSG_PING] = &HandleMessage_Ping,
375 [IPCMSG_GETGLOBAL] = &HandleMessage_GetGlobalAttr,
376 [IPCMSG_SETGLOBAL] = &HandleMessage_SetGlobalAttr,
378 [IPCMSG_CREATEWIN] = &HandleMessage_CreateWindow,
379 [IPCMSG_CLOSEWIN] = &HandleMessage_DestroyWindow,
380 [IPCMSG_SETWINATTR] = &HandleMessage_SetWindowAttr,
381 [IPCMSG_GETWINATTR] = &HandleMessage_GetWindowAttr,
382 [IPCMSG_SENDIPC] = &HandleMessage_SendIPC, // Use the GUI server for low-bandwith IPC
383 [IPCMSG_GETWINBUF] = &HandleMessage_GetWindowBuffer,
384 [IPCMSG_DAMAGERECT] = &HandleMessage_DamageRect,
385 [IPCMSG_PUSHDATA] = &HandleMessage_PushData, // to a window's buffer
386 [IPCMSG_BLIT] = &HandleMessage_Blit, // Copy data from one part of the window to another
387 [IPCMSG_DRAWCTL] = &HandleMessage_DrawCtl, // Draw a control
388 [IPCMSG_DRAWTEXT] = &HandleMessage_DrawText, // Draw text
391 void HandleMessage(CClient& client, CDeserialiser& message)
393 unsigned int command = message.ReadU8();
394 if( command >= sizeof(message_handlers)/sizeof(IPC::MessageHandler_op_t*) ) {
395 // Drop, invalid command
399 (message_handlers[command])(client, message);
402 CClientFailure::CClientFailure(std::string&& what):
406 const char *CClientFailure::what() const throw()
408 return m_what.c_str();
410 CClientFailure::~CClientFailure() throw()
416 IIPCChannel::~IIPCChannel()
420 }; // namespace AxWin