1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/* -*- Mode: C++; indent-tabs-mode: nil; tab-width: 4 -*-
* -*- coding: utf-8 -*-
*
* Copyright (C) 2023 KylinSoft Co., Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: sundagao <sundagao@kylinos.cn>
*/
#include "input-device-helper.h"
static Display* display = UsdBaseClass::getQx11Info();
Atom InputDeviceHelper::properyToAtom(const char* property)
{
return XInternAtom(display,property,False);
}
bool InputDeviceHelper::supportXinputExtension()
{
int xi_opcode ,event ,error;
return XQueryExtension(display, "XInputExtension" , &xi_opcode , &event, &error);
}
Atom InputDeviceHelper::deviceHadProperty(int device, const char *property)
{
Atom prop = properyToAtom(property);
return deviceHadProperty(device, prop);
}
Atom InputDeviceHelper::deviceHadProperty(int device, Atom prop)
{
int num_props;
Atom found = None;
Atom* props = XIListProperties(display,device,&num_props);
if (prop == None || !props) {
USD_LOG(LOG_WARNING,"get prop/props is failed");
return found;
}
for (int i = 0; i < num_props; ++i) {
if (prop == props[i]) {
found = prop;
}
}
XFree(props);
return found;
}
QVariantList InputDeviceHelper::getDeviceProp(int device, const char* property)
{
Atom prop = properyToAtom(property);
return getDeviceProp(device, prop);
}
QVariantList InputDeviceHelper::getDeviceProp(int device, Atom prop)
{
QVariantList valueList;
Atom real_type;
int real_format;
unsigned long nitems;
unsigned long bytes_after;
unsigned char* data = nullptr;
unsigned char* dataPtr = nullptr;
if (Success == XIGetProperty(display, device, prop, 0,
1000, False, AnyPropertyType, &real_type,
&real_format, &nitems, &bytes_after, &data)) {
dataPtr = data;
Atom float_atom = properyToAtom("FLOAT");
for (int i = 0; i < nitems; ++i) {
switch (real_type) {
case XA_INTEGER:
switch (real_format) {
case 8:
valueList << *(reinterpret_cast<int8_t*>(dataPtr));
break;
case 16:
valueList << *(reinterpret_cast<int16_t*>(dataPtr));
break;
case 32:
valueList << *(reinterpret_cast<int32_t*>(dataPtr));
break;
default:
break;
}
break;
default:
if (float_atom == real_type && real_format == 32) {
valueList << *(reinterpret_cast<float*>(dataPtr));
} else {
USD_LOG(LOG_DEBUG,"property real type is not expanded. real type :%d",real_type);
}
break;
}
dataPtr += real_format / 8;
}
XFree(data);
} else {
USD_LOG(LOG_WARNING,"get device propetry failed .");
}
return valueList;
}
void InputDeviceHelper::setDeviceProp(int device, const char *property, QVariantList value)
{
Atom prop = properyToAtom(property);
return setDeviceProp(device, prop, value);
}
void InputDeviceHelper::setDeviceProp(int device, Atom prop ,QVariantList value)
{
if (prop == None) {
USD_LOG(LOG_WARNING,"device property is none .");
return;
}
Atom real_type;
int real_format;
unsigned long nitems;
unsigned long bytes_after;
union {
unsigned char *c;
int16_t *s;
int32_t *l;
} data = {nullptr};
if (Success != XIGetProperty(display, device, prop, 0,
0, False, AnyPropertyType, &real_type,
&real_format, &nitems, &bytes_after, &data.c)) {
USD_LOG(LOG_WARNING,"get device propetry failed .");
return;
}
XFree(data.c);
Atom float_atom = properyToAtom("FLOAT");
int count = value.count();
data.c = reinterpret_cast<unsigned char*>(calloc(count,sizeof(int32_t)));
for (int i = 0; i < count; ++i) {
switch (real_type) {
//目前设置的属性fomat,以下case都已包含,如有需要可以添加
case XA_INTEGER:
switch (real_format) {
case 8:
data.c[i] = value.at(i).toInt();
break;
case 16:
data.l[i] = value.at(i).toInt();
break;
case 32:
data.l[i] = value.at(i).toInt();
break;
default:
break;
}
break;
default:
if (float_atom == real_type && real_format == 32) {
reinterpret_cast<float*>(data.l)[i] = value.at(i).toFloat();
}
break;
}
}
XIChangeProperty (display, device,prop,
real_type, real_format, PropModeReplace, data.c, count);
XSync(display,False);
free(data.c);
}
int InputDeviceHelper::getDeviceButtonMap(int device, unsigned char** map)
{
int ndevice;
int nbuttons = 0;
XDeviceInfo* deviceInfos = nullptr;
XDeviceInfo* deviceinfo = nullptr;
deviceInfos = XListInputDevices(display,&ndevice);
for (int i = 0; i < ndevice; ++i) {
if (device == deviceInfos[i].id) {
deviceinfo = &deviceInfos[i];
}
}
if (!deviceinfo) {
USD_LOG(LOG_WARNING,"con't find device .");
XFreeDeviceList(deviceInfos);
return nbuttons;
}
XDevice* dev = XOpenDevice(display,device);
if (!dev) {
USD_LOG(LOG_WARNING,"open device %d is failed",dev->device_id);
return nbuttons;
}
for (int i = 0; i < deviceinfo->num_classes; ++i) {
if (deviceinfo->inputclassinfo->c_class == ButtonClass) {
nbuttons = reinterpret_cast<XButtonInfoPtr>(deviceinfo->inputclassinfo)->num_buttons;
}
}
*map = reinterpret_cast<unsigned char*>(malloc(sizeof(unsigned char)*nbuttons));
nbuttons = XGetDeviceButtonMapping(display,dev,*map,nbuttons);
XCloseDevice(display,dev);
XFreeDeviceList(deviceInfos);
return nbuttons;
}
void InputDeviceHelper::setDeviceButtonMap(int device, int nbuttons, unsigned char* map)
{
XDevice* dev = XOpenDevice(display,device);
if (!dev) {
USD_LOG(LOG_WARNING,"open device %d is failed",device);
return;
}
XSetDeviceButtonMapping(display,dev,map,nbuttons);
XCloseDevice(display,dev);
XFree(map);
}
void InputDeviceHelper::changePtrFeedbackControl(int device , int threshold, int numerator, int denominator)
{
XDevice* dev = XOpenDevice(display, device);
if (!dev) {
USD_LOG(LOG_WARNING,"open device %d is failed",device);
return;
}
int nFeedbacks;
XFeedbackState *state = XGetFeedbackControl(display, dev, &nFeedbacks);
if (!state) {
USD_LOG(LOG_WARNING,"get feedback states failed .");
return;
}
int id = -1;
for (int i = 0; i < nFeedbacks; ++i) {
if (state->c_class == PtrFeedbackClass) {
id = state->id;
break;
}
state = reinterpret_cast<XFeedbackState *>((reinterpret_cast<char *>(state) + state->length));
}
XFreeFeedbackList(state);
if (-1 == id) {
USD_LOG(LOG_WARNING,"unable find ptrfeedback .");
return ;
}
XPtrFeedbackControl feedback;
feedback.c_class = PtrFeedbackClass;
feedback.length = sizeof(XPtrFeedbackControl);
feedback.id = id;
feedback.threshold = threshold;
feedback.accelNum = numerator;
feedback.accelDenom = denominator;
XChangeFeedbackControl(display, dev, DvAccelNum | DvAccelDenom | DvThreshold, (XFeedbackControl *)&feedback);
}
//禁用设备
void InputDeviceHelper::disable(int device)
{
Atom prop = properyToAtom("Device Enabled");
QVariantList list;
list << false;
setDeviceProp(device, prop, list);
}
//启用设备
void InputDeviceHelper::enabel(int device)
{
Atom prop = properyToAtom("Device Enabled");
QVariantList list;
list << true;
setDeviceProp(device, prop, list);
}
|