2 * AxWin4 Interface Library
3 * - By John Hodge (thePowersGang)
8 #include <axwin4/axwin.h>
9 #include "include/common.hpp"
10 #include <ipc_proto.hpp>
18 static const int MAX_WINDOW_ID = 16;
19 static ::std::mutex glWindowList;
20 static ::std::vector<tAxWin4_Window*> gWindowList;
22 extern "C" tAxWin4_Window *AxWin4_CreateWindow(const char *Name)
24 // Allocate a window ID
25 ::std::lock_guard<std::mutex> lock(glWindowList);
26 int id = ::std::find(gWindowList.begin(), gWindowList.end(), nullptr) - gWindowList.begin();
27 if( id >= MAX_WINDOW_ID ) {
28 throw ::std::runtime_error("AxWin4_CreateWindow - Out of IDs (TODO: Better exception)");
30 if( id == gWindowList.size() )
32 gWindowList.push_back(nullptr);
34 assert(gWindowList[id] == nullptr);
36 // Create window structure locally
37 tAxWin4_Window *ret = new tAxWin4_Window();
38 gWindowList[id] = ret;
41 ret->m_buffer = nullptr;
42 // Request creation of window
44 message.WriteU8(IPCMSG_CREATEWIN);
45 message.WriteU16(ret->m_id);
46 message.WriteString(Name);
47 ::AxWin::SendMessage(message);
51 extern "C" void AxWin4_ShowWindow(tAxWin4_Window *Window, bool Show)
54 message.WriteU8(IPCMSG_SETWINATTR);
55 message.WriteU16(Window->m_id);
56 message.WriteU16(IPC_WINATTR_SHOW);
57 message.WriteU8( (Show ? 1 : 0) );
58 ::AxWin::SendMessage(message);
61 extern "C" void AxWin4_SetWindowFlags(tAxWin4_Window *Window, unsigned int Flags)
64 message.WriteU8(IPCMSG_SETWINATTR);
65 message.WriteU16(Window->m_id);
66 message.WriteU16(IPC_WINATTR_FLAGS);
67 message.WriteU8( Flags );
68 ::AxWin::SendMessage(message);
71 extern "C" void AxWin4_MoveWindow(tAxWin4_Window *Window, int X, int Y)
74 message.WriteU8(IPCMSG_SETWINATTR);
75 message.WriteU16(Window->m_id);
76 message.WriteU16(IPC_WINATTR_POSITION);
79 ::AxWin::SendMessage(message);
81 extern "C" void AxWin4_ResizeWindow(tAxWin4_Window *Window, unsigned int W, unsigned int H)
84 message.WriteU8(IPCMSG_SETWINATTR);
85 message.WriteU16(Window->m_id);
86 message.WriteU16(IPC_WINATTR_DIMENSIONS);
89 ::AxWin::SendMessage(message);
92 extern "C" void AxWin4_SetTitle(tAxWin4_Window *Window, const char *Title)
95 message.WriteU8(IPCMSG_SETWINATTR);
96 message.WriteU16(Window->m_id);
97 message.WriteU16(IPC_WINATTR_TITLE);
98 message.WriteString(Title);
99 ::AxWin::SendMessage(message);
102 extern "C" void AxWin4_DamageRect(tAxWin4_Window *Window, unsigned int X, unsigned int Y, unsigned int W, unsigned int H)
105 message.WriteU8(IPCMSG_DAMAGERECT);
106 message.WriteU16(Window->m_id);
111 ::AxWin::SendMessage(message);
114 extern "C" void *AxWin4_GetWindowBuffer(tAxWin4_Window *Window)
116 if( Window->m_fd == -1 )
119 req.WriteU8(IPCMSG_GETWINBUF);
120 req.WriteU16(Window->m_id);
122 CDeserialiser response = GetSyncReply(req, IPCMSG_GETWINBUF);
123 unsigned int rspwin = response.ReadU16();
124 if( rspwin != Window->m_id )
126 _SysDebug("AxWin4_GetWindowBuffer: GETWINBUF reply for different window (%u != %u)", rspwin, Window->m_id);
130 uint64_t handle = response.ReadU64();
131 Window->m_fd = _SysUnMarshalFD(handle);
132 if( Window->m_fd == -1 ) {
133 _SysDebug("AxWin4_GetWindowBuffer: Unable to unmarshal resultant FD (0x%llx)", handle);
137 _SysDebug("AxWin4_GetWindowBuffer: %llx = %i", handle, Window->m_fd);
140 if( !Window->m_buffer )
142 size_t size = 640*480*4;
143 Window->m_buffer = _SysMMap(NULL, size, MMAP_PROT_WRITE, MMAP_MAP_SHARED, Window->m_fd, 0);
146 return Window->m_buffer;
149 }; // namespace AxWin