comparison src/video/directfb/SDL_DirectFB_WM.c @ 3023:d72a0dd80e8b

DirectFB cleanups & simple window manager - use SDL_getenv, not getenv ... - no more support for 0.9.25 - not even mentioned any longer on directfb.org - fix fullscreen issues - add a simple window manager unless the directfb team comes up with a working wm. The driver has support for a very, very basic window manager you may want to use when runnning with "wm=default". Use export SDL_DIRECTFB_WM=1 to enable basic window borders including icon support. In order to have the window title rendered, you need to have the following font installed: /usr/share/fonts/truetype/freefont/FreeSans.ttf
author Couriersud <couriersud@arcor.de>
date Sun, 11 Jan 2009 23:49:23 +0000
parents
children 62d4992e5a92
comparison
equal deleted inserted replaced
3022:db20dde98dd3 3023:d72a0dd80e8b
1 /*
2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 Sam Lantinga
20 slouken@libsdl.org
21 */
22 #include "SDL_config.h"
23
24 //#include "SDL_syswm.h"
25 //#include "../SDL_sysvideo.h"
26 //#include "../../events/SDL_keyboard_c.h"
27
28 #include "SDL_DirectFB_video.h"
29
30 #define COLOR_EXPAND(col) col.r, col.g, col.b, col.a
31
32 static DFB_Theme theme_std = {
33 4, 4, 8, 8,
34 {255, 200, 200, 200},
35 24,
36 {255, 0, 0, 255},
37 16,
38 {255, 255, 255, 255},
39 "/usr/share/fonts/truetype/freefont/FreeSans.ttf",
40 {255, 255, 0, 0},
41 {255, 255, 255, 0},
42 };
43
44 static DFB_Theme theme_none = {
45 0, 0, 0, 0,
46 {0, 0, 0, 0},
47 0,
48 {0, 0, 0, 0},
49 0,
50 {0, 0, 0, 0},
51 NULL
52 };
53
54 static void
55 DrTriangle(IDirectFBSurface * s, int down, int x, int y, int w)
56 {
57 int x1, x2, x3;
58 int y1, y2, y3;
59
60 if (down) {
61 x1 = x + w / 2;
62 x2 = x;
63 x3 = x + w;
64 y1 = y + w;
65 y2 = y;
66 y3 = y;
67 } else {
68 x1 = x + w / 2;
69 x2 = x;
70 x3 = x + w;
71 y1 = y;
72 y2 = y + w;
73 y3 = y + w;
74 }
75 s->FillTriangle(s, x1, y1, x2, y2, x3, y3);
76 }
77
78 static void
79 DrCaption(IDirectFBSurface * s, int x, int y, char *text)
80 {
81 DFBSurfaceTextFlags flags;
82
83 flags = DSTF_CENTER | DSTF_TOP;
84
85 s->DrawString(s, text, -1, x, y, flags);
86 }
87
88 void
89 DirectFB_WM_RedrawLayout(SDL_Window * window)
90 {
91 SDL_DFB_WINDOWDATA(window);
92 IDirectFBSurface *s = windata->window_surface;
93 DFB_Theme *t = &windata->theme;
94 int i;
95 int d = (t->caption_size - t->font_size) / 2;
96 int x, y, w;
97
98
99 if (!windata->is_managed || (window->flags & SDL_WINDOW_FULLSCREEN))
100 return;
101
102 //s->SetDrawingFlags(s, DSDRAW_BLEND);
103 s->SetColor(s, COLOR_EXPAND(t->frame_color));
104 /* top */
105 for (i = 0; i < t->top_size; i++)
106 s->DrawLine(s, 0, i, windata->size.w, i);
107 /* bottom */
108 for (i = windata->size.h - t->bottom_size; i < windata->size.h; i++)
109 s->DrawLine(s, 0, i, windata->size.w, i);
110 /* left */
111 for (i = 0; i < t->left_size; i++)
112 s->DrawLine(s, i, 0, i, windata->size.h);
113 /* right */
114 for (i = windata->size.w - t->right_size; i < windata->size.w; i++)
115 s->DrawLine(s, i, 0, i, windata->size.h);
116 /* Caption */
117 s->SetColor(s, COLOR_EXPAND(t->caption_color));
118 s->FillRectangle(s, t->left_size, t->top_size, windata->client.w,
119 t->caption_size);
120 /* Close Button */
121 w = t->caption_size;
122 x = windata->size.w - t->right_size - w + d;
123 y = t->top_size + d;
124 s->SetColor(s, COLOR_EXPAND(t->close_color));
125 DrTriangle(s, 1, x, y, w - 2 * d);
126 /* Max Button */
127 s->SetColor(s, COLOR_EXPAND(t->max_color));
128 DrTriangle(s, window->flags & SDL_WINDOW_MAXIMIZED ? 1 : 0, x - w,
129 y, w - 2 * d);
130
131 /* Caption */
132 if (window->title) {
133 s->SetColor(s, COLOR_EXPAND(t->font_color));
134 DrCaption(s, (x - w) / 2, t->top_size + d, window->title);
135 }
136 /* Icon */
137 if (windata->icon) {
138 DFBRectangle dr;
139
140 dr.x = t->left_size + d;
141 dr.y = t->top_size + d;
142 dr.w = w - 2 * d;
143 dr.h = w - 2 * d;
144 s->SetBlittingFlags(s, DSBLIT_BLEND_ALPHACHANNEL);
145
146 s->StretchBlit(s, windata->icon, NULL, &dr);
147 }
148 windata->wm_needs_redraw = 0;
149 }
150
151 DFBResult
152 DirectFB_WM_GetClientSize(_THIS, SDL_Window * window, int *cw, int *ch)
153 {
154 SDL_DFB_WINDOWDATA(window);
155 DFBResult ret;
156
157 ret = windata->window->GetSize(windata->window, cw, ch);
158 *cw -= windata->theme.left_size + windata->theme.right_size;
159 *ch -=
160 windata->theme.top_size + windata->theme.caption_size +
161 windata->theme.bottom_size;
162 return ret;
163 }
164
165 void
166 DirectFB_WM_AdjustWindowLayout(SDL_Window * window)
167 {
168 SDL_DFB_WINDOWDATA(window);
169
170 if (!windata->is_managed)
171 windata->theme = theme_none;
172 else if (window->flags & SDL_WINDOW_FULLSCREEN) {
173 windata->theme = theme_none;
174 } else if (window->flags & SDL_WINDOW_MAXIMIZED) {
175 windata->theme = theme_std;
176 windata->theme.left_size = 0;
177 windata->theme.right_size = 0;
178 windata->theme.top_size = 0;
179 windata->theme.bottom_size = 0;
180 } else {
181 windata->theme = theme_std;
182 }
183
184 windata->client.x = windata->theme.left_size;
185 windata->client.y = windata->theme.top_size + windata->theme.caption_size;
186 windata->client.w = window->w;
187 windata->client.h = window->h;
188 windata->size.w =
189 window->w + windata->theme.left_size + windata->theme.right_size;
190 windata->size.h =
191 window->h + windata->theme.top_size +
192 windata->theme.caption_size + windata->theme.bottom_size;
193 }
194
195 void
196 DirectFB_WM_MaximizeWindow(_THIS, SDL_Window * window)
197 {
198 SDL_DFB_WINDOWDATA(window);
199 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
200
201 windata->window->GetPosition(windata->window,
202 &windata->restore.x,
203 &windata->restore.y);
204 windata->window->GetSize(windata->window, &windata->restore.w,
205 &windata->restore.h);
206
207 /* Do this already here */
208 window->flags |= SDL_WINDOW_MAXIMIZED;
209 DirectFB_WM_AdjustWindowLayout(window);
210
211 windata->window->MoveTo(windata->window, 0, 0);
212 windata->window->Resize(windata->window,
213 display->current_mode.w,
214 display->current_mode.h);
215 SDL_SendWindowEvent(windata->sdl_id, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
216 }
217
218 void
219 DirectFB_WM_RestoreWindow(_THIS, SDL_Window * window)
220 {
221 SDL_DFB_WINDOWDATA(window);
222
223 /* Do this already here */
224 //window->flags &= ~(SDL_WINDOW_MAXIMIZED | SDL_WINDOW_MINIMIZED);
225
226 DirectFB_WM_AdjustWindowLayout(window);
227 windata->window->MoveTo(windata->window, windata->restore.x,
228 windata->restore.y);
229 windata->window->Resize(windata->window, windata->restore.w,
230 windata->restore.h);
231 SDL_SendWindowEvent(windata->sdl_id, SDL_WINDOWEVENT_RESTORED, 0, 0);
232 }
233
234 enum
235 {
236 WM_POS_NONE = 0x00,
237 WM_POS_CAPTION = 0x01,
238 WM_POS_CLOSE = 0x02,
239 WM_POS_MAX = 0x04,
240 WM_POS_LEFT = 0x08,
241 WM_POS_RIGHT = 0x10,
242 WM_POS_TOP = 0x20,
243 WM_POS_BOTTOM = 0x40,
244 };
245
246 static int
247 WMIsClient(DFB_WindowData * p, int x, int y)
248 {
249 x -= p->client.x;
250 y -= p->client.y;
251 if (x < 0 || y < 0)
252 return 0;
253 if (x >= p->client.w || y >= p->client.h)
254 return 0;
255 return 1;
256 }
257
258 static int
259 WMPos(DFB_WindowData * p, int x, int y)
260 {
261 int pos = WM_POS_NONE;
262
263 if (!WMIsClient(p, x, y)) {
264 if (y < p->theme.top_size) {
265 pos |= WM_POS_TOP;
266 } else if (y < p->client.y) {
267 if (x <
268 p->size.w - p->theme.right_size - 2 * p->theme.caption_size) {
269 pos |= WM_POS_CAPTION;
270 } else if (x <
271 p->size.w - p->theme.right_size -
272 p->theme.caption_size) {
273 pos |= WM_POS_MAX;
274 } else {
275 pos |= WM_POS_CLOSE;
276 }
277 } else if (y >= p->size.h - p->theme.bottom_size) {
278 pos |= WM_POS_BOTTOM;
279 }
280 if (x < p->theme.left_size) {
281 pos |= WM_POS_LEFT;
282 } else if (x >= p->size.w - p->theme.right_size) {
283 pos |= WM_POS_RIGHT;
284 }
285 }
286 return pos;
287 }
288
289 static int wm_grab;
290 static int wm_lastx;
291 static int wm_lasty;
292
293 int
294 DirectFB_WM_ProcessEvent(_THIS, SDL_Window *window, DFBWindowEvent * evt)
295 {
296 SDL_DFB_WINDOWDATA(window);
297
298 if (!windata->is_managed)
299 return 0;
300
301 switch (evt->type) {
302 case DWET_BUTTONDOWN:
303 if (evt->buttons & DIBM_LEFT) {
304 int pos = WMPos(windata, evt->x, evt->y);
305 switch (pos) {
306 case WM_POS_NONE:
307 return 0;
308 case WM_POS_CLOSE:
309 SDL_SendWindowEvent(windata->sdl_id, SDL_WINDOWEVENT_CLOSE, 0, 0);
310 return 1;
311 case WM_POS_MAX:
312 if (window->flags & SDL_WINDOW_MAXIMIZED) {
313 DirectFB_WM_RestoreWindow(_this, window);
314 } else {
315 DirectFB_WM_MaximizeWindow(_this, window);
316 }
317 return 1;
318 default:
319 wm_grab = pos;
320 windata->window->GrabPointer(windata->window);
321 wm_lastx = evt->cx;
322 wm_lasty = evt->cy;
323 }
324 }
325 return 1;
326 case DWET_BUTTONUP:
327 break;
328 case DWET_MOTION:
329 if (!wm_grab)
330 return 0;
331 if (evt->buttons & DIBM_LEFT) {
332 int dx = evt->cx - wm_lastx;
333 int dy = evt->cy - wm_lasty;
334 int cw, ch;
335
336 if (wm_grab & WM_POS_CAPTION)
337 windata->window->Move(windata->window, dx, dy);
338 if (wm_grab & WM_POS_RIGHT) {
339 windata->window->GetSize(windata->window, &cw, &ch);
340 windata->window->Resize(windata->window, cw + dx, ch);
341 }
342 if (wm_grab & WM_POS_BOTTOM) {
343 windata->window->GetSize(windata->window, &cw, &ch);
344 windata->window->Resize(windata->window, cw, ch + dy);
345 }
346 wm_lastx = evt->cx;
347 wm_lasty = evt->cy;
348 return 1;
349 }
350 windata->window->UngrabPointer(windata->window);
351 wm_grab = WM_POS_NONE;
352 break;
353 case DWET_KEYDOWN:
354 break;
355 case DWET_KEYUP:
356 break;
357 default:
358 ;
359 }
360 return 0;
361 }