71ea5c00e77b39da8c763cfb3419d1dedbffbf55
[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();
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->screen_w;
51                 this->info.current_h = this->hidden->screen_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         trace("%d, %d, %d, %08x", width, height, bpp, flags);
95
96         if (osdl_video_set_mode(this->hidden, width, height, bpp) < 0)
97                 return NULL;
98
99         if (!SDL_ReallocFormat(current, 16, 0xf800, 0x07e0, 0x001f, 0))
100                 return NULL;
101
102         current->flags = SDL_FULLSCREEN | SDL_DOUBLEBUF | SDL_HWSURFACE;
103         current->w = width;
104         current->h = height;
105         current->pitch = SDL_CalculatePitch(current);
106
107         current->pixels = osdl_video_flip(this->hidden);
108
109         return current;
110 }
111
112 static int omap_LockHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
113 {
114         trace("%p", surface);
115
116         return 0;
117 }
118
119 static void omap_UnlockHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
120 {
121         trace("%p", surface);
122 }
123
124 static int omap_FlipHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
125 {
126         trace("%p", surface);
127
128         surface->pixels = osdl_video_flip(this->hidden);
129
130         return 0;
131 }
132
133 /* we can't do hw surfaces (besides screen one) yet */
134 static int omap_AllocHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
135 {
136         trace("%p", surface);
137         return -1;
138 }
139
140 static void omap_FreeHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
141 {
142         trace("%p", surface);
143 }
144
145 static int omap_SetColors(SDL_VideoDevice *this, int firstcolor, int ncolors, SDL_Color *colors)
146 {
147         trace("%d, %d, %p", firstcolor, ncolors, colors);
148         return 0;
149 }
150
151 static void omap_UpdateRects(SDL_VideoDevice *this, int nrects, SDL_Rect *rects)
152 {
153         trace("%d, %p", nrects, rects);
154
155         if (nrects != 1 || rects->x != 0 || rects->y != 0 ||
156                         rects->w != this->screen->w || rects->h != this->screen->h) {
157                 static int warned = 0;
158                 if (!warned) {
159                         not_supported();
160                         warned = 1;
161                 }
162         }
163
164         this->screen->pixels = osdl_video_flip(this->hidden);
165 }
166
167 static void omap_InitOSKeymap(SDL_VideoDevice *this)
168 {
169         trace();
170 }
171
172 static int key_event_cb(void *cb_arg, int sdl_kc, int is_pressed)
173 {
174         SDL_keysym keysym = { 0, };
175
176         keysym.sym = sdl_kc;
177         SDL_PrivateKeyboard(is_pressed, &keysym);
178 }
179
180 static int ts_event_cb(void *cb_arg, int x, int y, unsigned int pressure)
181 {
182         static int was_pressed;
183
184         SDL_PrivateMouseMotion(0, 0, x, y);
185
186         pressure = !!pressure;
187         if (pressure != was_pressed) {
188                 SDL_PrivateMouseButton(pressure ? SDL_PRESSED : SDL_RELEASED, 1, 0, 0);
189                 was_pressed = pressure;
190         }
191 }
192
193 static void omap_PumpEvents(SDL_VideoDevice *this) 
194 {
195         struct SDL_PrivateVideoData *pdata = this->hidden;
196         int dummy;
197
198         trace();
199
200         omapsdl_input_get_events(0, key_event_cb, ts_event_cb, NULL);
201
202         // XXX: we might want to process some X events too
203         if (pdata->xenv_up)
204                 xenv_update(&dummy);
205 }
206
207 static SDL_VideoDevice *omap_create(int devindex)
208 {
209         SDL_VideoDevice *this;
210
211         this = calloc(1, sizeof(*this) + sizeof(*this->hidden));
212         if (this == NULL) {
213                 SDL_OutOfMemory();
214                 return 0;
215         }
216         this->hidden = (void *)(this + 1);
217         this->VideoInit = omap_VideoInit;
218         this->ListModes = omap_ListModes;
219         this->SetVideoMode = omap_SetVideoMode;
220         this->LockHWSurface = omap_LockHWSurface;
221         this->UnlockHWSurface = omap_UnlockHWSurface;
222         this->FlipHWSurface = omap_FlipHWSurface;
223         this->AllocHWSurface = omap_AllocHWSurface;
224         this->FreeHWSurface = omap_FreeHWSurface;
225         this->SetColors = omap_SetColors;
226         this->UpdateRects = omap_UpdateRects;
227         this->VideoQuit = omap_VideoQuit;
228         this->InitOSKeymap = omap_InitOSKeymap;
229         this->PumpEvents = omap_PumpEvents;
230         this->free = omap_free;
231
232         return this;
233 }
234
235 VideoBootStrap omapdss_bootstrap = {
236         "omapdss", "OMAP DSS2 Framebuffer Driver",
237         omap_available, omap_create
238 };
239