limited minimize support
[sdl_omap.git] / src / video / omapdss / sdlif.c
CommitLineData
9a8e84f8 1/*
0c7caf2b 2 * (C) GraÅžvydas "notaz" Ignotas, 2010-2012
9a8e84f8 3 *
4 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
5 * See the COPYING file in the top-level directory.
6 */
7
b9a19b44 8#include <stdio.h>
9a8e84f8 9#include <stdlib.h>
090bcb85 10#include <X11/XF86keysym.h>
9a8e84f8 11
12#include "../SDL_sysvideo.h"
13#include "../SDL_pixels_c.h"
14#include "../../events/SDL_events_c.h"
f9b3f440 15
16#include "linux/xenv.h"
9ab462cd 17#include "osdl.h"
9a8e84f8 18
19
9a8e84f8 20static int omap_available(void)
21{
22 trace();
23 return 1;
24}
25
26static void omap_free(SDL_VideoDevice *device)
27{
28 trace();
29 free(device);
30}
31
32static int omap_VideoInit(SDL_VideoDevice *this, SDL_PixelFormat *vformat)
33{
b9a19b44 34 const char *tmp;
35 int w, h, ret;
36
9a8e84f8 37 trace();
38
39 // default to 16bpp
40 vformat->BitsPerPixel = 16;
41
42 omapsdl_input_init();
5f4b1fd3 43 omapsdl_config(this->hidden);
9a8e84f8 44
b9a19b44 45 tmp = getenv("SDL_OMAP_DEFAULT_MODE");
46 if (tmp != NULL && sscanf(tmp, "%dx%d", &w, &h) == 2) {
47 this->info.current_w = w;
48 this->info.current_h = h;
49 }
50 else if (osdl_video_detect_screen(this->hidden) == 0) {
5f4b1fd3 51 this->info.current_w = this->hidden->phys_w;
52 this->info.current_h = this->hidden->phys_h;
b9a19b44 53 }
54
5d957fa5 55 this->handles_any_size = 1;
b9a19b44 56 this->info.hw_available = 1;
57
9a8e84f8 58 return 0;
59}
60
61static void omap_VideoQuit(SDL_VideoDevice *this)
62{
63 trace();
64
f641fccb 65 osdl_video_finish(this->hidden);
9a8e84f8 66 this->screen->pixels = NULL;
17e19802 67 omapsdl_input_finish();
9a8e84f8 68}
69
70static SDL_Rect **omap_ListModes(SDL_VideoDevice *this, SDL_PixelFormat *format, Uint32 flags)
71{
5d957fa5 72 static SDL_Rect omap_mode_max = {
73 /* with handles_any_size, should accept anything up to this
74 * XXX: possibly set this dynamically based on free vram? */
75 0, 0, 1600, 1200
9a8e84f8 76 };
b9a19b44 77 /* broken API needs this stupidity */
9a8e84f8 78 static SDL_Rect *omap_modes[] = {
5d957fa5 79 &omap_mode_max,
9a8e84f8 80 NULL
81 };
82
83 trace();
84
85 if (format->BitsPerPixel <= 8)
86 // not (yet?) supported
87 return NULL;
88
89 return omap_modes;
90}
91
92static SDL_Surface *omap_SetVideoMode(SDL_VideoDevice *this, SDL_Surface *current, int width,
93 int height, int bpp, Uint32 flags)
94{
5f4b1fd3 95 struct SDL_PrivateVideoData *pdata = this->hidden;
3576a5ac 96 SDL_PixelFormat *format;
0bb19c41 97 Uint32 unhandled_flags;
0c7caf2b 98 int doublebuf;
455c8c43 99 void *fbmem;
3576a5ac 100
9a8e84f8 101 trace("%d, %d, %d, %08x", width, height, bpp, flags);
102
5f4b1fd3 103 omapsdl_config_from_env(pdata);
0bb19c41 104
3576a5ac 105 switch (bpp) {
106 case 16:
107 format = SDL_ReallocFormat(current, 16, 0xf800, 0x07e0, 0x001f, 0);
108 break;
109 case 24:
110 format = SDL_ReallocFormat(current, 24, 0xff0000, 0xff00, 0xff, 0);
111 break;
112 case 32:
5848b0cd 113 format = SDL_ReallocFormat(current, 32, 0xff0000, 0xff00, 0xff, 0);
3576a5ac 114 break;
115 default:
0bb19c41 116 err("SetVideoMode: bpp %d not supported", bpp);
3576a5ac 117 return NULL;
118 }
119 if (format == NULL)
f641fccb 120 return NULL;
9a8e84f8 121
5f4b1fd3 122 if (!(flags & SDL_DOUBLEBUF) && pdata->cfg_force_doublebuf) {
0bb19c41 123 log("forcing SDL_DOUBLEBUF");
124 flags |= SDL_DOUBLEBUF;
125 }
126
455c8c43 127 if (pdata->border_l | pdata->border_r | pdata->border_t | pdata->border_b) {
128 if (pdata->border_l + pdata->border_r >= width
129 || pdata->border_t + pdata->border_b >= height)
130 {
131 err("specified border too large, ignoring");
132 pdata->border_l = pdata->border_r = pdata->border_t = pdata->border_b = 0;
133 }
134 }
135
0c7caf2b 136 doublebuf = (flags & SDL_DOUBLEBUF) ? 1 : 0;
455c8c43 137 fbmem = osdl_video_set_mode(pdata,
138 pdata->border_l, pdata->border_r, pdata->border_t, pdata->border_b,
090bcb85 139 width, height, bpp, &doublebuf, this->wm_title);
455c8c43 140 if (fbmem == NULL) {
0c7caf2b 141 err("failing on mode %dx%d@%d, doublebuf %s, border %d,%d,%d,%d",
142 width, height, bpp, (flags & SDL_DOUBLEBUF) ? "on" : "off",
143 pdata->border_l, pdata->border_r, pdata->border_t, pdata->border_b);
9a8e84f8 144 return NULL;
455c8c43 145 }
0c7caf2b 146 if ((flags & SDL_DOUBLEBUF) && !doublebuf) {
147 log("doublebuffering could not be set\n");
148 flags &= ~SDL_DOUBLEBUF;
149 }
9a8e84f8 150
0bb19c41 151 flags |= SDL_FULLSCREEN | SDL_HWSURFACE;
152 unhandled_flags = flags & ~(SDL_FULLSCREEN | SDL_HWSURFACE | SDL_DOUBLEBUF);
153 if (unhandled_flags != 0) {
154 log("dropping unhandled flags: %08x", unhandled_flags);
155 flags &= ~unhandled_flags;
156 }
157
158 current->flags = flags;
9a8e84f8 159 current->w = width;
160 current->h = height;
161 current->pitch = SDL_CalculatePitch(current);
455c8c43 162 current->pixels = fbmem;
9225e894 163 pdata->app_uses_flip = 0;
9a8e84f8 164
5f4b1fd3 165 if (pdata->layer_w != 0 && pdata->layer_h != 0) {
455c8c43 166 int v_width = width - (pdata->border_l + pdata->border_r);
167 int v_height = height - (pdata->border_t + pdata->border_b);
168 pdata->ts_xmul = (v_width << 16) / pdata->layer_w;
169 pdata->ts_ymul = (v_height << 16) / pdata->layer_h;
5f4b1fd3 170 }
171
9a8e84f8 172 return current;
173}
174
9a8e84f8 175static int omap_LockHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
176{
177 trace("%p", surface);
178
179 return 0;
180}
181
182static void omap_UnlockHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
183{
184 trace("%p", surface);
185}
186
187static int omap_FlipHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
188{
9225e894 189 struct SDL_PrivateVideoData *pdata = this->hidden;
190
9a8e84f8 191 trace("%p", surface);
192
9225e894 193 surface->pixels = osdl_video_flip(pdata);
194 pdata->app_uses_flip = 1;
9a8e84f8 195
196 return 0;
197}
198
37299201 199/* we can't do hw surfaces (besides screen one) yet */
200static int omap_AllocHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
201{
202 trace("%p", surface);
203 return -1;
204}
205
206static void omap_FreeHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
207{
208 trace("%p", surface);
209}
210
9a8e84f8 211static int omap_SetColors(SDL_VideoDevice *this, int firstcolor, int ncolors, SDL_Color *colors)
212{
213 trace("%d, %d, %p", firstcolor, ncolors, colors);
214 return 0;
215}
216
217static void omap_UpdateRects(SDL_VideoDevice *this, int nrects, SDL_Rect *rects)
218{
9225e894 219 struct SDL_PrivateVideoData *pdata = this->hidden;
220
9a8e84f8 221 trace("%d, %p", nrects, rects);
222
9225e894 223 /* for doublebuf forcing on apps */
224 if (nrects == 1 && rects->x == 0 && rects->y == 0
225 && !pdata->app_uses_flip && (this->screen->flags & SDL_DOUBLEBUF)
226 && rects->w == this->screen->w && rects->h == this->screen->h)
0bb19c41 227 {
9225e894 228 this->screen->pixels = osdl_video_flip(pdata);
9a8e84f8 229 }
9a8e84f8 230}
231
232static void omap_InitOSKeymap(SDL_VideoDevice *this)
233{
234 trace();
235}
236
b260fb56 237static int key_event_cb(void *cb_arg, int sdl_kc, int sdl_sc, int is_pressed)
9a8e84f8 238{
239 SDL_keysym keysym = { 0, };
240
241 keysym.sym = sdl_kc;
b260fb56 242 keysym.scancode = sdl_sc;
9a8e84f8 243 SDL_PrivateKeyboard(is_pressed, &keysym);
244}
245
5f4b1fd3 246/* clamp x to min..max-1 */
247#define clamp(x, min, max) \
248 if (x < (min)) x = min; \
249 if (x >= (max)) x = max
250
e1837b2c 251static void translate_mouse(SDL_VideoDevice *this, int *x, int *y)
17e19802 252{
5f4b1fd3 253 struct SDL_PrivateVideoData *pdata = this->hidden;
5f4b1fd3 254
255 if (!pdata->cfg_no_ts_translate && pdata->layer_w != 0 && pdata->layer_h != 0) {
e1837b2c 256 *x = pdata->border_l + ((*x - pdata->layer_x) * pdata->ts_xmul >> 16);
257 *y = pdata->border_t + ((*y - pdata->layer_y) * pdata->ts_ymul >> 16);
258 clamp(*x, 0, this->screen->w);
259 clamp(*y, 0, this->screen->h);
5f4b1fd3 260 }
e1837b2c 261}
17e19802 262
e1837b2c 263static int ts_event_cb(void *cb_arg, int x, int y, unsigned int pressure)
264{
265 static int was_pressed;
266 SDL_VideoDevice *this = cb_arg;
267 struct SDL_PrivateVideoData *pdata = this->hidden;
268
269 translate_mouse(this, &x, &y);
17e19802 270
271 pressure = !!pressure;
272 if (pressure != was_pressed) {
e1837b2c 273 SDL_PrivateMouseButton(pressure ? SDL_PRESSED : SDL_RELEASED, 1, x, y);
17e19802 274 was_pressed = pressure;
275 }
e1837b2c 276 else
277 SDL_PrivateMouseMotion(0, 0, x, y);
278}
279
090bcb85 280static int xmouseb_event_cb(void *cb_arg, int x, int y, int button, int is_pressed)
e1837b2c 281{
282 SDL_VideoDevice *this = cb_arg;
283 struct SDL_PrivateVideoData *pdata = this->hidden;
284
285 translate_mouse(this, &x, &y);
286 SDL_PrivateMouseButton(is_pressed ? SDL_PRESSED : SDL_RELEASED, button, x, y);
287}
288
090bcb85 289static int xmousem_event_cb(void *cb_arg, int x, int y)
e1837b2c 290{
291 SDL_VideoDevice *this = cb_arg;
292 struct SDL_PrivateVideoData *pdata = this->hidden;
293
294 translate_mouse(this, &x, &y);
295 SDL_PrivateMouseMotion(0, 0, x, y);
17e19802 296}
297
090bcb85 298static int xkey_cb(void *cb_arg, int kc, int is_pressed)
299{
300 SDL_VideoDevice *this = cb_arg;
301 struct SDL_PrivateVideoData *pdata = this->hidden;
302 int ret;
303
304 if (kc == XF86XK_MenuKB && is_pressed) {
305 ret = osdl_video_pause(pdata, 1);
306 if (ret == 0) {
307 xenv_minimize();
308 osdl_video_pause(pdata, 0);
309 omapsdl_input_get_events(0, NULL, NULL, NULL);
310 }
311 }
312}
313
9a8e84f8 314static void omap_PumpEvents(SDL_VideoDevice *this)
315{
f9b3f440 316 struct SDL_PrivateVideoData *pdata = this->hidden;
e1837b2c 317 int read_tslib = 1;
f9b3f440 318
9a8e84f8 319 trace();
320
e1837b2c 321 if (pdata->xenv_up) {
322 if (!pdata->cfg_ts_force_tslib) {
090bcb85 323 xenv_update(xkey_cb, xmouseb_event_cb, xmousem_event_cb, this);
e1837b2c 324 if (pdata->xenv_mouse)
325 read_tslib = 0;
326 }
327 else {
328 /* just flush X event queue */
329 xenv_update(NULL, NULL, NULL, NULL);
330 }
331 }
f9b3f440 332
e1837b2c 333 omapsdl_input_get_events(0, key_event_cb,
334 read_tslib ? ts_event_cb : NULL, this);
9a8e84f8 335}
336
337static SDL_VideoDevice *omap_create(int devindex)
338{
339 SDL_VideoDevice *this;
340
341 this = calloc(1, sizeof(*this) + sizeof(*this->hidden));
342 if (this == NULL) {
343 SDL_OutOfMemory();
344 return 0;
345 }
346 this->hidden = (void *)(this + 1);
347 this->VideoInit = omap_VideoInit;
348 this->ListModes = omap_ListModes;
349 this->SetVideoMode = omap_SetVideoMode;
350 this->LockHWSurface = omap_LockHWSurface;
351 this->UnlockHWSurface = omap_UnlockHWSurface;
352 this->FlipHWSurface = omap_FlipHWSurface;
37299201 353 this->AllocHWSurface = omap_AllocHWSurface;
354 this->FreeHWSurface = omap_FreeHWSurface;
9a8e84f8 355 this->SetColors = omap_SetColors;
356 this->UpdateRects = omap_UpdateRects;
357 this->VideoQuit = omap_VideoQuit;
358 this->InitOSKeymap = omap_InitOSKeymap;
359 this->PumpEvents = omap_PumpEvents;
360 this->free = omap_free;
361
362 return this;
363}
364
365VideoBootStrap omapdss_bootstrap = {
366 "omapdss", "OMAP DSS2 Framebuffer Driver",
367 omap_available, omap_create
368};
369