Usermode/libc - Fix strchr and strrchr behavior
[tpg/acess2.git] / Usermode / Libraries / libaxwin4.so_src / wm.cpp
1 /*
2  * AxWin4 Interface Library
3  * - By John Hodge (thePowersGang)
4  *
5  * wm.cpp
6  * - Window Management
7  */
8 #include <axwin4/axwin.h>
9 #include "include/common.hpp"
10 #include <ipc_proto.hpp>
11 #include <vector>
12 #include <algorithm>
13 #include <mutex>
14 #include <cassert>
15
16 namespace AxWin {
17
18 static const int        MAX_WINDOW_ID = 16;
19 static ::std::mutex     glWindowList;
20 static ::std::vector<tAxWin4_Window*>   gWindowList;
21
22 extern "C" tAxWin4_Window *AxWin4_CreateWindow(const char *Name)
23 {
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)");
29         }
30         if( id == gWindowList.size() )
31         {
32                 gWindowList.push_back(nullptr);
33         }
34         assert(gWindowList[id] == nullptr);
35         
36         // Create window structure locally
37         tAxWin4_Window *ret = new tAxWin4_Window();
38         gWindowList[id] = ret;
39         ret->m_id = id;
40         ret->m_fd = -1;
41         ret->m_buffer = nullptr;
42         // Request creation of window
43         CSerialiser     message;
44         message.WriteU8(IPCMSG_CREATEWIN);
45         message.WriteU16(ret->m_id);
46         message.WriteString(Name);
47         ::AxWin::SendMessage(message);
48         return ret;
49 }
50
51 extern "C" void AxWin4_ShowWindow(tAxWin4_Window *Window, bool Show)
52 {
53         CSerialiser     message;
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);
59 }
60
61 extern "C" void AxWin4_SetWindowFlags(tAxWin4_Window *Window, unsigned int Flags)
62 {
63         CSerialiser     message;
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);
69 }
70
71 extern "C" void AxWin4_MoveWindow(tAxWin4_Window *Window, int X, int Y)
72 {
73         CSerialiser     message;
74         message.WriteU8(IPCMSG_SETWINATTR);
75         message.WriteU16(Window->m_id);
76         message.WriteU16(IPC_WINATTR_POSITION);
77         message.WriteS16(X);
78         message.WriteS16(Y);
79         ::AxWin::SendMessage(message);
80 }
81 extern "C" void AxWin4_ResizeWindow(tAxWin4_Window *Window, unsigned int W, unsigned int H)
82 {
83         CSerialiser     message;
84         message.WriteU8(IPCMSG_SETWINATTR);
85         message.WriteU16(Window->m_id);
86         message.WriteU16(IPC_WINATTR_DIMENSIONS);
87         message.WriteU16(W);
88         message.WriteU16(H);
89         ::AxWin::SendMessage(message);
90 }
91
92 extern "C" void AxWin4_SetTitle(tAxWin4_Window *Window, const char *Title)
93 {
94         CSerialiser     message;
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);
100 }
101
102 extern "C" void AxWin4_DamageRect(tAxWin4_Window *Window, unsigned int X, unsigned int Y, unsigned int W, unsigned int H)
103 {
104         CSerialiser     message;
105         message.WriteU8(IPCMSG_DAMAGERECT);
106         message.WriteU16(Window->m_id);
107         message.WriteU16(X);
108         message.WriteU16(Y);
109         message.WriteU16(W);
110         message.WriteU16(H);
111         ::AxWin::SendMessage(message);
112 }
113
114 extern "C" void *AxWin4_GetWindowBuffer(tAxWin4_Window *Window)
115 {
116         if( Window->m_fd == -1 )
117         {
118                 CSerialiser     req;
119                 req.WriteU8(IPCMSG_GETWINBUF);
120                 req.WriteU16(Window->m_id);
121                 
122                 CDeserialiser   response = GetSyncReply(req, IPCMSG_GETWINBUF);
123                 unsigned int rspwin = response.ReadU16();
124                 if( rspwin != Window->m_id )
125                 {
126                         _SysDebug("AxWin4_GetWindowBuffer: GETWINBUF reply for different window (%u != %u)", rspwin, Window->m_id);
127                         return NULL;
128                 }
129                 
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);
134                         return NULL;
135                 }
136                 
137                 _SysDebug("AxWin4_GetWindowBuffer: %llx = %i", handle, Window->m_fd);
138         }
139
140         if( !Window->m_buffer )
141         {
142                 size_t  size = 640*480*4;
143                 Window->m_buffer = _SysMMap(NULL, size, MMAP_PROT_WRITE, MMAP_MAP_SHARED, Window->m_fd, 0);
144         }
145
146         return Window->m_buffer;
147 }
148
149 };      // namespace AxWin
150

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