]> git.mxchange.org Git - flightgear.git/blob - src/Main/FGEventHandler.cxx
#545: Fix ATC chatter sound settings being ignored
[flightgear.git] / src / Main / FGEventHandler.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4 #include <osg/Camera>
5 #include <osg/GraphicsContext>
6 #include <osg/Math>
7 #include <osg/Viewport>
8 #include <osgViewer/Viewer>
9
10 #include <plib/pu.h>
11 #include <Main/fg_props.hxx>
12 #include "CameraGroup.hxx"
13 #include "FGEventHandler.hxx"
14 #include "WindowSystemAdapter.hxx"
15 #include "renderer.hxx"
16
17 #if !defined(X_DISPLAY_MISSING)
18 #define X_DOUBLE_SCROLL_BUG 1
19 #endif
20
21 #ifdef SG_MAC
22 // hack - during interactive resize on Mac, OSG queues and then flushes
23 // a large number of resize events, without doing any drawing.
24 extern void puCleanUpJunk ( void ) ;
25 #endif
26
27 namespace flightgear
28 {
29 const int displayStatsKey = 1;
30 const int printStatsKey = 2;
31
32
33 // The manipulator is responsible for updating a Viewer's camera. Its
34 // event handling method is also a convenient place to run the FG idle
35 // and draw handlers.
36
37 FGEventHandler::FGEventHandler() :
38     idleHandler(0),
39     keyHandler(0),
40     mouseClickHandler(0),
41     mouseMotionHandler(0),
42     statsHandler(new FGStatsHandler),
43     statsEvent(new osgGA::GUIEventAdapter),
44     statsType(osgViewer::StatsHandler::NO_STATS),
45     currentModifiers(0),
46     resizable(true),
47     mouseWarped(false),
48     scrollButtonPressed(false)
49 {
50     using namespace osgGA;
51     statsHandler->setKeyEventTogglesOnScreenStats(displayStatsKey);
52     statsHandler->setKeyEventPrintsOutStats(printStatsKey);
53     statsEvent->setEventType(GUIEventAdapter::KEYDOWN);
54
55     // OSG reports NumPad keycodes independent of the NumLock modifier.
56     // Both KP-4 and KP-Left are reported as KEY_KP_Left (0xff96), so we
57     // have to generate the locked keys ourselves.
58     numlockKeyMap[GUIEventAdapter::KEY_KP_Insert]  = '0';
59     numlockKeyMap[GUIEventAdapter::KEY_KP_End] = '1';
60     numlockKeyMap[GUIEventAdapter::KEY_KP_Down] = '2';
61     numlockKeyMap[GUIEventAdapter::KEY_KP_Page_Down] = '3';
62     numlockKeyMap[GUIEventAdapter::KEY_KP_Left] = '4';
63     numlockKeyMap[GUIEventAdapter::KEY_KP_Begin] = '5';
64     numlockKeyMap[GUIEventAdapter::KEY_KP_Right] = '6';
65     numlockKeyMap[GUIEventAdapter::KEY_KP_Home] = '7';
66     numlockKeyMap[GUIEventAdapter::KEY_KP_Up] = '8';
67     numlockKeyMap[GUIEventAdapter::KEY_KP_Page_Up] = '9';
68     numlockKeyMap[GUIEventAdapter::KEY_KP_Delete] = '.';
69
70     // mapping when NumLock is off
71     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Insert]     = PU_KEY_INSERT;
72     noNumlockKeyMap[GUIEventAdapter::KEY_KP_End]        = PU_KEY_END;
73     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Down]       = PU_KEY_DOWN;
74     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Page_Down]  = PU_KEY_PAGE_DOWN;
75     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Left]       = PU_KEY_LEFT;
76     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Begin]      = '5';
77     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Right]      = PU_KEY_RIGHT;
78     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Home]       = PU_KEY_HOME;
79     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Up]         = PU_KEY_UP;
80     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Page_Up]    = PU_KEY_PAGE_UP;
81     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Delete]     = 127;
82
83     for (int i = 0; i < 128; i++)
84         release_keys[i] = i;
85 }
86
87 namespace
88 {
89 // Translate OSG modifier mask to FG modifier mask.
90 int osgToFGModifiers(int modifiers)
91 {
92     int result = 0;
93     if (modifiers & osgGA::GUIEventAdapter::MODKEY_SHIFT)
94         result |= KEYMOD_SHIFT;
95
96     if (modifiers & osgGA::GUIEventAdapter::MODKEY_CTRL)
97         result |= KEYMOD_CTRL;
98
99     if (modifiers & osgGA::GUIEventAdapter::MODKEY_ALT)
100         result |= KEYMOD_ALT;
101
102     if (modifiers & osgGA::GUIEventAdapter::MODKEY_META)
103         result |= KEYMOD_META;
104
105     if (modifiers & osgGA::GUIEventAdapter::MODKEY_SUPER)
106         result |= KEYMOD_SUPER;
107
108     if (modifiers & osgGA::GUIEventAdapter::MODKEY_HYPER)
109         result |= KEYMOD_HYPER;
110     return result;
111 }
112 }
113
114 #if 0
115 void FGEventHandler::init(const osgGA::GUIEventAdapter& ea,
116                           osgGA::GUIActionAdapter& us)
117 {
118     currentModifiers = osgToFGModifiers(ea.getModKeyMask());
119     (void)handle(ea, us);
120 }
121 #endif
122
123 // Calculate event coordinates in the viewport of the GUI camera, if
124 // possible. Otherwise return false and (-1, -1).
125 namespace
126 {
127 bool
128 eventToViewport(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us,
129                 int& x, int& y)
130 {
131     x = -1;
132     y = -1;
133
134     const osg::GraphicsContext* eventGC = ea.getGraphicsContext();
135     const osg::GraphicsContext::Traits* traits = eventGC->getTraits();
136     osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
137     if (!guiCamera)
138         return false;
139     osg::Viewport* vport = guiCamera->getViewport();
140     if (!vport)
141         return false;
142     
143     // Scale x, y to the dimensions of the window
144     double wx = (((ea.getX() - ea.getXmin()) / (ea.getXmax() - ea.getXmin()))
145                  * (float)traits->width);
146     double wy = (((ea.getY() - ea.getYmin()) / (ea.getYmax() - ea.getYmin()))
147                  * (float)traits->height);
148     if (vport->x() <= wx && wx <= vport->x() + vport->width()
149         && vport->y() <= wy && wy <= vport->y() + vport->height()) {
150         // Finally, into viewport coordinates. Change y to "increasing
151         // downwards".
152         x = wx - vport->x();
153         y = vport->height() - (wy - vport->y());
154         return true;
155     } else {
156         return false;
157     }
158 }
159 }
160
161 bool FGEventHandler::handle(const osgGA::GUIEventAdapter& ea,
162                             osgGA::GUIActionAdapter& us)
163 {
164     int x = 0;
165     int y = 0;
166
167     switch (ea.getEventType()) {
168     case osgGA::GUIEventAdapter::FRAME:
169         mouseWarped = false;
170         handleStats(us);
171         return true;
172     case osgGA::GUIEventAdapter::KEYDOWN:
173     case osgGA::GUIEventAdapter::KEYUP:
174     {
175         int key, modmask;
176         handleKey(ea, key, modmask);
177         eventToViewport(ea, us, x, y);
178         if (keyHandler)
179             (*keyHandler)(key, modmask, x, y);
180         return true;
181     }
182     case osgGA::GUIEventAdapter::PUSH:
183     case osgGA::GUIEventAdapter::RELEASE:
184     {
185         bool mainWindow = eventToViewport(ea, us, x, y);
186         int button = 0;
187         switch (ea.getButton()) {
188         case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
189             button = 0;
190             break;
191         case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
192             button = 1;
193             break;
194         case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
195             button = 2;
196             break;
197         }
198         if (mouseClickHandler)
199             (*mouseClickHandler)(button,
200                                  (ea.getEventType()
201                                   == osgGA::GUIEventAdapter::RELEASE), x, y, mainWindow, &ea);
202         return true;
203     }
204     case osgGA::GUIEventAdapter::SCROLL:
205     {
206         bool mainWindow = eventToViewport(ea, us, x, y);
207 #ifdef X_DOUBLE_SCROLL_BUG
208         scrollButtonPressed = !scrollButtonPressed;
209         if (!scrollButtonPressed) // Drop the button release event
210             return true;
211 #endif
212         int button;
213         if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_2D) {
214             if (ea.getScrollingDeltaY() > 0)
215                 button = 3;
216             else if (ea.getScrollingDeltaY() < 0)
217                 button = 4;
218             else
219                 button = -1;
220         } else if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_UP)
221             button = 3;
222         else
223             button = 4;
224         if (mouseClickHandler && button != -1) {
225             (*mouseClickHandler)(button, 0, x, y, mainWindow, &ea);
226             (*mouseClickHandler)(button, 1, x, y, mainWindow, &ea);
227         }
228         return true;
229     }
230     case osgGA::GUIEventAdapter::MOVE:
231     case osgGA::GUIEventAdapter::DRAG:
232         // If we warped the mouse, then disregard all pointer motion
233         // events for this frame. We really want to flush the event
234         // queue of mouse events, but don't have the ability to do
235         // that with osgViewer.
236         if (mouseWarped)
237             return true;
238         if (eventToViewport(ea, us, x, y) && mouseMotionHandler)
239             (*mouseMotionHandler)(x, y);
240         return true;
241     case osgGA::GUIEventAdapter::RESIZE:
242         SG_LOG(SG_VIEW, SG_DEBUG, "FGEventHandler::handle: RESIZE event " << ea.getWindowHeight() << " x " << ea.getWindowWidth() << ", resizable: " << resizable);
243         CameraGroup::getDefault()->resized();
244         if (resizable)
245           globals->get_renderer()->resize(ea.getWindowWidth(), ea.getWindowHeight());
246         statsHandler->handle(ea, us);
247       #ifdef SG_MAC
248         // work around OSG Cocoa-Viewer issue with resize event handling,
249         // where resize events are queued up, then dispatched in a batch, with
250         // no interveningd drawing calls.
251         puCleanUpJunk();
252       #endif
253         return true;
254      case osgGA::GUIEventAdapter::CLOSE_WINDOW:
255     case osgGA::GUIEventAdapter::QUIT_APPLICATION:
256         fgOSExit(0);
257         return true;
258     default:
259         return false;
260     }
261 }
262
263 void FGEventHandler::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
264                                int& modifiers)
265 {
266     using namespace osgGA;
267     key = ea.getKey();
268     // XXX Probably other translations are needed too.
269     switch (key) {
270     case GUIEventAdapter::KEY_Escape:      key = 0x1b; break;
271     case GUIEventAdapter::KEY_Return:      key = '\n'; break;
272     case GUIEventAdapter::KEY_BackSpace:   key = '\b'; break;
273     case GUIEventAdapter::KEY_Delete:      key = 0x7f; break;
274     case GUIEventAdapter::KEY_Tab:         key = '\t'; break;
275     case GUIEventAdapter::KEY_Left:        key = PU_KEY_LEFT;      break;
276     case GUIEventAdapter::KEY_Up:          key = PU_KEY_UP;        break;
277     case GUIEventAdapter::KEY_Right:       key = PU_KEY_RIGHT;     break;
278     case GUIEventAdapter::KEY_Down:        key = PU_KEY_DOWN;      break;
279     case GUIEventAdapter::KEY_Page_Up:     key = PU_KEY_PAGE_UP;   break;
280     case GUIEventAdapter::KEY_Page_Down:   key = PU_KEY_PAGE_DOWN; break;
281     case GUIEventAdapter::KEY_Home:        key = PU_KEY_HOME;      break;
282     case GUIEventAdapter::KEY_End:         key = PU_KEY_END;       break;
283     case GUIEventAdapter::KEY_Insert:      key = PU_KEY_INSERT;    break;
284     case GUIEventAdapter::KEY_F1:          key = PU_KEY_F1;        break;
285     case GUIEventAdapter::KEY_F2:          key = PU_KEY_F2;        break;
286     case GUIEventAdapter::KEY_F3:          key = PU_KEY_F3;        break;
287     case GUIEventAdapter::KEY_F4:          key = PU_KEY_F4;        break;
288     case GUIEventAdapter::KEY_F5:          key = PU_KEY_F5;        break;
289     case GUIEventAdapter::KEY_F6:          key = PU_KEY_F6;        break;
290     case GUIEventAdapter::KEY_F7:          key = PU_KEY_F7;        break;
291     case GUIEventAdapter::KEY_F8:          key = PU_KEY_F8;        break;
292     case GUIEventAdapter::KEY_F9:          key = PU_KEY_F9;        break;
293     case GUIEventAdapter::KEY_F10:         key = PU_KEY_F10;       break;
294     case GUIEventAdapter::KEY_F11:         key = PU_KEY_F11;       break;
295     case GUIEventAdapter::KEY_F12:         key = PU_KEY_F12;       break;
296     case GUIEventAdapter::KEY_KP_Enter:    key = '\r'; break;
297     case GUIEventAdapter::KEY_KP_Add:      key = '+';  break;
298     case GUIEventAdapter::KEY_KP_Divide:   key = '/';  break;
299     case GUIEventAdapter::KEY_KP_Multiply: key = '*';  break;
300     case GUIEventAdapter::KEY_KP_Subtract: key = '-';  break;
301     }
302     osgGA::GUIEventAdapter::EventType eventType = ea.getEventType();
303
304 #ifdef __APPLE__
305     // Num Lock is always true on Mac
306     std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
307     if (numPadIter != numlockKeyMap.end()) {
308         key = numPadIter->second;
309     }
310 #else
311     if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_NUM_LOCK)
312     {
313         // NumLock on: map to numeric keys
314         std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
315         if (numPadIter != numlockKeyMap.end()) {
316             key = numPadIter->second;
317         }
318     }
319     else
320     {
321         // NumLock off: map to PU arrow keys
322         std::map<int, int>::iterator numPadIter = noNumlockKeyMap.find(key);
323         if (numPadIter != noNumlockKeyMap.end()) {
324             key = numPadIter->second;
325         }
326     }
327 #endif
328
329     modifiers = osgToFGModifiers(ea.getModKeyMask());
330     currentModifiers = modifiers;
331     if (eventType == osgGA::GUIEventAdapter::KEYUP)
332         modifiers |= KEYMOD_RELEASED;
333
334     // Release the letter key, for which the key press was reported. This
335     // is to deal with Ctrl-press -> a-press -> Ctrl-release -> a-release
336     // correctly.
337     if (key >= 0 && key < 128) {
338         if (modifiers & KEYMOD_RELEASED) {
339             key = release_keys[key];
340         } else {
341             release_keys[key] = key;
342             if (key >= 1 && key <= 26) {
343                 release_keys[key + '@'] = key;
344                 release_keys[key + '`'] = key;
345             } else if (key >= 'A' && key <= 'Z') {
346                 release_keys[key - '@'] = key;
347                 release_keys[tolower(key)] = key;
348             } else if (key >= 'a' && key <= 'z') {
349                 release_keys[key - '`'] = key;
350                 release_keys[toupper(key)] = key;
351             }
352         }
353     }
354 }
355
356 void FGEventHandler::handleStats(osgGA::GUIActionAdapter& us)
357 {
358     static SGPropertyNode_ptr display = fgGetNode("/sim/rendering/on-screen-statistics", true);
359     static SGPropertyNode_ptr print = fgGetNode("/sim/rendering/print-statistics", true);
360
361     int type = display->getIntValue() % osgViewer::StatsHandler::LAST;
362     if (type != statsType) {
363         statsEvent->setKey(displayStatsKey);
364         do {
365             statsType = (statsType + 1) % osgViewer::StatsHandler::LAST;
366             statsHandler->handle(*statsEvent, us);
367         } while (statsType != type);
368
369         display->setIntValue(statsType);
370     }
371
372     if (print->getBoolValue()) {
373         statsEvent->setKey(printStatsKey);
374         statsHandler->handle(*statsEvent, us);
375         print->setBoolValue(false);
376     }
377 }
378
379 void eventToWindowCoords(const osgGA::GUIEventAdapter* ea,
380                          double& x, double& y)
381 {
382     using namespace osg;
383     const GraphicsContext* gc = ea->getGraphicsContext();
384     const GraphicsContext::Traits* traits = gc->getTraits() ;
385     // Scale x, y to the dimensions of the window
386     x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
387          * (double)traits->width);
388     y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
389          * (double)traits->height);
390     if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS)
391         y = (double)traits->height - y;
392 }
393
394 void eventToWindowCoordsYDown(const osgGA::GUIEventAdapter* ea,
395                               double& x, double& y)
396 {
397     using namespace osg;
398     const GraphicsContext* gc = ea->getGraphicsContext();
399     const GraphicsContext::Traits* traits = gc->getTraits() ;
400     // Scale x, y to the dimensions of the window
401     x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
402          * (double)traits->width);
403     y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
404          * (double)traits->height);
405     if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS)
406         y = (double)traits->height - y;
407 }
408 }