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

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