device: various warning fixes for input-overhaul on Linux

This commit is contained in:
rdb 2018-06-23 23:30:44 +02:00
parent 234fd675b1
commit 11723b27ee
6 changed files with 43 additions and 44 deletions

View File

@ -96,7 +96,7 @@ TypeHandle EvdevInputDevice::_type_handle;
* Creates a new device representing the evdev device with the given index. * Creates a new device representing the evdev device with the given index.
*/ */
EvdevInputDevice:: EvdevInputDevice::
EvdevInputDevice(LinuxInputDeviceManager *manager, int index) : EvdevInputDevice(LinuxInputDeviceManager *manager, size_t index) :
_manager(manager), _manager(manager),
_index(index), _index(index),
_fd(-1), _fd(-1),
@ -113,7 +113,7 @@ EvdevInputDevice(LinuxInputDeviceManager *manager, int index) :
_rtrigger_code(-1) { _rtrigger_code(-1) {
char path[64]; char path[64];
sprintf(path, "/dev/input/event%d", index); sprintf(path, "/dev/input/event%zd", index);
_fd = open(path, O_RDWR | O_NONBLOCK); _fd = open(path, O_RDWR | O_NONBLOCK);
if (_fd >= 0) { if (_fd >= 0) {
@ -338,7 +338,7 @@ init_device() {
// Test for specific name tags // Test for specific name tags
string lowercase_name = _name; string lowercase_name = _name;
for(int x=0; x<_name.length(); x++) { for(size_t x = 0; x < _name.length(); ++x) {
lowercase_name[x] = tolower(lowercase_name[x]); lowercase_name[x] = tolower(lowercase_name[x]);
} }
if (lowercase_name.find("gamepad") != string::npos) { if (lowercase_name.find("gamepad") != string::npos) {
@ -362,7 +362,7 @@ init_device() {
} }
// Check which device type got the most points // Check which device type got the most points
size_t highest_score = 0; int highest_score = 0;
for (size_t i = 0; i < DC_COUNT; i++) { for (size_t i = 0; i < DC_COUNT; i++) {
if (device_scores[i] > highest_score) { if (device_scores[i] > highest_score) {
highest_score = device_scores[i]; highest_score = device_scores[i];
@ -406,7 +406,7 @@ init_device() {
} }
_buttons.push_back(button); _buttons.push_back(button);
if (i >= _button_indices.size()) { if ((size_t)i >= _button_indices.size()) {
_button_indices.resize(i + 1, -1); _button_indices.resize(i + 1, -1);
} }
_button_indices[i] = button_index; _button_indices[i] = button_index;
@ -594,11 +594,11 @@ init_device() {
char path[64]; char path[64];
char buffer[256]; char buffer[256];
const char *parent = ""; const char *parent = "";
sprintf(path, "/sys/class/input/event%d/device/device/../product", _index); sprintf(path, "/sys/class/input/event%zd/device/device/../product", _index);
FILE *f = fopen(path, "r"); FILE *f = fopen(path, "r");
if (!f) { if (!f) {
parent = "../"; parent = "../";
sprintf(path, "/sys/class/input/event%d/device/device/%s../product", _index, parent); sprintf(path, "/sys/class/input/event%zd/device/device/%s../product", _index, parent);
f = fopen(path, "r"); f = fopen(path, "r");
} }
if (f) { if (f) {
@ -610,7 +610,7 @@ init_device() {
} }
fclose(f); fclose(f);
} }
sprintf(path, "/sys/class/input/event%d/device/device/%s../manufacturer", _index, parent); sprintf(path, "/sys/class/input/event%zd/device/device/%s../manufacturer", _index, parent);
f = fopen(path, "r"); f = fopen(path, "r");
if (f) { if (f) {
if (fgets(buffer, sizeof(buffer), f) != nullptr) { if (fgets(buffer, sizeof(buffer), f) != nullptr) {
@ -619,7 +619,7 @@ init_device() {
} }
fclose(f); fclose(f);
} }
sprintf(path, "/sys/class/input/event%d/device/device/%s../serial", _index, parent); sprintf(path, "/sys/class/input/event%zd/device/device/%s../serial", _index, parent);
f = fopen(path, "r"); f = fopen(path, "r");
if (f) { if (f) {
if (fgets(buffer, sizeof(buffer), f) != nullptr) { if (fgets(buffer, sizeof(buffer), f) != nullptr) {
@ -709,7 +709,7 @@ process_events() {
button_changed(_dpad_up_button, events[i].value < 0); button_changed(_dpad_up_button, events[i].value < 0);
button_changed(_dpad_up_button+1, events[i].value > 0); button_changed(_dpad_up_button+1, events[i].value > 0);
} }
nassertd(code >= 0 && code < _control_indices.size()) break; nassertd(code >= 0 && (size_t)code < _control_indices.size()) break;
index = _control_indices[code]; index = _control_indices[code];
if (index >= 0) { if (index >= 0) {
control_changed(index, events[i].value); control_changed(index, events[i].value);
@ -717,7 +717,7 @@ process_events() {
break; break;
case EV_KEY: case EV_KEY:
nassertd(code >= 0 && code < _button_indices.size()) break; nassertd(code >= 0 && (size_t)code < _button_indices.size()) break;
index = _button_indices[code]; index = _button_indices[code];
if (index >= 0) { if (index >= 0) {
button_changed(index, events[i].value != 0); button_changed(index, events[i].value != 0);

View File

@ -27,7 +27,7 @@ class LinuxInputDeviceManager;
*/ */
class EXPCL_PANDA_DEVICE EvdevInputDevice : public InputDevice { class EXPCL_PANDA_DEVICE EvdevInputDevice : public InputDevice {
public: public:
EvdevInputDevice(LinuxInputDeviceManager *manager, int index); EvdevInputDevice(LinuxInputDeviceManager *manager, size_t index);
virtual ~EvdevInputDevice(); virtual ~EvdevInputDevice();
private: private:
@ -41,7 +41,7 @@ private:
LinuxInputDeviceManager *_manager; LinuxInputDeviceManager *_manager;
int _fd; int _fd;
int _index; size_t _index;
bool _can_write; bool _can_write;
int _ff_id; int _ff_id;

View File

@ -48,11 +48,11 @@ LinuxInputDeviceManager() {
// Scan /dev/input for a list of input devices. // Scan /dev/input for a list of input devices.
DIR *dir = opendir("/dev/input"); DIR *dir = opendir("/dev/input");
if (dir) { if (dir) {
vector_int indices; std::vector<size_t> indices;
dirent *entry = readdir(dir); dirent *entry = readdir(dir);
while (entry != nullptr) { while (entry != nullptr) {
int index = -1; size_t index;
if (entry->d_type == DT_CHR && sscanf(entry->d_name, "event%d", &index) == 1) { if (entry->d_type == DT_CHR && sscanf(entry->d_name, "event%zd", &index) == 1) {
indices.push_back(index); indices.push_back(index);
} }
entry = readdir(dir); entry = readdir(dir);
@ -64,9 +64,8 @@ LinuxInputDeviceManager() {
std::sort(indices.begin(), indices.end()); std::sort(indices.begin(), indices.end());
_evdev_devices.resize(indices.back() + 1, nullptr); _evdev_devices.resize(indices.back() + 1, nullptr);
vector_int::const_iterator it; for (size_t index : indices) {
for (it = indices.begin(); it != indices.end(); ++it) { consider_add_evdev_device(index);
consider_add_evdev_device(*it);
} }
} else { } else {
device_cat.error() device_cat.error()
@ -93,7 +92,7 @@ LinuxInputDeviceManager::
* This is the preferred interface for input devices on Linux. * This is the preferred interface for input devices on Linux.
*/ */
InputDevice *LinuxInputDeviceManager:: InputDevice *LinuxInputDeviceManager::
consider_add_evdev_device(int ev_index) { consider_add_evdev_device(size_t ev_index) {
if (ev_index < _evdev_devices.size()) { if (ev_index < _evdev_devices.size()) {
if (_evdev_devices[ev_index] != nullptr) { if (_evdev_devices[ev_index] != nullptr) {
// We already have this device. FIXME: probe it and add it to the // We already have this device. FIXME: probe it and add it to the
@ -107,7 +106,7 @@ consider_add_evdev_device(int ev_index) {
// Check if we can directly read the event device. // Check if we can directly read the event device.
char path[64]; char path[64];
sprintf(path, "/dev/input/event%d", ev_index); sprintf(path, "/dev/input/event%zd", ev_index);
if (access(path, R_OK) == 0) { if (access(path, R_OK) == 0) {
PT(InputDevice) device = new EvdevInputDevice(this, ev_index); PT(InputDevice) device = new EvdevInputDevice(this, ev_index);
@ -133,7 +132,7 @@ consider_add_evdev_device(int ev_index) {
// (notably, force feedback). // (notably, force feedback).
// We do this by checking for a js# directory inside the sysfs directory. // We do this by checking for a js# directory inside the sysfs directory.
sprintf(path, "/sys/class/input/event%d/device", ev_index); sprintf(path, "/sys/class/input/event%zd/device", ev_index);
DIR *dir = opendir(path); DIR *dir = opendir(path);
if (dir == nullptr) { if (dir == nullptr) {
@ -146,8 +145,8 @@ consider_add_evdev_device(int ev_index) {
dirent *entry = readdir(dir); dirent *entry = readdir(dir);
while (entry != nullptr) { while (entry != nullptr) {
int js_index = -1; size_t js_index;
if (sscanf(entry->d_name, "js%d", &js_index) == 1) { if (sscanf(entry->d_name, "js%zd", &js_index) == 1) {
// Yes, we fonud a corresponding js device. Try adding it. // Yes, we fonud a corresponding js device. Try adding it.
closedir(dir); closedir(dir);
@ -177,9 +176,9 @@ consider_add_evdev_device(int ev_index) {
* cannot be accessed. * cannot be accessed.
*/ */
InputDevice *LinuxInputDeviceManager:: InputDevice *LinuxInputDeviceManager::
consider_add_js_device(int js_index) { consider_add_js_device(size_t js_index) {
char path[64]; char path[64];
sprintf(path, "/dev/input/js%d", js_index); sprintf(path, "/dev/input/js%zd", js_index);
if (access(path, R_OK) == 0) { if (access(path, R_OK) == 0) {
PT(LinuxJoystickDevice) device = new LinuxJoystickDevice(this, js_index); PT(LinuxJoystickDevice) device = new LinuxJoystickDevice(this, js_index);
@ -254,8 +253,8 @@ update() {
if (event->mask & IN_DELETE) { if (event->mask & IN_DELETE) {
// The device was deleted. If we have it, remove it. // The device was deleted. If we have it, remove it.
int index = -1; size_t index;
if (sscanf(event->name, "event%d", &index) == 1) { if (sscanf(event->name, "event%zd", &index) == 1) {
// Check if we have this evdev device. If so, disconnect it. // Check if we have this evdev device. If so, disconnect it.
if (index < _evdev_devices.size()) { if (index < _evdev_devices.size()) {
PT(InputDevice) device = _evdev_devices[index]; PT(InputDevice) device = _evdev_devices[index];
@ -280,8 +279,8 @@ update() {
// to check for the latter since it seems that the device may get the // to check for the latter since it seems that the device may get the
// IN_CREATE event before the driver gets the permissions set properly. // IN_CREATE event before the driver gets the permissions set properly.
int index = -1; size_t index;
if (sscanf(event->name, "event%d", &index) == 1) { if (sscanf(event->name, "event%zd", &index) == 1) {
InputDevice *device = consider_add_evdev_device(index); InputDevice *device = consider_add_evdev_device(index);
if (device != nullptr && device->is_connected()) { if (device != nullptr && device->is_connected()) {
throw_event("connect-device", device); throw_event("connect-device", device);

View File

@ -27,8 +27,8 @@ private:
LinuxInputDeviceManager(); LinuxInputDeviceManager();
~LinuxInputDeviceManager(); ~LinuxInputDeviceManager();
InputDevice *consider_add_evdev_device(int index); InputDevice *consider_add_evdev_device(size_t index);
InputDevice *consider_add_js_device(int index); InputDevice *consider_add_js_device(size_t index);
virtual void update(); virtual void update();

View File

@ -28,7 +28,7 @@ TypeHandle LinuxJoystickDevice::_type_handle;
* Creates a new device using the Linux joystick device with the given index. * Creates a new device using the Linux joystick device with the given index.
*/ */
LinuxJoystickDevice:: LinuxJoystickDevice::
LinuxJoystickDevice(LinuxInputDeviceManager *manager, int index) : LinuxJoystickDevice(LinuxInputDeviceManager *manager, size_t index) :
_manager(manager), _manager(manager),
_fd(-1), _fd(-1),
_index(index), _index(index),
@ -97,7 +97,7 @@ open_device() {
nassertr(_lock.debug_is_locked(), false); nassertr(_lock.debug_is_locked(), false);
char path[64]; char path[64];
sprintf(path, "/dev/input/js%d", _index); sprintf(path, "/dev/input/js%zd", _index);
_fd = open(path, O_RDONLY | O_NONBLOCK); _fd = open(path, O_RDONLY | O_NONBLOCK);
@ -287,7 +287,7 @@ open_device() {
} }
// Get additional information from sysfs. // Get additional information from sysfs.
sprintf(path, "/sys/class/input/js%d/device/id/vendor", _index); sprintf(path, "/sys/class/input/js%zd/device/id/vendor", _index);
FILE *f = fopen(path, "r"); FILE *f = fopen(path, "r");
if (f) { if (f) {
if (fscanf(f, "%hx", &_vendor_id) < 1) { if (fscanf(f, "%hx", &_vendor_id) < 1) {
@ -295,7 +295,7 @@ open_device() {
} }
fclose(f); fclose(f);
} }
sprintf(path, "/sys/class/input/js%d/device/id/product", _index); sprintf(path, "/sys/class/input/js%zd/device/id/product", _index);
f = fopen(path, "r"); f = fopen(path, "r");
if (f) { if (f) {
if (fscanf(f, "%hx", &_product_id) < 1) { if (fscanf(f, "%hx", &_product_id) < 1) {
@ -304,7 +304,7 @@ open_device() {
fclose(f); fclose(f);
} }
char buffer[256]; char buffer[256];
sprintf(path, "/sys/class/input/js%d/device/device/../product", _index); sprintf(path, "/sys/class/input/js%zd/device/device/../product", _index);
f = fopen(path, "r"); f = fopen(path, "r");
if (f) { if (f) {
if (fgets(buffer, sizeof(buffer), f) != nullptr) { if (fgets(buffer, sizeof(buffer), f) != nullptr) {
@ -315,7 +315,7 @@ open_device() {
} }
fclose(f); fclose(f);
} }
sprintf(path, "/sys/class/input/js%d/device/device/../manufacturer", _index); sprintf(path, "/sys/class/input/js%zd/device/device/../manufacturer", _index);
f = fopen(path, "r"); f = fopen(path, "r");
if (f) { if (f) {
if (fgets(buffer, sizeof(buffer), f) != nullptr) { if (fgets(buffer, sizeof(buffer), f) != nullptr) {
@ -324,7 +324,7 @@ open_device() {
} }
fclose(f); fclose(f);
} }
sprintf(path, "/sys/class/input/js%d/device/device/../serial", _index); sprintf(path, "/sys/class/input/js%zd/device/device/../serial", _index);
f = fopen(path, "r"); f = fopen(path, "r");
if (f) { if (f) {
if (fgets(buffer, sizeof(buffer), f) != nullptr) { if (fgets(buffer, sizeof(buffer), f) != nullptr) {
@ -343,8 +343,8 @@ open_device() {
// are all 0, which indicates that the driver hasn't received any data for // are all 0, which indicates that the driver hasn't received any data for
// this gamepad yet (which means it hasn't been plugged in for this session) // this gamepad yet (which means it hasn't been plugged in for this session)
if (strncmp(name, "Xbox 360 Wireless Receiver", 26) == 0) { if (strncmp(name, "Xbox 360 Wireless Receiver", 26) == 0) {
for (int i = 0; i < _controls.size(); ++i) { for (const auto &control : _controls) {
if (_controls[i].state != 0.0) { if (control.state != 0.0) {
_is_connected = true; _is_connected = true;
return true; return true;
} }

View File

@ -26,7 +26,7 @@ class LinuxInputDeviceManager;
*/ */
class EXPCL_PANDA_DEVICE LinuxJoystickDevice : public InputDevice { class EXPCL_PANDA_DEVICE LinuxJoystickDevice : public InputDevice {
PUBLISHED: PUBLISHED:
LinuxJoystickDevice(LinuxInputDeviceManager *manager, int index); LinuxJoystickDevice(LinuxInputDeviceManager *manager, size_t index);
virtual ~LinuxJoystickDevice(); virtual ~LinuxJoystickDevice();
bool check_events() const; bool check_events() const;
@ -41,7 +41,7 @@ private:
LinuxInputDeviceManager *_manager; LinuxInputDeviceManager *_manager;
int _fd; int _fd;
int _index; size_t _index;
// These are used for D-pad / hat switch emulation. // These are used for D-pad / hat switch emulation.
int _dpad_x_axis; int _dpad_x_axis;