a4443c8ef921178f02ffe336ef0541fe9ef5bb15
[sdl_omap.git] / src / video / omapdss / sdlif.c
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
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include "../SDL_sysvideo.h"
12 #include "../SDL_pixels_c.h"
13 #include "../../events/SDL_events_c.h"
14
15 #include "linux/xenv.h"
16 #include "omapsdl.h"
17
18
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 {
33         const char *tmp;
34         int w, h, ret;
35
36         trace();
37
38         // default to 16bpp
39         vformat->BitsPerPixel = 16;
40
41         omapsdl_input_init();
42         omapsdl_config(this->hidden);
43
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) {
50                 this->info.current_w = this->hidden->phys_w;
51                 this->info.current_h = this->hidden->phys_h;
52         }
53
54         this->handles_any_size = 1;
55         this->info.hw_available = 1;
56
57         return 0;
58 }
59
60 static void omap_VideoQuit(SDL_VideoDevice *this)
61 {
62         trace();
63
64         osdl_video_finish(this->hidden);
65         this->screen->pixels = NULL;
66         omapsdl_input_finish();
67 }
68
69 static SDL_Rect **omap_ListModes(SDL_VideoDevice *this, SDL_PixelFormat *format, Uint32 flags)
70 {
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
75         };
76         /* broken API needs this stupidity */
77         static SDL_Rect *omap_modes[] = {
78                 &omap_mode_max,
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 {
94         struct SDL_PrivateVideoData *pdata = this->hidden;
95         SDL_PixelFormat *format;
96         Uint32 unhandled_flags;
97         int ret;
98
99         trace("%d, %d, %d, %08x", width, height, bpp, flags);
100
101         omapsdl_config_from_env(pdata);
102
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:
114                 err("SetVideoMode: bpp %d not supported", bpp);
115                 return NULL;
116         }
117         if (format == NULL)
118                 return NULL;
119
120         if (!(flags & SDL_DOUBLEBUF) && pdata->cfg_force_doublebuf) {
121                 log("forcing SDL_DOUBLEBUF");
122                 flags |= SDL_DOUBLEBUF;
123         }
124
125         ret = osdl_video_set_mode(pdata, width, height, bpp,
126                 (flags & SDL_DOUBLEBUF) ? 1 : 0);
127         if (ret < 0)
128                 return NULL;
129
130         flags |= SDL_FULLSCREEN | SDL_HWSURFACE;
131         unhandled_flags = flags & ~(SDL_FULLSCREEN | SDL_HWSURFACE | SDL_DOUBLEBUF);
132         if (unhandled_flags != 0) {
133                 log("dropping unhandled flags: %08x", unhandled_flags);
134                 flags &= ~unhandled_flags;
135         }
136
137         current->flags = flags;
138         current->w = width;
139         current->h = height;
140         current->pitch = SDL_CalculatePitch(current);
141
142         if (pdata->layer_w != 0 && pdata->layer_h != 0) {
143                 pdata->ts_xmul = (width  << 16) / pdata->layer_w;
144                 pdata->ts_ymul = (height << 16) / pdata->layer_h;
145         }
146
147         current->pixels = osdl_video_flip(pdata);
148
149         return current;
150 }
151
152 static int omap_LockHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
153 {
154         trace("%p", surface);
155
156         return 0;
157 }
158
159 static void omap_UnlockHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
160 {
161         trace("%p", surface);
162 }
163
164 static int omap_FlipHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
165 {
166         trace("%p", surface);
167
168         surface->pixels = osdl_video_flip(this->hidden);
169
170         return 0;
171 }
172
173 /* we can't do hw surfaces (besides screen one) yet */
174 static int omap_AllocHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
175 {
176         trace("%p", surface);
177         return -1;
178 }
179
180 static void omap_FreeHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
181 {
182         trace("%p", surface);
183 }
184
185 static int omap_SetColors(SDL_VideoDevice *this, int firstcolor, int ncolors, SDL_Color *colors)
186 {
187         trace("%d, %d, %p", firstcolor, ncolors, colors);
188         return 0;
189 }
190
191 static void omap_UpdateRects(SDL_VideoDevice *this, int nrects, SDL_Rect *rects)
192 {
193         trace("%d, %p", nrects, rects);
194
195         /* hmh.. */
196         if (nrects == 1 && rects->x == 0 && rects->y == 0 &&
197             (this->screen->flags & SDL_DOUBLEBUF) &&
198             rects->w == this->screen->w && rects->h == this->screen->h)
199         {
200                 this->screen->pixels = osdl_video_flip(this->hidden);
201         }
202 }
203
204 static void omap_InitOSKeymap(SDL_VideoDevice *this)
205 {
206         trace();
207 }
208
209 static int key_event_cb(void *cb_arg, int sdl_kc, int is_pressed)
210 {
211         SDL_keysym keysym = { 0, };
212
213         keysym.sym = sdl_kc;
214         SDL_PrivateKeyboard(is_pressed, &keysym);
215 }
216
217 /* clamp x to min..max-1 */
218 #define clamp(x, min, max) \
219         if (x < (min)) x = min; \
220         if (x >= (max)) x = max
221
222 static int ts_event_cb(void *cb_arg, int x, int y, unsigned int pressure)
223 {
224         static int was_pressed;
225         SDL_VideoDevice *this = cb_arg;
226         struct SDL_PrivateVideoData *pdata = this->hidden;
227         int xoffs;
228
229         if (!pdata->cfg_no_ts_translate && pdata->layer_w != 0 && pdata->layer_h != 0) {
230                 x = (x - pdata->layer_x) * pdata->ts_xmul >> 16;
231                 y = (y - pdata->layer_y) * pdata->ts_ymul >> 16;
232                 clamp(x, 0, this->screen->w);
233                 clamp(y, 0, this->screen->h);
234         }
235
236         SDL_PrivateMouseMotion(0, 0, x, y);
237
238         pressure = !!pressure;
239         if (pressure != was_pressed) {
240                 SDL_PrivateMouseButton(pressure ? SDL_PRESSED : SDL_RELEASED, 1, 0, 0);
241                 was_pressed = pressure;
242         }
243 }
244
245 static void omap_PumpEvents(SDL_VideoDevice *this) 
246 {
247         struct SDL_PrivateVideoData *pdata = this->hidden;
248         int dummy;
249
250         trace();
251
252         omapsdl_input_get_events(0, key_event_cb, ts_event_cb, this);
253
254         // XXX: we might want to process some X events too
255         if (pdata->xenv_up)
256                 xenv_update(&dummy);
257 }
258
259 static SDL_VideoDevice *omap_create(int devindex)
260 {
261         SDL_VideoDevice *this;
262
263         this = calloc(1, sizeof(*this) + sizeof(*this->hidden));
264         if (this == NULL) {
265                 SDL_OutOfMemory();
266                 return 0;
267         }
268         this->hidden = (void *)(this + 1);
269         this->VideoInit = omap_VideoInit;
270         this->ListModes = omap_ListModes;
271         this->SetVideoMode = omap_SetVideoMode;
272         this->LockHWSurface = omap_LockHWSurface;
273         this->UnlockHWSurface = omap_UnlockHWSurface;
274         this->FlipHWSurface = omap_FlipHWSurface;
275         this->AllocHWSurface = omap_AllocHWSurface;
276         this->FreeHWSurface = omap_FreeHWSurface;
277         this->SetColors = omap_SetColors;
278         this->UpdateRects = omap_UpdateRects;
279         this->VideoQuit = omap_VideoQuit;
280         this->InitOSKeymap = omap_InitOSKeymap;
281         this->PumpEvents = omap_PumpEvents;
282         this->free = omap_free;
283
284         return this;
285 }
286
287 VideoBootStrap omapdss_bootstrap = {
288         "omapdss", "OMAP DSS2 Framebuffer Driver",
289         omap_available, omap_create
290 };
291