From c44e9b262486faee1fcd2b101f8cdfebacdd3ad3 Mon Sep 17 00:00:00 2001 From: wins1ey Date: Sun, 2 Jun 2024 22:10:38 +0100 Subject: [PATCH 1/5] Created .clang-format --- .clang-format | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..90507d8 --- /dev/null +++ b/.clang-format @@ -0,0 +1,4 @@ +--- +BasedOnStyle: WebKit +IndentCaseBlocks: true +IndentCaseLabels: true From 43c6b1d75383da636cf1e6fd68296be03cc202da Mon Sep 17 00:00:00 2001 From: wins1ey Date: Sun, 2 Jun 2024 22:10:49 +0100 Subject: [PATCH 2/5] Added 'format' rule to Makefile --- Makefile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Makefile b/Makefile index 581a810..0de3f86 100644 --- a/Makefile +++ b/Makefile @@ -71,4 +71,7 @@ remove-schema: clean: $(RM) -r $(BIN) $(OBJ_DIR) $(SRC_DIR)/main.h +format: + clang-format -i $(SOURCES) $(COMPONENTS) $(SRC_DIR)/*.h $(SRC_DIR)/component/*.h + .PHONY: all main.h install uninstall remove-schema clean From cff2920472c87f0c815b8bc448e7c65e7865164a Mon Sep 17 00:00:00 2001 From: wins1ey Date: Sun, 2 Jun 2024 22:11:02 +0100 Subject: [PATCH 3/5] Created format.yml workflow --- .github/workflows/format.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .github/workflows/format.yml diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml new file mode 100644 index 0000000..27296db --- /dev/null +++ b/.github/workflows/format.yml @@ -0,0 +1,23 @@ +on: [pull_request] + +jobs: + format-check: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Format files + run: | + clang-format -i $(find src -name '*.c' -or -name '*.h') + + - name: Check for changes + run: | + changed_files=$(git diff --name-only) + if [ -n "$changed_files" ]; then + echo "The following files require formatting:" + echo "$changed_files" + echo "Run 'make format' and commit the changes." + exit 1 + fi From 8c600cf20259cbe5d7b9472ab9c14c45c59a154b Mon Sep 17 00:00:00 2001 From: wins1ey Date: Mon, 3 Jun 2024 13:08:01 +0100 Subject: [PATCH 4/5] Update format.yml to run on push --- .github/workflows/format.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 27296db..044e52f 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -1,4 +1,4 @@ -on: [pull_request] +on: [push] jobs: format-check: From 7a3501837e87059c1662baa2e77120fedc783234 Mon Sep 17 00:00:00 2001 From: wins1ey Date: Mon, 3 Jun 2024 14:41:58 +0100 Subject: [PATCH 5/5] Run 'make format' --- src/auto-splitter.c | 177 ++++--- src/bind.c | 885 ++++++++++++++++------------------- src/bind.h | 26 +- src/component/best-sum.c | 49 +- src/component/clock.c | 65 +-- src/component/components.c | 37 +- src/component/components.h | 49 +- src/component/pb.c | 57 +-- src/component/prev-segment.c | 76 ++- src/component/splits.c | 242 ++++------ src/component/title.c | 41 +- src/component/wr.c | 59 +-- src/main.c | 836 +++++++++++++-------------------- src/memory.c | 168 +++---- src/memory.h | 4 +- src/process.c | 36 +- src/process.h | 7 +- src/settings.c | 75 ++- src/settings.h | 4 +- src/timer.c | 563 ++++++++-------------- src/timer.h | 92 ++-- 21 files changed, 1503 insertions(+), 2045 deletions(-) diff --git a/src/auto-splitter.c b/src/auto-splitter.c index d2d308a..4b96ab5 100644 --- a/src/auto-splitter.c +++ b/src/auto-splitter.c @@ -1,19 +1,19 @@ #include -#include -#include -#include #include -#include #include -#include +#include +#include #include +#include +#include +#include +#include #include #include -#include -#include "memory.h" #include "auto-splitter.h" +#include "memory.h" #include "process.h" #include "settings.h" @@ -50,12 +50,13 @@ extern game_process process; // I have no idea how this works // https://stackoverflow.com/a/2336245 -static void mkdir_p(const char *dir, __mode_t permissions) { - char tmp[256] = {0}; - char *p = NULL; +static void mkdir_p(const char* dir, __mode_t permissions) +{ + char tmp[256] = { 0 }; + char* p = NULL; size_t len; - snprintf(tmp, sizeof(tmp),"%s",dir); + snprintf(tmp, sizeof(tmp), "%s", dir); len = strlen(tmp); if (tmp[len - 1] == '/') tmp[len - 1] = 0; @@ -70,7 +71,7 @@ static void mkdir_p(const char *dir, __mode_t permissions) { void check_directories() { - char libresplit_directory[PATH_MAX] = {0}; + char libresplit_directory[PATH_MAX] = { 0 }; get_libresplit_folder_path(libresplit_directory); char auto_splitters_directory[PATH_MAX]; @@ -106,28 +107,27 @@ void check_directories() } static const luaL_Reg lj_lib_load[] = { - { "", luaopen_base }, - { LUA_STRLIBNAME, luaopen_string }, - { LUA_MATHLIBNAME, luaopen_math }, - { LUA_BITLIBNAME, luaopen_bit }, - { LUA_JITLIBNAME, luaopen_jit }, - { NULL, NULL } + { "", luaopen_base }, + { LUA_STRLIBNAME, luaopen_string }, + { LUA_MATHLIBNAME, luaopen_math }, + { LUA_BITLIBNAME, luaopen_bit }, + { LUA_JITLIBNAME, luaopen_jit }, + { NULL, NULL } }; -LUALIB_API void luaL_openlibs(lua_State *L) +LUALIB_API void luaL_openlibs(lua_State* L) { - const luaL_Reg *lib; - for (lib = lj_lib_load; lib->func; lib++) { - lua_pushcfunction(L, lib->func); - lua_pushstring(L, lib->name); - lua_call(L, 1, 0); - } + const luaL_Reg* lib; + for (lib = lj_lib_load; lib->func; lib++) { + lua_pushcfunction(L, lib->func); + lua_pushstring(L, lib->name); + lua_call(L, 1, 0); + } } void disable_functions(lua_State* L, const char** functions) { - for (int i = 0; functions[i] != NULL; i++) - { + for (int i = 0; functions[i] != NULL; i++) { lua_pushnil(L); lua_setglobal(L, functions[i]); } @@ -144,87 +144,89 @@ void disable_functions(lua_State* L, const char** functions) Example: `call_va("functionName", "dd>d", x, y, &z);` */ -bool call_va(lua_State* L, const char *func, const char *sig, ...) { +bool call_va(lua_State* L, const char* func, const char* sig, ...) +{ va_list vl; - int narg, nres; /* number of arguments and results */ - + int narg, nres; /* number of arguments and results */ + va_start(vl, sig); - lua_getglobal(L, func); /* get function */ - + lua_getglobal(L, func); /* get function */ + /* push arguments */ narg = 0; - while (*sig) { /* push arguments */ + while (*sig) { /* push arguments */ switch (*sig++) { - case 'd': /* double argument */ + case 'd': /* double argument */ lua_pushnumber(L, va_arg(vl, double)); break; - - case 'i': /* int argument */ + + case 'i': /* int argument */ lua_pushnumber(L, va_arg(vl, int)); break; - - case 's': /* string argument */ - lua_pushstring(L, va_arg(vl, char *)); + + case 's': /* string argument */ + lua_pushstring(L, va_arg(vl, char*)); break; case 'b': lua_pushboolean(L, va_arg(vl, int)); break; - + case '>': break; - + default: printf("invalid option (%c)\n", *(sig - 1)); return false; } - if(*(sig - 1) == '>') break; + if (*(sig - 1) == '>') + break; narg++; luaL_checkstack(L, 1, "too many arguments"); } - + /* do the call */ - nres = strlen(sig); /* number of expected results */ + nres = strlen(sig); /* number of expected results */ if (lua_pcall(L, narg, nres, 0) != LUA_OK) { const char* err = lua_tostring(L, -1); printf("error running function '%s': %s\n", func, err); return false; } - + /* retrieve results */ - nres = -nres; /* stack index of first result */ - while (*sig) { /* get results */ + nres = -nres; /* stack index of first result */ + while (*sig) { /* get results */ switch (*sig++) { - case 'd': /* double result */ + case 'd': /* double result */ if (!lua_isnumber(L, nres)) { printf("function '%s' wrong result type, expected double\n", func); return false; } - *va_arg(vl, double *) = lua_tonumber(L, nres); + *va_arg(vl, double*) = lua_tonumber(L, nres); break; - - case 'i': /* int result */ + + case 'i': /* int result */ if (!lua_isnumber(L, nres)) { printf("function '%s' wrong result type, expected int\n", func); return false; } - *va_arg(vl, int *) = (int)lua_tonumber(L, nres); + *va_arg(vl, int*) = (int)lua_tonumber(L, nres); break; - - case 's': /* string result */ + + case 's': /* string result */ if (!lua_isstring(L, nres)) { printf("function '%s' wrong result type, expected string\n", func); return false; } - *va_arg(vl, const char **) = lua_tostring(L, nres); + *va_arg(vl, const char**) = lua_tostring(L, nres); break; case 'b': - if (!lua_isboolean(L, nres)){ - printf("function '%s' wrong result type, expected boolean\n", func); + if (!lua_isboolean(L, nres)) { + printf("function '%s' wrong result type, expected boolean\n", func); return false; } - *va_arg(vl, bool *) = lua_toboolean(L, nres); + *va_arg(vl, bool*) = lua_toboolean(L, nres); break; default: @@ -243,15 +245,13 @@ void startup(lua_State* L) lua_pcall(L, 0, 0, 0); lua_getglobal(L, "refreshRate"); - if (lua_isnumber(L, -1)) - { + if (lua_isnumber(L, -1)) { refresh_rate = lua_tointeger(L, -1); } lua_pop(L, 1); // Remove 'refreshRate' from the stack lua_getglobal(L, "mapsCacheCycles"); - if (lua_isnumber(L, -1)) - { + if (lua_isnumber(L, -1)) { maps_cache_cycles = lua_tointeger(L, -1); maps_cache_cycles_value = maps_cache_cycles; } @@ -260,18 +260,18 @@ void startup(lua_State* L) void state(lua_State* L) { - call_va(L,"state", ""); + call_va(L, "state", ""); } void update(lua_State* L) { - call_va(L,"update", ""); + call_va(L, "update", ""); } void start(lua_State* L) { bool ret; - if(call_va(L, "start", ">b", &ret)){ + if (call_va(L, "start", ">b", &ret)) { atomic_store(&call_start, ret); } lua_pop(L, 1); // Remove the return value from the stack @@ -280,7 +280,7 @@ void start(lua_State* L) void split(lua_State* L) { bool ret; - if (call_va(L,"split", ">b", &ret)) { + if (call_va(L, "split", ">b", &ret)) { atomic_store(&call_split, ret); } lua_pop(L, 1); // Remove the return value from the stack @@ -289,9 +289,8 @@ void split(lua_State* L) void is_loading(lua_State* L) { bool loading; - if(call_va(L,"isLoading", ">b", &loading)){ - if (loading != prev_is_loading) - { + if (call_va(L, "isLoading", ">b", &loading)) { + if (loading != prev_is_loading) { atomic_store(&toggle_loading, true); prev_is_loading = !prev_is_loading; } @@ -302,8 +301,8 @@ void is_loading(lua_State* L) void reset(lua_State* L) { bool shouldReset; - if(call_va(L,"reset",">b",&shouldReset)){ - if(shouldReset) + if (call_va(L, "reset", ">b", &shouldReset)) { + if (shouldReset) atomic_store(&call_reset, true); } lua_pop(L, 1); // Remove the return value from the stack @@ -325,8 +324,7 @@ void run_auto_splitter() strcpy(current_file, auto_splitter_file); // Load the Lua file - if (luaL_loadfile(L, auto_splitter_file) != LUA_OK) - { + if (luaL_loadfile(L, auto_splitter_file) != LUA_OK) { // Error loading the file const char* error_msg = lua_tostring(L, -1); lua_pop(L, 1); // Remove the error message from the stack @@ -337,8 +335,7 @@ void run_auto_splitter() } // Execute the Lua file - if (lua_pcall(L, 0, LUA_MULTRET, 0) != LUA_OK) - { + if (lua_pcall(L, 0, LUA_MULTRET, 0) != LUA_OK) { // Error executing the file const char* error_msg = lua_tostring(L, -1); lua_pop(L, 1); // Remove the error message from the stack @@ -376,51 +373,42 @@ void run_auto_splitter() bool update_exists = lua_isfunction(L, -1); lua_pop(L, 1); // Remove 'update' from the stack - if (startup_exists) - { + if (startup_exists) { startup(L); } printf("Refresh rate: %d\n", refresh_rate); int rate = 1000000 / refresh_rate; - while (1) - { + while (1) { struct timespec clock_start; clock_gettime(CLOCK_MONOTONIC, &clock_start); - if (!atomic_load(&auto_splitter_enabled) || strcmp(current_file, auto_splitter_file) != 0 || !process_exists() || process.pid == 0) - { + if (!atomic_load(&auto_splitter_enabled) || strcmp(current_file, auto_splitter_file) != 0 || !process_exists() || process.pid == 0) { break; } - if (state_exists) - { + if (state_exists) { state(L); } - if (update_exists) - { + if (update_exists) { update(L); } - if (start_exists) - { + if (start_exists) { start(L); } - if (split_exists) - { + if (split_exists) { split(L); } - if (is_loading_exists) - { + if (is_loading_exists) { is_loading(L); } - if (reset_exists) - { + if (reset_exists) { reset(L); } @@ -436,8 +424,7 @@ void run_auto_splitter() clock_gettime(CLOCK_MONOTONIC, &clock_end); long long duration = (clock_end.tv_sec - clock_start.tv_sec) * 1000000 + (clock_end.tv_nsec - clock_start.tv_nsec) / 1000; // printf("duration: %llu\n", duration); - if (duration < rate) - { + if (duration < rate) { usleep(rate - duration); } } diff --git a/src/bind.c b/src/bind.c index 361c5d4..64525c5 100644 --- a/src/bind.c +++ b/src/bind.c @@ -21,15 +21,15 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#include #include +#include #include -#include +#include +#include #include #include -#include -#include +#include #include "bind.h" @@ -37,9 +37,11 @@ /* #define DEBUG */ #ifdef DEBUG -# define TRACE(x) x +#define TRACE(x) x #else -# define TRACE(x) do {} while (FALSE); +#define TRACE(x) \ + do { \ + } while (FALSE); #endif #define MODIFIERS_ERROR ((GdkModifierType)(-1)) @@ -58,19 +60,17 @@ */ #define WE_ONLY_USE_ONE_GROUP 0 - -struct Binding -{ - KeybinderHandler handler; - void *user_data; - char *keystring; - GDestroyNotify notify; - /* GDK "distilled" values */ - guint keyval; - GdkModifierType modifiers; +struct Binding { + KeybinderHandler handler; + void* user_data; + char* keystring; + GDestroyNotify notify; + /* GDK "distilled" values */ + guint keyval; + GdkModifierType modifiers; }; -static GSList *bindings = NULL; +static GSList* bindings = NULL; static guint32 last_event_time = 0; static gboolean processing_event = FALSE; @@ -78,124 +78,106 @@ static gboolean processing_event = FALSE; * given group (keyboard layout) and level ("shift level"). */ static GdkModifierType -FinallyGetModifiersForKeycode (XkbDescPtr xkb, - KeyCode key, - uint group, - uint level) +FinallyGetModifiersForKeycode(XkbDescPtr xkb, + KeyCode key, + uint group, + uint level) { - int nKeyGroups; - int effectiveGroup; - XkbKeyTypeRec *type; - int k; - - nKeyGroups = XkbKeyNumGroups(xkb, key); - if ((!XkbKeycodeInRange(xkb, key)) || (nKeyGroups == 0)) - { - return MODIFIERS_ERROR; - } - - /* Taken from GDK's MyEnhancedXkbTranslateKeyCode */ - /* find the offset of the effective group */ - effectiveGroup = group; - if (effectiveGroup >= nKeyGroups) - { - unsigned groupInfo = XkbKeyGroupInfo(xkb,key); - switch (XkbOutOfRangeGroupAction(groupInfo)) - { - default: - effectiveGroup %= nKeyGroups; - break; - case XkbClampIntoRange: - effectiveGroup = nKeyGroups-1; - break; - case XkbRedirectIntoRange: - effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo); - if (effectiveGroup >= nKeyGroups) - effectiveGroup = 0; - break; - } - } - type = XkbKeyKeyType(xkb, key, effectiveGroup); - for (k = 0; k < type->map_count; k++) - { - if (type->map[k].active && type->map[k].level == level) - { - if (type->preserve) - { - return (type->map[k].mods.mask & - ~type->preserve[k].mask); - } - else - { - return type->map[k].mods.mask; - } - } - } - return MODIFIERS_NONE; + int nKeyGroups; + int effectiveGroup; + XkbKeyTypeRec* type; + int k; + + nKeyGroups = XkbKeyNumGroups(xkb, key); + if ((!XkbKeycodeInRange(xkb, key)) || (nKeyGroups == 0)) { + return MODIFIERS_ERROR; + } + + /* Taken from GDK's MyEnhancedXkbTranslateKeyCode */ + /* find the offset of the effective group */ + effectiveGroup = group; + if (effectiveGroup >= nKeyGroups) { + unsigned groupInfo = XkbKeyGroupInfo(xkb, key); + switch (XkbOutOfRangeGroupAction(groupInfo)) { + default: + effectiveGroup %= nKeyGroups; + break; + case XkbClampIntoRange: + effectiveGroup = nKeyGroups - 1; + break; + case XkbRedirectIntoRange: + effectiveGroup = XkbOutOfRangeGroupNumber(groupInfo); + if (effectiveGroup >= nKeyGroups) + effectiveGroup = 0; + break; + } + } + type = XkbKeyKeyType(xkb, key, effectiveGroup); + for (k = 0; k < type->map_count; k++) { + if (type->map[k].active && type->map[k].level == level) { + if (type->preserve) { + return (type->map[k].mods.mask & ~type->preserve[k].mask); + } else { + return type->map[k].mods.mask; + } + } + } + return MODIFIERS_NONE; } /* Grab or ungrab the keycode+modifiers combination, first plainly, and then * including each ignorable modifier in turn. */ static gboolean -grab_ungrab_with_ignorable_modifiers (GdkWindow *rootwin, - uint keycode, - uint modifiers, - gboolean grab) +grab_ungrab_with_ignorable_modifiers(GdkWindow* rootwin, + uint keycode, + uint modifiers, + gboolean grab) { - guint i; - gboolean success = FALSE; - GdkDisplay *display = gdk_display_get_default(); - - /* Ignorable modifiers */ - guint mod_masks [] = - { - 0, /* modifier only */ - GDK_MOD2_MASK, - GDK_LOCK_MASK, - GDK_MOD2_MASK | GDK_LOCK_MASK, - }; - - gdk_x11_display_error_trap_push (display); - - for (i = 0; i < G_N_ELEMENTS (mod_masks); i++) - { - if (grab) - { - XGrabKey (GDK_WINDOW_XDISPLAY (rootwin), - keycode, - modifiers | mod_masks [i], - GDK_WINDOW_XID (rootwin), - True, - GrabModeSync, - GrabModeSync); - } - else - { - XUngrabKey (GDK_WINDOW_XDISPLAY (rootwin), - keycode, - modifiers | mod_masks [i], - GDK_WINDOW_XID (rootwin)); - } - } - gdk_display_flush(display); - if (gdk_x11_display_error_trap_pop(display)) - { - TRACE (g_warning ("Failed grab/ungrab")); - if (grab) - { - /* On error, immediately release keys again */ - grab_ungrab_with_ignorable_modifiers(rootwin, - keycode, - modifiers, - FALSE); - } - } - else - { - success = TRUE; - } - return success; + guint i; + gboolean success = FALSE; + GdkDisplay* display = gdk_display_get_default(); + + /* Ignorable modifiers */ + guint mod_masks[] = { + 0, /* modifier only */ + GDK_MOD2_MASK, + GDK_LOCK_MASK, + GDK_MOD2_MASK | GDK_LOCK_MASK, + }; + + gdk_x11_display_error_trap_push(display); + + for (i = 0; i < G_N_ELEMENTS(mod_masks); i++) { + if (grab) { + XGrabKey(GDK_WINDOW_XDISPLAY(rootwin), + keycode, + modifiers | mod_masks[i], + GDK_WINDOW_XID(rootwin), + True, + GrabModeSync, + GrabModeSync); + } else { + XUngrabKey(GDK_WINDOW_XDISPLAY(rootwin), + keycode, + modifiers | mod_masks[i], + GDK_WINDOW_XID(rootwin)); + } + } + gdk_display_flush(display); + if (gdk_x11_display_error_trap_pop(display)) { + TRACE(g_warning("Failed grab/ungrab")); + if (grab) { + /* On error, immediately release keys again */ + grab_ungrab_with_ignorable_modifiers(rootwin, + keycode, + modifiers, + FALSE); + } + } else { + success = TRUE; + } + return success; } /* Grab or ungrab then keyval and modifiers combination, grabbing all key @@ -203,285 +185,261 @@ grab_ungrab_with_ignorable_modifiers (GdkWindow *rootwin, * Includes ignorable modifiers using grab_ungrab_with_ignorable_modifiers. */ static gboolean -grab_ungrab (GdkWindow *rootwin, - uint keyval, - uint modifiers, - gboolean grab) +grab_ungrab(GdkWindow* rootwin, + uint keyval, + uint modifiers, + gboolean grab) { - int k; - GdkKeymap *map; - GdkKeymapKey *keys; - gint n_keys; - GdkModifierType add_modifiers; - XkbDescPtr xmap; - gboolean success = FALSE; - - xmap = XkbGetMap(GDK_WINDOW_XDISPLAY(rootwin), - XkbAllClientInfoMask, - XkbUseCoreKbd); - - GdkDisplay *display = gdk_display_get_default(); - map = gdk_keymap_get_for_display(display); - gdk_keymap_get_entries_for_keyval(map, keyval, &keys, &n_keys); - - if (n_keys == 0) - return FALSE; - - for (k = 0; k < n_keys; k++) - { - /* NOTE: We only bind for the first group, - * so regardless of current keyboard layout, it will - * grab the key from the default Layout. - */ - if (keys[k].group != WE_ONLY_USE_ONE_GROUP) - { - continue; - } - - add_modifiers = FinallyGetModifiersForKeycode(xmap, - keys[k].keycode, - keys[k].group, - keys[k].level); - - if (add_modifiers == MODIFIERS_ERROR) - { - continue; - } - TRACE (g_print("grab/ungrab keycode: %d, lev: %d, grp: %d, ", - keys[k].keycode, keys[k].level, keys[k].group)); - TRACE (g_print("modifiers: 0x%x (consumed: 0x%x)\n", - add_modifiers | modifiers, add_modifiers)); - if (grab_ungrab_with_ignorable_modifiers(rootwin, - keys[k].keycode, - add_modifiers | modifiers, - grab)) - { - - success = TRUE; - } - else - { - /* When grabbing, break on error */ - if (grab && !success) - { - break; - } - } - - } - g_free(keys); - XkbFreeClientMap(xmap, 0, TRUE); - - return success; + int k; + GdkKeymap* map; + GdkKeymapKey* keys; + gint n_keys; + GdkModifierType add_modifiers; + XkbDescPtr xmap; + gboolean success = FALSE; + + xmap = XkbGetMap(GDK_WINDOW_XDISPLAY(rootwin), + XkbAllClientInfoMask, + XkbUseCoreKbd); + + GdkDisplay* display = gdk_display_get_default(); + map = gdk_keymap_get_for_display(display); + gdk_keymap_get_entries_for_keyval(map, keyval, &keys, &n_keys); + + if (n_keys == 0) + return FALSE; + + for (k = 0; k < n_keys; k++) { + /* NOTE: We only bind for the first group, + * so regardless of current keyboard layout, it will + * grab the key from the default Layout. + */ + if (keys[k].group != WE_ONLY_USE_ONE_GROUP) { + continue; + } + + add_modifiers = FinallyGetModifiersForKeycode(xmap, + keys[k].keycode, + keys[k].group, + keys[k].level); + + if (add_modifiers == MODIFIERS_ERROR) { + continue; + } + TRACE(g_print("grab/ungrab keycode: %d, lev: %d, grp: %d, ", + keys[k].keycode, keys[k].level, keys[k].group)); + TRACE(g_print("modifiers: 0x%x (consumed: 0x%x)\n", + add_modifiers | modifiers, add_modifiers)); + if (grab_ungrab_with_ignorable_modifiers(rootwin, + keys[k].keycode, + add_modifiers | modifiers, + grab)) { + + success = TRUE; + } else { + /* When grabbing, break on error */ + if (grab && !success) { + break; + } + } + } + g_free(keys); + XkbFreeClientMap(xmap, 0, TRUE); + + return success; } static gboolean -keyvalues_equal (guint kv1, guint kv2) +keyvalues_equal(guint kv1, guint kv2) { - return kv1 == kv2; + return kv1 == kv2; } /* Compare modifier set equality, * while accepting overloaded modifiers (MOD1 and META together) */ static gboolean -modifiers_equal (GdkModifierType mf1, GdkModifierType mf2) +modifiers_equal(GdkModifierType mf1, GdkModifierType mf2) { - GdkModifierType ignored = 0; - - /* Accept MOD1 + META as MOD1 */ - if (mf1 & mf2 & GDK_MOD1_MASK) - { - ignored |= GDK_META_MASK; - } - /* Accept SUPER + HYPER as SUPER */ - if (mf1 & mf2 & GDK_SUPER_MASK) - { - ignored |= GDK_HYPER_MASK; - } - if ((mf1 & ~ignored) == (mf2 & ~ignored)) - { - return TRUE; - } - return FALSE; + GdkModifierType ignored = 0; + + /* Accept MOD1 + META as MOD1 */ + if (mf1 & mf2 & GDK_MOD1_MASK) { + ignored |= GDK_META_MASK; + } + /* Accept SUPER + HYPER as SUPER */ + if (mf1 & mf2 & GDK_SUPER_MASK) { + ignored |= GDK_HYPER_MASK; + } + if ((mf1 & ~ignored) == (mf2 & ~ignored)) { + return TRUE; + } + return FALSE; } static gboolean -do_grab_key (struct Binding *binding) +do_grab_key(struct Binding* binding) { - gboolean success; - GdkWindow *rootwin = gdk_get_default_root_window (); - GdkDisplay *display = gdk_display_get_default(); - GdkKeymap *keymap = gdk_keymap_get_for_display (display); + gboolean success; + GdkWindow* rootwin = gdk_get_default_root_window(); + GdkDisplay* display = gdk_display_get_default(); + GdkKeymap* keymap = gdk_keymap_get_for_display(display); + GdkModifierType modifiers; + guint keysym = 0; - GdkModifierType modifiers; - guint keysym = 0; + if (keymap == NULL || rootwin == NULL) { + return FALSE; + } - if (keymap == NULL || rootwin == NULL) - { - return FALSE; - } + gtk_accelerator_parse(binding->keystring, &keysym, &modifiers); - gtk_accelerator_parse(binding->keystring, &keysym, &modifiers); + if (keysym == 0) { + return FALSE; + } - if (keysym == 0) - { - return FALSE; - } + binding->keyval = keysym; + binding->modifiers = modifiers; + TRACE(g_print("Grabbing keyval: %d, vmodifiers: 0x%x, name: %s\n", + keysym, modifiers, binding->keystring)); - binding->keyval = keysym; - binding->modifiers = modifiers; - TRACE (g_print ("Grabbing keyval: %d, vmodifiers: 0x%x, name: %s\n", - keysym, modifiers, binding->keystring)); + /* Map virtual modifiers to non-virtual modifiers */ + gdk_keymap_map_virtual_modifiers(keymap, &modifiers); - /* Map virtual modifiers to non-virtual modifiers */ - gdk_keymap_map_virtual_modifiers(keymap, &modifiers); + if (modifiers == binding->modifiers && (GDK_SUPER_MASK | GDK_HYPER_MASK | GDK_META_MASK) & modifiers) { + g_warning("Failed to map virtual modifiers"); + return FALSE; + } - if (modifiers == binding->modifiers && - (GDK_SUPER_MASK | GDK_HYPER_MASK | GDK_META_MASK) & modifiers) - { - g_warning ("Failed to map virtual modifiers"); - return FALSE; - } + success = grab_ungrab(rootwin, keysym, modifiers, TRUE /* grab */); - success = grab_ungrab (rootwin, keysym, modifiers, TRUE /* grab */); + if (!success) { + g_warning("Binding '%s' failed!", binding->keystring); + } - if (!success) - { - g_warning ("Binding '%s' failed!", binding->keystring); - } - - return success; + return success; } static gboolean -do_ungrab_key (struct Binding *binding) +do_ungrab_key(struct Binding* binding) { - GdkDisplay *display = gdk_display_get_default(); - GdkKeymap *keymap = gdk_keymap_get_for_display (display); - GdkWindow *rootwin = gdk_get_default_root_window (); - GdkModifierType modifiers; + GdkDisplay* display = gdk_display_get_default(); + GdkKeymap* keymap = gdk_keymap_get_for_display(display); + GdkWindow* rootwin = gdk_get_default_root_window(); + GdkModifierType modifiers; - if (keymap == NULL || rootwin == NULL) - { - return FALSE; - } + if (keymap == NULL || rootwin == NULL) { + return FALSE; + } - TRACE (g_print ("Ungrabbing keyval: %d, vmodifiers: 0x%x, name: %s\n", - binding->keyval, binding->modifiers, binding->keystring)); + TRACE(g_print("Ungrabbing keyval: %d, vmodifiers: 0x%x, name: %s\n", + binding->keyval, binding->modifiers, binding->keystring)); - /* Map virtual modifiers to non-virtual modifiers */ - modifiers = binding->modifiers; - gdk_keymap_map_virtual_modifiers(keymap, &modifiers); + /* Map virtual modifiers to non-virtual modifiers */ + modifiers = binding->modifiers; + gdk_keymap_map_virtual_modifiers(keymap, &modifiers); - grab_ungrab (rootwin, binding->keyval, modifiers, FALSE /* ungrab */); - return TRUE; + grab_ungrab(rootwin, binding->keyval, modifiers, FALSE /* ungrab */); + return TRUE; } static GdkFilterReturn -filter_func (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data) +filter_func(GdkXEvent* gdk_xevent, GdkEvent* event, gpointer data) { - XEvent *xevent = (XEvent *) gdk_xevent; - GdkDisplay *display = gdk_display_get_default(); - GdkKeymap *keymap = gdk_keymap_get_for_display(display); - guint keyval; - GdkModifierType consumed, modifiers; - guint mod_mask = gtk_accelerator_get_default_mod_mask(); - GSList *iter; - GdkWindow *rootwin = data; - - //(void) event; - (void) data; - - switch (xevent->type) - { - case KeyPress: - modifiers = xevent->xkey.state; - - TRACE (g_print ("Got KeyPress keycode: %d, modifiers: 0x%x\n", - xevent->xkey.keycode, - xevent->xkey.state)); - - gdk_keymap_translate_keyboard_state( - keymap, - xevent->xkey.keycode, - modifiers, - /* See top comment why we don't use this here: - XkbGroupForCoreState (xevent->xkey.state) - */ - WE_ONLY_USE_ONE_GROUP, - &keyval, NULL, NULL, &consumed); - - /* Map non-virtual to virtual modifiers */ - modifiers &= ~consumed; - gdk_keymap_add_virtual_modifiers(keymap, &modifiers); - modifiers &= mod_mask; - - TRACE (g_print ("Translated keyval: %d, vmodifiers: 0x%x, name: %s\n", - keyval, modifiers, - gtk_accelerator_name(keyval, modifiers))); - - /* - * Set the last event time for use when showing - * windows to avoid anti-focus-stealing code. - */ - processing_event = TRUE; - last_event_time = xevent->xkey.time; - - iter = bindings; - while (iter != NULL) - { - /* NOTE: ``iter`` might be removed from the list - * in the callback. - */ - struct Binding *binding = iter->data; - iter = iter->next; - - if (keyvalues_equal(binding->keyval, keyval) && - modifiers_equal(binding->modifiers, modifiers)) - { - TRACE (g_print ("Calling handler for '%s'...\n", - binding->keystring)); - - (binding->handler) (binding->keystring, - binding->user_data); - } - } - - processing_event = FALSE; - break; - case KeyRelease: - TRACE (g_print ("Got KeyRelease! \n")); - break; - } - XAllowEvents(GDK_WINDOW_XDISPLAY(rootwin), - ReplayKeyboard, xevent->xkey.time); - XFlush(GDK_WINDOW_XDISPLAY(rootwin)); - - return GDK_FILTER_CONTINUE; + XEvent* xevent = (XEvent*)gdk_xevent; + GdkDisplay* display = gdk_display_get_default(); + GdkKeymap* keymap = gdk_keymap_get_for_display(display); + guint keyval; + GdkModifierType consumed, modifiers; + guint mod_mask = gtk_accelerator_get_default_mod_mask(); + GSList* iter; + GdkWindow* rootwin = data; + + //(void) event; + (void)data; + + switch (xevent->type) { + case KeyPress: + modifiers = xevent->xkey.state; + + TRACE(g_print("Got KeyPress keycode: %d, modifiers: 0x%x\n", + xevent->xkey.keycode, + xevent->xkey.state)); + + gdk_keymap_translate_keyboard_state( + keymap, + xevent->xkey.keycode, + modifiers, + /* See top comment why we don't use this here: + XkbGroupForCoreState (xevent->xkey.state) + */ + WE_ONLY_USE_ONE_GROUP, + &keyval, NULL, NULL, &consumed); + + /* Map non-virtual to virtual modifiers */ + modifiers &= ~consumed; + gdk_keymap_add_virtual_modifiers(keymap, &modifiers); + modifiers &= mod_mask; + + TRACE(g_print("Translated keyval: %d, vmodifiers: 0x%x, name: %s\n", + keyval, modifiers, + gtk_accelerator_name(keyval, modifiers))); + + /* + * Set the last event time for use when showing + * windows to avoid anti-focus-stealing code. + */ + processing_event = TRUE; + last_event_time = xevent->xkey.time; + + iter = bindings; + while (iter != NULL) { + /* NOTE: ``iter`` might be removed from the list + * in the callback. + */ + struct Binding* binding = iter->data; + iter = iter->next; + + if (keyvalues_equal(binding->keyval, keyval) && modifiers_equal(binding->modifiers, modifiers)) { + TRACE(g_print("Calling handler for '%s'...\n", + binding->keystring)); + + (binding->handler)(binding->keystring, + binding->user_data); + } + } + + processing_event = FALSE; + break; + case KeyRelease: + TRACE(g_print("Got KeyRelease! \n")); + break; + } + XAllowEvents(GDK_WINDOW_XDISPLAY(rootwin), + ReplayKeyboard, xevent->xkey.time); + XFlush(GDK_WINDOW_XDISPLAY(rootwin)); + + return GDK_FILTER_CONTINUE; } static void -keymap_changed (GdkKeymap *map) +keymap_changed(GdkKeymap* map) { - GSList *iter; + GSList* iter; - (void) map; + (void)map; - TRACE (g_print ("Keymap changed! Regrabbing keys...")); + TRACE(g_print("Keymap changed! Regrabbing keys...")); - for (iter = bindings; iter != NULL; iter = iter->next) - { - struct Binding *binding = iter->data; - do_ungrab_key (binding); - } + for (iter = bindings; iter != NULL; iter = iter->next) { + struct Binding* binding = iter->data; + do_ungrab_key(binding); + } - for (iter = bindings; iter != NULL; iter = iter->next) - { - struct Binding *binding = iter->data; - do_grab_key (binding); - } + for (iter = bindings; iter != NULL; iter = iter->next) { + struct Binding* binding = iter->data; + do_grab_key(binding); + } } /** @@ -492,27 +450,25 @@ keymap_changed (GdkKeymap *map) * This function must be called after initializing GTK, before calling any * other function in the library. Can only be called once. */ -void -keybinder_init () +void keybinder_init() { - GdkDisplay *display = gdk_display_get_default(); - GdkKeymap *keymap = gdk_keymap_get_for_display (display); - GdkWindow *rootwin = gdk_get_default_root_window (); - - gdk_window_add_filter (rootwin, filter_func, rootwin); - - /* Workaround: Make sure modmap is up to date - * There is possibly a bug in GTK+ where virtual modifiers are not - * mapped because the modmap is not updated. The following function - * updates it. - */ - (void) gdk_keymap_have_bidi_layouts(keymap); - - - g_signal_connect (keymap, - "keys_changed", - G_CALLBACK (keymap_changed), - NULL); + GdkDisplay* display = gdk_display_get_default(); + GdkKeymap* keymap = gdk_keymap_get_for_display(display); + GdkWindow* rootwin = gdk_get_default_root_window(); + + gdk_window_add_filter(rootwin, filter_func, rootwin); + + /* Workaround: Make sure modmap is up to date + * There is possibly a bug in GTK+ where virtual modifiers are not + * mapped because the modmap is not updated. The following function + * updates it. + */ + (void)gdk_keymap_have_bidi_layouts(keymap); + + g_signal_connect(keymap, + "keys_changed", + G_CALLBACK(keymap_changed), + NULL); } /** @@ -530,11 +486,11 @@ keybinder_init () * Returns: %TRUE if the accelerator could be grabbed */ gboolean -keybinder_bind (const char *keystring, - KeybinderHandler handler, - void *user_data) +keybinder_bind(const char* keystring, + KeybinderHandler handler, + void* user_data) { - return keybinder_bind_full(keystring, handler, user_data, NULL); + return keybinder_bind_full(keystring, handler, user_data, NULL); } /** @@ -554,33 +510,30 @@ keybinder_bind (const char *keystring, * Returns: %TRUE if the accelerator could be grabbed */ gboolean -keybinder_bind_full (const char *keystring, - KeybinderHandler handler, - void *user_data, - GDestroyNotify notify) +keybinder_bind_full(const char* keystring, + KeybinderHandler handler, + void* user_data, + GDestroyNotify notify) { - struct Binding *binding; - gboolean success; - - binding = g_new0 (struct Binding, 1); - binding->keystring = g_strdup (keystring); - binding->handler = handler; - binding->user_data = user_data; - binding->notify = notify; - - /* Sets the binding's keycode and modifiers */ - success = do_grab_key (binding); - - if (success) - { - bindings = g_slist_prepend (bindings, binding); - } - else - { - g_free (binding->keystring); - g_free (binding); - } - return success; + struct Binding* binding; + gboolean success; + + binding = g_new0(struct Binding, 1); + binding->keystring = g_strdup(keystring); + binding->handler = handler; + binding->user_data = user_data; + binding->notify = notify; + + /* Sets the binding's keycode and modifiers */ + success = do_grab_key(binding); + + if (success) { + bindings = g_slist_prepend(bindings, binding); + } else { + g_free(binding->keystring); + g_free(binding); + } + return success; } /** @@ -593,31 +546,27 @@ keybinder_bind_full (const char *keystring, * This function is excluded from introspected bindings and is replaced by * keybinder_unbind_all. */ -void -keybinder_unbind (const char *keystring, KeybinderHandler handler) +void keybinder_unbind(const char* keystring, KeybinderHandler handler) { - GSList *iter; - - for (iter = bindings; iter != NULL; iter = iter->next) - { - struct Binding *binding = iter->data; - - if (strcmp (keystring, binding->keystring) != 0 || - handler != binding->handler) - continue; - - do_ungrab_key (binding); - bindings = g_slist_remove (bindings, binding); - - TRACE (g_print("unbind, notify: %p\n", binding->notify)); - if (binding->notify) - { - binding->notify(binding->user_data); - } - g_free (binding->keystring); - g_free (binding); - break; - } + GSList* iter; + + for (iter = bindings; iter != NULL; iter = iter->next) { + struct Binding* binding = iter->data; + + if (strcmp(keystring, binding->keystring) != 0 || handler != binding->handler) + continue; + + do_ungrab_key(binding); + bindings = g_slist_remove(bindings, binding); + + TRACE(g_print("unbind, notify: %p\n", binding->notify)); + if (binding->notify) { + binding->notify(binding->user_data); + } + g_free(binding->keystring); + g_free(binding); + break; + } } /** @@ -630,35 +579,32 @@ keybinder_unbind (const char *keystring, KeybinderHandler handler) * * Since: 0.3.0 */ -void keybinder_unbind_all (const char *keystring) +void keybinder_unbind_all(const char* keystring) { - GSList *iter = bindings; - - for (iter = bindings; iter != NULL; iter = iter->next) - { - struct Binding *binding = iter->data; - - if (strcmp (keystring, binding->keystring) != 0) - { - continue; - } - - do_ungrab_key (binding); - bindings = g_slist_remove (bindings, binding); - - TRACE (g_print("unbind_all, notify: %p\n", binding->notify)); - if (binding->notify) - { - binding->notify(binding->user_data); - } - g_free (binding->keystring); - g_free (binding); - - /* re-start scan from head of new list */ - iter = bindings; - if (!iter) - break; - } + GSList* iter = bindings; + + for (iter = bindings; iter != NULL; iter = iter->next) { + struct Binding* binding = iter->data; + + if (strcmp(keystring, binding->keystring) != 0) { + continue; + } + + do_ungrab_key(binding); + bindings = g_slist_remove(bindings, binding); + + TRACE(g_print("unbind_all, notify: %p\n", binding->notify)); + if (binding->notify) { + binding->notify(binding->user_data); + } + g_free(binding->keystring); + g_free(binding); + + /* re-start scan from head of new list */ + iter = bindings; + if (!iter) + break; + } } /** @@ -667,14 +613,11 @@ void keybinder_unbind_all (const char *keystring) * Returns: the current event timestamp */ guint32 -keybinder_get_current_event_time (void) +keybinder_get_current_event_time(void) { - if (processing_event) - { - return last_event_time; - } - else - { - return GDK_CURRENT_TIME; - } + if (processing_event) { + return last_event_time; + } else { + return GDK_CURRENT_TIME; + } } diff --git a/src/bind.h b/src/bind.h index ca34e06..cdf4c69 100644 --- a/src/bind.h +++ b/src/bind.h @@ -28,26 +28,26 @@ G_BEGIN_DECLS -typedef void (* KeybinderHandler) (const char *keystring, void *user_data); +typedef void (*KeybinderHandler)(const char* keystring, void* user_data); -void keybinder_init (void); +void keybinder_init(void); -gboolean keybinder_bind (const char *keystring, - KeybinderHandler handler, - void *user_data); +gboolean keybinder_bind(const char* keystring, + KeybinderHandler handler, + void* user_data); gboolean -keybinder_bind_full (const char *keystring, - KeybinderHandler handler, - void *user_data, - GDestroyNotify notify); +keybinder_bind_full(const char* keystring, + KeybinderHandler handler, + void* user_data, + GDestroyNotify notify); -void keybinder_unbind (const char *keystring, - KeybinderHandler handler); +void keybinder_unbind(const char* keystring, + KeybinderHandler handler); -void keybinder_unbind_all (const char *keystring); +void keybinder_unbind_all(const char* keystring); -guint32 keybinder_get_current_event_time (void); +guint32 keybinder_get_current_event_time(void); G_END_DECLS diff --git a/src/component/best-sum.c b/src/component/best-sum.c index 0378b1c..561b31e 100644 --- a/src/component/best-sum.c +++ b/src/component/best-sum.c @@ -1,23 +1,21 @@ #include "components.h" -typedef struct _LSBestSum -{ +typedef struct _LSBestSum { LSComponent base; - GtkWidget *container; - GtkWidget *sum_of_bests; + GtkWidget* container; + GtkWidget* sum_of_bests; } LSBestSum; extern LSComponentOps ls_best_sum_operations; #define SUM_OF_BEST_SEGMENTS "Sum of best segments" -LSComponent *ls_component_best_sum_new() +LSComponent* ls_component_best_sum_new() { - LSBestSum *self; - GtkWidget *label; + LSBestSum* self; + GtkWidget* label; self = malloc(sizeof(LSBestSum)); - if (!self) - { + if (!self) { return NULL; } self->base.ops = &ls_best_sum_operations; @@ -39,54 +37,51 @@ LSComponent *ls_component_best_sum_new() gtk_container_add(GTK_CONTAINER(self->container), self->sum_of_bests); gtk_widget_show(self->sum_of_bests); - return (LSComponent *)self; + return (LSComponent*)self; } -static void best_sum_delete(LSComponent *self) +static void best_sum_delete(LSComponent* self) { free(self); } -static GtkWidget *best_sum_widget(LSComponent *self) +static GtkWidget* best_sum_widget(LSComponent* self) { - return ((LSBestSum *)self)->container; + return ((LSBestSum*)self)->container; } -static void best_sum_show_game(LSComponent *self_, - ls_game *game, ls_timer *timer) +static void best_sum_show_game(LSComponent* self_, + ls_game* game, ls_timer* timer) { - LSBestSum *self = (LSBestSum *)self_; + LSBestSum* self = (LSBestSum*)self_; char str[256]; - if (game->split_count && timer->sum_of_bests) - { + if (game->split_count && timer->sum_of_bests) { ls_time_string(str, timer->sum_of_bests); gtk_label_set_text(GTK_LABEL(self->sum_of_bests), str); } } -static void best_sum_clear_game(LSComponent *self_) +static void best_sum_clear_game(LSComponent* self_) { - LSBestSum *self = (LSBestSum *)self_; + LSBestSum* self = (LSBestSum*)self_; gtk_label_set_text(GTK_LABEL(self->sum_of_bests), ""); } -static void best_sum_draw(LSComponent *self_, ls_game *game, - ls_timer *timer) +static void best_sum_draw(LSComponent* self_, ls_game* game, + ls_timer* timer) { - LSBestSum *self = (LSBestSum *)self_; + LSBestSum* self = (LSBestSum*)self_; char str[256]; remove_class(self->sum_of_bests, "time"); gtk_label_set_text(GTK_LABEL(self->sum_of_bests), "-"); - if (timer->sum_of_bests) - { + if (timer->sum_of_bests) { add_class(self->sum_of_bests, "time"); ls_time_string(str, timer->sum_of_bests); gtk_label_set_text(GTK_LABEL(self->sum_of_bests), str); } } -LSComponentOps ls_best_sum_operations = -{ +LSComponentOps ls_best_sum_operations = { .delete = best_sum_delete, .widget = best_sum_widget, .show_game = best_sum_show_game, diff --git a/src/component/clock.c b/src/component/clock.c index 0801bd3..ebb5480 100644 --- a/src/component/clock.c +++ b/src/component/clock.c @@ -1,22 +1,20 @@ #include "components.h" -typedef struct _LSTimer -{ +typedef struct _LSTimer { LSComponent base; - GtkWidget *time; - GtkWidget *time_seconds; - GtkWidget *time_millis; + GtkWidget* time; + GtkWidget* time_seconds; + GtkWidget* time_millis; } LSTimer; extern LSComponentOps ls_timer_operations; -LSComponent *ls_component_timer_new() +LSComponent* ls_component_timer_new() { - LSTimer *self; - GtkWidget *spacer; + LSTimer* self; + GtkWidget* spacer; self = malloc(sizeof(LSTimer)); - if (!self) - { + if (!self) { return NULL; } self->base.ops = &ls_timer_operations; @@ -48,40 +46,37 @@ LSComponent *ls_component_timer_new() gtk_container_add(GTK_CONTAINER(spacer), self->time_millis); gtk_widget_show(self->time_millis); - - return (LSComponent *)self; + return (LSComponent*)self; } // Avoid collision with timer_delete of time.h -static void ls_timer_delete(LSComponent *self) +static void ls_timer_delete(LSComponent* self) { free(self); } -static GtkWidget *timer_widget(LSComponent *self) +static GtkWidget* timer_widget(LSComponent* self) { - return ((LSTimer *)self)->time; + return ((LSTimer*)self)->time; } -static void timer_clear_game(LSComponent *self_) +static void timer_clear_game(LSComponent* self_) { - LSTimer *self = (LSTimer *)self_; + LSTimer* self = (LSTimer*)self_; gtk_label_set_text(GTK_LABEL(self->time_seconds), ""); gtk_label_set_text(GTK_LABEL(self->time_millis), ""); remove_class(self->time, "behind"); remove_class(self->time, "losing"); - } -static void timer_draw(LSComponent *self_, ls_game *game, ls_timer *timer) +static void timer_draw(LSComponent* self_, ls_game* game, ls_timer* timer) { - LSTimer *self = (LSTimer *)self_; + LSTimer* self = (LSTimer*)self_; char str[256], millis[256]; int curr; curr = timer->curr_split; - if (curr == game->split_count) - { + if (curr == game->split_count) { --curr; } @@ -90,32 +85,23 @@ static void timer_draw(LSComponent *self_, ls_game *game, ls_timer *timer) remove_class(self->time, "losing"); remove_class(self->time, "best-split"); - if (curr == game->split_count) - { + if (curr == game->split_count) { curr = game->split_count - 1; } - if (timer->time <= 0) - { + if (timer->time <= 0) { add_class(self->time, "delay"); - } - else - { + } else { if (timer->curr_split == game->split_count && timer->split_info[curr] - & LS_INFO_BEST_SPLIT) - { + & LS_INFO_BEST_SPLIT) { add_class(self->time, "best-split"); - } - else - { + } else { if (timer->split_info[curr] - & LS_INFO_BEHIND_TIME) - { + & LS_INFO_BEHIND_TIME) { add_class(self->time, "behind"); } if (timer->split_info[curr] - & LS_INFO_LOSING_TIME) - { + & LS_INFO_LOSING_TIME) { add_class(self->time, "losing"); } } @@ -126,8 +112,7 @@ static void timer_draw(LSComponent *self_, ls_game *game, ls_timer *timer) gtk_label_set_text(GTK_LABEL(self->time_millis), millis); } -LSComponentOps ls_timer_operations = -{ +LSComponentOps ls_timer_operations = { .delete = ls_timer_delete, .widget = timer_widget, .clear_game = timer_clear_game, diff --git a/src/component/components.c b/src/component/components.c index 97746e9..9ce4bc4 100644 --- a/src/component/components.c +++ b/src/component/components.c @@ -1,31 +1,30 @@ #include "components.h" -LSComponent *ls_component_title_new(); -LSComponent *ls_component_splits_new(); -LSComponent *ls_component_timer_new(); -LSComponent *ls_component_prev_segment_new(); -LSComponent *ls_component_best_sum_new(); -LSComponent *ls_component_pb_new(); -LSComponent *ls_component_wr_new(); +LSComponent* ls_component_title_new(); +LSComponent* ls_component_splits_new(); +LSComponent* ls_component_timer_new(); +LSComponent* ls_component_prev_segment_new(); +LSComponent* ls_component_best_sum_new(); +LSComponent* ls_component_pb_new(); +LSComponent* ls_component_wr_new(); -LSComponentAvailable ls_components[] = -{ - {"title", ls_component_title_new}, - {"splits", ls_component_splits_new}, - {"timer", ls_component_timer_new}, - {"prev-segment", ls_component_prev_segment_new}, - {"best-sum", ls_component_best_sum_new}, - {"pb", ls_component_pb_new}, - {"wr", ls_component_wr_new}, - {NULL, NULL} +LSComponentAvailable ls_components[] = { + { "title", ls_component_title_new }, + { "splits", ls_component_splits_new }, + { "timer", ls_component_timer_new }, + { "prev-segment", ls_component_prev_segment_new }, + { "best-sum", ls_component_best_sum_new }, + { "pb", ls_component_pb_new }, + { "wr", ls_component_wr_new }, + { NULL, NULL } }; -void add_class(GtkWidget *widget, const char *class) +void add_class(GtkWidget* widget, const char* class) { gtk_style_context_add_class(gtk_widget_get_style_context(widget), class); } -void remove_class(GtkWidget *widget, const char *class) +void remove_class(GtkWidget* widget, const char* class) { gtk_style_context_remove_class(gtk_widget_get_style_context(widget), class); } diff --git a/src/component/components.h b/src/component/components.h index 71587d5..53687be 100644 --- a/src/component/components.h +++ b/src/component/components.h @@ -1,10 +1,10 @@ #ifndef __COMPONENTS_H__ #define __COMPONENTS_H__ -#include -#include #include #include +#include +#include #include "../timer.h" @@ -12,40 +12,37 @@ typedef struct _LSComponent LSComponent; typedef struct _LSComponentOps LSComponentOps; typedef struct _LSComponentAvailable LSComponentAvailable; -struct _LSComponent -{ - LSComponentOps *ops; +struct _LSComponent { + LSComponentOps* ops; }; -struct _LSComponentOps -{ - void (*delete)(LSComponent *self); - GtkWidget *(*widget)(LSComponent *self); - - void (*resize)(LSComponent *self, int win_width, int win_height); - void (*show_game)(LSComponent *self, ls_game *game, ls_timer *timer); - void (*clear_game)(LSComponent *self); - void (*draw)(LSComponent *self, ls_game *game, ls_timer *timer); - - void (*start_split)(LSComponent *self, ls_timer *timer); - void (*skip)(LSComponent *self, ls_timer *timer); - void (*unsplit)(LSComponent *self, ls_timer *timer); - void (*stop_reset)(LSComponent *self, ls_timer *timer); - void (*cancel_run)(LSComponent *self, ls_timer *timer); +struct _LSComponentOps { + void (*delete)(LSComponent* self); + GtkWidget* (*widget)(LSComponent* self); + + void (*resize)(LSComponent* self, int win_width, int win_height); + void (*show_game)(LSComponent* self, ls_game* game, ls_timer* timer); + void (*clear_game)(LSComponent* self); + void (*draw)(LSComponent* self, ls_game* game, ls_timer* timer); + + void (*start_split)(LSComponent* self, ls_timer* timer); + void (*skip)(LSComponent* self, ls_timer* timer); + void (*unsplit)(LSComponent* self, ls_timer* timer); + void (*stop_reset)(LSComponent* self, ls_timer* timer); + void (*cancel_run)(LSComponent* self, ls_timer* timer); }; -struct _LSComponentAvailable -{ - char *name; - LSComponent *(*new)(); +struct _LSComponentAvailable { + char* name; + LSComponent* (*new)(); }; // A NULL-terminated array of all available components extern LSComponentAvailable ls_components[]; // Utility functions -void add_class(GtkWidget *widget, const char *class); +void add_class(GtkWidget* widget, const char* class); -void remove_class(GtkWidget *widget, const char *class); +void remove_class(GtkWidget* widget, const char* class); #endif /* __COMPONENTS_H__ */ diff --git a/src/component/pb.c b/src/component/pb.c index 16e7414..656efd3 100644 --- a/src/component/pb.c +++ b/src/component/pb.c @@ -1,23 +1,21 @@ #include "components.h" -typedef struct _LSPb -{ +typedef struct _LSPb { LSComponent base; - GtkWidget *container; - GtkWidget *personal_best; + GtkWidget* container; + GtkWidget* personal_best; } LSPb; extern LSComponentOps ls_pb_operations; #define PERSONAL_BEST "Personal best" -LSComponent *ls_component_pb_new() +LSComponent* ls_component_pb_new() { - LSPb *self; - GtkWidget *label; + LSPb* self; + GtkWidget* label; self = malloc(sizeof(LSPb)); - if (!self) - { + if (!self) { return NULL; } self->base.ops = &ls_pb_operations; @@ -39,46 +37,43 @@ LSComponent *ls_component_pb_new() gtk_container_add(GTK_CONTAINER(self->container), self->personal_best); gtk_widget_show(self->personal_best); - return (LSComponent *)self; + return (LSComponent*)self; } -static void pb_delete(LSComponent *self) +static void pb_delete(LSComponent* self) { free(self); } -static GtkWidget *pb_widget(LSComponent *self) +static GtkWidget* pb_widget(LSComponent* self) { - return ((LSPb *)self)->container; + return ((LSPb*)self)->container; } -static void pb_show_game(LSComponent *self_, - ls_game *game, ls_timer *timer) +static void pb_show_game(LSComponent* self_, + ls_game* game, ls_timer* timer) { - LSPb *self = (LSPb *)self_; + LSPb* self = (LSPb*)self_; char str[256]; - if (game->split_count && game->split_times[game->split_count - 1]) - { - if (game->split_times[game->split_count - 1]) - { + if (game->split_count && game->split_times[game->split_count - 1]) { + if (game->split_times[game->split_count - 1]) { ls_time_string( str, game->split_times[game->split_count - 1]); gtk_label_set_text(GTK_LABEL(self->personal_best), str); } } - } -static void pb_clear_game(LSComponent *self_) +static void pb_clear_game(LSComponent* self_) { - LSPb *self = (LSPb *)self_; + LSPb* self = (LSPb*)self_; gtk_label_set_text(GTK_LABEL(self->personal_best), ""); } -static void pb_draw(LSComponent *self_, ls_game *game, - ls_timer *timer) +static void pb_draw(LSComponent* self_, ls_game* game, + ls_timer* timer) { - LSPb *self = (LSPb *)self_; + LSPb* self = (LSPb*)self_; char str[256]; remove_class(self->personal_best, "time"); gtk_label_set_text(GTK_LABEL(self->personal_best), "-"); @@ -86,15 +81,12 @@ static void pb_draw(LSComponent *self_, ls_game *game, && timer->split_times[game->split_count - 1] && (!game->split_times[game->split_count - 1] || (timer->split_times[game->split_count - 1] - < game->split_times[game->split_count - 1]))) - { + < game->split_times[game->split_count - 1]))) { add_class(self->personal_best, "time"); ls_time_string( str, timer->split_times[game->split_count - 1]); gtk_label_set_text(GTK_LABEL(self->personal_best), str); - } - else if (game->split_times[game->split_count - 1]) - { + } else if (game->split_times[game->split_count - 1]) { add_class(self->personal_best, "time"); ls_time_string( str, game->split_times[game->split_count - 1]); @@ -102,8 +94,7 @@ static void pb_draw(LSComponent *self_, ls_game *game, } } -LSComponentOps ls_pb_operations = -{ +LSComponentOps ls_pb_operations = { .delete = pb_delete, .widget = pb_widget, .show_game = pb_show_game, diff --git a/src/component/prev-segment.c b/src/component/prev-segment.c index 7c9d6e6..d406a58 100644 --- a/src/component/prev-segment.c +++ b/src/component/prev-segment.c @@ -1,24 +1,22 @@ #include "components.h" -typedef struct _LSPrevSegment -{ +typedef struct _LSPrevSegment { LSComponent base; - GtkWidget *container; - GtkWidget *previous_segment_label; - GtkWidget *previous_segment; + GtkWidget* container; + GtkWidget* previous_segment_label; + GtkWidget* previous_segment; } LSPrevSegment; extern LSComponentOps ls_prev_segment_operations; #define PREVIOUS_SEGMENT "Previous segment" -#define LIVE_SEGMENT "Live segment" +#define LIVE_SEGMENT "Live segment" -LSComponent *ls_component_prev_segment_new() +LSComponent* ls_component_prev_segment_new() { - LSPrevSegment *self; + LSPrevSegment* self; self = malloc(sizeof(LSPrevSegment)); - if (!self) - { + if (!self) { return NULL; } self->base.ops = &ls_prev_segment_operations; @@ -30,10 +28,10 @@ LSComponent *ls_component_prev_segment_new() self->previous_segment_label = gtk_label_new(PREVIOUS_SEGMENT); add_class(self->previous_segment_label, "prev-segment-label"); gtk_widget_set_halign(self->previous_segment_label, - GTK_ALIGN_START); + GTK_ALIGN_START); gtk_widget_set_hexpand(self->previous_segment_label, TRUE); gtk_container_add(GTK_CONTAINER(self->container), - self->previous_segment_label); + self->previous_segment_label); gtk_widget_show(self->previous_segment_label); self->previous_segment = gtk_label_new(NULL); @@ -42,45 +40,44 @@ LSComponent *ls_component_prev_segment_new() gtk_container_add(GTK_CONTAINER(self->container), self->previous_segment); gtk_widget_show(self->previous_segment); - return (LSComponent *)self; + return (LSComponent*)self; } -static void prev_segment_delete(LSComponent *self) +static void prev_segment_delete(LSComponent* self) { free(self); } -static GtkWidget *prev_segment_widget(LSComponent *self) +static GtkWidget* prev_segment_widget(LSComponent* self) { - return ((LSPrevSegment *)self)->container; + return ((LSPrevSegment*)self)->container; } -static void prev_segment_show_game(LSComponent *self_, - ls_game *game, ls_timer *timer) +static void prev_segment_show_game(LSComponent* self_, + ls_game* game, ls_timer* timer) { - LSPrevSegment *self = (LSPrevSegment *)self_; + LSPrevSegment* self = (LSPrevSegment*)self_; remove_class(self->previous_segment, "behind"); remove_class(self->previous_segment, "losing"); remove_class(self->previous_segment, "best-segment"); } -static void prev_segment_clear_game(LSComponent *self_) +static void prev_segment_clear_game(LSComponent* self_) { - LSPrevSegment *self = (LSPrevSegment *)self_; + LSPrevSegment* self = (LSPrevSegment*)self_; gtk_label_set_text(GTK_LABEL(self->previous_segment_label), - PREVIOUS_SEGMENT); + PREVIOUS_SEGMENT); gtk_label_set_text(GTK_LABEL(self->previous_segment), ""); } -static void prev_segment_draw(LSComponent *self_, ls_game *game, - ls_timer *timer) +static void prev_segment_draw(LSComponent* self_, ls_game* game, + ls_timer* timer) { - LSPrevSegment *self = (LSPrevSegment *)self_; - const char *label; + LSPrevSegment* self = (LSPrevSegment*)self_; + const char* label; char str[256]; int prev, curr = timer->curr_split; - if (curr == game->split_count) - { + if (curr == game->split_count) { --curr; } @@ -91,8 +88,7 @@ static void prev_segment_draw(LSComponent *self_, ls_game *game, gtk_label_set_text(GTK_LABEL(self->previous_segment), "-"); label = PREVIOUS_SEGMENT; - if (timer->segment_deltas[curr] > 0) - { + if (timer->segment_deltas[curr] > 0) { // Live segment label = LIVE_SEGMENT; remove_class(self->previous_segment, "best-segment"); @@ -101,23 +97,16 @@ static void prev_segment_draw(LSComponent *self_, ls_game *game, add_class(self->previous_segment, "delta"); ls_delta_string(str, timer->segment_deltas[curr]); gtk_label_set_text(GTK_LABEL(self->previous_segment), str); - } - else if (curr) - { + } else if (curr) { prev = timer->curr_split - 1; // Previous segment - if (timer->curr_split) - { + if (timer->curr_split) { prev = timer->curr_split - 1; - if (timer->segment_deltas[prev]) - { + if (timer->segment_deltas[prev]) { if (timer->split_info[prev] - & LS_INFO_BEST_SEGMENT) - { + & LS_INFO_BEST_SEGMENT) { add_class(self->previous_segment, "best-segment"); - } - else if (timer->segment_deltas[prev] > 0) - { + } else if (timer->segment_deltas[prev] > 0) { add_class(self->previous_segment, "behind"); add_class(self->previous_segment, "losing"); } @@ -130,8 +119,7 @@ static void prev_segment_draw(LSComponent *self_, ls_game *game, gtk_label_set_text(GTK_LABEL(self->previous_segment_label), label); } -LSComponentOps ls_prev_segment_operations = -{ +LSComponentOps ls_prev_segment_operations = { .delete = prev_segment_delete, .widget = prev_segment_widget, .show_game = prev_segment_show_game, diff --git a/src/component/splits.c b/src/component/splits.c index b6414a2..65aa577 100644 --- a/src/component/splits.c +++ b/src/component/splits.c @@ -1,29 +1,27 @@ #include "components.h" -typedef struct _LSSplits -{ +typedef struct _LSSplits { LSComponent base; int split_count; - GtkWidget *container; - GtkWidget *splits; - GtkWidget *split_last; - GtkAdjustment *split_adjust; - GtkWidget *split_scroller; - GtkWidget *split_viewport; - GtkWidget **split_rows; - GtkWidget **split_titles; - GtkWidget **split_deltas; - GtkWidget **split_times; + GtkWidget* container; + GtkWidget* splits; + GtkWidget* split_last; + GtkAdjustment* split_adjust; + GtkWidget* split_scroller; + GtkWidget* split_viewport; + GtkWidget** split_rows; + GtkWidget** split_titles; + GtkWidget** split_deltas; + GtkWidget** split_times; } LSSplits; extern LSComponentOps ls_splits_operations; -LSComponent *ls_component_splits_new() +LSComponent* ls_component_splits_new() { - LSSplits *self; + LSSplits* self; self = malloc(sizeof(LSSplits)); - if (!self) - { + if (!self) { return NULL; } self->base.ops = &ls_splits_operations; @@ -38,7 +36,7 @@ LSComponent *ls_component_splits_new() self->split_viewport = gtk_viewport_new(NULL, NULL); gtk_container_add(GTK_CONTAINER(self->split_scroller), - self->split_viewport); + self->split_viewport); gtk_widget_show(self->split_viewport); self->splits = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); @@ -46,7 +44,7 @@ LSComponent *ls_component_splits_new() gtk_widget_set_hexpand(self->splits, TRUE); gtk_container_add(GTK_CONTAINER(self->split_viewport), self->splits); gtk_widget_show(self->splits); - + self->split_last = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); add_class(self->split_last, "split-last"); gtk_widget_set_hexpand(self->split_last, TRUE); @@ -56,22 +54,22 @@ LSComponent *ls_component_splits_new() gtk_container_add(GTK_CONTAINER(self->container), self->split_scroller); gtk_container_add(GTK_CONTAINER(self->container), self->split_last); gtk_widget_show(self->container); - return (LSComponent *)self; + return (LSComponent*)self; } -static void splits_delete(LSComponent *self) +static void splits_delete(LSComponent* self) { free(self); } -static GtkWidget *splits_widget(LSComponent *self) +static GtkWidget* splits_widget(LSComponent* self) { - return ((LSSplits *)self)->container; + return ((LSSplits*)self)->container; } -static void splits_trailer(LSComponent *self_) +static void splits_trailer(LSComponent* self_) { - LSSplits *self = (LSSplits *)self_; + LSSplits* self = (LSSplits*)self_; int height, split_h, last = self->split_count - 1; double curr_scroll = gtk_adjustment_get_value(self->split_adjust); double scroll_max = gtk_adjustment_get_upper(self->split_adjust); @@ -79,100 +77,87 @@ static void splits_trailer(LSComponent *self_) g_object_ref(self->split_rows[last]); split_h = gtk_widget_get_allocated_height(self->split_titles[last]); height = gtk_widget_get_allocated_height(self->splits); - if (gtk_widget_get_parent(self->split_rows[last]) == self->splits) - { - if (curr_scroll + page_size < scroll_max) - { + if (gtk_widget_get_parent(self->split_rows[last]) == self->splits) { + if (curr_scroll + page_size < scroll_max) { // move last split to split_last gtk_container_remove(GTK_CONTAINER(self->splits), - self->split_rows[last]); + self->split_rows[last]); gtk_container_add(GTK_CONTAINER(self->split_last), - self->split_rows[last]); + self->split_rows[last]); gtk_widget_show(self->split_last); } - } - else - { - if (curr_scroll + page_size == scroll_max) - { + } else { + if (curr_scroll + page_size == scroll_max) { // move last split to split box gtk_container_remove(GTK_CONTAINER(self->split_last), - self->split_rows[last]); + self->split_rows[last]); gtk_container_add(GTK_CONTAINER(self->splits), - self->split_rows[last]); + self->split_rows[last]); gtk_adjustment_set_upper(self->split_adjust, - scroll_max + height); + scroll_max + height); gtk_adjustment_set_value(self->split_adjust, - curr_scroll + split_h); + curr_scroll + split_h); gtk_widget_hide(self->split_last); } } g_object_unref(self->split_rows[last]); } -static void splits_show_game(LSComponent *self_, ls_game *game, - ls_timer *timer) +static void splits_show_game(LSComponent* self_, ls_game* game, + ls_timer* timer) { - LSSplits *self = (LSSplits *)self_; + LSSplits* self = (LSSplits*)self_; char str[256]; int i; self->split_count = game->split_count; - self->split_rows = calloc(self->split_count, sizeof(GtkWidget *)); - self->split_titles = calloc(self->split_count, sizeof(GtkWidget *)); - self->split_deltas = calloc(self->split_count, sizeof(GtkWidget *)); - self->split_times = calloc(self->split_count, sizeof(GtkWidget *)); + self->split_rows = calloc(self->split_count, sizeof(GtkWidget*)); + self->split_titles = calloc(self->split_count, sizeof(GtkWidget*)); + self->split_deltas = calloc(self->split_count, sizeof(GtkWidget*)); + self->split_times = calloc(self->split_count, sizeof(GtkWidget*)); - for (i = 0; i < self->split_count; ++i) - { + for (i = 0; i < self->split_count; ++i) { self->split_rows[i] = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); add_class(self->split_rows[i], "split"); gtk_widget_set_hexpand(self->split_rows[i], TRUE); gtk_container_add(GTK_CONTAINER(self->splits), - self->split_rows[i]); - + self->split_rows[i]); + self->split_titles[i] = gtk_label_new(game->split_titles[i]); add_class(self->split_titles[i], "split-title"); gtk_widget_set_halign(self->split_titles[i], GTK_ALIGN_START); gtk_widget_set_hexpand(self->split_titles[i], TRUE); gtk_container_add(GTK_CONTAINER(self->split_rows[i]), - self->split_titles[i]); + self->split_titles[i]); self->split_deltas[i] = gtk_label_new(NULL); add_class(self->split_deltas[i], "split-delta"); gtk_widget_set_size_request(self->split_deltas[i], 1, -1); gtk_container_add(GTK_CONTAINER(self->split_rows[i]), - self->split_deltas[i]); + self->split_deltas[i]); self->split_times[i] = gtk_label_new(NULL); add_class(self->split_times[i], "split-time"); gtk_widget_set_halign(self->split_times[i], GTK_ALIGN_END); gtk_container_add(GTK_CONTAINER(self->split_rows[i]), - self->split_times[i]); + self->split_times[i]); - if (game->split_times[i]) - { + if (game->split_times[i]) { ls_split_string(str, game->split_times[i]); gtk_label_set_text(GTK_LABEL(self->split_times[i]), str); } if (game->split_titles[i] - && strlen(game->split_titles[i])) - { - char *c = &str[12]; + && strlen(game->split_titles[i])) { + char* c = &str[12]; strcpy(str, "split-title-"); strcpy(c, game->split_titles[i]); - do - { - if (!isalnum(*c)) - { + do { + if (!isalnum(*c)) { *c = '-'; - } - else - { + } else { *c = tolower(*c); } - } - while (*++c != '\0'); + } while (*++c != '\0'); { add_class(self->split_rows[i], str); } @@ -185,17 +170,16 @@ static void splits_show_game(LSComponent *self_, ls_game *game, splits_trailer(self_); } -static void splits_clear_game(LSComponent *self_) +static void splits_clear_game(LSComponent* self_) { - LSSplits *self = (LSSplits *)self_; + LSSplits* self = (LSSplits*)self_; int i; gtk_widget_hide(self->splits); gtk_widget_hide(self->split_last); - for (i = self->split_count - 1; i >= 0; --i) - { + for (i = self->split_count - 1; i >= 0; --i) { gtk_container_remove( - GTK_CONTAINER(gtk_widget_get_parent(self->split_rows[i])), - self->split_rows[i]); + GTK_CONTAINER(gtk_widget_get_parent(self->split_rows[i])), + self->split_rows[i]); } gtk_adjustment_set_value(self->split_adjust, 0); free(self->split_rows); @@ -206,43 +190,35 @@ static void splits_clear_game(LSComponent *self_) } #define SHOW_DELTA_THRESHOLD (-30 * 1000000LL) -static void splits_draw(LSComponent *self_, ls_game *game, ls_timer *timer) +static void splits_draw(LSComponent* self_, ls_game* game, ls_timer* timer) { - LSSplits *self = (LSSplits *)self_; + LSSplits* self = (LSSplits*)self_; char str[256]; int i; - for (i = 0; i < self->split_count; ++i) - { + for (i = 0; i < self->split_count; ++i) { if (i == timer->curr_split - && timer->start_time) - { + && timer->start_time) { add_class(self->split_rows[i], "current-split"); - } - else - { + } else { remove_class(self->split_rows[i], "current-split"); } - + remove_class(self->split_times[i], "time"); remove_class(self->split_times[i], "done"); gtk_label_set_text(GTK_LABEL(self->split_times[i]), "-"); - if (i < timer->curr_split) - { + if (i < timer->curr_split) { add_class(self->split_times[i], "done"); - if (timer->split_times[i]) - { + if (timer->split_times[i]) { add_class(self->split_times[i], "time"); ls_split_string(str, timer->split_times[i]); gtk_label_set_text(GTK_LABEL(self->split_times[i]), str); } - } - else if (game->split_times[i]) - { + } else if (game->split_times[i]) { add_class(self->split_times[i], "time"); ls_split_string(str, game->split_times[i]); gtk_label_set_text(GTK_LABEL(self->split_times[i]), str); } - + remove_class(self->split_deltas[i], "best-split"); remove_class(self->split_deltas[i], "best-segment"); remove_class(self->split_deltas[i], "behind"); @@ -250,36 +226,27 @@ static void splits_draw(LSComponent *self_, ls_game *game, ls_timer *timer) remove_class(self->split_deltas[i], "delta"); gtk_label_set_text(GTK_LABEL(self->split_deltas[i]), ""); if (i < timer->curr_split - || timer->split_deltas[i] >= SHOW_DELTA_THRESHOLD) - { - if (timer->split_info[i] & LS_INFO_BEST_SPLIT) - { + || timer->split_deltas[i] >= SHOW_DELTA_THRESHOLD) { + if (timer->split_info[i] & LS_INFO_BEST_SPLIT) { add_class(self->split_deltas[i], "best-split"); } - if (timer->split_info[i] & LS_INFO_BEST_SEGMENT) - { + if (timer->split_info[i] & LS_INFO_BEST_SEGMENT) { add_class(self->split_deltas[i], "best-segment"); } - if (timer->split_info[i] & LS_INFO_BEHIND_TIME) - { + if (timer->split_info[i] & LS_INFO_BEHIND_TIME) { add_class(self->split_deltas[i], "behind"); if (timer->split_info[i] - & LS_INFO_LOSING_TIME) - { + & LS_INFO_LOSING_TIME) { add_class(self->split_deltas[i], "losing"); } - } - else - { + } else { remove_class(self->split_deltas[i], "behind"); if (timer->split_info[i] - & LS_INFO_LOSING_TIME) - { + & LS_INFO_LOSING_TIME) { add_class(self->split_deltas[i], "losing"); } } - if (timer->split_deltas[i]) - { + if (timer->split_deltas[i]) { add_class(self->split_deltas[i], "delta"); ls_delta_string(str, timer->split_deltas[i]); gtk_label_set_text(GTK_LABEL(self->split_deltas[i]), str); @@ -288,32 +255,25 @@ static void splits_draw(LSComponent *self_, ls_game *game, ls_timer *timer) } // keep split sizes in sync - if (self->split_count) - { + if (self->split_count) { int width; int time_width = 0, delta_width = 0; - for (i = 0; i < self->split_count; ++i) - { + for (i = 0; i < self->split_count; ++i) { width = gtk_widget_get_allocated_width(self->split_deltas[i]); - if (width > delta_width) - { + if (width > delta_width) { delta_width = width; } width = gtk_widget_get_allocated_width(self->split_times[i]); - if (width > time_width) - { + if (width > time_width) { time_width = width; } } - for (i = 0; i < self->split_count; ++i) - { - if (delta_width) - { + for (i = 0; i < self->split_count; ++i) { + if (delta_width) { gtk_widget_set_size_request( self->split_deltas[i], delta_width, -1); } - if (time_width) - { + if (time_width) { width = gtk_widget_get_allocated_width( self->split_times[i]); gtk_widget_set_margin_start(self->split_times[i], @@ -325,9 +285,9 @@ static void splits_draw(LSComponent *self_, ls_game *game, ls_timer *timer) splits_trailer(self_); } -static void splits_scroll_to_split(LSComponent *self_, ls_timer *timer) +static void splits_scroll_to_split(LSComponent* self_, ls_timer* timer) { - LSSplits *self = (LSSplits *)self_; + LSSplits* self = (LSSplits*)self_; int split_x, split_y; int split_h; int scroller_h; @@ -336,16 +296,13 @@ static void splits_scroll_to_split(LSComponent *self_, ls_timer *timer) int prev = timer->curr_split - 1; int curr = timer->curr_split; int next = timer->curr_split + 1; - if (prev < 0) - { + if (prev < 0) { prev = 0; } - if (curr >= self->split_count) - { + if (curr >= self->split_count) { curr = self->split_count - 1; } - if (next >= self->split_count) - { + if (next >= self->split_count) { next = self->split_count - 1; } curr_scroll = gtk_adjustment_get_value(self->split_adjust); @@ -355,47 +312,40 @@ static void splits_scroll_to_split(LSComponent *self_, ls_timer *timer) 0, 0, &split_x, &split_y); scroller_h = gtk_widget_get_allocated_height(self->split_scroller); split_h = gtk_widget_get_allocated_height(self->split_titles[prev]); - if (curr != next && curr != prev) - { + if (curr != next && curr != prev) { split_h += gtk_widget_get_allocated_height(self->split_titles[curr]); } - if (next != prev) - { + if (next != prev) { int h = gtk_widget_get_allocated_height(self->split_titles[next]); - if (split_h + h < scroller_h) - { + if (split_h + h < scroller_h) { split_h += h; } } min_scroll = split_y + curr_scroll - scroller_h + split_h; max_scroll = split_y + curr_scroll; - if (curr_scroll > max_scroll) - { + if (curr_scroll > max_scroll) { gtk_adjustment_set_value(self->split_adjust, max_scroll); - } - else if (curr_scroll < min_scroll) - { + } else if (curr_scroll < min_scroll) { gtk_adjustment_set_value(self->split_adjust, min_scroll); } } -void splits_start_split(LSComponent *self, ls_timer *timer) +void splits_start_split(LSComponent* self, ls_timer* timer) { splits_scroll_to_split(self, timer); } -void splits_skip(LSComponent *self, ls_timer *timer) +void splits_skip(LSComponent* self, ls_timer* timer) { splits_scroll_to_split(self, timer); } -void splits_unsplit(LSComponent *self, ls_timer *timer) +void splits_unsplit(LSComponent* self, ls_timer* timer) { splits_scroll_to_split(self, timer); } -LSComponentOps ls_splits_operations = -{ +LSComponentOps ls_splits_operations = { .delete = splits_delete, .widget = splits_widget, .show_game = splits_show_game, diff --git a/src/component/title.c b/src/component/title.c index 93dac5b..8346ce7 100644 --- a/src/component/title.c +++ b/src/component/title.c @@ -1,21 +1,19 @@ #include "components.h" -typedef struct _LSTitle -{ +typedef struct _LSTitle { LSComponent base; - GtkWidget *header; - GtkWidget *title; - GtkWidget *attempt_count; + GtkWidget* header; + GtkWidget* title; + GtkWidget* attempt_count; } LSTitle; extern LSComponentOps ls_title_operations; // defined at the end of the file -LSComponent *ls_component_title_new() +LSComponent* ls_component_title_new() { - LSTitle *self; + LSTitle* self; self = malloc(sizeof(LSTitle)); - if (!self) - { + if (!self) { return NULL; } self->base.ops = &ls_title_operations; @@ -38,25 +36,25 @@ LSComponent *ls_component_title_new() gtk_container_add(GTK_CONTAINER(self->header), self->attempt_count); gtk_widget_show(self->attempt_count); - return (LSComponent *)self; + return (LSComponent*)self; } -static void title_delete(LSComponent *self) +static void title_delete(LSComponent* self) { free(self); } -static GtkWidget *title_widget(LSComponent *self) +static GtkWidget* title_widget(LSComponent* self) { - return ((LSTitle *)self)->header; + return ((LSTitle*)self)->header; } -static void title_resize(LSComponent *self_, int win_width, int win_height) +static void title_resize(LSComponent* self_, int win_width, int win_height) { GdkRectangle rect; int attempt_count_width; int title_width; - LSTitle *self = (LSTitle *)self_; + LSTitle* self = (LSTitle*)self_; gtk_widget_hide(self->title); gtk_widget_get_allocation(self->attempt_count, &rect); @@ -67,26 +65,25 @@ static void title_resize(LSComponent *self_, int win_width, int win_height) gtk_widget_set_allocation(self->title, &rect); } -static void title_show_game(LSComponent *self_, ls_game *game, - ls_timer *timer) +static void title_show_game(LSComponent* self_, ls_game* game, + ls_timer* timer) { char str[64]; - LSTitle *self = (LSTitle *)self_; + LSTitle* self = (LSTitle*)self_; gtk_label_set_text(GTK_LABEL(self->title), game->title); sprintf(str, "#%d", game->attempt_count); gtk_label_set_text(GTK_LABEL(self->attempt_count), str); } -static void title_draw(LSComponent *self_, ls_game *game, ls_timer *timer) +static void title_draw(LSComponent* self_, ls_game* game, ls_timer* timer) { char str[64]; - LSTitle *self = (LSTitle *)self_; + LSTitle* self = (LSTitle*)self_; sprintf(str, "#%d", game->attempt_count); gtk_label_set_text(GTK_LABEL(self->attempt_count), str); } -LSComponentOps ls_title_operations = -{ +LSComponentOps ls_title_operations = { .delete = title_delete, .widget = title_widget, .resize = title_resize, diff --git a/src/component/wr.c b/src/component/wr.c index b25ca02..8a1ac75 100644 --- a/src/component/wr.c +++ b/src/component/wr.c @@ -1,23 +1,21 @@ #include "components.h" -typedef struct _LSWr -{ +typedef struct _LSWr { LSComponent base; - GtkWidget *container; - GtkWidget *world_record_label; - GtkWidget *world_record; + GtkWidget* container; + GtkWidget* world_record_label; + GtkWidget* world_record; } LSWr; extern LSComponentOps ls_wr_operations; #define WORLD_RECORD "World record" -LSComponent *ls_component_wr_new() +LSComponent* ls_component_wr_new() { - LSWr *self; + LSWr* self; self = malloc(sizeof(LSWr)); - if (!self) - { + if (!self) { return NULL; } self->base.ops = &ls_wr_operations; @@ -36,27 +34,26 @@ LSComponent *ls_component_wr_new() gtk_widget_set_halign(self->world_record, GTK_ALIGN_END); gtk_container_add(GTK_CONTAINER(self->container), self->world_record); - return (LSComponent *)self; + return (LSComponent*)self; } -static void wr_delete(LSComponent *self) +static void wr_delete(LSComponent* self) { free(self); } -static GtkWidget *wr_widget(LSComponent *self) +static GtkWidget* wr_widget(LSComponent* self) { - return ((LSWr *)self)->container; + return ((LSWr*)self)->container; } -static void wr_show_game(LSComponent *self_, - ls_game *game, ls_timer *timer) +static void wr_show_game(LSComponent* self_, + ls_game* game, ls_timer* timer) { - LSWr *self = (LSWr *)self_; + LSWr* self = (LSWr*)self_; gtk_widget_set_halign(self->world_record_label, GTK_ALIGN_START); gtk_widget_set_hexpand(self->world_record_label, TRUE); - if (game->world_record) - { + if (game->world_record) { char str[256]; ls_time_string(str, game->world_record); gtk_label_set_text(GTK_LABEL(self->world_record), str); @@ -65,38 +62,32 @@ static void wr_show_game(LSComponent *self_, } } -static void wr_clear_game(LSComponent *self_) +static void wr_clear_game(LSComponent* self_) { - LSWr *self = (LSWr *)self_; + LSWr* self = (LSWr*)self_; gtk_widget_hide(self->world_record_label); gtk_widget_hide(self->world_record); } -static void wr_draw(LSComponent *self_, ls_game *game, - ls_timer *timer) +static void wr_draw(LSComponent* self_, ls_game* game, + ls_timer* timer) { - LSWr *self = (LSWr *)self_; + LSWr* self = (LSWr*)self_; char str[256]; if (timer->curr_split == game->split_count - && game->world_record) - { + && game->world_record) { if (timer->split_times[game->split_count - 1] && timer->split_times[game->split_count - 1] - < game->world_record) - { - ls_time_string(str, timer->split_times[ - game->split_count - 1]); - } - else - { + < game->world_record) { + ls_time_string(str, timer->split_times[game->split_count - 1]); + } else { ls_time_string(str, game->world_record); } gtk_label_set_text(GTK_LABEL(self->world_record), str); } } -LSComponentOps ls_wr_operations = -{ +LSComponentOps ls_wr_operations = { .delete = wr_delete, .widget = wr_widget, .show_game = wr_show_game, diff --git a/src/main.c b/src/main.c index e8cf000..a67f506 100644 --- a/src/main.c +++ b/src/main.c @@ -1,36 +1,34 @@ #include +#include #include -#include #include -#include -#include +#include #include -#include +#include +#include #include -#include "timer.h" -#include "main.h" +#include "auto-splitter.h" #include "bind.h" #include "component/components.h" -#include "auto-splitter.h" +#include "main.h" #include "settings.h" +#include "timer.h" -#define LS_APP_TYPE (ls_app_get_type ()) -#define LS_APP(obj) \ - (G_TYPE_CHECK_INSTANCE_CAST \ - ((obj), LS_APP_TYPE, LSApp)) +#define LS_APP_TYPE (ls_app_get_type()) +#define LS_APP(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), LS_APP_TYPE, LSApp)) -typedef struct _LSApp LSApp; -typedef struct _LSAppClass LSAppClass; +typedef struct _LSApp LSApp; +typedef struct _LSAppClass LSAppClass; -#define LS_APP_WINDOW_TYPE (ls_app_window_get_type ()) -#define LS_APP_WINDOW(obj) \ - (G_TYPE_CHECK_INSTANCE_CAST \ - ((obj), LS_APP_WINDOW_TYPE, LSAppWindow)) +#define LS_APP_WINDOW_TYPE (ls_app_window_get_type()) +#define LS_APP_WINDOW(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), LS_APP_WINDOW_TYPE, LSAppWindow)) -typedef struct _LSAppWindow LSAppWindow; -typedef struct _LSAppWindowClass LSAppWindowClass; +typedef struct _LSAppWindow LSAppWindow; +typedef struct _LSAppWindowClass LSAppWindowClass; #define WINDOW_PAD (8) @@ -42,18 +40,17 @@ typedef struct GdkModifierType mods; } Keybind; -struct _LSAppWindow -{ +struct _LSAppWindow { GtkApplicationWindow parent; char data_path[PATH_MAX]; int decorated; - ls_game *game; - ls_timer *timer; - GdkDisplay *display; - GtkWidget *box; - GList *components; - GtkWidget *footer; - GtkCssProvider *style; + ls_game* game; + ls_timer* timer; + GdkDisplay* display; + GtkWidget* box; + GList* components; + GtkWidget* footer; + GtkCssProvider* style; gboolean hide_cursor; gboolean global_hotkeys; Keybind keybind_start_split; @@ -64,14 +61,13 @@ struct _LSAppWindow Keybind keybind_toggle_decorations; }; -struct _LSAppWindowClass -{ +struct _LSAppWindowClass { GtkApplicationWindowClass parent_class; }; G_DEFINE_TYPE(LSAppWindow, ls_app_window, GTK_TYPE_APPLICATION_WINDOW); -static Keybind parse_keybind(const gchar *accelerator) +static Keybind parse_keybind(const gchar* accelerator) { Keybind kb; gtk_accelerator_parse(accelerator, &kb.key, &kb.mods); @@ -80,19 +76,16 @@ static Keybind parse_keybind(const gchar *accelerator) static int keybind_match(Keybind kb, GdkEventKey key) { - return key.keyval == kb.key && - kb.mods == (key.state & gtk_accelerator_get_default_mod_mask()); + return key.keyval == kb.key && kb.mods == (key.state & gtk_accelerator_get_default_mod_mask()); } -static void ls_app_window_destroy(GtkWidget *widget, gpointer data) +static void ls_app_window_destroy(GtkWidget* widget, gpointer data) { - LSAppWindow *win = (LSAppWindow*)widget; - if (win->timer) - { + LSAppWindow* win = (LSAppWindow*)widget; + if (win->timer) { ls_timer_release(win->timer); } - if (win->game) - { + if (win->game) { ls_game_release(win->game); } atomic_store(&auto_splitter_enabled, 0); @@ -101,95 +94,81 @@ static void ls_app_window_destroy(GtkWidget *widget, gpointer data) static gpointer save_game_thread(gpointer data) { - ls_game *game = data; + ls_game* game = data; ls_game_save(game); return NULL; } -static void save_game(ls_game *game) +static void save_game(ls_game* game) { g_thread_new("save_game", save_game_thread, game); } -static void ls_app_window_clear_game(LSAppWindow *win) +static void ls_app_window_clear_game(LSAppWindow* win) { - GdkScreen *screen; - GList *l; + GdkScreen* screen; + GList* l; gtk_widget_hide(win->box); - for (l = win->components; l != NULL; l = l->next) - { - LSComponent *component = l->data; - if (component->ops->clear_game) - { + for (l = win->components; l != NULL; l = l->next) { + LSComponent* component = l->data; + if (component->ops->clear_game) { component->ops->clear_game(component); } } // remove game's style - if (win->style) - { + if (win->style) { screen = gdk_display_get_default_screen(win->display); gtk_style_context_remove_provider_for_screen( - screen, GTK_STYLE_PROVIDER(win->style)); + screen, GTK_STYLE_PROVIDER(win->style)); g_object_unref(win->style); win->style = NULL; } } // Forward declarations -static void timer_start(LSAppWindow *win); -static void timer_stop(LSAppWindow *win); -static void timer_split(LSAppWindow *win); -static void timer_reset(LSAppWindow *win); +static void timer_start(LSAppWindow* win); +static void timer_stop(LSAppWindow* win); +static void timer_split(LSAppWindow* win); +static void timer_reset(LSAppWindow* win); static gboolean ls_app_window_step(gpointer data) { - LSAppWindow *win = data; + LSAppWindow* win = data; long long now = ls_time_now(); static int set_cursor; - if (win->hide_cursor && !set_cursor) - { - GdkWindow *gdk_window = gtk_widget_get_window(GTK_WIDGET(win)); - if (gdk_window) - { - GdkCursor *cursor = gdk_cursor_new_for_display(win->display, GDK_BLANK_CURSOR); + if (win->hide_cursor && !set_cursor) { + GdkWindow* gdk_window = gtk_widget_get_window(GTK_WIDGET(win)); + if (gdk_window) { + GdkCursor* cursor = gdk_cursor_new_for_display(win->display, GDK_BLANK_CURSOR); gdk_window_set_cursor(gdk_window, cursor); set_cursor = 1; } } - if (win->timer) - { + if (win->timer) { ls_timer_step(win->timer, now); - - if(atomic_load(&auto_splitter_enabled)) - { - if (atomic_load(&call_start) && !win->timer->loading) - { + + if (atomic_load(&auto_splitter_enabled)) { + if (atomic_load(&call_start) && !win->timer->loading) { timer_start(win); atomic_store(&call_start, 0); } - if (atomic_load(&call_split)) - { + if (atomic_load(&call_split)) { timer_split(win); atomic_store(&call_split, 0); } - if (atomic_load(&toggle_loading)) - { + if (atomic_load(&toggle_loading)) { win->timer->loading = !win->timer->loading; - if (win->timer->running && win->timer->loading) - { + if (win->timer->running && win->timer->loading) { timer_stop(win); - } - else if (win->timer->started && !win->timer->running && !win->timer->loading) - { + } else if (win->timer->started && !win->timer->running && !win->timer->loading) { timer_start(win); } atomic_store(&toggle_loading, 0); } - if (atomic_load(&call_reset)) - { + if (atomic_load(&call_reset)) { timer_reset(win); atomic_store(&call_reset, 0); } @@ -199,15 +178,14 @@ static gboolean ls_app_window_step(gpointer data) return TRUE; } -static int ls_app_window_find_theme(LSAppWindow *win, - const char *theme_name, - const char *theme_variant, - char *str) +static int ls_app_window_find_theme(LSAppWindow* win, + const char* theme_name, + const char* theme_variant, + char* str) { char theme_path[PATH_MAX]; - struct stat st = {0}; - if (!theme_name || !strlen(theme_name)) - { + struct stat st = { 0 }; + if (!theme_name || !strlen(theme_name)) { str[0] = '\0'; return 0; } @@ -215,8 +193,7 @@ static int ls_app_window_find_theme(LSAppWindow *win, strcat(theme_path, theme_name); strcat(theme_path, "/"); strcat(theme_path, theme_name); - if (theme_variant && strlen(theme_variant)) - { + if (theme_variant && strlen(theme_variant)) { strcat(theme_path, "-"); strcat(theme_path, theme_variant); } @@ -225,33 +202,30 @@ static int ls_app_window_find_theme(LSAppWindow *win, strcpy(str, win->data_path); strcat(str, "/themes"); strcat(str, theme_path); - if (stat(str, &st) == -1) - { + if (stat(str, &st) == -1) { return 0; } return 1; } -static void ls_app_window_show_game(LSAppWindow *win) +static void ls_app_window_show_game(LSAppWindow* win) { - GdkScreen *screen; + GdkScreen* screen; char str[PATH_MAX]; - GList *l; + GList* l; // set dimensions - if (win->game->width > 0 && win->game->height > 0) - { + if (win->game->width > 0 && win->game->height > 0) { gtk_widget_set_size_request(GTK_WIDGET(win), - win->game->width, - win->game->height); + win->game->width, + win->game->height); } // set game theme if (ls_app_window_find_theme(win, - win->game->theme, - win->game->theme_variant, - str)) - { + win->game->theme, + win->game->theme_variant, + str)) { win->style = gtk_css_provider_new(); screen = gdk_display_get_default_screen(win->display); gtk_style_context_add_provider_for_screen( @@ -263,11 +237,9 @@ static void ls_app_window_show_game(LSAppWindow *win) str, NULL); } - for (l = win->components; l != NULL; l = l->next) - { - LSComponent *component = l->data; - if (component->ops->show_game) - { + for (l = win->components; l != NULL; l = l->next) { + LSComponent* component = l->data; + if (component->ops->show_game) { component->ops->show_game(component, win->game, win->timer); } } @@ -275,75 +247,61 @@ static void ls_app_window_show_game(LSAppWindow *win) gtk_widget_show(win->box); } -static void resize_window(LSAppWindow *win, - int window_width, - int window_height) +static void resize_window(LSAppWindow* win, + int window_width, + int window_height) { - GList *l; - for (l = win->components; l != NULL; l = l->next) - { - LSComponent *component = l->data; - if (component->ops->resize) - { + GList* l; + for (l = win->components; l != NULL; l = l->next) { + LSComponent* component = l->data; + if (component->ops->resize) { component->ops->resize(component, - window_width - 2 * WINDOW_PAD, - window_height); + window_width - 2 * WINDOW_PAD, + window_height); } } } -static gboolean ls_app_window_resize(GtkWidget *widget, - GdkEvent *event, - gpointer data) +static gboolean ls_app_window_resize(GtkWidget* widget, + GdkEvent* event, + gpointer data) { - LSAppWindow *win = (LSAppWindow*)widget; + LSAppWindow* win = (LSAppWindow*)widget; resize_window(win, event->configure.width, event->configure.height); return FALSE; } -static void timer_start_split(LSAppWindow *win) +static void timer_start_split(LSAppWindow* win) { - if (win->timer) - { - GList *l; - if (!win->timer->running) - { - if (ls_timer_start(win->timer)) - { + if (win->timer) { + GList* l; + if (!win->timer->running) { + if (ls_timer_start(win->timer)) { save_game(win->game); } - } - else - { + } else { ls_timer_split(win->timer); } - for (l = win->components; l != NULL; l = l->next) - { - LSComponent *component = l->data; - if (component->ops->start_split) - { + for (l = win->components; l != NULL; l = l->next) { + LSComponent* component = l->data; + if (component->ops->start_split) { component->ops->start_split(component, win->timer); } } } } -static void timer_start(LSAppWindow *win) +static void timer_start(LSAppWindow* win) { - if (win->timer) - { - GList *l; - if (!win->timer->running) - { - if (ls_timer_start(win->timer)) - { + if (win->timer) { + GList* l; + if (!win->timer->running) { + if (ls_timer_start(win->timer)) { save_game(win->game); } - for (l = win->components; l != NULL; l = l->next) - { - LSComponent *component = l->data; - if (component->ops->start_split) - { + for (l = win->components; l != NULL; l = l->next) { + LSComponent* component = l->data; + if (component->ops->start_split) { component->ops->start_split(component, win->timer); } } @@ -351,19 +309,15 @@ static void timer_start(LSAppWindow *win) } } -static void timer_split(LSAppWindow *win) +static void timer_split(LSAppWindow* win) { - if (win->timer) - { - GList *l; - if (win->timer->running) - { + if (win->timer) { + GList* l; + if (win->timer->running) { ls_timer_split(win->timer); - for (l = win->components; l != NULL; l = l->next) - { - LSComponent *component = l->data; - if (component->ops->start_split) - { + for (l = win->components; l != NULL; l = l->next) { + LSComponent* component = l->data; + if (component->ops->start_split) { component->ops->start_split(component, win->timer); } } @@ -371,210 +325,169 @@ static void timer_split(LSAppWindow *win) } } -static void timer_stop(LSAppWindow *win) +static void timer_stop(LSAppWindow* win) { - if (win->timer) - { - GList *l; - if (win->timer->running) - { + if (win->timer) { + GList* l; + if (win->timer->running) { ls_timer_stop(win->timer); } - for (l = win->components; l != NULL; l = l->next) - { - LSComponent *component = l->data; - if (component->ops->stop_reset) - { + for (l = win->components; l != NULL; l = l->next) { + LSComponent* component = l->data; + if (component->ops->stop_reset) { component->ops->stop_reset(component, win->timer); } } } } -static void timer_stop_reset(LSAppWindow *win) +static void timer_stop_reset(LSAppWindow* win) { - if (win->timer) - { - GList *l; - if (win->timer->running) - { + if (win->timer) { + GList* l; + if (win->timer->running) { ls_timer_stop(win->timer); - } - else - { - if (ls_timer_reset(win->timer)) - { + } else { + if (ls_timer_reset(win->timer)) { ls_app_window_clear_game(win); ls_app_window_show_game(win); save_game(win->game); } } - for (l = win->components; l != NULL; l = l->next) - { - LSComponent *component = l->data; - if (component->ops->stop_reset) - { + for (l = win->components; l != NULL; l = l->next) { + LSComponent* component = l->data; + if (component->ops->stop_reset) { component->ops->stop_reset(component, win->timer); } } } } -static void timer_reset(LSAppWindow *win) +static void timer_reset(LSAppWindow* win) { - if (win->timer) - { - GList *l; - if (win->timer->running) - { + if (win->timer) { + GList* l; + if (win->timer->running) { ls_timer_stop(win->timer); - for (l = win->components; l != NULL; l = l->next) - { - LSComponent *component = l->data; - if (component->ops->stop_reset) - { + for (l = win->components; l != NULL; l = l->next) { + LSComponent* component = l->data; + if (component->ops->stop_reset) { component->ops->stop_reset(component, win->timer); } } } - if (ls_timer_reset(win->timer)) - { + if (ls_timer_reset(win->timer)) { ls_app_window_clear_game(win); ls_app_window_show_game(win); save_game(win->game); } - for (l = win->components; l != NULL; l = l->next) - { - LSComponent *component = l->data; - if (component->ops->stop_reset) - { + for (l = win->components; l != NULL; l = l->next) { + LSComponent* component = l->data; + if (component->ops->stop_reset) { component->ops->stop_reset(component, win->timer); } } } } -static void timer_cancel_run(LSAppWindow *win) +static void timer_cancel_run(LSAppWindow* win) { - if (win->timer) - { - GList *l; - if (ls_timer_cancel(win->timer)) - { + if (win->timer) { + GList* l; + if (ls_timer_cancel(win->timer)) { ls_app_window_clear_game(win); ls_app_window_show_game(win); save_game(win->game); } - for (l = win->components; l != NULL; l = l->next) - { - LSComponent *component = l->data; - if (component->ops->cancel_run) - { + for (l = win->components; l != NULL; l = l->next) { + LSComponent* component = l->data; + if (component->ops->cancel_run) { component->ops->cancel_run(component, win->timer); } } } } - -static void timer_skip(LSAppWindow *win) +static void timer_skip(LSAppWindow* win) { - if (win->timer) - { - GList *l; + if (win->timer) { + GList* l; ls_timer_skip(win->timer); - for (l = win->components; l != NULL; l = l->next) - { - LSComponent *component = l->data; - if (component->ops->skip) - { + for (l = win->components; l != NULL; l = l->next) { + LSComponent* component = l->data; + if (component->ops->skip) { component->ops->skip(component, win->timer); } } } } -static void timer_unsplit(LSAppWindow *win) +static void timer_unsplit(LSAppWindow* win) { - if (win->timer) - { - GList *l; + if (win->timer) { + GList* l; ls_timer_unsplit(win->timer); - for (l = win->components; l != NULL; l = l->next) - { - LSComponent *component = l->data; - if (component->ops->unsplit) - { + for (l = win->components; l != NULL; l = l->next) { + LSComponent* component = l->data; + if (component->ops->unsplit) { component->ops->unsplit(component, win->timer); } } } } - -static void toggle_decorations(LSAppWindow *win) +static void toggle_decorations(LSAppWindow* win) { gtk_window_set_decorated(GTK_WINDOW(win), !win->decorated); win->decorated = !win->decorated; } -static void keybind_start_split(GtkWidget *widget, LSAppWindow *win) +static void keybind_start_split(GtkWidget* widget, LSAppWindow* win) { timer_start_split(win); } -static void keybind_stop_reset(const char *str, LSAppWindow *win) +static void keybind_stop_reset(const char* str, LSAppWindow* win) { timer_stop_reset(win); } -static void keybind_cancel(const char *str, LSAppWindow *win) +static void keybind_cancel(const char* str, LSAppWindow* win) { timer_cancel_run(win); } -static void keybind_skip(const char *str, LSAppWindow *win) +static void keybind_skip(const char* str, LSAppWindow* win) { timer_skip(win); } -static void keybind_unsplit(const char *str, LSAppWindow *win) +static void keybind_unsplit(const char* str, LSAppWindow* win) { timer_unsplit(win); } -static void keybind_toggle_decorations(const char *str, LSAppWindow *win) +static void keybind_toggle_decorations(const char* str, LSAppWindow* win) { toggle_decorations(win); } -static gboolean ls_app_window_keypress(GtkWidget *widget, - GdkEvent *event, - gpointer data) +static gboolean ls_app_window_keypress(GtkWidget* widget, + GdkEvent* event, + gpointer data) { - LSAppWindow *win = (LSAppWindow*)data; - if (keybind_match(win->keybind_start_split, event->key)) - { + LSAppWindow* win = (LSAppWindow*)data; + if (keybind_match(win->keybind_start_split, event->key)) { timer_start_split(win); - } - else if (keybind_match(win->keybind_stop_reset, event->key)) - { + } else if (keybind_match(win->keybind_stop_reset, event->key)) { timer_stop_reset(win); - } - else if (keybind_match(win->keybind_cancel, event->key)) - { + } else if (keybind_match(win->keybind_cancel, event->key)) { timer_cancel_run(win); - } - else if (keybind_match(win->keybind_unsplit, event->key)) - { + } else if (keybind_match(win->keybind_unsplit, event->key)) { timer_unsplit(win); - } - else if (keybind_match(win->keybind_skip_split, event->key)) - { + } else if (keybind_match(win->keybind_skip_split, event->key)) { timer_skip(win); - } - else if (keybind_match(win->keybind_toggle_decorations, event->key)) - { + } else if (keybind_match(win->keybind_toggle_decorations, event->key)) { toggle_decorations(win); } return TRUE; @@ -582,36 +495,31 @@ static gboolean ls_app_window_keypress(GtkWidget *widget, static gboolean ls_app_window_draw(gpointer data) { - LSAppWindow *win = data; - if (win->timer) - { - GList *l; - for (l = win->components; l != NULL; l = l->next) - { - LSComponent *component = l->data; - if (component->ops->draw) - { + LSAppWindow* win = data; + if (win->timer) { + GList* l; + for (l = win->components; l != NULL; l = l->next) { + LSComponent* component = l->data; + if (component->ops->draw) { component->ops->draw(component, win->game, win->timer); } } - } - else - { + } else { GdkRectangle rect; gtk_widget_get_allocation(GTK_WIDGET(win), &rect); gdk_window_invalidate_rect(gtk_widget_get_window(GTK_WIDGET(win)), - &rect, FALSE); + &rect, FALSE); } return TRUE; } -static void ls_app_window_init(LSAppWindow *win) +static void ls_app_window_init(LSAppWindow* win) { - GtkCssProvider *provider; - GdkScreen *screen; + GtkCssProvider* provider; + GdkScreen* screen; char str[PATH_MAX]; - const char *theme; - const char *theme_variant; + const char* theme; + const char* theme_variant; int i; win->display = gdk_display_get_default(); @@ -622,21 +530,21 @@ static void ls_app_window_init(LSAppWindow *win) get_libresplit_folder_path(win->data_path); // load settings - GSettings *settings = g_settings_new("com.github.wins1ey.libresplit"); + GSettings* settings = g_settings_new("com.github.wins1ey.libresplit"); win->hide_cursor = g_settings_get_boolean(settings, "hide-cursor"); win->global_hotkeys = g_settings_get_boolean(settings, "global-hotkeys"); win->keybind_start_split = parse_keybind( - g_settings_get_string(settings, "keybind-start-split")); + g_settings_get_string(settings, "keybind-start-split")); win->keybind_stop_reset = parse_keybind( - g_settings_get_string(settings, "keybind-stop-reset")); + g_settings_get_string(settings, "keybind-stop-reset")); win->keybind_cancel = parse_keybind( - g_settings_get_string(settings, "keybind-cancel")); + g_settings_get_string(settings, "keybind-cancel")); win->keybind_unsplit = parse_keybind( - g_settings_get_string(settings, "keybind-unsplit")); + g_settings_get_string(settings, "keybind-unsplit")); win->keybind_skip_split = parse_keybind( - g_settings_get_string(settings, "keybind-skip-split")); + g_settings_get_string(settings, "keybind-skip-split")); win->keybind_toggle_decorations = parse_keybind( - g_settings_get_string(settings, "keybind-toggle-decorations")); + g_settings_get_string(settings, "keybind-toggle-decorations")); win->decorated = g_settings_get_boolean(settings, "start-decorated"); gtk_window_set_decorated(GTK_WINDOW(win), win->decorated); @@ -649,14 +557,13 @@ static void ls_app_window_init(LSAppWindow *win) GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); gtk_css_provider_load_from_data( GTK_CSS_PROVIDER(provider), - (char *)__src_main_css, __src_main_css_len, NULL); + (char*)__src_main_css, __src_main_css_len, NULL); g_object_unref(provider); // Load theme theme = g_settings_get_string(settings, "theme"); theme_variant = g_settings_get_string(settings, "theme-variant"); - if (ls_app_window_find_theme(win, theme, theme_variant, str)) - { + if (ls_app_window_find_theme(win, theme, theme_variant, str)) { provider = gtk_css_provider_new(); screen = gdk_display_get_default_screen(win->display); gtk_style_context_add_provider_for_screen( @@ -675,12 +582,11 @@ static void ls_app_window_init(LSAppWindow *win) win->timer = 0; g_signal_connect(win, "destroy", - G_CALLBACK(ls_app_window_destroy), NULL); + G_CALLBACK(ls_app_window_destroy), NULL); g_signal_connect(win, "configure-event", - G_CALLBACK(ls_app_window_resize), win); + G_CALLBACK(ls_app_window_resize), win); - if (win->global_hotkeys) - { + if (win->global_hotkeys) { keybinder_init(); keybinder_bind( g_settings_get_string(settings, "keybind-start-split"), @@ -706,11 +612,9 @@ static void ls_app_window_init(LSAppWindow *win) g_settings_get_string(settings, "keybind-toggle-decorations"), (KeybinderHandler)keybind_toggle_decorations, win); - } - else - { + } else { g_signal_connect(win, "key_press_event", - G_CALLBACK(ls_app_window_keypress), win); + G_CALLBACK(ls_app_window_keypress), win); } win->box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); @@ -721,18 +625,15 @@ static void ls_app_window_init(LSAppWindow *win) // Create all available components (TODO: change this in the future) win->components = NULL; - for (i = 0; ls_components[i].name != NULL; i++) - { - LSComponent *component = ls_components[i].new(); - if (component) - { - GtkWidget *widget = component->ops->widget(component); - if (widget) - { + for (i = 0; ls_components[i].name != NULL; i++) { + LSComponent* component = ls_components[i].new(); + if (component) { + GtkWidget* widget = component->ops->widget(component); + if (widget) { gtk_widget_set_margin_start(widget, WINDOW_PAD); gtk_widget_set_margin_end(widget, WINDOW_PAD); gtk_container_add(GTK_CONTAINER(win->box), - component->ops->widget(component)); + component->ops->widget(component)); } win->components = g_list_append(win->components, component); } @@ -746,85 +647,72 @@ static void ls_app_window_init(LSAppWindow *win) gtk_widget_show(win->footer); g_timeout_add(1, ls_app_window_step, win); - g_timeout_add((int)(1000 / 30.), ls_app_window_draw, win); + g_timeout_add((int)(1000 / 30.), ls_app_window_draw, win); } -static void ls_app_window_class_init(LSAppWindowClass *class) +static void ls_app_window_class_init(LSAppWindowClass* class) { } -static LSAppWindow *ls_app_window_new(LSApp *app) +static LSAppWindow* ls_app_window_new(LSApp* app) { - LSAppWindow *win; + LSAppWindow* win; win = g_object_new(LS_APP_WINDOW_TYPE, "application", app, NULL); gtk_window_set_type_hint(GTK_WINDOW(win), GDK_WINDOW_TYPE_HINT_DIALOG); return win; } -static void ls_app_window_open(LSAppWindow *win, const char *file) +static void ls_app_window_open(LSAppWindow* win, const char* file) { - if (win->timer) - { + if (win->timer) { ls_app_window_clear_game(win); ls_timer_release(win->timer); win->timer = 0; } - if (win->game) - { + if (win->game) { ls_game_release(win->game); win->game = 0; } - if (ls_game_create(&win->game, file)) - { + if (ls_game_create(&win->game, file)) { win->game = 0; - } - else if (ls_timer_create(&win->timer, win->game)) - { + } else if (ls_timer_create(&win->timer, win->game)) { win->timer = 0; - } - else - { + } else { ls_app_window_show_game(win); } } -struct _LSApp -{ +struct _LSApp { GtkApplication parent; }; -struct _LSAppClass -{ +struct _LSAppClass { GtkApplicationClass parent_class; }; G_DEFINE_TYPE(LSApp, ls_app, GTK_TYPE_APPLICATION); -static void open_activated(GSimpleAction *action, - GVariant *parameter, - gpointer app) +static void open_activated(GSimpleAction* action, + GVariant* parameter, + gpointer app) { char splits_path[PATH_MAX]; - GList *windows; - LSAppWindow *win; - GtkWidget *dialog; - struct stat st = {0}; + GList* windows; + LSAppWindow* win; + GtkWidget* dialog; + struct stat st = { 0 }; gint res; - if (parameter != NULL) - { + if (parameter != NULL) { app = parameter; } windows = gtk_application_get_windows(GTK_APPLICATION(app)); - if (windows) - { + if (windows) { win = LS_APP_WINDOW(windows->data); - } - else - { + } else { win = ls_app_window_new(LS_APP(app)); } - dialog = gtk_file_chooser_dialog_new ( + dialog = gtk_file_chooser_dialog_new( "Open File", GTK_WINDOW(win), GTK_FILE_CHOOSER_ACTION_OPEN, "_Cancel", GTK_RESPONSE_CANCEL, "_Open", GTK_RESPONSE_ACCEPT, @@ -832,18 +720,16 @@ static void open_activated(GSimpleAction *action, strcpy(splits_path, win->data_path); strcat(splits_path, "/splits"); - if (stat(splits_path, &st) == -1) - { + if (stat(splits_path, &st) == -1) { mkdir(splits_path, 0700); } gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), - splits_path); + splits_path); res = gtk_dialog_run(GTK_DIALOG(dialog)); - if (res == GTK_RESPONSE_ACCEPT) - { - char *filename; - GtkFileChooser *chooser = GTK_FILE_CHOOSER(dialog); + if (res == GTK_RESPONSE_ACCEPT) { + char* filename; + GtkFileChooser* chooser = GTK_FILE_CHOOSER(dialog); filename = gtk_file_chooser_get_filename(chooser); ls_app_window_open(win, filename); ls_update_setting("split_file", json_string(filename)); @@ -852,31 +738,27 @@ static void open_activated(GSimpleAction *action, gtk_widget_destroy(dialog); } -static void open_auto_splitter(GSimpleAction *action, - GVariant *parameter, - gpointer app) +static void open_auto_splitter(GSimpleAction* action, + GVariant* parameter, + gpointer app) { char auto_splitters_path[PATH_MAX]; - GList *windows; - LSAppWindow *win; - GtkWidget *dialog; - struct stat st = {0}; + GList* windows; + LSAppWindow* win; + GtkWidget* dialog; + struct stat st = { 0 }; gint res; - if (parameter != NULL) - { + if (parameter != NULL) { app = parameter; } windows = gtk_application_get_windows(GTK_APPLICATION(app)); - if (windows) - { + if (windows) { win = LS_APP_WINDOW(windows->data); - } - else - { + } else { win = ls_app_window_new(LS_APP(app)); } - dialog = gtk_file_chooser_dialog_new ( + dialog = gtk_file_chooser_dialog_new( "Open File", GTK_WINDOW(win), GTK_FILE_CHOOSER_ACTION_OPEN, "_Cancel", GTK_RESPONSE_CANCEL, "_Open", GTK_RESPONSE_ACCEPT, @@ -884,18 +766,16 @@ static void open_auto_splitter(GSimpleAction *action, strcpy(auto_splitters_path, win->data_path); strcat(auto_splitters_path, "/auto-splitters"); - if (stat(auto_splitters_path, &st) == -1) - { + if (stat(auto_splitters_path, &st) == -1) { mkdir(auto_splitters_path, 0700); } gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), - auto_splitters_path); + auto_splitters_path); res = gtk_dialog_run(GTK_DIALOG(dialog)); - if (res == GTK_RESPONSE_ACCEPT) - { - GtkFileChooser *chooser = GTK_FILE_CHOOSER(dialog); - char *filename = gtk_file_chooser_get_filename(chooser); + if (res == GTK_RESPONSE_ACCEPT) { + GtkFileChooser* chooser = GTK_FILE_CHOOSER(dialog); + char* filename = gtk_file_chooser_get_filename(chooser); strcpy(auto_splitter_file, filename); ls_update_setting("auto_splitter_file", json_string(filename)); g_free(filename); @@ -903,28 +783,23 @@ static void open_auto_splitter(GSimpleAction *action, gtk_widget_destroy(dialog); } -static void save_activated(GSimpleAction *action, - GVariant *parameter, - gpointer app) +static void save_activated(GSimpleAction* action, + GVariant* parameter, + gpointer app) { - GList *windows; - LSAppWindow *win; - if (parameter != NULL) - { + GList* windows; + LSAppWindow* win; + if (parameter != NULL) { app = parameter; } windows = gtk_application_get_windows(GTK_APPLICATION(app)); - if (windows) - { + if (windows) { win = LS_APP_WINDOW(windows->data); - } - else - { + } else { win = ls_app_window_new(LS_APP(app)); } - if (win->game && win->timer) - { + if (win->game && win->timer) { int width, height; gtk_window_get_size(GTK_WINDOW(win), &width, &height); win->game->width = width; @@ -934,108 +809,92 @@ static void save_activated(GSimpleAction *action, } } -static void reload_activated(GSimpleAction *action, - GVariant *parameter, - gpointer app) +static void reload_activated(GSimpleAction* action, + GVariant* parameter, + gpointer app) { - GList *windows; - LSAppWindow *win; - char *path; - if (parameter != NULL) - { + GList* windows; + LSAppWindow* win; + char* path; + if (parameter != NULL) { app = parameter; } windows = gtk_application_get_windows(GTK_APPLICATION(app)); - if (windows) - { + if (windows) { win = LS_APP_WINDOW(windows->data); - } - else - { + } else { win = ls_app_window_new(LS_APP(app)); } - if (win->game) - { + if (win->game) { path = strdup(win->game->path); ls_app_window_open(win, path); free(path); } } -static void close_activated(GSimpleAction *action, - GVariant *parameter, - gpointer app) +static void close_activated(GSimpleAction* action, + GVariant* parameter, + gpointer app) { - GList *windows; - LSAppWindow *win; - if (parameter != NULL) - { + GList* windows; + LSAppWindow* win; + if (parameter != NULL) { app = parameter; } - + windows = gtk_application_get_windows(GTK_APPLICATION(app)); - if (windows) - { + if (windows) { win = LS_APP_WINDOW(windows->data); - } - else - { + } else { win = ls_app_window_new(LS_APP(app)); } - if (win->game && win->timer) - { + if (win->game && win->timer) { ls_app_window_clear_game(win); } - if (win->timer) - { + if (win->timer) { ls_timer_release(win->timer); win->timer = 0; } - if (win->game) - { + if (win->game) { ls_game_release(win->game); win->game = 0; } gtk_widget_set_size_request(GTK_WIDGET(win), -1, -1); } -static void quit_activated(GSimpleAction *action, - GVariant *parameter, - gpointer app) +static void quit_activated(GSimpleAction* action, + GVariant* parameter, + gpointer app) { exit(0); } -static void toggle_auto_splitter(GtkCheckMenuItem *menu_item, gpointer user_data) +static void toggle_auto_splitter(GtkCheckMenuItem* menu_item, gpointer user_data) { gboolean active = gtk_check_menu_item_get_active(menu_item); - if (active) - { + if (active) { atomic_store(&auto_splitter_enabled, 1); ls_update_setting("auto_splitter_enabled", json_true()); - } - else - { + } else { atomic_store(&auto_splitter_enabled, 0); ls_update_setting("auto_splitter_enabled", json_false()); } } // Create the context menu -static gboolean button_right_click(GtkWidget *widget, GdkEventButton *event, gpointer app) +static gboolean button_right_click(GtkWidget* widget, GdkEventButton* event, gpointer app) { - if (event->button == GDK_BUTTON_SECONDARY) - { - GtkWidget *menu = gtk_menu_new(); - GtkWidget *menu_open_splits = gtk_menu_item_new_with_label("Open Splits"); - GtkWidget *menu_save_splits = gtk_menu_item_new_with_label("Save Splits"); - GtkWidget *menu_open_auto_splitter = gtk_menu_item_new_with_label("Open Auto Splitter"); - GtkWidget *menu_enable_auto_splitter = gtk_check_menu_item_new_with_label("Enable Auto Splitter"); + if (event->button == GDK_BUTTON_SECONDARY) { + GtkWidget* menu = gtk_menu_new(); + GtkWidget* menu_open_splits = gtk_menu_item_new_with_label("Open Splits"); + GtkWidget* menu_save_splits = gtk_menu_item_new_with_label("Save Splits"); + GtkWidget* menu_open_auto_splitter = gtk_menu_item_new_with_label("Open Auto Splitter"); + GtkWidget* menu_enable_auto_splitter = gtk_check_menu_item_new_with_label("Enable Auto Splitter"); gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_enable_auto_splitter), atomic_load(&auto_splitter_enabled)); - GtkWidget *menu_reload = gtk_menu_item_new_with_label("Reload"); - GtkWidget *menu_close = gtk_menu_item_new_with_label("Close"); - GtkWidget *menu_quit = gtk_menu_item_new_with_label("Quit"); + GtkWidget* menu_reload = gtk_menu_item_new_with_label("Reload"); + GtkWidget* menu_close = gtk_menu_item_new_with_label("Close"); + GtkWidget* menu_quit = gtk_menu_item_new_with_label("Quit"); // Add the menu items to the menu gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_open_splits); @@ -1056,123 +915,104 @@ static gboolean button_right_click(GtkWidget *widget, GdkEventButton *event, gpo g_signal_connect(menu_quit, "activate", G_CALLBACK(quit_activated), app); gtk_widget_show_all(menu); - gtk_menu_popup_at_pointer(GTK_MENU(menu), (GdkEvent *)event); + gtk_menu_popup_at_pointer(GTK_MENU(menu), (GdkEvent*)event); return TRUE; } return FALSE; } -static void ls_app_activate(GApplication *app) +static void ls_app_activate(GApplication* app) { - LSAppWindow *win; + LSAppWindow* win; win = ls_app_window_new(LS_APP(app)); gtk_window_present(GTK_WINDOW(win)); - if (get_setting_value("libresplit", "split_file") != NULL) - { + if (get_setting_value("libresplit", "split_file") != NULL) { // Check if split file exists - struct stat st = {0}; + struct stat st = { 0 }; char splits_path[PATH_MAX]; strcpy(splits_path, json_string_value(get_setting_value("libresplit", "split_file"))); - if (stat(splits_path, &st) == -1) - { + if (stat(splits_path, &st) == -1) { printf("%s does not exist\n", splits_path); open_activated(NULL, NULL, app); - } - else - { + } else { ls_app_window_open(win, splits_path); } - } - else - { + } else { open_activated(NULL, NULL, app); } - if (get_setting_value("libresplit", "auto_splitter_file") != NULL) - { - struct stat st = {0}; + if (get_setting_value("libresplit", "auto_splitter_file") != NULL) { + struct stat st = { 0 }; char auto_splitters_path[PATH_MAX]; strcpy(auto_splitters_path, json_string_value(get_setting_value("libresplit", "auto_splitter_file"))); - if (stat(auto_splitters_path, &st) == -1) - { + if (stat(auto_splitters_path, &st) == -1) { printf("%s does not exist\n", auto_splitters_path); - } - else - { + } else { strcpy(auto_splitter_file, auto_splitters_path); } } - if (get_setting_value("libresplit", "auto_splitter_enabled") != NULL) - { - if (json_is_false(get_setting_value("libresplit", "auto_splitter_enabled"))) - { + if (get_setting_value("libresplit", "auto_splitter_enabled") != NULL) { + if (json_is_false(get_setting_value("libresplit", "auto_splitter_enabled"))) { atomic_store(&auto_splitter_enabled, 0); - } - else - { + } else { atomic_store(&auto_splitter_enabled, 1); } } g_signal_connect(win, "button_press_event", G_CALLBACK(button_right_click), app); } -static void ls_app_init(LSApp *app) +static void ls_app_init(LSApp* app) { } -static void ls_app_open(GApplication *app, - GFile **files, - gint n_files, - const gchar *hint) +static void ls_app_open(GApplication* app, + GFile** files, + gint n_files, + const gchar* hint) { - GList *windows; - LSAppWindow *win; + GList* windows; + LSAppWindow* win; int i; windows = gtk_application_get_windows(GTK_APPLICATION(app)); - if (windows) - { + if (windows) { win = LS_APP_WINDOW(windows->data); - } - else - { + } else { win = ls_app_window_new(LS_APP(app)); } - for (i = 0; i < n_files; i++) - { + for (i = 0; i < n_files; i++) { ls_app_window_open(win, g_file_get_path(files[i])); } gtk_window_present(GTK_WINDOW(win)); } -LSApp *ls_app_new(void) +LSApp* ls_app_new(void) { g_set_application_name("LS"); return g_object_new(LS_APP_TYPE, - "application-id", "com.github.wins1ey.libresplit", - "flags", G_APPLICATION_HANDLES_OPEN, - NULL); + "application-id", "com.github.wins1ey.libresplit", + "flags", G_APPLICATION_HANDLES_OPEN, + NULL); } -static void ls_app_class_init(LSAppClass *class) +static void ls_app_class_init(LSAppClass* class) { G_APPLICATION_CLASS(class)->activate = ls_app_activate; G_APPLICATION_CLASS(class)->open = ls_app_open; } -static void *ls_auto_splitter() +static void* ls_auto_splitter() { while (1) { - if (atomic_load(&auto_splitter_enabled) && auto_splitter_file[0] != '\0') - { + if (atomic_load(&auto_splitter_enabled) && auto_splitter_file[0] != '\0') { run_auto_splitter(); } - if(atomic_load(&exit_requested)) + if (atomic_load(&exit_requested)) return 0; usleep(50000); } return NULL; } -int main(int argc, char *argv[]) +int main(int argc, char* argv[]) { check_directories(); diff --git a/src/memory.c b/src/memory.c index 9806b64..92191f1 100644 --- a/src/memory.c +++ b/src/memory.c @@ -1,10 +1,10 @@ +#include #include #include #include #include #include #include -#include #include @@ -14,32 +14,29 @@ bool memory_error; extern game_process process; -#define READ_MEMORY_FUNCTION(value_type) \ - value_type read_memory_##value_type(uint64_t mem_address, int32_t* err) \ - { \ - value_type value; \ - \ - struct iovec mem_local; \ - struct iovec mem_remote; \ - \ - mem_local.iov_base = &value; \ - mem_local.iov_len = sizeof(value); \ - mem_remote.iov_len = sizeof(value); \ - mem_remote.iov_base = (void*)(uintptr_t)mem_address; \ - \ - ssize_t mem_n_read = process_vm_readv(process.pid, &mem_local, 1, &mem_remote, 1, 0); \ - if (mem_n_read == -1) \ - { \ - *err = (int32_t)errno; \ - memory_error = true; \ - } \ - else if (mem_n_read != (ssize_t)mem_remote.iov_len) \ - { \ +#define READ_MEMORY_FUNCTION(value_type) \ + value_type read_memory_##value_type(uint64_t mem_address, int32_t* err) \ + { \ + value_type value; \ + \ + struct iovec mem_local; \ + struct iovec mem_remote; \ + \ + mem_local.iov_base = &value; \ + mem_local.iov_len = sizeof(value); \ + mem_remote.iov_len = sizeof(value); \ + mem_remote.iov_base = (void*)(uintptr_t)mem_address; \ + \ + ssize_t mem_n_read = process_vm_readv(process.pid, &mem_local, 1, &mem_remote, 1, 0); \ + if (mem_n_read == -1) { \ + *err = (int32_t)errno; \ + memory_error = true; \ + } else if (mem_n_read != (ssize_t)mem_remote.iov_len) { \ printf("Error reading process memory: short read of %ld bytes\n", (long)mem_n_read); \ - exit(1); \ - } \ - \ - return value; \ + exit(1); \ + } \ + \ + return value; \ } READ_MEMORY_FUNCTION(int8_t) @@ -57,8 +54,7 @@ READ_MEMORY_FUNCTION(bool) char* read_memory_string(uint64_t mem_address, int buffer_size) { char* buffer = (char*)malloc(buffer_size); - if (buffer == NULL) - { + if (buffer == NULL) { // Handle memory allocation failure return NULL; } @@ -72,12 +68,9 @@ char* read_memory_string(uint64_t mem_address, int buffer_size) mem_remote.iov_base = (void*)(uintptr_t)mem_address; ssize_t mem_n_read = process_vm_readv(process.pid, &mem_local, 1, &mem_remote, 1, 0); - if (mem_n_read == -1) - { + if (mem_n_read == -1) { buffer[0] = '\0'; - } - else if (mem_n_read != (ssize_t)mem_remote.iov_len) - { + } else if (mem_n_read != (ssize_t)mem_remote.iov_len) { printf("Error reading process memory: short read of %ld bytes\n", (long)mem_n_read); exit(1); } @@ -90,14 +83,26 @@ char* read_memory_string(uint64_t mem_address, int buffer_size) True if the error was printed False if the error is unknown */ -bool handle_memory_error(uint32_t err) { - if (err == 0) return false; +bool handle_memory_error(uint32_t err) +{ + if (err == 0) + return false; switch (err) { - case EFAULT: printf("EFAULT: Invalid memory space/address\n"); break; - case EINVAL: printf("EINVAL: An error ocurred while reading memory\n"); break; - case ENOMEM: printf("ENOMEM: Please get more memory\n"); break; - case EPERM: printf("EPERM: Permission denied\n"); break; - case ESRCH: printf("ESRCH: No process with specified PID exists\n"); break; + case EFAULT: + printf("EFAULT: Invalid memory space/address\n"); + break; + case EINVAL: + printf("EINVAL: An error ocurred while reading memory\n"); + break; + case ENOMEM: + printf("ENOMEM: Please get more memory\n"); + break; + case EPERM: + printf("EPERM: Permission denied\n"); + break; + case ESRCH: + printf("ESRCH: No process with specified PID exists\n"); + break; } return true; } @@ -109,16 +114,12 @@ int read_address(lua_State* L) const char* value_type = lua_tostring(L, 1); int i; - if (lua_isnumber(L, 2)) - { + if (lua_isnumber(L, 2)) { address = process.base_address + lua_tointeger(L, 2); i = 3; - } - else - { - const char* module = lua_tostring(L,2); - if (strcmp(process.name, module) != 0) - { + } else { + const char* module = lua_tostring(L, 2); + if (strcmp(process.name, module) != 0) { process.dll_address = find_base_address(module); } address = process.dll_address + lua_tointeger(L, 3); @@ -127,79 +128,53 @@ int read_address(lua_State* L) int error = 0; - for (; i <= lua_gettop(L); i++) - { - if (address <= UINT32_MAX) - { + for (; i <= lua_gettop(L); i++) { + if (address <= UINT32_MAX) { address = read_memory_uint32_t((uint64_t)address, &error); - if (memory_error) break; - } - else - { + if (memory_error) + break; + } else { address = read_memory_uint64_t(address, &error); - if (memory_error) break; + if (memory_error) + break; } address += lua_tointeger(L, i); } - - if (strcmp(value_type, "sbyte") == 0) - { + if (strcmp(value_type, "sbyte") == 0) { int8_t value = read_memory_int8_t(address, &error); lua_pushinteger(L, (int)value); - } - else if (strcmp(value_type, "byte") == 0) - { + } else if (strcmp(value_type, "byte") == 0) { uint8_t value = read_memory_uint8_t(address, &error); lua_pushinteger(L, (int)value); - } - else if (strcmp(value_type, "short") == 0) - { + } else if (strcmp(value_type, "short") == 0) { short value = read_memory_int16_t(address, &error); lua_pushinteger(L, (int)value); - } - else if (strcmp(value_type, "ushort") == 0) - { + } else if (strcmp(value_type, "ushort") == 0) { unsigned short value = read_memory_uint16_t(address, &error); lua_pushinteger(L, (int)value); - } - else if (strcmp(value_type, "int") == 0) - { + } else if (strcmp(value_type, "int") == 0) { int value = read_memory_int32_t(address, &error); lua_pushinteger(L, value); - } - else if (strcmp(value_type, "uint") == 0) - { + } else if (strcmp(value_type, "uint") == 0) { unsigned int value = read_memory_uint32_t(address, &error); lua_pushinteger(L, (int)value); - } - else if (strcmp(value_type, "long") == 0) - { + } else if (strcmp(value_type, "long") == 0) { long value = read_memory_int64_t(address, &error); lua_pushinteger(L, (int)value); - } - else if (strcmp(value_type, "ulong") == 0) - { + } else if (strcmp(value_type, "ulong") == 0) { unsigned long value = read_memory_uint64_t(address, &error); lua_pushinteger(L, (int)value); - } - else if (strcmp(value_type, "float") == 0) - { + } else if (strcmp(value_type, "float") == 0) { float value = read_memory_float(address, &error); lua_pushnumber(L, (double)value); - } - else if (strcmp(value_type, "double") == 0) - { + } else if (strcmp(value_type, "double") == 0) { double value = read_memory_double(address, &error); lua_pushnumber(L, value); - } - else if (strcmp(value_type, "bool") == 0) - { + } else if (strcmp(value_type, "bool") == 0) { bool value = read_memory_bool(address, &error); lua_pushboolean(L, value ? 1 : 0); - } - else if (strstr(value_type, "string") != NULL) - { + } else if (strstr(value_type, "string") != NULL) { int buffer_size = atoi(value_type + 6); if (buffer_size < 2) { printf("Invalid string size, please read documentation"); @@ -209,15 +184,12 @@ int read_address(lua_State* L) lua_pushstring(L, value != NULL ? value : ""); free(value); return 1; - } - else - { + } else { printf("Invalid value type: %s\n", value_type); exit(1); } - if (memory_error) - { + if (memory_error) { lua_pushinteger(L, -1); handle_memory_error(error); } diff --git a/src/memory.h b/src/memory.h index c38ce36..4173ed8 100644 --- a/src/memory.h +++ b/src/memory.h @@ -2,10 +2,10 @@ #define __MEMORY_H__ #include -#include #include +#include -ssize_t process_vm_readv(int pid, struct iovec *mem_local, int liovcnt, struct iovec *mem_remote, int riovcnt, int flags); +ssize_t process_vm_readv(int pid, struct iovec* mem_local, int liovcnt, struct iovec* mem_remote, int riovcnt, int flags); int read_address(lua_State* L); diff --git a/src/process.c b/src/process.c index 29c94c2..fc8d7a7 100644 --- a/src/process.c +++ b/src/process.c @@ -1,15 +1,15 @@ #include +#include #include #include #include #include #include -#include #include -#include "process.h" #include "auto-splitter.h" +#include "process.h" struct game_process process; #define MAPS_CACHE_MAX_SIZE 32 @@ -20,14 +20,12 @@ void execute_command(const char* command, char* output) { char buffer[4096]; FILE* pipe = popen(command, "r"); - if (!pipe) - { + if (!pipe) { fprintf(stderr, "Error executing command: %s\n", command); exit(1); } - while (fgets(buffer, 128, pipe) != NULL) - { + while (fgets(buffer, 128, pipe) != NULL) { strcat(output, buffer); } @@ -53,7 +51,7 @@ uintptr_t find_base_address(const char* module) snprintf(path, sizeof(path), "/proc/%d/maps", process.pid); - FILE *f = fopen(path, "r"); + FILE* f = fopen(path, "r"); if (f) { char current_line[PATH_MAX + 100]; @@ -66,7 +64,7 @@ uintptr_t find_base_address(const char* module) ProcessMap map; if (parseMapsLine(current_line, &map)) { p_maps_cache[p_maps_cache_size] = map; - p_maps_cache_size++; + p_maps_cache_size++; } } return addr_start; @@ -82,15 +80,13 @@ void stock_process_id(const char* pid_command) char pid_output[PATH_MAX + 100]; pid_output[0] = '\0'; - while (atomic_load(&auto_splitter_enabled)) - { + while (atomic_load(&auto_splitter_enabled)) { execute_command(pid_command, pid_output); process.pid = strtoul(pid_output, NULL, 10); printf("\033[2J\033[1;1H"); // Clear the console if (process.pid) { size_t newlinePos = strcspn(pid_output, "\n"); - if (newlinePos != strlen(pid_output) - 1 && pid_output[0] != '\0') - { + if (newlinePos != strlen(pid_output) - 1 && pid_output[0] != '\0') { printf("Multiple PID's found for process: %s\n", process.name); } break; @@ -118,7 +114,8 @@ int find_process_id(lua_State* L) return 0; } -int getPid(lua_State* L) { +int getPid(lua_State* L) +{ lua_pushinteger(L, process.pid); return 1; } @@ -129,16 +126,17 @@ int process_exists() return result == 0; } -bool parseMapsLine(char* line,ProcessMap *map) { +bool parseMapsLine(char* line, ProcessMap* map) +{ size_t end; - char mode[8]; - unsigned long offset; - unsigned int major_id, minor_id, node_id; + char mode[8]; + unsigned long offset; + unsigned int major_id, minor_id, node_id; // Thank you kernel source code int sscanf_res = sscanf(line, "%lx-%lx %7s %lx %u:%u %u %s", &map->start, - &end, mode, &offset, &major_id, - &minor_id, &node_id, map->name); + &end, mode, &offset, &major_id, + &minor_id, &node_id, map->name); if (!sscanf_res) return false; diff --git a/src/process.h b/src/process.h index 4b04ea4..27a5fa7 100644 --- a/src/process.h +++ b/src/process.h @@ -7,9 +7,8 @@ #include -struct game_process -{ - const char *name; +struct game_process { + const char* name; int pid; uintptr_t base_address; uintptr_t dll_address; @@ -26,6 +25,6 @@ uintptr_t find_base_address(const char* module); int process_exists(); int find_process_id(lua_State* L); int getPid(lua_State* L); -bool parseMapsLine(char* line, ProcessMap *map); +bool parseMapsLine(char* line, ProcessMap* map); #endif /* __PROCESS_H__ */ diff --git a/src/settings.c b/src/settings.c index 97ff6ca..e21117c 100644 --- a/src/settings.c +++ b/src/settings.c @@ -1,21 +1,21 @@ #include -#include #include +#include #include -#include #include -#include +#include #include #include "settings.h" -void get_libresplit_folder_path(char* out_path) { - struct passwd * pw = getpwuid(getuid()); +void get_libresplit_folder_path(char* out_path) +{ + struct passwd* pw = getpwuid(getuid()); char* XDG_CONFIG_HOME = getenv("XDG_CONFIG_HOME"); char* base_dir = strcat(pw->pw_dir, "/.config/libresplit"); if (XDG_CONFIG_HOME != NULL) { - char config_dir[PATH_MAX] = {0}; + char config_dir[PATH_MAX] = { 0 }; strcpy(config_dir, XDG_CONFIG_HOME); strcat(config_dir, "/libresplit"); strcpy(base_dir, config_dir); @@ -23,101 +23,86 @@ void get_libresplit_folder_path(char* out_path) { strcpy(out_path, base_dir); } -void ls_update_setting(const char *setting, json_t *value) +void ls_update_setting(const char* setting, json_t* value) { char settings_path[PATH_MAX]; get_libresplit_folder_path(settings_path); strcat(settings_path, "/settings.json"); // Load existing settings - json_t *root = NULL; - FILE *file = fopen(settings_path, "r"); - if (file) - { + json_t* root = NULL; + FILE* file = fopen(settings_path, "r"); + if (file) { json_error_t error; root = json_loadf(file, 0, &error); fclose(file); - if (!root) - { + if (!root) { printf("Failed to load settings: %s\n", error.text); return; } - } - else - { + } else { // If file doesn't exist, create a new settings object root = json_object(); } // Update specific setting - json_t *ls_obj = json_object_get(root, "libresplit"); - if (!ls_obj) - { + json_t* ls_obj = json_object_get(root, "libresplit"); + if (!ls_obj) { ls_obj = json_object(); json_object_set(root, "libresplit", ls_obj); } json_object_set_new(ls_obj, setting, value); // Save updated settings back to the file - FILE *output_file = fopen(settings_path, "w"); - if (output_file) - { + FILE* output_file = fopen(settings_path, "w"); + if (output_file) { json_dumpf(root, output_file, JSON_INDENT(4)); fclose(output_file); - } - else - { + } else { printf("Failed to save settings to %s\n", settings_path); } json_decref(root); } -json_t *load_settings() +json_t* load_settings() { char settings_path[PATH_MAX]; get_libresplit_folder_path(settings_path); strcat(settings_path, "/settings.json"); - FILE *file = fopen(settings_path, "r"); - if (file) - { + FILE* file = fopen(settings_path, "r"); + if (file) { json_error_t error; - json_t *root = json_loadf(file, 0, &error); + json_t* root = json_loadf(file, 0, &error); fclose(file); - if (!root) - { + if (!root) { printf("Failed to load settings: %s\n", error.text); return NULL; } return root; - } - else - { + } else { printf("Failed to open settings file\n"); return NULL; } } -json_t *get_setting_value(const char *section, const char *setting) +json_t* get_setting_value(const char* section, const char* setting) { - json_t *root = load_settings(); - if (!root) - { + json_t* root = load_settings(); + if (!root) { return NULL; } - json_t *section_obj = json_object_get(root, section); - if (!section_obj) - { + json_t* section_obj = json_object_get(root, section); + if (!section_obj) { printf("Section '%s' not found\n", section); json_decref(root); return NULL; } - json_t *value = json_object_get(section_obj, setting); - if (!value) - { + json_t* value = json_object_get(section_obj, setting); + if (!value) { printf("Setting '%s' not found in section '%s'\n", setting, section); json_decref(root); return NULL; diff --git a/src/settings.h b/src/settings.h index 2526473..e1c888e 100644 --- a/src/settings.h +++ b/src/settings.h @@ -4,7 +4,7 @@ #include void get_libresplit_folder_path(char* out_path); -void ls_update_setting(const char *setting, json_t *value); -json_t *get_setting_value(const char *section, const char *setting); +void ls_update_setting(const char* setting, json_t* value); +json_t* get_setting_value(const char* section, const char* setting); #endif /* __SETTINGS_H__ */ diff --git a/src/timer.c b/src/timer.c index 7b2d023..b86415b 100644 --- a/src/timer.c +++ b/src/timer.c @@ -1,9 +1,9 @@ +#include "timer.h" +#include #include #include #include #include -#include -#include "timer.h" long long ls_time_now(void) { @@ -12,7 +12,7 @@ long long ls_time_now(void) return timespec.tv_sec * 1000000LL + timespec.tv_nsec / 1000; } -long long ls_time_value(const char *string) +long long ls_time_value(const char* string) { char seconds_part[256]; double subseconds_part = 0.; @@ -20,19 +20,16 @@ long long ls_time_value(const char *string) int minutes = 0; int seconds = 0; int sign = 1; - if (!string || !strlen(string)) - { + if (!string || !strlen(string)) { return 0; } sscanf(string, "%[^.]%lf", seconds_part, &subseconds_part); string = seconds_part; - if (string[0] == '-') - { + if (string[0] == '-') { sign = -1; ++string; } - switch (sscanf(string, "%d:%d:%d", &hours, &minutes, &seconds)) - { + switch (sscanf(string, "%d:%d:%d", &hours, &minutes, &seconds)) { case 2: seconds = minutes; minutes = hours; @@ -44,392 +41,318 @@ long long ls_time_value(const char *string) hours = 0; break; } - return sign * ((hours * 60 * 60 - + minutes * 60 - + seconds) * 1000000LL - + (int)(subseconds_part * 1000000.)); + return sign * ((hours * 60 * 60 + minutes * 60 + seconds) * 1000000LL + (int)(subseconds_part * 1000000.)); } -static void ls_time_string_format(char *string, - char *millis, - long long time, - int serialized, - int delta, - int compact) +static void ls_time_string_format(char* string, + char* millis, + long long time, + int serialized, + int delta, + int compact) { int hours, minutes, seconds; char dot_subsecs[256]; - const char *sign = ""; - if (time < 0) - { + const char* sign = ""; + if (time < 0) { time = -time; sign = "-"; - } - else if (delta) - { + } else if (delta) { sign = "+"; } hours = time / (1000000LL * 60 * 60); minutes = (time / (1000000LL * 60)) % 60; seconds = (time / 1000000LL) % 60; sprintf(dot_subsecs, ".%06lld", time % 1000000LL); - if (!serialized) - { + if (!serialized) { /* Show only a dot and 2 decimal places instead of all 6 */ dot_subsecs[3] = '\0'; } - if (millis) - { + if (millis) { strcpy(millis, &dot_subsecs[1]); dot_subsecs[0] = '\0'; } - if (hours) - { - if (!compact) - { + if (hours) { + if (!compact) { sprintf(string, "%s%d:%02d:%02d%s", - sign, hours, minutes, seconds, dot_subsecs); - } - else - { + sign, hours, minutes, seconds, dot_subsecs); + } else { sprintf(string, "%s%d:%02d", sign, hours, minutes); } - } - else if (minutes) - { - if (!compact) - { + } else if (minutes) { + if (!compact) { sprintf(string, "%s%d:%02d%s", - sign, minutes, seconds, dot_subsecs); - } - else - { + sign, minutes, seconds, dot_subsecs); + } else { sprintf(string, "%s%d:%02d", sign, minutes, seconds); } - } - else - { + } else { sprintf(string, "%s%d%s", sign, seconds, dot_subsecs); } } -static void ls_time_string_serialized(char *string, - long long time) +static void ls_time_string_serialized(char* string, + long long time) { ls_time_string_format(string, NULL, time, 1, 0, 0); } -void ls_time_string(char *string, long long time) +void ls_time_string(char* string, long long time) { ls_time_string_format(string, NULL, time, 0, 0, 0); } -void ls_time_millis_string(char *seconds, char *millis, long long time) +void ls_time_millis_string(char* seconds, char* millis, long long time) { ls_time_string_format(seconds, millis, time, 0, 0, 0); } -void ls_split_string(char *string, long long time) +void ls_split_string(char* string, long long time) { ls_time_string_format(string, NULL, time, 0, 0, 1); } -void ls_delta_string(char *string, long long time) +void ls_delta_string(char* string, long long time) { ls_time_string_format(string, NULL, time, 0, 1, 1); } -void ls_game_release(ls_game *game) +void ls_game_release(ls_game* game) { int i; - if (game->path) - { + if (game->path) { free(game->path); } - if (game->title) - { + if (game->title) { free(game->title); } - if (game->theme) - { + if (game->theme) { free(game->theme); } - if (game->theme_variant) - { + if (game->theme_variant) { free(game->theme_variant); } - if (game->split_titles) - { - for (i = 0; i < game->split_count; ++i) - { - if (game->split_titles[i]) - { + if (game->split_titles) { + for (i = 0; i < game->split_count; ++i) { + if (game->split_titles[i]) { free(game->split_titles[i]); } } free(game->split_titles); } - if (game->split_times) - { + if (game->split_times) { free(game->split_times); } - if (game->segment_times) - { + if (game->segment_times) { free(game->segment_times); } - if (game->best_splits) - { + if (game->best_splits) { free(game->best_splits); } - if (game->best_segments) - { + if (game->best_segments) { free(game->best_segments); } } -int ls_game_create(ls_game **game_ptr, const char *path) +int ls_game_create(ls_game** game_ptr, const char* path) { int error = 0; - ls_game *game; + ls_game* game; int i; - json_t *json = 0; - json_t *ref; + json_t* json = 0; + json_t* ref; json_error_t json_error; // allocate game game = calloc(1, sizeof(ls_game)); - if (!game) - { + if (!game) { error = 1; goto game_create_done; } // copy path to file game->path = strdup(path); - if (!game->path) - { + if (!game->path) { error = 1; goto game_create_done; } // load json json = json_load_file(game->path, 0, &json_error); - if (!json) - { + if (!json) { error = 1; goto game_create_done; } // copy title ref = json_object_get(json, "title"); - if (ref) - { + if (ref) { game->title = strdup(json_string_value(ref)); - if (!game->title) - { + if (!game->title) { error = 1; goto game_create_done; } } // copy theme ref = json_object_get(json, "theme"); - if (ref) - { + if (ref) { game->theme = strdup(json_string_value(ref)); - if (!game->theme) - { + if (!game->theme) { error = 1; goto game_create_done; } } // copy theme variant ref = json_object_get(json, "theme_variant"); - if (ref) - { + if (ref) { game->theme_variant = strdup(json_string_value(ref)); - if (!game->theme_variant) - { + if (!game->theme_variant) { error = 1; goto game_create_done; } } // get attempt count ref = json_object_get(json, "attempt_count"); - if (ref) - { + if (ref) { game->attempt_count = json_integer_value(ref); } // get width ref = json_object_get(json, "width"); - if (ref) - { + if (ref) { game->width = json_integer_value(ref); } // get height ref = json_object_get(json, "height"); - if (ref) - { + if (ref) { game->height = json_integer_value(ref); } // get delay ref = json_object_get(json, "start_delay"); - if (ref) - { + if (ref) { game->start_delay = ls_time_value( json_string_value(ref)); } // get wr ref = json_object_get(json, "world_record"); - if (ref) - { + if (ref) { game->world_record = ls_time_value( json_string_value(ref)); } // get splits ref = json_object_get(json, "splits"); - if (ref) - { + if (ref) { game->split_count = json_array_size(ref); // allocate titles game->split_titles = calloc(game->split_count, - sizeof(char *)); - if (!game->split_titles) - { + sizeof(char*)); + if (!game->split_titles) { error = 1; goto game_create_done; } // allocate splits game->split_times = calloc(game->split_count, - sizeof(long long)); - if (!game->split_times) - { + sizeof(long long)); + if (!game->split_times) { error = 1; goto game_create_done; } game->segment_times = calloc(game->split_count, - sizeof(long long)); - if (!game->segment_times) - { + sizeof(long long)); + if (!game->segment_times) { error = 1; goto game_create_done; } game->best_splits = calloc(game->split_count, - sizeof(long long)); - if (!game->best_splits) - { + sizeof(long long)); + if (!game->best_splits) { error = 1; goto game_create_done; } game->best_segments = calloc(game->split_count, - sizeof(long long)); - if (!game->best_segments) - { + sizeof(long long)); + if (!game->best_segments) { error = 1; goto game_create_done; } // copy splits - for (i = 0; i < game->split_count; ++i) - { - json_t *split; - json_t *split_ref; + for (i = 0; i < game->split_count; ++i) { + json_t* split; + json_t* split_ref; split = json_array_get(ref, i); split_ref = json_object_get(split, "title"); - if (split_ref) - { + if (split_ref) { game->split_titles[i] = strdup( json_string_value(split_ref)); - if (!game->split_titles[i]) - { + if (!game->split_titles[i]) { error = 1; goto game_create_done; } } split_ref = json_object_get(split, "time"); - if (split_ref) - { + if (split_ref) { game->split_times[i] = ls_time_value( json_string_value(split_ref)); } - if (i && game->split_times[i] && game->split_times[i-1]) - { - game->segment_times[i] = game->split_times[i] - game->split_times[i-1]; - } - else if (!i && game->split_times[0]) - { + if (i && game->split_times[i] && game->split_times[i - 1]) { + game->segment_times[i] = game->split_times[i] - game->split_times[i - 1]; + } else if (!i && game->split_times[0]) { game->segment_times[0] = game->split_times[0]; } split_ref = json_object_get(split, "best_time"); - if (split_ref) - { + if (split_ref) { game->best_splits[i] = ls_time_value( json_string_value(split_ref)); - } - else if (game->split_times[i]) - { + } else if (game->split_times[i]) { game->best_splits[i] = game->split_times[i]; } split_ref = json_object_get(split, "best_segment"); - if (split_ref) - { + if (split_ref) { game->best_segments[i] = ls_time_value( json_string_value(split_ref)); - } - else if (game->segment_times[i]) - { + } else if (game->segment_times[i]) { game->best_segments[i] = game->segment_times[i]; } } } - game_create_done: - if (!error) - { +game_create_done: + if (!error) { *game_ptr = game; - } - else if (game) - { + } else if (game) { ls_game_release(game); } - if (json) - { + if (json) { json_decref(json); } return error; } -void ls_game_update_splits(ls_game *game, - const ls_timer *timer) +void ls_game_update_splits(ls_game* game, + const ls_timer* timer) { - if (timer->curr_split) - { + if (timer->curr_split) { int size; if (timer->split_times[game->split_count - 1] && timer->split_times[game->split_count - 1] - < game->world_record) - { + < game->world_record) { game->world_record = timer->split_times[game->split_count - 1]; } size = timer->curr_split * sizeof(long long); if (timer->split_times[game->split_count - 1] - < game->split_times[game->split_count - 1]) - { + < game->split_times[game->split_count - 1]) { memcpy(game->split_times, timer->split_times, size); } memcpy(game->segment_times, timer->segment_times, size); - for (int i = 0; i < game->split_count; ++i) - { - if (timer->split_times[i] < game->best_splits[i]) - { + for (int i = 0; i < game->split_count; ++i) { + if (timer->split_times[i] < game->best_splits[i]) { game->best_splits[i] = timer->split_times[i]; } - if (timer->segment_times[i] < game->best_segments[i]) - { + if (timer->segment_times[i] < game->best_segments[i]) { game->best_segments[i] = timer->segment_times[i]; } } } } -void ls_game_update_bests(ls_game *game, - const ls_timer *timer) +void ls_game_update_bests(ls_game* game, + const ls_timer* timer) { - if (timer->curr_split) - { + if (timer->curr_split) { int size; size = timer->curr_split * sizeof(long long); memcpy(game->best_splits, timer->best_splits, size); @@ -437,37 +360,32 @@ void ls_game_update_bests(ls_game *game, } } -int ls_game_save(const ls_game *game) +int ls_game_save(const ls_game* game) { int error = 0; char str[256]; - json_t *json = json_object(); - json_t *splits = json_array(); + json_t* json = json_object(); + json_t* splits = json_array(); int i; - if (game->title) - { + if (game->title) { json_object_set_new(json, "title", json_string(game->title)); } - if (game->attempt_count) - { + if (game->attempt_count) { json_object_set_new(json, "attempt_count", - json_integer(game->attempt_count)); + json_integer(game->attempt_count)); } - if (game->world_record) - { + if (game->world_record) { ls_time_string_serialized(str, game->world_record); json_object_set_new(json, "world_record", json_string(str)); } - if (game->start_delay) - { + if (game->start_delay) { ls_time_string_serialized(str, game->start_delay); json_object_set_new(json, "start_delay", json_string(str)); } - for (i = 0; i < game->split_count; ++i) - { - json_t *split = json_object(); + for (i = 0; i < game->split_count; ++i) { + json_t* split = json_object(); json_object_set_new(split, "title", - json_string(game->split_titles[i])); + json_string(game->split_titles[i])); ls_time_string_serialized(str, game->split_times[i]); json_object_set_new(split, "time", json_string(str)); ls_time_string_serialized(str, game->best_splits[i]); @@ -477,65 +395,53 @@ int ls_game_save(const ls_game *game) json_array_append_new(splits, split); } json_object_set_new(json, "splits", splits); - if (game->theme) - { + if (game->theme) { json_object_set_new(json, "theme", json_string(game->theme)); } - if (game->theme_variant) - { + if (game->theme_variant) { json_object_set_new(json, "theme_variant", - json_string(game->theme_variant)); + json_string(game->theme_variant)); } - if (game->width) - { + if (game->width) { json_object_set_new(json, "width", json_integer(game->width)); } - if (game->height) - { + if (game->height) { json_object_set_new(json, "height", json_integer(game->height)); } if (!json_dump_file(json, game->path, - JSON_PRESERVE_ORDER | JSON_INDENT(2))) - { + JSON_PRESERVE_ORDER | JSON_INDENT(2))) { error = 1; } json_decref(json); return error; } -void ls_timer_release(ls_timer *timer) +void ls_timer_release(ls_timer* timer) { - if (timer->split_times) - { + if (timer->split_times) { free(timer->split_times); } - if (timer->split_deltas) - { + if (timer->split_deltas) { free(timer->split_deltas); } - if (timer->segment_times) - { + if (timer->segment_times) { free(timer->segment_times); } - if (timer->segment_deltas) - { + if (timer->segment_deltas) { free(timer->segment_deltas); } - if (timer->split_info) - { + if (timer->split_info) { free(timer->split_info); } - if (timer->best_splits) - { + if (timer->best_splits) { free(timer->best_splits); } - if (timer->best_segments) - { + if (timer->best_segments) { free(timer->best_segments); } } -static void reset_timer(ls_timer *timer) +static void reset_timer(ls_timer* timer) { int i; int size; @@ -553,32 +459,25 @@ static void reset_timer(ls_timer *timer) size = timer->game->split_count * sizeof(int); memset(timer->split_info, 0, size); timer->sum_of_bests = 0; - for (i = 0; i < timer->game->split_count; ++i) - { - if (timer->best_segments[i]) - { + for (i = 0; i < timer->game->split_count; ++i) { + if (timer->best_segments[i]) { timer->sum_of_bests += timer->best_segments[i]; - } - else if (timer->game->best_segments[i]) - { + } else if (timer->game->best_segments[i]) { timer->sum_of_bests += timer->game->best_segments[i]; - } - else - { + } else { timer->sum_of_bests = 0; break; } } } -int ls_timer_create(ls_timer **timer_ptr, ls_game *game) +int ls_timer_create(ls_timer** timer_ptr, ls_game* game) { int error = 0; - ls_timer *timer; + ls_timer* timer; // allocate timer timer = calloc(1, sizeof(ls_timer)); - if (!timer) - { + if (!timer) { error = 1; goto timer_create_done; } @@ -586,146 +485,113 @@ int ls_timer_create(ls_timer **timer_ptr, ls_game *game) timer->attempt_count = &game->attempt_count; // alloc splits timer->split_times = calloc(timer->game->split_count, - sizeof(long long)); - if (!timer->split_times) - { + sizeof(long long)); + if (!timer->split_times) { error = 1; goto timer_create_done; } timer->split_deltas = calloc(timer->game->split_count, - sizeof(long long)); - if (!timer->split_deltas) - { + sizeof(long long)); + if (!timer->split_deltas) { error = 1; goto timer_create_done; } timer->segment_times = calloc(timer->game->split_count, - sizeof(long long)); - if (!timer->segment_times) - { + sizeof(long long)); + if (!timer->segment_times) { error = 1; goto timer_create_done; } timer->segment_deltas = calloc(timer->game->split_count, - sizeof(long long)); - if (!timer->segment_deltas) - { + sizeof(long long)); + if (!timer->segment_deltas) { error = 1; goto timer_create_done; } timer->best_splits = calloc(timer->game->split_count, - sizeof(long long)); - if (!timer->best_splits) - { + sizeof(long long)); + if (!timer->best_splits) { error = 1; goto timer_create_done; } timer->best_segments = calloc(timer->game->split_count, - sizeof(long long)); - if (!timer->best_segments) - { + sizeof(long long)); + if (!timer->best_segments) { error = 1; goto timer_create_done; } timer->split_info = calloc(timer->game->split_count, - sizeof(int)); - if (!timer->split_info) - { + sizeof(int)); + if (!timer->split_info) { error = 1; goto timer_create_done; } reset_timer(timer); - timer_create_done: - if (!error) - { +timer_create_done: + if (!error) { *timer_ptr = timer; - } - else if (timer) - { + } else if (timer) { ls_timer_release(timer); } return error; } -void ls_timer_step(ls_timer *timer, long long now) +void ls_timer_step(ls_timer* timer, long long now) { timer->now = now; - if (timer->running) - { + if (timer->running) { long long delta = timer->now - timer->start_time; - timer->time += delta; // Accumulate the elapsed time - if (timer->curr_split < timer->game->split_count) - { + timer->time += delta; // Accumulate the elapsed time + if (timer->curr_split < timer->game->split_count) { timer->split_times[timer->curr_split] = timer->time; // calc delta - if (timer->game->split_times[timer->curr_split]) - { - timer->split_deltas[timer->curr_split] = - timer->split_times[timer->curr_split] + if (timer->game->split_times[timer->curr_split]) { + timer->split_deltas[timer->curr_split] = timer->split_times[timer->curr_split] - timer->game->split_times[timer->curr_split]; } // check for behind time - if (timer->split_deltas[timer->curr_split] > 0) - { + if (timer->split_deltas[timer->curr_split] > 0) { timer->split_info[timer->curr_split] |= LS_INFO_BEHIND_TIME; - } - else - { + } else { timer->split_info[timer->curr_split] &= ~LS_INFO_BEHIND_TIME; } - if (!timer->curr_split || timer->split_times[timer->curr_split - 1]) - { + if (!timer->curr_split || timer->split_times[timer->curr_split - 1]) { // calc segment time and delta - timer->segment_times[timer->curr_split] = - timer->split_times[timer->curr_split]; - if (timer->curr_split) - { - timer->segment_times[timer->curr_split] -= - timer->split_times[timer->curr_split - 1]; + timer->segment_times[timer->curr_split] = timer->split_times[timer->curr_split]; + if (timer->curr_split) { + timer->segment_times[timer->curr_split] -= timer->split_times[timer->curr_split - 1]; } - if (timer->game->segment_times[timer->curr_split]) - { - timer->segment_deltas[timer->curr_split] = - timer->segment_times[timer->curr_split] + if (timer->game->segment_times[timer->curr_split]) { + timer->segment_deltas[timer->curr_split] = timer->segment_times[timer->curr_split] - timer->game->segment_times[timer->curr_split]; } } // check for losing time - if (timer->curr_split) - { + if (timer->curr_split) { if (timer->split_deltas[timer->curr_split] - > timer->split_deltas[timer->curr_split - 1]) - { + > timer->split_deltas[timer->curr_split - 1]) { timer->split_info[timer->curr_split] |= LS_INFO_LOSING_TIME; - } - else - { + } else { timer->split_info[timer->curr_split] &= ~LS_INFO_LOSING_TIME; } - } - else if (timer->split_deltas[timer->curr_split] > 0) - { + } else if (timer->split_deltas[timer->curr_split] > 0) { timer->split_info[timer->curr_split] |= LS_INFO_LOSING_TIME; - } - else - { + } else { timer->split_info[timer->curr_split] &= ~LS_INFO_LOSING_TIME; } } } - timer->start_time = now; // Update the start time for the next iteration + timer->start_time = now; // Update the start time for the next iteration } -int ls_timer_start(ls_timer *timer) +int ls_timer_start(ls_timer* timer) { - if (timer->curr_split < timer->game->split_count) - { - if (!timer->started) - { + if (timer->curr_split < timer->game->split_count) { + if (!timer->started) { ++*timer->attempt_count; timer->started = 1; } @@ -734,56 +600,43 @@ int ls_timer_start(ls_timer *timer) return timer->running; } -int ls_timer_split(ls_timer *timer) +int ls_timer_split(ls_timer* timer) { - if (timer->running && timer->time > 0) - { - if (timer->curr_split < timer->game->split_count) - { + if (timer->running && timer->time > 0) { + if (timer->curr_split < timer->game->split_count) { int i; // check for best split and segment if (!timer->best_splits[timer->curr_split] || timer->split_times[timer->curr_split] - < timer->best_splits[timer->curr_split]) - { - timer->best_splits[timer->curr_split] = - timer->split_times[timer->curr_split]; + < timer->best_splits[timer->curr_split]) { + timer->best_splits[timer->curr_split] = timer->split_times[timer->curr_split]; timer->split_info[timer->curr_split] |= LS_INFO_BEST_SPLIT; } if (!timer->best_segments[timer->curr_split] || timer->segment_times[timer->curr_split] - < timer->best_segments[timer->curr_split]) - { - timer->best_segments[timer->curr_split] = - timer->segment_times[timer->curr_split]; + < timer->best_segments[timer->curr_split]) { + timer->best_segments[timer->curr_split] = timer->segment_times[timer->curr_split]; timer->split_info[timer->curr_split] |= LS_INFO_BEST_SEGMENT; } // update sum of bests timer->sum_of_bests = 0; - for (i = 0; i < timer->game->split_count; ++i) - { - if (timer->best_segments[i]) - { + for (i = 0; i < timer->game->split_count; ++i) { + if (timer->best_segments[i]) { timer->sum_of_bests += timer->best_segments[i]; - } - else if (timer->game->best_segments[i]) - { + } else if (timer->game->best_segments[i]) { timer->sum_of_bests += timer->game->best_segments[i]; - } - else - { + } else { timer->sum_of_bests = 0; break; } } ++timer->curr_split; // stop timer if last split - if (timer->curr_split == timer->game->split_count) - { + if (timer->curr_split == timer->game->split_count) { ls_timer_stop(timer); - ls_game_update_splits((ls_game *)timer->game, timer); + ls_game_update_splits((ls_game*)timer->game, timer); } return timer->curr_split; } @@ -791,12 +644,10 @@ int ls_timer_split(ls_timer *timer) return 0; } -int ls_timer_skip(ls_timer *timer) +int ls_timer_skip(ls_timer* timer) { - if (timer->running && timer->time > 0) - { - if (timer->curr_split < timer->game->split_count) - { + if (timer->running && timer->time > 0) { + if (timer->curr_split < timer->game->split_count) { timer->split_times[timer->curr_split] = 0; timer->split_deltas[timer->curr_split] = 0; timer->split_info[timer->curr_split] = 0; @@ -808,22 +659,19 @@ int ls_timer_skip(ls_timer *timer) return 0; } -int ls_timer_unsplit(ls_timer *timer) +int ls_timer_unsplit(ls_timer* timer) { - if (timer->curr_split) - { + if (timer->curr_split) { int i; int curr = --timer->curr_split; - for (i = curr; i < timer->game->split_count; ++i) - { + for (i = curr; i < timer->game->split_count; ++i) { timer->split_times[i] = timer->game->split_times[i]; timer->split_deltas[i] = 0; timer->split_info[i] = 0; timer->segment_times[i] = timer->game->segment_times[i]; timer->segment_deltas[i] = 0; } - if (timer->curr_split + 1 == timer->game->split_count) - { + if (timer->curr_split + 1 == timer->game->split_count) { timer->running = 1; } return timer->curr_split; @@ -831,17 +679,15 @@ int ls_timer_unsplit(ls_timer *timer) return 0; } -void ls_timer_stop(ls_timer *timer) +void ls_timer_stop(ls_timer* timer) { timer->running = 0; } -int ls_timer_reset(ls_timer *timer) +int ls_timer_reset(ls_timer* timer) { - if (!timer->running) - { - if (timer->started && timer->time <= 0) - { + if (!timer->running) { + if (timer->started && timer->time <= 0) { return ls_timer_cancel(timer); } reset_timer(timer); @@ -850,14 +696,11 @@ int ls_timer_reset(ls_timer *timer) return 0; } -int ls_timer_cancel(ls_timer *timer) +int ls_timer_cancel(ls_timer* timer) { - if (!timer->running) - { - if (timer->started) - { - if (*timer->attempt_count > 0) - { + if (!timer->running) { + if (timer->started) { + if (*timer->attempt_count > 0) { --*timer->attempt_count; } } diff --git a/src/timer.h b/src/timer.h index 00202ef..f6e7ef4 100644 --- a/src/timer.h +++ b/src/timer.h @@ -1,33 +1,31 @@ #ifndef __TIMER_H__ #define __TIMER_H__ -#define LS_INFO_BEHIND_TIME (1) -#define LS_INFO_LOSING_TIME (2) -#define LS_INFO_BEST_SPLIT (4) -#define LS_INFO_BEST_SEGMENT (8) - -struct ls_game -{ - char *path; - char *title; - char *theme; - char *theme_variant; +#define LS_INFO_BEHIND_TIME (1) +#define LS_INFO_LOSING_TIME (2) +#define LS_INFO_BEST_SPLIT (4) +#define LS_INFO_BEST_SEGMENT (8) + +struct ls_game { + char* path; + char* title; + char* theme; + char* theme_variant; int attempt_count; int width; int height; long long world_record; long long start_delay; - char **split_titles; + char** split_titles; int split_count; - long long *split_times; - long long *segment_times; - long long *best_splits; - long long *best_segments; + long long* split_times; + long long* segment_times; + long long* best_splits; + long long* best_segments; }; typedef struct ls_game ls_game; -struct ls_timer -{ +struct ls_timer { int started; int running; int loading; @@ -37,58 +35,58 @@ struct ls_timer long long sum_of_bests; long long world_record; int curr_split; - long long *split_times; - long long *split_deltas; - long long *segment_times; - long long *segment_deltas; - int *split_info; - long long *best_splits; - long long *best_segments; - const ls_game *game; - int *attempt_count; + long long* split_times; + long long* split_deltas; + long long* segment_times; + long long* segment_deltas; + int* split_info; + long long* best_splits; + long long* best_segments; + const ls_game* game; + int* attempt_count; }; typedef struct ls_timer ls_timer; long long ls_time_now(void); -long long ls_time_value(const char *string); +long long ls_time_value(const char* string); -void ls_time_string(char *string, long long time); +void ls_time_string(char* string, long long time); -void ls_time_millis_string(char *seconds, char *millis, long long time); +void ls_time_millis_string(char* seconds, char* millis, long long time); -void ls_split_string(char *string, long long time); +void ls_split_string(char* string, long long time); -void ls_delta_string(char *string, long long time); +void ls_delta_string(char* string, long long time); -int ls_game_create(ls_game **game_ptr, const char *path); +int ls_game_create(ls_game** game_ptr, const char* path); -void ls_game_update_splits(ls_game *game, const ls_timer *timer); +void ls_game_update_splits(ls_game* game, const ls_timer* timer); -void ls_game_update_bests(ls_game *game, const ls_timer *timer); +void ls_game_update_bests(ls_game* game, const ls_timer* timer); -int ls_game_save(const ls_game *game); +int ls_game_save(const ls_game* game); -void ls_game_release(ls_game *game); +void ls_game_release(ls_game* game); -int ls_timer_create(ls_timer **timer_ptr, ls_game *game); +int ls_timer_create(ls_timer** timer_ptr, ls_game* game); -void ls_timer_release(ls_timer *timer); +void ls_timer_release(ls_timer* timer); -int ls_timer_start(ls_timer *timer); +int ls_timer_start(ls_timer* timer); -void ls_timer_step(ls_timer *timer, long long now); +void ls_timer_step(ls_timer* timer, long long now); -int ls_timer_split(ls_timer *timer); +int ls_timer_split(ls_timer* timer); -int ls_timer_skip(ls_timer *timer); +int ls_timer_skip(ls_timer* timer); -int ls_timer_unsplit(ls_timer *timer); +int ls_timer_unsplit(ls_timer* timer); -void ls_timer_stop(ls_timer *timer); +void ls_timer_stop(ls_timer* timer); -int ls_timer_reset(ls_timer *timer); +int ls_timer_reset(ls_timer* timer); -int ls_timer_cancel(ls_timer *timer); +int ls_timer_cancel(ls_timer* timer); #endif /* __TIMER_H__ */