2 * (C) GraÅžvydas "notaz" Ignotas, 2010-2012
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.
10 #include <X11/XF86keysym.h>
12 #include "../SDL_sysvideo.h"
13 #include "../SDL_pixels_c.h"
14 #include "../../events/SDL_events_c.h"
16 #include "linux/xenv.h"
20 static int omap_available(void)
26 static void omap_free(SDL_VideoDevice *device)
32 static int omap_VideoInit(SDL_VideoDevice *this, SDL_PixelFormat *vformat)
40 vformat->BitsPerPixel = 16;
43 omapsdl_config(this->hidden);
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;
50 else if (osdl_video_detect_screen(this->hidden) == 0) {
51 this->info.current_w = this->hidden->phys_w;
52 this->info.current_h = this->hidden->phys_h;
55 this->handles_any_size = 1;
56 this->info.hw_available = 1;
61 static void omap_VideoQuit(SDL_VideoDevice *this)
65 osdl_video_finish(this->hidden);
66 this->screen->pixels = NULL;
67 omapsdl_input_finish();
70 static SDL_Rect **omap_ListModes(SDL_VideoDevice *this, SDL_PixelFormat *format, Uint32 flags)
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? */
77 /* broken API needs this stupidity */
78 static SDL_Rect *omap_modes[] = {
85 if (format->BitsPerPixel <= 8)
86 // not (yet?) supported
92 static SDL_Surface *omap_SetVideoMode(SDL_VideoDevice *this, SDL_Surface *current, int width,
93 int height, int bpp, Uint32 flags)
95 struct SDL_PrivateVideoData *pdata = this->hidden;
96 SDL_PixelFormat *format;
97 Uint32 unhandled_flags;
101 trace("%d, %d, %d, %08x", width, height, bpp, flags);
103 omapsdl_config_from_env(pdata);
107 format = SDL_ReallocFormat(current, 16, 0xf800, 0x07e0, 0x001f, 0);
110 format = SDL_ReallocFormat(current, 24, 0xff0000, 0xff00, 0xff, 0);
113 format = SDL_ReallocFormat(current, 32, 0xff0000, 0xff00, 0xff, 0);
116 err("SetVideoMode: bpp %d not supported", bpp);
122 if (!(flags & SDL_DOUBLEBUF) && pdata->cfg_force_doublebuf) {
123 log("forcing SDL_DOUBLEBUF");
124 flags |= SDL_DOUBLEBUF;
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)
131 err("specified border too large, ignoring");
132 pdata->border_l = pdata->border_r = pdata->border_t = pdata->border_b = 0;
136 doublebuf = (flags & SDL_DOUBLEBUF) ? 1 : 0;
137 fbmem = osdl_video_set_mode(pdata,
138 pdata->border_l, pdata->border_r, pdata->border_t, pdata->border_b,
139 width, height, bpp, &doublebuf, this->wm_title);
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);
146 if ((flags & SDL_DOUBLEBUF) && !doublebuf) {
147 log("doublebuffering could not be set\n");
148 flags &= ~SDL_DOUBLEBUF;
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;
158 current->flags = flags;
161 current->pitch = SDL_CalculatePitch(current);
162 current->pixels = fbmem;
163 pdata->app_uses_flip = 0;
165 if (pdata->layer_w != 0 && pdata->layer_h != 0) {
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;
175 static int omap_LockHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
177 trace("%p", surface);
182 static void omap_UnlockHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
184 trace("%p", surface);
187 static int omap_FlipHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
189 struct SDL_PrivateVideoData *pdata = this->hidden;
191 trace("%p", surface);
193 surface->pixels = osdl_video_flip(pdata);
194 pdata->app_uses_flip = 1;
199 /* we can't do hw surfaces (besides screen one) yet */
200 static int omap_AllocHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
202 trace("%p", surface);
206 static void omap_FreeHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
208 trace("%p", surface);
211 static int omap_SetColors(SDL_VideoDevice *this, int firstcolor, int ncolors, SDL_Color *colors)
213 trace("%d, %d, %p", firstcolor, ncolors, colors);
217 static void omap_UpdateRects(SDL_VideoDevice *this, int nrects, SDL_Rect *rects)
219 struct SDL_PrivateVideoData *pdata = this->hidden;
221 trace("%d, %p", nrects, rects);
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)
228 this->screen->pixels = osdl_video_flip(pdata);
232 static void omap_InitOSKeymap(SDL_VideoDevice *this)
237 static int key_event_cb(void *cb_arg, int sdl_kc, int sdl_sc, int is_pressed)
239 SDL_keysym keysym = { 0, };
245 keysym.scancode = sdl_sc;
247 /* 0xff if pandora's Fn, so we exclude it too.. */
248 if (is_pressed && sdl_kc < 0xff && SDL_TranslateUNICODE) {
249 mod = SDL_GetModState();
250 if (!(mod & KMOD_CTRL) && (!!(mod & KMOD_SHIFT) ^ !!(mod & KMOD_CAPS)))
253 /* prefer X mapping, if that doesn't work use hardcoded one */
254 ret = xenv_keycode_to_keysym(sdl_sc, shift);
256 keysym.unicode = ret;
257 if ((mod & KMOD_CTRL)
258 && 0x60 <= keysym.unicode && keysym.unicode <= 0x7f)
260 keysym.unicode -= 0x60;
263 if ((keysym.unicode & 0xff00) == 0xff00)
264 keysym.unicode &= ~0xff00;
267 keysym.unicode = sdl_kc;
268 if ((mod & KMOD_CTRL) && 0x60 <= sdl_kc && sdl_kc <= 0x7f)
270 keysym.unicode = sdl_kc - 0x60;
272 else if (shift && 'a' <= sdl_kc && sdl_kc <= 'z')
274 keysym.unicode = sdl_kc - 'a' + 'A';
279 SDL_PrivateKeyboard(is_pressed, &keysym);
282 /* clamp x to min..max-1 */
283 #define clamp(x, min, max) \
284 if (x < (min)) x = min; \
285 if (x >= (max)) x = max
287 static void translate_mouse(SDL_VideoDevice *this, int *x, int *y)
289 struct SDL_PrivateVideoData *pdata = this->hidden;
291 if (!pdata->cfg_no_ts_translate && pdata->layer_w != 0 && pdata->layer_h != 0) {
292 *x = pdata->border_l + ((*x - pdata->layer_x) * pdata->ts_xmul >> 16);
293 *y = pdata->border_t + ((*y - pdata->layer_y) * pdata->ts_ymul >> 16);
294 clamp(*x, 0, this->screen->w);
295 clamp(*y, 0, this->screen->h);
299 static int ts_event_cb(void *cb_arg, int x, int y, unsigned int pressure)
301 static int was_pressed;
302 SDL_VideoDevice *this = cb_arg;
303 struct SDL_PrivateVideoData *pdata = this->hidden;
305 translate_mouse(this, &x, &y);
307 pressure = !!pressure;
308 if (pressure != was_pressed) {
309 SDL_PrivateMouseButton(pressure ? SDL_PRESSED : SDL_RELEASED, 1, x, y);
310 was_pressed = pressure;
313 SDL_PrivateMouseMotion(0, 0, x, y);
316 static int xmouseb_event_cb(void *cb_arg, int x, int y, int button, int is_pressed)
318 SDL_VideoDevice *this = cb_arg;
319 struct SDL_PrivateVideoData *pdata = this->hidden;
321 translate_mouse(this, &x, &y);
322 SDL_PrivateMouseButton(is_pressed ? SDL_PRESSED : SDL_RELEASED, button, x, y);
325 static int xmousem_event_cb(void *cb_arg, int x, int y)
327 SDL_VideoDevice *this = cb_arg;
328 struct SDL_PrivateVideoData *pdata = this->hidden;
330 translate_mouse(this, &x, &y);
331 SDL_PrivateMouseMotion(0, 0, x, y);
334 static int xkey_cb(void *cb_arg, int kc, int is_pressed)
336 SDL_VideoDevice *this = cb_arg;
337 struct SDL_PrivateVideoData *pdata = this->hidden;
340 if (kc == XF86XK_MenuKB && is_pressed) {
341 ret = osdl_video_pause(pdata, 1);
344 osdl_video_pause(pdata, 0);
345 omapsdl_input_get_events(0, NULL, NULL, NULL);
350 static void omap_PumpEvents(SDL_VideoDevice *this)
352 struct SDL_PrivateVideoData *pdata = this->hidden;
357 if (pdata->xenv_up) {
358 if (!pdata->cfg_ts_force_tslib) {
359 xenv_update(xkey_cb, xmouseb_event_cb, xmousem_event_cb, this);
360 if (pdata->xenv_mouse)
364 /* just flush X event queue */
365 xenv_update(NULL, NULL, NULL, NULL);
369 omapsdl_input_get_events(0, key_event_cb,
370 read_tslib ? ts_event_cb : NULL, this);
373 static SDL_VideoDevice *omap_create(int devindex)
375 SDL_VideoDevice *this;
377 this = calloc(1, sizeof(*this) + sizeof(*this->hidden));
382 this->hidden = (void *)(this + 1);
383 this->VideoInit = omap_VideoInit;
384 this->ListModes = omap_ListModes;
385 this->SetVideoMode = omap_SetVideoMode;
386 this->LockHWSurface = omap_LockHWSurface;
387 this->UnlockHWSurface = omap_UnlockHWSurface;
388 this->FlipHWSurface = omap_FlipHWSurface;
389 this->AllocHWSurface = omap_AllocHWSurface;
390 this->FreeHWSurface = omap_FreeHWSurface;
391 this->SetColors = omap_SetColors;
392 this->UpdateRects = omap_UpdateRects;
393 this->VideoQuit = omap_VideoQuit;
394 this->InitOSKeymap = omap_InitOSKeymap;
395 this->PumpEvents = omap_PumpEvents;
396 this->free = omap_free;
401 VideoBootStrap omapdss_bootstrap = {
402 "omapdss", "OMAP DSS2 Framebuffer Driver",
403 omap_available, omap_create