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

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