File: SDLAppHandler.cpp

package info (click to toggle)
adanaxisgpl 1.2.5.dfsg.1-6
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, sid, stretch
  • size: 39,988 kB
  • sloc: ansic: 69,023; cpp: 65,175; ruby: 14,217; xml: 8,416; sh: 7,434; perl: 2,233; objc: 256; makefile: 154; yacc: 92
file content (843 lines) | stat: -rw-r--r-- 22,863 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
//%Header {
/*****************************************************************************
 *
 * File: src/GL/SDLAppHandler.cpp
 *
 * Author: Andy Southgate 2002-2007
 *
 * This file contains original work by Andy Southgate.  The author and his
 * employer (Mushware Limited) irrevocably waive all of their copyright rights
 * vested in this particular version of this file to the furthest extent
 * permitted.  The author and Mushware Limited also irrevocably waive any and
 * all of their intellectual property rights arising from said file and its
 * creation that would otherwise restrict the rights of any party to use and/or
 * distribute the use of, the techniques and methods used herein.  A written
 * waiver can be obtained via http://www.mushware.com/.
 *
 * This software carries NO WARRANTY of any kind.
 *
 ****************************************************************************/
//%Header } CkSk48jyH/Lvp9pXNnIBSQ
/*
 * $Id: SDLAppHandler.cpp,v 1.71 2007/06/29 12:05:01 southa Exp $
 * $Log: SDLAppHandler.cpp,v $
 * Revision 1.71  2007/06/29 12:05:01  southa
 * Mandriva packaging
 *
 * Revision 1.70  2007/06/28 15:15:14  southa
 * Mandriva fixes
 *
 * Revision 1.69  2007/06/26 16:27:50  southa
 * X11 tweaks
 *
 * Revision 1.68  2007/06/25 17:58:47  southa
 * X11 fixes
 *
 * Revision 1.67  2007/04/18 12:44:37  southa
 * Cache purge fix and pre-release tweaks
 *
 * Revision 1.66  2007/03/09 19:50:12  southa
 * Resident textures
 *
 * Revision 1.65  2006/11/25 21:26:32  southa
 * Display mode definitions
 *
 * Revision 1.64  2006/11/08 18:30:54  southa
 * Key and axis configuration
 *
 * Revision 1.63  2006/08/02 16:25:54  southa
 * Prerelease work
 *
 * Revision 1.62  2006/07/28 16:52:20  southa
 * Options work
 *
 * Revision 1.61  2006/07/27 13:51:35  southa
 * Menu and control fixes
 *
 * Revision 1.60  2006/07/24 18:46:48  southa
 * Depth sorting
 *
 * Revision 1.59  2006/07/21 10:52:06  southa
 * win32 build fixes
 *
 * Revision 1.58  2006/07/18 16:58:37  southa
 * Texture fixes
 *
 * Revision 1.57  2006/07/17 14:43:39  southa
 * Billboarded deco objects
 *
 * Revision 1.56  2006/07/11 19:49:04  southa
 * Control menu
 *
 * Revision 1.55  2006/07/08 16:05:55  southa
 * Ruby menus and key handling
 *
 * Revision 1.54  2006/07/07 18:13:57  southa
 * Menu start and stop
 *
 * Revision 1.53  2006/07/04 16:55:27  southa
 * Ruby key handling
 *
 * Revision 1.52  2006/07/02 09:43:27  southa
 * MushGLFont work
 *
 * Revision 1.51  2006/06/30 17:26:10  southa
 * Render prelude
 *
 * Revision 1.50  2006/06/01 15:38:55  southa
 * DrawArray verification and fixes
 *
 * Revision 1.49  2005/07/14 12:50:30  southa
 * Extrusion work
 *
 * Revision 1.48  2005/07/08 12:07:07  southa
 * MushGaem control work
 *
 * Revision 1.47  2005/06/06 15:07:09  southa
 * X11 work
 *
 * Revision 1.46  2005/05/27 12:48:36  southa
 * Registration box tweaks
 *
 * Revision 1.45  2005/05/26 16:05:29  southa
 * win32 support
 *
 * Revision 1.44  2005/05/19 13:02:01  southa
 * Mac release work
 *
 * Revision 1.43  2005/04/10 00:09:22  southa
 * Registration
 *
 * Revision 1.42  2005/04/01 00:02:02  southa
 * Build tweaks
 *
 * Revision 1.41  2005/03/25 22:04:48  southa
 * Dialogue and MushcoreIO fixes
 *
 * Revision 1.40  2005/03/13 00:34:46  southa
 * Build fixes, key support and stereo
 *
 * Revision 1.39  2005/01/26 00:48:46  southa
 * MushMeshGroup and rendering
 *
 * Revision 1.38  2004/01/02 21:13:06  southa
 * Source conditioning
 *
 * Revision 1.37  2003/09/17 19:40:30  southa
 * Source conditioning upgrades
 *
 * Revision 1.36  2003/08/21 23:08:33  southa
 * Fixed file headers
 *
 * Revision 1.35  2003/04/13 08:39:18  southa
 * Bring window to foreground on mode change
 *
 * Revision 1.34  2003/01/20 10:45:24  southa
 * Singleton tidying
 *
 * Revision 1.33  2003/01/13 14:31:56  southa
 * Build frameworks for Mac OS X
 *
 * Revision 1.32  2003/01/12 17:32:51  southa
 * Mushcore work
 *
 * Revision 1.31  2003/01/09 14:56:59  southa
 * Created Mushcore
 *
 * Revision 1.30  2002/12/29 20:59:53  southa
 * More build fixes
 *
 * Revision 1.29  2002/12/20 13:17:37  southa
 * Namespace changes, licence changes and source conditioning
 *
 * Revision 1.28  2002/12/03 20:28:15  southa
 * Network, player and control work
 *
 * Revision 1.27  2002/11/18 18:55:56  southa
 * Game resume and quit
 *
 * Revision 1.26  2002/10/22 20:42:02  southa
 * Source conditioning
 *
 * Revision 1.25  2002/10/17 15:50:58  southa
 * Config saving, pause and quit
 *
 * Revision 1.24  2002/10/15 14:02:30  southa
 * Mode changes
 *
 * Revision 1.23  2002/10/14 18:13:17  southa
 * GLModeDef work
 *
 * Revision 1.22  2002/10/12 15:25:11  southa
 * Facet renderer
 *
 * Revision 1.21  2002/10/06 22:09:59  southa
 * Initial lighting test
 *
 * Revision 1.20  2002/09/02 14:48:02  southa
 * RPM building
 *
 * Revision 1.19  2002/09/01 16:29:18  southa
 * Support Redhat paths for include files
 * Revision 1.18.4.1  2002/09/04 10:18:04  southa
 * Fixed for MacOS X 10.2
 *
 * Revision 1.18  2002/08/27 08:56:22  southa
 * Source conditioning
 *
 * Revision 1.17  2002/08/17 10:41:50  southa
 * Designer fixes
 *
 * Revision 1.16  2002/08/10 12:34:46  southa
 * Added current dialogues
 *
 * Revision 1.15  2002/08/07 13:36:48  southa
 * Conditioned source
 *
 * Revision 1.14  2002/08/05 22:30:19  southa
 * Made mode selection more robust
 *
 * Revision 1.13  2002/08/02 15:20:54  southa
 * Frame rate timing
 *
 * Revision 1.11  2002/07/31 16:27:16  southa
 * Collision checking work
 *
 * Revision 1.10  2002/07/19 16:25:21  southa
 * Texture tweaks
 *
 * Revision 1.9  2002/07/19 15:44:40  southa
 * Graphic optimisations
 *
 * Revision 1.8  2002/07/10 16:37:39  southa
 * Cursor removal
 *
 * Revision 1.7  2002/07/08 14:22:02  southa
 * Rotated desks
 *
 * Revision 1.6  2002/07/07 13:44:18  southa
 * Screen size config variables
 *
 * Revision 1.5  2002/07/06 18:04:18  southa
 * More designer work
 *
 * Revision 1.4  2002/07/02 18:36:56  southa
 * Selection in designer, mouse buttons
 *
 * Revision 1.3  2002/06/27 12:36:06  southa
 * Build process fixes
 *
 * Revision 1.2  2002/06/23 10:42:33  southa
 * SDL input
 *
 * Revision 1.1  2002/06/21 18:50:16  southa
 * SDLAppHandler added
 *
 */

