shithub: m8c

Download patch

ref: 1b0b3e911bb9a19c0c1500fb5a0154395c41fff7
parent: 621a093f2391ca8c8c2fc63eadee31dd2efa584c
parent: 173be3c5f566ecbc1dca69f5e953ebe3ca9878ff
author: Jonne Kokkonen <jonne.kokkonen@gmail.com>
date: Sat Aug 24 19:52:06 EDT 2024

Merge pull request #165 from laamaa/refactor/code_cleanup

Refactor/code cleanup

--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,6 +2,8 @@
 
 project(m8c LANGUAGES C)
 
+set(CMAKE_C_FLAGS "-O2 -Wall -Wextra")
+
 set(APP_NAME m8c)
 
 find_package(PkgConfig REQUIRED)
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@
 INCLUDES = $(shell pkg-config --libs sdl2 libserialport | sed 's/-mwindows//')
 
 #Set any compiler flags you want to use (e.g. -I/usr/include/somefolder `pkg-config --cflags gtk+-3.0` ), or leave blank
-local_CFLAGS = $(CFLAGS) $(shell pkg-config --cflags sdl2 libserialport) -Wall -O2 -pipe -I.
+local_CFLAGS = $(CFLAGS) $(shell pkg-config --cflags sdl2 libserialport) -Wall -Wextra -O2 -pipe -I.
 
 #Set the compiler you are using ( gcc for C or g++ for C++ )
 CC = gcc
--- a/src/SDL2_inprint.h
+++ b/src/SDL2_inprint.h
@@ -12,8 +12,8 @@
 
 extern void inrenderer(SDL_Renderer *renderer);
 extern void infont(SDL_Texture *font);
-extern void incolor1(SDL_Color *color);
-extern void incolor(Uint32 color, Uint32 unused); /* Color must be in 0x00RRGGBB format ! */
+extern void incolor1(const SDL_Color *color);
+extern void incolor(Uint32 color); /* Color must be in 0x00RRGGBB format ! */
 extern void inprint(SDL_Renderer *dst, const char *str, Uint32 x, Uint32 y, Uint32 fgcolor,
                     Uint32 bgcolor);
 
--- a/src/audio.c
+++ b/src/audio.c
@@ -11,7 +11,7 @@
 static unsigned int audio_paused = 0;
 static unsigned int audio_initialized = 0;
 
