This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_QWin.cc
694 lines (648 loc) · 19.6 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2006 Sam Lantinga
4
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.
9
10
11
12
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
13
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
21
Sam Lantinga
slouken@libsdl.org
*/
22
#include "SDL_config.h"
23
24
25
26
#include "SDL_QWin.h"
#include <qapplication.h>
#include <qdirectpainter_qws.h>
27
28
29
screenRotationT screenRotation = SDL_QT_NO_ROTATION;
30
31
32
33
34
35
36
37
38
SDL_QWin::SDL_QWin (const QSize & size):
QWidget (0, "SDL_main"),
my_painter (0),
my_image (0),
my_inhibit_resize (false),
my_mouse_pos (-1, -1),
my_flags (0),
my_has_fullscreen (false),
my_locked (0)
39
{
40
setBackgroundMode (NoBackground);
41
42
}
43
44
45
46
47
48
SDL_QWin::~SDL_QWin ()
{
// Nothing to do yet.
if (my_image) {
delete my_image;
}
49
50
}
51
52
53
54
55
56
57
58
void
SDL_QWin::setImage (QImage * image)
{
if (my_image) {
delete my_image;
}
my_image = image;
// setFixedSize(image->size());
59
60
}
61
62
63
64
65
66
67
68
69
70
71
72
73
void
SDL_QWin::resizeEvent (QResizeEvent * e)
{
if (size () != qApp->desktop ()->size ()) {
// Widget is not the correct size, so do the fullscreen magic
my_has_fullscreen = false;
enableFullscreen ();
}
if (my_inhibit_resize) {
my_inhibit_resize = false;
} else {
SDL_PrivateResize (e->size ().width (), e->size ().height ());
}
74
75
}
76
77
78
79
80
81
void
SDL_QWin::focusInEvent (QFocusEvent *)
{
// Always do it here, no matter the size.
enableFullscreen ();
SDL_PrivateAppActive (true, SDL_APPINPUTFOCUS);
82
83
}
84
85
86
87
88
void
SDL_QWin::focusOutEvent (QFocusEvent *)
{
my_has_fullscreen = false;
SDL_PrivateAppActive (false, SDL_APPINPUTFOCUS);
89
90
}
91
92
93
94
95
void
SDL_QWin::closeEvent (QCloseEvent * e)
{
SDL_PrivateQuit ();
e->ignore ();
96
97
}
98
99
100
101
102
103
104
105
106
107
108
void
SDL_QWin::setMousePos (const QPoint & pos)
{
if (my_image->width () == height ()) {
if (screenRotation == SDL_QT_ROTATION_90)
my_mouse_pos = QPoint (height () - pos.y (), pos.x ());
else if (screenRotation == SDL_QT_ROTATION_270)
my_mouse_pos = QPoint (pos.y (), width () - pos.x ());
} else {
my_mouse_pos = pos;
}
109
110
}
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
void
SDL_QWin::mouseMoveEvent (QMouseEvent * e)
{
Qt::ButtonState button = e->button ();
int sdlstate = 0;
if ((button & Qt::LeftButton)) {
sdlstate |= SDL_BUTTON_LMASK;
}
if ((button & Qt::RightButton)) {
sdlstate |= SDL_BUTTON_RMASK;
}
if ((button & Qt::MidButton)) {
sdlstate |= SDL_BUTTON_MMASK;
}
setMousePos (e->pos ());
SDL_PrivateMouseMotion (sdlstate, 0, my_mouse_pos.x (),
my_mouse_pos.y ());
128
129
}
130
131
132
133
134
135
136
137
138
void
SDL_QWin::mousePressEvent (QMouseEvent * e)
{
mouseMoveEvent (e);
Qt::ButtonState button = e->button ();
SDL_PrivateMouseButton (SDL_PRESSED,
(button & Qt::LeftButton) ? 1 :
((button & Qt::RightButton) ? 2 : 3),
my_mouse_pos.x (), my_mouse_pos.y ());
139
140
}
141
142
143
144
145
146
147
148
149
150
void
SDL_QWin::mouseReleaseEvent (QMouseEvent * e)
{
setMousePos (e->pos ());
Qt::ButtonState button = e->button ();
SDL_PrivateMouseButton (SDL_RELEASED,
(button & Qt::LeftButton) ? 1 :
((button & Qt::RightButton) ? 2 : 3),
my_mouse_pos.x (), my_mouse_pos.y ());
my_mouse_pos = QPoint (-1, -1);
151
152
}
153
static inline void
154
155
gs_fastRotateBlit_3 (unsigned short *fb,
unsigned short *bits, const QRect & rect)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// FIXME: this only works correctly for 240x320 displays
int startx, starty;
int width, height;
startx = rect.left () >> 1;
starty = rect.top () >> 1;
width = ((rect.right () - rect.left ()) >> 1) + 2;
height = ((rect.bottom () - rect.top ()) >> 1) + 2;
if ((startx + width) > 120) {
width = 120 - startx; // avoid horizontal overflow
}
if ((starty + height) > 160) {
height = 160 - starty; // avoid vertical overflow
}
ulong *sp1, *sp2, *dp1, *dp2;
ulong stop, sbot, dtop, dbot;
sp1 = (ulong *) bits + startx + starty * 240;
sp2 = sp1 + 120;
dp1 = (ulong *) fb + (159 - starty) + startx * 320;
dp2 = dp1 + 160;
int rowadd = (-320 * width) - 1;
int rowadd2 = 240 - width;
// transfer in cells of 2x2 pixels in words
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// read source pixels
stop = *sp1;
sbot = *sp2;
// rotate pixels
dtop = (sbot & 0xffff) + ((stop & 0xffff) << 16);
dbot = ((sbot & 0xffff0000) >> 16) + (stop & 0xffff0000);
// write to framebuffer
*dp1 = dtop;
*dp2 = dbot;
// update source ptrs
sp1++;
sp2++;
// update dest ptrs - 2 pix at a time
dp1 += 320;
dp2 += 320;
}
// adjust src ptrs - skip a row as we work in pairs
sp1 += rowadd2;
sp2 += rowadd2;
// adjust dest ptrs for rotation
dp1 += rowadd;
dp2 += rowadd;
207
208
209
}
}
210
static inline void
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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
gs_fastRotateBlit_1 (unsigned short *fb,
unsigned short *bits, const QRect & rect)
{
// FIXME: this only works correctly for 240x320 displays
int startx, starty;
int width, height;
startx = rect.left () >> 1;
starty = rect.top () >> 1;
width = ((rect.right () - rect.left ()) >> 1) + 2;
height = ((rect.bottom () - rect.top ()) >> 1) + 2;
if ((startx + width) > 120) {
width = 120 - startx; // avoid horizontal overflow
}
if ((starty + height) > 160) {
height = 160 - starty; // avoid vertical overflow
}
ulong *sp1, *sp2, *dp1, *dp2;
ulong stop, sbot, dtop, dbot;
fb += 320 * 239; // Move "fb" to top left corner
sp1 = (ulong *) bits + startx + starty * 240;
sp2 = sp1 + 120;
dp1 = (ulong *) fb - startx * 320 - starty;
dp2 = dp1 - 160;
int rowadd = (320 * width) + 1;
int rowadd2 = 240 - width;
// transfer in cells of 2x2 pixels in words
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// read
stop = *sp1;
sbot = *sp2;
// rotate
dtop = (stop & 0xffff) + ((sbot & 0xffff) << 16);
dbot = ((stop & 0xffff0000) >> 16) + (sbot & 0xffff0000);
// write
*dp1 = dtop;
*dp2 = dbot;
// update source ptrs
sp1++;
sp2++;
// update dest ptrs - 2 pix at a time
dp1 -= 320;
dp2 -= 320;
}
// adjust src ptrs - skip a row as we work in pairs
sp1 += rowadd2;
sp2 += rowadd2;
// adjust dest ptrs for rotation
dp1 += rowadd;
dp2 += rowadd;
264
265
266
267
}
}
// desktop, SL-A300 etc
268
269
/* *INDENT-OFF* */
bool SDL_QWin::repaintRotation0 (const QRect & rect)
270
271
{
if (my_image->width () == width ()) {
272
273
uchar * fb = (uchar *) my_painter->frameBuffer ();
uchar * buf = (uchar *) my_image->bits ();
274
275
276
if (rect == my_image->rect ()) {
SDL_memcpy (fb, buf, width () * height () * 2);
} else {
277
278
279
280
int h = rect.height ();
int wd = rect.width () << 1;
int fblineadd = my_painter->lineStep ();
int buflineadd = my_image->bytesPerLine ();
281
fb += (rect.left () << 1) + rect.top () * my_painter->lineStep ();
282
buf += (rect.left () << 1) + rect.top () * my_image->bytesPerLine ();
283
284
285
286
287
288
while (h--) {
SDL_memcpy (fb, buf, wd);
fb += fblineadd;
buf += buflineadd;
}
}
289
} else {
290
return false; // FIXME: Landscape
291
}
292
#ifdef __i386__
293
my_painter->fillRect (rect, QBrush (Qt::NoBrush));
294
#endif
295
return true;
296
}
297
/* *INDENT-ON* */
298
299
300
// Sharp Zaurus SL-5500 etc
301
302
/* *INDENT-OFF* */
bool SDL_QWin::repaintRotation3 (const QRect & rect)
303
304
{
if (my_image->width () == width ()) {
305
306
ushort * fb = (ushort *) my_painter->frameBuffer ();
ushort * buf = (ushort *) my_image->bits ();
307
308
309
310
gs_fastRotateBlit_3 (fb, buf, rect);
} else {
// landscape mode
if (screenRotation == SDL_QT_ROTATION_90) {
311
312
uchar * fb = (uchar *) my_painter->frameBuffer ();
uchar * buf = (uchar *) my_image->bits ();
313
314
315
if (rect == my_image->rect ()) {
SDL_memcpy (fb, buf, width () * height () * 2);
} else {
316
317
318
319
int h = rect.height ();
int wd = rect.width () << 1;
int fblineadd = my_painter->lineStep ();
int buflineadd = my_image->bytesPerLine ();
320
321
322
323
324
325
326
327
328
329
330
331
332
fb +=
(rect.left () << 1) +
rect.top () * my_painter->lineStep ();
buf +=
(rect.left () << 1) +
rect.top () * my_image->bytesPerLine ();
while (h--) {
SDL_memcpy (fb, buf, wd);
fb += fblineadd;
buf += buflineadd;
}
}
} else if (screenRotation == SDL_QT_ROTATION_270) {
333
334
335
336
337
338
int h = rect.height ();
int wd = rect.width ();
int fblineadd = my_painter->lineStep () - (rect.width () << 1);
int buflineadd = my_image->bytesPerLine () - (rect.width () << 1);
int w;
339
340
uchar * fb = (uchar *) my_painter->frameBuffer ();
uchar * buf = (uchar *) my_image->bits ();
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
fb +=
((my_painter->width () -
(rect.top () +
rect.height ())) * my_painter->lineStep ()) +
((my_painter->height () -
((rect.left () + rect.width ()))) << 1);
buf +=
my_image->bytesPerLine () * (rect.top () +
rect.height ()) -
(((my_image->width () -
(rect.left () + rect.width ())) << 1) + 2);
while (h--) {
w = wd;
while (w--)
*((unsigned short *) fb)++ = *((unsigned short *) buf)--;
fb += fblineadd;
buf -= buflineadd;
}
}
363
}
364
return true;
365
}
366
/* *INDENT-ON* */
367
368
// ipaq 3800...
369
370
/* *INDENT-OFF* */
bool SDL_QWin::repaintRotation1 (const QRect & rect)
371
372
{
if (my_image->width () == width ()) {
373
374
ushort *fb = (ushort *) my_painter->frameBuffer ();
ushort *buf = (ushort *) my_image->bits ();
375
376
377
378
379
gs_fastRotateBlit_1 (fb, buf, rect);
} else {
return false; // FIXME: landscape mode
}
return true;
380
}
381
/* *INDENT-ON* */
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
void
SDL_QWin::repaintRect (const QRect & rect)
{
if (!my_painter || !rect.width () || !rect.height ()) {
return;
}
if (QPixmap::defaultDepth () == 16) {
switch (my_painter->transformOrientation ()) {
case 3:
if (repaintRotation3 (rect)) {
return;
}
break;
case 1:
if (repaintRotation1 (rect)) {
return;
}
break;
case 0:
if (repaintRotation0 (rect)) {
return;
}
break;
}
408
}
409
my_painter->drawImage (rect.topLeft (), *my_image, rect);
410
411
412
}
// This paints the current buffer to the screen, when desired.
413
414
415
416
417
418
419
420
421
void
SDL_QWin::paintEvent (QPaintEvent * ev)
{
if (my_image) {
lockScreen (true);
repaintRect (ev->rect ());
unlockScreen ();
}
}
422
423
424
425
426
/* Function to translate a keyboard transition and queue the key event
* This should probably be a table although this method isn't exactly
* slow.
*/
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
void
SDL_QWin::QueueKey (QKeyEvent * e, int pressed)
{
SDL_keysym keysym;
int scancode = e->key ();
/* Set the keysym information */
if (scancode >= 'A' && scancode <= 'Z') {
// Qt sends uppercase, SDL wants lowercase
keysym.sym = static_cast < SDLKey > (scancode + 32);
} else if (scancode >= 0x1000) {
// Special keys
switch (scancode) {
case Qt::Key_Escape:
scancode = SDLK_ESCAPE;
break;
case Qt::Key_Tab:
scancode = SDLK_TAB;
break;
case Qt::Key_Backspace:
scancode = SDLK_BACKSPACE;
break;
case Qt::Key_Return:
scancode = SDLK_RETURN;
break;
case Qt::Key_Enter:
scancode = SDLK_KP_ENTER;
break;
case Qt::Key_Insert:
scancode = SDLK_INSERT;
break;
case Qt::Key_Delete:
scancode = SDLK_DELETE;
break;
case Qt::Key_Pause:
scancode = SDLK_PAUSE;
break;
case Qt::Key_Print:
scancode = SDLK_PRINT;
break;
case Qt::Key_SysReq:
scancode = SDLK_SYSREQ;
break;
case Qt::Key_Home:
scancode = SDLK_HOME;
break;
case Qt::Key_End:
scancode = SDLK_END;
break;
// We want the control keys to rotate with the screen
case Qt::Key_Left:
if (screenRotation == SDL_QT_ROTATION_90)
scancode = SDLK_UP;
else if (screenRotation == SDL_QT_ROTATION_270)
scancode = SDLK_DOWN;
else
scancode = SDLK_LEFT;
break;
case Qt::Key_Up:
if (screenRotation == SDL_QT_ROTATION_90)
scancode = SDLK_RIGHT;
else if (screenRotation == SDL_QT_ROTATION_270)
scancode = SDLK_LEFT;
else
scancode = SDLK_UP;
break;
case Qt::Key_Right:
if (screenRotation == SDL_QT_ROTATION_90)
scancode = SDLK_DOWN;
else if (screenRotation == SDL_QT_ROTATION_270)
scancode = SDLK_UP;
else
scancode = SDLK_RIGHT;
break;
case Qt::Key_Down:
if (screenRotation == SDL_QT_ROTATION_90)
scancode = SDLK_LEFT;
else if (screenRotation == SDL_QT_ROTATION_270)
scancode = SDLK_RIGHT;
else
scancode = SDLK_DOWN;
break;
case Qt::Key_Prior:
scancode = SDLK_PAGEUP;
break;
case Qt::Key_Next:
scancode = SDLK_PAGEDOWN;
break;
case Qt::Key_Shift:
scancode = SDLK_LSHIFT;
break;
case Qt::Key_Control:
scancode = SDLK_LCTRL;
break;
case Qt::Key_Meta:
scancode = SDLK_LMETA;
break;
case Qt::Key_Alt:
scancode = SDLK_LALT;
break;
case Qt::Key_CapsLock:
scancode = SDLK_CAPSLOCK;
break;
case Qt::Key_NumLock:
scancode = SDLK_NUMLOCK;
break;
case Qt::Key_ScrollLock:
scancode = SDLK_SCROLLOCK;
break;
case Qt::Key_F1:
scancode = SDLK_F1;
break;
case Qt::Key_F2:
scancode = SDLK_F2;
break;
case Qt::Key_F3:
scancode = SDLK_F3;
break;
case Qt::Key_F4:
scancode = SDLK_F4;
break;
case Qt::Key_F5:
scancode = SDLK_F5;
break;
case Qt::Key_F6:
scancode = SDLK_F6;
break;
case Qt::Key_F7:
scancode = SDLK_F7;
break;
case Qt::Key_F8:
scancode = SDLK_F8;
break;
case Qt::Key_F9:
scancode = SDLK_F9;
break;
case Qt::Key_F10:
scancode = SDLK_F10;
break;
case Qt::Key_F11:
scancode = SDLK_F11;
break;
case Qt::Key_F12:
scancode = SDLK_F12;
break;
case Qt::Key_F13:
scancode = SDLK_F13;
break;
case Qt::Key_F14:
scancode = SDLK_F14;
break;
case Qt::Key_F15:
scancode = SDLK_F15;
break;
case Qt::Key_Super_L:
scancode = SDLK_LSUPER;
break;
case Qt::Key_Super_R:
scancode = SDLK_RSUPER;
break;
case Qt::Key_Menu:
scancode = SDLK_MENU;
break;
case Qt::Key_Help:
scancode = SDLK_HELP;
break;
case Qt::Key_F33:
// FIXME: This is a hack to enable the OK key on
// Zaurii devices. SDLK_RETURN is a suitable key to use
// since it often is used as such.
// david@hedbor.org
scancode = SDLK_RETURN;
break;
default:
scancode = SDLK_UNKNOWN;
break;
}
keysym.sym = static_cast < SDLKey > (scancode);
} else {
keysym.sym = static_cast < SDLKey > (scancode);
}
keysym.scancode = scancode;
keysym.mod = KMOD_NONE;
ButtonState st = e->state ();
if ((st & ShiftButton)) {
keysym.mod = static_cast < SDLMod > (keysym.mod | KMOD_LSHIFT);
}
if ((st & ControlButton)) {
keysym.mod = static_cast < SDLMod > (keysym.mod | KMOD_LCTRL);
}
if ((st & AltButton)) {
keysym.mod = static_cast < SDLMod > (keysym.mod | KMOD_LALT);
}
if (SDL_TranslateUNICODE) {
QChar qchar = e->text ()[0];
keysym.unicode = qchar.unicode ();
} else {
keysym.unicode = 0;
}
/* NUMLOCK and CAPSLOCK are implemented as double-presses in reality */
// if ( (keysym.sym == SDLK_NUMLOCK) || (keysym.sym == SDLK_CAPSLOCK) ) {
// pressed = 1;
// }
/* Queue the key event */
if (pressed) {
SDL_PrivateKeyboard (SDL_PRESSED, &keysym);
} else {
SDL_PrivateKeyboard (SDL_RELEASED, &keysym);
637
638
639
}
}
640
641
642
643
644
void
SDL_QWin::setFullscreen (bool fs_on)
{
my_has_fullscreen = false;
enableFullscreen ();
645
646
}
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
void
SDL_QWin::enableFullscreen ()
{
// Make sure size is correct
if (!my_has_fullscreen) {
setFixedSize (qApp->desktop ()->size ());
// This call is needed because showFullScreen won't work
// correctly if the widget already considers itself to be fullscreen.
showNormal ();
// This is needed because showNormal() forcefully changes the window
// style to WSTyle_TopLevel.
setWFlags (WStyle_Customize | WStyle_NoBorder);
// Enable fullscreen.
showFullScreen ();
my_has_fullscreen = true;
}
663
}
664
665
/* *INDENT-OFF* */
666
667
bool
SDL_QWin::lockScreen (bool force)
668
669
670
671
672
673
674
{
if (!my_painter) {
if (force || (isVisible () && isActiveWindow ())) {
my_painter = new QDirectPainter (this);
} else {
return false;
}
675
}
676
677
my_locked++; // Increate lock refcount
return true;
678
}
679
/* *INDENT-ON* */
680
681
682
683
684
685
686
687
688
689
690
691
void
SDL_QWin::unlockScreen ()
{
if (my_locked > 0) {
my_locked--; // decrease lock refcount;
}
if (!my_locked && my_painter) {
my_painter->end ();
delete my_painter;
my_painter = 0;
}
692
}
693
694
/* vi: set ts=4 sw=4 expandtab: */