4628735241fc54be7b62d00640da1a25d3ad21fd
[sdl_omap.git] / 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 #include "omapsdl.h"
15
16
17 static int omap_available(void) 
18 {
19         trace();
20         return 1;
21 }
22
23 static void omap_free(SDL_VideoDevice *device)
24 {
25         trace();
26         free(device);
27 }
28
29 static int omap_VideoInit(SDL_VideoDevice *this, SDL_PixelFormat *vformat)
30 {
31         const char *tmp;
32         int w, h, ret;
33
34         trace();
35
36         // default to 16bpp
37         vformat->BitsPerPixel = 16;
38
39         omapsdl_input_init();
40         omapsdl_config();
41
42         tmp = getenv("SDL_OMAP_DEFAULT_MODE");
43         if (tmp != NULL && sscanf(tmp, "%dx%d", &w, &h) == 2) {
44                 this->info.current_w = w;
45                 this->info.current_h = h;
46         }
47         else if (osdl_video_detect_screen(this->hidden) == 0) {
48                 this->info.current_w = this->hidden->screen_w;
49                 this->info.current_h = this->hidden->screen_h;
50         }
51
52         this->handles_any_size = 1;
53         this->info.hw_available = 1;
54
55         return 0;
56 }
57
58 static void omap_VideoQuit(SDL_VideoDevice *this)
59 {
60         trace();
61
62         osdl_video_finish(this->hidden);
63         this->screen->pixels = NULL;
64         omapsdl_input_finish();
65 }
66
67 static SDL_Rect **omap_ListModes(SDL_VideoDevice *this, SDL_PixelFormat *format, Uint32 flags)
68 {
69         static SDL_Rect omap_mode_max = {
70                 /* with handles_any_size, should accept anything up to this
71                  * XXX: possibly set this dynamically based on free vram? */
72                 0, 0, 1600, 1200
73         };
74         /* broken API needs this stupidity */
75         static SDL_Rect *omap_modes[] = {
76                 &omap_mode_max,
77                 NULL
78         };
79
80         trace();
81
82         if (format->BitsPerPixel <= 8)
83                 // not (yet?) supported
84                 return NULL;
85
86         return omap_modes;
87 }
88
89 static SDL_Surface *omap_SetVideoMode(SDL_VideoDevice *this, SDL_Surface *current, int width,
90                                         int height, int bpp, Uint32 flags)
91 {
92         trace("%d, %d, %d, %08x", width, height, bpp, flags);
93
94         if (osdl_video_set_mode(this->hidden, width, height, bpp) < 0)
95                 return NULL;
96
97         if (!SDL_ReallocFormat(current, 16, 0xf800, 0x07e0, 0x001f, 0))
98                 return NULL;
99
100         current->flags = SDL_FULLSCREEN | SDL_DOUBLEBUF | SDL_HWSURFACE;
101         current->w = width;
102         current->h = height;
103         current->pitch = SDL_CalculatePitch(current);
104
105         current->pixels = osdl_video_flip(this->hidden);
106
107         return current;
108 }
109
110 static int omap_LockHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
111 {
112         trace("%p", surface);
113
114         return 0;
115 }
116
117 static void omap_UnlockHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
118 {
119         trace("%p", surface);
120 }
121
122 static int omap_FlipHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
123 {
124         trace("%p", surface);
125
126         surface->pixels = osdl_video_flip(this->hidden);
127
128         return 0;
129 }
130
131 /* we can't do hw surfaces (besides screen one) yet */
132 static int omap_AllocHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
133 {
134         trace("%p", surface);
135         return -1;
136 }
137
138 static void omap_FreeHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
139 {
140         trace("%p", surface);
141 }
142
143 static int omap_SetColors(SDL_VideoDevice *this, int firstcolor, int ncolors, SDL_Color *colors)
144 {
145         trace("%d, %d, %p", firstcolor, ncolors, colors);
146         return 0;
147 }
148
149 static void omap_UpdateRects(SDL_VideoDevice *this, int nrects, SDL_Rect *rects)
150 {
151         trace("%d, %p", nrects, rects);
152
153         if (nrects != 1 || rects->x != 0 || rects->y != 0 ||
154                         rects->w != this->screen->w || rects->h != this->screen->h) {
155                 static int warned = 0;
156                 if (!warned) {
157                         not_supported();
158                         warned = 1;
159                 }
160         }
161
162         this->screen->pixels = osdl_video_flip(this->hidden);
163 }
164
165 static void omap_InitOSKeymap(SDL_VideoDevice *this)
166 {
167         trace();
168 }
169
170 static int key_event_cb(void *cb_arg, int sdl_kc, int is_pressed)
171 {
172         SDL_keysym keysym = { 0, };
173
174         keysym.sym = sdl_kc;
175         SDL_PrivateKeyboard(is_pressed, &keysym);
176 }
177
178 static int ts_event_cb(void *cb_arg, int x, int y, unsigned int pressure)
179 {
180         static int was_pressed;
181
182         SDL_PrivateMouseMotion(0, 0, x, y);
183
184         pressure = !!pressure;
185         if (pressure != was_pressed) {
186                 SDL_PrivateMouseButton(pressure ? SDL_PRESSED : SDL_RELEASED, 1, 0, 0);
187                 was_pressed = pressure;
188         }
189 }
190
191 static void omap_PumpEvents(SDL_VideoDevice *this) 
192 {
193         trace();
194
195         omapsdl_input_get_events(0, key_event_cb, ts_event_cb, NULL);
196 }
197
198 static SDL_VideoDevice *omap_create(int devindex)
199 {
200         SDL_VideoDevice *this;
201
202         this = calloc(1, sizeof(*this) + sizeof(*this->hidden));
203         if (this == NULL) {
204                 SDL_OutOfMemory();
205                 return 0;
206         }
207         this->hidden = (void *)(this + 1);
208         this->VideoInit = omap_VideoInit;
209         this->ListModes = omap_ListModes;
210         this->SetVideoMode = omap_SetVideoMode;
211         this->LockHWSurface = omap_LockHWSurface;
212         this->UnlockHWSurface = omap_UnlockHWSurface;
213         this->FlipHWSurface = omap_FlipHWSurface;
214         this->AllocHWSurface = omap_AllocHWSurface;
215         this->FreeHWSurface = omap_FreeHWSurface;
216         this->SetColors = omap_SetColors;
217         this->UpdateRects = omap_UpdateRects;
218         this->VideoQuit = omap_VideoQuit;
219         this->InitOSKeymap = omap_InitOSKeymap;
220         this->PumpEvents = omap_PumpEvents;
221         this->free = omap_free;
222
223         return this;
224 }
225
226 VideoBootStrap omapdss_bootstrap = {
227         "omapdss", "OMAP DSS2 Framebuffer Driver",
228         omap_available, omap_create
229 };
230