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 _SysDebug("RegisterClient(&client=%p)", &client);
69 // allocate a client ID, and save
70 for( int i = 0; i < 100; i ++ )
72 uint16_t id = giNextClient++;
73 if(giNextClient == 0) giNextClient = 1;
74 auto r = glClients.insert( ::std::pair<uint16_t,CClient*>(id, &client) );
75 if( r.second == true )
81 // Wut? 100 attempts and fail!
82 assert(!"Todo - Better way of handling client ID reuse");
85 CClient* GetClientByID(uint16_t id)
87 auto it = glClients.find(id);
88 if(it == glClients.end()) {
89 //_SysDebug("Client %i not registered", id);
93 //_SysDebug("Client %i %i = %p", id, it->first, it->second);
98 void DeregisterClient(CClient& client)
100 glClients.erase( client.id() );
104 void SendMessage_NotifyDims(CClient& client, unsigned int WinID, unsigned int NewW, unsigned int NewH)
106 _SysDebug("TODO: IPC::SendMessage_NotifyDims");
108 void SendMessage_MouseButton(CClient& client, unsigned int WinID, unsigned int X, unsigned int Y, uint8_t Button, bool Pressed)
111 msg.WriteU8(IPCMSG_INPUTEVENT);
112 msg.WriteU8(IPC_INEV_MOUSEBTN);
117 msg.WriteU8(Pressed ? 0 : 1);
118 client.SendMessage(msg);
120 void SendMessage_MouseMove(CClient& client, unsigned int WinID, unsigned int X, unsigned int Y)
122 _SysDebug("TODO: IPC::SendMessage_MouseButton");
124 void SendMessage_KeyEvent(CClient& client, unsigned int WinID, uint32_t KeySym, bool Pressed, const char *Translated)
127 msg.WriteU8(IPCMSG_INPUTEVENT);
128 msg.WriteU8(IPC_INEV_KEYBOARD);
130 msg.WriteU16(KeySym);
131 msg.WriteU8(Pressed ? 0 : 1);
132 msg.WriteString(Translated);
133 client.SendMessage(msg);
137 void HandleMessage_Nop(CClient& client, CDeserialiser& message)
141 void HandleMessage_Reply(CClient& client, CDeserialiser& message)
143 // Reply to a sent message
144 // - Not many messages need server-bound replies
145 int orig_command = message.ReadU8();
149 // Ping reply, mark client as still responding
157 void HandleMessage_Ping(CClient& client, CDeserialiser& message)
159 // A client has asked for a ping, we pong them back
161 reply.WriteU8(IPCMSG_REPLY);
162 reply.WriteU8(IPCMSG_PING);
163 client.SendMessage(reply);
166 void HandleMessage_GetGlobalAttr(CClient& client, CDeserialiser& message)
168 uint16_t attr_id = message.ReadU16();
171 reply.WriteU8(IPCMSG_REPLY);
172 reply.WriteU8(IPCMSG_GETGLOBAL);
173 reply.WriteU16(attr_id);
177 case IPC_GLOBATTR_SCREENDIMS: {
178 uint8_t screen_id = message.ReadU8();
180 gpCompositor->GetScreenDims(screen_id, &w, &h);
181 reply.WriteU16( (w <= UINT16_MAX ? w : UINT16_MAX) );
182 reply.WriteU16( (h <= UINT16_MAX ? h : UINT16_MAX) );
184 case IPC_GLOBATTR_MAXAREA:
185 assert(!"TODO: IPC_GLOBATTR_MAXAREA");
188 throw IPC::CClientFailure("Bad global attribute ID");
191 client.SendMessage(reply);
194 void HandleMessage_SetGlobalAttr(CClient& client, CDeserialiser& message)
196 uint16_t attr_id = message.ReadU16();
200 case IPC_GLOBATTR_SCREENDIMS:
203 case IPC_GLOBATTR_MAXAREA:
204 assert(!"TODO: IPC_GLOBATTR_MAXAREA");
207 throw IPC::CClientFailure("Bad global attribute ID");
211 void HandleMessage_CreateWindow(CClient& client, CDeserialiser& message)
213 uint16_t new_id = message.ReadU16();
214 //uint16_t parent_id = message.ReadU16();
215 //CWindow* parent = client.GetWindow( parent_id );
216 ::std::string name = message.ReadString();
218 ::_SysDebug("_CreateWindow: (%i, '%s')", new_id, name.c_str());
219 client.SetWindow( new_id, new CWindow(*gpCompositor, client, name, new_id) );
222 void HandleMessage_DestroyWindow(CClient& client, CDeserialiser& message)
224 uint16_t win_id = message.ReadU16();
225 _SysDebug("_DestroyWindow: (%i)", win_id);
227 CWindow* win = client.GetWindow(win_id);
229 throw IPC::CClientFailure("_DestroyWindow: Bad window");
231 client.SetWindow(win_id, 0);
233 // TODO: Directly inform compositor?
237 void HandleMessage_SetWindowAttr(CClient& client, CDeserialiser& message)
239 uint16_t win_id = message.ReadU16();
240 uint16_t attr_id = message.ReadU16();
241 _SysDebug("_SetWindowAttr: (Win=%i, ID=%i)", win_id, attr_id);
243 CWindow* win = client.GetWindow(win_id);
245 throw IPC::CClientFailure("_SetWindowAttr - Bad window");
250 case IPC_WINATTR_DIMENSIONS: {
251 uint16_t new_w = message.ReadU16();
252 uint16_t new_h = message.ReadU16();
253 win->Resize(new_w, new_h);
255 case IPC_WINATTR_POSITION: {
256 int16_t new_x = message.ReadS16();
257 int16_t new_y = message.ReadS16();
258 win->Move(new_x, new_y);
260 case IPC_WINATTR_SHOW:
261 win->Show( message.ReadU8() != 0 );
263 case IPC_WINATTR_FLAGS:
264 win->SetFlags( message.ReadU8() ); // TODO: U8? why so small?
266 case IPC_WINATTR_TITLE:
267 assert(!"TODO: IPC_WINATTR_TITLE");
270 _SysDebug("HandleMessage_SetWindowAttr - Bad attr %u", attr_id);
271 throw IPC::CClientFailure("Bad window attr");
275 void HandleMessage_GetWindowAttr(CClient& client, CDeserialiser& message)
277 assert(!"TODO HandleMessage_GetWindowAttr");
280 void HandleMessage_SendIPC(CClient& client, CDeserialiser& message)
282 assert(!"TODO HandleMessage_SendIPC");
285 void HandleMessage_GetWindowBuffer(CClient& client, CDeserialiser& message)
287 uint16_t win_id = message.ReadU16();
288 _SysDebug("_GetWindowBuffer: (%i)", win_id);
290 CWindow* win = client.GetWindow(win_id);
292 throw IPC::CClientFailure("_PushData: Bad window");
295 uint64_t handle = win->m_surface.GetSHMHandle();
298 reply.WriteU8(IPCMSG_REPLY);
299 reply.WriteU8(IPCMSG_GETWINBUF);
300 reply.WriteU16(win_id);
301 reply.WriteU64(handle);
302 client.SendMessage(reply);
305 void HandleMessage_DamageRect(CClient& client, CDeserialiser& message)
307 uint16_t winid = message.ReadU16();
308 uint16_t x = message.ReadU16();
309 uint16_t y = message.ReadU16();
310 uint16_t w = message.ReadU16();
311 uint16_t h = message.ReadU16();
313 _SysDebug("_DamageRect: (%i %i,%i %ix%i)", winid, x, y, w, h);
315 CWindow* win = client.GetWindow(winid);
317 throw IPC::CClientFailure("_PushData: Bad window");
325 void HandleMessage_PushData(CClient& client, CDeserialiser& message)
327 uint16_t win_id = message.ReadU16();
328 uint16_t x = message.ReadU16();
329 uint16_t y = message.ReadU16();
330 uint16_t w = message.ReadU16();
331 uint16_t h = message.ReadU16();
332 _SysDebug("_PushData: (%i, (%i,%i) %ix%i)", win_id, x, y, w, h);
334 CWindow* win = client.GetWindow(win_id);
336 throw IPC::CClientFailure("_PushData: Bad window");
339 for( unsigned int row = 0; row < h; row ++ )
341 const ::std::vector<uint8_t> scanline_data = message.ReadBuffer();
342 if( scanline_data.size() != w * 4 ) {
343 _SysDebug("ERROR _PushData: Scanline buffer size mismatch (%i,%i)",
344 scanline_data.size(), w*4);
347 win->DrawScanline(y+row, x, w, scanline_data.data());
350 void HandleMessage_Blit(CClient& client, CDeserialiser& message)
352 assert(!"TODO HandleMessage_Blit");
354 void HandleMessage_DrawCtl(CClient& client, CDeserialiser& message)
356 uint16_t win_id = message.ReadU16();
357 uint16_t x = message.ReadU16();
358 uint16_t y = message.ReadU16();
359 uint16_t w = message.ReadU16();
360 uint16_t h = message.ReadU16();
361 uint16_t ctrl_id = message.ReadU16();
362 uint16_t frame = message.ReadU16();
363 _SysDebug("_DrawCtl: (%i, (%i,%i) %ix%i Ctl%i frame?=0x%04x)", win_id, x, y, w, h, ctrl_id, frame);
365 CWindow* win = client.GetWindow(win_id);
367 throw IPC::CClientFailure("_DrawCtl: Bad window");
370 const CControl* ctrl = CControl::GetByID(ctrl_id);
372 throw IPC::CClientFailure("_DrawCtl: Invalid control ID");
376 ctrl->Render(win->m_surface, area);
378 void HandleMessage_DrawText(CClient& client, CDeserialiser& message)
380 uint16_t win_id = message.ReadU16();
381 uint16_t x = message.ReadU16();
382 uint16_t y = message.ReadU16();
383 uint16_t w = message.ReadU16();
384 uint16_t h = message.ReadU16();
385 uint16_t font = message.ReadU16();
386 ::std::string str = message.ReadString();
387 _SysDebug("_DrawText: (%i (%i,%i) %ix%i Font%i \"%s\")", win_id, x, y, w, h, font, str.c_str());
389 CWindow* win = client.GetWindow(win_id);
391 throw IPC::CClientFailure("_DrawText: Bad window");
394 // 1. Get font from client structure
395 //CFont& font = client.GetFont(font_id);
398 //CRect area(x, y, w, h);
399 //font->Render(win->m_surface, area, str, h);
401 _SysDebug("TODO: HandleMessage_DrawText");
404 typedef void MessageHandler_op_t(CClient& client, CDeserialiser& message);
406 MessageHandler_op_t *message_handlers[] = {
407 [IPCMSG_NULL] = &HandleMessage_Nop,
408 [IPCMSG_REPLY] = &HandleMessage_Reply,
409 [IPCMSG_PING] = &HandleMessage_Ping,
410 [IPCMSG_GETGLOBAL] = &HandleMessage_GetGlobalAttr,
411 [IPCMSG_SETGLOBAL] = &HandleMessage_SetGlobalAttr,
413 [IPCMSG_CREATEWIN] = &HandleMessage_CreateWindow,
414 [IPCMSG_CLOSEWIN] = &HandleMessage_DestroyWindow,
415 [IPCMSG_SETWINATTR] = &HandleMessage_SetWindowAttr,
416 [IPCMSG_GETWINATTR] = &HandleMessage_GetWindowAttr,
417 [IPCMSG_SENDIPC] = &HandleMessage_SendIPC, // Use the GUI server for low-bandwith IPC
418 [IPCMSG_GETWINBUF] = &HandleMessage_GetWindowBuffer,
419 [IPCMSG_DAMAGERECT] = &HandleMessage_DamageRect,
420 [IPCMSG_PUSHDATA] = &HandleMessage_PushData, // to a window's buffer
421 [IPCMSG_BLIT] = &HandleMessage_Blit, // Copy data from one part of the window to another
422 [IPCMSG_DRAWCTL] = &HandleMessage_DrawCtl, // Draw a control
423 [IPCMSG_DRAWTEXT] = &HandleMessage_DrawText, // Draw text
426 void HandleMessage(CClient& client, CDeserialiser& message)
428 unsigned int command = message.ReadU8();
429 if( command >= sizeof(message_handlers)/sizeof(IPC::MessageHandler_op_t*) ) {
430 // Drop, invalid command
434 (message_handlers[command])(client, message);
437 CClientFailure::CClientFailure(std::string&& what):
441 const char *CClientFailure::what() const throw()
443 return m_what.c_str();
445 CClientFailure::~CClientFailure() throw()
451 IIPCChannel::~IIPCChannel()
455 }; // namespace AxWin