ref: 1c4fa7f9cd7a53bce609d956664cda5021831bbd
parent: 7f74d42da1d0defad619ee88d40596ba2493238a
author: Jonne Kokkonen <jonne.kokkonen@gmail.com>
date: Sat Aug 24 19:32:58 EDT 2024
add -Wextra to cflags, fix keyboard controls not working, lots of refactoring and 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,26 +22,24 @@
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;
}
- for (i = 0; i < devcount_in; 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) {--- 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]);
}
--- 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/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 <SDL2/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,9 +229,9 @@
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;
}
@@ -239,11 +239,11 @@
// 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();
--- 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,9 +16,9 @@
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)) {@@ -25,7 +25,7 @@
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);
+ 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 {@@ -42,7 +42,7 @@
return -1; // buffer empty, pop fails
}
uint32_t space1 = rb->max_size - rb->head;
- uint32_t n = (length <= rb->size) ? length : rb->size;
+ uint32_t n = length <= rb->size ? length : rb->size;
if (n <= space1) {SDL_memcpy(data, rb->buffer + rb->head, n);
} else {--- 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_
--
⑨