Usermode/AxWin3 - Fixed bad blit clipping
[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
145 /*
146 void WM_RaiseWindow(tWindow *Window)
147 {
148         // Move to the last render position (move to top)
149         while(Window && Window->Parent)
150         {
151                 if( Window->NextSibling )
152                 {
153                         // remove
154                         if( Window->PrevSibling )
155                                 Window->PrevSibling->NextSibling = Window->NextSibling;
156                         Window->NextSibling->PrevSibling = Window->PrevSibling;
157                         // Mutate self
158                         Window->PrevSibling = Window->Parent->LastChild;
159                         Window->NextSibling = NULL;
160                         // re-add
161                         Window->PrevSibling->NextSibling = Window;
162                         Window->Parent->LastChild = Window;
163                 }
164                 _SysDebug("WM_RaiseWindow: Raised %p", Window);
165                 Window = Window->Parent;
166         }
167 }
168 */
169
170 void WM_FocusWindow(tWindow *Destination)
171 {
172         struct sWndMsg_Bool     _msg;
173         
174         if( gpWM_FocusedWindow == Destination )
175                 return ;
176         if( Destination && !(Destination->Flags & WINFLAG_SHOW) )
177                 return ;
178         
179         if( gpWM_FocusedWindow )
180         {
181                 _msg.Val = 0;
182                 WM_SendMessage(NULL, gpWM_FocusedWindow, WNDMSG_FOCUS, sizeof(_msg), &_msg);
183         }
184         if( Destination )
185         {
186                 _msg.Val = 1;
187                 WM_SendMessage(NULL, Destination, WNDMSG_FOCUS, sizeof(_msg), &_msg);
188         }
189                 
190         WM_Invalidate(gpWM_FocusedWindow);
191         WM_Invalidate(Destination);
192
193         WM_RaiseWindow(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         else
214         {
215                 Window->Flags &= ~WINFLAG_SHOW;
216
217                 if( Window == gpWM_FocusedWindow )
218                         WM_FocusWindow(Window->Parent);
219                 
220                 // Just a little memory saving for large hidden windows
221                 if(Window->RenderBuffer) {
222                         free(Window->RenderBuffer);
223                         Window->RenderBuffer = NULL;
224                 }
225         }
226         
227         WM_Invalidate(Window);
228 }
229
230 void WM_DecorateWindow(tWindow *Window, int bDecorate)
231 {
232         if( !(Window->Flags & WINFLAG_NODECORATE) == !!bDecorate )
233                 return ;
234         
235         if(bDecorate)
236                 Window->Flags &= ~WINFLAG_NODECORATE;
237         else
238                 Window->Flags |= WINFLAG_NODECORATE;
239         
240         // Needed because the window size changes
241         if(Window->RenderBuffer) {
242                 free(Window->RenderBuffer);
243                 Window->RenderBuffer = NULL;
244         }
245         
246         WM_Invalidate(Window);
247 }
248
249 void WM_SetRelative(tWindow *Window, int bRelativeToParent)
250 {
251 //      _SysDebug("WM_SetRelative: (%p{Parent=%p},%i)", Window, Window->Parent, bRelativeToParent);
252         // No meaning if no parent
253         if( !Window->Parent )
254                 return ;
255
256         // Check that the flag is changing
257         if( !!bRelativeToParent == !!(Window->Flags & WINFLAG_RELATIVE) )
258                 return ;
259
260         if( bRelativeToParent ) {
261                 // Set
262                 Window->Flags |= WINFLAG_RELATIVE;
263                 WM_MoveWindow(Window, Window->X, Window->Y);
264         }
265         else {
266                 // Clear
267                 Window->Flags &= ~WINFLAG_RELATIVE;
268                 WM_MoveWindow(Window, Window->X - Window->Parent->X, Window->Y - Window->Parent->Y);
269         }
270 }
271
272 int WM_MoveWindow(tWindow *Window, int X, int Y)
273 {
274 //      _SysDebug("Move %p to (%i,%i)", Window, X, Y);
275         // Clip coordinates
276         if(X + Window->W < 0)   X = -Window->W + 1;
277         if(Y + Window->H < 0)   Y = -Window->H + 1;
278         if(X >= giScreenWidth)  X = giScreenWidth - 1;
279         if(Y >= giScreenHeight) Y = giScreenHeight - 1;
280         
281         // If relative to the parent, extra checks
282         if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
283         {
284                 if( X > Window->Parent->W )     return 1;
285                 if( Y > Window->Parent->H )     return 1;
286         }
287         // TODO: Re-sanitise
288
289         _SysDebug("WM_MoveWindow: (%i,%i)", X, Y);
290         Window->X = X;  Window->Y = Y;
291
292         // TODO: Why invalidate buffer?
293         WM_Invalidate(Window);
294
295         return 0;
296 }
297
298 int WM_ResizeWindow(tWindow *Window, int W, int H)
299 {
300         if(W <= 0 || H <= 0 )   return 1;
301         if(Window->X + W < 0)   Window->X = -W + 1;
302         if(Window->Y + H < 0)   Window->Y = -H + 1;
303
304         if( Window->W == W && Window->H == H )
305                 return 0;
306
307         _SysDebug("WM_Resizeindow: %ix%i", W, H);
308         Window->W = W;  Window->H = H;
309
310         if(Window->RenderBuffer) {
311                 free(Window->RenderBuffer);
312                 Window->RenderBuffer = NULL;
313         }
314         WM_Invalidate(Window);
315
316         {
317                 struct sWndMsg_Resize   msg;
318                 msg.W = W;
319                 msg.H = H;
320                 WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
321         }
322         
323         return 0;
324 }
325
326 int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, const void *Data)
327 {
328 //      _SysDebug("WM_SendMessage: (%p, %p, %i, %i, %p)", Source, Dest, Message, Length, Data);
329         if(Dest == NULL) {
330                 _SysDebug("WM_SendMessage: NULL destination from %p", __builtin_return_address(0));
331                 return -2;
332         }
333         if(Length > 0 && Data == NULL) {
334                 _SysDebug("WM_SendMessage: non-zero length and NULL data");
335                 return -1;
336         }
337
338         if( Decorator_HandleMessage(Dest, Message, Length, Data) != 1 )
339         {
340                 // TODO: Catch errors from ->HandleMessage
341 //              _SysDebug("WM_SendMessage: Decorator grabbed message?");
342                 return 0;
343         }
344         
345         // ->HandleMessage returns 1 when the message was not handled
346         if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
347         {
348                 // TODO: Catch errors from ->HandleMessage
349 //              _SysDebug("WM_SendMessage: Renderer grabbed message?");
350                 return 0;
351         }
352
353         // TODO: Implement message masking
354
355         // Dispatch to client
356         if(Dest->Client)
357         {
358                 uint32_t        src_id;
359                 if(!Source)
360                         src_id = -1;
361                 else if(Source->Client != Dest->Client) {
362                         // TODO: Support different client source windows
363                         _SysDebug("WM_SendMessage: TODO - Support inter-client messages");
364                         return -1;
365                 }
366                 else {
367                         src_id = Source->ID;
368                 }
369                 
370 //              _SysDebug("WM_SendMessage: IPC Dispatch");
371                 IPC_SendWMMessage(Dest->Client, src_id, Dest->ID, Message, Length, Data);
372         }       
373
374         return 1;
375 }
376
377 int WM_SendIPCReply(tWindow *Window, int Message, size_t Length, const void *Data)
378 {
379         IPC_SendReply(Window->Client, Window->ID, Message, Length, Data);
380         return 0;
381 }
382
383 void WM_Invalidate(tWindow *Window)
384 {
385         if(!Window)     return ;
386 //      _SysDebug("Invalidating %p", Window);
387         // Don't invalidate twice (speedup)
388 //      if( !(Window->Flags & WINFLAG_CLEAN) )  return;
389
390         // Mark for re-render
391         Window->Flags &= ~WINFLAG_CLEAN;
392
393         // Mark up the tree that a child window has changed     
394         while( (Window = Window->Parent) )
395                 Window->Flags &= ~WINFLAG_CHILDCLEAN;
396 }
397
398 // --- Rendering / Update
399 void WM_int_UpdateWindow(tWindow *Window)
400 {
401          int    bDecoratorRedraw = 0;
402
403         // Ignore hidden windows
404         if( !(Window->Flags & WINFLAG_SHOW) )
405                 return ;
406         
407         // Render
408         if( !(Window->Flags & WINFLAG_CLEAN) )
409         {
410                 // Calculate RealW/RealH
411                 if( !(Window->Flags & WINFLAG_NODECORATE) )
412                 {
413                         //_SysDebug("Applying decorations to %p", Window);
414                         Decorator_UpdateBorderSize(Window);
415                         Window->RealW = Window->BorderL + Window->W + Window->BorderR;
416                         Window->RealH = Window->BorderT + Window->H + Window->BorderB;
417                         bDecoratorRedraw = 1;
418                 }
419                 else
420                 {
421                         Window->BorderL = 0;
422                         Window->BorderR = 0;
423                         Window->BorderT = 0;
424                         Window->BorderB = 0;
425                         Window->RealW = Window->W;
426                         Window->RealH = Window->H;
427                 }
428                 
429                 if( (Window->Flags & WINFLAG_RELATIVE) && Window->Parent )
430                 {
431                         Window->RealX = Window->Parent->X + Window->Parent->BorderL + Window->X;
432                         Window->RealY = Window->Parent->Y + Window->Parent->BorderT + Window->Y;
433                 }
434                 else
435                 {
436                         Window->RealX = Window->X;
437                         Window->RealY = Window->Y;
438                 }
439
440                 Window->Renderer->Redraw(Window);
441                 Window->Flags |= WINFLAG_CLEAN;
442         }
443         
444         // Process children
445         if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
446         {
447                 tWindow *child;
448                 for( child = Window->FirstChild; child; child = child->NextSibling )
449                 {
450                         WM_int_UpdateWindow(child);
451                 }
452                 Window->Flags |= WINFLAG_CHILDCLEAN;
453         }
454         
455         if( bDecoratorRedraw )
456                 Decorator_Redraw(Window);
457 }
458
459 void WM_int_BlitWindow(tWindow *Window)
460 {
461         tWindow *child;
462
463         // Ignore hidden windows
464         if( !(Window->Flags & WINFLAG_SHOW) )
465                 return ;
466
467 //      _SysDebug("Blit %p (%p) to (%i,%i) %ix%i", Window, Window->RenderBuffer,
468 //              Window->RealX, Window->RealY, Window->RealW, Window->RealH);
469         Video_Blit(Window->RenderBuffer, Window->RealX, Window->RealY, Window->RealW, Window->RealH);
470         
471         if( Window == gpWM_FocusedWindow && Window->CursorW )
472         {
473                 Video_FillRect(
474                         Window->RealX + Window->BorderL + Window->CursorX,
475                         Window->RealY + Window->BorderT + Window->CursorY,
476                         Window->CursorW, Window->CursorH,
477                         0x000000
478                         );
479         }
480
481         for( child = Window->FirstChild; child; child = child->NextSibling )
482         {
483                 WM_int_BlitWindow(child);
484         }
485 }
486
487 void WM_Update(void)
488 {
489         // Don't redraw if nothing has changed
490         if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
491                 return ;        
492
493         // - Iterate through visible windows, updating them as needed
494         WM_int_UpdateWindow(gpWM_RootWindow);
495         
496         // - Draw windows from back to front to the render buffer
497         WM_int_BlitWindow(gpWM_RootWindow);
498
499         Video_Update();
500 }
501

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