Merge branch 'master' of git://github.com/thepowersgang/acess2
[tpg/acess2.git] / Usermode / Libraries / libaxwin4.so_src / wm.cpp
index f023ea8..cae79c7 100644 (file)
@@ -11,6 +11,7 @@
 #include <vector>
 #include <algorithm>
 #include <mutex>
+#include <cassert>
 
 namespace AxWin {
 
@@ -22,7 +23,7 @@ extern "C" tAxWin4_Window *AxWin4_CreateWindow(const char *Name)
 {
        // Allocate a window ID
        ::std::lock_guard<std::mutex>   lock(glWindowList);
-       int id = ::std::find(gWindowList.begin(), gWindowList.end(), nullptr) - gWindowList.end();
+       int id = ::std::find(gWindowList.begin(), gWindowList.end(), nullptr) - gWindowList.begin();
        if( id >= MAX_WINDOW_ID ) {
                throw ::std::runtime_error("AxWin4_CreateWindow - Out of IDs (TODO: Better exception)");
        }
@@ -30,11 +31,14 @@ extern "C" tAxWin4_Window *AxWin4_CreateWindow(const char *Name)
        {
                gWindowList.push_back(nullptr);
        }
+       assert(gWindowList[id] == nullptr);
        
        // Create window structure locally
        tAxWin4_Window *ret = new tAxWin4_Window();
        gWindowList[id] = ret;
        ret->m_id = id;
+       ret->m_fd = -1;
+       ret->m_buffer = nullptr;
        // Request creation of window
        CSerialiser     message;
        message.WriteU8(IPCMSG_CREATEWIN);
@@ -95,13 +99,51 @@ extern "C" void AxWin4_SetTitle(tAxWin4_Window *Window, const char *Title)
        ::AxWin::SendMessage(message);
 }
 
+extern "C" void AxWin4_DamageRect(tAxWin4_Window *Window, unsigned int X, unsigned int Y, unsigned int W, unsigned int H)
+{
+       CSerialiser     message;
+       message.WriteU8(IPCMSG_DAMAGERECT);
+       message.WriteU16(Window->m_id);
+       message.WriteU16(X);
+       message.WriteU16(Y);
+       message.WriteU16(W);
+       message.WriteU16(H);
+       ::AxWin::SendMessage(message);
+}
+
 extern "C" void *AxWin4_GetWindowBuffer(tAxWin4_Window *Window)
 {
-       //if( !Window->m_buffer )
-       //{
-       //      // TODO: Support non-blocking operations        
-       //}
-       return NULL;
+       if( Window->m_fd == -1 )
+       {
+               CSerialiser     req;
+               req.WriteU8(IPCMSG_GETWINBUF);
+               req.WriteU16(Window->m_id);
+               
+               CDeserialiser   response = GetSyncReply(req, IPCMSG_GETWINBUF);
+               unsigned int rspwin = response.ReadU16();
+               if( rspwin != Window->m_id )
+               {
+                       _SysDebug("AxWin4_GetWindowBuffer: GETWINBUF reply for different window (%u != %u)", rspwin, Window->m_id);
+                       return NULL;
+               }
+               
+               uint64_t handle = response.ReadU64();
+               Window->m_fd = _SysUnMarshalFD(handle);
+               if( Window->m_fd == -1 ) {
+                       _SysDebug("AxWin4_GetWindowBuffer: Unable to unmarshal resultant FD (0x%llx)", handle);
+                       return NULL;
+               }
+               
+               _SysDebug("AxWin4_GetWindowBuffer: %llx = %i", handle, Window->m_fd);
+       }
+
+       if( !Window->m_buffer )
+       {
+               size_t  size = 640*480*4;
+               Window->m_buffer = _SysMMap(NULL, size, MMAP_PROT_WRITE, MMAP_MAP_SHARED, Window->m_fd, 0);
+       }
+
+       return Window->m_buffer;
 }
 
 };     // namespace AxWin

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