Sfoglia il codice sorgente

device: various warning fixes for input-overhaul on Linux

rdb 7 anni fa
parent
commit
11723b27ee

+ 12 - 12
panda/src/device/evdevInputDevice.cxx

@@ -96,7 +96,7 @@ TypeHandle EvdevInputDevice::_type_handle;
  * Creates a new device representing the evdev device with the given index.
  */
 EvdevInputDevice::
-EvdevInputDevice(LinuxInputDeviceManager *manager, int index) :
+EvdevInputDevice(LinuxInputDeviceManager *manager, size_t index) :
   _manager(manager),
   _index(index),
   _fd(-1),
@@ -113,7 +113,7 @@ EvdevInputDevice(LinuxInputDeviceManager *manager, int index) :
   _rtrigger_code(-1) {
 
   char path[64];
-  sprintf(path, "/dev/input/event%d", index);
+  sprintf(path, "/dev/input/event%zd", index);
 
   _fd = open(path, O_RDWR | O_NONBLOCK);
   if (_fd >= 0) {
@@ -338,8 +338,8 @@ init_device() {
 
     // Test for specific name tags
     string lowercase_name = _name;
-    for(int x=0; x<_name.length(); x++) {
-      lowercase_name[x]=tolower(lowercase_name[x]);
+    for(size_t x = 0; x < _name.length(); ++x) {
+      lowercase_name[x] = tolower(lowercase_name[x]);
     }
     if (lowercase_name.find("gamepad") != string::npos) {
       device_scores[DC_gamepad] += 10;
@@ -362,7 +362,7 @@ init_device() {
     }
 
     // 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++) {
       if (device_scores[i] > highest_score) {
         highest_score = device_scores[i];
@@ -406,7 +406,7 @@ init_device() {
         }
 
         _buttons.push_back(button);
-        if (i >= _button_indices.size()) {
+        if ((size_t)i >= _button_indices.size()) {
           _button_indices.resize(i + 1, -1);
         }
         _button_indices[i] = button_index;
@@ -594,11 +594,11 @@ init_device() {
   char path[64];
   char buffer[256];
   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");
   if (!f) {
     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");
   }
   if (f) {
@@ -610,7 +610,7 @@ init_device() {
     }
     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");
   if (f) {
     if (fgets(buffer, sizeof(buffer), f) != nullptr) {
@@ -619,7 +619,7 @@ init_device() {
     }
     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");
   if (f) {
     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+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];
       if (index >= 0) {
         control_changed(index, events[i].value);
@@ -717,7 +717,7 @@ process_events() {
       break;
 
     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];
       if (index >= 0) {
         button_changed(index, events[i].value != 0);

+ 2 - 2
panda/src/device/evdevInputDevice.h

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

+ 16 - 17
panda/src/device/linuxInputDeviceManager.cxx

@@ -48,11 +48,11 @@ LinuxInputDeviceManager() {
   // Scan /dev/input for a list of input devices.
   DIR *dir = opendir("/dev/input");
   if (dir) {
-    vector_int indices;
+    std::vector<size_t> indices;
     dirent *entry = readdir(dir);
     while (entry != nullptr) {
-      int index = -1;
-      if (entry->d_type == DT_CHR && sscanf(entry->d_name, "event%d", &index) == 1) {
+      size_t index;
+      if (entry->d_type == DT_CHR && sscanf(entry->d_name, "event%zd", &index) == 1) {
         indices.push_back(index);
       }
       entry = readdir(dir);
@@ -64,9 +64,8 @@ LinuxInputDeviceManager() {
     std::sort(indices.begin(), indices.end());
     _evdev_devices.resize(indices.back() + 1, nullptr);
 
-    vector_int::const_iterator it;
-    for (it = indices.begin(); it != indices.end(); ++it) {
-      consider_add_evdev_device(*it);
+    for (size_t index : indices) {
+      consider_add_evdev_device(index);
     }
   } else {
     device_cat.error()
@@ -93,7 +92,7 @@ LinuxInputDeviceManager::
  * This is the preferred interface for input devices on Linux.
  */
 InputDevice *LinuxInputDeviceManager::
-consider_add_evdev_device(int ev_index) {
+consider_add_evdev_device(size_t ev_index) {
   if (ev_index < _evdev_devices.size()) {
     if (_evdev_devices[ev_index] != nullptr) {
       // 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.
   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) {
     PT(InputDevice) device = new EvdevInputDevice(this, ev_index);
@@ -133,7 +132,7 @@ consider_add_evdev_device(int ev_index) {
   // (notably, force feedback).
 
   // 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);
   if (dir == nullptr) {
@@ -146,8 +145,8 @@ consider_add_evdev_device(int ev_index) {
 
   dirent *entry = readdir(dir);
   while (entry != nullptr) {
-    int js_index = -1;
-    if (sscanf(entry->d_name, "js%d", &js_index) == 1) {
+    size_t js_index;
+    if (sscanf(entry->d_name, "js%zd", &js_index) == 1) {
       // Yes, we fonud a corresponding js device.  Try adding it.
       closedir(dir);
 
@@ -177,9 +176,9 @@ consider_add_evdev_device(int ev_index) {
  * cannot be accessed.
  */
 InputDevice *LinuxInputDeviceManager::
-consider_add_js_device(int js_index) {
+consider_add_js_device(size_t js_index) {
   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) {
     PT(LinuxJoystickDevice) device = new LinuxJoystickDevice(this, js_index);
@@ -254,8 +253,8 @@ update() {
     if (event->mask & IN_DELETE) {
       // The device was deleted.  If we have it, remove it.
 
-      int index = -1;
-      if (sscanf(event->name, "event%d", &index) == 1) {
+      size_t index;
+      if (sscanf(event->name, "event%zd", &index) == 1) {
         // Check if we have this evdev device.  If so, disconnect it.
         if (index < _evdev_devices.size()) {
           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
       // IN_CREATE event before the driver gets the permissions set properly.
 
-      int index = -1;
-      if (sscanf(event->name, "event%d", &index) == 1) {
+      size_t index;
+      if (sscanf(event->name, "event%zd", &index) == 1) {
         InputDevice *device = consider_add_evdev_device(index);
         if (device != nullptr && device->is_connected()) {
           throw_event("connect-device", device);

+ 2 - 2
panda/src/device/linuxInputDeviceManager.h

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

+ 9 - 9
panda/src/device/linuxJoystickDevice.cxx

@@ -28,7 +28,7 @@ TypeHandle LinuxJoystickDevice::_type_handle;
  * Creates a new device using the Linux joystick device with the given index.
  */
 LinuxJoystickDevice::
-LinuxJoystickDevice(LinuxInputDeviceManager *manager, int index) :
+LinuxJoystickDevice(LinuxInputDeviceManager *manager, size_t index) :
   _manager(manager),
   _fd(-1),
   _index(index),
@@ -97,7 +97,7 @@ open_device() {
   nassertr(_lock.debug_is_locked(), false);
 
   char path[64];
-  sprintf(path, "/dev/input/js%d", _index);
+  sprintf(path, "/dev/input/js%zd", _index);
 
   _fd = open(path, O_RDONLY | O_NONBLOCK);
 
@@ -287,7 +287,7 @@ open_device() {
   }
 
   // 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");
   if (f) {
     if (fscanf(f, "%hx", &_vendor_id) < 1) {
@@ -295,7 +295,7 @@ open_device() {
     }
     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");
   if (f) {
     if (fscanf(f, "%hx", &_product_id) < 1) {
@@ -304,7 +304,7 @@ open_device() {
     fclose(f);
   }
   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");
   if (f) {
     if (fgets(buffer, sizeof(buffer), f) != nullptr) {
@@ -315,7 +315,7 @@ open_device() {
     }
     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");
   if (f) {
     if (fgets(buffer, sizeof(buffer), f) != nullptr) {
@@ -324,7 +324,7 @@ open_device() {
     }
     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");
   if (f) {
     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
   // this gamepad yet (which means it hasn't been plugged in for this session)
   if (strncmp(name, "Xbox 360 Wireless Receiver", 26) == 0) {
-    for (int i = 0; i < _controls.size(); ++i) {
-      if (_controls[i].state != 0.0) {
+    for (const auto &control : _controls) {
+      if (control.state != 0.0) {
         _is_connected = true;
         return true;
       }

+ 2 - 2
panda/src/device/linuxJoystickDevice.h

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