Merge branch 'master' of git://localhost/acess2
[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 void WM_DestroyWindow(tWindow *Window)
98 {
99         // TODO: Lock window and flag as invalid
100         
101         // - Remove from render tree
102         {
103                 // TODO: Lock render tree?
104                 tWindow *prev = Window->PrevSibling;
105                 tWindow *next = Window->NextSibling;
106                 if(prev)
107                         prev->NextSibling = next;
108                 else
109                         Window->Parent->FirstChild = next;
110                 if(next)
111                         next->PrevSibling = prev;
112                 else
113                         Window->Parent->LastChild = prev;
114         }
115         // - Full invalidate
116         WM_Invalidate(Window, 1);
117         
118         // - Remove from inheritance tree?
119         
120         // - Clean up render children
121         {
122                 // Lock should not be needed
123                 tWindow *win, *next;
124                 for( win = Window->FirstChild; win; win = next )
125                 {
126                         next = win->NextSibling;
127                         ASSERT(Window->FirstChild->Parent == Window);
128                         WM_DestroyWindow(win);
129                 }
130         }
131         
132         // - Clean up inheriting children?
133
134         // - Tell renderer to clean up
135         if( Window->Renderer->DestroyWindow )
136                 Window->Renderer->DestroyWindow(Window);
137         else
138                 _SysDebug("WARN: Renderer '%s' does not have a destroy function", Window->Renderer->Name);
139
140         // - Tell client to clean up
141         WM_SendMessage(NULL, Window, WNDMSG_DESTROY, 0, NULL);
142
143         // - Clean up render cache and window structure
144         free(Window->Title);
145         free(Window->RenderBuffer);
146         free(Window);
147 }
148
149 tWindow *WM_GetWindowByID(tWindow *Requester, uint32_t ID)
150 {
151         return IPC_int_GetWindow(Requester->Client, ID);
152 }
153
154 tWindow *WM_CreateWindowStruct(size_t ExtraSize)
155 {
156         tWindow *ret;
157         
158         ret = calloc( sizeof(tWindow) + ExtraSize, 1 );
159         ret->RendererInfo = ret + 1;    // Get end of tWindow
160         
161         return ret;
162 }
163
164 void WM_SetWindowTitle(tWindow *Window, const char *Title)
165 {
166         if(Window->Title)
167                 free(Window->Title);
168         Window->Title = strdup(Title);
169         _SysDebug("Window %p title set to '%s'", Window, Title);
170 }
171
172 void WM_RaiseWindow(tWindow *Window)
173 {
174         tWindow *parent = Window->Parent;
175         if(!Window->Parent)     return ;
176         
177         // Remove from list
178         if(Window->PrevSibling)
179                 Window->PrevSibling->NextSibling = Window->NextSibling;
180         if(Window->NextSibling)
181                 Window->NextSibling->PrevSibling = Window->PrevSibling;
182         if(parent->FirstChild == Window)
183                 parent->FirstChild = Window->NextSibling;
184         if(parent->LastChild == Window)
185                 parent->LastChild = Window->PrevSibling;
186
187         // Append to end
188         if(parent->LastChild)
189                 parent->LastChild->NextSibling = Window;
190         else
191                 parent->FirstChild = Window;
192         Window->PrevSibling = parent->LastChild;
193         Window->NextSibling = NULL;
194         parent->LastChild = Window;
195         
196         _SysDebug("Raised %p", Window);
197 }
198
199 /*
200 void WM_RaiseWindow(tWindow *Window)
201 {
202         // Move to the last render position (move to top)
203         while(Window && Window->Parent)
204         {
205                 if( Window->NextSibling )
206                 {
207                         // remove
208                         if( Window->PrevSibling )
209                                 Window->PrevSibling->NextSibling = Window->NextSibling;
210                         Window->NextSibling->PrevSibling = Window->PrevSibling;
211                         // Mutate self
212                         Window->PrevSibling = Window->Parent->LastChild;
213                         Window->NextSibling = NULL;
214                         // re-add
215                         Window->PrevSibling->NextSibling = Window;
216                         Window->Parent->LastChild = Window;
217                 }
218                 _SysDebug("WM_RaiseWindow: Raised %p", Window);
219                 Window = Window->Parent;
220         }
221 }
222 */
223
224 void WM_FocusWindow(tWindow *Destination)
225 {
226         struct sWndMsg_Bool     _msg;
227         
228         _SysDebug("WM_FocusWindow(%p)", Destination);
229
230         if( gpWM_FocusedWindow == Destination )
231                 return ;
232         if( Destination && !(Destination->Flags & WINFLAG_SHOW) )
233                 return ;
234         
235         if( gpWM_FocusedWindow )
236         {
237                 _msg.Val = 0;
238                 WM_SendMessage(NULL, gpWM_FocusedWindow, WNDMSG_FOCUS, sizeof(_msg), &_msg);
239         }
240         if( Destination )
241         {
242                 _msg.Val = 1;
243                 WM_SendMessage(NULL, Destination, WNDMSG_FOCUS, sizeof(_msg), &_msg);
244         }
245         
246         // TODO: Leave it up to the renderer to decide to invalidate
247         WM_Invalidate(gpWM_FocusedWindow, 1);
248         WM_Invalidate(Destination, 1);
249
250         gpWM_FocusedWindow = Destination;
251 }
252
253
254 void WM_ShowWindow(tWindow *Window, int bShow)
255 {
256         struct sWndMsg_Bool     _msg;
257         
258         if( !!(Window->Flags & WINFLAG_SHOW) == bShow )
259                 return ;
260         
261         // Window is being hidden, invalidate parents
262         if( !bShow )
263                 WM_Invalidate(Window, 0);
264
265         // Message window
266         _msg.Val = !!bShow;
267         WM_SendMessage(NULL, Window, WNDMSG_SHOW, sizeof(_msg), &_msg);
268
269         // Update the flag
270         if(bShow) {
271                 Window->Flags |= WINFLAG_SHOW;
272                 _SysDebug("Window %p shown", Window);
273         }
274         else
275         {
276                 Window->Flags &= ~WINFLAG_SHOW;
277
278                 if( Window == gpWM_FocusedWindow )
279                         WM_FocusWindow(Window->Parent);
280                 
281                 // Just a little memory saving for large hidden windows
282                 if(Window->RenderBuffer) {
283                         free(Window->RenderBuffer);
284                         Window->RenderBuffer = NULL;
285                 }
286                 _SysDebug("Window %p hidden", Window);
287         }
288         
289         // Window has been shown, invalidate everything
290         if( bShow )
291                 WM_Invalidate(Window, 1);
292 }
293
294 void WM_DecorateWindow(tWindow *Window, int bDecorate)
295 {
296         if( !(Window->Flags & WINFLAG_NODECORATE) == !!bDecorate )
297                 return ;
298         
299         if(bDecorate)
300                 Window->Flags &= ~WINFLAG_NODECORATE;
301         else
302                 Window->Flags |= WINFLAG_NODECORATE;
303         
304         // Needed because the window size changes
305         if(Window->RenderBuffer) {
306                 free(Window->RenderBuffer);
307                 Window->RenderBuffer = NULL;
308         }
309         
310         WM_Invalidate(Window, 1);
311 }
312
313 void WM_SetRelative(tWindow *Window, int bRelativeToParent)
314 {
315 //      _SysDebug("WM_SetRelative: (%p{Parent=%p},%i)", Window, Window->Parent, bRelativeToParent);
316         // No meaning if no parent
317         if( !Window->Parent )
318                 return ;
319
320         // Check that the flag is changing
321         if( !!bRelativeToParent == !!(Window->Flags & WINFLAG_RELATIVE) )
322                 return ;
323
324         if( bRelativeToParent ) {
325                 // Set
326                 Window->Flags |= WINFLAG_RELATIVE;
327                 WM_MoveWindow(Window, Window->X, Window->Y);
328         }
329         else {
330                 // Clear
331                 Window->Flags &= ~WINFLAG_RELATIVE;
332                 WM_MoveWindow(Window, Window->X - Window->Parent->X, Window->Y - Window->Parent->Y);
333         }
334 }
335
336 int WM_MoveWindow(tWindow *Window, int X, int Y)
337 {
338 //      _SysDebug("Move %p to (%i,%i)", Window, X, Y);
339         // Clip coordinates
340         if(X + Window->W < 0)   X = -Window->W + 1;
341         if(Y + Window->H < 0)   Y = -Window->H + 1;
342         if(X >= giScreenWidth)  X = giScreenWidth - 1;
343         if(Y >= giScreenHeight) Y = giScreenHeight - 1;
344         
345         // If relative to the parent, extra checks
346         if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
347         {
348                 if( X > Window->Parent->W )     return 1;
349                 if( Y > Window->Parent->H )     return 1;
350         }
351         // TODO: Re-sanitise
352
353         if( Window->X == X && Window->Y == Y ) {
354                 _SysDebug("WM_MoveWindow: Equal (%i,%i)", X, Y);
355                 return 0;
356         }
357
358         if( Window->Parent )
359                 Window->Parent->Flags |= WINFLAG_NEEDREBLIT;
360
361         _SysDebug("WM_MoveWindow: (%i,%i)", X, Y);
362         Window->X = X;  Window->Y = Y;
363
364         // Mark up the tree that a child window has changed     
365         WM_Invalidate(Window, 0);
366
367         return 0;
368 }
369
370 int WM_ResizeWindow(tWindow *Window, int W, int H)
371 {
372         if(W <= 0 || H <= 0 )   return 1;
373         if(Window->X + W < 0)   Window->X = -W + 1;
374         if(Window->Y + H < 0)   Window->Y = -H + 1;
375
376         if( Window->W == W && Window->H == H )
377                 return 0;
378         
379         // If the window size has decreased, force the parent to reblit
380         if( Window->Parent && (Window->W > W || Window->H > H) )
381                 Window->Parent->Flags |= WINFLAG_NEEDREBLIT;
382
383         _SysDebug("WM_ResizeWindow: %p:%i %ix%i", Window->Client, Window->ID, W, H);
384         Window->W = W;  Window->H = H;
385
386         if(Window->RenderBuffer) {
387                 free(Window->RenderBuffer);
388                 Window->RenderBuffer = NULL;
389         }
390         WM_Invalidate(Window, 1);
391
392         {
393                 struct sWndMsg_Resize   msg;
394                 msg.W = W;
395                 msg.H = H;
396                 WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
397         }
398         
399         return 0;
400 }
401
402 int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, const void *Data)
403 {
404 //      _SysDebug("WM_SendMessage: (%p, %p, %i, %i, %p)", Source, Dest, Message, Length, Data);
405         if(Dest == NULL) {
406                 _SysDebug("WM_SendMessage: NULL destination from %p", __builtin_return_address(0));
407                 return -2;
408         }
409         if(Length > 0 && Data == NULL) {
410                 _SysDebug("WM_SendMessage: non-zero length and NULL data");
411                 return -1;
412         }
413
414         if( Decorator_HandleMessage(Dest, Message, Length, Data) != 1 )
415         {
416                 // TODO: Catch errors from ->HandleMessage
417 //              _SysDebug("WM_SendMessage: Decorator grabbed message?");
418                 return 0;
419         }
420         
421         // ->HandleMessage returns 1 when the message was not handled
422         if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
423         {
424                 // TODO: Catch errors from ->HandleMessage
425 //              _SysDebug("WM_SendMessage: Renderer grabbed message?");
426                 return 0;
427         }
428
429         // TODO: Implement message masking
430
431         // Dispatch to client
432         if(Dest->Client)
433         {
434                 uint32_t        src_id;
435                 if(!Source)
436                         src_id = -1;
437                 else if(Source->Client != Dest->Client) {
438                         // TODO: Support different client source windows
439                         _SysDebug("WM_SendMessage: TODO - Support inter-client messages");
440                         return -1;
441                 }
442                 else {
443                         src_id = Source->ID;
444                 }
445                 
446 //              _SysDebug("WM_SendMessage: IPC Dispatch");
447                 IPC_SendWMMessage(Dest->Client, src_id, Dest->ID, Message, Length, Data);
448         }       
449
450         return 1;
451 }
452
453 int WM_SendIPCReply(tWindow *Window, int Message, size_t Length, const void *Data)
454 {
455         IPC_SendReply(Window->Client, Window->ID, Message, Length, Data);
456         return 0;
457 }
458
459 void WM_Invalidate(tWindow *Window, int bClearClean)
460 {
461         if(!Window)     return ;
462         
463         // Don't bother invalidating if the window isn't shown
464         if( !(Window->Flags & WINFLAG_SHOW) )
465                 return ;
466         
467         // Mark for re-render
468         if( bClearClean )
469                 Window->Flags &= ~WINFLAG_CLEAN;
470
471         // Mark up the tree that a child window has changed     
472         while( (Window = Window->Parent) && (Window->Flags & WINFLAG_SHOW) )
473                 Window->Flags &= ~WINFLAG_CHILDCLEAN;
474 }
475
476 // --- Rendering / Update
477 void WM_int_UpdateWindow(tWindow *Window)
478 {
479          int    bDecoratorRedraw = 0;
480
481         // Ignore hidden windows
482         if( !(Window->Flags & WINFLAG_SHOW) )
483                 return ;
484         
485         if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
486         {
487                 Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
488                 Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
489         }
490         else
491         {
492                 Window->RealX = Window->X;
493                 Window->RealY = Window->Y;
494         }
495         
496         // Render
497         if( !(Window->Flags & WINFLAG_CLEAN) )
498         {
499                 // Calculate RealW/RealH
500                 if( !(Window->Flags & WINFLAG_NODECORATE) )
501                 {
502                         //_SysDebug("Applying decorations to %p", Window);
503                         Decorator_UpdateBorderSize(Window);
504                         Window->RealW = Window->BorderL + Window->W + Window->BorderR;
505                         Window->RealH = Window->BorderT + Window->H + Window->BorderB;
506                         bDecoratorRedraw = 1;
507                 }
508                 else
509                 {
510                         Window->BorderL = 0;
511                         Window->BorderR = 0;
512                         Window->BorderT = 0;
513                         Window->BorderB = 0;
514                         Window->RealW = Window->W;
515                         Window->RealH = Window->H;
516                 }
517
518                 Window->Renderer->Redraw(Window);
519                 Window->Flags |= WINFLAG_CLEAN|WINFLAG_NEEDREBLIT;
520         }
521         
522         // Process children
523         if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
524         {
525                 tWindow *child;
526                 for( child = Window->FirstChild; child; child = child->NextSibling )
527                 {
528                         WM_int_UpdateWindow(child);
529                 }
530                 Window->Flags |= WINFLAG_CHILDCLEAN;
531         }
532         
533         if( bDecoratorRedraw )
534                 Decorator_Redraw(Window);
535 }
536
537 void WM_int_BlitWindow(tWindow *Window, int bForceReblit)
538 {
539         tWindow *child;
540
541         // Ignore hidden windows
542         if( !(Window->Flags & WINFLAG_SHOW) )
543                 return ;
544         
545         // Duplicated position update to handle window moving
546         if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
547         {
548                 Window->RealX = Window->Parent->RealX + Window->Parent->BorderL + Window->X;
549                 Window->RealY = Window->Parent->RealY + Window->Parent->BorderT + Window->Y;
550         }
551         else
552         {
553                 Window->RealX = Window->X;
554                 Window->RealY = Window->Y;
555         }
556
557 //      _SysDebug("Blit %p (%p) to (%i,%i) %ix%i", Window, Window->RenderBuffer,
558 //              Window->RealX, Window->RealY, Window->RealW, Window->RealH);
559         // TODO Don't blit unless:
560         // a) A parent has been reblitted (thus clobbering the existing content)
561         // b) A child has moved (exposing a previously hidden area)
562         if( bForceReblit || (Window->Flags & WINFLAG_NEEDREBLIT) )
563         {
564                 Video_Blit(Window->RenderBuffer, Window->RealX, Window->RealY, Window->RealW, Window->RealH);
565                 Window->Flags &= ~WINFLAG_NEEDREBLIT;
566                 bForceReblit = 1;
567         }
568         
569         if( Window == gpWM_FocusedWindow && Window->CursorW )
570         {
571                 Video_FillRect(
572                         Window->RealX + Window->BorderL + Window->CursorX,
573                         Window->RealY + Window->BorderT + Window->CursorY,
574                         Window->CursorW, Window->CursorH,
575                         0x000000
576                         );
577         }
578
579         for( child = Window->FirstChild; child; child = child->NextSibling )
580         {
581                 WM_int_BlitWindow(child, bForceReblit);
582         }
583 }
584
585 void WM_Update(void)
586 {
587         // Don't redraw if nothing has changed
588         if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
589                 return ;        
590
591         // - Iterate through visible windows, updating them as needed
592         WM_int_UpdateWindow(gpWM_RootWindow);
593         
594         // - Draw windows from back to front to the render buffer
595         WM_int_BlitWindow(gpWM_RootWindow, 0);
596
597         Video_Update();
598 }
599

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