#include "SDLAppHandler.h"

#include "GLAppSignal.h"
#include "GLStandard.h"
#include "GLState.h"
#include "GLUtils.h"

#include "mushMedia.h"
#include "mushPlatform.h"

#include "GLSTL.h"

using namespace Mushware;
using namespace std;


SDLAppHandler::SDLAppHandler():
    m_redisplay(false),
    m_visible(true),
    m_keyState(MediaKeyboard::kNumKeys, false),
    m_latchedKeyState(MediaKeyboard::kNumKeys, false),
    m_mouseX(0),
    m_mouseY(0),
    m_unboundedMouseX(0),
    m_unboundedMouseY(0),
    m_controlBuffer(kControlBufferSize, SDLControlEntry(0)),
    m_controlBufferIndex(0),
    m_firstDelta(true),
    m_mouseSensitivity(1.0),
    m_screenEntered(false),
    m_shiftAtStartupPressed(false)
{
    m_deviceList.resize(kNumDevices);
    for (U32 i=0; i<kNumDevices; ++i)
    {
        m_deviceList[i].resize(kAxesPerDevice, 0);
    }
}

void
SDLAppHandler::Initialise(void)
{
    m_mouseX=0;
    m_mouseY=0;
    m_unboundedMouseX=0;
    m_unboundedMouseY=0;
    m_firstDelta=true;
    MediaJoystick::Sgl();

    if ((PlatformInputUtils::CurrentKeyModifiers() & PlatformInputUtils::kKeyModShift) != 0)
    {
        m_shiftAtStartupPressed = true;
    }
}

