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

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