2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
25 #include <qapplication.h>
26 #include <qdirectpainter_qws.h>
28 screenRotationT screenRotation = SDL_QT_NO_ROTATION;
30 SDL_QWin::SDL_QWin(const QSize& size)
31 : QWidget(0, "SDL_main"), my_painter(0), my_image(0),
32 my_inhibit_resize(false), my_mouse_pos(-1,-1), my_flags(0),
33 my_has_fullscreen(false), my_locked(0)
35 setBackgroundMode(NoBackground);
38 SDL_QWin::~SDL_QWin() {
45 void SDL_QWin::setImage(QImage *image) {
50 // setFixedSize(image->size());
53 void SDL_QWin::resizeEvent(QResizeEvent *e) {
54 if(size() != qApp->desktop()->size()) {
55 // Widget is not the correct size, so do the fullscreen magic
56 my_has_fullscreen = false;
59 if(my_inhibit_resize) {
60 my_inhibit_resize = false;
62 SDL_PrivateResize(e->size().width(), e->size().height());
66 void SDL_QWin::focusInEvent(QFocusEvent *) {
67 // Always do it here, no matter the size.
69 SDL_PrivateAppActive(true, SDL_APPINPUTFOCUS);
72 void SDL_QWin::focusOutEvent(QFocusEvent *) {
73 my_has_fullscreen = false;
74 SDL_PrivateAppActive(false, SDL_APPINPUTFOCUS);
77 void SDL_QWin::closeEvent(QCloseEvent *e) {
82 void SDL_QWin::setMousePos(const QPoint &pos) {
83 if(my_image->width() == height()) {
84 if (screenRotation == SDL_QT_ROTATION_90)
85 my_mouse_pos = QPoint(height()-pos.y(), pos.x());
86 else if (screenRotation == SDL_QT_ROTATION_270)
87 my_mouse_pos = QPoint(pos.y(), width()-pos.x());
93 void SDL_QWin::mouseMoveEvent(QMouseEvent *e) {
94 Qt::ButtonState button = e->button();
96 if( (button & Qt::LeftButton)) {
97 sdlstate |= SDL_BUTTON_LMASK;
99 if( (button & Qt::RightButton)) {
100 sdlstate |= SDL_BUTTON_RMASK;
102 if( (button & Qt::MidButton)) {
103 sdlstate |= SDL_BUTTON_MMASK;
105 setMousePos(e->pos());
106 SDL_PrivateMouseMotion(sdlstate, 0, my_mouse_pos.x(), my_mouse_pos.y());
109 void SDL_QWin::mousePressEvent(QMouseEvent *e) {
111 Qt::ButtonState button = e->button();
112 SDL_PrivateMouseButton(SDL_PRESSED,
113 (button & Qt::LeftButton) ? 1 :
114 ((button & Qt::RightButton) ? 2 : 3),
115 my_mouse_pos.x(), my_mouse_pos.y());
118 void SDL_QWin::mouseReleaseEvent(QMouseEvent *e) {
119 setMousePos(e->pos());
120 Qt::ButtonState button = e->button();
121 SDL_PrivateMouseButton(SDL_RELEASED,
122 (button & Qt::LeftButton) ? 1 :
123 ((button & Qt::RightButton) ? 2 : 3),
124 my_mouse_pos.x(), my_mouse_pos.y());
125 my_mouse_pos = QPoint(-1, -1);
129 gs_fastRotateBlit_3 ( unsigned short *fb,
130 unsigned short *bits,
133 // FIXME: this only works correctly for 240x320 displays
137 startx = rect.left() >> 1;
138 starty = rect.top() >> 1;
139 width = ((rect.right() - rect.left()) >> 1) + 2;
140 height = ((rect.bottom() - rect.top()) >> 1) + 2;
142 if((startx+width) > 120) {
143 width = 120 - startx; // avoid horizontal overflow
145 if((starty+height) > 160) {
146 height = 160 - starty; // avoid vertical overflow
149 ulong *sp1, *sp2, *dp1, *dp2;
150 ulong stop, sbot, dtop, dbot;
152 sp1 = (ulong*)bits + startx + starty*240;
154 dp1 = (ulong *)fb + (159 - starty) + startx*320;
156 int rowadd = (-320*width) - 1;
157 int rowadd2 = 240 - width;
158 // transfer in cells of 2x2 pixels in words
159 for (int y=0; y<height; y++) {
160 for (int x=0; x<width; x++) {
161 // read source pixels
165 dtop = (sbot & 0xffff) + ((stop & 0xffff)<<16);
166 dbot = ((sbot & 0xffff0000)>>16) + (stop & 0xffff0000);
167 // write to framebuffer
170 // update source ptrs
172 // update dest ptrs - 2 pix at a time
176 // adjust src ptrs - skip a row as we work in pairs
179 // adjust dest ptrs for rotation
186 gs_fastRotateBlit_1 ( unsigned short *fb,
187 unsigned short *bits,
188 const QRect& rect ) {
189 // FIXME: this only works correctly for 240x320 displays
193 startx = rect.left() >> 1;
194 starty = rect.top() >> 1;
195 width = ((rect.right() - rect.left()) >> 1) + 2;
196 height = ((rect.bottom() - rect.top()) >> 1) + 2;
198 if((startx+width) > 120) {
199 width = 120 - startx; // avoid horizontal overflow
201 if((starty+height) > 160) {
202 height = 160 - starty; // avoid vertical overflow
205 ulong *sp1, *sp2, *dp1, *dp2;
206 ulong stop, sbot, dtop, dbot;
207 fb += 320*239; // Move "fb" to top left corner
208 sp1 = (ulong*)bits + startx + starty*240;
210 dp1 = (ulong*)fb - startx * 320 - starty;
212 int rowadd = (320*width) + 1;
213 int rowadd2 = 240 - width;
214 // transfer in cells of 2x2 pixels in words
215 for (int y=0; y<height; y++) {
216 for (int x=0; x<width; x++) {
221 dtop = (stop & 0xffff) + ((sbot & 0xffff)<<16);
222 dbot = ((stop & 0xffff0000)>>16) + (sbot & 0xffff0000);
226 // update source ptrs
228 // update dest ptrs - 2 pix at a time
232 // adjust src ptrs - skip a row as we work in pairs
235 // adjust dest ptrs for rotation
241 // desktop, SL-A300 etc
242 bool SDL_QWin::repaintRotation0(const QRect& rect) {
243 if(my_image->width() == width()) {
244 uchar *fb = (uchar*)my_painter->frameBuffer();
245 uchar *buf = (uchar*)my_image->bits();
246 if(rect == my_image->rect()) {
247 SDL_memcpy(fb, buf, width()*height()*2);
249 int h = rect.height();
250 int wd = rect.width()<<1;
251 int fblineadd = my_painter->lineStep();
252 int buflineadd = my_image->bytesPerLine();
253 fb += (rect.left()<<1) + rect.top() * my_painter->lineStep();
254 buf += (rect.left()<<1) + rect.top() * my_image->bytesPerLine();
256 SDL_memcpy(fb, buf, wd);
262 return false; // FIXME: Landscape
265 my_painter->fillRect( rect, QBrush( Qt::NoBrush ) );
271 // Sharp Zaurus SL-5500 etc
272 bool SDL_QWin::repaintRotation3(const QRect& rect) {
273 if(my_image->width() == width()) {
274 ushort *fb = (ushort*)my_painter->frameBuffer();
275 ushort *buf = (ushort*)my_image->bits();
276 gs_fastRotateBlit_3(fb, buf, rect);
279 if (screenRotation == SDL_QT_ROTATION_90) {
280 uchar *fb = (uchar*)my_painter->frameBuffer();
281 uchar *buf = (uchar*)my_image->bits();
282 if(rect == my_image->rect()) {
283 SDL_memcpy(fb, buf, width()*height()*2);
285 int h = rect.height();
286 int wd = rect.width()<<1;
287 int fblineadd = my_painter->lineStep();
288 int buflineadd = my_image->bytesPerLine();
289 fb += (rect.left()<<1) + rect.top() * my_painter->lineStep();
290 buf += (rect.left()<<1) + rect.top() * my_image->bytesPerLine();
292 SDL_memcpy(fb, buf, wd);
297 } else if (screenRotation == SDL_QT_ROTATION_270) {
298 int h = rect.height();
299 int wd = rect.width();
300 int fblineadd = my_painter->lineStep() - (rect.width() << 1);
301 int buflineadd = my_image->bytesPerLine() - (rect.width() << 1);
304 uchar *fb = (uchar*)my_painter->frameBuffer();
305 uchar *buf = (uchar*)my_image->bits();
307 fb += ((my_painter->width() - (rect.top() + rect.height())) *
308 my_painter->lineStep()) + ((my_painter->height() - ((rect.left() +
309 rect.width()))) << 1);
311 buf += my_image->bytesPerLine() * (rect.top() + rect.height()) -
312 (((my_image->width() - (rect.left() + rect.width())) << 1) + 2);
316 while(w--) *((unsigned short*)fb)++ = *((unsigned short*)buf)--;
326 bool SDL_QWin::repaintRotation1(const QRect& rect) {
327 if(my_image->width() == width()) {
328 ushort *fb = (ushort*)my_painter->frameBuffer();
329 ushort *buf = (ushort*)my_image->bits();
330 gs_fastRotateBlit_1(fb, buf, rect);
332 return false; // FIXME: landscape mode
337 void SDL_QWin::repaintRect(const QRect& rect) {
338 if(!my_painter || !rect.width() || !rect.height()) {
342 if(QPixmap::defaultDepth() == 16) {
343 switch(my_painter->transformOrientation()) {
345 if(repaintRotation3(rect)) { return; }
348 if(repaintRotation1(rect)) { return; }
351 if(repaintRotation0(rect)) { return; }
355 my_painter->drawImage(rect.topLeft(), *my_image, rect);
358 // This paints the current buffer to the screen, when desired.
359 void SDL_QWin::paintEvent(QPaintEvent *ev) {
362 repaintRect(ev->rect());
367 /* Function to translate a keyboard transition and queue the key event
368 * This should probably be a table although this method isn't exactly
371 void SDL_QWin::QueueKey(QKeyEvent *e, int pressed)
374 int scancode = e->key();
375 /* Set the keysym information */
376 if(scancode >= 'A' && scancode <= 'Z') {
377 // Qt sends uppercase, SDL wants lowercase
378 keysym.sym = static_cast<SDLKey>(scancode + 32);
379 } else if(scancode >= 0x1000) {
382 case Qt::Key_Escape: scancode = SDLK_ESCAPE; break;
383 case Qt::Key_Tab: scancode = SDLK_TAB; break;
384 case Qt::Key_Backspace: scancode = SDLK_BACKSPACE; break;
385 case Qt::Key_Return: scancode = SDLK_RETURN; break;
386 case Qt::Key_Enter: scancode = SDLK_KP_ENTER; break;
387 case Qt::Key_Insert: scancode = SDLK_INSERT; break;
388 case Qt::Key_Delete: scancode = SDLK_DELETE; break;
389 case Qt::Key_Pause: scancode = SDLK_PAUSE; break;
390 case Qt::Key_Print: scancode = SDLK_PRINT; break;
391 case Qt::Key_SysReq: scancode = SDLK_SYSREQ; break;
392 case Qt::Key_Home: scancode = SDLK_HOME; break;
393 case Qt::Key_End: scancode = SDLK_END; break;
394 // We want the control keys to rotate with the screen
396 if (screenRotation == SDL_QT_ROTATION_90) scancode = SDLK_UP;
397 else if (screenRotation == SDL_QT_ROTATION_270) scancode = SDLK_DOWN;
398 else scancode = SDLK_LEFT;
401 if (screenRotation == SDL_QT_ROTATION_90) scancode = SDLK_RIGHT;
402 else if (screenRotation == SDL_QT_ROTATION_270) scancode = SDLK_LEFT;
403 else scancode = SDLK_UP;
406 if (screenRotation == SDL_QT_ROTATION_90) scancode = SDLK_DOWN;
407 else if (screenRotation == SDL_QT_ROTATION_270) scancode = SDLK_UP;
408 else scancode = SDLK_RIGHT;
411 if (screenRotation == SDL_QT_ROTATION_90) scancode = SDLK_LEFT;
412 else if (screenRotation == SDL_QT_ROTATION_270) scancode = SDLK_RIGHT;
413 else scancode = SDLK_DOWN;
415 case Qt::Key_Prior: scancode = SDLK_PAGEUP; break;
416 case Qt::Key_Next: scancode = SDLK_PAGEDOWN; break;
417 case Qt::Key_Shift: scancode = SDLK_LSHIFT; break;
418 case Qt::Key_Control: scancode = SDLK_LCTRL; break;
419 case Qt::Key_Meta: scancode = SDLK_LMETA; break;
420 case Qt::Key_Alt: scancode = SDLK_LALT; break;
421 case Qt::Key_CapsLock: scancode = SDLK_CAPSLOCK; break;
422 case Qt::Key_NumLock: scancode = SDLK_NUMLOCK; break;
423 case Qt::Key_ScrollLock: scancode = SDLK_SCROLLOCK; break;
424 case Qt::Key_F1: scancode = SDLK_F1; break;
425 case Qt::Key_F2: scancode = SDLK_F2; break;
426 case Qt::Key_F3: scancode = SDLK_F3; break;
427 case Qt::Key_F4: scancode = SDLK_F4; break;
428 case Qt::Key_F5: scancode = SDLK_F5; break;
429 case Qt::Key_F6: scancode = SDLK_F6; break;
430 case Qt::Key_F7: scancode = SDLK_F7; break;
431 case Qt::Key_F8: scancode = SDLK_F8; break;
432 case Qt::Key_F9: scancode = SDLK_F9; break;
433 case Qt::Key_F10: scancode = SDLK_F10; break;
434 case Qt::Key_F11: scancode = SDLK_F11; break;
435 case Qt::Key_F12: scancode = SDLK_F12; break;
436 case Qt::Key_F13: scancode = SDLK_F13; break;
437 case Qt::Key_F14: scancode = SDLK_F14; break;
438 case Qt::Key_F15: scancode = SDLK_F15; break;
439 case Qt::Key_Super_L: scancode = SDLK_LSUPER; break;
440 case Qt::Key_Super_R: scancode = SDLK_RSUPER; break;
441 case Qt::Key_Menu: scancode = SDLK_MENU; break;
442 case Qt::Key_Help: scancode = SDLK_HELP; break;
445 // FIXME: This is a hack to enable the OK key on
446 // Zaurii devices. SDLK_RETURN is a suitable key to use
447 // since it often is used as such.
449 scancode = SDLK_RETURN;
452 scancode = SDLK_UNKNOWN;
455 keysym.sym = static_cast<SDLKey>(scancode);
457 keysym.sym = static_cast<SDLKey>(scancode);
459 keysym.scancode = scancode;
460 keysym.mod = KMOD_NONE;
461 ButtonState st = e->state();
462 if( (st & ShiftButton) ) { keysym.mod = static_cast<SDLMod>(keysym.mod | KMOD_LSHIFT); }
463 if( (st & ControlButton) ) { keysym.mod = static_cast<SDLMod>(keysym.mod | KMOD_LCTRL); }
464 if( (st & AltButton) ) { keysym.mod = static_cast<SDLMod>(keysym.mod | KMOD_LALT); }
465 if ( SDL_TranslateUNICODE ) {
466 QChar qchar = e->text()[0];
467 keysym.unicode = qchar.unicode();
472 /* NUMLOCK and CAPSLOCK are implemented as double-presses in reality */
473 // if ( (keysym.sym == SDLK_NUMLOCK) || (keysym.sym == SDLK_CAPSLOCK) ) {
477 /* Queue the key event */
479 SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
481 SDL_PrivateKeyboard(SDL_RELEASED, &keysym);
485 void SDL_QWin::setFullscreen(bool fs_on) {
486 my_has_fullscreen = false;
490 void SDL_QWin::enableFullscreen() {
491 // Make sure size is correct
492 if(!my_has_fullscreen) {
493 setFixedSize(qApp->desktop()->size());
494 // This call is needed because showFullScreen won't work
495 // correctly if the widget already considers itself to be fullscreen.
497 // This is needed because showNormal() forcefully changes the window
498 // style to WSTyle_TopLevel.
499 setWFlags(WStyle_Customize | WStyle_NoBorder);
500 // Enable fullscreen.
502 my_has_fullscreen = true;
506 bool SDL_QWin::lockScreen(bool force) {
508 if(force || (isVisible() && isActiveWindow())) {
509 my_painter = new QDirectPainter(this);
514 my_locked++; // Increate lock refcount
518 void SDL_QWin::unlockScreen() {
520 my_locked--; // decrease lock refcount;
522 if(!my_locked && my_painter) {