shithub: m8c

Download patch

ref: ae22bca6100884fabcf28d67ff34d7b5aada7654
parent: de923fd0d773a575d3bff75600b0b0f0673f8790
author: Jonne Kokkonen <jonne.kokkonen@gmail.com>
date: Wed Apr 9 08:02:23 EDT 2025

serialport backend: double check device disconnection if no data received

--- a/src/backends/m8_libserialport.c
+++ b/src/backends/m8_libserialport.c
@@ -81,6 +81,33 @@
   return 0;
 }
 
+// Checks for connected devices and whether the specified device still exists
+static int serial_port_connected() {
+
+  int device_found = 0;
+
+  struct sp_port **port_list;
+
+  const enum sp_return result = sp_list_ports(&port_list);
+
+  if (result != SP_OK) {
+    SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "sp_list_ports() failed!\n");
+    abort();
+  }
+
+  for (int i = 0; port_list[i] != NULL; i++) {
+    const struct sp_port *port = port_list[i];
+
+    if (detect_m8_serial_device(port)) {
+      if (strcmp(sp_get_port_name(port), sp_get_port_name(m8_port)) == 0)
+        device_found = 1;
+    }
+  }
+
+  sp_free_port_list(port_list);
+  return device_found;
+}
+
 static void process_received_bytes(const uint8_t *buffer, int bytes_read, slip_handler_s *slip) {
   const uint8_t *cur = buffer;
   const uint8_t *end = buffer + bytes_read;
@@ -350,6 +377,11 @@
   } else {
     empty_cycles++;
     if (empty_cycles >= conf->wait_packets) {
+      // try opening the serial port to check if it's alive
+      if (serial_port_connected()) {
+        // the device is still there, carry on
+        return DEVICE_PROCESSING;
+      }
       SDL_LogError(SDL_LOG_CATEGORY_SYSTEM,
                    "No messages received for %d cycles, assuming device disconnected",
                    empty_cycles);
--- a/src/input.c
+++ b/src/input.c
@@ -166,7 +166,6 @@
 static void handle_sdl_events(config_params_s *conf) {
 
   static int prev_key_analog = 0;
-  static unsigned int ticks_window_resized = 0;
 
   SDL_Event event;
 
--