1 | // Copyright (c) 2013 by István Váradi
|
---|
2 |
|
---|
3 | // This file is part of XPLRA, a remote-access plugin for X-Plane
|
---|
4 |
|
---|
5 | // Redistribution and use in source and binary forms, with or without
|
---|
6 | // modification, are permitted provided that the following conditions are met:
|
---|
7 |
|
---|
8 | // 1. Redistributions of source code must retain the above copyright notice, this
|
---|
9 | // list of conditions and the following disclaimer.
|
---|
10 | // 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
11 | // this list of conditions and the following disclaimer in the documentation
|
---|
12 | // and/or other materials provided with the distribution.
|
---|
13 |
|
---|
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
---|
15 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
16 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
17 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
---|
18 | // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
19 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
20 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
21 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
23 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 |
|
---|
25 | // The views and conclusions contained in the software and documentation are those
|
---|
26 | // of the authors and should not be interpreted as representing official policies,
|
---|
27 | // either expressed or implied, of the FreeBSD Project.
|
---|
28 |
|
---|
29 | //------------------------------------------------------------------------------
|
---|
30 |
|
---|
31 | #include "HotkeyHandler.h"
|
---|
32 |
|
---|
33 | #include "Protocol.h"
|
---|
34 |
|
---|
35 | #include <hu/varadiistvan/xplcommon/Util.h>
|
---|
36 |
|
---|
37 | #include <hu/varadiistvan/scpl/io/DataStream.h>
|
---|
38 |
|
---|
39 | #include <XPLMDisplay.h>
|
---|
40 |
|
---|
41 | //------------------------------------------------------------------------------
|
---|
42 |
|
---|
43 | using xplra::HotkeyHandler;
|
---|
44 |
|
---|
45 | using hu::varadiistvan::xplcommon::Util;
|
---|
46 |
|
---|
47 | using hu::varadiistvan::scpl::io::DataStream;
|
---|
48 |
|
---|
49 | //------------------------------------------------------------------------------
|
---|
50 |
|
---|
51 | HotkeyHandler::Hotkeys::Hotkeys(size_t numHotkeys, DataStream& stream) :
|
---|
52 | numHotkeys(numHotkeys),
|
---|
53 | hotkeys(new uint16_t[numHotkeys]),
|
---|
54 | pressed(0)
|
---|
55 | {
|
---|
56 | if (stream.read(hotkeys, sizeof(uint16_t)*numHotkeys)) {
|
---|
57 | pressed = new uint8_t[numHotkeys];
|
---|
58 | memset(pressed, 0, sizeof(uint8_t)*numHotkeys);
|
---|
59 | } else {
|
---|
60 | delete hotkeys; hotkeys = 0;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | //------------------------------------------------------------------------------
|
---|
65 |
|
---|
66 | HotkeyHandler::Hotkeys::~Hotkeys()
|
---|
67 | {
|
---|
68 | delete [] pressed;
|
---|
69 | delete [] hotkeys;
|
---|
70 | }
|
---|
71 |
|
---|
72 | //------------------------------------------------------------------------------
|
---|
73 |
|
---|
74 | bool HotkeyHandler::Hotkeys::handleKey(uint16_t code)
|
---|
75 | {
|
---|
76 | for(size_t i = 0; i<numHotkeys; ++i) {
|
---|
77 | Util::debug("handleKey: %04x\n", hotkeys[i]);
|
---|
78 |
|
---|
79 | if (hotkeys[i]==code) {
|
---|
80 | pressed[i] = 1;
|
---|
81 | return true;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | return false;
|
---|
85 | }
|
---|
86 |
|
---|
87 | //------------------------------------------------------------------------------
|
---|
88 |
|
---|
89 | void HotkeyHandler::Hotkeys::writePressed(DataStream& stream)
|
---|
90 | {
|
---|
91 | uint8_t pressed1[numHotkeys];
|
---|
92 |
|
---|
93 | for(size_t i = 0; i<numHotkeys; ++i) {
|
---|
94 | pressed1[i] = __sync_val_compare_and_swap(pressed + i, 1, 0);
|
---|
95 | }
|
---|
96 |
|
---|
97 | stream.writeU32(numHotkeys);
|
---|
98 | stream.write(pressed1, sizeof(pressed1));
|
---|
99 | }
|
---|
100 |
|
---|
101 | //------------------------------------------------------------------------------
|
---|
102 | //------------------------------------------------------------------------------
|
---|
103 |
|
---|
104 | int HotkeyHandler::keyCallback(char c, XPLMKeyFlags flags, char virtualKey,
|
---|
105 | void* refCon)
|
---|
106 | {
|
---|
107 | HotkeyHandler* hotkeyHandler = reinterpret_cast<HotkeyHandler*>(refCon);
|
---|
108 | return hotkeyHandler->handleKey(c, flags, virtualKey) ? 0 : 1;
|
---|
109 | }
|
---|
110 |
|
---|
111 | //------------------------------------------------------------------------------
|
---|
112 |
|
---|
113 | HotkeyHandler::HotkeyHandler()
|
---|
114 | {
|
---|
115 | XPLMRegisterKeySniffer(&keyCallback, 0, this);
|
---|
116 | }
|
---|
117 |
|
---|
118 | //------------------------------------------------------------------------------
|
---|
119 |
|
---|
120 | void HotkeyHandler::registerHotkeys(Hotkeys* hotkeys)
|
---|
121 | {
|
---|
122 | mutex.lock();
|
---|
123 | hotkeySets.push_back(hotkeys);
|
---|
124 | mutex.unlock();
|
---|
125 | }
|
---|
126 |
|
---|
127 | //------------------------------------------------------------------------------
|
---|
128 |
|
---|
129 | void HotkeyHandler::unregisterHotkeys(Hotkeys* hotkeys)
|
---|
130 | {
|
---|
131 | mutex.lock();
|
---|
132 | hotkeySets.remove(hotkeys);
|
---|
133 | mutex.unlock();
|
---|
134 | }
|
---|
135 |
|
---|
136 | //------------------------------------------------------------------------------
|
---|
137 |
|
---|
138 | bool HotkeyHandler::handleKey(char /*c*/, XPLMKeyFlags flags, char virtualKey)
|
---|
139 | {
|
---|
140 | if ( (flags&xplm_DownFlag)==0 ) return false;
|
---|
141 |
|
---|
142 | uint16_t code = static_cast<uint16_t>(virtualKey);
|
---|
143 | if ( (flags&xplm_ShiftFlag)!=0 ) code |= Protocol::HOTKEY_MODIFIER_SHIFT;
|
---|
144 | if ( (flags&xplm_ControlFlag)!=0 ) code |= Protocol::HOTKEY_MODIFIER_CONTROL;
|
---|
145 |
|
---|
146 | Util::debug("code=0x%04x\n", code);
|
---|
147 |
|
---|
148 | bool handled = false;
|
---|
149 |
|
---|
150 | mutex.lock();
|
---|
151 | for(hotkeySets_t::iterator i = hotkeySets.begin(); i!=hotkeySets.end();
|
---|
152 | ++i)
|
---|
153 | {
|
---|
154 | Hotkeys* hotkeys = *i;
|
---|
155 | if (hotkeys->handleKey(code)) {
|
---|
156 | handled = true;
|
---|
157 | }
|
---|
158 | }
|
---|
159 | mutex.unlock();
|
---|
160 |
|
---|
161 | return handled;
|
---|
162 | }
|
---|
163 |
|
---|
164 | //------------------------------------------------------------------------------
|
---|
165 |
|
---|
166 | // Local Variables:
|
---|
167 | // mode: C++
|
---|
168 | // c-basic-offset: 4
|
---|
169 | // indent-tabs-mode: nil
|
---|
170 | // End:
|
---|