void
SDLAppHandler::Idle(void)
{
    MushcoreAppHandler::Idle();
}

void
SDLAppHandler::Display(void)
{
}

void
SDLAppHandler::KeyboardSignal(const GLKeyboardSignal& inSignal)
{
    U32 keyValue = inSignal.KeyValue();
    if (keyValue >= m_keyState.size())
    {
        m_keyState.resize(keyValue+1, false);
    }

    AddToControlBuffer(keyValue, inSignal.keyDown);

    m_keyState[keyValue]=inSignal.keyDown;
    if (inSignal.keyDown)
    {
        m_latchedKeyState[keyValue]=true;
    }
}

bool
SDLAppHandler::KeyStateGet(const Mushware::U32 inKey) const
{
    if (inKey >= m_keyState.size())
    {
        ostringstream message;
        message << "Key number " << inKey << " too large";
        throw MushcoreDataFail(message.str());
    }
    return m_keyState[inKey];
}

bool
SDLAppHandler::LatchedKeyStateTake(const Mushware::U32 inKey)
{
    if (inKey >= m_keyState.size())
    {
        ostringstream message;
        message << "Key number " << inKey << " too large";
        throw MushcoreDataFail(message.str());
    }
    bool state=m_latchedKeyState[inKey];
    if (state)
    {
        m_latchedKeyState[inKey]=false;
    }
    return state;
}

void
SDLAppHandler::MousePositionGet(tVal& outX, tVal& outY) const
{
    // Return with origin in the centre and bottom left at -x,-y.  The screen should
    // measure 1 in its largest dimension
    outX=(m_mouseX - m_width/2) / m_greatestDimension;
    outY=(m_height/2 - m_mouseY) / m_greatestDimension;
}

void
SDLAppHandler::UnboundedMousePositionGet(S32& outX, S32& outY) const
{
    outX=m_unboundedMouseX;
    outY=-m_unboundedMouseY;
}

tVal
SDLAppHandler::ScaledUnboundedMouseX(void) const
{
    return m_mouseSensitivity * m_unboundedMouseX / 640.0;
}

tVal
SDLAppHandler::ScaledUnboundedMouseY(void) const
{
    return -m_mouseSensitivity * m_unboundedMouseY / 640.0;
}

