src/video/qtopia/SDL_QWin.cc
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1665 28193f833b2b
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
    25 #include <qapplication.h>
    25 #include <qapplication.h>
    26 #include <qdirectpainter_qws.h>
    26 #include <qdirectpainter_qws.h>
    27 
    27 
    28 screenRotationT screenRotation = SDL_QT_NO_ROTATION;
    28 screenRotationT screenRotation = SDL_QT_NO_ROTATION;
    29 
    29 
    30 SDL_QWin::SDL_QWin (const QSize & size):
    30 SDL_QWin::SDL_QWin(const QSize & size):
    31 QWidget (0, "SDL_main"),
    31 QWidget(0, "SDL_main"),
    32 my_painter (0),
    32 my_painter(0),
    33 my_image (0),
    33 my_image(0),
    34 my_inhibit_resize (false),
    34 my_inhibit_resize(false),
    35 my_mouse_pos (-1, -1),
    35 my_mouse_pos(-1, -1),
    36 my_flags (0),
    36 my_flags(0),
    37 my_has_fullscreen (false),
    37 my_has_fullscreen(false),
    38 my_locked (0)
    38 my_locked(0)
    39 {
    39 {
    40     setBackgroundMode (NoBackground);
    40     setBackgroundMode(NoBackground);
    41 }
    41 }
    42 
    42 
    43 SDL_QWin::~SDL_QWin ()
    43 SDL_QWin::~SDL_QWin()
    44 {
    44 {
    45     // Nothing to do yet.
    45     // Nothing to do yet.
    46     if (my_image) {
    46     if (my_image) {
    47         delete my_image;
    47         delete my_image;
    48     }
    48     }
    49 }
    49 }
    50 
    50 
    51 void
    51 void
    52 SDL_QWin::setImage (QImage * image)
    52 SDL_QWin::setImage(QImage * image)
    53 {
    53 {
    54     if (my_image) {
    54     if (my_image) {
    55         delete my_image;
    55         delete my_image;
    56     }
    56     }
    57     my_image = image;
    57     my_image = image;
    58     //  setFixedSize(image->size());
    58     //  setFixedSize(image->size());
    59 }
    59 }
    60 
    60 
    61 void
    61 void
    62 SDL_QWin::resizeEvent (QResizeEvent * e)
    62 SDL_QWin::resizeEvent(QResizeEvent * e)
    63 {
    63 {
    64     if (size () != qApp->desktop ()->size ()) {
    64     if (size() != qApp->desktop()->size()) {
    65         // Widget is not the correct size, so do the fullscreen magic
    65         // Widget is not the correct size, so do the fullscreen magic
    66         my_has_fullscreen = false;
    66         my_has_fullscreen = false;
    67         enableFullscreen ();
    67         enableFullscreen();
    68     }
    68     }
    69     if (my_inhibit_resize) {
    69     if (my_inhibit_resize) {
    70         my_inhibit_resize = false;
    70         my_inhibit_resize = false;
    71     } else {
    71     } else {
    72         SDL_PrivateResize (e->size ().width (), e->size ().height ());
    72         SDL_PrivateResize(e->size().width(), e->size().height());
    73     }
    73     }
    74 }
    74 }
    75 
    75 
    76 void
    76 void
    77 SDL_QWin::focusInEvent (QFocusEvent *)
    77 SDL_QWin::focusInEvent(QFocusEvent *)
    78 {
    78 {
    79     // Always do it here, no matter the size.
    79     // Always do it here, no matter the size.
    80     enableFullscreen ();
    80     enableFullscreen();
    81     SDL_PrivateAppActive (true, SDL_APPINPUTFOCUS);
    81     SDL_PrivateAppActive(true, SDL_APPINPUTFOCUS);
    82 }
    82 }
    83 
    83 
    84 void
    84 void
    85 SDL_QWin::focusOutEvent (QFocusEvent *)
    85 SDL_QWin::focusOutEvent(QFocusEvent *)
    86 {
    86 {
    87     my_has_fullscreen = false;
    87     my_has_fullscreen = false;
    88     SDL_PrivateAppActive (false, SDL_APPINPUTFOCUS);
    88     SDL_PrivateAppActive(false, SDL_APPINPUTFOCUS);
    89 }
    89 }
    90 
    90 
    91 void
    91 void
    92 SDL_QWin::closeEvent (QCloseEvent * e)
    92 SDL_QWin::closeEvent(QCloseEvent * e)
    93 {
    93 {
    94     SDL_PrivateQuit ();
    94     SDL_PrivateQuit();
    95     e->ignore ();
    95     e->ignore();
    96 }
    96 }
    97 
    97 
    98 void
    98 void
    99 SDL_QWin::setMousePos (const QPoint & pos)
    99 SDL_QWin::setMousePos(const QPoint & pos)
   100 {
   100 {
   101     if (my_image->width () == height ()) {
   101     if (my_image->width() == height()) {
   102         if (screenRotation == SDL_QT_ROTATION_90)
   102         if (screenRotation == SDL_QT_ROTATION_90)
   103             my_mouse_pos = QPoint (height () - pos.y (), pos.x ());
   103             my_mouse_pos = QPoint(height() - pos.y(), pos.x());
   104         else if (screenRotation == SDL_QT_ROTATION_270)
   104         else if (screenRotation == SDL_QT_ROTATION_270)
   105             my_mouse_pos = QPoint (pos.y (), width () - pos.x ());
   105             my_mouse_pos = QPoint(pos.y(), width() - pos.x());
   106     } else {
   106     } else {
   107         my_mouse_pos = pos;
   107         my_mouse_pos = pos;
   108     }
   108     }
   109 }
   109 }
   110 
   110 
   111 void
   111 void
   112 SDL_QWin::mouseMoveEvent (QMouseEvent * e)
   112 SDL_QWin::mouseMoveEvent(QMouseEvent * e)
   113 {
   113 {
   114     Qt::ButtonState button = e->button ();
   114     Qt::ButtonState button = e->button();
   115     int sdlstate = 0;
   115     int sdlstate = 0;
   116     if ((button & Qt::LeftButton)) {
   116     if ((button & Qt::LeftButton)) {
   117         sdlstate |= SDL_BUTTON_LMASK;
   117         sdlstate |= SDL_BUTTON_LMASK;
   118     }
   118     }
   119     if ((button & Qt::RightButton)) {
   119     if ((button & Qt::RightButton)) {
   120         sdlstate |= SDL_BUTTON_RMASK;
   120         sdlstate |= SDL_BUTTON_RMASK;
   121     }
   121     }
   122     if ((button & Qt::MidButton)) {
   122     if ((button & Qt::MidButton)) {
   123         sdlstate |= SDL_BUTTON_MMASK;
   123         sdlstate |= SDL_BUTTON_MMASK;
   124     }
   124     }
   125     setMousePos (e->pos ());
   125     setMousePos(e->pos());
   126     SDL_PrivateMouseMotion (sdlstate, 0, my_mouse_pos.x (),
   126     SDL_PrivateMouseMotion(sdlstate, 0, my_mouse_pos.x(), my_mouse_pos.y());
   127                             my_mouse_pos.y ());
   127 }
   128 }
   128 
   129 
   129 void
   130 void
   130 SDL_QWin::mousePressEvent(QMouseEvent * e)
   131 SDL_QWin::mousePressEvent (QMouseEvent * e)
   131 {
   132 {
   132     mouseMoveEvent(e);
   133     mouseMoveEvent (e);
   133     Qt::ButtonState button = e->button();
   134     Qt::ButtonState button = e->button ();
   134     SDL_PrivateMouseButton(SDL_PRESSED,
   135     SDL_PrivateMouseButton (SDL_PRESSED,
   135                            (button & Qt::LeftButton) ? 1 :
   136                             (button & Qt::LeftButton) ? 1 :
   136                            ((button & Qt::RightButton) ? 2 : 3),
   137                             ((button & Qt::RightButton) ? 2 : 3),
   137                            my_mouse_pos.x(), my_mouse_pos.y());
   138                             my_mouse_pos.x (), my_mouse_pos.y ());
   138 }
   139 }
   139 
   140 
   140 void
   141 void
   141 SDL_QWin::mouseReleaseEvent(QMouseEvent * e)
   142 SDL_QWin::mouseReleaseEvent (QMouseEvent * e)
   142 {
   143 {
   143     setMousePos(e->pos());
   144     setMousePos (e->pos ());
   144     Qt::ButtonState button = e->button();
   145     Qt::ButtonState button = e->button ();
   145     SDL_PrivateMouseButton(SDL_RELEASED,
   146     SDL_PrivateMouseButton (SDL_RELEASED,
   146                            (button & Qt::LeftButton) ? 1 :
   147                             (button & Qt::LeftButton) ? 1 :
   147                            ((button & Qt::RightButton) ? 2 : 3),
   148                             ((button & Qt::RightButton) ? 2 : 3),
   148                            my_mouse_pos.x(), my_mouse_pos.y());
   149                             my_mouse_pos.x (), my_mouse_pos.y ());
   149     my_mouse_pos = QPoint(-1, -1);
   150     my_mouse_pos = QPoint (-1, -1);
       
   151 }
   150 }
   152 
   151 
   153 static inline void
   152 static inline void
   154 gs_fastRotateBlit_3 (unsigned short *fb,
   153 gs_fastRotateBlit_3(unsigned short *fb,
   155                      unsigned short *bits, const QRect & rect)
   154                     unsigned short *bits, const QRect & rect)
   156 {
   155 {
   157     // FIXME: this only works correctly for 240x320 displays
   156     // FIXME: this only works correctly for 240x320 displays
   158     int startx, starty;
   157     int startx, starty;
   159     int width, height;
   158     int width, height;
   160 
   159 
   161     startx = rect.left () >> 1;
   160     startx = rect.left() >> 1;
   162     starty = rect.top () >> 1;
   161     starty = rect.top() >> 1;
   163     width = ((rect.right () - rect.left ()) >> 1) + 2;
   162     width = ((rect.right() - rect.left()) >> 1) + 2;
   164     height = ((rect.bottom () - rect.top ()) >> 1) + 2;
   163     height = ((rect.bottom() - rect.top()) >> 1) + 2;
   165 
   164 
   166     if ((startx + width) > 120) {
   165     if ((startx + width) > 120) {
   167         width = 120 - startx;   // avoid horizontal overflow
   166         width = 120 - startx;   // avoid horizontal overflow
   168     }
   167     }
   169     if ((starty + height) > 160) {
   168     if ((starty + height) > 160) {
   206         dp2 += rowadd;
   205         dp2 += rowadd;
   207     }
   206     }
   208 }
   207 }
   209 
   208 
   210 static inline void
   209 static inline void
   211 gs_fastRotateBlit_1 (unsigned short *fb,
   210 gs_fastRotateBlit_1(unsigned short *fb,
   212                      unsigned short *bits, const QRect & rect)
   211                     unsigned short *bits, const QRect & rect)
   213 {
   212 {
   214     // FIXME: this only works correctly for 240x320 displays
   213     // FIXME: this only works correctly for 240x320 displays
   215     int startx, starty;
   214     int startx, starty;
   216     int width, height;
   215     int width, height;
   217 
   216 
   218     startx = rect.left () >> 1;
   217     startx = rect.left() >> 1;
   219     starty = rect.top () >> 1;
   218     starty = rect.top() >> 1;
   220     width = ((rect.right () - rect.left ()) >> 1) + 2;
   219     width = ((rect.right() - rect.left()) >> 1) + 2;
   221     height = ((rect.bottom () - rect.top ()) >> 1) + 2;
   220     height = ((rect.bottom() - rect.top()) >> 1) + 2;
   222 
   221 
   223     if ((startx + width) > 120) {
   222     if ((startx + width) > 120) {
   224         width = 120 - startx;   // avoid horizontal overflow
   223         width = 120 - startx;   // avoid horizontal overflow
   225     }
   224     }
   226     if ((starty + height) > 160) {
   225     if ((starty + height) > 160) {
   379     return true;
   378     return true;
   380 }
   379 }
   381 /* *INDENT-ON* */
   380 /* *INDENT-ON* */
   382 
   381 
   383 void
   382 void
   384 SDL_QWin::repaintRect (const QRect & rect)
   383 SDL_QWin::repaintRect(const QRect & rect)
   385 {
   384 {
   386     if (!my_painter || !rect.width () || !rect.height ()) {
   385     if (!my_painter || !rect.width() || !rect.height()) {
   387         return;
   386         return;
   388     }
   387     }
   389 
   388 
   390     if (QPixmap::defaultDepth () == 16) {
   389     if (QPixmap::defaultDepth() == 16) {
   391         switch (my_painter->transformOrientation ()) {
   390         switch (my_painter->transformOrientation()) {
   392         case 3:
   391         case 3:
   393             if (repaintRotation3 (rect)) {
   392             if (repaintRotation3(rect)) {
   394                 return;
   393                 return;
   395             }
   394             }
   396             break;
   395             break;
   397         case 1:
   396         case 1:
   398             if (repaintRotation1 (rect)) {
   397             if (repaintRotation1(rect)) {
   399                 return;
   398                 return;
   400             }
   399             }
   401             break;
   400             break;
   402         case 0:
   401         case 0:
   403             if (repaintRotation0 (rect)) {
   402             if (repaintRotation0(rect)) {
   404                 return;
   403                 return;
   405             }
   404             }
   406             break;
   405             break;
   407         }
   406         }
   408     }
   407     }
   409     my_painter->drawImage (rect.topLeft (), *my_image, rect);
   408     my_painter->drawImage(rect.topLeft(), *my_image, rect);
   410 }
   409 }
   411 
   410 
   412 // This paints the current buffer to the screen, when desired. 
   411 // This paints the current buffer to the screen, when desired. 
   413 void
   412 void
   414 SDL_QWin::paintEvent (QPaintEvent * ev)
   413 SDL_QWin::paintEvent(QPaintEvent * ev)
   415 {
   414 {
   416     if (my_image) {
   415     if (my_image) {
   417         lockScreen (true);
   416         lockScreen(true);
   418         repaintRect (ev->rect ());
   417         repaintRect(ev->rect());
   419         unlockScreen ();
   418         unlockScreen();
   420     }
   419     }
   421 }
   420 }
   422 
   421 
   423 /* Function to translate a keyboard transition and queue the key event
   422 /* Function to translate a keyboard transition and queue the key event
   424  * This should probably be a table although this method isn't exactly
   423  * This should probably be a table although this method isn't exactly
   425  * slow.
   424  * slow.
   426  */
   425  */
   427 void
   426 void
   428 SDL_QWin::QueueKey (QKeyEvent * e, int pressed)
   427 SDL_QWin::QueueKey(QKeyEvent * e, int pressed)
   429 {
   428 {
   430     SDL_keysym keysym;
   429     SDL_keysym keysym;
   431     int scancode = e->key ();
   430     int scancode = e->key();
   432     /* Set the keysym information */
   431     /* Set the keysym information */
   433     if (scancode >= 'A' && scancode <= 'Z') {
   432     if (scancode >= 'A' && scancode <= 'Z') {
   434         // Qt sends uppercase, SDL wants lowercase
   433         // Qt sends uppercase, SDL wants lowercase
   435         keysym.sym = static_cast < SDLKey > (scancode + 32);
   434         keysym.sym = static_cast < SDLKey > (scancode + 32);
   436     } else if (scancode >= 0x1000) {
   435     } else if (scancode >= 0x1000) {
   605     } else {
   604     } else {
   606         keysym.sym = static_cast < SDLKey > (scancode);
   605         keysym.sym = static_cast < SDLKey > (scancode);
   607     }
   606     }
   608     keysym.scancode = scancode;
   607     keysym.scancode = scancode;
   609     keysym.mod = KMOD_NONE;
   608     keysym.mod = KMOD_NONE;
   610     ButtonState st = e->state ();
   609     ButtonState st = e->state();
   611     if ((st & ShiftButton)) {
   610     if ((st & ShiftButton)) {
   612         keysym.mod = static_cast < SDLMod > (keysym.mod | KMOD_LSHIFT);
   611         keysym.mod = static_cast < SDLMod > (keysym.mod | KMOD_LSHIFT);
   613     }
   612     }
   614     if ((st & ControlButton)) {
   613     if ((st & ControlButton)) {
   615         keysym.mod = static_cast < SDLMod > (keysym.mod | KMOD_LCTRL);
   614         keysym.mod = static_cast < SDLMod > (keysym.mod | KMOD_LCTRL);
   616     }
   615     }
   617     if ((st & AltButton)) {
   616     if ((st & AltButton)) {
   618         keysym.mod = static_cast < SDLMod > (keysym.mod | KMOD_LALT);
   617         keysym.mod = static_cast < SDLMod > (keysym.mod | KMOD_LALT);
   619     }
   618     }
   620     if (SDL_TranslateUNICODE) {
   619     if (SDL_TranslateUNICODE) {
   621         QChar qchar = e->text ()[0];
   620         QChar qchar = e->text()[0];
   622         keysym.unicode = qchar.unicode ();
   621         keysym.unicode = qchar.unicode();
   623     } else {
   622     } else {
   624         keysym.unicode = 0;
   623         keysym.unicode = 0;
   625     }
   624     }
   626 
   625 
   627     /* NUMLOCK and CAPSLOCK are implemented as double-presses in reality */
   626     /* NUMLOCK and CAPSLOCK are implemented as double-presses in reality */
   629     //            pressed = 1;
   628     //            pressed = 1;
   630     //    }
   629     //    }
   631 
   630 
   632     /* Queue the key event */
   631     /* Queue the key event */
   633     if (pressed) {
   632     if (pressed) {
   634         SDL_PrivateKeyboard (SDL_PRESSED, &keysym);
   633         SDL_PrivateKeyboard(SDL_PRESSED, &keysym);
   635     } else {
   634     } else {
   636         SDL_PrivateKeyboard (SDL_RELEASED, &keysym);
   635         SDL_PrivateKeyboard(SDL_RELEASED, &keysym);
   637     }
   636     }
   638 }
   637 }
   639 
   638 
   640 void
   639 void
   641 SDL_QWin::setFullscreen (bool fs_on)
   640 SDL_QWin::setFullscreen(bool fs_on)
   642 {
   641 {
   643     my_has_fullscreen = false;
   642     my_has_fullscreen = false;
   644     enableFullscreen ();
   643     enableFullscreen();
   645 }
   644 }
   646 
   645 
   647 void
   646 void
   648 SDL_QWin::enableFullscreen ()
   647 SDL_QWin::enableFullscreen()
   649 {
   648 {
   650     // Make sure size is correct
   649     // Make sure size is correct
   651     if (!my_has_fullscreen) {
   650     if (!my_has_fullscreen) {
   652         setFixedSize (qApp->desktop ()->size ());
   651         setFixedSize(qApp->desktop()->size());
   653         // This call is needed because showFullScreen won't work
   652         // This call is needed because showFullScreen won't work
   654         // correctly if the widget already considers itself to be fullscreen.
   653         // correctly if the widget already considers itself to be fullscreen.
   655         showNormal ();
   654         showNormal();
   656         // This is needed because showNormal() forcefully changes the window
   655         // This is needed because showNormal() forcefully changes the window
   657         // style to WSTyle_TopLevel.
   656         // style to WSTyle_TopLevel.
   658         setWFlags (WStyle_Customize | WStyle_NoBorder);
   657         setWFlags(WStyle_Customize | WStyle_NoBorder);
   659         // Enable fullscreen.
   658         // Enable fullscreen.
   660         showFullScreen ();
   659         showFullScreen();
   661         my_has_fullscreen = true;
   660         my_has_fullscreen = true;
   662     }
   661     }
   663 }
   662 }
   664 
   663 
   665 /* *INDENT-OFF* */
   664 /* *INDENT-OFF* */
   677     return true;
   676     return true;
   678 }
   677 }
   679 /* *INDENT-ON* */
   678 /* *INDENT-ON* */
   680 
   679 
   681 void
   680 void
   682 SDL_QWin::unlockScreen ()
   681 SDL_QWin::unlockScreen()
   683 {
   682 {
   684     if (my_locked > 0) {
   683     if (my_locked > 0) {
   685         my_locked--;            // decrease lock refcount;
   684         my_locked--;            // decrease lock refcount;
   686     }
   685     }
   687     if (!my_locked && my_painter) {
   686     if (!my_locked && my_painter) {
   688         my_painter->end ();
   687         my_painter->end();
   689         delete my_painter;
   688         delete my_painter;
   690         my_painter = 0;
   689         my_painter = 0;
   691     }
   690     }
   692 }
   691 }
   693 
   692