X-Plane Remote Access Plugin and Client Library
HotkeyHandler.cc
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 
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 
67 {
68  delete [] pressed;
69  delete [] hotkeys;
70 }
71 
72 //------------------------------------------------------------------------------
73 
75 {
76  for(size_t i = 0; i<numHotkeys; ++i) {
77  if (hotkeys[i]==code) {
78  pressed[i] = 1;
79  return true;
80  }
81  }
82  return false;
83 }
84 
85 //------------------------------------------------------------------------------
86 
87 void HotkeyHandler::Hotkeys::writePressed(DataStream& stream)
88 {
89  uint8_t pressed1[numHotkeys];
90 
91  for(size_t i = 0; i<numHotkeys; ++i) {
92  pressed1[i] = __sync_val_compare_and_swap(pressed + i, 1, 0);
93  }
94 
95  stream.writeU32(numHotkeys);
96  stream.write(pressed1, sizeof(pressed1));
97 }
98 
99 //------------------------------------------------------------------------------
100 //------------------------------------------------------------------------------
101 
102 int HotkeyHandler::keyCallback(char c, XPLMKeyFlags flags, char virtualKey,
103  void* refCon)
104 {
105  HotkeyHandler* hotkeyHandler = reinterpret_cast<HotkeyHandler*>(refCon);
106  return hotkeyHandler->handleKey(c, flags, virtualKey) ? 0 : 1;
107 }
108 
109 //------------------------------------------------------------------------------
110 
112 {
113  XPLMRegisterKeySniffer(&keyCallback, 0, this);
114 }
115 
116 //------------------------------------------------------------------------------
117 
119 {
120  mutex.lock();
121  hotkeySets.push_back(hotkeys);
122  mutex.unlock();
123 }
124 
125 //------------------------------------------------------------------------------
126 
128 {
129  mutex.lock();
130  hotkeySets.remove(hotkeys);
131  mutex.unlock();
132 }
133 
134 //------------------------------------------------------------------------------
135 
136 bool HotkeyHandler::handleKey(char /*c*/, XPLMKeyFlags flags, char virtualKey)
137 {
138  if ( (flags&xplm_DownFlag)==0 ) return false;
139 
140  uint16_t code = static_cast<uint16_t>(virtualKey);
141  if ( (flags&xplm_ShiftFlag)!=0 ) code |= Protocol::HOTKEY_MODIFIER_SHIFT;
142  if ( (flags&xplm_ControlFlag)!=0 ) code |= Protocol::HOTKEY_MODIFIER_CONTROL;
143 
144  bool handled = false;
145 
146  mutex.lock();
147  for(hotkeySets_t::iterator i = hotkeySets.begin(); i!=hotkeySets.end();
148  ++i)
149  {
150  Hotkeys* hotkeys = *i;
151  if (hotkeys->handleKey(code)) {
152  handled = true;
153  }
154  }
155  mutex.unlock();
156 
157  return handled;
158 }
159 
160 //------------------------------------------------------------------------------
161 
162 // Local Variables:
163 // mode: C++
164 // c-basic-offset: 4
165 // indent-tabs-mode: nil
166 // End:
bool handleKey(uint16_t code)
void writePressed(hu::varadiistvan::scpl::io::DataStream &stream)
bool handleKey(char c, XPLMKeyFlags flags, char virtualKey)
hotkeySets_t hotkeySets
static int keyCallback(char c, XPLMKeyFlags flags, char virtualKey, void *refCon)
void unregisterHotkeys(Hotkeys *hotkeys)
hu::varadiistvan::scpl::Mutex mutex
void registerHotkeys(Hotkeys *hotkeys)
static const uint16_t HOTKEY_MODIFIER_CONTROL
Definition: Protocol.h:250
static const uint16_t HOTKEY_MODIFIER_SHIFT
Definition: Protocol.h:245