void
SDLAppHandler::EnterScreen(const GLModeDef& inDef)
{
    GLUtils::Decache();

    m_width=inDef.Width();
    m_height=inDef.Height();
    m_bpp=0;
    m_showCursor=!inDef.FullScreen();
    U32 sdlFlags=0;
    if (inDef.FullScreen())
    {
        sdlFlags=SDL_OPENGL|SDL_FULLSCREEN;
    }
    else
    {
        sdlFlags=SDL_OPENGL;
    }

    bool carryOn = true;
    
    SDL_Surface *pSurface = NULL;
    
    
    
    for (U32 i=0; i<16 && carryOn; ++i)
    {
        PlatformVideoUtils::Sgl().ModeChangePrologue(); // Can quit SDL video
    
        MediaSDL::Sgl().InitVideoIfRequired();
    
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
    
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

#if (SDL_MAJOR_VERSION * 10000 + SDL_MINOR_VERSION * 100 + SDL_PATCHLEVEL) >= 10210
        // Attributes supported from SDL version 1.2.10 onwards
        SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
#endif


#if (SDL_MAJOR_VERSION * 10000 + SDL_MINOR_VERSION * 100 + SDL_PATCHLEVEL) >= 10206
        // Attributes supported from SDL version 1.2.6 onwards
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
#endif
        
        pSurface=SDL_SetVideoMode(m_width, m_height, m_bpp, sdlFlags);

#if (SDL_MAJOR_VERSION * 10000 + SDL_MINOR_VERSION * 100 + SDL_PATCHLEVEL) >= 10206
        if (pSurface == NULL)
        {
            MushcoreLog::Sgl().WarningLog() << "SDL video mode failed - trying without FSAA" << endl;
            SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
            SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
            pSurface=SDL_SetVideoMode(m_width, m_height, m_bpp, sdlFlags);
        }
#endif

        if (pSurface == NULL)
        {
            MushcoreLog::Sgl().WarningLog() << "SDL video mode failed again - trying for ANYFORMAT mode" << endl;
            pSurface=SDL_SetVideoMode(m_width, m_height, m_bpp, sdlFlags|SDL_ANYFORMAT);
        }
        
        if (pSurface != NULL)
        {
            // Mode successful
            carryOn = false;
        }
        else
        {
            carryOn = PlatformVideoUtils::Sgl().ModeSelectFixAttempt(i);
        }
    }
    
    PlatformVideoUtils::Sgl().ModeChangeEpilogue();
    if (pSurface == NULL) throw(MushcoreDeviceFail("Could not select a compatible display mode"));

    // Got video mode
    if (m_width > m_height)
    {
        m_greatestDimension=m_width;
    }
    else
    {
        m_greatestDimension=m_height;
    }
    SetCursorState(m_showCursor);
    SDL_WM_SetCaption(MushcoreInfo::Sgl().ApplicationNameGet().c_str(), (MushcoreInfo::Sgl().ApplicationNameGet()+".bmp").c_str());

    GLState::Reset();
    for (U32 i=0; i<m_keyState.size(); ++i)
    {
        m_keyState[i] = false;
    }

    if (inDef.FullScreen())
    {
        SDL_WM_GrabInput(SDL_GRAB_ON);
    }
    else
    {
        // Don't grab input so that user can still work
        SDL_WM_GrabInput(SDL_GRAB_OFF);
    }

    m_modeDef=inDef;
    PlatformVideoUtils::Sgl().Acquaint();
    PlatformVideoUtils::AppActivate();
    m_screenEntered = true;
}

const GLModeDef&
SDLAppHandler::CurrentModeDefGet(void)
{
    return m_modeDef;
}

void
SDLAppHandler::PostRedisplay(void)
{
    m_redisplay=true;
}

void
SDLAppHandler::SwapBuffers(void)
{
    SDL_GL_SwapBuffers();
}

U32
SDLAppHandler::MillisecondsGet(void) const
{
    return SDL_GetTicks();
}

bool
SDLAppHandler::HasFocus(void) const
{
    return ((SDL_GetAppState() & SDL_APPINPUTFOCUS) != 0);
}

void
SDLAppHandler::SetCursorState(bool inValue)
{
    if (!inValue && !m_showCursor)
    {
        SDL_ShowCursor(SDL_DISABLE);
    }
    else
    {
        SDL_ShowCursor(SDL_ENABLE);
        PlatformVideoUtils::ForceShowCursor();
    }
}

void
SDLAppHandler::Signal(const MushcoreAppSignal& inSignal)
{
    switch (inSignal.SigNumberGet())
    {
        case GLAppSignal::kDisplay:
            Display();
            break;

        case GLAppSignal::kVisible:
            m_visible=true;
            break;

        case GLAppSignal::kNotVisible:
            m_visible=false;
            break;

        case GLAppSignal::kKeyboard:
        {
            KeyboardSignal(dynamic_cast<const GLKeyboardSignal&>(inSignal));
        }
        break;

        default:
        {
            if (inSignal.IsMushcoreAppSignal())
            {
                MushcoreAppHandler::Signal(inSignal);
            }
            else
            {
                cerr << "Unhandled SDLAppHandler signal 0x" << hex << inSignal.SigNumberGet() << dec << endl;
            }
        }
        break;
    }
}

