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> |
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" |
9ab462cd |
16 | #include "osdl.h" |
9a8e84f8 |
17 | |
18 | |
9a8e84f8 |
19 | static int omap_available(void) |
20 | { |
21 | trace(); |
22 | return 1; |
23 | } |
24 | |
25 | static void omap_free(SDL_VideoDevice *device) |
26 | { |
27 | trace(); |
28 | free(device); |
29 | } |
30 | |
31 | static 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 | |
60 | static 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 | |
69 | static 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 | |
91 | static 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; |
0c7caf2b |
97 | int doublebuf; |
455c8c43 |
98 | void *fbmem; |
3576a5ac |
99 | |
9a8e84f8 |
100 | trace("%d, %d, %d, %08x", width, height, bpp, flags); |
101 | |
5f4b1fd3 |
102 | omapsdl_config_from_env(pdata); |
0bb19c41 |
103 | |
3576a5ac |
104 | switch (bpp) { |
105 | case 16: |
106 | format = SDL_ReallocFormat(current, 16, 0xf800, 0x07e0, 0x001f, 0); |
107 | break; |
108 | case 24: |
109 | format = SDL_ReallocFormat(current, 24, 0xff0000, 0xff00, 0xff, 0); |
110 | break; |
111 | case 32: |
5848b0cd |
112 | format = SDL_ReallocFormat(current, 32, 0xff0000, 0xff00, 0xff, 0); |
3576a5ac |
113 | break; |
114 | default: |
0bb19c41 |
115 | err("SetVideoMode: bpp %d not supported", bpp); |
3576a5ac |
116 | return NULL; |
117 | } |
118 | if (format == NULL) |
f641fccb |
119 | return NULL; |
9a8e84f8 |
120 | |
5f4b1fd3 |
121 | if (!(flags & SDL_DOUBLEBUF) && pdata->cfg_force_doublebuf) { |
0bb19c41 |
122 | log("forcing SDL_DOUBLEBUF"); |
123 | flags |= SDL_DOUBLEBUF; |
124 | } |
125 | |
455c8c43 |
126 | if (pdata->border_l | pdata->border_r | pdata->border_t | pdata->border_b) { |
127 | if (pdata->border_l + pdata->border_r >= width |
128 | || pdata->border_t + pdata->border_b >= height) |
129 | { |
130 | err("specified border too large, ignoring"); |
131 | pdata->border_l = pdata->border_r = pdata->border_t = pdata->border_b = 0; |
132 | } |
133 | } |
134 | |
0c7caf2b |
135 | doublebuf = (flags & SDL_DOUBLEBUF) ? 1 : 0; |
455c8c43 |
136 | fbmem = osdl_video_set_mode(pdata, |
137 | pdata->border_l, pdata->border_r, pdata->border_t, pdata->border_b, |
0c7caf2b |
138 | width, height, bpp, &doublebuf); |
455c8c43 |
139 | if (fbmem == NULL) { |
0c7caf2b |
140 | err("failing on mode %dx%d@%d, doublebuf %s, border %d,%d,%d,%d", |
141 | width, height, bpp, (flags & SDL_DOUBLEBUF) ? "on" : "off", |
142 | pdata->border_l, pdata->border_r, pdata->border_t, pdata->border_b); |
9a8e84f8 |
143 | return NULL; |
455c8c43 |
144 | } |
0c7caf2b |
145 | if ((flags & SDL_DOUBLEBUF) && !doublebuf) { |
146 | log("doublebuffering could not be set\n"); |
147 | flags &= ~SDL_DOUBLEBUF; |
148 | } |
9a8e84f8 |
149 | |
0bb19c41 |
150 | flags |= SDL_FULLSCREEN | SDL_HWSURFACE; |
151 | unhandled_flags = flags & ~(SDL_FULLSCREEN | SDL_HWSURFACE | SDL_DOUBLEBUF); |
152 | if (unhandled_flags != 0) { |
153 | log("dropping unhandled flags: %08x", unhandled_flags); |
154 | flags &= ~unhandled_flags; |
155 | } |
156 | |
157 | current->flags = flags; |
9a8e84f8 |
158 | current->w = width; |
159 | current->h = height; |
160 | current->pitch = SDL_CalculatePitch(current); |
455c8c43 |
161 | current->pixels = fbmem; |
9225e894 |
162 | pdata->app_uses_flip = 0; |
9a8e84f8 |
163 | |
5f4b1fd3 |
164 | if (pdata->layer_w != 0 && pdata->layer_h != 0) { |
455c8c43 |
165 | int v_width = width - (pdata->border_l + pdata->border_r); |
166 | int v_height = height - (pdata->border_t + pdata->border_b); |
167 | pdata->ts_xmul = (v_width << 16) / pdata->layer_w; |
168 | pdata->ts_ymul = (v_height << 16) / pdata->layer_h; |
5f4b1fd3 |
169 | } |
170 | |
9a8e84f8 |
171 | return current; |
172 | } |
173 | |
9a8e84f8 |
174 | static int omap_LockHWSurface(SDL_VideoDevice *this, SDL_Surface *surface) |
175 | { |
176 | trace("%p", surface); |
177 | |
178 | return 0; |
179 | } |
180 | |
181 | static void omap_UnlockHWSurface(SDL_VideoDevice *this, SDL_Surface *surface) |
182 | { |
183 | trace("%p", surface); |
184 | } |
185 | |
186 | static int omap_FlipHWSurface(SDL_VideoDevice *this, SDL_Surface *surface) |
187 | { |
9225e894 |
188 | struct SDL_PrivateVideoData *pdata = this->hidden; |
189 | |
9a8e84f8 |
190 | trace("%p", surface); |
191 | |
9225e894 |
192 | surface->pixels = osdl_video_flip(pdata); |
193 | pdata->app_uses_flip = 1; |
9a8e84f8 |
194 | |
195 | return 0; |
196 | } |
197 | |
37299201 |
198 | /* we can't do hw surfaces (besides screen one) yet */ |
199 | static int omap_AllocHWSurface(SDL_VideoDevice *this, SDL_Surface *surface) |
200 | { |
201 | trace("%p", surface); |
202 | return -1; |
203 | } |
204 | |
205 | static void omap_FreeHWSurface(SDL_VideoDevice *this, SDL_Surface *surface) |
206 | { |
207 | trace("%p", surface); |
208 | } |
209 | |
9a8e84f8 |
210 | static int omap_SetColors(SDL_VideoDevice *this, int firstcolor, int ncolors, SDL_Color *colors) |
211 | { |
212 | trace("%d, %d, %p", firstcolor, ncolors, colors); |
213 | return 0; |
214 | } |
215 | |
216 | static void omap_UpdateRects(SDL_VideoDevice *this, int nrects, SDL_Rect *rects) |
217 | { |
9225e894 |
218 | struct SDL_PrivateVideoData *pdata = this->hidden; |
219 | |
9a8e84f8 |
220 | trace("%d, %p", nrects, rects); |
221 | |
9225e894 |
222 | /* for doublebuf forcing on apps */ |
223 | if (nrects == 1 && rects->x == 0 && rects->y == 0 |
224 | && !pdata->app_uses_flip && (this->screen->flags & SDL_DOUBLEBUF) |
225 | && rects->w == this->screen->w && rects->h == this->screen->h) |
0bb19c41 |
226 | { |
9225e894 |
227 | this->screen->pixels = osdl_video_flip(pdata); |
9a8e84f8 |
228 | } |
9a8e84f8 |
229 | } |
230 | |
231 | static void omap_InitOSKeymap(SDL_VideoDevice *this) |
232 | { |
233 | trace(); |
234 | } |
235 | |
b260fb56 |
236 | static int key_event_cb(void *cb_arg, int sdl_kc, int sdl_sc, int is_pressed) |
9a8e84f8 |
237 | { |
238 | SDL_keysym keysym = { 0, }; |
239 | |
240 | keysym.sym = sdl_kc; |
b260fb56 |
241 | keysym.scancode = sdl_sc; |
9a8e84f8 |
242 | SDL_PrivateKeyboard(is_pressed, &keysym); |
243 | } |
244 | |
5f4b1fd3 |
245 | /* clamp x to min..max-1 */ |
246 | #define clamp(x, min, max) \ |
247 | if (x < (min)) x = min; \ |
248 | if (x >= (max)) x = max |
249 | |
e1837b2c |
250 | static void translate_mouse(SDL_VideoDevice *this, int *x, int *y) |
17e19802 |
251 | { |
5f4b1fd3 |
252 | struct SDL_PrivateVideoData *pdata = this->hidden; |
5f4b1fd3 |
253 | |
254 | if (!pdata->cfg_no_ts_translate && pdata->layer_w != 0 && pdata->layer_h != 0) { |
e1837b2c |
255 | *x = pdata->border_l + ((*x - pdata->layer_x) * pdata->ts_xmul >> 16); |
256 | *y = pdata->border_t + ((*y - pdata->layer_y) * pdata->ts_ymul >> 16); |
257 | clamp(*x, 0, this->screen->w); |
258 | clamp(*y, 0, this->screen->h); |
5f4b1fd3 |
259 | } |
e1837b2c |
260 | } |
17e19802 |
261 | |
e1837b2c |
262 | static int ts_event_cb(void *cb_arg, int x, int y, unsigned int pressure) |
263 | { |
264 | static int was_pressed; |
265 | SDL_VideoDevice *this = cb_arg; |
266 | struct SDL_PrivateVideoData *pdata = this->hidden; |
267 | |
268 | translate_mouse(this, &x, &y); |
17e19802 |
269 | |
270 | pressure = !!pressure; |
271 | if (pressure != was_pressed) { |
e1837b2c |
272 | SDL_PrivateMouseButton(pressure ? SDL_PRESSED : SDL_RELEASED, 1, x, y); |
17e19802 |
273 | was_pressed = pressure; |
274 | } |
e1837b2c |
275 | else |
276 | SDL_PrivateMouseMotion(0, 0, x, y); |
277 | } |
278 | |
279 | static int mouseb_event_cb(void *cb_arg, int x, int y, int button, int is_pressed) |
280 | { |
281 | SDL_VideoDevice *this = cb_arg; |
282 | struct SDL_PrivateVideoData *pdata = this->hidden; |
283 | |
284 | translate_mouse(this, &x, &y); |
285 | SDL_PrivateMouseButton(is_pressed ? SDL_PRESSED : SDL_RELEASED, button, x, y); |
286 | } |
287 | |
288 | static int mousem_event_cb(void *cb_arg, int x, int y) |
289 | { |
290 | SDL_VideoDevice *this = cb_arg; |
291 | struct SDL_PrivateVideoData *pdata = this->hidden; |
292 | |
293 | translate_mouse(this, &x, &y); |
294 | SDL_PrivateMouseMotion(0, 0, x, y); |
17e19802 |
295 | } |
296 | |
9a8e84f8 |
297 | static void omap_PumpEvents(SDL_VideoDevice *this) |
298 | { |
f9b3f440 |
299 | struct SDL_PrivateVideoData *pdata = this->hidden; |
e1837b2c |
300 | int read_tslib = 1; |
f9b3f440 |
301 | |
9a8e84f8 |
302 | trace(); |
303 | |
e1837b2c |
304 | if (pdata->xenv_up) { |
305 | if (!pdata->cfg_ts_force_tslib) { |
306 | xenv_update(NULL, mouseb_event_cb, mousem_event_cb, this); |
307 | if (pdata->xenv_mouse) |
308 | read_tslib = 0; |
309 | } |
310 | else { |
311 | /* just flush X event queue */ |
312 | xenv_update(NULL, NULL, NULL, NULL); |
313 | } |
314 | } |
f9b3f440 |
315 | |
e1837b2c |
316 | omapsdl_input_get_events(0, key_event_cb, |
317 | read_tslib ? ts_event_cb : NULL, this); |
9a8e84f8 |
318 | } |
319 | |
320 | static SDL_VideoDevice *omap_create(int devindex) |
321 | { |
322 | SDL_VideoDevice *this; |
323 | |
324 | this = calloc(1, sizeof(*this) + sizeof(*this->hidden)); |
325 | if (this == NULL) { |
326 | SDL_OutOfMemory(); |
327 | return 0; |
328 | } |
329 | this->hidden = (void *)(this + 1); |
330 | this->VideoInit = omap_VideoInit; |
331 | this->ListModes = omap_ListModes; |
332 | this->SetVideoMode = omap_SetVideoMode; |
333 | this->LockHWSurface = omap_LockHWSurface; |
334 | this->UnlockHWSurface = omap_UnlockHWSurface; |
335 | this->FlipHWSurface = omap_FlipHWSurface; |
37299201 |
336 | this->AllocHWSurface = omap_AllocHWSurface; |
337 | this->FreeHWSurface = omap_FreeHWSurface; |
9a8e84f8 |
338 | this->SetColors = omap_SetColors; |
339 | this->UpdateRects = omap_UpdateRects; |
340 | this->VideoQuit = omap_VideoQuit; |
341 | this->InitOSKeymap = omap_InitOSKeymap; |
342 | this->PumpEvents = omap_PumpEvents; |
343 | this->free = omap_free; |
344 | |
345 | return this; |
346 | } |
347 | |
348 | VideoBootStrap omapdss_bootstrap = { |
349 | "omapdss", "OMAP DSS2 Framebuffer Driver", |
350 | omap_available, omap_create |
351 | }; |
352 | |