Usermode/AxWin3 - Implemented sending messages to windows
[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
15 // === GLOBALS ===
16 tWMRenderer     *gpWM_Renderers;
17 tWindow *gpWM_RootWindow;
18
19 // === CODE ===
20 void WM_Initialise(void)
21 {
22         WM_CreateWindow(NULL, 0x0088FF, "Background");
23         gpWM_RootWindow->W = giScreenWidth;
24         gpWM_RootWindow->H = giScreenHeight;
25         gpWM_RootWindow->Flags = WINFLAG_SHOW;
26 }
27
28 void WM_RegisterRenderer(tWMRenderer *Renderer)
29 {
30         // TODO: Catch out duplicates
31         Renderer->Next = gpWM_Renderers;
32         gpWM_Renderers = Renderer;
33 }
34
35 // --- Manipulation
36 tWindow *WM_CreateWindow(tWindow *Parent, int RendererArg, const char *RendererName)
37 {
38         tWMRenderer     *renderer;
39         tWindow *ret;
40         
41         // - Get Renderer
42         for( renderer = gpWM_Renderers; renderer; renderer = renderer->Next )
43         {
44                 if(strcmp(RendererName, renderer->Name) == 0)
45                         break;
46         }
47         if(renderer == NULL)
48                 return NULL;
49
50         if(!Parent)
51                 Parent = gpWM_RootWindow;
52
53         // - Call create window function
54         ret = renderer->CreateWindow(RendererArg);
55         ret->Parent = Parent;
56         ret->Renderer = renderer;
57         ret->Flags = WINFLAG_CLEAN;     // Note, not acutally clean, but it makes invaidate work
58
59         // Append to parent
60         if(Parent)
61         {
62                 if(Parent->LastChild)
63                         Parent->LastChild->NextSibling = ret;
64                 else
65                         Parent->FirstChild = ret;
66                 ret->PrevSibling = Parent->LastChild;
67                 Parent->LastChild = ret;
68         }
69         else
70         {
71                 gpWM_RootWindow = ret;
72         }
73
74         // - Return!
75         return ret;
76 }
77
78 tWindow *WM_CreateWindowStruct(size_t ExtraSize)
79 {
80         tWindow *ret;
81         
82         ret = calloc( sizeof(tWindow) + ExtraSize, 1 );
83         ret->RendererInfo = ret + 1;    // Get end of tWindow
84         
85         return ret;
86 }
87
88 void WM_ShowWindow(tWindow *Window, int bShow)
89 {
90         // TODO: Message window
91         if(bShow)
92                 Window->Flags |= WINFLAG_SHOW;
93         else
94                 Window->Flags &= ~WINFLAG_SHOW;
95         WM_Invalidate(Window);
96 }
97
98 int WM_MoveWindow(tWindow *Window, int X, int Y)
99 {
100         // Clip coordinates
101         if(X + Window->W < 0)   X = -Window->W + 1;
102         if(Y + Window->H < 0)   Y = -Window->H + 1;
103         if(X >= giScreenWidth)  X = giScreenWidth - 1;
104         if(Y >= giScreenHeight) Y = giScreenHeight - 1;
105         
106         Window->X = X;  Window->Y = Y;
107
108         WM_Invalidate(Window);
109
110         return 0;
111 }
112
113 int WM_ResizeWindow(tWindow *Window, int W, int H)
114 {
115         if(W <= 0 || H <= 0 )   return 1;
116         if(Window->X + W < 0)   Window->X = -W + 1;
117         if(Window->Y + H < 0)   Window->Y = -H + 1;
118         
119         Window->W = W;  Window->H = H;
120
121         WM_Invalidate(Window);
122
123         {
124                 struct sWndMsg_Resize   msg;
125                 msg.W = W;
126                 msg.H = H;
127                 WM_SendMessage(NULL, Window, WNDMSG_RESIZE, sizeof(msg), &msg);
128         }
129         
130         return 0;
131 }
132
133 int WM_SendMessage(tWindow *Source, tWindow *Dest, int Message, int Length, void *Data)
134 {
135         if(Dest == NULL)        return -2;
136         if(Length > 0 && Data == NULL)  return -1;
137         
138         // ->HandleMessage returns 1 when the message was not handled
139         if( Dest->Renderer->HandleMessage(Dest, Message, Length, Data) != 1 )
140         {
141                 // TODO: Catch errors from ->HandleMessage
142                 return 0;
143         }
144         
145         // TODO: Pass on to user
146         _SysDebug("WM_SendMessage: TODO - Implement sending to client application");
147         
148         return 1;
149 }
150
151 void WM_Invalidate(tWindow *Window)
152 {
153         // Don't invalidate twice (speedup)
154         if( !(Window->Flags & WINFLAG_CLEAN) )  return;
155
156 //      _SysDebug("Invalidating %p");
157         
158         // Mark for re-render
159         Window->Flags &= ~WINFLAG_CLEAN;
160
161         // Mark up the tree that a child window has changed     
162         while( (Window = Window->Parent) )
163         {
164 //              _SysDebug("Childclean removed from %p", Window);
165                 Window->Flags &= ~WINFLAG_CHILDCLEAN;
166         }
167 }
168
169 // --- Rendering / Update
170 void WM_int_UpdateWindow(tWindow *Window)
171 {
172         tWindow *child;
173
174         // Ignore hidden windows
175         if( !(Window->Flags & WINFLAG_SHOW) )
176                 return ;
177         
178         // Render
179         if( !(Window->Flags & WINFLAG_CLEAN) )
180         {
181                 Window->Renderer->Redraw(Window);
182                 Window->Flags |= WINFLAG_CLEAN;
183         }
184         
185         // Process children
186         if( !(Window->Flags & WINFLAG_CHILDCLEAN) )
187         {
188                 for( child = Window->FirstChild; child; child = child->NextSibling )
189                 {
190                         WM_int_UpdateWindow(child);
191                 }
192                 Window->Flags |= WINFLAG_CHILDCLEAN;
193         }
194         
195 }
196
197 void WM_int_BlitWindow(tWindow *Window)
198 {
199         tWindow *child;
200
201         // Ignore hidden windows
202         if( !(Window->Flags & WINFLAG_SHOW) )
203                 return ;
204         
205         Video_Blit(Window->RenderBuffer, Window->X, Window->Y, Window->W, Window->H);
206         
207         for( child = Window->FirstChild; child; child = child->NextSibling )
208         {
209                 WM_int_BlitWindow(child);
210         }
211 }
212
213 void WM_Update(void)
214 {
215         // Don't redraw if nothing has changed
216         if( (gpWM_RootWindow->Flags & WINFLAG_CHILDCLEAN) )
217                 return ;        
218
219         // - Iterate through visible windows, updating them as needed
220         WM_int_UpdateWindow(gpWM_RootWindow);
221         
222         // - Draw windows from back to front to the render buffer
223         WM_int_BlitWindow(gpWM_RootWindow);
224
225         Video_Update();
226 }
227
228 // --- WM Render Routines
229 // TODO: Move to another file?
230 void WM_Render_FillRect(tWindow *Window, int X, int Y, int W, int H, tColour Colour)
231 {
232         uint32_t        *dest;
233          int    i;
234 //      _SysDebug("WM_Render_FilledRect(%p, 0x%x...", Window, Colour);
235 //      _SysDebug(" (%i,%i), %ix%i)", X, Y, W, H);
236         // Clip to window dimensions
237         if(X < 0) { W += X; X = 0; }
238         if(Y < 0) { H += Y; Y = 0; }
239         if(X >= Window->W)      return;
240         if(Y >= Window->H)      return;
241         if(X + W > Window->W)   W = Window->W - X;
242         if(Y + H > Window->H)   H = Window->H - Y;
243 //      _SysDebug(" Clipped to (%i,%i), %ix%i", X, Y, W, H);
244
245         // Render to buffer
246         // Create if needed?
247
248         if(!Window->RenderBuffer) {
249                 Window->RenderBuffer = malloc(Window->W*Window->H*4);
250         }
251
252         dest = (uint32_t*)Window->RenderBuffer + Y*Window->W + X;
253         while( H -- )
254         {
255                 for( i = W; i --; )
256                         *dest++ = Colour;
257                 dest += Window->W - W;
258         }
259 }
260
261 void WM_Render_DrawRect(tWindow *Window, int X, int Y, int W, int H, tColour Colour)
262 {       
263         WM_Render_FillRect(Window, X, Y, W, 1, Colour);
264         WM_Render_FillRect(Window, X, Y+H-1, W, 1, Colour);
265         WM_Render_FillRect(Window, X, Y, 1, H, Colour);
266         WM_Render_FillRect(Window, X+W-1, Y, 1, H, Colour);
267 }
268
269 void WM_Render_DrawText(tWindow *Window, int X, int Y, int W, int H, void *Font, tColour Colour, const char *Text)
270 {
271         // TODO: Implement
272         _SysDebug("WM_Render_DrawText - TODO: Implement");
273 }
274
275 /**
276  * \brief Draw an image to the screen
277  * \todo Maybe have support for an offset in the image
278  */
279 void WM_Render_DrawImage(tWindow *Window, int X, int Y, int W, int H, tImage *Image)
280 {
281          int    x, y;
282         uint32_t        *dest;
283         uint8_t *data;
284         
285         // Sanity please
286         if( !Image )    return ;
287
288         // Allocate
289         if(!Window->RenderBuffer) {
290                 Window->RenderBuffer = malloc(Window->W*Window->H*4);
291         }
292         
293         // Bounds Check
294         if( X >= Window->W )    return ;
295         if( Y >= Window->H )    return ;
296         
297         // Wrap to image size
298         if( W > Image->Width )  W = Image->Width;
299         if( H > Image->Height ) H = Image->Height;
300         
301         // Wrap to screen size
302         if( X + W > Window->W ) W = Window->W - X;
303         if( Y + H > Window->H ) H = Window->H - Y;
304
305         dest = (uint32_t*)Window->RenderBuffer + Y * Window->W + X;
306         data = Image->Data;
307
308         // Do the render
309         switch( Image->Format )
310         {
311         case IMGFMT_BGRA:
312                 for( y = 0; y < H; y ++ )
313                 {
314                          int    r, g, b, a;     // New
315                          int    or, og, ob;     // Original
316                         for( x = 0; x < W; x ++ )
317                         {
318                                 b = data[x*4+0]; g = data[x*4+1]; r = data[x*4+2]; a = data[x*4+3];
319                                 if( a == 0 )    continue;       // 100% transparent
320                                 ob = dest[x]&0xFF; og = (dest[x] >> 8)&0xFF; or = (dest[x] >> 16)&0xFF;
321                                 // Handle Alpha
322                                 switch(a)
323                                 {
324                                 // Transparent: Handled above
325                                 // Solid
326                                 case 0xFF:      break;
327                                 // Half
328                                 case 0x80:
329                                         r = (or + r) / 2;
330                                         g = (og + g) / 2;
331                                         b = (ob + b) / 2;
332                                         break;
333                                 // General
334                                 default:
335                                         r = (or * (255-a) + r * a) / 255;
336                                         g = (og * (255-a) + g * a) / 255;
337                                         b = (ob * (255-a) + b * a) / 255;
338                                         break;
339                                 }
340                                 dest[x] = b | (g << 8) | (r << 16);
341                         }
342                         data += Image->Width * 4;
343                         dest += Window->W;
344                 }
345                 break;
346         
347         // RGB
348         case IMGFMT_RGB:
349                 for( y = 0; y < H; y ++ )
350                 {
351                         for( x = 0; x < W; x ++ )
352                         {
353                                 //        Blue           Green                Red
354                                 dest[x] = data[x*3+2] | (data[x*3+1] << 8) | (data[x*3+0] << 16);
355                         }
356                         data += W * 3;
357                         dest += Window->W;
358                 }
359                 break;
360         default:
361                 _SysDebug("ERROR: Unknown image format %i\n", Image->Format);
362                 break;
363         }
364 }
365

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