void
SDLAppHandler::MainLoop(void)
{
    m_doQuit=false;
    while (!m_doQuit)
    {
        PollForControlEvents();
        if (m_redisplay)
        {
            m_redisplay=false;
            Display();
        }
        Idle();
    }

    MediaSDL::Sgl().QuitVideoIfRequired();
}

void
SDLAppHandler::AddToControlBuffer(U32 inKeyValue, bool inKeyDirection)
{
    MUSHCOREASSERT(m_controlBufferIndex < m_controlBuffer.size());

    SDLControlEntry& controlEntry = m_controlBuffer[m_controlBufferIndex];
    controlEntry.timestamp = MillisecondsGet();
    controlEntry.keyValue = inKeyValue;
    controlEntry.keyDirection = inKeyDirection;
    controlEntry.unboundedMouseX = m_unboundedMouseX;
    controlEntry.unboundedMouseY = m_unboundedMouseY;

    ++m_controlBufferIndex;
    if (m_controlBufferIndex >= m_controlBuffer.size())
    {
        m_controlBufferIndex=0;
    }
}

void
SDLAppHandler::PollForControlEvents(void)
{
    U32 loopCtr=0;
    SDL_Event event;
    while (SDL_PollEvent(&event))
    {
        switch(event.type)
        {
            case SDL_KEYDOWN:
            case SDL_KEYUP:
                Signal(GLKeyboardSignal((event.key.type==SDL_KEYDOWN),
                                        static_cast<U32>(event.key.keysym.sym),
                                        static_cast<U32>(event.key.keysym.mod),
                                        m_mouseX, m_mouseY));
                break;

            case SDL_MOUSEMOTION:
            {
                // We log what the mouse position _was_ in the history, so as we track backwards
                // regenerating the control state we'll see the change at the right time
                AddToControlBuffer(0, 0);
                m_mouseX=event.motion.x;
                m_mouseY=event.motion.y;
                S32 mouseDeltaX = event.motion.xrel;
                S32 mouseDeltaY = event.motion.yrel;

                if (!CurrentModeDefGet().FullScreen())
                {
                    PlatformInputUtils::MouseDeltaOverrideGet(mouseDeltaX, mouseDeltaY);
                }
                m_unboundedMouseX += mouseDeltaX;
                m_unboundedMouseY += mouseDeltaY;
            }
            break;

            case SDL_MOUSEBUTTONDOWN:
            case SDL_MOUSEBUTTONUP:
            {
                U32 button=event.button.button-1;
                if (event.button.button < 1 ||
                    button > (MediaKeyboard::kKeyMouse6 - MediaKeyboard::kKeyMouse0))
                {
                    cerr << "Button index " << button << " ignored" << endl;
                    break;
                }
                m_mouseX=event.button.x;
                m_mouseY=event.button.y;
                Signal(GLKeyboardSignal((event.button.state == SDL_PRESSED),
                                        MediaKeyboard::kKeyMouse0+button,
                                        0,
                                        event.button.x, event.button.y));
            }
            break;

            case SDL_QUIT:
                m_doQuit=true;
                break;

            case SDL_JOYAXISMOTION:
            {
                if (event.jaxis.which < kNumDevices && event.jaxis.axis < kAxesPerDevice)
                {
                    tVal axisValue = - event.jaxis.value / 32768.0;
                    MushcoreUtil::Constrain<tVal>(axisValue, -1, 1);
                    m_deviceList[event.jaxis.which][event.jaxis.axis] = axisValue;
                }
            }
            break;

            case SDL_JOYBUTTONDOWN:
            case SDL_JOYBUTTONUP:
            {
                if (event.jbutton.which < kNumDevices)
                {
                    U32 keyValue = MediaKeyboard::kKeyStick0 +
                        MediaKeyboard::kKeyStickSpacing * event.jbutton.which +
                        event.jbutton.button;
                    Signal(GLKeyboardSignal((event.jbutton.state == SDL_PRESSED),
                                            keyValue,
                                            0,
                                            m_mouseX, m_mouseY));
                }
            }
            break;

            case SDL_JOYHATMOTION:
            {
                if (event.jhat.which < kNumDevices)
                {
                    bool leftState = (event.jhat.value & SDL_HAT_LEFT) != 0;
                    bool rightState = (event.jhat.value & SDL_HAT_RIGHT) != 0;
                    bool upState = (event.jhat.value & SDL_HAT_UP) != 0;
                    bool downState = (event.jhat.value & SDL_HAT_DOWN) != 0;

                    U32 keyBase = MediaKeyboard::kKeyStick0 +
                        MediaKeyboard::kKeyStickSpacing * event.jhat.which +
                        MediaKeyboard::kKeyStickHat +
                        4 * event.jhat.hat;
                    Signal(GLKeyboardSignal(leftState,
                                            keyBase + MediaKeyboard::kKeyStickHatLeft,
                                            0,
                                            m_mouseX, m_mouseY));
                    Signal(GLKeyboardSignal(rightState,
                                            keyBase + MediaKeyboard::kKeyStickHatRight,
                                            0,
                                            m_mouseX, m_mouseY));
                    Signal(GLKeyboardSignal(upState,
                                            keyBase + MediaKeyboard::kKeyStickHatUp,
                                            0,
                                            m_mouseX, m_mouseY));
                    Signal(GLKeyboardSignal(downState,
                                            keyBase + MediaKeyboard::kKeyStickHatDown,
                                            0,
                                            m_mouseX, m_mouseY));
                }
            }
            break;

            default:
                // Ignore
                break;
        }

        if (++loopCtr > 100) break;
    }

    if (loopCtr > 100)
    {
        MushcoreLog::Sgl().WarningLog() << "event loopCtr overrun";
    }
}

