Usermode/AxWin3 - Splitting widget types out into separate files
[tpg/acess2.git] / Usermode / Applications / axwin3_src / WM / wm.c
1 /*
2  * Acess2 Window Manager v3
3  * - By John Hodge (thePowersGang)
4  *
5  * wm.c
6  * - Window manager core
7  */
8 #include <common.h>
9 #include <wm_renderer.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <video.h>
13 #include <wm_messages.h>
14 #include <decorator.h>
15
16 // === IMPORTS ===
17 extern void     IPC_SendWMMessage(tIPC_Client *Client, uint32_t Src, uint32_t Dst, int Msg, int Len, const void *Data);
18
19 // === GLOBALS ===
20 tWMRenderer     *gpWM_Renderers;
21 tWindow *gpWM_RootWindow;
22 //! Window which will recieve the next keyboard event
23 tWindow *gpWM_FocusedWindow;
24
25 // === CODE ===
26 void WM_Initialise(void)
27 {
28         WM_CreateWindow(NULL, NULL, 0, 0x0088FF, "Background");
29         gpWM_RootWindow->W = giScreenWidth;
30         gpWM_RootWindow->H = giScreenHeight;
31         gpWM_RootWindow->Flags = WINFLAG_SHOW|WINFLAG_NODECORATE;
32 }
33
34 void WM_RegisterRenderer(tWMRenderer *Renderer)
35 {
36         // TODO: Catch out duplicates
37         Renderer->Next = gpWM_Renderers;
38         gpWM_Renderers = Renderer;
39 }
40
41 // --- Manipulation
42 tWindow *WM_CreateWindow(tWindow *Parent, tIPC_Client *Client, uint32_t ID, int RendererArg, const char *RendererName)
43 {
44         tWMRenderer     *renderer;
45         tWindow *ret;
46         
47         // - Get Renderer
48         for( renderer = gpWM_Renderers; renderer; renderer = renderer->Next )
49         {
50                 if(strcmp(RendererName, renderer->Name) == 0)
51                         break;
52         }
53         if(renderer == NULL)
54                 return NULL;
55
56         if(!Parent)
57                 Parent = gpWM_RootWindow;
58
59         // - Call create window function
60         ret = renderer->CreateWindow(RendererArg);
61         ret->Client = Client;
62         ret->ID = ID;
63         ret->Parent = Parent;
64         ret->Renderer = renderer;
65         ret->Flags |= WINFLAG_CLEAN;    // Needed to stop invaildate early exiting
66
67         // Append to parent
68         if(Parent)
69         {
70                 if(Parent->LastChild)
71                         Parent->LastChild->NextSibling = ret;
72                 else
73                         Parent->FirstChild = ret;
74                 ret->PrevSibling = Parent->LastChild;
75                 Parent->LastChild = ret;
76                 ret->NextSibling = NULL;
77         }
78         else
79         {
80                 gpWM_RootWindow = ret;
81         }
82
83         // Don't decorate child windows by default
84         if(Parent != gpWM_RootWindow)
85         {
86                 ret->Flags |= WINFLAG_NODECORATE;
87         }
88         
89         // - Return!
90         return ret;
91 }
92
93 tWindow *WM_CreateWindowStruct(size_t ExtraSize)
94 {
95         tWindow *ret;
96         
97         ret = calloc( sizeof(tWindow) + ExtraSize, 1 );
98         ret->RendererInfo = ret + 1;    // Get end of tWindow
99         
100         return ret;
101 }
102
103 void WM_SetWindowTitle(tWindow *Window, const char *Title)
104 {
105         if(Window->Title)
106                 free(Window->Title);
107         Window->Title = strdup(Title);
108         _SysDebug("Window %p title set to '%s'", Window, Title);
109 }
110
111 void WM_RaiseWindow(tWindow *Window)
112 {
113         tWindow *parent = Window->Parent;
114         if(!Window->Parent)     return ;
115         
116         // Remove from list
117         if(Window->PrevSibling)
118                 Window->PrevSibling->NextSibling = Window->NextSibling;
119         if(Window->NextSibling)
120                 Window->NextSibling->PrevSibling = Window->PrevSibling;
121         if(parent->FirstChild == Window)
122                 parent->FirstChild = Window->NextSibling;
123         if(parent->LastChild == Window)
124                 parent->LastChild = Window->PrevSibling;
125
126         // Append to end
127         if(parent->LastChild)
128                 parent->LastChild->NextSibling = Window;
129         else
130                 parent->FirstChild = Window;
131         Window->PrevSibling = parent->LastChild;
132         Window->NextSibling = NULL;
133         parent->LastChild = Window;
134 }
135
136 void WM_FocusWindow(tWindow *Destination)
137 {
138         struct sWndMsg_Bool     _msg;
139         
140         if( gpWM_FocusedWindow == Destination )
141                 return ;
142         if( Destination && !(Destination->Flags & WINFLAG_SHOW) )
143                 return ;
144         
145         _msg.Val = 0;
146         WM_SendMessage(NULL, gpWM_FocusedWindow, WNDMSG_FOCUS, sizeof(_msg), &_msg);
147         _msg.Val = 1;
148         WM_SendMessage(NULL, Destination, WNDMSG_FOCUS, sizeof(_msg), &_msg);
149         
150         gpWM_FocusedWindow = Destination;
151 }
152
153
154 void WM_ShowWindow(tWindow *Window, int bShow)
155 {
156         // Message window
157         struct sWndMsg_Bool     _msg;
158         
159         if( !!(Window->Flags & WINFLAG_SHOW) == bShow )
160                 return ;
161
162         _msg.Val = !!bShow;
163         WM_SendMessage(NULL, Window, WNDMSG_SHOW, sizeof(_msg), &_msg);
164         
165         if(bShow)
166                 Window->Flags |= WINFLAG_SHOW;
167         else {
168                 Window->Flags &= ~WINFLAG_SHOW;
169                 if( Window == gpWM_FocusedWindow )
170                         WM_FocusWindow(Window->Parent);
171         }
172         // Just a little memory saving for large hidden windows
173         if(Window->RenderBuffer)
174                 free(Window->RenderBuffer);
175         
176         WM_Invalidate(Window);
177 }
178
179 void WM_DecorateWindow(tWindow *Window, int bDecorate)
180 {
181         if( !(Window->Flags & WINFLAG_NODECORATE) == !!bDecorate )
182                 return ;
183         
184         if(bDecorate)
185                 Window->Flags &= ~WINFLAG_NODECORATE;
186         else
187                 Window->Flags |= WINFLAG_NODECORATE;
188         
189         // Needed because the window size changes
190         if(Window->RenderBuffer)
191                 free(Window->RenderBuffer);
192         
193         WM_Invalidate(Window);
194 }
195
196 int WM_MoveWindow(tWindow *Window, int X, int Y)
197 {
198         // Clip coordinates
199         if(X + Window->W < 0)   X = -Window->W + 1;
200         if(Y + Window->H < 0)   Y = -Window->H + 1;
201         if(X >= giScreenWidth)  X = giScreenWidth - 1;
202         if(Y >= giScreenHeight) Y = giScreenHeight - 1;
203         
204         Window->X = X;  Window->Y = Y;
205
206         WM_Invalidate(Window);
207
208         return 0;
209 }
210
211 int WM_ResizeWindow(tWindow *Window, int W, int H)
212 {
213         if(W <= 0 || H <= 0 )   return 1;
214         if(Window->X + W < 0)   Window->X = -W + 1;
215         if(Window->Y + H < 0)   Window->Y = -H + 1;
216
217         Window->W = W;  Window->H = H;
218
219         if(Window->RenderBuffer)
220                 free(Window->RenderBuffer);
221         WM_Invalidate(Window);
222
223         {
224                 struct sWndMsg_Resize   msg;
225                 msg.W = W;
226                 msg.H = H;
227                 WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
228         }
229         
230         return 0;
231 }
232
233 int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, const void *Data)
234 {
235         if(Dest == NULL)        return -2;
236         if(Length > 0 && Data == NULL)  return -1;
237
238         if( Decorator_HandleMessage(Dest, Message, Length, Data) != 1 )
239         {
240                 // TODO: Catch errors from ->HandleMessage
241                 return 0;
242         }
243         
244         // ->HandleMessage returns 1 when the message was not handled
245         if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
246         {
247                 // TODO: Catch errors from ->HandleMessage
248                 return 0;
249         }
250
251         // TODO: Implement message masking
252
253         if(Dest->Client)
254         {
255                 uint32_t        src_id;
256                 if(!Source)
257                         src_id = -1;
258                 else if(Source->Client != Dest->Client) {
259                         // TODO: Support different client source windows
260                         _SysDebug("WM_SendMessage: TODO - Support inter-client messages");
261                         return -1;
262                 }
263                 else {
264                         src_id = Source->ID;
265                 }
266                 
267                 IPC_SendWMMessage(Dest->Client, src_id, Dest->ID, Message, Length, Data);
268         }       
269
270         return 1;
271 }
272
273 void WM_Invalidate(tWindow *Window)
274 {
275         _SysDebug("Invalidating %p", Window);
276         // Don't invalidate twice (speedup)
277 //      if( !(Window->Flags & WINFLAG_CLEAN) )  return;
278
279         // Mark for re-render
280         Window->Flags &= ~WINFLAG_CLEAN;
281
282         // Mark up the tree that a child window has changed     
283         while( (Window = Window->Parent) )
284                 Window->Flags &= ~WINFLAG_CHILDCLEAN;
285 }
286
287 // --- Rendering / Update
288 void WM_int_UpdateWindow(tWindow *Window)
289 {
290         tWindow *child;
291
292         // Ignore hidden windows
293         if( !(Window->Flags & WINFLAG_SHOW) )
294                 return ;
295         
296         // Render
297         if( !(Window->Flags & WINFLAG_CLEAN) )
298         {
299                 // Calculate RealW/RealH
300                 if( !(Window->Flags & WINFLAG_NODECORATE) )
301                 {
302                         _SysDebug("Applying decorations to %p", Window);
303                         Decorator_UpdateBorderSize(Window);
304                         Window->RealW = Window->BorderL + Window->W + Window->BorderR;
305                         Window->RealH = Window->BorderT + Window->H + Window->BorderB;
306                         Decorator_Redraw(Window);
307                 }
308                 else
309                 {
310                         Window->BorderL = 0;
311                         Window->BorderR = 0;
312                         Window->BorderT = 0;
313                         Window->BorderB = 0;
314                         Window->RealW = Window->W;
315                         Window->RealH = Window->H;
316                 }
317                 
318                 Window->Renderer->Redraw(Window);
319                 Window->Flags |= WINFLAG_CLEAN;
320         }
321         
322         // Process children
323         if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
324         {
325                 for( child = Window->FirstChild; child; child = child->NextSibling )
326                 {
327                         WM_int_UpdateWindow(child);
328                 }
329                 Window->Flags |= WINFLAG_CHILDCLEAN;
330         }
331         
332 }
333
334 void WM_int_BlitWindow(tWindow *Window)
335 {
336         tWindow *child;
337
338         // Ignore hidden windows
339         if( !(Window->Flags & WINFLAG_SHOW) )
340                 return ;
341
342         _SysDebug("Blit %p to (%i,%i) %ix%i", Window, Window->X, Window->Y, Window->RealW, Window->RealH);
343         Video_Blit(Window->RenderBuffer, Window->X, Window->Y, Window->RealW, Window->RealH);
344         
345         for( child = Window->FirstChild; child; child = child->NextSibling )
346         {
347                 WM_int_BlitWindow(child);
348         }
349 }
350
351 void WM_Update(void)
352 {
353         // Don't redraw if nothing has changed
354         if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
355                 return ;        
356
357         // - Iterate through visible windows, updating them as needed
358         WM_int_UpdateWindow(gpWM_RootWindow);
359         
360         // - Draw windows from back to front to the render buffer
361         WM_int_BlitWindow(gpWM_RootWindow);
362
363         Video_Update();
364 }
365

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