2 Simple DirectMedia Layer
3 Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "../../SDL_internal.h"
23 #if SDL_VIDEO_DRIVER_DIRECTFB
25 #include "SDL_DirectFB_video.h"
26 #include "SDL_DirectFB_window.h"
28 #include "../../events/SDL_windowevents_c.h"
30 #define COLOR_EXPAND(col) col.r, col.g, col.b, col.a
32 static DFB_Theme theme_std = {
39 "/usr/share/fonts/truetype/freefont/FreeSans.ttf",
44 static DFB_Theme theme_none = {
55 DrawTriangle(IDirectFBSurface * s, int down, int x, int y, int w)
75 s->FillTriangle(s, x1, y1, x2, y2, x3, y3);
79 LoadFont(_THIS, SDL_Window * window)
81 SDL_DFB_DEVICEDATA(_this);
82 SDL_DFB_WINDOWDATA(window);
84 if (windata->font != NULL) {
85 SDL_DFB_RELEASE(windata->font);
87 SDL_DFB_CHECK(windata->window_surface->SetFont(windata->window_surface, windata->font));
90 if (windata->theme.font != NULL)
92 DFBFontDescription fdesc;
95 fdesc.flags = DFDESC_HEIGHT;
96 fdesc.height = windata->theme.font_size;
97 SDL_DFB_CHECK(devdata->
98 dfb->CreateFont(devdata->dfb, windata->theme.font,
99 &fdesc, &windata->font));
100 SDL_DFB_CHECK(windata->window_surface->SetFont(windata->window_surface, windata->font));
105 DrawCraption(_THIS, IDirectFBSurface * s, int x, int y, char *text)
107 DFBSurfaceTextFlags flags;
109 flags = DSTF_CENTER | DSTF_TOP;
111 s->DrawString(s, text, -1, x, y, flags);
115 DirectFB_WM_RedrawLayout(_THIS, SDL_Window * window)
117 SDL_DFB_WINDOWDATA(window);
118 IDirectFBSurface *s = windata->window_surface;
119 DFB_Theme *t = &windata->theme;
121 int d = (t->caption_size - t->font_size) / 2;
125 if (!windata->is_managed || (window->flags & SDL_WINDOW_FULLSCREEN))
128 SDL_DFB_CHECK(s->SetSrcBlendFunction(s, DSBF_ONE));
129 SDL_DFB_CHECK(s->SetDstBlendFunction(s, DSBF_ZERO));
130 SDL_DFB_CHECK(s->SetDrawingFlags(s, DSDRAW_NOFX));
131 SDL_DFB_CHECK(s->SetBlittingFlags(s, DSBLIT_NOFX));
133 LoadFont(_this, window);
134 /* s->SetDrawingFlags(s, DSDRAW_BLEND); */
135 s->SetColor(s, COLOR_EXPAND(t->frame_color));
137 for (i = 0; i < t->top_size; i++)
138 s->DrawLine(s, 0, i, windata->size.w, i);
140 for (i = windata->size.h - t->bottom_size; i < windata->size.h; i++)
141 s->DrawLine(s, 0, i, windata->size.w, i);
143 for (i = 0; i < t->left_size; i++)
144 s->DrawLine(s, i, 0, i, windata->size.h);
146 for (i = windata->size.w - t->right_size; i < windata->size.w; i++)
147 s->DrawLine(s, i, 0, i, windata->size.h);
149 s->SetColor(s, COLOR_EXPAND(t->caption_color));
150 s->FillRectangle(s, t->left_size, t->top_size, windata->client.w,
154 x = windata->size.w - t->right_size - w + d;
156 s->SetColor(s, COLOR_EXPAND(t->close_color));
157 DrawTriangle(s, 1, x, y, w - 2 * d);
159 s->SetColor(s, COLOR_EXPAND(t->max_color));
160 DrawTriangle(s, window->flags & SDL_WINDOW_MAXIMIZED ? 1 : 0, x - w,
164 if (*window->title) {
165 s->SetColor(s, COLOR_EXPAND(t->font_color));
166 DrawCraption(_this, s, (x - w) / 2, t->top_size + d, window->title);
172 dr.x = t->left_size + d;
173 dr.y = t->top_size + d;
176 s->SetBlittingFlags(s, DSBLIT_BLEND_ALPHACHANNEL);
178 s->StretchBlit(s, windata->icon, NULL, &dr);
180 windata->wm_needs_redraw = 0;
184 DirectFB_WM_GetClientSize(_THIS, SDL_Window * window, int *cw, int *ch)
186 SDL_DFB_WINDOWDATA(window);
187 IDirectFBWindow *dfbwin = windata->dfbwin;
189 SDL_DFB_CHECK(dfbwin->GetSize(dfbwin, cw, ch));
190 dfbwin->GetSize(dfbwin, cw, ch);
191 *cw -= windata->theme.left_size + windata->theme.right_size;
193 windata->theme.top_size + windata->theme.caption_size +
194 windata->theme.bottom_size;
199 DirectFB_WM_AdjustWindowLayout(SDL_Window * window, int flags, int w, int h)
201 SDL_DFB_WINDOWDATA(window);
203 if (!windata->is_managed)
204 windata->theme = theme_none;
205 else if (flags & SDL_WINDOW_BORDERLESS)
206 /* desc.caps |= DWCAPS_NODECORATION;) */
207 windata->theme = theme_none;
208 else if (flags & SDL_WINDOW_FULLSCREEN) {
209 windata->theme = theme_none;
210 } else if (flags & SDL_WINDOW_MAXIMIZED) {
211 windata->theme = theme_std;
212 windata->theme.left_size = 0;
213 windata->theme.right_size = 0;
214 windata->theme.top_size = 0;
215 windata->theme.bottom_size = 0;
217 windata->theme = theme_std;
220 windata->client.x = windata->theme.left_size;
221 windata->client.y = windata->theme.top_size + windata->theme.caption_size;
222 windata->client.w = w;
223 windata->client.h = h;
225 w + windata->theme.left_size + windata->theme.right_size;
227 h + windata->theme.top_size +
228 windata->theme.caption_size + windata->theme.bottom_size;
235 WM_POS_CAPTION = 0x01,
241 WM_POS_BOTTOM = 0x40,
245 WMIsClient(DFB_WindowData * p, int x, int y)
251 if (x >= p->client.w || y >= p->client.h)
257 WMPos(DFB_WindowData * p, int x, int y)
259 int pos = WM_POS_NONE;
261 if (!WMIsClient(p, x, y)) {
262 if (y < p->theme.top_size) {
264 } else if (y < p->client.y) {
266 p->size.w - p->theme.right_size - 2 * p->theme.caption_size) {
267 pos |= WM_POS_CAPTION;
269 p->size.w - p->theme.right_size -
270 p->theme.caption_size) {
275 } else if (y >= p->size.h - p->theme.bottom_size) {
276 pos |= WM_POS_BOTTOM;
278 if (x < p->theme.left_size) {
280 } else if (x >= p->size.w - p->theme.right_size) {
288 DirectFB_WM_ProcessEvent(_THIS, SDL_Window * window, DFBWindowEvent * evt)
290 SDL_DFB_DEVICEDATA(_this);
291 SDL_DFB_WINDOWDATA(window);
292 DFB_WindowData *gwindata = ((devdata->grabbed_window) ? (DFB_WindowData *) ((devdata->grabbed_window)->driverdata) : NULL);
293 IDirectFBWindow *dfbwin = windata->dfbwin;
294 DFBWindowOptions wopts;
296 if (!windata->is_managed)
299 SDL_DFB_CHECK(dfbwin->GetOptions(dfbwin, &wopts));
302 case DWET_BUTTONDOWN:
303 if (evt->buttons & DIBM_LEFT) {
304 int pos = WMPos(windata, evt->x, evt->y);
309 windata->wm_grab = WM_POS_NONE;
310 SDL_SendWindowEvent(window, SDL_WINDOWEVENT_CLOSE, 0,
314 windata->wm_grab = WM_POS_NONE;
315 if (window->flags & SDL_WINDOW_MAXIMIZED) {
316 SDL_RestoreWindow(window);
318 SDL_MaximizeWindow(window);
322 if (!(wopts & DWOP_KEEP_STACKING)) {
323 DirectFB_RaiseWindow(_this, window);
325 if (window->flags & SDL_WINDOW_MAXIMIZED)
329 windata->wm_grab = pos;
330 if (gwindata != NULL)
331 SDL_DFB_CHECK(gwindata->dfbwin->UngrabPointer(gwindata->dfbwin));
332 SDL_DFB_CHECK(dfbwin->GrabPointer(dfbwin));
333 windata->wm_lastx = evt->cx;
334 windata->wm_lasty = evt->cy;
339 if (!windata->wm_grab)
341 if (!(evt->buttons & DIBM_LEFT)) {
342 if (windata->wm_grab & (WM_POS_RIGHT | WM_POS_BOTTOM)) {
343 int dx = evt->cx - windata->wm_lastx;
344 int dy = evt->cy - windata->wm_lasty;
346 if (!(wopts & DWOP_KEEP_SIZE)) {
348 if ((windata->wm_grab & (WM_POS_BOTTOM | WM_POS_RIGHT)) == WM_POS_BOTTOM)
350 else if ((windata->wm_grab & (WM_POS_BOTTOM | WM_POS_RIGHT)) == WM_POS_RIGHT)
352 SDL_DFB_CHECK(dfbwin->GetSize(dfbwin, &cw, &ch));
354 /* necessary to trigger an event - ugly */
355 SDL_DFB_CHECK(dfbwin->DisableEvents(dfbwin, DWET_ALL));
356 SDL_DFB_CHECK(dfbwin->Resize(dfbwin, cw + dx + 1, ch + dy));
357 SDL_DFB_CHECK(dfbwin->EnableEvents(dfbwin, DWET_ALL));
359 SDL_DFB_CHECK(dfbwin->Resize(dfbwin, cw + dx, ch + dy));
362 SDL_DFB_CHECK(dfbwin->UngrabPointer(dfbwin));
363 if (gwindata != NULL)
364 SDL_DFB_CHECK(gwindata->dfbwin->GrabPointer(gwindata->dfbwin));
365 windata->wm_grab = WM_POS_NONE;
370 if (!windata->wm_grab)
372 if (evt->buttons & DIBM_LEFT) {
373 int dx = evt->cx - windata->wm_lastx;
374 int dy = evt->cy - windata->wm_lasty;
376 if (windata->wm_grab & WM_POS_CAPTION) {
377 if (!(wopts & DWOP_KEEP_POSITION))
378 SDL_DFB_CHECK(dfbwin->Move(dfbwin, dx, dy));
380 if (windata->wm_grab & (WM_POS_RIGHT | WM_POS_BOTTOM)) {
381 if (!(wopts & DWOP_KEEP_SIZE)) {
384 /* Make sure all events are disabled for this operation ! */
385 SDL_DFB_CHECK(dfbwin->DisableEvents(dfbwin, DWET_ALL));
387 if ((windata->wm_grab & (WM_POS_BOTTOM | WM_POS_RIGHT)) == WM_POS_BOTTOM)
389 else if ((windata->wm_grab & (WM_POS_BOTTOM | WM_POS_RIGHT)) == WM_POS_RIGHT)
392 SDL_DFB_CHECK(dfbwin->GetSize(dfbwin, &cw, &ch));
393 SDL_DFB_CHECK(dfbwin->Resize(dfbwin, cw + dx, ch + dy));
395 SDL_DFB_CHECK(dfbwin->EnableEvents(dfbwin, DWET_ALL));
398 windata->wm_lastx = evt->cx;
399 windata->wm_lasty = evt->cy;
413 #endif /* SDL_VIDEO_DRIVER_DIRECTFB */