Mushware::tVal
SDLAppHandler::DeviceAxis(Mushware::U32 inDevice, Mushware::U32 inAxis)
{
    if (inDevice >= kNumDevices || inAxis >= kAxesPerDevice)
    {
        ostringstream message;
        message << "Bad device (" << inDevice << ") or axis (" << inAxis << ") number";
        throw MushcoreDataFail(message.str());
    }
    return m_deviceList[inDevice][inAxis];
}

void
SDLAppHandler::KeysOfInterestSet(const vector<Mushware::U32>& inKeyValues)
{
    m_keysOfInterest = inKeyValues;
}

void
SDLAppHandler::ReadHistoricControlState(S32& outUnboundedMouseX, S32& outUnboundedMouseY, vector<bool>& outKeys, tVal inAtMsec)
{
    if (outKeys.size() < m_keysOfInterest.size())
    {
        outKeys.resize(m_keysOfInterest.size());
    }
    U32 keysOfInterestSize = m_keysOfInterest.size();
    for (U32 i=0; i<keysOfInterestSize; ++i)
    {
        U32 keyValue = m_keysOfInterest[i];
        MUSHCOREASSERT(keyValue < m_keyState.size());
        outKeys[i] = m_keyState[keyValue];
    }

    outUnboundedMouseX = m_unboundedMouseX;
    outUnboundedMouseY = m_unboundedMouseY;

    // Wind the control state back in time

    U32 bufferIndex = m_controlBufferIndex;
    U32 bufferSize = m_controlBuffer.size();
    do
    {
        if (bufferIndex == 0)
        {
            bufferIndex = bufferSize;
        }
        --bufferIndex;
        SDLControlEntry& bufferEntry = m_controlBuffer[bufferIndex];
        if (bufferEntry.timestamp > inAtMsec)
        {
            // Unwind this entry from the returned state
            outUnboundedMouseX = bufferEntry.unboundedMouseX;
            outUnboundedMouseY = bufferEntry.unboundedMouseY;
            if (bufferEntry.keyValue != 0)
            {
                for (U32 i=0; i<keysOfInterestSize; ++i)
                {
                    U32 keyValue = m_keysOfInterest[i];
                    if (bufferEntry.keyValue == keyValue)
                    {
                        // Negated because we are _removing_ this entry from the current state
                        outKeys[i] = !bufferEntry.keyDirection;
                        break;
                    }
                }
            }
        }
        else
        {
            // Timestamp are now older than we need to go
            break;
        }
    } while (bufferIndex != m_controlBufferIndex);
}

void
SDLAppHandler::AppQuit(void)
{
    m_doQuit=true;
}

void
SDLAppHandler::KeyRepeatSet(bool inValue)
{
    if (inValue)
    {
        SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
    }
    else
    {
        //  Disable
        SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
    }
}