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