From a2a575b829eb5a0326995cacfbbef1bc8b53d31e Mon Sep 17 00:00:00 2001 From: Jonne Kokkonen Date: Mon, 19 Aug 2024 22:10:27 +0300 Subject: [PATCH 1/4] Add missing include guards to font header files --- src/font1.h | 6 +++++- src/font2.h | 3 +++ src/font3.h | 4 ++++ src/font4.h | 4 ++++ src/font5.h | 4 ++++ 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/font1.h b/src/font1.h index 8ac3718..1eaf669 100644 --- 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 @@ struct inline_font font_v1_small = { 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 diff --git a/src/font2.h b/src/font2.h index d2dffe0..c8de8ef 100644 --- 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 @@ struct inline_font font_v1_large = { 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 diff --git a/src/font3.h b/src/font3.h index 4602981..ef6b4b7 100644 --- 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 @@ struct inline_font font_v2_small = { 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 diff --git a/src/font4.h b/src/font4.h index 27b87fa..b4b42af 100644 --- 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 @@ struct inline_font font_v2_large = { 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 diff --git a/src/font5.h b/src/font5.h index e763d51..08395f3 100644 --- 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 @@ struct inline_font font_v2_huge = { 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 From 7f74d42da1d0defad619ee88d40596ba2493238a Mon Sep 17 00:00:00 2001 From: Jonne Kokkonen Date: Mon, 19 Aug 2024 22:19:54 +0300 Subject: [PATCH 2/4] Clean up unreachable code and redundant else keywords --- src/audio.c | 77 ++++++++++++++-------------- src/command.c | 131 ++++++++++++++++++++--------------------------- src/main.c | 22 ++++---- src/ringbuffer.c | 42 ++++++++------- 4 files changed, 125 insertions(+), 147 deletions(-) diff --git a/src/audio.c b/src/audio.c index bda6bb1..fcde690 100644 --- a/src/audio.c +++ b/src/audio.c @@ -40,48 +40,47 @@ int audio_init(unsigned int audio_buffer_size, const char *output_device_name) { 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; - } - } - if (m8_device_id == -1) { - // forget about it - SDL_Log("Cannot find M8 audio input device"); - return 0; + } + 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; } + } + 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; - - // 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_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; + } - 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 diff --git a/src/command.c b/src/command.c index 4aafec5..53acfaa 100644 --- a/src/command.c +++ b/src/command.c @@ -42,8 +42,7 @@ int process_command(uint8_t *data, uint32_t size) { 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,74 +51,64 @@ int process_command(uint8_t *data, uint32_t size) { 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 */ - - static struct draw_rectangle_command rectcmd; - - 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; } + /* 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 */ - break; + static struct draw_rectangle_command rectcmd; - case draw_character_command: + 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; + } + + 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", 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,21 +117,17 @@ int process_command(uint8_t *data, uint32_t size) { draw_oscilloscope_waveform_command_maxdatalength, size); dump_packet(size, recv_buf); return 0; - break; - } else { - - 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); + } + struct draw_oscilloscope_waveform_command osccmd; - osccmd.waveform_size = 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); - draw_waveform(&osccmd); - return 1; - } + osccmd.waveform_size = size - 4; - break; + draw_waveform(&osccmd); + return 1; + } case joypad_keypressedstate_command: { if (size != joypad_keypressedstate_command_datalength) { @@ -152,12 +137,10 @@ int process_command(uint8_t *data, uint32_t size) { 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 @@ int process_command(uint8_t *data, uint32_t size) { 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; } diff --git a/src/main.c b/src/main.c index b4a2193..de12e6f 100644 --- a/src/main.c +++ b/src/main.c @@ -234,7 +234,8 @@ int main(int argc, char *argv[]) { SDL_LogCritical(SDL_LOG_CATEGORY_ERROR, "Error %d reading serial. \n", (int)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; @@ -261,17 +262,16 @@ int main(int argc, char *argv[]) { 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; } diff --git a/src/ringbuffer.c b/src/ringbuffer.c index 5e4e490..d97ecb4 100644 --- a/src/ringbuffer.c +++ b/src/ringbuffer.c @@ -23,35 +23,33 @@ 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 } From 1c4fa7f9cd7a53bce609d956664cda5021831bbd Mon Sep 17 00:00:00 2001 From: Jonne Kokkonen Date: Sat, 24 Aug 2024 23:32:58 +0300 Subject: [PATCH 3/4] add -Wextra to cflags, fix keyboard controls not working, lots of refactoring and cleanup --- CMakeLists.txt | 2 + Makefile | 2 +- src/SDL2_inprint.h | 4 +- src/audio.c | 12 ++- src/command.c | 4 +- src/config.c | 18 ++--- src/config.h | 60 +++++++-------- src/fx_cube.c | 34 ++++----- src/gamecontrollers.c | 14 ++-- src/gamecontrollers.h | 5 +- src/ini.c | 170 +++++++++++++++++++++--------------------- src/ini.h | 4 +- src/inprint2.c | 23 +++--- src/input.c | 60 +++++++-------- src/main.c | 20 ++--- src/render.c | 27 +++---- src/render.h | 3 +- src/ringbuffer.c | 10 +-- src/serial.c | 57 +++++++------- src/serial.h | 3 +- src/slip.c | 2 +- src/usb.h | 8 +- 22 files changed, 266 insertions(+), 276 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e5a94d9..45b2051 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.15) project(m8c LANGUAGES C) +set(CMAKE_C_FLAGS "-O2 -Wall -Wextra") + set(APP_NAME m8c) find_package(PkgConfig REQUIRED) diff --git a/Makefile b/Makefile index f9c44e3..ae4679b 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ DEPS = src/serial.h src/slip.h src/command.h src/render.h src/ini.h src/config.h 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 diff --git a/src/SDL2_inprint.h b/src/SDL2_inprint.h index 5b98861..f1e9468 100644 --- a/src/SDL2_inprint.h +++ b/src/SDL2_inprint.h @@ -12,8 +12,8 @@ extern void kill_inline_font(void); 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); diff --git a/src/audio.c b/src/audio.c index fcde690..53c9491 100644 --- a/src/audio.c +++ b/src/audio.c @@ -11,7 +11,7 @@ static SDL_AudioDeviceID devid_out = 0; 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 @@ void toggle_audio(unsigned int audio_buffer_size, const char *output_device_name 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) { diff --git a/src/command.c b/src/command.c index 53acfaa..14a922e 100644 --- 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 @@ enum m8_command_bytes { 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]); } diff --git a/src/config.c b/src/config.c index 6e8581a..50848a9 100644 --- a/src/config.c +++ b/src/config.c @@ -7,10 +7,10 @@ #include #include -/* 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 @@ config_params_s init_config() { } // 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 @@ void write_config(config_params_s *conf) { 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 @@ void read_config(config_params_s *conf) { 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_audio_config(ini_t *ini, config_params_s *conf) { } } -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 @@ void read_graphics_config(ini_t *ini, config_params_s *conf) { 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 @@ void read_key_config(ini_t *ini, config_params_s *conf) { 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"); diff --git a/src/config.h b/src/config.h index 8c0ca39..0a65907 100644 --- 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 @@ typedef struct config_params_s { 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 diff --git a/src/fx_cube.c b/src/fx_cube.c index d4669ce..43bcd0b 100644 --- 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 #include +#include "SDL2_inprint.h" static SDL_Texture *texture_cube; static SDL_Texture *texture_text; @@ -20,7 +18,7 @@ static int edges[12][2] = {{0, 1}, {1, 3}, {3, 2}, {2, 0}, {4, 5}, {5, 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 scale(float factor0, float factor1, float factor2) { } } -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 @@ static void rotate_cube(float angle_x, float angle_y) { } } -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 @@ void fx_cube_update() { 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}; } diff --git a/src/gamecontrollers.c b/src/gamecontrollers.c index 400aefb..02497ea 100644 --- a/src/gamecontrollers.c +++ b/src/gamecontrollers.c @@ -32,7 +32,7 @@ int gamecontrollers_initialize() { } 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 @@ void gamecontrollers_close() { } // 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 @@ static int get_game_controller_button(config_params_s *conf, SDL_GameController // 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,18 +133,18 @@ int gamecontrollers_handle_buttons(config_params_s *conf) { 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++) { 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 diff --git a/src/gamecontrollers.h b/src/gamecontrollers.h index 30d5182..c247a2d 100644 --- a/src/gamecontrollers.h +++ b/src/gamecontrollers.h @@ -7,13 +7,12 @@ #include "config.h" #include "input.h" -#include #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_ diff --git a/src/ini.c b/src/ini.c index 6c31131..f80fee3 100644 --- a/src/ini.c +++ b/src/ini.c @@ -20,10 +20,10 @@ * SOFTWARE. */ +#include #include #include #include -#include #include "ini.h" @@ -32,11 +32,10 @@ struct ini_t { 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 @@ static int strcmpci(const char *a, const char *b) { } /* 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,21 +52,20 @@ static char* next(ini_t *ini, char *p) { 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'; } 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 @@ static char *unescape_quoted_value(ini_t *ini, char *p) { /* 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 @@ static char *unescape_quoted_value(ini_t *ini, char *p) { 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); + break; - case ';': - p = discard_line(ini, p); + 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); - - /* 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); + /* 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 @@ ini_t* ini_load(const char *filename) { /* 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 @@ ini_t* ini_load(const char *filename) { /* 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 @@ ini_t* ini_load(const char *filename) { 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 @@ const char* ini_get(ini_t *ini, const char *section, const char *key) { } 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 @@ const char* ini_get(ini_t *ini, const char *section, const char *key) { 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 @@ int ini_sget( if (scanfmt) { sscanf(val, scanfmt, dst); } else { - *((const char**) dst) = val; + *(const char **)dst = val; } return 1; } diff --git a/src/ini.h b/src/ini.h index cd6af9f..df889dd 100644 --- a/src/ini.h +++ b/src/ini.h @@ -14,7 +14,7 @@ typedef struct ini_t ini_t; 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 diff --git a/src/inprint2.c b/src/inprint2.c index 76058fa..45e3ce5 100644 --- 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 struct inline_font *selected_inline_font; 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 @@ void prepare_inline_font(struct inline_font *font) { 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 @@ void infont(SDL_Texture *font) { 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 @@ void inprint(SDL_Renderer *dst, const char *str, Uint32 x, Uint32 y, Uint32 fgco 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; diff --git a/src/input.c b/src/input.c index e739614..46236be 100644 --- 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 -#include - -#include "config.h" #include "input.h" -#include "render.h" +#include "config.h" #include "gamecontrollers.h" +#include "render.h" +#include uint8_t keyjazz_enabled = 0; uint8_t keyjazz_base_octave = 2; @@ -15,7 +10,7 @@ uint8_t keyjazz_velocity = 0x64; 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 @@ static input_msg_s handle_keyjazz(SDL_Event *event, uint8_t keyvalue, config_par 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 @@ static input_msg_s handle_normal_keys(SDL_Event *event, config_params_s *conf, u } 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,14 +191,16 @@ void handle_sdl_events(config_params_s *conf) { 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; } 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 @@ void handle_sdl_events(config_params_s *conf) { // 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 @@ void handle_sdl_events(config_params_s *conf) { 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 @@ void handle_sdl_events(config_params_s *conf) { } // 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 @@ void handle_sdl_events(config_params_s *conf) { 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 @@ void handle_sdl_events(config_params_s *conf) { case special: if (event.type == SDL_KEYDOWN) { keycode = key.value; - } else { + } else if (event.type == SDL_KEYUP) { keycode = 0; } break; @@ -294,19 +293,20 @@ void handle_sdl_events(config_params_s *conf) { // 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; diff --git a/src/main.c b/src/main.c index de12e6f..c16bca4 100644 --- a/src/main.c +++ b/src/main.c @@ -24,11 +24,11 @@ enum state run = WAIT_FOR_DEVICE; 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 @@ int main(int argc, char *argv[]) { 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 @@ int main(int argc, char *argv[]) { } // 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 main(int argc, char *argv[]) { } } - 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 @@ int main(int argc, char *argv[]) { 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 @@ int main(int argc, char *argv[]) { 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 @@ int main(int argc, char *argv[]) { // 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(); diff --git a/src/render.c b/src/render.c index e189986..1acf6b7 100644 --- a/src/render.c +++ b/src/render.c @@ -41,7 +41,7 @@ uint8_t fullscreen = 0; 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 @@ static void check_and_adjust_window_and_texture_size(const unsigned int new_widt // 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 @@ static void check_and_adjust_window_and_texture_size(const unsigned int new_widt } // 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_m8_model(unsigned int model) { } } -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 close_renderer() { 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 @@ void toggle_fullscreen() { 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 @@ int draw_character(struct draw_character_command *command) { 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 draw_waveform(struct draw_oscilloscope_waveform_command *command) { } } -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]; diff --git a/src/render.h b/src/render.h index 907d026..9c2c589 100644 --- a/src/render.h +++ b/src/render.h @@ -4,6 +4,7 @@ #ifndef RENDER_H_ #define RENDER_H_ +#include #include "command.h" int initialize_sdl(int init_fullscreen, int init_use_gpu); @@ -12,7 +13,7 @@ void close_renderer(); 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); diff --git a/src/ringbuffer.c b/src/ringbuffer.c index d97ecb4..fd2fcfb 100644 --- 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,16 +16,16 @@ void ring_buffer_free(RingBuffer *rb) { 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); + 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 @@ uint32_t ring_buffer_pop(RingBuffer *rb, uint8_t *data, uint32_t length) { 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 { diff --git a/src/serial.c b/src/serial.c index 98b0d0f..9032c42 100644 --- a/src/serial.c +++ b/src/serial.c @@ -19,9 +19,9 @@ struct sp_port *m8_port = NULL; // 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 @@ static int detect_m8_serial_device(struct sp_port *m8_port) { 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 @@ int list_devices() { } 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 @@ int check_serial_port() { /* 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 @@ int check_serial_port() { /* 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 @@ int check_serial_port() { 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 @@ int init_serial(int verbose, char *preferred_device) { /* 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 @@ int init_serial(int verbose, char *preferred_device) { 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 @@ int init_serial(int verbose, char *preferred_device) { } // 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 @@ static int check(enum sp_return result) { } 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 reset_display() { } 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 enable_and_reset_display() { } 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 @@ int disconnect() { 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 @@ int send_msg_controller(uint8_t input) { 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; diff --git a/src/serial.h b/src/serial.h index 244ba4a..40ac420 100644 --- a/src/serial.h +++ b/src/serial.h @@ -5,7 +5,6 @@ #define _SERIAL_H_ #include -#include #ifdef USE_LIBUSB // Max packet length of the USB endpoint #define serial_read_size 1024 @@ -15,7 +14,7 @@ int init_serial_with_file_descriptor(int file_descriptor); #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(); diff --git a/src/slip.c b/src/slip.c index 51594c8..e4a6551 100644 --- a/src/slip.c +++ b/src/slip.c @@ -50,7 +50,7 @@ slip_error_t slip_init(slip_handler_s *slip, const slip_descriptor_s *descriptor 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); diff --git a/src/usb.h b/src/usb.h index 74b50bc..cd65a9a 100644 --- 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 extern libusb_device_handle *devh; -#endif // M8C_USB_H -#endif +#endif // USE_LIBUSB +#endif // M8C_USB_H_ From 173be3c5f566ecbc1dca69f5e953ebe3ca9878ff Mon Sep 17 00:00:00 2001 From: Jonne Kokkonen Date: Sat, 24 Aug 2024 23:34:47 +0300 Subject: [PATCH 4/4] fix input.c sdl import --- src/input.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/input.c b/src/input.c index 46236be..97c0414 100644 --- a/src/input.c +++ b/src/input.c @@ -2,7 +2,7 @@ #include "config.h" #include "gamecontrollers.h" #include "render.h" -#include +#include uint8_t keyjazz_enabled = 0; uint8_t keyjazz_base_octave = 2;