Debian Codegraph @

src/Gui/3Dconnexion/GuiNativeEventMac.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2018 Torsten Sadowski <tsadowski[at]gmx.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 /*
24 Development tools and related technology provided under license from 3Dconnexion.
25 (c) 1992 - 2012 3Dconnexion. All rights reserved
26 */
27
28 /*
29 With special thanks to marcxs for making the first steps
30 */
31
32 #include <FCConfig.h>
33 #include "GuiNativeEventMac.h"
34
35 #include <unistd.h>
36 #include "GuiApplicationNativeEventAware.h"
37 #include <Base/Console.h>
38
39 // Suppress warnings to kConnexionMsgDeviceState and tdxAppID
40 #if defined(__clang__)
41 # pragma clang diagnostic push
42 # pragma clang diagnostic ignored "-Wfour-char-constants"
43 #endif
44
45 UInt16 Gui::GuiNativeEvent::tdxClientID = 0;
46 uint32_t Gui::GuiNativeEvent::lastButtons = 0;
47
48 /* ----------------------------------------------------------------------------
49 Handler for driver events. This function is able to handle the events in
50 different ways: (1) re-package the events as Carbon events, (2) compute
51 them directly, (3) write the event info in a shared memory location for
52 usage by reader threads.
53 */
54 void Gui::GuiNativeEvent::tdx_drv_handler(
55 io_connect_t connection,
56 natural_t messageType,
57 void* messageArgument
58 )
59 {
60 static bool firstTime = true;
61 if (firstTime) {
62 firstTime = false;
63 Base::Console().log("Call connexion handler\n");
64 }
65 Q_UNUSED(connection)
66 // printf("tdx_drv_handler\n");
67 // printf("connection: %X\n", connection);
68 // printf("messageType %c%c%c%c\n", messageType/0x1000000, messageType/0x10000,
69 // messageType/0x100, messageType);
70 auto msg = (ConnexionDeviceStatePtr)messageArgument;
71
72 switch (messageType) {
73 case kConnexionMsgDeviceState:
74 /* Device state messages are broadcast to all clients. It is up to
75 * the client to figure out if the message is meant for them. This
76 * is done by comparing the "client" id sent in the message to our
77 * assigned id when the connection to the driver was established.
78 */
79 // printf("msg->client: %d, tdxClientID: %d\n", msg->client, tdxClientID);
80 Base::Console().log("msg->client: %d, msg->command: %d\n", msg->client, msg->command);
81
82 if (msg->client == tdxClientID) {
83 switch (msg->command) {
84 case kConnexionCmdHandleAxis: {
85 motionDataArray[0] = -msg->axis[0];
86 motionDataArray[1] = msg->axis[1];
87 motionDataArray[2] = msg->axis[2];
88 motionDataArray[3] = -msg->axis[3];
89 motionDataArray[4] = msg->axis[4];
90 motionDataArray[5] = msg->axis[5];
91 mainApp->postMotionEvent(motionDataArray);
92 break;
93 }
94
95 case kConnexionCmdHandleButtons: {
96 // printf("value: %d\n", msg->value);
97 // printf("buttons: %u\n", msg->buttons);
98 uint32_t changedButtons = msg->buttons ^ lastButtons;
99 uint32_t pressedButtons = msg->buttons & changedButtons;
100 uint32_t releasedButtons = lastButtons & changedButtons;
101 for (uint8_t bt = 0; bt < 32; bt++) {
102 if (pressedButtons & 1) {
103 mainApp->postButtonEvent(bt, 1);
104 }
105 pressedButtons = pressedButtons >> 1;
106 }
107 for (uint8_t bt = 0; bt < 32; bt++) {
108 if (releasedButtons & 1) {
109 mainApp->postButtonEvent(bt, 0);
110 }
111 releasedButtons = releasedButtons >> 1;
112 }
113
114 lastButtons = msg->buttons;
115 break;
116 }
117
118 default:
119 break;
120
121 } /* switch */
122 }
123 break;
124
125 default:
126 /* other messageTypes can happen and should be ignored */
127 break;
128 }
129 }
130
131 Gui::GuiNativeEvent::GuiNativeEvent(Gui::GUIApplicationNativeEventAware* app)
132 : GuiAbstractNativeEvent(app)
133 {}
134
135 Gui::GuiNativeEvent::~GuiNativeEvent()
136 {
137 // if 3Dconnexion library was loaded at runtime
138 if (InstallConnexionHandlers) {
139 // Close our connection with the 3dx driver
140 if (tdxClientID) {
141 UnregisterConnexionClient(tdxClientID);
142 }
143 CleanupConnexionHandlers();
144 Base::Console().log("Disconnected from 3Dconnexion driver\n");
145 }
146 }
147
148 void Gui::GuiNativeEvent::initSpaceball(QMainWindow* window)
149 {
150 Q_UNUSED(window)
151 OSStatus err;
152 /* make sure the framework is installed */
153 if (SetConnexionHandlers == NULL) {
154 Base::Console().log("3Dconnexion framework not found!\n");
155 return;
156 }
157 /* install 3dx message handler in order to receive driver events */
158 err = SetConnexionHandlers(tdx_drv_handler, 0L, 0L, false);
159 assert(err == 0);
160 if (err) {
161 Base::Console().log("Error installing 3Dconnexion handler\n");
162 return;
163 }
164 /* register our app with the driver */
165 // Pascal string Application name required to register driver for application
166 UInt8 tdxAppName[] = {7, 'F', 'r', 'e', 'e', 'C', 'A', 'D'};
167 tdxClientID = RegisterConnexionClient(
168 kConnexionClientWildcard,
169 tdxAppName,
170 kConnexionClientModeTakeOver,
171 kConnexionMaskAll
172 );
173 if (tdxClientID == 0) {
174 Base::Console().log("Couldn't connect to 3Dconnexion driver\n");
175 return;
176 }
177
178 // Turn on all features and buttons
179 SetConnexionClientMask(tdxClientID, kConnexionMaskAll);
180 SetConnexionClientButtonMask(tdxClientID, kConnexionMaskAllButtons);
181
182 Base::Console().log("3Dconnexion driver initialized. Client ID: %d\n", tdxClientID);
183 mainApp->setSpaceballPresent(true);
184 }
185
186 #include "3Dconnexion/moc_GuiNativeEventMac.cpp"