]> git.mxchange.org Git - flightgear.git/blob - src/Input/FGMacOSXEventInput.cxx
Mac-specific fgjs / event-input fixes from Tatsuhiro.
[flightgear.git] / src / Input / FGMacOSXEventInput.cxx
1 // FGMacOSXEventInput.cxx -- handle event driven input devices for Mac OS X
2 //
3 // Written by Tatsuhiro Nishioka, started Aug. 2009.
4 //
5 // Copyright (C) 2009 Tasuhiro Nishioka, tat <dot> fgmacosx <at> gmail <dot> com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #include "FGMacOSXEventInput.hxx"
24
25 using std::stringstream;
26 using std::map;
27 using std::string;
28
29 #define GetHIDElementLongValue(element, key) ({ \
30       long value = 0; \
31       CFNumberRef refType = (CFNumberRef)CFDictionaryGetValue(element, CFSTR(key)); \
32       if (refType) { CFNumberGetValue(refType, kCFNumberLongType, &value); } \
33       value; })
34
35 #define GetHIDElementBooleanValue(element, key) ({ \
36       bool value = 0; \
37       CFBooleanRef refType = (CFBooleanRef)CFDictionaryGetValue(element, CFSTR(key)); \
38       if (refType) { value = CFBooleanGetValue(refType); } \
39       value; })
40
41 #define GetHIDElementStringValue(element, key) ({ \
42       const char *ref = ""; \
43       const char *buf = ref; \
44       CFStringRef refType = (CFStringRef)CFDictionaryGetValue(element, CFSTR(key)); \
45       if (refType) { \
46         buf = CFStringGetCStringPtr(refType, CFStringGetSystemEncoding());  \
47         if (!buf) buf = ref; \
48        } \
49       buf; })
50
51 // HID Element Types (log / debug use)
52 struct HIDTypes HID_TYPE_TABLE[] = {
53   {kIOHIDElementTypeInput_Misc, kHIDElementType, "Input Misc"},
54   {kIOHIDElementTypeInput_Button, kHIDElementType, "Input Button"},
55   {kIOHIDElementTypeInput_Axis, kHIDElementType, "Input Axis"},
56   {kIOHIDElementTypeInput_ScanCodes, kHIDElementType, "Input ScanCodes"},
57   {kIOHIDElementTypeOutput, kHIDElementType, "Output"},
58   {kIOHIDElementTypeFeature, kHIDElementType, "Feature"},
59   {kIOHIDElementTypeCollection, kHIDElementType, "Collection"},
60   {-1, kHIDElementType, ""}
61 };
62
63 // HID Element Pages (log / debug use)
64 struct HIDTypes HID_PAGE_TABLE[] = {
65   // Page
66   {kHIDPage_GenericDesktop, kHIDElementPage, "GenericDesktop"},
67   {kHIDPage_Simulation, kHIDElementPage, "Simulation Controls"},
68   {kHIDPage_VR, kHIDElementPage, "VR Controls"},
69   {kHIDPage_Sport, kHIDElementPage, "Sport Controls"},
70   {kHIDPage_Game, kHIDElementPage, "Game Controls"},
71   {0x06, kHIDElementPage, "Generic Device Controls"},
72   {kHIDPage_KeyboardOrKeypad, kHIDElementPage, "KeyboardOrKeypad"},
73   {kHIDPage_LEDs, kHIDElementPage, "LEDs"},
74   {kHIDPage_Button, kHIDElementPage, "Button"},
75   {kHIDPage_Ordinal, kHIDElementPage, "Ordinal"},
76   {kHIDPage_Telephony, kHIDElementPage, "Telephony"},
77   {kHIDPage_Consumer, kHIDElementPage, "Consumer"},
78   {kHIDPage_Digitizer, kHIDElementPage, "Digitizer"},
79   {kHIDPage_PID, kHIDElementPage, "PID"},
80   {kHIDPage_VendorDefinedStart, kHIDElementPage, "VendorDefinedStart"},
81   {-1, kHIDElementPage, ""}
82 };
83
84 #define USAGE_KEY(PAGE,USAGE) (PAGE << 16 | USAGE)
85 #define GET_PAGE(KEY)  (KEY >> 16)
86 #define GET_USAGE(KEY) (KEY & 0xFFFF)
87
88 #define GD_USAGE(USAGE) USAGE_KEY(kHIDPage_GenericDesktop, USAGE)
89 #define SIM_USAGE(USAGE) USAGE_KEY(kHIDPage_GenericDesktop, USAGE)
90 #define VR_USAGE(USAGE) USAGE_KEY(kHIDPage_VR, USAGE)
91 #define SP_USAGE(USAGE) USAGE_KEY(kHIDPage_Sport, USAGE)
92 #define GAME_USAGE(USAGE) USAGE_KEY(kHIDPage_Game, USAGE)
93 #define GDC_USAGE(USAGE) USAGE_KEY(0x06, USAGE) // Generic Device Control is "Reserved" in Apple's definition
94 #define KEY_USAGE(USAGE) USAGE_KEY(kHIDPage_KeyboardOrKeypad, USAGE)
95 #define LED_USAGE(USAGE) USAGE_KEY(kHIDPage_LEDs, USAGE)
96 #define BUTTON_USAGE(USAGE) USAGE_KEY(kHIDPage_Button, USAGE)
97 #define DIG_USAGE(USAGE) USAGE_KEY(kHIDPage_Digitizer, USAGE)
98 #define CON_USAGE(USAGE) USAGE_KEY(kHIDPage_Consumer, USAGE)
99
100 // HID Element Usage <-> FGEventData convertion data
101 // See http://www.usb.org/developers/devclass_docs/Hut1_12.pdf for detail on HID pages and usages
102 // Note 
103 // kHIDUsageAxis is FG specific type of DV since it is needed to be normalized into -1.0 to 1.0
104 // kHIDUsageHat also is FG specific type of DV. it's value is converted into two axis events
105 struct HIDTypes HID_USAGE_TABLE[] = {
106   // Generic Desktop Page
107   {GD_USAGE(kHIDUsage_GD_X), kHIDUsageAxis, "x-translate"},
108   {GD_USAGE(kHIDUsage_GD_Y), kHIDUsageAxis, "y-translate"},
109   {GD_USAGE(kHIDUsage_GD_Z), kHIDUsageAxis, "z-translate"},  
110   {GD_USAGE(kHIDUsage_GD_Rx), kHIDUsageAxis, "x-rotate"},  
111   {GD_USAGE(kHIDUsage_GD_Ry), kHIDUsageAxis, "y-rotate"},
112   {GD_USAGE(kHIDUsage_GD_Rz), kHIDUsageAxis, "z-rotate"},
113   {GD_USAGE(kHIDUsage_GD_Slider), kHIDUsageAxis, "slider"},
114   {GD_USAGE(kHIDUsage_GD_Dial), kHIDUsageAxis, "dial"},
115   {GD_USAGE(kHIDUsage_GD_Wheel), kHIDUsageAxis, "wheel"},
116   {GD_USAGE(kHIDUsage_GD_Hatswitch), kHIDUsageHat, "hat"},
117   
118   {GD_USAGE(kHIDUsage_GD_CountedBuffer), kHIDUsageNotSupported, "counted-buffer"},
119   {GD_USAGE(kHIDUsage_GD_ByteCount), kHIDUsageNotSupported, "byte-count"},
120   {GD_USAGE(kHIDUsage_GD_MotionWakeup), kHIDUsageDF, "motion-wakeup"},
121   {GD_USAGE(kHIDUsage_GD_Start), kHIDUsageOOC, "button-start"},
122   {GD_USAGE(kHIDUsage_GD_Select), kHIDUsageOOC, "button-select"},
123   {GD_USAGE(kHIDUsage_GD_Vx), kHIDUsageAxis, "x-vector"},
124   {GD_USAGE(kHIDUsage_GD_Vy), kHIDUsageAxis, "y-vector"},
125   {GD_USAGE(kHIDUsage_GD_Vz), kHIDUsageAxis, "z-vector"},
126   {GD_USAGE(kHIDUsage_GD_Vbrx), kHIDUsageAxis, "x-rel-vector"},
127   {GD_USAGE(kHIDUsage_GD_Vbry), kHIDUsageAxis, "y-rel-vector"},
128   {GD_USAGE(kHIDUsage_GD_Vbrz), kHIDUsageAxis, "z-rel-vector"},
129   {GD_USAGE(kHIDUsage_GD_Vno),  kHIDUsageAxis, "no-vector"},
130
131   {GD_USAGE(kHIDUsage_GD_SystemPowerDown), kHIDUsageOSC, "system-power-down"},
132   {GD_USAGE(kHIDUsage_GD_SystemSleep), kHIDUsageOSC, "system-sleep"},
133   {GD_USAGE(kHIDUsage_GD_SystemWakeUp), kHIDUsageOSC, "system-wake-up"},
134   {GD_USAGE(kHIDUsage_GD_SystemContextMenu), kHIDUsageOSC, "system-context-menu"},
135   {GD_USAGE(kHIDUsage_GD_SystemMainMenu), kHIDUsageOSC, "system-main-menu"},
136   {GD_USAGE(kHIDUsage_GD_SystemAppMenu), kHIDUsageOSC, "system-app-menu"},
137   {GD_USAGE(kHIDUsage_GD_SystemMenuHelp), kHIDUsageOSC, "system-menu-help"},
138   {GD_USAGE(kHIDUsage_GD_SystemMenuExit), kHIDUsageOSC, "system-menu-exit"},
139   {GD_USAGE(kHIDUsage_GD_SystemMenu), kHIDUsageOSC, "system-menu"},
140   {GD_USAGE(kHIDUsage_GD_SystemMenuRight), kHIDUsageRTC, "system-menu-right"},
141   {GD_USAGE(kHIDUsage_GD_SystemMenuLeft), kHIDUsageRTC, "system-menu-left"},
142   {GD_USAGE(kHIDUsage_GD_SystemMenuUp), kHIDUsageRTC, "system-menu-up"},
143   {GD_USAGE(kHIDUsage_GD_SystemMenuDown), kHIDUsageRTC, "system-menu-down"},
144   {GD_USAGE(kHIDUsage_GD_DPadUp), kHIDUsageOOC, "dpad-up"},
145   {GD_USAGE(kHIDUsage_GD_DPadDown), kHIDUsageOOC, "dpad-down"},
146   {GD_USAGE(kHIDUsage_GD_DPadRight), kHIDUsageOOC, "dpad-right"},
147   {GD_USAGE(kHIDUsage_GD_DPadLeft), kHIDUsageOOC, "dpad-left"},
148
149   // Game Controls Page
150   {GAME_USAGE(kHIDUsage_Game_TurnRightOrLeft), kHIDUsageAxis, "turn"},
151   {GAME_USAGE(kHIDUsage_Game_PitchUpOrDown), kHIDUsageAxis, "pitch"},
152   {GAME_USAGE(kHIDUsage_Game_MoveRightOrLeft), kHIDUsageAxis, "x-move"},
153   {GAME_USAGE(kHIDUsage_Game_MoveForwardOrBackward), kHIDUsageAxis, "y-move"},
154   {GAME_USAGE(kHIDUsage_Game_MoveUpOrDown), kHIDUsageAxis, "z-move"},
155   {GAME_USAGE(kHIDUsage_Game_LeanRightOrLeft), kHIDUsageAxis, "x-lean"},
156   {GAME_USAGE(kHIDUsage_Game_LeanForwardOrBackward), kHIDUsageAxis, "y-lean"},
157
158   // General Control Devices Page
159   {GDC_USAGE(0x20), kHIDUsageDV, "battery-strength"},
160   {GDC_USAGE(0x21), kHIDUsageDV, "wireless-channel"},
161   {GDC_USAGE(0x22), kHIDUsageDV, "wireless-id"},
162   {GDC_USAGE(0x23), kHIDUsageDV, "discover-wireless-control"},
163   {GDC_USAGE(0x24), kHIDUsageOSC, "security-code-character-entered"},
164   {GDC_USAGE(0x25), kHIDUsageOSC, "security-code-character-erased"},
165   {GDC_USAGE(0x26), kHIDUsageOSC, "security-code-cleared"},
166
167   // Simulation Controls Page
168   {SIM_USAGE(kHIDUsage_Sim_Aileron), kHIDUsageAxis, "aileron"},
169   {SIM_USAGE(kHIDUsage_Sim_AileronTrim), kHIDUsageAxis, "aileron-trim"},
170   {SIM_USAGE(kHIDUsage_Sim_AntiTorqueControl), kHIDUsageAxis, "anti-torque-control"},
171   {SIM_USAGE(kHIDUsage_Sim_AutopilotEnable), kHIDUsageOOC, "button-autopilot-enable"},
172   {SIM_USAGE(kHIDUsage_Sim_ChaffRelease), kHIDUsageOSC, "button-chaff-release"},
173   {SIM_USAGE(kHIDUsage_Sim_CollectiveControl), kHIDUsageAxis, "collective-control"},
174   {SIM_USAGE(kHIDUsage_Sim_DiveBrake), kHIDUsageAxis, "dive-brake"},
175   {SIM_USAGE(kHIDUsage_Sim_ElectronicCountermeasures), kHIDUsageOOC, "electronic-countermeasures"}, // OOC
176   {SIM_USAGE(kHIDUsage_Sim_Elevator), kHIDUsageAxis, "elevator"},
177   {SIM_USAGE(kHIDUsage_Sim_ElevatorTrim), kHIDUsageAxis, "elevator-trim"},
178   {SIM_USAGE(kHIDUsage_Sim_Rudder), kHIDUsageAxis, "rudder"},
179   {SIM_USAGE(kHIDUsage_Sim_Throttle), kHIDUsageAxis, "throttle"},
180   {SIM_USAGE(kHIDUsage_Sim_FlightCommunications), kHIDUsageOOC, "button-flight-communications"}, // OOC
181   {SIM_USAGE(kHIDUsage_Sim_FlareRelease), kHIDUsageOSC, "button-flare-release"},
182   {SIM_USAGE(kHIDUsage_Sim_LandingGear), kHIDUsageOOC, "button-landing-gear"}, // OOC
183   {SIM_USAGE(kHIDUsage_Sim_ToeBrake), kHIDUsageAxis, "toe-brake"},
184   {SIM_USAGE(kHIDUsage_Sim_Trigger), kHIDUsageMC, "button-trigger"},
185   {SIM_USAGE(kHIDUsage_Sim_WeaponsArm), kHIDUsageOOC, "button-weapons-arm"}, // OOC
186   {SIM_USAGE(kHIDUsage_Sim_Weapons), kHIDUsageOSC, "button-weapons"},
187   {SIM_USAGE(kHIDUsage_Sim_WingFlaps), kHIDUsageAxis, "wing-flaps"},  // DV
188   {SIM_USAGE(kHIDUsage_Sim_Accelerator), kHIDUsageAxis, "accelerator"}, // DV
189   {SIM_USAGE(kHIDUsage_Sim_Brake), kHIDUsageAxis, "brake"}, // DV
190   {SIM_USAGE(kHIDUsage_Sim_Clutch), kHIDUsageAxis, "clutch"}, // DV
191   {SIM_USAGE(kHIDUsage_Sim_Shifter), kHIDUsageAxis, "shifter"}, // DV
192   {SIM_USAGE(kHIDUsage_Sim_Steering), kHIDUsageAxis, "steering"}, // DV
193   {SIM_USAGE(kHIDUsage_Sim_TurretDirection), kHIDUsageAxis, "turret-direction"}, // DV
194   {SIM_USAGE(kHIDUsage_Sim_BarrelElevation), kHIDUsageAxis, "barrel-elevation"}, // DV
195   {SIM_USAGE(kHIDUsage_Sim_DivePlane), kHIDUsageAxis, "dive-plane"}, // DV
196   {SIM_USAGE(kHIDUsage_Sim_Ballast), kHIDUsageAxis, "ballast"}, // DV
197   {SIM_USAGE(kHIDUsage_Sim_BicycleCrank), kHIDUsageAxis, "bicycle-crank"}, // DV
198   {SIM_USAGE(kHIDUsage_Sim_HandleBars), kHIDUsageAxis, "handle-bars"}, // DV
199   {SIM_USAGE(kHIDUsage_Sim_FrontBrake), kHIDUsageAxis, "front-brake"}, // DV
200   {SIM_USAGE(kHIDUsage_Sim_RearBrake), kHIDUsageAxis, "rear-brake"}, // DV
201
202   // Digitizer Controls Page
203   {DIG_USAGE(kHIDUsage_Dig_TipPressure), kHIDUsageAxis, "tip-pressure"}, // DV
204   {DIG_USAGE(kHIDUsage_Dig_BarrelPressure), kHIDUsageAxis, "barrel-pressure"}, // DV
205   {DIG_USAGE(kHIDUsage_Dig_InRange), kHIDUsageMC, "in-range"}, // MC
206   {DIG_USAGE(kHIDUsage_Dig_Touch), kHIDUsageMC, "touch"}, // MC
207   {DIG_USAGE(kHIDUsage_Dig_Untouch), kHIDUsageOSC, "button-untouch"}, // OSC
208   {DIG_USAGE(kHIDUsage_Dig_Tap), kHIDUsageOSC, "button-tap"}, // OSC
209   {DIG_USAGE(kHIDUsage_Dig_Quality), kHIDUsageDV, "quality"}, // DV
210   {DIG_USAGE(kHIDUsage_Dig_DataValid), kHIDUsageDV, "button-data-valid"}, // MC
211   {DIG_USAGE(kHIDUsage_Dig_TransducerIndex), kHIDUsageDV, "transducer-index"}, // DV
212   {DIG_USAGE(kHIDUsage_Dig_BatteryStrength), kHIDUsageDV, "battery-strength"}, // DV
213   {DIG_USAGE(kHIDUsage_Dig_Invert), kHIDUsageMC, "invert"}, // MC
214   {DIG_USAGE(kHIDUsage_Dig_XTilt), kHIDUsageAxis, "x-tilt"}, // DV
215   {DIG_USAGE(kHIDUsage_Dig_YTilt), kHIDUsageAxis, "y-tilt"}, // DV
216   {DIG_USAGE(kHIDUsage_Dig_Azimuth), kHIDUsageAxis, "azimuth"}, // DV
217   {DIG_USAGE(kHIDUsage_Dig_Altitude), kHIDUsageAxis, "altitude"}, // DV
218   {DIG_USAGE(kHIDUsage_Dig_Twist), kHIDUsageAxis, "twist"}, // DV
219   {DIG_USAGE(kHIDUsage_Dig_TipSwitch), kHIDUsageMC, "button-tipswitch"}, // MC
220   {DIG_USAGE(kHIDUsage_Dig_SecondaryTipSwitch), kHIDUsageMC, "button-secondary-tipswitch"}, // MC
221   {DIG_USAGE(kHIDUsage_Dig_BarrelSwitch), kHIDUsageMC, "button-barrelswitch"}, // MC
222   {DIG_USAGE(kHIDUsage_Dig_Eraser), kHIDUsageMC, "eraser"}, // MC
223   {DIG_USAGE(kHIDUsage_Dig_TabletPick), kHIDUsageMC, "table-pick"}, // MC
224
225  // Consumer Page
226   {CON_USAGE(kHIDUsage_Csmr_Plus10), kHIDUsageOSC, "plus10"},
227   {CON_USAGE(kHIDUsage_Csmr_Plus100), kHIDUsageOSC, "plus100"},
228   {CON_USAGE(kHIDUsage_Csmr_AMOrPM), kHIDUsageOSC, "am-pm"},
229   {CON_USAGE(kHIDUsage_Csmr_Power), kHIDUsageOOC, "power"},
230   {CON_USAGE(kHIDUsage_Csmr_Reset), kHIDUsageOSC, "reset"},
231   {CON_USAGE(kHIDUsage_Csmr_Sleep), kHIDUsageOSC, "sleep"},
232   {CON_USAGE(kHIDUsage_Csmr_SleepAfter), kHIDUsageOSC, "sleep-after"},
233   {CON_USAGE(kHIDUsage_Csmr_SleepMode), kHIDUsageRTC, "sleep-mode"},
234   {CON_USAGE(kHIDUsage_Csmr_Illumination), kHIDUsageOOC, "illumination"},
235   {CON_USAGE(kHIDUsage_Csmr_Menu), kHIDUsageOOC, "menu"},
236   {CON_USAGE(kHIDUsage_Csmr_MenuPick), kHIDUsageOSC, "menu-pick"},
237   {CON_USAGE(kHIDUsage_Csmr_MenuUp), kHIDUsageOSC, "menu-up"},
238   {CON_USAGE(kHIDUsage_Csmr_MenuDown), kHIDUsageOSC, "menu-down"},
239   {CON_USAGE(kHIDUsage_Csmr_MenuLeft), kHIDUsageOSC, "menu-left"},
240   {CON_USAGE(kHIDUsage_Csmr_MenuRight), kHIDUsageOSC, "menu-right"},
241   {CON_USAGE(kHIDUsage_Csmr_MenuEscape), kHIDUsageOSC, "menu-escape"},
242   {CON_USAGE(kHIDUsage_Csmr_MenuValueIncrease), kHIDUsageOSC, "menu-value-increase"},
243   {CON_USAGE(kHIDUsage_Csmr_MenuValueDecrease), kHIDUsageOSC, "menu-value-decrease"},
244   {CON_USAGE(kHIDUsage_Csmr_DataOnScreen), kHIDUsageOOC, "data-on-screen"},
245   {CON_USAGE(kHIDUsage_Csmr_ClosedCaption), kHIDUsageOOC, "closed-caption"},
246   {CON_USAGE(kHIDUsage_Csmr_ClosedCaptionSelect), kHIDUsageSel, "closed-caption-select"},
247   {CON_USAGE(kHIDUsage_Csmr_VCROrTV), kHIDUsageOOC, "vcr-tv"},
248   {CON_USAGE(kHIDUsage_Csmr_BroadcastMode), kHIDUsageOSC, "broadcast-mode"},
249   {CON_USAGE(kHIDUsage_Csmr_Snapshot), kHIDUsageOSC, "snapshot"},
250   {CON_USAGE(kHIDUsage_Csmr_Still), kHIDUsageOSC, "still"},
251   {CON_USAGE(kHIDUsage_Csmr_Assign), kHIDUsageOSC, "assign"},
252   {CON_USAGE(kHIDUsage_Csmr_ModeStep), kHIDUsageOSC, "mode-step"},
253   {CON_USAGE(kHIDUsage_Csmr_RecallLast), kHIDUsageOSC, "recall-last"},
254   {CON_USAGE(kHIDUsage_Csmr_EnterChannel), kHIDUsageOSC, "enter-channel"},
255   {CON_USAGE(kHIDUsage_Csmr_OrderMovie), kHIDUsageOSC, "order-movie"},
256   {CON_USAGE(kHIDUsage_Csmr_Channel), kHIDUsageDV, "channel"}, // LC
257   {CON_USAGE(kHIDUsage_Csmr_MediaSelection), kHIDUsageSel, "media-selection"},
258   {CON_USAGE(kHIDUsage_Csmr_MediaSelectComputer), kHIDUsageSel, "media-select-computer"},
259   {CON_USAGE(kHIDUsage_Csmr_MediaSelectTV), kHIDUsageSel, "media-select-tv"},
260   {CON_USAGE(kHIDUsage_Csmr_MediaSelectWWW), kHIDUsageSel, "media-seleci-www"},
261   {CON_USAGE(kHIDUsage_Csmr_MediaSelectDVD), kHIDUsageSel, "media-select-dvd"},
262   {CON_USAGE(kHIDUsage_Csmr_MediaSelectTelephone), kHIDUsageSel, "media-select-telephone"},
263   {CON_USAGE(kHIDUsage_Csmr_MediaSelectProgramGuide), kHIDUsageSel, "media-select-programguide"},
264   {CON_USAGE(kHIDUsage_Csmr_MediaSelectVideoPhone), kHIDUsageSel, "media-select-videophone"},
265   {CON_USAGE(kHIDUsage_Csmr_MediaSelectGames), kHIDUsageSel, "media-select-games"},
266   {CON_USAGE(kHIDUsage_Csmr_MediaSelectMessages), kHIDUsageSel, "media-select-messages"},
267   {CON_USAGE(kHIDUsage_Csmr_MediaSelectCD), kHIDUsageSel, "media-select-cd"},
268   {CON_USAGE(kHIDUsage_Csmr_MediaSelectVCR), kHIDUsageSel, "media-select-vcr"},
269   {CON_USAGE(kHIDUsage_Csmr_MediaSelectTuner), kHIDUsageOSC, "media-select-tuner"},
270   {CON_USAGE(kHIDUsage_Csmr_Quit), kHIDUsageOSC, "quit"},
271   {CON_USAGE(kHIDUsage_Csmr_Help), kHIDUsageOOC, "help"},
272   {CON_USAGE(kHIDUsage_Csmr_MediaSelectTape), kHIDUsageSel, "media-select-tape"},
273   {CON_USAGE(kHIDUsage_Csmr_MediaSelectCable), kHIDUsageSel, "media-select-cable"},
274   {CON_USAGE(kHIDUsage_Csmr_MediaSelectSatellite), kHIDUsageSel, "media-select-satellite"},
275   {CON_USAGE(kHIDUsage_Csmr_MediaSelectSecurity), kHIDUsageSel, "media-select-security"},
276   {CON_USAGE(kHIDUsage_Csmr_MediaSelectHome), kHIDUsageSel, "media-select-home"},
277   {CON_USAGE(kHIDUsage_Csmr_MediaSelectCall), kHIDUsageSel, "media-select-call"},
278   {CON_USAGE(kHIDUsage_Csmr_ChannelIncrement), kHIDUsageOSC, "channel-increment"},
279   {CON_USAGE(kHIDUsage_Csmr_ChannelDecrement), kHIDUsageOSC, "channel-decrement"},
280   {CON_USAGE(kHIDUsage_Csmr_Media), kHIDUsageSel, "media"},
281   {CON_USAGE(kHIDUsage_Csmr_VCRPlus), kHIDUsageOSC, "vcr-plus"},
282   {CON_USAGE(kHIDUsage_Csmr_Once), kHIDUsageOSC, "once"},
283   {CON_USAGE(kHIDUsage_Csmr_Daily), kHIDUsageOSC, "daily"},
284   {CON_USAGE(kHIDUsage_Csmr_Weekly), kHIDUsageOSC, "weekly"},
285   {CON_USAGE(kHIDUsage_Csmr_Monthly), kHIDUsageOSC, "monthly"},
286   {CON_USAGE(kHIDUsage_Csmr_Play), kHIDUsageOOC, "play"},
287   {CON_USAGE(kHIDUsage_Csmr_Pause), kHIDUsageOOC, "pause"},
288   {CON_USAGE(kHIDUsage_Csmr_Record), kHIDUsageOOC, "record"},
289   {CON_USAGE(kHIDUsage_Csmr_FastForward), kHIDUsageOOC,"fastforward"},
290   {CON_USAGE(kHIDUsage_Csmr_Rewind), kHIDUsageOOC, "rewind"},
291   {CON_USAGE(kHIDUsage_Csmr_ScanNextTrack), kHIDUsageOSC, "scan-next-track"},
292   {CON_USAGE(kHIDUsage_Csmr_ScanPreviousTrack), kHIDUsageOSC, "scan-previous-track"},
293   {CON_USAGE(kHIDUsage_Csmr_Stop), kHIDUsageOSC, "stop"},
294   {CON_USAGE(kHIDUsage_Csmr_Eject), kHIDUsageOSC, "eject"},
295   {CON_USAGE(kHIDUsage_Csmr_RandomPlay), kHIDUsageOOC, "random-play"},
296   {CON_USAGE(kHIDUsage_Csmr_SelectDisc), kHIDUsageNotSupported, "select-disc"}, //NamedArray
297
298   {CON_USAGE(kHIDUsage_Csmr_VolumeIncrement), kHIDUsageRTC, "volume-increment"},
299   {CON_USAGE(kHIDUsage_Csmr_VolumeDecrement), kHIDUsageRTC, "volume-decrement"},
300   {CON_USAGE(kHIDUsage_Csmr_PlayOrPause), kHIDUsageOSC, "play-pause"},
301   {CON_USAGE(kHIDUsage_Csmr_Mute), kHIDUsageOOC, "mute"},
302   // Too many... and the rest are T.B.D. ;-)
303   {-1, kHIDElementPage, ""},
304 };
305
306 static HIDTypeByID hidTypeByID(HID_TYPE_TABLE);
307 static HIDTypeByID hidPageByID(HID_PAGE_TABLE);
308 static HIDTypeByID hidUsageByID(HID_USAGE_TABLE);
309
310
311 HIDElement::HIDElement(CFDictionaryRef element, long page, long usage) : 
312   page(page), usage(usage), value(0.0), lastValue(0.0) {
313
314   cookie = (IOHIDElementCookie)GetHIDElementLongValue(element, kIOHIDElementCookieKey);
315   name = hidUsageByID.getName(USAGE_KEY(page, usage));
316 }
317
318 long HIDElement::read(IOHIDDeviceInterface **interface)
319 {
320   IOHIDEventStruct event;
321   lastValue = value;
322   IOReturn ret = (*interface)->getElementValue(interface, cookie, &event);
323   if (ret == kIOReturnSuccess) {
324     value = event.value;
325 //    SG_LOG(SG_INPUT, SG_BULK, "Element: " << name << "; value = " << value);
326     return event.value;
327   } else {
328     SG_LOG(SG_INPUT, SG_WARN, "Failed reading value for HID Element: " << name);
329     return 0;
330   }
331 }
332
333 bool HIDElement::isUpdated()
334 {
335   return (value != lastValue);
336 }
337
338 void HIDElement::generateEvent(FGMacOSXInputDevice *device, double dt, int modifiers)
339 {
340   SG_LOG(SG_INPUT, SG_DEBUG, "Generating Input Event: " << name << "=" << value);
341   FGMacOSXEventData eventData(name, (float)value, dt, modifiers);
342   device->HandleEvent(eventData);
343 }
344
345 AxisElement::AxisElement(CFDictionaryRef element, long page, long usage) : 
346   HIDElement(element, page, usage), dead_band(0.00), saturate(1.0)
347 {
348   min = GetHIDElementLongValue(element, kIOHIDElementMinKey); 
349   max = GetHIDElementLongValue(element, kIOHIDElementMaxKey);
350   isRelative = GetHIDElementBooleanValue(element, kIOHIDElementIsRelativeKey);
351   isWrapping = GetHIDElementBooleanValue(element, kIOHIDElementIsWrappingKey);
352   isNonLinear = GetHIDElementBooleanValue(element, kIOHIDElementIsNonLinearKey);
353
354   name = ((isRelative == true) ? "rel-" : "abs-") + name;
355
356   center = min + (max - abs(min)) * 0.5;
357   SG_LOG(SG_INPUT, SG_DEBUG, "HID Axis Element; " << name << " min: " << min << " max:" << max << " center: " << center);
358   SG_LOG(SG_INPUT, SG_DEBUG, "isRelative=" << isRelative << ", isWrapping=" << isWrapping << ", isNonLinear=" << isNonLinear);
359 }
360
361 long AxisElement::read(IOHIDDeviceInterface **interface)
362 {
363   lastValue = value;
364   return HIDElement::read(interface);
365 }
366
367 // FIXME: This can be removed when AxisInputEvent can normalize its value
368 void AxisElement::generateEvent(FGMacOSXInputDevice *device, double dt, int modifiers)
369 {
370   float normalizedValue = (float)value;
371   if (!isRelative) {
372     normalizedValue = (value - (float)center) / (float)(max - center);
373     if (fabs(normalizedValue) < dead_band)
374       normalizedValue = 0.0;
375   }
376
377   SG_LOG(SG_INPUT, SG_DEBUG, "Generating Input Event: " << name << "=" << normalizedValue);
378   FGMacOSXEventData eventData(name, normalizedValue, dt, modifiers);
379   device->HandleEvent(eventData);
380 }
381
382 ButtonElement::ButtonElement(CFDictionaryRef element, long page, long usage) :
383   HIDElement(element, page, usage)
384 {
385   if (name == "") {
386     stringstream ss;
387     ss << (page == kHIDPage_KeyboardOrKeypad ? "keyboard-" : "button-") << usage - 1;
388     ss >> name;
389   }
390 }
391
392 HatElement::HatElement(CFDictionaryRef element, long page, long usage, int id) :
393   HIDElement(element, page, usage), id(id)
394 {
395   min = GetHIDElementLongValue(element, kIOHIDElementMinKey);
396   max = GetHIDElementLongValue(element, kIOHIDElementMaxKey);
397   lastValue = 8;
398 }
399
400 void HatElement::generateEvent(FGMacOSXInputDevice *device, double dt, int modifiers)
401 {
402   // Hat value is from 0 to 8, representing:
403   // 0:N, 1:NE, 2:E, 3:SE, 4:S, 5:SW, 6:W, 7:NW, 8:Neutral
404   // FG can't bind hat directly, so need to convert its value to two axis events
405   static float xvalues[] = {0, 1, 1,  1,  0, -1, -1, -1, 0};
406   static float yvalues[] = {1, 1, 0, -1, -1, -1,  0,  1, 0};
407   stringstream ss1, ss2;
408   ss1 << "abs-hat" << id << "-x";
409   SG_LOG(SG_INPUT, SG_BULK, "Generating Input Event: " << ss1.str() << "=" << xvalues[(int)value]);
410   FGMacOSXEventData eventDataX(ss1.str(), xvalues[(int)value], dt, modifiers);
411   ss2 << "abs-hat" << id << "-y";
412   SG_LOG(SG_INPUT, SG_BULK, "Generating Input Event: " << ss2.str() << "=" << yvalues[(int)value]);
413   FGMacOSXEventData eventDataY(ss2.str(), yvalues[(int)value], dt, modifiers);
414   device->HandleEvent((FGEventData &)eventDataX);
415   device->HandleEvent((FGEventData &)eventDataY);
416 }
417
418
419 LEDElement::LEDElement(CFDictionaryRef element, long page, long usage) :
420   HIDElement(element, page, usage)
421 {
422   stringstream ss;
423   if (name == "") {
424     ss << "led-" << usage;
425     ss >> name;
426   }
427 }
428
429 void LEDElement::write(IOHIDDeviceInterface **interface, double value) {
430   IOHIDEventStruct event = (IOHIDEventStruct){kIOHIDElementTypeOutput, cookie, 0, {0}, 0, 0};
431   event.value = value;
432   (*interface)->setElementValue(interface, cookie, &event, 0, NULL, NULL, NULL);
433 }
434
435 //
436 // This is just for testing....
437 //
438 FeatureElement::FeatureElement(CFDictionaryRef element, long page, long usage, int count=1) :
439   HIDElement(element, page, usage) 
440 {
441   stringstream ss;
442   if (name == "") {
443     ss << "feature-" << usage;
444     if (count > 1) 
445       ss << "-" << count;
446     ss >> name;
447   }
448 }
449
450 long FeatureElement::read(IOHIDDeviceInterface **interface) {
451   IOHIDEventStruct event;
452   IOReturn ret = (*interface)->queryElementValue(interface, cookie, &event, 0, NULL, NULL, NULL);
453   if (ret != kIOReturnSuccess) {
454     ret = (*interface)->getElementValue(interface, cookie, &event);
455     if (ret != kIOReturnSuccess) {
456       SG_LOG(SG_INPUT, SG_WARN, "Can't get element value for feature element: " << getName());
457       return 0;
458     }
459   }
460   return event.value;
461 }
462
463 //
464 // HIDElementFactory
465 //
466 void HIDElementFactory::create(CFTypeRef element, FGMacOSXInputDevice *inputDevice)
467 {
468   assert(CFGetTypeID(element) == CFArrayGetTypeID());
469   CFRange range = {0, CFArrayGetCount((CFArrayRef)element)};
470   CFArrayApplyFunction((CFArrayRef) element, range, 
471                        HIDElementFactory::elementEnumerator, (void *)inputDevice);
472 };
473
474
475 void HIDElementFactory::elementEnumerator( const void *element, void *inputDevice) {
476   if (CFGetTypeID((CFTypeRef) element) != CFDictionaryGetTypeID()) {
477     SG_LOG(SG_INPUT, SG_WARN, "Element Enumerator passed non-dictionary value.");
478   }
479   HIDElementFactory::parseElement((CFDictionaryRef)element, (FGMacOSXInputDevice *)inputDevice);
480 };
481
482
483 void HIDElementFactory::parseElement(CFDictionaryRef element, FGMacOSXInputDevice *inputDevice) {
484   long page = GetHIDElementLongValue(element, kIOHIDElementUsagePageKey);
485   long usage = GetHIDElementLongValue(element, kIOHIDElementUsageKey);
486
487   static int id=0;
488   static map<FGMacOSXInputDevice *, map<long, unsigned> > elementCount;
489
490   long type = GetHIDElementLongValue(element, kIOHIDElementTypeKey);
491
492   if (type == kIOHIDElementTypeCollection) {
493     id = 0;
494     SG_LOG(SG_INPUT, SG_DEBUG, "Collection: " << hidTypeByID.getName(type) << "(" << type << ")" 
495                                               << ":" << hidPageByID.getName(page) << "(" << page << ")" 
496                                               << ":" << hidUsageByID.getName(USAGE_KEY(page, usage)) << "(" << usage << ")");
497     HIDElementFactory::create(CFDictionaryGetValue(element, CFSTR(kIOHIDElementKey)), inputDevice);
498   } else {
499     HIDUsageType usageType = hidUsageByID.getType(USAGE_KEY(page, usage));
500     // FIXME: Any other elegant way for counting the same usage on a device?
501     // This is mainly needed for feature / hat elements to avoid assigning the sane event name.
502     elementCount[inputDevice][USAGE_KEY(page, usage)] += 1;
503
504     switch (usageType) {
505       case kHIDUsageAxis:
506           inputDevice->addElement(new AxisElement(element, page, usage));
507           break;
508       case kHIDUsageDV:
509       case kHIDUsageDF:
510           inputDevice->addElement(new HIDElement(element, page, usage));
511           break;
512       case kHIDUsageHat:
513           inputDevice->addElement(new HatElement(element, page, usage, elementCount[inputDevice][USAGE_KEY(page, usage)]));
514           break;
515       case kHIDUsageOOC:
516       case kHIDUsageOSC:
517       case kHIDUsageMC:
518       case kHIDUsageRTC:
519           if (usage > 0)
520             inputDevice->addElement(new ButtonElement(element, page, usage));
521           break;
522           
523       default:
524         if ((page == kHIDPage_Button || type == kIOHIDElementTypeInput_Button) && usage > 0) {
525         // FIXME: most of KeyboardOrKeypad elements should be treated as Selector type, not as Button...
526           inputDevice->addElement(new ButtonElement(element, page, usage));
527         } else if (page == kHIDPage_LEDs && usage > 0) {
528           inputDevice->addElement(new LEDElement(element, page, usage));
529 /* Feature elements are not fully tested yet
530         } else if (type == kIOHIDElementTypeFeature) {
531           // just for testing feature elements
532           inputDevice->addElement(new FeatureElement(element, page, usage, elementCount[inputDevice][USAGE_KEY(page, usage)]));
533 */
534         } else {
535           SG_LOG(SG_INPUT, SG_INFO, "HID Element Page/Usage is not supported: type=" << hidTypeByID.getName(type) 
536                     << "(" << type << ")" << ", page=" << hidPageByID.getName(page) << "(" << page << ")" 
537                     << ", usage=" << usage);
538         }
539     }
540   }
541 }
542
543 //
544 // FGMacOSXInputDevice implementation
545 //
546
547 FGMacOSXInputDevice::FGMacOSXInputDevice(io_object_t device) : device(device), devInterface(NULL)
548 {
549   CFDictionaryRef properties = getProperties();
550   string deviceName = GetHIDElementStringValue(properties, kIOHIDProductKey);
551   if (deviceName == "") {
552     deviceName = GetHIDElementStringValue(properties, "USB Product Name");
553   }
554
555   SetName(deviceName);
556   CFRelease(properties);
557 }
558
559 const char *FGMacOSXInputDevice::TranslateEventName(FGEventData &eventData)
560 {
561   FGMacOSXEventData &macEvent = (FGMacOSXEventData &)eventData;
562   return macEvent.name.c_str();
563 }
564
565
566 //
567 // Outputs value to an writable element (like LEDElement)
568 //
569 void FGMacOSXInputDevice::Send(const char *eventName, double value)
570 {
571   HIDElement *element = elements[eventName];
572   if (element) {
573     element->write(devInterface, value);
574   } else {
575     SG_LOG(SG_INPUT, SG_WARN, "No element to handle event: " << eventName);
576   }
577 }
578
579
580 CFDictionaryRef FGMacOSXInputDevice::getProperties(io_object_t device)
581 {
582   IOReturn ret;
583   CFMutableDictionaryRef properties;
584
585   ret = IORegistryEntryCreateCFProperties( device, &properties, kCFAllocatorDefault, kNilOptions);
586   if (ret != kIOReturnSuccess || !properties) {
587     SG_LOG(SG_INPUT, SG_WARN, "Error getting device properties.");
588     return NULL;
589   }
590
591   return properties;
592 }
593
594 //
595 // Adds HID element to FGMacOSXInputDevice.
596 // On Mac OS X, update() will read value of each HID element.
597 // Thus, FGMacOSXInputDevice needs all supported elements when a device is opened
598 //
599 void FGMacOSXInputDevice::addElement(HIDElement *element)
600 {
601   elements[element->getName()] = element;
602   int count = elements.size();
603   SG_LOG(SG_INPUT, SG_DEBUG, "adding element " << count << ":" << element->getName());
604 }
605
606 void FGMacOSXInputDevice::Open() {
607   // create device interface
608   IOReturn ret;
609   SInt32 score;
610   IOCFPlugInInterface **plugin;
611   SG_LOG(SG_INPUT, SG_INFO, "Opening HID : " << GetName());
612
613   ret = IOCreatePlugInInterfaceForService(device,
614                                           kIOHIDDeviceUserClientTypeID,
615                                           kIOCFPlugInInterfaceID,
616                                           &plugin, &score);
617         
618   if (ret != kIOReturnSuccess) {
619     SG_LOG(SG_INPUT, SG_ALERT, "Error creating a plugin for HID : " << GetName());
620     throw std::exception();
621     return;
622   }
623
624   HRESULT result = (*plugin)->QueryInterface(plugin, 
625                                              CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID), 
626                                              (LPVOID*)&devInterface );
627         
628   if (result != S_OK)
629     SG_LOG(SG_INPUT, SG_WARN, "Failed Querying HID plugin interface: " << GetName());
630
631   (*plugin)->Release(plugin); // don't leak a ref
632   if (devInterface == NULL) {
633     return;
634     throw std::exception();
635   }
636         
637   // store the interface in this instance
638   ret = (*devInterface)->open(devInterface, 0);
639   if (ret != kIOReturnSuccess) {
640     SG_LOG(SG_INPUT, SG_ALERT, "Error opening device interface: " << GetName());
641     throw std::exception();
642     return;
643   }
644   CFDictionaryRef props = getProperties();
645                 
646   // recursively adds all supported HID elements to FGMacOSXInputDevice
647   CFTypeRef topLevelElement = CFDictionaryGetValue (props, CFSTR(kIOHIDElementKey));
648   HIDElementFactory::create(topLevelElement, this);
649   CFRelease(props);
650 }
651
652 void FGMacOSXInputDevice::Close() {
653   SG_LOG(SG_INPUT, SG_INFO, "Closing HID: " << GetName());
654   if (devInterface) {
655     (*devInterface)->close(devInterface);
656   }
657
658   map<string, HIDElement *>::iterator it;
659   for (it = elements.begin(); it != elements.end(); it++) {
660     if ((*it).second)
661       delete (*it).second;
662   }
663   elements.clear();
664 }
665
666 //
667 // Reads values of assigned HIDElement and generates events
668 //
669 void FGMacOSXInputDevice::update(double dt)
670 {
671   map<string, HIDElement *>::iterator it;
672   for (it = elements.begin(); it != elements.end(); it++) {
673     (*it).second->read(devInterface);
674     if ((*it).second->isUpdated()) { // Is this needed?
675       int modifiers = fgGetKeyModifiers();
676       (*it).second->generateEvent(this, dt, modifiers);
677     }
678   }
679 }
680
681 //
682 // FGMacOSXEventInput implementation
683 //
684 FGMacOSXEventInput::~FGMacOSXEventInput() {
685   deviceIndices.clear();
686 }
687
688 void FGMacOSXEventInput::init()
689 {
690   IOReturn ret;
691   SG_LOG(SG_INPUT, SG_INFO, "initializing FGMacOSXEventInput");
692
693   // We want all HID devices for matching
694   CFMutableDictionaryRef matchingDictionary = IOServiceMatching(kIOHIDDeviceKey);
695
696   // Needs to retain machingDict since IOServiceAddMatchingNotification consumes one reference.
697   matchingDictionary = (CFMutableDictionaryRef) CFRetain(matchingDictionary);
698   matchingDictionary = (CFMutableDictionaryRef) CFRetain(matchingDictionary);
699
700   // Registers Hotplug notification for plug and play.
701   notifyPort = IONotificationPortCreate(kIOMasterPortDefault); 
702   runLoopSource = IONotificationPortGetRunLoopSource(notifyPort);
703   CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopDefaultMode);
704   ret = IOServiceAddMatchingNotification(notifyPort, kIOFirstMatchNotification, 
705                                          matchingDictionary, FGMacOSXEventInput::deviceAttached, (void *)this, &addedIterator);
706   ret = IOServiceAddMatchingNotification(notifyPort, kIOTerminatedNotification, 
707                                          matchingDictionary, FGMacOSXEventInput::deviceDetached, (void *)this, &removedIterator);
708
709   // prepare for notification by calling these callback funcs to remove existing HID device iterators
710   FGMacOSXEventInput::deviceAttached((void *)this, addedIterator);
711   FGMacOSXEventInput::deviceDetached((void *)this, removedIterator);
712 }
713
714
715 void FGMacOSXEventInput::attachDevice(io_iterator_t iterator)
716 {
717   io_object_t device;
718
719   while ((device = IOIteratorNext(iterator))) {
720     FGMacOSXInputDevice *inputDevice = new FGMacOSXInputDevice(device);
721     if (inputDevice) {
722       SG_LOG(SG_INPUT, SG_INFO, "HID Device Atached: " << inputDevice->GetName());
723       unsigned index = AddDevice(inputDevice);
724       // Needs to check if AddDevice closed the device due to lack of config file
725       if (index != FGEventInput::INVALID_DEVICE_INDEX) {
726         // maps device with FG device index. 
727         // This map is needed in detachDevice to tell FGEventInput which deivce ID is to be removed
728         deviceIndices[device] = index;
729       }
730     }
731     IOObjectRelease(device);
732   }
733 }
734
735
736 void FGMacOSXEventInput::detachDevice(io_iterator_t iterator)
737 {
738   io_object_t device;
739
740   while ((device = IOIteratorNext(iterator))) {
741     unsigned index = deviceIndices[device];
742     if (index != FGEventInput::INVALID_DEVICE_INDEX) {
743       FGMacOSXInputDevice *inputDevice = dynamic_cast<FGMacOSXInputDevice *>(input_devices[index]);
744       if (inputDevice) {
745         SG_LOG(SG_INPUT, SG_INFO, "HID Device Detached: " << inputDevice->GetName());
746         RemoveDevice(index);
747         deviceIndices.erase(device); 
748       } else {
749         SG_LOG(SG_INPUT, SG_WARN, "Invalid device index:" << index << ". Detach failed.");
750       }
751     } else {
752       SG_LOG(SG_INPUT, SG_INFO, "Device ID unmatched: " << (int)device << " No HID deivce is detached since it is not supported by FG.");
753     }
754     IOObjectRelease(device);
755   }
756 }
757
758 //
759 // read all elements in each input device
760 //
761 void FGMacOSXEventInput::update(double dt)
762 {
763   FGEventInput::update(dt);
764
765   map<int, FGInputDevice*>::const_iterator it;
766   for (it = input_devices.begin(); it != input_devices.end(); it++) {
767     if ((*it).second) {
768       FGMacOSXInputDevice *inputDevice = dynamic_cast<FGMacOSXInputDevice *>((*it).second);
769       if (inputDevice) {
770         inputDevice->update(dt);
771       } else {
772         SG_LOG(SG_INPUT, SG_WARN, "Invalid device. Update failed.");
773       }
774     }
775   }
776 }