-void toggle_audio(unsigned int audio_buffer_size, const char *output_device_name) {
+void toggle_audio(const unsigned int audio_buffer_size, const char *output_device_name) {
   if (!audio_initialized) {
     audio_init(audio_buffer_size, output_device_name);
     return;
@@ -22,66 +22,63 @@
   SDL_Log(audio_paused ? "Audio paused" : "Audio resumed");
 }
 
-void audio_cb_in(void *userdata, uint8_t *stream, int len) {
+void audio_cb_in(void *, uint8_t *stream, int len) {
   SDL_QueueAudio(devid_out, stream, len);
 }
 
-int audio_init(unsigned int audio_buffer_size, const char *output_device_name) {
+int audio_init(const unsigned int audio_buffer_size, const char *output_device_name) {
 
-  int i = 0;
   int m8_device_id = -1;
-  int devcount_in = 0; // audio input device count
 
   // wait for system to initialize possible new audio devices
   SDL_Delay(500);
 
-  devcount_in = SDL_GetNumAudioDevices(SDL_TRUE);
+  const int devcount_in = SDL_GetNumAudioDevices(SDL_TRUE);
 
   if (devcount_in < 1) {
     SDL_Log("No audio capture devices, SDL Error: %s", SDL_GetError());
     return 0;
-  } else {
-    for (i = 0; i < devcount_in; i++) {
-      // Check if input device exists before doing anything else
-      SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "%s", SDL_GetAudioDeviceName(i, SDL_TRUE));
-      if (SDL_strstr(SDL_GetAudioDeviceName(i, SDL_TRUE), "M8") != NULL) {
-        SDL_Log("M8 Audio Input device found: %s", SDL_GetAudioDeviceName(i, SDL_TRUE));
-        m8_device_id = i;
-      }
+  }
+  for (int i = 0; i < devcount_in; i++) {
+    // Check if input device exists before doing anything else
+    SDL_LogDebug(SDL_LOG_CATEGORY_AUDIO, "%s", SDL_GetAudioDeviceName(i, SDL_TRUE));
+    if (SDL_strstr(SDL_GetAudioDeviceName(i, SDL_TRUE), "M8") != NULL) {
+      SDL_Log("M8 Audio Input device found: %s", SDL_GetAudioDeviceName(i, SDL_TRUE));
+      m8_device_id = i;
     }
-    if (m8_device_id == -1) {
-      // forget about it
-      SDL_Log("Cannot find M8 audio input device");
-      return 0;
-    }
+  }
+  if (m8_device_id == -1) {
+    // forget about it
+    SDL_Log("Cannot find M8 audio input device");
+    return 0;
+  }
 
-    SDL_AudioSpec want_in, have_in, want_out, have_out;
+  SDL_AudioSpec want_in, have_in, want_out, have_out;
 
-    // Open output device first to avoid possible Directsound errors
-    SDL_zero(want_out);
-    want_out.freq = 44100;
-    want_out.format = AUDIO_S16;
-    want_out.channels = 2;
-    want_out.samples = audio_buffer_size;
-    devid_out = SDL_OpenAudioDevice(output_device_name, 0, &want_out, &have_out,
-                                    SDL_AUDIO_ALLOW_ANY_CHANGE);
-    if (devid_out == 0) {
-      SDL_Log("Failed to open output: %s", SDL_GetError());
-      return 0;
-    }
+  // Open output device first to avoid possible Directsound errors
+  SDL_zero(want_out);
+  want_out.freq = 44100;
+  want_out.format = AUDIO_S16;
+  want_out.channels = 2;
+  want_out.samples = audio_buffer_size;
+  devid_out =
+      SDL_OpenAudioDevice(output_device_name, 0, &want_out, &have_out, SDL_AUDIO_ALLOW_ANY_CHANGE);
+  if (devid_out == 0) {
+    SDL_Log("Failed to open output: %s", SDL_GetError());
+    return 0;
+  }
 
-    SDL_zero(want_in);
-    want_in.freq = 44100;
-    want_in.format = AUDIO_S16;
-    want_in.channels = 2;
-    want_in.samples = audio_buffer_size;
-    want_in.callback = audio_cb_in;
-    devid_in = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(m8_device_id, SDL_TRUE), SDL_TRUE,
-                                   &want_in, &have_in, SDL_AUDIO_ALLOW_ANY_CHANGE);
-    if (devid_in == 0) {
-      SDL_Log("Failed to open M8 audio device, SDL Error: %s", SDL_GetError());
-      return 0;
-    }
+  SDL_zero(want_in);
+  want_in.freq = 44100;
+  want_in.format = AUDIO_S16;
+  want_in.channels = 2;
+  want_in.samples = audio_buffer_size;
+  want_in.callback = audio_cb_in;
+  devid_in = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(m8_device_id, SDL_TRUE), SDL_TRUE, &want_in,
+                                 &have_in, SDL_AUDIO_ALLOW_ANY_CHANGE);
+  if (devid_in == 0) {
+    SDL_Log("Failed to open M8 audio device, SDL Error: %s", SDL_GetError());
+    return 0;
   }
 
   // Start audio processing
--- a/src/command.c
+++ b/src/command.c
@@ -7,7 +7,7 @@
 #include "render.h"
 
 // Convert 2 little-endian 8bit bytes to a 16bit integer
-static uint16_t decodeInt16(uint8_t *data, uint8_t start) {
+static uint16_t decodeInt16(const uint8_t *data, const uint8_t start) {
   return data[start] | (uint16_t)data[start + 1] << 8;
 }
 
@@ -26,7 +26,7 @@
   system_info_command_datalength = 6
 };
 
-static inline void dump_packet(uint32_t size, uint8_t *recv_buf) {
+static void dump_packet(const uint32_t size, const uint8_t *recv_buf) {
   for (uint16_t a = 0; a < size; a++) {
     SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "0x%02X ", recv_buf[a]);
   }
@@ -42,8 +42,7 @@
 
   switch (recv_buf[0]) {
 
-  case draw_rectangle_command:
-
+  case draw_rectangle_command: {
     if (size < draw_rectangle_command_min_datalength ||
         size > draw_rectangle_command_max_datalength) {
       SDL_LogError(SDL_LOG_CATEGORY_ERROR,
@@ -52,52 +51,47 @@
                    size);
       dump_packet(size, recv_buf);
       return 0;
-      break;
-    } else {
+    }
+    /* Support variable sized rectangle commands
+             If colors are omitted, the last drawn color should be used
+             If size is omitted, the size should be 1x1 pixels
+             So basically the command can be 5, 8, 9 or 12 bytes long */
 
-      /* Support variable sized rectangle commands
-         If colors are omitted, the last drawn color should be used
-         If size is omitted, the size should be 1x1 pixels
-         So basically the command can be 5, 8, 9 or 12 bytes long */
+    static struct draw_rectangle_command rectcmd;
 
-      static struct draw_rectangle_command rectcmd;
+    rectcmd.pos.x = decodeInt16(recv_buf, 1);
+    rectcmd.pos.y = decodeInt16(recv_buf, 3);
 
-      rectcmd.pos.x = decodeInt16(recv_buf, 1);
-      rectcmd.pos.y = decodeInt16(recv_buf, 3);
-
-      switch (size) {
-      case 5:
-        rectcmd.size.width = 1;
-        rectcmd.size.height = 1;
-        break;
-      case 8:
-        rectcmd.size.width = 1;
-        rectcmd.size.height = 1;
-        rectcmd.color.r = recv_buf[5];
-        rectcmd.color.g = recv_buf[6];
-        rectcmd.color.b = recv_buf[7];
-        break;
-      case 9:
-        rectcmd.size.width = decodeInt16(recv_buf, 5);
-        rectcmd.size.height = decodeInt16(recv_buf, 7);
-        break;
-      default:
-        rectcmd.size.width = decodeInt16(recv_buf, 5);
-        rectcmd.size.height = decodeInt16(recv_buf, 7);
-        rectcmd.color.r = recv_buf[9];
-        rectcmd.color.g = recv_buf[10];
-        rectcmd.color.b = recv_buf[11];
-        break;
-      }
-
-      draw_rectangle(&rectcmd);
-      return 1;
+    switch (size) {
+    case 5:
+      rectcmd.size.width = 1;
+      rectcmd.size.height = 1;
+      break;
+    case 8:
+      rectcmd.size.width = 1;
+      rectcmd.size.height = 1;
+      rectcmd.color.r = recv_buf[5];
+      rectcmd.color.g = recv_buf[6];
+      rectcmd.color.b = recv_buf[7];
+      break;
+    case 9:
+      rectcmd.size.width = decodeInt16(recv_buf, 5);
+      rectcmd.size.height = decodeInt16(recv_buf, 7);
+      break;
+    default:
+      rectcmd.size.width = decodeInt16(recv_buf, 5);
+      rectcmd.size.height = decodeInt16(recv_buf, 7);
+      rectcmd.color.r = recv_buf[9];
+      rectcmd.color.g = recv_buf[10];
+      rectcmd.color.b = recv_buf[11];
+      break;
     }
 
-    break;
+    draw_rectangle(&rectcmd);
+    return 1;
+  }
 
-  case draw_character_command:
-
+  case draw_character_command: {
     if (size != draw_character_command_datalength) {
       SDL_LogError(SDL_LOG_CATEGORY_ERROR,
                    "Invalid draw character packet: expected length %d, got %d",
@@ -104,22 +98,17 @@
                    draw_character_command_datalength, size);
       dump_packet(size, recv_buf);
       return 0;
-      break;
-    } else {
-
-      struct draw_character_command charcmd = {
-          recv_buf[1],                                          // char
-          {decodeInt16(recv_buf, 2), decodeInt16(recv_buf, 4)}, // position x/y
-          {recv_buf[6], recv_buf[7], recv_buf[8]},              // foreground r/g/b
-          {recv_buf[9], recv_buf[10], recv_buf[11]}};           // background r/g/b
-      draw_character(&charcmd);
-      return 1;
     }
+    struct draw_character_command charcmd = {
+        recv_buf[1],                                          // char
+        {decodeInt16(recv_buf, 2), decodeInt16(recv_buf, 4)}, // position x/y
+        {recv_buf[6], recv_buf[7], recv_buf[8]},              // foreground r/g/b
+        {recv_buf[9], recv_buf[10], recv_buf[11]}};           // background r/g/b
+    draw_character(&charcmd);
+    return 1;
+  }
 
-    break;
-
-  case draw_oscilloscope_waveform_command:
-
+  case draw_oscilloscope_waveform_command: {
     if (size < draw_oscilloscope_waveform_command_mindatalength ||
         size > draw_oscilloscope_waveform_command_maxdatalength) {
       SDL_LogError(SDL_LOG_CATEGORY_ERROR,
@@ -128,22 +117,18 @@
                    draw_oscilloscope_waveform_command_maxdatalength, size);
       dump_packet(size, recv_buf);
       return 0;
-      break;
-    } else {
+    }
+    struct draw_oscilloscope_waveform_command osccmd;
 
-      struct draw_oscilloscope_waveform_command osccmd;
+    osccmd.color = (struct color){recv_buf[1], recv_buf[2], recv_buf[3]}; // color r/g/b
+    memcpy(osccmd.waveform, &recv_buf[4], size - 4);
 
-      osccmd.color = (struct color){recv_buf[1], recv_buf[2], recv_buf[3]}; // color r/g/b
-      memcpy(osccmd.waveform, &recv_buf[4], size - 4);
+    osccmd.waveform_size = size - 4;
 
-      osccmd.waveform_size = size - 4;
+    draw_waveform(&osccmd);
+    return 1;
+  }
 
-      draw_waveform(&osccmd);
-      return 1;
-    }
-
-    break;
-
   case joypad_keypressedstate_command: {
     if (size != joypad_keypressedstate_command_datalength) {
       SDL_LogError(SDL_LOG_CATEGORY_ERROR,
@@ -152,12 +137,10 @@
                    joypad_keypressedstate_command_datalength, size);
       dump_packet(size, recv_buf);
       return 0;
-      break;
     }
 
     // nothing is done with joypad key pressed packets for now
     return 1;
-    break;
   }
 
   case system_info_command: {
@@ -188,14 +171,12 @@
     set_font_mode(recv_buf[5]);
 
     return 1;
-    break;
   }
 
   default:
-    SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Invalid packet\n");
+    SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Invalid packet");
     dump_packet(size, recv_buf);
     return 0;
-    break;
   }
   return 1;
 }
--- a/src/config.c
+++ b/src/config.c
@@ -7,10 +7,10 @@
 #include <assert.h>
 #include <stdio.h>
 
-/* Case insensitive string compare from ini.h library */
+/* Case-insensitive string compare from ini.h library */
 static int strcmpci(const char *a, const char *b) {
   for (;;) {
-    int d = tolower(*a) - tolower(*b);
+    const int d = tolower(*a) - tolower(*b);
     if (d != 0 || !*a) {
       return d;
     }
@@ -77,7 +77,7 @@
 }
 
 // Write config to file
-void write_config(config_params_s *conf) {
+void write_config(const config_params_s *conf) {
 
   // Open the default config file for writing
   char config_path[1024] = {0};
@@ -165,8 +165,8 @@
 
   if (rw != NULL) {
     // Write ini_values array to config file
-    for (int i = 0; i < INI_LINE_COUNT; i++) {
-      size_t len = SDL_strlen(ini_values[i]);
+    for (unsigned int i = 0; i < INI_LINE_COUNT; i++) {
+      const size_t len = SDL_strlen(ini_values[i]);
       if (SDL_RWwrite(rw, ini_values[i], 1, len) != len) {
         SDL_LogDebug(SDL_LOG_CATEGORY_SYSTEM, "Couldn't write line into config file.");
       } else {
@@ -204,7 +204,7 @@
   write_config(conf);
 }
 
-void read_audio_config(ini_t *ini, config_params_s *conf) {
+void read_audio_config(const ini_t *ini, config_params_s *conf) {
   const char *param_audio_enabled = ini_get(ini, "audio", "audio_enabled");
   const char *param_audio_buffer_size = ini_get(ini, "audio", "audio_buffer_size");
   const char *param_audio_device_name = ini_get(ini, "audio", "audio_device_name");
@@ -226,7 +226,7 @@
   }
 }
 
-void read_graphics_config(ini_t *ini, config_params_s *conf) {
+void read_graphics_config(const ini_t *ini, config_params_s *conf) {
   const char *param_fs = ini_get(ini, "graphics", "fullscreen");
   const char *param_gpu = ini_get(ini, "graphics", "use_gpu");
   const char *idle_ms = ini_get(ini, "graphics", "idle_ms");
@@ -259,7 +259,7 @@
     conf->wait_packets = SDL_atoi(wait_packets);
 }
 
-void read_key_config(ini_t *ini, config_params_s *conf) {
+void read_key_config(const ini_t *ini, config_params_s *conf) {
   // TODO: Some form of validation
 
   const char *key_up = ini_get(ini, "keyboard", "key_up");
@@ -322,7 +322,7 @@
     conf->key_jazz_dec_velocity = SDL_atoi(key_toggle_audio);
 }
 
-void read_gamepad_config(ini_t *ini, config_params_s *conf) {
+void read_gamepad_config(const ini_t *ini, config_params_s *conf) {
   // TODO: Some form of validation
 
   const char *gamepad_up = ini_get(ini, "gamepad", "gamepad_up");
--- a/src/config.h
+++ b/src/config.h
@@ -8,34 +8,34 @@
 
 typedef struct config_params_s {
   char *filename;
-  int init_fullscreen;
-  int init_use_gpu;
-  int idle_ms;
-  int wait_for_device;
-  int wait_packets;
-  int audio_enabled;
-  int audio_buffer_size;
+  unsigned int init_fullscreen;
+  unsigned int init_use_gpu;
+  unsigned int idle_ms;
+  unsigned int wait_for_device;
+  unsigned int wait_packets;
+  unsigned int audio_enabled;
+  unsigned int audio_buffer_size;
   const char *audio_device_name;
 
-  int key_up;
-  int key_left;
-  int key_down;
-  int key_right;
-  int key_select;
-  int key_select_alt;
-  int key_start;
-  int key_start_alt;
-  int key_opt;
-  int key_opt_alt;
-  int key_edit;
-  int key_edit_alt;
-  int key_delete;
-  int key_reset;
-  int key_jazz_inc_octave;
-  int key_jazz_dec_octave;
-  int key_jazz_inc_velocity;
-  int key_jazz_dec_velocity;
-  int key_toggle_audio;
+  unsigned int key_up;
+  unsigned int key_left;
+  unsigned int key_down;
+  unsigned int key_right;
+  unsigned int key_select;
+  unsigned int key_select_alt;
+  unsigned int key_start;
+  unsigned int key_start_alt;
+  unsigned int key_opt;
+  unsigned int key_opt_alt;
+  unsigned int key_edit;
+  unsigned int key_edit_alt;
+  unsigned int key_delete;
+  unsigned int key_reset;
+  unsigned int key_jazz_inc_octave;
+  unsigned int key_jazz_dec_octave;
+  unsigned int key_jazz_inc_velocity;
+  unsigned int key_jazz_dec_velocity;
+  unsigned int key_toggle_audio;
 
   int gamepad_up;
   int gamepad_left;
@@ -62,9 +62,9 @@
 
 config_params_s init_config();
 void read_config(config_params_s *conf);
-void read_audio_config(ini_t *config, config_params_s *conf);
-void read_graphics_config(ini_t *config, config_params_s *conf);
-void read_key_config(ini_t *config, config_params_s *conf);
-void read_gamepad_config(ini_t *config, config_params_s *conf);
+void read_audio_config(const ini_t *ini, config_params_s *conf);
+void read_graphics_config(const ini_t *ini, config_params_s *conf);
+void read_key_config(const ini_t *ini, config_params_s *conf);
+void read_gamepad_config(const ini_t *ini, config_params_s *conf);
 
 #endif
--- a/src/font1.h
+++ b/src/font1.h
@@ -1,3 +1,6 @@
+#ifndef FONT1_H_
+#define FONT1_H_
+
 #include "inline_font.h"
 
 struct inline_font font_v1_small = {
@@ -49,4 +52,5 @@
         0xCE, 0x77, 0x9D, 0xEF, 0xFD, 0xD1, 0xF8, 0x63, 0x08, 0xC5, 0xDE, 0x77, 0x9F, 0xF8, 0xC6,
         0x31, 0x8F, 0xC7, 0x0C, 0x10, 0x08, 0x04, 0x00, 0x10, 0x1C, 0x10, 0x20, 0xA0, 0xC0, 0x00,
         0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x3E, 0x4F, 0x80, 0x00,
-    }};
\ No newline at end of file
+    }};
+#endif // FONT1_H_
\ No newline at end of file
--- a/src/font2.h
+++ b/src/font2.h
@@ -1,3 +1,5 @@
+#ifndef FONT2_H_
+#define FONT2_H_
 #include "inline_font.h"
 
 struct inline_font font_v1_large = {
@@ -80,3 +82,4 @@
         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x9F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
         0xE7, 0x00, 0xFF, 0x00, 0x00,
     }};
+#endif // FONT2_H_
\ No newline at end of file
--- a/src/font3.h
+++ b/src/font3.h
@@ -1,3 +1,6 @@
+#ifndef FONT3_H_
+#define FONT3_H_
+
 #include "inline_font.h"
 
 struct inline_font font_v2_small = {
@@ -87,3 +90,4 @@
         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
         0xFF, 0xC0, 0x0F, 0xF0, 0x07, 0xFC, 0x00, 0x00,
     }};
+#endif // FONT3_H_
\ No newline at end of file
--- a/src/font4.h
+++ b/src/font4.h
@@ -1,3 +1,6 @@
+#ifndef FONT4_H_
+#define FONT4_H_
+
 #include "inline_font.h"
 
 struct inline_font font_v2_large = {
@@ -102,3 +105,4 @@
         0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF,
         0xFF, 0xFF, 0xFF, 0xF0, 0x00, 0xFF, 0x00, 0x3F, 0xF0, 0x00, 0x00,
     }};
+#endif // FONT4_H_
\ No newline at end of file
--- a/src/font5.h
+++ b/src/font5.h
@@ -1,3 +1,6 @@
+#ifndef FONT5_H_
+#define FONT5_H_
+
 #include "inline_font.h"
 
 struct inline_font font_v2_huge = {
@@ -137,3 +140,4 @@
         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x03, 0xFF, 0x00, 0x0F, 0xFF, 0x00, 0x00, 0x00,
     }};
+#endif // FONT5_H_
\ No newline at end of file
--- a/src/fx_cube.c
+++ b/src/fx_cube.c
@@ -1,8 +1,6 @@
-#include "SDL2_inprint.h"
-#include "SDL_pixels.h"
-#include "SDL_render.h"
 #include <SDL.h>
 #include <time.h>
+#include "SDL2_inprint.h"
 
 static SDL_Texture *texture_cube;
 static SDL_Texture *texture_text;
@@ -20,7 +18,7 @@
 
 static float nodes[8][3];
 
-static void scale(float factor0, float factor1, float factor2) {
+static void scale(const float factor0, const float factor1, const float factor2) {
   for (int i = 0; i < 8; i++) {
     nodes[i][0] *= factor0;
     nodes[i][1] *= factor1;
@@ -28,14 +26,14 @@
   }
 }
 
-static void rotate_cube(float angle_x, float angle_y) {
-  float sin_x = SDL_sin(angle_x);
-  float cos_x = SDL_cos(angle_x);
-  float sin_y = SDL_sin(angle_y);
-  float cos_y = SDL_cos(angle_y);
+static void rotate_cube(const float angle_x, const float angle_y) {
+  const float sin_x = SDL_sin(angle_x);
+  const float cos_x = SDL_cos(angle_x);
+  const float sin_y = SDL_sin(angle_y);
+  const float cos_y = SDL_cos(angle_y);
   for (int i = 0; i < 8; i++) {
-    float x = nodes[i][0];
-    float y = nodes[i][1];
+    const float x = nodes[i][0];
+    const float y = nodes[i][1];
     float z = nodes[i][2];
 
     nodes[i][0] = x * cos_x - z * sin_x;
@@ -48,9 +46,9 @@
   }
 }
 
-void fx_cube_init(SDL_Renderer *target_renderer, SDL_Color foreground_color,
-                  unsigned int texture_width, unsigned int texture_height,
-                  unsigned int font_glyph_width) {
+void fx_cube_init(SDL_Renderer *target_renderer, const SDL_Color foreground_color,
+                  const unsigned int texture_width, const unsigned int texture_height,
+                  const unsigned int font_glyph_width) {
 
   fx_renderer = target_renderer;
   line_color = foreground_color;
@@ -102,15 +100,15 @@
   SDL_SetRenderDrawColor(fx_renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
   SDL_RenderClear(fx_renderer);
 
-  unsigned int seconds = SDL_GetTicks() / 1000;
-  float scalefactor = 1 + (SDL_sin(seconds) * 0.005);
+  const unsigned int seconds = SDL_GetTicks() / 1000;
+  const float scalefactor = 1 + SDL_sin(seconds) * 0.005;
 
   scale(scalefactor, scalefactor, scalefactor);
   rotate_cube(M_PI / 180, M_PI / 270);
 
   for (int i = 0; i < 12; i++) {
-    float *p1 = nodes[edges[i][0]];
-    float *p2 = nodes[edges[i][1]];
+    const float *p1 = nodes[edges[i][0]];
+    const float *p2 = nodes[edges[i][1]];
     points[points_counter++] = (SDL_Point){p1[0] + center_x, nodes[edges[i][0]][1] + center_y};
     points[points_counter++] = (SDL_Point){p2[0] + center_x, p2[1] + center_y};
   }
--- a/src/gamecontrollers.c
+++ b/src/gamecontrollers.c
@@ -32,7 +32,7 @@
   }
 
   if (db_rw != NULL) {
-    int mappings = SDL_GameControllerAddMappingsFromRW(db_rw, 1);
+    const int mappings = SDL_GameControllerAddMappingsFromRW(db_rw, 1);
     if (mappings != -1)
       SDL_Log("Found %d game controller mappings", mappings);
     else
@@ -66,8 +66,8 @@
 }
 
 // Check whether a button is pressed on a gamepad and return 1 if pressed.
-static int get_game_controller_button(config_params_s *conf, SDL_GameController *controller,
-                                      int button) {
+static int get_game_controller_button(const config_params_s *conf, SDL_GameController *controller,
+                                      const int button) {
 
   const int button_mappings[8] = {conf->gamepad_up,     conf->gamepad_down, conf->gamepad_left,
                                   conf->gamepad_right,  conf->gamepad_opt,  conf->gamepad_edit,
@@ -111,7 +111,7 @@
 
 // Handle game controllers, simply check all buttons and analog axis on every
 // cycle
-int gamecontrollers_handle_buttons(config_params_s *conf) {
+int gamecontrollers_handle_buttons(const config_params_s *conf) {
 
   const int keycodes[8] = {key_up,  key_down, key_left,   key_right,
                            key_opt, key_edit, key_select, key_start};
@@ -133,7 +133,7 @@
   return key;
 }
 
-input_msg_s gamecontrollers_handle_special_messages(config_params_s *conf) {
+input_msg_s gamecontrollers_handle_special_messages(const config_params_s *conf) {
   input_msg_s msg = {0};
   // Read special case game controller buttons quit and reset
   for (int gc = 0; gc < num_joysticks; gc++) {
@@ -140,11 +140,11 @@
     if (SDL_GameControllerGetButton(game_controllers[gc], conf->gamepad_quit) &&
         (SDL_GameControllerGetButton(game_controllers[gc], conf->gamepad_select) ||
          SDL_GameControllerGetAxis(game_controllers[gc], conf->gamepad_analog_axis_select)))
-      msg = (input_msg_s){special, msg_quit};
+      msg = (input_msg_s){special, msg_quit, 0, 0};
     else if (SDL_GameControllerGetButton(game_controllers[gc], conf->gamepad_reset) &&
              (SDL_GameControllerGetButton(game_controllers[gc], conf->gamepad_select) ||
               SDL_GameControllerGetAxis(game_controllers[gc], conf->gamepad_analog_axis_select)))
-      msg = (input_msg_s){special, msg_reset_display};
+      msg = (input_msg_s){special, msg_reset_display, 0, 0};
   }
   return msg;
 }
\ No newline at end of file
--- a/src/gamecontrollers.h
+++ b/src/gamecontrollers.h
@@ -7,13 +7,12 @@
 
 #include "config.h"
 #include "input.h"
-#include <SDL_gamecontroller.h>
 
 #define MAX_CONTROLLERS 4
 
 int gamecontrollers_initialize();
 void gamecontrollers_close();
-int gamecontrollers_handle_buttons(config_params_s *conf);
-input_msg_s gamecontrollers_handle_special_messages(config_params_s *conf);
+int gamecontrollers_handle_buttons(const config_params_s *conf);
+input_msg_s gamecontrollers_handle_special_messages(const config_params_s *conf);
 
 #endif //GAMECONTROLLERS_H_
--- a/src/ini.c
+++ b/src/ini.c
@@ -20,10 +20,10 @@
  * SOFTWARE.
  */
 
+#include <ctype.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <ctype.h>
 
 #include "ini.h"
 
@@ -32,11 +32,10 @@
   char *end;
 };
 
-
 /* Case insensitive string compare */
 static int strcmpci(const char *a, const char *b) {
   for (;;) {
-    int d = tolower(*a) - tolower(*b);
+    const int d = tolower(*a) - tolower(*b);
     if (d != 0 || !*a) {
       return d;
     }
@@ -45,7 +44,7 @@
 }
 
 /* Returns the next string in the split data */
-static char* next(ini_t *ini, char *p) {
+static char *next(const ini_t *ini, char *p) {
   p += strlen(p);
   while (p < ini->end && *p == '\0') {
     p++;
@@ -53,13 +52,13 @@
   return p;
 }
 
-static void trim_back(ini_t *ini, char *p) {
+static void trim_back(const ini_t *ini, char *p) {
   while (p >= ini->data && (*p == ' ' || *p == '\t' || *p == '\r')) {
     *p-- = '\0';
   }
 }
 
-static char* discard_line(ini_t *ini, char *p) {
+static char *discard_line(const ini_t *ini, char *p) {
   while (p < ini->end && *p != '\n') {
     *p++ = '\0';
   }
@@ -66,8 +65,7 @@
   return p;
 }
 
-
-static char *unescape_quoted_value(ini_t *ini, char *p) {
+static char *unescape_quoted_value(const ini_t *ini, char *p) {
   /* Use `q` as write-head and `p` as read-head, `p` is always ahead of `q`
    * as escape sequences are always larger than their resultant data */
   char *q = p;
@@ -77,13 +75,22 @@
       /* Handle escaped char */
       p++;
       switch (*p) {
-        default   : *q = *p;    break;
-        case 'r'  : *q = '\r';  break;
-        case 'n'  : *q = '\n';  break;
-        case 't'  : *q = '\t';  break;
-        case '\r' :
-        case '\n' :
-        case '\0' : goto end;
+      default:
+        *q = *p;
+        break;
+      case 'r':
+        *q = '\r';
+        break;
+      case 'n':
+        *q = '\n';
+        break;
+      case 't':
+        *q = '\t';
+        break;
+      case '\r':
+      case '\n':
+      case '\0':
+        goto end;
       }
 
     } else {
@@ -96,84 +103,81 @@
   return q;
 }
 
-
 /* Splits data in place into strings containing section-headers, keys and
  * values using one or more '\0' as a delimiter. Unescapes quoted values */
-static void split_data(ini_t *ini) {
+static void split_data(const ini_t *ini) {
   char *value_start, *line_start;
   char *p = ini->data;
 
   while (p < ini->end) {
     switch (*p) {
-      case '\r':
-      case '\n':
-      case '\t':
-      case ' ':
-        *p = '\0';
-        /* Fall through */
+    case '\r':
+    case '\n':
+    case '\t':
+    case ' ':
+      *p = '\0';
+      /* Fall through */
 
-      case '\0':
-        p++;
-        break;
+    case '\0':
+      p++;
+      break;
 
-      case '[':
-        p += strcspn(p, "]\n");
-        *p = '\0';
-        break;
+    case '[':
+      p += strcspn(p, "]\n");
+      *p = '\0';
+      break;
 
-      case ';':
-        p = discard_line(ini, p);
+    case ';':
+      p = discard_line(ini, p);
+      break;
+
+    default:
+      line_start = p;
+      p += strcspn(p, "=\n");
+
+      /* Is line missing a '='? */
+      if (*p != '=') {
+        p = discard_line(ini, line_start);
         break;
+      }
+      trim_back(ini, p - 1);
 
-      default:
-        line_start = p;
-        p += strcspn(p, "=\n");
+      /* Replace '=' and whitespace after it with '\0' */
+      do {
+        *p++ = '\0';
+      } while (*p == ' ' || *p == '\r' || *p == '\t');
 
-        /* Is line missing a '='? */
-        if (*p != '=') {
-          p = discard_line(ini, line_start);
-          break;
-        }
-        trim_back(ini, p - 1);
+      /* Is a value after '=' missing? */
+      if (*p == '\n' || *p == '\0') {
+        p = discard_line(ini, line_start);
+        break;
+      }
 
-        /* Replace '=' and whitespace after it with '\0' */
-        do {
-          *p++ = '\0';
-        } while (*p == ' ' || *p == '\r' || *p == '\t');
+      if (*p == '"') {
+        /* Handle quoted string value */
+        value_start = p;
+        p = unescape_quoted_value(ini, p);
 
-        /* Is a value after '=' missing? */
-        if (*p == '\n' || *p == '\0') {
+        /* Was the string empty? */
+        if (p == value_start) {
           p = discard_line(ini, line_start);
           break;
         }
 
-        if (*p == '"') {
-          /* Handle quoted string value */
-          value_start = p;
-          p = unescape_quoted_value(ini, p);
+        /* Discard the rest of the line after the string value */
+        p = discard_line(ini, p);
 
-          /* Was the string empty? */
-          if (p == value_start) {
-            p = discard_line(ini, line_start);
-            break;
-          }
-
-          /* Discard the rest of the line after the string value */
-          p = discard_line(ini, p);
-
-        } else {
-          /* Handle normal value */
-          p += strcspn(p, "\n");
-          trim_back(ini, p - 1);
-        }
-        break;
+      } else {
+        /* Handle normal value */
+        p += strcspn(p, "\n");
+        trim_back(ini, p - 1);
+      }
+      break;
     }
   }
 }
 
-
-
-ini_t* ini_load(const char *filename) {
+ini_t *ini_load(const char *filename) {
   ini_t *ini = NULL;
   FILE *fp = NULL;
   int n, sz;
@@ -194,7 +198,7 @@
   /* Get file size */
   fseek(fp, 0, SEEK_END);
   sz = ftell(fp);
-  if (sz==0) {
+  if (sz == 0) {
     goto fail;
   }
   rewind(fp);
@@ -202,7 +206,7 @@
   /* Load file content into memory, null terminate, init end var */
   ini->data = malloc(sz + 1);
   ini->data[sz] = '\0';
-  ini->end = ini->data  + sz;
+  ini->end = ini->data + sz;
   n = fread(ini->data, 1, sz, fp);
   if (n != sz) {
     goto fail;
@@ -216,21 +220,20 @@
   return ini;
 
 fail:
-  if (fp) fclose(fp);
-  if (ini) ini_free(ini);
+  if (fp)
+    fclose(fp);
+  if (ini)
+    ini_free(ini);
   return NULL;
 }
 
-
 void ini_free(ini_t *ini) {
   free(ini->data);
   free(ini);
 }
 
-
-const char* ini_get(ini_t *ini, const char *section, const char *key) {
-  char *current_section = "";
-  char *val;
+const char *ini_get(const ini_t *ini, const char *section, const char *key) {
+  const char *current_section = "";
   char *p = ini->data;
 
   if (*p == '\0') {
@@ -244,7 +247,7 @@
 
     } else {
       /* Handle key */
-      val = next(ini, p);
+      char *val = next(ini, p);
       if (!section || !strcmpci(section, current_section)) {
         if (!strcmpci(p, key)) {
           return val;
@@ -259,11 +262,8 @@
   return NULL;
 }
 
-
-int ini_sget(
-  ini_t *ini, const char *section, const char *key,
-  const char *scanfmt, void *dst
-) {
+int ini_sget(const ini_t *ini, const char *section, const char *key, const char *scanfmt,
+             void *dst) {
   const char *val = ini_get(ini, section, key);
   if (!val) {
     return 0;
@@ -271,7 +271,7 @@
   if (scanfmt) {
     sscanf(val, scanfmt, dst);
   } else {
-    *((const char**) dst) = val;
+    *(const char **)dst = val;
   }
   return 1;
 }
--- a/src/ini.h
+++ b/src/ini.h
@@ -14,7 +14,7 @@
 
 ini_t*      ini_load(const char *filename);
 void        ini_free(ini_t *ini);
-const char* ini_get(ini_t *ini, const char *section, const char *key);
-int         ini_sget(ini_t *ini, const char *section, const char *key, const char *scanfmt, void *dst);
+const char* ini_get(const ini_t *ini, const char *section, const char *key);
+int         ini_sget(const ini_t *ini, const char *section, const char *key, const char *scanfmt, void *dst);
 
 #endif
--- a/src/inprint2.c
+++ b/src/inprint2.c
@@ -9,7 +9,7 @@
 #define CHARACTERS_PER_COLUMN 1
 
 // Offset for seeking from limited character sets
-static const int font_offset = 127 - (CHARACTERS_PER_ROW * CHARACTERS_PER_COLUMN);
+static const int font_offset = 127 - CHARACTERS_PER_ROW * CHARACTERS_PER_COLUMN;
 
 static SDL_Renderer *selected_renderer = NULL;
 static SDL_Texture *inline_font = NULL;
@@ -18,7 +18,6 @@
 static Uint16 selected_font_w, selected_font_h;
 
 void prepare_inline_font(struct inline_font *font) {
-  SDL_Surface *surface;
 
   selected_font_w = font->width;
   selected_font_h = font->height;
@@ -32,7 +31,7 @@
   SDL_RWops *font_bmp =
       SDL_RWFromConstMem(selected_inline_font->image_data, selected_inline_font->image_size);
 
-  surface = SDL_LoadBMP_RW(font_bmp, 1);
+  SDL_Surface *surface = SDL_LoadBMP_RW(font_bmp, 1);
 
   // Black is transparent
   SDL_SetColorKey(surface, SDL_TRUE, SDL_MapRGB(surface->format, 0, 0, 0));
@@ -66,19 +65,19 @@
   selected_font_w = w;
   selected_font_h = h;
 }
-void incolor1(SDL_Color *color) {
+void incolor1(const SDL_Color *color) {
   SDL_SetTextureColorMod(selected_font, color->r, color->g, color->b);
 }
-void incolor(Uint32 fore, Uint32 unused) /* Color must be in 0x00RRGGBB format ! */
+void incolor(const Uint32 fore) /* Color must be in 0x00RRGGBB format ! */
 {
   SDL_Color pal[1];
   pal[0].r = (Uint8)((fore & 0x00FF0000) >> 16);
   pal[0].g = (Uint8)((fore & 0x0000FF00) >> 8);
-  pal[0].b = (Uint8)((fore & 0x000000FF));
+  pal[0].b = (Uint8)(fore & 0x000000FF);
   SDL_SetTextureColorMod(selected_font, pal[0].r, pal[0].g, pal[0].b);
 }
-void inprint(SDL_Renderer *dst, const char *str, Uint32 x, Uint32 y, Uint32 fgcolor,
-             Uint32 bgcolor) {
+void inprint(SDL_Renderer *dst, const char *str, Uint32 x, Uint32 y, const Uint32 fgcolor,
+             const Uint32 bgcolor) {
   SDL_Rect s_rect;
   SDL_Rect d_rect;
   SDL_Rect bg_rect;
@@ -112,13 +111,13 @@
       continue;
     }
     if (fgcolor != previous_fgcolor) {
-      incolor(fgcolor, 0);
+      incolor(fgcolor);
       previous_fgcolor = fgcolor;
     }
 
-    if (bgcolor != -1) {
-      SDL_SetRenderDrawColor(selected_renderer, (Uint8)((bgcolor & 0x00FF0000) >> 16),
-                             (Uint8)((bgcolor & 0x0000FF00) >> 8), (Uint8)((bgcolor & 0x000000FF)),
+    if (bgcolor != fgcolor) {
+      SDL_SetRenderDrawColor(selected_renderer, (bgcolor & 0x00FF0000) >> 16,
+                             (bgcolor & 0x0000FF00) >> 8, bgcolor & 0x000000FF,
                              0xFF);
       bg_rect = d_rect;
       bg_rect.w = selected_inline_font->glyph_x;
--- a/src/input.c
+++ b/src/input.c
@@ -1,13 +1,8 @@
-// Copyright 2021 Jonne Kokkonen
-// Released under the MIT licence, https://opensource.org/licenses/MIT
-
-#include <SDL.h>
-#include <stdio.h>
-
-#include "config.h"
 #include "input.h"
-#include "render.h"
+#include "config.h"
 #include "gamecontrollers.h"
+#include "render.h"
+#include <SDL.h>
 
 uint8_t keyjazz_enabled = 0;
 uint8_t keyjazz_base_octave = 2;
@@ -15,7 +10,7 @@
 
 static uint8_t keycode = 0; // value of the pressed key
 
-static input_msg_s key = {normal, 0};
+static input_msg_s key = {normal, 0, 0, 0};
 
 uint8_t toggle_input_keyjazz() {
   keyjazz_enabled = !keyjazz_enabled;
@@ -153,8 +148,8 @@
   return key;
 }
 
-static input_msg_s handle_normal_keys(SDL_Event *event, config_params_s *conf, uint8_t keyvalue) {
-  input_msg_s key = {normal, keyvalue};
+static input_msg_s handle_normal_keys(const SDL_Event *event, const config_params_s *conf) {
+  input_msg_s key = {normal, 0, 0, 0};
 
   if (event->key.keysym.scancode == conf->key_up) {
     key.value = key_up;
@@ -179,10 +174,9 @@
   } else if (event->key.keysym.scancode == conf->key_delete) {
     key.value = key_opt | key_edit;
   } else if (event->key.keysym.scancode == conf->key_reset) {
-    key = (input_msg_s){special, msg_reset_display};
+    key = (input_msg_s){special, msg_reset_display, 0, 0};
   } else if (event->key.keysym.scancode == conf->key_toggle_audio) {
-    key = (input_msg_s){special, msg_toggle_audio};
-
+    key = (input_msg_s){special, msg_toggle_audio, 0, 0};
   } else {
     key.value = 0;
   }
@@ -197,7 +191,7 @@
   SDL_Event event;
 
   // Read joysticks
-  int key_analog = gamecontrollers_handle_buttons(conf);
+  const int key_analog = gamecontrollers_handle_buttons(conf);
   if (prev_key_analog != key_analog) {
     keycode = key_analog;
     prev_key_analog = key_analog;
@@ -204,7 +198,9 @@
   }
 
   input_msg_s gcmsg = gamecontrollers_handle_special_messages(conf);
-  if (gcmsg.type == special) { key = gcmsg; }
+  if (gcmsg.type == special) {
+    key = gcmsg;
+  }
 
   while (SDL_PollEvent(&event)) {
 
@@ -218,7 +214,7 @@
 
     // Handle SDL quit events (for example, window close)
     case SDL_QUIT:
-      key = (input_msg_s){special, msg_quit};
+      key = (input_msg_s){special, msg_quit, 0, 0};
       break;
 
     case SDL_WINDOWEVENT:
@@ -226,13 +222,12 @@
         static uint32_t ticks_window_resized = 0;
         if (SDL_GetTicks() - ticks_window_resized > 500) {
           SDL_Log("Resizing window...");
-          key = (input_msg_s){special, msg_reset_display};
+          key = (input_msg_s){special, msg_reset_display, 0, 0};
           ticks_window_resized = SDL_GetTicks();
         }
       }
       break;
 
-    // Keyboard events. Special events are handled within SDL_KEYDOWN.
     case SDL_KEYDOWN:
 
       if (event.key.repeat > 0) {
@@ -246,22 +241,26 @@
       }
 
       // ALT+F4 quits program
-      else if (event.key.keysym.sym == SDLK_F4 && (event.key.keysym.mod & KMOD_ALT) > 0) {
-        key = (input_msg_s){special, msg_quit};
+      if (event.key.keysym.sym == SDLK_F4 && (event.key.keysym.mod & KMOD_ALT) > 0) {
+        key = (input_msg_s){special, msg_quit, 0, 0};
         break;
       }
 
       // ESC = toggle keyjazz
-      else if (event.key.keysym.sym == SDLK_ESCAPE) {
+      if (event.key.keysym.sym == SDLK_ESCAPE) {
         display_keyjazz_overlay(toggle_input_keyjazz(), keyjazz_base_octave, keyjazz_velocity);
+        break;
       }
 
-    // Normal keyboard inputs
+    // Intentional fallthrough
     case SDL_KEYUP:
-      key = handle_normal_keys(&event, conf, 0);
 
-      if (keyjazz_enabled)
+      // Normal keyboard inputs
+      key = handle_normal_keys(&event, conf);
+
+      if (keyjazz_enabled) {
         key = handle_keyjazz(&event, key.value, conf);
+      }
       break;
 
     default:
@@ -272,7 +271,7 @@
     case normal:
       if (event.type == SDL_KEYDOWN) {
         keycode |= key.value;
-      } else {
+      } else if (event.type == SDL_KEYUP) {
         keycode &= ~key.value;
       }
       break;
@@ -281,7 +280,7 @@
     case special:
       if (event.type == SDL_KEYDOWN) {
         keycode = key.value;
-      } else {
+      } else if (event.type == SDL_KEYUP) {
         keycode = 0;
       }
       break;
@@ -294,19 +293,20 @@
 // Returns the currently pressed keys to main
 input_msg_s get_input_msg(config_params_s *conf) {
 
-  key = (input_msg_s){normal, 0};
+  key = (input_msg_s){normal, 0, 0, 0};
 
   // Query for SDL events
   handle_sdl_events(conf);
 
   if (!keyjazz_enabled && keycode == (key_start | key_select | key_opt | key_edit)) {
-    key = (input_msg_s){special, msg_reset_display};
+    key = (input_msg_s){special, msg_reset_display, 0, 0};
   }
 
   if (key.type == normal) {
     /* Normal input keys go through some event-based manipulation in
        handle_sdl_events(), the value is stored in keycode variable */
-    return (input_msg_s){key.type, keycode};
+    const input_msg_s input = (input_msg_s){key.type, keycode, 0, 0};
+    return input;
   } else {
     // Special event keys already have the correct keycode baked in
     return key;
--- a/src/main.c
+++ b/src/main.c
@@ -24,11 +24,11 @@
 uint8_t need_display_reset = 0;
 
 // Handles CTRL+C / SIGINT
-void intHandler(int dummy) { run = QUIT; }
+void intHandler() { run = QUIT; }
 
 void close_serial_port() { disconnect(); }
 
-int main(int argc, char *argv[]) {
+int main(const int argc, char *argv[]) {
 
   if (argc == 2 && SDL_strcmp(argv[1], "--list") == 0) {
     return list_devices();
@@ -128,7 +128,7 @@
 
       while (run == WAIT_FOR_DEVICE) {
         // get current input
-        input_msg_s input = get_input_msg(&conf);
+        const input_msg_s input = get_input_msg(&conf);
         if (input.type == special && input.value == msg_quit) {
           SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "Input message QUIT.");
           run = QUIT;
@@ -141,7 +141,7 @@
         }
 
         // Poll for M8 device every second
-        if (port_inited == 0 && (SDL_GetTicks() - ticks_poll_device > 1000)) {
+        if (port_inited == 0 && SDL_GetTicks() - ticks_poll_device > 1000) {
           ticks_poll_device = SDL_GetTicks();
           if (run == WAIT_FOR_DEVICE && init_serial(0, preferred_device) == 1) {
 
@@ -152,7 +152,7 @@
               }
             }
 
-            int result = enable_and_reset_display();
+            const int result = enable_and_reset_display();
             // Device was found; enable display and proceed to the main loop
             if (result == 1) {
               run = RUN;
@@ -186,7 +186,7 @@
     while (run == RUN) {
 
       // get current inputs
-      input_msg_s input = get_input_msg(&conf);
+      const input_msg_s input = get_input_msg(&conf);
 
       switch (input.type) {
       case normal:
@@ -229,20 +229,21 @@
 
       while (1) {
         // read serial port
-        int bytes_read = serial_read(serial_buf, serial_read_size);
+        const int bytes_read = serial_read(serial_buf, serial_read_size);
         if (bytes_read < 0) {
-          SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "Error %d reading serial. \n", (int)bytes_read);
+          SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "Error %d reading serial.", bytes_read);
           run = QUIT;
           break;
-        } else if (bytes_read > 0) {
+        }
+        if (bytes_read > 0) {
           // input from device: reset the zero byte counter and create a
           // pointer to the serial buffer
           zerobyte_packets = 0;
-          uint8_t *cur = serial_buf;
+          const uint8_t *cur = serial_buf;
           const uint8_t *end = serial_buf + bytes_read;
           while (cur < end) {
             // process the incoming bytes into commands and draw them
-            int n = slip_read_byte(&slip, *(cur++));
+            const int n = slip_read_byte(&slip, *cur++);
             if (n != SLIP_NO_ERROR) {
               if (n == SLIP_ERROR_INVALID_PACKET) {
                 reset_display();
@@ -261,17 +262,16 @@
             if (check_serial_port()) {
               // the device is still there, carry on
               break;
-            } else {
-              port_inited = 0;
-              run = WAIT_FOR_DEVICE;
-              close_serial_port();
-              if (conf.audio_enabled == 1) {
-                audio_destroy();
-              }
-              /* we'll make one more loop to see if the device is still there
-               * but just sending zero bytes. if it doesn't get detected when
-               * resetting the port, it will disconnect */
             }
+            port_inited = 0;
+            run = WAIT_FOR_DEVICE;
+            close_serial_port();
+            if (conf.audio_enabled == 1) {
+              audio_destroy();
+            }
+            /* we'll make one more loop to see if the device is still there
+             * but just sending zero bytes. if it doesn't get detected when
+             * resetting the port, it will disconnect */
           }
           break;
         }
--- a/src/render.c
+++ b/src/render.c
@@ -41,7 +41,7 @@
 static uint8_t dirty = 0;
 
 // Initializes SDL and creates a renderer and required surfaces
-int initialize_sdl(int init_fullscreen, int init_use_gpu) {
+int initialize_sdl(const int init_fullscreen, const int init_use_gpu) {
 
   if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
     SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "SDL_Init: %s\n", SDL_GetError());
@@ -97,8 +97,8 @@
 
   // Query window size and resize if smaller than default
   SDL_GetWindowSize(win, &w, &h);
-  if (w < (texture_width * 2) || h < (texture_height * 2)) {
-    SDL_SetWindowSize(win, (texture_width * 2), (texture_height * 2));
+  if (w < texture_width * 2 || h < texture_height * 2) {
+    SDL_SetWindowSize(win, texture_width * 2, texture_height * 2);
   }
 
   SDL_DestroyTexture(maintexture);
@@ -112,7 +112,7 @@
 }
 
 // Set M8 hardware model in use. 0 = MK1, 1 = MK2
-void set_m8_model(unsigned int model) {
+void set_m8_model(const unsigned int model) {
 
   switch (model) {
   case 1:
@@ -126,7 +126,7 @@
   }
 }
 
-void set_font_mode(unsigned int mode) {
+void set_font_mode(int mode) {
   if (mode < 0 || mode > 2) {
     // bad font mode
     return;
@@ -155,7 +155,7 @@
 
 void toggle_fullscreen() {
 
-  int fullscreen_state = SDL_GetWindowFlags(win) & SDL_WINDOW_FULLSCREEN;
+  const int fullscreen_state = SDL_GetWindowFlags(win) & SDL_WINDOW_FULLSCREEN;
 
   SDL_SetWindowFullscreen(win, fullscreen_state ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
   SDL_ShowCursor(fullscreen_state);
@@ -165,10 +165,10 @@
 
 int draw_character(struct draw_character_command *command) {
 
-  uint32_t fgcolor =
-      (command->foreground.r << 16) | (command->foreground.g << 8) | command->foreground.b;
-  uint32_t bgcolor =
-      (command->background.r << 16) | (command->background.g << 8) | command->background.b;
+  const uint32_t fgcolor =
+      command->foreground.r << 16 | command->foreground.g << 8 | command->foreground.b;
+  const uint32_t bgcolor =
+      command->background.r << 16 | command->background.g << 8 | command->background.b;
 
   /* Notes:
      If large font is enabled, offset the screen elements by a fixed amount.
@@ -178,7 +178,7 @@
 
   inprint(rend, (char *)&command->c, command->pos.x,
           command->pos.y + text_offset_y + screen_offset_y, fgcolor,
-          (bgcolor == fgcolor) ? -1 : bgcolor);
+          bgcolor);
 
   dirty = 1;
 
@@ -273,12 +273,13 @@
   }
 }
 
-void display_keyjazz_overlay(uint8_t show, uint8_t base_octave, uint8_t velocity) {
+void display_keyjazz_overlay(const uint8_t show, const uint8_t base_octave,
+                             const uint8_t velocity) {
 
   const Uint16 overlay_offset_x = texture_width - (fonts[font_mode]->glyph_x * 7 + 1);
   const Uint16 overlay_offset_y = texture_height - (fonts[font_mode]->glyph_y + 1);
   const Uint32 bgcolor =
-      (background_color.r << 16) | (background_color.g << 8) | background_color.b;
+      background_color.r << 16 | background_color.g << 8 | background_color.b;
 
   if (show) {
     char overlay_text[7];
--- a/src/render.h
+++ b/src/render.h
@@ -4,6 +4,7 @@
 #ifndef RENDER_H_
 #define RENDER_H_
 
+#include <stdint.h>
 #include "command.h"
 
 int initialize_sdl(int init_fullscreen, int init_use_gpu);
@@ -12,7 +13,7 @@
 void draw_waveform(struct draw_oscilloscope_waveform_command *command);
 void draw_rectangle(struct draw_rectangle_command *command);
 int draw_character(struct draw_character_command *command);
-void set_font_mode(unsigned int mode);
+void set_font_mode(int mode);
 void set_m8_model(unsigned int model);
 void view_changed(int view);
 
--- a/src/ringbuffer.c
+++ b/src/ringbuffer.c
@@ -3,7 +3,7 @@
 
 RingBuffer *ring_buffer_create(uint32_t size) {
   RingBuffer *rb = SDL_malloc(sizeof(*rb));
-  rb->buffer = SDL_malloc(sizeof(*(rb->buffer)) * size);
+  rb->buffer = SDL_malloc(sizeof(*rb->buffer) * size);
   rb->head = 0;
   rb->tail = 0;
   rb->max_size = size;
@@ -16,42 +16,40 @@
   free(rb);
 }
 
-uint32_t ring_buffer_empty(RingBuffer *rb) { return (rb->size == 0); }
+uint32_t ring_buffer_empty(RingBuffer *rb) { return rb->size == 0; }
 
-uint32_t ring_buffer_full(RingBuffer *rb) { return (rb->size == rb->max_size); }
+uint32_t ring_buffer_full(RingBuffer *rb) { return rb->size == rb->max_size; }
 
 uint32_t ring_buffer_push(RingBuffer *rb, const uint8_t *data, uint32_t length) {
   if (ring_buffer_full(rb)) {
     return -1; // buffer full, push fails
+  }
+  uint32_t space1 = rb->max_size - rb->tail;
+  uint32_t n = length <= rb->max_size - rb->size ? length : rb->max_size - rb->size;
+  if (n <= space1) {
+    SDL_memcpy(rb->buffer + rb->tail, data, n);
   } else {
-    uint32_t space1 = rb->max_size - rb->tail;
-    uint32_t n = (length <= rb->max_size - rb->size) ? length : (rb->max_size - rb->size);
-    if (n <= space1) {
-      SDL_memcpy(rb->buffer + rb->tail, data, n);
-    } else {
-      SDL_memcpy(rb->buffer + rb->tail, data, space1);
-      SDL_memcpy(rb->buffer, data + space1, n - space1);
-    }
-    rb->tail = (rb->tail + n) % rb->max_size;
-    rb->size += n;
-    return n; // push successful, returns number of bytes pushed
+    SDL_memcpy(rb->buffer + rb->tail, data, space1);
+    SDL_memcpy(rb->buffer, data + space1, n - space1);
   }
+  rb->tail = (rb->tail + n) % rb->max_size;
+  rb->size += n;
+  return n; // push successful, returns number of bytes pushed
 }
 
 uint32_t ring_buffer_pop(RingBuffer *rb, uint8_t *data, uint32_t length) {
   if (ring_buffer_empty(rb)) {
     return -1; // buffer empty, pop fails
+  }
+  uint32_t space1 = rb->max_size - rb->head;
+  uint32_t n = length <= rb->size ? length : rb->size;
+  if (n <= space1) {
+    SDL_memcpy(data, rb->buffer + rb->head, n);
   } else {
-    uint32_t space1 = rb->max_size - rb->head;
-    uint32_t n = (length <= rb->size) ? length : rb->size;
-    if (n <= space1) {
-      SDL_memcpy(data, rb->buffer + rb->head, n);
-    } else {
-      SDL_memcpy(data, rb->buffer + rb->head, space1);
-      SDL_memcpy(data + space1, rb->buffer, n - space1);
-    }
-    rb->head = (rb->head + n) % rb->max_size;
-    rb->size -= n;
-    return n; // pop successful, returns number of bytes popped
+    SDL_memcpy(data, rb->buffer + rb->head, space1);
+    SDL_memcpy(data + space1, rb->buffer, n - space1);
   }
+  rb->head = (rb->head + n) % rb->max_size;
+  rb->size -= n;
+  return n; // pop successful, returns number of bytes popped
 }
--- a/src/serial.c
+++ b/src/serial.c
@@ -19,9 +19,9 @@
 // Helper function for error handling
 static int check(enum sp_return result);
 
-static int detect_m8_serial_device(struct sp_port *m8_port) {
+static int detect_m8_serial_device(const struct sp_port *m8_port) {
   // Check the connection method - we want USB serial devices
-  enum sp_transport transport = sp_get_port_transport(m8_port);
+  const enum sp_transport transport = sp_get_port_transport(m8_port);
 
   if (transport == SP_TRANSPORT_USB) {
     // Get the USB vendor and product IDs.
@@ -37,7 +37,7 @@
 
 int list_devices() {
   struct sp_port **port_list;
-  enum sp_return result = sp_list_ports(&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");
@@ -45,7 +45,7 @@
   }
 
   for (int i = 0; port_list[i] != NULL; i++) {
-    struct sp_port *port = port_list[i];
+    const struct sp_port *port = port_list[i];
 
     if (detect_m8_serial_device(port)) {
       SDL_Log("Found M8 device: %s", sp_get_port_name(port));
@@ -67,7 +67,7 @@
 
   /* Call sp_list_ports() to get the ports. The port_list
    * pointer will be updated to refer to the array created. */
-  enum sp_return result = sp_list_ports(&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");
@@ -77,7 +77,7 @@
   /* Iterate through the ports. When port_list[i] is NULL
    * this indicates the end of the list. */
   for (int i = 0; port_list[i] != NULL; i++) {
-    struct sp_port *port = port_list[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)
@@ -89,7 +89,7 @@
   return device_found;
 }
 
-int init_serial(int verbose, char *preferred_device) {
+int init_serial(const int verbose, const char *preferred_device) {
   if (m8_port != NULL) {
     // Port is already initialized
     return 1;
@@ -113,7 +113,7 @@
   /* Iterate through the ports. When port_list[i] is NULL
    * this indicates the end of the list. */
   for (int i = 0; port_list[i] != NULL; i++) {
-    struct sp_port *port = port_list[i];
+    const struct sp_port *port = port_list[i];
 
     if (detect_m8_serial_device(port)) {
       char *port_name = sp_get_port_name(port);
@@ -130,8 +130,7 @@
 
   if (m8_port != NULL) {
     // Open the serial port and configure it
-    SDL_Log("Opening port.\n");
-    enum sp_return result;
+    SDL_Log("Opening port.");
 
     result = sp_open(m8_port, SP_MODE_READ_WRITE);
     if (check(result) != SP_OK)
@@ -167,7 +166,7 @@
 }
 
 // Helper function for error handling.
-static int check(enum sp_return result) {
+static int check(const enum sp_return result) {
 
   char *error_message;
 
@@ -194,12 +193,10 @@
 }
 
 int reset_display() {
-  int result;
-
   SDL_Log("Reset display\n");
 
-  char buf[1] = {'R'};
-  result = sp_blocking_write(m8_port, buf, 1, 5);
+  const char buf[1] = {'R'};
+  int result = sp_blocking_write(m8_port, buf, 1, 5);
   if (result != 1) {
     SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error resetting M8 display, code %d", result);
     return 0;
@@ -208,12 +205,11 @@
 }
 
 int enable_and_reset_display() {
-  int result;
 
   SDL_Log("Enabling and resetting M8 display\n");
 
-  char buf[1] = {'E'};
-  result = sp_blocking_write(m8_port, buf, 1, 5);
+  const char buf[1] = {'E'};
+  int result = sp_blocking_write(m8_port, buf, 1, 5);
   if (result != 1) {
     SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error enabling M8 display, code %d", result);
     return 0;
@@ -225,13 +221,12 @@
 }
 
 int disconnect() {
-  int result;
 
   SDL_Log("Disconnecting M8\n");
 
-  char buf[1] = {'D'};
+  const char buf[1] = {'D'};
 
-  result = sp_blocking_write(m8_port, buf, 1, 5);
+  int result = sp_blocking_write(m8_port, buf, 1, 5);
   if (result != 1) {
     SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error sending disconnect, code %d", result);
     result = 0;
@@ -242,15 +237,14 @@
   return result;
 }
 
-int serial_read(uint8_t *serial_buf, int count) {
+int serial_read(uint8_t *serial_buf, const int count) {
   return sp_nonblocking_read(m8_port, serial_buf, count);
 }
 
-int send_msg_controller(uint8_t input) {
-  char buf[2] = {'C', input};
-  size_t nbytes = 2;
-  int result;
-  result = sp_blocking_write(m8_port, buf, nbytes, 5);
+int send_msg_controller(const uint8_t input) {
+  const char buf[2] = {'C', input};
+  const size_t nbytes = 2;
+  int result = sp_blocking_write(m8_port, buf, nbytes, 5);
   if (result != nbytes) {
     SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error sending input, code %d", result);
     return -1;
@@ -258,13 +252,12 @@
   return 1;
 }
 
-int send_msg_keyjazz(uint8_t note, uint8_t velocity) {
+int send_msg_keyjazz(const uint8_t note, uint8_t velocity) {
   if (velocity > 0x7F)
     velocity = 0x7F;
-  char buf[3] = {'K', note, velocity};
-  size_t nbytes = 3;
-  int result;
-  result = sp_blocking_write(m8_port, buf, nbytes, 5);
+  const char buf[3] = {'K', note, velocity};
+  const size_t nbytes = 3;
+  int result = sp_blocking_write(m8_port, buf, nbytes, 5);
   if (result != nbytes) {
     SDL_LogError(SDL_LOG_CATEGORY_SYSTEM, "Error sending keyjazz, code %d", result);
     return -1;
--- a/src/serial.h
+++ b/src/serial.h
@@ -5,7 +5,6 @@
 #define _SERIAL_H_
 #include <stdint.h>
 
-#include <stdint.h>
 #ifdef USE_LIBUSB
 // Max packet length of the USB endpoint
 #define serial_read_size 1024
@@ -15,7 +14,7 @@
 #define serial_read_size 1024
 #endif
 
-int init_serial(int verbose, char *preferred_device);
+int init_serial(int verbose, const char *preferred_device);
 int list_devices();
 int check_serial_port();
 int reset_display();
--- a/src/slip.c
+++ b/src/slip.c
@@ -50,7 +50,7 @@
   return SLIP_NO_ERROR;
 }
 
-static slip_error_t put_byte_to_buffer(slip_handler_s *slip, uint8_t byte) {
+static slip_error_t put_byte_to_buffer(slip_handler_s *slip, const uint8_t byte) {
   slip_error_t error = SLIP_NO_ERROR;
 
   assert(slip != NULL);
--- a/src/usb.h
+++ b/src/usb.h
@@ -1,9 +1,9 @@
+#ifndef M8C_USB_H_
+#define M8C_USB_H_
 #ifdef USE_LIBUSB
-#ifndef M8C_USB_H
-#define M8C_USB_H
 
 #include <libusb.h>
 extern libusb_device_handle *devh;
 
-#endif // M8C_USB_H
-#endif
+#endif // USE_LIBUSB
+#endif // M8C_USB_H_
--