Usermode/AxWin3 - Fixed error with relative windows and input
[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 extern void     IPC_SendReply(tIPC_Client *Client, uint32_t WinID, int MsgID, size_t Len, const void *Data);
19 extern tWindow  *IPC_int_GetWindow(tIPC_Client *Client, uint32_t ID);
20
21 // === GLOBALS ===
22 tWMRenderer     *gpWM_Renderers;
23 tWindow *gpWM_RootWindow;
24 //! Window which will recieve the next keyboard event
25 tWindow *gpWM_FocusedWindow;
26 //! Hilighted window (owner of the currently focused window)
27 tWindow *gpWM_HilightedWindow;
28
29 // === CODE ===
30 void WM_Initialise(void)
31 {
32         WM_CreateWindow(NULL, NULL, 0, 0x0088FF, "Background");
33         gpWM_RootWindow->W = giScreenWidth;
34         gpWM_RootWindow->H = giScreenHeight;
35         gpWM_RootWindow->Flags = WINFLAG_SHOW|WINFLAG_NODECORATE;
36 }
37
38 void WM_RegisterRenderer(tWMRenderer *Renderer)
39 {
40         // TODO: Catch out duplicates
41         Renderer->Next = gpWM_Renderers;
42         gpWM_Renderers = Renderer;
43 }
44
45 // --- Manipulation
46 tWindow *WM_CreateWindow(tWindow *Parent, tIPC_Client *Client, uint32_t ID, int RendererArg, const char *RendererName)
47 {
48         tWMRenderer     *renderer;
49         tWindow *ret;
50         
51         // - Get Renderer
52         for( renderer = gpWM_Renderers; renderer; renderer = renderer->Next )
53         {
54                 if(strcmp(RendererName, renderer->Name) == 0)
55                         break;
56         }
57         if(renderer == NULL)
58                 return NULL;
59
60         // - Call create window function
61         ret = renderer->CreateWindow(RendererArg);
62         ret->Client = Client;
63         ret->ID = ID;
64         ret->Parent = Parent;
65         if(!ret->Parent)
66                 ret->Parent = gpWM_RootWindow;
67         ret->Owner = Parent;
68         ret->Renderer = renderer;
69         ret->Flags |= WINFLAG_CLEAN;    // Needed to stop invaildate early exiting
70
71         // Append to parent
72         if(ret->Parent)
73         {
74                 if(ret->Parent->LastChild)
75                         ret->Parent->LastChild->NextSibling = ret;
76                 else
77                         ret->Parent->FirstChild = ret;
78                 ret->PrevSibling = ret->Parent->LastChild;
79                 ret->Parent->LastChild = ret;
80                 ret->NextSibling = NULL;
81         }
82         else
83         {
84                 gpWM_RootWindow = ret;
85         }
86
87         // Don't decorate child windows by default
88         if(Parent)
89         {
90                 ret->Flags |= WINFLAG_NODECORATE;
91         }
92         
93         // - Return!
94         return ret;
95 }
96
97 tWindow *WM_GetWindowByID(tWindow *Requester, uint32_t ID)
98 {
99         return IPC_int_GetWindow(Requester->Client, ID);
100 }
101
102 tWindow *WM_CreateWindowStruct(size_t ExtraSize)
103 {
104         tWindow *ret;
105         
106         ret = calloc( sizeof(tWindow) + ExtraSize, 1 );
107         ret->RendererInfo = ret + 1;    // Get end of tWindow
108         
109         return ret;
110 }
111
112 void WM_SetWindowTitle(tWindow *Window, const char *Title)
113 {
114         if(Window->Title)
115                 free(Window->Title);
116         Window->Title = strdup(Title);
117         _SysDebug("Window %p title set to '%s'", Window, Title);
118 }
119
120 void WM_RaiseWindow(tWindow *Window)
121 {
122         tWindow *parent = Window->Parent;
123         if(!Window->Parent)     return ;
124         
125         // Remove from list
126         if(Window->PrevSibling)
127                 Window->PrevSibling->NextSibling = Window->NextSibling;
128         if(Window->NextSibling)
129                 Window->NextSibling->PrevSibling = Window->PrevSibling;
130         if(parent->FirstChild == Window)
131                 parent->FirstChild = Window->NextSibling;
132         if(parent->LastChild == Window)
133                 parent->LastChild = Window->PrevSibling;
134
135         // Append to end
136         if(parent->LastChild)
137                 parent->LastChild->NextSibling = Window;
138         else
139                 parent->FirstChild = Window;
140         Window->PrevSibling = parent->LastChild;
141         Window->NextSibling = NULL;
142         parent->LastChild = Window;
143         
144         _SysDebug("Raised %p", Window);
145 }
146
147 /*
148 void WM_RaiseWindow(tWindow *Window)
149 {
150         // Move to the last render position (move to top)
151         while(Window && Window->Parent)
152         {
153                 if( Window->NextSibling )
154                 {
155                         // remove
156                         if( Window->PrevSibling )
157                                 Window->PrevSibling->NextSibling = Window->NextSibling;
158                         Window->NextSibling->PrevSibling = Window->PrevSibling;
159                         // Mutate self
160                         Window->PrevSibling = Window->Parent->LastChild;
161                         Window->NextSibling = NULL;
162                         // re-add
163                         Window->PrevSibling->NextSibling = Window;
164                         Window->Parent->LastChild = Window;
165                 }
166                 _SysDebug("WM_RaiseWindow: Raised %p", Window);
167                 Window = Window->Parent;
168         }
169 }
170 */
171
172 void WM_FocusWindow(tWindow *Destination)
173 {
174         struct sWndMsg_Bool     _msg;
175         
176         if( gpWM_FocusedWindow == Destination )
177                 return ;
178         if( Destination && !(Destination->Flags & WINFLAG_SHOW) )
179                 return ;
180         
181         if( gpWM_FocusedWindow )
182         {
183                 _msg.Val = 0;
184                 WM_SendMessage(NULL, gpWM_FocusedWindow, WNDMSG_FOCUS, sizeof(_msg), &_msg);
185         }
186         if( Destination )
187         {
188                 _msg.Val = 1;
189                 WM_SendMessage(NULL, Destination, WNDMSG_FOCUS, sizeof(_msg), &_msg);
190         }
191                 
192         WM_Invalidate(gpWM_FocusedWindow);
193         WM_Invalidate(Destination);
194
195         gpWM_FocusedWindow = Destination;
196 }
197
198
199 void WM_ShowWindow(tWindow *Window, int bShow)
200 {
201         struct sWndMsg_Bool     _msg;
202         
203         if( !!(Window->Flags & WINFLAG_SHOW) == bShow )
204                 return ;
205
206         // Message window
207         _msg.Val = !!bShow;
208         WM_SendMessage(NULL, Window, WNDMSG_SHOW, sizeof(_msg), &_msg);
209
210         // Update the flag
211         if(bShow) {
212                 Window->Flags |= WINFLAG_SHOW;
213                 _SysDebug("Window %p shown", Window);
214         }
215         else
216         {
217                 Window->Flags &= ~WINFLAG_SHOW;
218
219                 if( Window == gpWM_FocusedWindow )
220                         WM_FocusWindow(Window->Parent);
221                 
222                 // Just a little memory saving for large hidden windows
223                 if(Window->RenderBuffer) {
224                         free(Window->RenderBuffer);
225                         Window->RenderBuffer = NULL;
226                 }
227                 _SysDebug("Window %p hidden", Window);
228         }
229         
230         WM_Invalidate(Window);
231 }
232
233 void WM_DecorateWindow(tWindow *Window, int bDecorate)
234 {
235         if( !(Window->Flags & WINFLAG_NODECORATE) == !!bDecorate )
236                 return ;
237         
238         if(bDecorate)
239                 Window->Flags &= ~WINFLAG_NODECORATE;
240         else
241                 Window->Flags |= WINFLAG_NODECORATE;
242         
243         // Needed because the window size changes
244         if(Window->RenderBuffer) {
245                 free(Window->RenderBuffer);
246                 Window->RenderBuffer = NULL;
247         }
248         
249         WM_Invalidate(Window);
250 }
251
252 void WM_SetRelative(tWindow *Window, int bRelativeToParent)
253 {
254 //      _SysDebug("WM_SetRelative: (%p{Parent=%p},%i)", Window, Window->Parent, bRelativeToParent);
255         // No meaning if no parent
256         if( !Window->Parent )
257                 return ;
258
259         // Check that the flag is changing
260         if( !!bRelativeToParent == !!(Window->Flags & WINFLAG_RELATIVE) )
261                 return ;
262
263         if( bRelativeToParent ) {
264                 // Set
265                 Window->Flags |= WINFLAG_RELATIVE;
266                 WM_MoveWindow(Window, Window->X, Window->Y);
267         }
268         else {
269                 // Clear
270                 Window->Flags &= ~WINFLAG_RELATIVE;
271                 WM_MoveWindow(Window, Window->X - Window->Parent->X, Window->Y - Window->Parent->Y);
272         }
273 }
274
275 int WM_MoveWindow(tWindow *Window, int X, int Y)
276 {
277 //      _SysDebug("Move %p to (%i,%i)", Window, X, Y);
278         // Clip coordinates
279         if(X + Window->W < 0)   X = -Window->W + 1;
280         if(Y + Window->H < 0)   Y = -Window->H + 1;
281         if(X >= giScreenWidth)  X = giScreenWidth - 1;
282         if(Y >= giScreenHeight) Y = giScreenHeight - 1;
283         
284         // If relative to the parent, extra checks
285         if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
286         {
287                 if( X > Window->Parent->W )     return 1;
288                 if( Y > Window->Parent->H )     return 1;
289         }
290         // TODO: Re-sanitise
291
292         _SysDebug("WM_MoveWindow: (%i,%i)", X, Y);
293         Window->X = X;  Window->Y = Y;
294
295         // TODO: Why invalidate buffer?
296         WM_Invalidate(Window);
297
298         return 0;
299 }
300
301 int WM_ResizeWindow(tWindow *Window, int W, int H)
302 {
303         if(W <= 0 || H <= 0 )   return 1;
304         if(Window->X + W < 0)   Window->X = -W + 1;
305         if(Window->Y + H < 0)   Window->Y = -H + 1;
306
307         if( Window->W == W && Window->H == H )
308                 return 0;
309
310         _SysDebug("WM_Resizeindow: %ix%i", W, H);
311         Window->W = W;  Window->H = H;
312
313         if(Window->RenderBuffer) {
314                 free(Window->RenderBuffer);
315                 Window->RenderBuffer = NULL;
316         }
317         WM_Invalidate(Window);
318
319         {
320                 struct sWndMsg_Resize   msg;
321                 msg.W = W;
322                 msg.H = H;
323                 WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
324         }
325         
326         return 0;
327 }
328
329 int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, const void *Data)
330 {
331 //      _SysDebug("WM_SendMessage: (%p, %p, %i, %i, %p)", Source, Dest, Message, Length, Data);
332         if(Dest == NULL) {
333                 _SysDebug("WM_SendMessage: NULL destination from %p", __builtin_return_address(0));
334                 return -2;
335         }
336         if(Length > 0 && Data == NULL) {
337                 _SysDebug("WM_SendMessage: non-zero length and NULL data");
338                 return -1;
339         }
340
341         if( Decorator_HandleMessage(Dest, Message, Length, Data) != 1 )
342         {
343                 // TODO: Catch errors from ->HandleMessage
344 //              _SysDebug("WM_SendMessage: Decorator grabbed message?");
345                 return 0;
346         }
347         
348         // ->HandleMessage returns 1 when the message was not handled
349         if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
350         {
351                 // TODO: Catch errors from ->HandleMessage
352 //              _SysDebug("WM_SendMessage: Renderer grabbed message?");
353                 return 0;
354         }
355
356         // TODO: Implement message masking
357
358         // Dispatch to client
359         if(Dest->Client)
360         {
361                 uint32_t        src_id;
362                 if(!Source)
363                         src_id = -1;
364                 else if(Source->Client != Dest->Client) {
365                         // TODO: Support different client source windows
366                         _SysDebug("WM_SendMessage: TODO - Support inter-client messages");
367                         return -1;
368                 }
369                 else {
370                         src_id = Source->ID;
371                 }
372                 
373 //              _SysDebug("WM_SendMessage: IPC Dispatch");
374                 IPC_SendWMMessage(Dest->Client, src_id, Dest->ID, Message, Length, Data);
375         }       
376
377         return 1;
378 }
379
380 int WM_SendIPCReply(tWindow *Window, int Message, size_t Length, const void *Data)
381 {
382         IPC_SendReply(Window->Client, Window->ID, Message, Length, Data);
383         return 0;
384 }
385
386 void WM_Invalidate(tWindow *Window)
387 {
388         if(!Window)     return ;
389 //      _SysDebug("Invalidating %p", Window);
390         // Don't invalidate twice (speedup)
391 //      if( !(Window->Flags & WINFLAG_CLEAN) )  return;
392
393         // Mark for re-render
394         Window->Flags &= ~WINFLAG_CLEAN;
395
396         // Mark up the tree that a child window has changed     
397         while( (Window = Window->Parent) )
398                 Window->Flags &= ~WINFLAG_CHILDCLEAN;
399 }
400
401 // --- Rendering / Update
402 void WM_int_UpdateWindow(tWindow *Window)
403 {
404          int    bDecoratorRedraw = 0;
405
406         // Ignore hidden windows
407         if( !(Window->Flags & WINFLAG_SHOW) )
408                 return ;
409         
410         // Render
411         if( !(Window->Flags & WINFLAG_CLEAN) )
412         {
413                 // Calculate RealW/RealH
414                 if( !(Window->Flags & WINFLAG_NODECORATE) )
415                 {
416                         //_SysDebug("Applying decorations to %p", Window);
417                         Decorator_UpdateBorderSize(Window);
418                         Window->RealW = Window->BorderL + Window->W + Window->BorderR;
419                         Window->RealH = Window->BorderT + Window->H + Window->BorderB;
420                         bDecoratorRedraw = 1;
421                 }
422                 else
423                 {
424                         Window->BorderL = 0;
425                         Window->BorderR = 0;
426                         Window->BorderT = 0;
427                         Window->BorderB = 0;
428                         Window->RealW = Window->W;
429                         Window->RealH = Window->H;
430                 }
431                 
432                 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
433                 {
434                         Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
435                         Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
436                 }
437                 else
438                 {
439                         Window->RealX = Window->X;
440                         Window->RealY = Window->Y;
441                 }
442
443                 Window->Renderer->Redraw(Window);
444                 Window->Flags |= WINFLAG_CLEAN;
445         }
446         
447         // Process children
448         if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
449         {
450                 tWindow *child;
451                 for( child = Window->FirstChild; child; child = child->NextSibling )
452                 {
453                         WM_int_UpdateWindow(child);
454                 }
455                 Window->Flags |= WINFLAG_CHILDCLEAN;
456         }
457         
458         if( bDecoratorRedraw )
459                 Decorator_Redraw(Window);
460 }
461
462 void WM_int_BlitWindow(tWindow *Window)
463 {
464         tWindow *child;
465
466         // Ignore hidden windows
467         if( !(Window->Flags & WINFLAG_SHOW) )
468                 return ;
469
470 //      _SysDebug("Blit %p (%p) to (%i,%i) %ix%i", Window, Window->RenderBuffer,
471 //              Window->RealX, Window->RealY, Window->RealW, Window->RealH);
472         Video_Blit(Window->RenderBuffer, Window->RealX, Window->RealY, Window->RealW, Window->RealH);
473         
474         if( Window == gpWM_FocusedWindow && Window->CursorW )
475         {
476                 Video_FillRect(
477                         Window->RealX + Window->BorderL + Window->CursorX,
478                         Window->RealY + Window->BorderT + Window->CursorY,
479                         Window->CursorW, Window->CursorH,
480                         0x000000
481                         );
482         }
483
484         for( child = Window->FirstChild; child; child = child->NextSibling )
485         {
486                 WM_int_BlitWindow(child);
487         }
488 }
489
490 void WM_Update(void)
491 {
492         // Don't redraw if nothing has changed
493         if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
494                 return ;        
495
496         // - Iterate through visible windows, updating them as needed
497         WM_int_UpdateWindow(gpWM_RootWindow);
498         
499         // - Draw windows from back to front to the render buffer
500         WM_int_BlitWindow(gpWM_RootWindow);
501
502         Video_Update();
503 }
504

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