/
SDL_sysvideo.cc
398 lines (338 loc) · 11.4 KB
1
/*
2
3
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2006 Sam Lantinga
4
5
6
7
8
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
9
10
11
12
13
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
14
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
20
Sam Lantinga
slouken@libsdl.org
21
22
23
24
25
26
27
*/
/* Qtopia based framebuffer implementation */
#include <unistd.h>
#include <qapplication.h>
28
#include <qpe/qpeapplication.h>
29
30
31
32
33
34
35
#include "SDL_timer.h"
#include "SDL_QWin.h"
extern "C" {
36
37
#include "../SDL_sysvideo.h"
#include "../../events/SDL_events_c.h"
38
#include "SDL_sysevents_c.h"
39
#include "SDL_sysmouse_c.h"
40
41
42
43
44
45
#include "SDL_syswm_c.h"
#include "SDL_lowvideo.h"
//#define QTOPIA_DEBUG
#define QT_HIDDEN_SIZE 32 /* starting hidden window size */
46
47
48
49
/* Name of the environment variable used to invert the screen rotation or not:
Possible values:
!=0 : Screen is 270° rotated
0: Screen is 90° rotated*/
50
#define SDL_QT_ROTATION_ENV_NAME "SDL_QT_INVERT_ROTATION"
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* Initialization/Query functions */
static int QT_VideoInit(_THIS, SDL_PixelFormat *vformat);
static SDL_Rect **QT_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
static SDL_Surface *QT_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
static void QT_UpdateMouse(_THIS);
static int QT_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
static void QT_VideoQuit(_THIS);
/* Hardware surface functions */
static int QT_AllocHWSurface(_THIS, SDL_Surface *surface);
static int QT_LockHWSurface(_THIS, SDL_Surface *surface);
static void QT_UnlockHWSurface(_THIS, SDL_Surface *surface);
static void QT_FreeHWSurface(_THIS, SDL_Surface *surface);
static int QT_ToggleFullScreen(_THIS, int fullscreen);
68
69
static int QT_IconifyWindow(_THIS);
static SDL_GrabMode QT_GrabInput(_THIS, SDL_GrabMode mode);
70
71
72
73
74
75
76
77
78
79
/* FB driver bootstrap functions */
static int QT_Available(void)
{
return(1);
}
static void QT_DeleteDevice(SDL_VideoDevice *device)
{
80
81
SDL_free(device->hidden);
SDL_free(device);
82
83
84
85
86
87
88
}
static SDL_VideoDevice *QT_CreateDevice(int devindex)
{
SDL_VideoDevice *device;
/* Initialize all variables that we clean on shutdown */
89
device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
90
if ( device ) {
91
SDL_memset(device, 0, (sizeof *device));
92
device->hidden = (struct SDL_PrivateVideoData *)
93
SDL_malloc((sizeof *device->hidden));
94
95
96
97
}
if ( (device == NULL) || (device->hidden == NULL) ) {
SDL_OutOfMemory();
if ( device ) {
98
SDL_free(device);
99
100
101
}
return(0);
}
102
SDL_memset(device->hidden, 0, (sizeof *device->hidden));
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* Set the function pointers */
device->VideoInit = QT_VideoInit;
device->ListModes = QT_ListModes;
device->SetVideoMode = QT_SetVideoMode;
device->UpdateMouse = QT_UpdateMouse;
device->SetColors = QT_SetColors;
device->UpdateRects = NULL;
device->VideoQuit = QT_VideoQuit;
device->AllocHWSurface = QT_AllocHWSurface;
device->CheckHWBlit = NULL;
device->FillHWRect = NULL;
device->SetHWColorKey = NULL;
device->SetHWAlpha = NULL;
device->LockHWSurface = QT_LockHWSurface;
device->UnlockHWSurface = QT_UnlockHWSurface;
device->FlipHWSurface = NULL;
device->FreeHWSurface = QT_FreeHWSurface;
device->SetIcon = NULL;
device->SetCaption = QT_SetWMCaption;
123
124
device->IconifyWindow = QT_IconifyWindow;
device->GrabInput = QT_GrabInput;
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
device->GetWMInfo = NULL;
device->FreeWMCursor = QT_FreeWMCursor;
device->CreateWMCursor = QT_CreateWMCursor;
device->ShowWMCursor = QT_ShowWMCursor;
device->WarpWMCursor = QT_WarpWMCursor;
device->InitOSKeymap = QT_InitOSKeymap;
device->PumpEvents = QT_PumpEvents;
device->free = QT_DeleteDevice;
device->ToggleFullScreen = QT_ToggleFullScreen;
/* Set the driver flags */
device->handles_any_size = 0;
return device;
}
VideoBootStrap Qtopia_bootstrap = {
"qtopia", "Qtopia / QPE graphics",
QT_Available, QT_CreateDevice
};
/* Function to sort the display_list */
static int CompareModes(const void *A, const void *B)
{
#if 0
const display_mode *a = (display_mode *)A;
const display_mode *b = (display_mode *)B;
if ( a->space == b->space ) {
return((b->virtual_width*b->virtual_height)-
(a->virtual_width*a->virtual_height));
} else {
return(ColorSpaceToBitsPerPixel(b->space)-
ColorSpaceToBitsPerPixel(a->space));
}
#endif
return 0;
}
/* Yes, this isn't the fastest it could be, but it works nicely */
static int QT_AddMode(_THIS, int index, unsigned int w, unsigned int h)
{
SDL_Rect *mode;
int i;
int next_mode;
/* Check to see if we already have this mode */
if ( SDL_nummodes[index] > 0 ) {
for ( i=SDL_nummodes[index]-1; i >= 0; --i ) {
mode = SDL_modelist[index][i];
if ( (mode->w == w) && (mode->h == h) ) {
return(0);
}
}
}
/* Set up the new video mode rectangle */
183
mode = (SDL_Rect *)SDL_malloc(sizeof *mode);
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
if ( mode == NULL ) {
SDL_OutOfMemory();
return(-1);
}
mode->x = 0;
mode->y = 0;
mode->w = w;
mode->h = h;
#ifdef QTOPIA_DEBUG
fprintf(stderr, "Adding mode %dx%d at %d bytes per pixel\n", w, h, index+1);
#endif
/* Allocate the new list of modes, and fill in the new mode */
next_mode = SDL_nummodes[index];
SDL_modelist[index] = (SDL_Rect **)
199
SDL_realloc(SDL_modelist[index], (1+next_mode+1)*sizeof(SDL_Rect *));
200
201
202
if ( SDL_modelist[index] == NULL ) {
SDL_OutOfMemory();
SDL_nummodes[index] = 0;
203
SDL_free(mode);
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
return(-1);
}
SDL_modelist[index][next_mode] = mode;
SDL_modelist[index][next_mode+1] = NULL;
SDL_nummodes[index]++;
return(0);
}
int QT_VideoInit(_THIS, SDL_PixelFormat *vformat)
{
/* Initialize the QPE Application */
/* Determine the screen depth */
vformat->BitsPerPixel = QPixmap::defaultDepth();
// For now we hardcode the current depth because anything else
// might as well be emulated by SDL rather than by Qtopia.
QSize desktop_size = qApp->desktop()->size();
QT_AddMode(_this, ((vformat->BitsPerPixel+7)/8)-1,
desktop_size.width(), desktop_size.height());
QT_AddMode(_this, ((vformat->BitsPerPixel+7)/8)-1,
desktop_size.height(), desktop_size.width());
/* Create the window / widget */
SDL_Win = new SDL_QWin(QSize(QT_HIDDEN_SIZE, QT_HIDDEN_SIZE));
230
((QPEApplication*)qApp)->showMainWidget(SDL_Win);
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* Fill in some window manager capabilities */
_this->info.wm_available = 0;
/* We're done! */
return(0);
}
/* We support any dimension at our bit-depth */
SDL_Rect **QT_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
{
SDL_Rect **modes;
modes = ((SDL_Rect **)0);
if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
modes = SDL_modelist[((format->BitsPerPixel+7)/8)-1];
} else {
if ( format->BitsPerPixel ==
_this->screen->format->BitsPerPixel ) {
modes = ((SDL_Rect **)-1);
}
}
return(modes);
}
/* Various screen update functions available */
static void QT_NormalUpdate(_THIS, int numrects, SDL_Rect *rects);
static int QT_SetFullScreen(_THIS, SDL_Surface *screen, int fullscreen)
{
return -1;
}
static int QT_ToggleFullScreen(_THIS, int fullscreen)
{
return -1;
}
/* FIXME: check return values and cleanup here */
SDL_Surface *QT_SetVideoMode(_THIS, SDL_Surface *current,
int width, int height, int bpp, Uint32 flags)
{
273
274
275
276
277
QImage *qimage;
QSize desktop_size = qApp->desktop()->size();
278
current->flags = 0; //SDL_FULLSCREEN; // We always run fullscreen.
279
280
281
if(width <= desktop_size.width()
&& height <= desktop_size.height()) {
282
283
current->w = desktop_size.width();
current->h = desktop_size.height();
284
} else if(width <= desktop_size.height() && height <= desktop_size.width()) {
285
// Landscape mode
286
char * envString = SDL_getenv(SDL_QT_ROTATION_ENV_NAME);
287
288
int envValue = envString ? atoi(envString) : 0;
screenRotation = envValue ? SDL_QT_ROTATION_270 : SDL_QT_ROTATION_90;
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
current->h = desktop_size.width();
current->w = desktop_size.height();
} else {
SDL_SetError("Unsupported resolution, %dx%d\n", width, height);
}
if ( flags & SDL_OPENGL ) {
SDL_SetError("OpenGL not supported");
return(NULL);
}
/* Create the QImage framebuffer */
qimage = new QImage(current->w, current->h, bpp);
if (qimage->isNull()) {
SDL_SetError("Couldn't create screen bitmap");
delete qimage;
return(NULL);
}
current->pitch = qimage->bytesPerLine();
current->pixels = (void *)qimage->bits();
SDL_Win->setImage(qimage);
_this->UpdateRects = QT_NormalUpdate;
SDL_Win->setFullscreen(true);
/* We're done */
return(current);
}
/* Update the current mouse state and position */
void QT_UpdateMouse(_THIS)
{
QPoint point(-1, -1);
if ( SDL_Win->isActiveWindow() ) {
point = SDL_Win->mousePos();
}
if ( (point.x() >= 0) && (point.x() < SDL_VideoSurface->w) &&
(point.y() >= 0) && (point.y() < SDL_VideoSurface->h) ) {
SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
SDL_PrivateMouseMotion(0, 0,
(Sint16)point.x(), (Sint16)point.y());
} else {
SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
}
}
/* We don't actually allow hardware surfaces other than the main one */
static int QT_AllocHWSurface(_THIS, SDL_Surface *surface)
{
return(-1);
}
static void QT_FreeHWSurface(_THIS, SDL_Surface *surface)
{
return;
}
static int QT_LockHWSurface(_THIS, SDL_Surface *surface)
{
return(0);
}
static void QT_UnlockHWSurface(_THIS, SDL_Surface *surface)
{
return;
}
static void QT_NormalUpdate(_THIS, int numrects, SDL_Rect *rects)
{
352
353
354
355
356
357
358
if(SDL_Win->lockScreen()) {
for(int i=0; i<numrects; ++i ) {
QRect rect(rects[i].x, rects[i].y,
rects[i].w, rects[i].h);
SDL_Win->repaintRect(rect);
}
SDL_Win->unlockScreen();
359
360
361
362
363
364
365
366
367
368
}
}
/* Is the system palette settable? */
int QT_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
{
return -1;
}
void QT_VideoQuit(_THIS)
{
369
370
371
372
373
374
375
// This is dumb, but if I free this, the app doesn't exit correctly.
// Of course, this will leak memory if init video is done more than once.
// Sucks but such is life.
// -- David Hedbor
// delete SDL_Win;
// SDL_Win = 0;
376
_this->screen->pixels = NULL;
377
QT_GrabInput(_this, SDL_GRAB_OFF);
378
379
}
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
static int QT_IconifyWindow(_THIS) {
SDL_Win->hide();
return true;
}
static SDL_GrabMode QT_GrabInput(_THIS, SDL_GrabMode mode) {
if(mode == SDL_GRAB_OFF) {
QPEApplication::grabKeyboard();
qApp->processEvents();
QPEApplication::ungrabKeyboard();
} else {
QPEApplication::grabKeyboard();
}
qApp->processEvents();
return mode;
}
398
}; /* Extern C */