93d18f7e05404654195777caee924ad392cadfab
[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 <stdlib.h>
9
10 #include "../SDL_sysvideo.h"
11 #include "../SDL_pixels_c.h"
12 #include "../../events/SDL_events_c.h"
13 #include "omapsdl.h"
14
15
16 static int omap_available(void) 
17 {
18         trace();
19         return 1;
20 }
21
22 static void omap_free(SDL_VideoDevice *device)
23 {
24         trace();
25         free(device);
26 }
27
28 static int omap_VideoInit(SDL_VideoDevice *this, SDL_PixelFormat *vformat)
29 {
30         trace();
31
32         // default to 16bpp
33         vformat->BitsPerPixel = 16;
34
35         omapsdl_input_init();
36         omapsdl_config();
37
38         return 0;
39 }
40
41 static void omap_VideoQuit(SDL_VideoDevice *this)
42 {
43         trace();
44
45         osdl_video_finish(this->hidden);
46         this->screen->pixels = NULL;
47         omapsdl_input_finish();
48 }
49
50 static SDL_Rect **omap_ListModes(SDL_VideoDevice *this, SDL_PixelFormat *format, Uint32 flags)
51 {
52         static SDL_Rect omap_mode_list[] = {
53                 // XXX: we are not really restricted to fixed modes
54                 // FIXME: should really check the display for max supported
55                 { 0, 0, 800, 480 },
56                 { 0, 0, 720, 480 },
57                 { 0, 0, 640, 480 },
58                 { 0, 0, 640, 400 },
59                 { 0, 0, 512, 384 },
60                 { 0, 0, 320, 240 },
61                 { 0, 0, 320, 200 },
62         };
63         // broken API needs this
64         static SDL_Rect *omap_modes[] = {
65                 &omap_mode_list[0],
66                 &omap_mode_list[1],
67                 &omap_mode_list[2],
68                 &omap_mode_list[3],
69                 &omap_mode_list[4],
70                 &omap_mode_list[5],
71                 &omap_mode_list[6],
72                 NULL
73         };
74
75         trace();
76
77         if (format->BitsPerPixel <= 8)
78                 // not (yet?) supported
79                 return NULL;
80
81         return omap_modes;
82 }
83
84 static SDL_Surface *omap_SetVideoMode(SDL_VideoDevice *this, SDL_Surface *current, int width,
85                                         int height, int bpp, Uint32 flags)
86 {
87         trace("%d, %d, %d, %08x", width, height, bpp, flags);
88
89         if (osdl_video_set_mode(this->hidden, width, height, bpp) < 0)
90                 return NULL;
91
92         if (!SDL_ReallocFormat(current, 16, 0xf800, 0x07e0, 0x001f, 0))
93                 return NULL;
94
95         current->flags = SDL_FULLSCREEN | SDL_DOUBLEBUF | SDL_HWSURFACE;
96         current->w = width;
97         current->h = height;
98         current->pitch = SDL_CalculatePitch(current);
99
100         current->pixels = osdl_video_flip(this->hidden);
101
102         return current;
103 }
104
105 static int omap_LockHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
106 {
107         trace("%p", surface);
108
109         return 0;
110 }
111
112 static void omap_UnlockHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
113 {
114         trace("%p", surface);
115 }
116
117 static int omap_FlipHWSurface(SDL_VideoDevice *this, SDL_Surface *surface)
118 {
119         trace("%p", surface);
120
121         surface->pixels = osdl_video_flip(this->hidden);
122
123         return 0;
124 }
125
126 static int omap_SetColors(SDL_VideoDevice *this, int firstcolor, int ncolors, SDL_Color *colors)
127 {
128         trace("%d, %d, %p", firstcolor, ncolors, colors);
129         return 0;
130 }
131
132 static void omap_UpdateRects(SDL_VideoDevice *this, int nrects, SDL_Rect *rects)
133 {
134         trace("%d, %p", nrects, rects);
135
136         if (nrects != 1 || rects->x != 0 || rects->y != 0 ||
137                         rects->w != this->screen->w || rects->h != this->screen->h) {
138                 static int warned = 0;
139                 if (!warned) {
140                         not_supported();
141                         warned = 1;
142                 }
143         }
144
145         this->screen->pixels = osdl_video_flip(this->hidden);
146 }
147
148 static void omap_InitOSKeymap(SDL_VideoDevice *this)
149 {
150         trace();
151 }
152
153 static int key_event_cb(void *cb_arg, int sdl_kc, int is_pressed)
154 {
155         SDL_keysym keysym = { 0, };
156
157         keysym.sym = sdl_kc;
158         SDL_PrivateKeyboard(is_pressed, &keysym);
159 }
160
161 static int ts_event_cb(void *cb_arg, int x, int y, unsigned int pressure)
162 {
163         static int was_pressed;
164
165         SDL_PrivateMouseMotion(0, 0, x, y);
166
167         pressure = !!pressure;
168         if (pressure != was_pressed) {
169                 SDL_PrivateMouseButton(pressure ? SDL_PRESSED : SDL_RELEASED, 1, 0, 0);
170                 was_pressed = pressure;
171         }
172 }
173
174 static void omap_PumpEvents(SDL_VideoDevice *this) 
175 {
176         trace();
177
178         omapsdl_input_get_events(0, key_event_cb, ts_event_cb, NULL);
179 }
180
181 static SDL_VideoDevice *omap_create(int devindex)
182 {
183         SDL_VideoDevice *this;
184
185         this = calloc(1, sizeof(*this) + sizeof(*this->hidden));
186         if (this == NULL) {
187                 SDL_OutOfMemory();
188                 return 0;
189         }
190         this->hidden = (void *)(this + 1);
191         this->VideoInit = omap_VideoInit;
192         this->ListModes = omap_ListModes;
193         this->SetVideoMode = omap_SetVideoMode;
194         this->LockHWSurface = omap_LockHWSurface;
195         this->UnlockHWSurface = omap_UnlockHWSurface;
196         this->FlipHWSurface = omap_FlipHWSurface;
197         this->SetColors = omap_SetColors;
198         this->UpdateRects = omap_UpdateRects;
199         this->VideoQuit = omap_VideoQuit;
200         this->InitOSKeymap = omap_InitOSKeymap;
201         this->PumpEvents = omap_PumpEvents;
202         this->free = omap_free;
203
204         return this;
205 }
206
207 VideoBootStrap omapdss_bootstrap = {
208         "omapdss", "OMAP DSS2 Framebuffer Driver",
209         omap_available, omap_create
210 };
211