diff --git a/src/auto-splitter.c b/src/auto-splitter.c index d2d308a..f2fcea1 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" @@ -28,7 +28,7 @@ atomic_bool toggle_loading = false; atomic_bool call_reset = false; bool prev_is_loading; -static const char* disabled_functions[] = { +static const char *disabled_functions[] = { "collectgarbage", "dofile", "getmetatable", @@ -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}; +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) { - 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) +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,74 +144,76 @@ 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 */ + + 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); + 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); 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); 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; @@ -220,8 +222,8 @@ bool call_va(lua_State* L, const char *func, const char *sig, ...) { 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); @@ -237,61 +239,58 @@ bool call_va(lua_State* L, const char *func, const char *sig, ...) { return true; } -void startup(lua_State* L) +void startup(lua_State *L) { lua_getglobal(L, "startup"); 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; } lua_pop(L, 1); // Remove 'mapsCacheCycles' from the stack } -void state(lua_State* L) +void state(lua_State *L) { - call_va(L,"state", ""); + call_va(L, "state", ""); } -void update(lua_State* L) +void update(lua_State *L) { - call_va(L,"update", ""); + call_va(L, "update", ""); } -void start(lua_State* L) +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 } -void split(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 } -void is_loading(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; } @@ -299,11 +298,11 @@ void is_loading(lua_State* L) lua_pop(L, 1); // Remove the return value from the stack } -void reset(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 @@ -311,7 +310,7 @@ void reset(lua_State* L) void run_auto_splitter() { - lua_State* L = luaL_newstate(); + lua_State *L = luaL_newstate(); luaL_openlibs(L); disable_functions(L, disabled_functions); lua_pushcfunction(L, find_process_id); @@ -325,10 +324,9 @@ 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); + const char *error_msg = lua_tostring(L, -1); lua_pop(L, 1); // Remove the error message from the stack fprintf(stderr, "Lua syntax error: %s\n", error_msg); lua_close(L); @@ -337,10 +335,9 @@ 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); + const char *error_msg = lua_tostring(L, -1); lua_pop(L, 1); // Remove the error message from the stack fprintf(stderr, "Lua runtime error: %s\n", error_msg); lua_close(L); @@ -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..b02d147 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,16 +60,14 @@ */ #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; @@ -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..85172d2 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..dc5cb37 100644 --- a/src/component/best-sum.c +++ b/src/component/best-sum.c @@ -1,7 +1,6 @@ #include "components.h" -typedef struct _LSBestSum -{ +typedef struct _LSBestSum { LSComponent base; GtkWidget *container; GtkWidget *sum_of_bests; @@ -16,8 +15,7 @@ LSComponent *ls_component_best_sum_new() GtkWidget *label; self = malloc(sizeof(LSBestSum)); - if (!self) - { + if (!self) { return NULL; } self->base.ops = &ls_best_sum_operations; @@ -53,12 +51,11 @@ static GtkWidget *best_sum_widget(LSComponent *self) } static void best_sum_show_game(LSComponent *self_, - ls_game *game, ls_timer *timer) + ls_game *game, ls_timer *timer) { 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); } @@ -71,22 +68,20 @@ static void best_sum_clear_game(LSComponent *self_) } static void best_sum_draw(LSComponent *self_, ls_game *game, - ls_timer *timer) + ls_timer *timer) { 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..a03d136 100644 --- a/src/component/clock.c +++ b/src/component/clock.c @@ -1,7 +1,6 @@ #include "components.h" -typedef struct _LSTimer -{ +typedef struct _LSTimer { LSComponent base; GtkWidget *time; GtkWidget *time_seconds; @@ -15,8 +14,7 @@ LSComponent *ls_component_timer_new() GtkWidget *spacer; self = malloc(sizeof(LSTimer)); - if (!self) - { + if (!self) { return NULL; } self->base.ops = &ls_timer_operations; @@ -48,7 +46,6 @@ LSComponent *ls_component_timer_new() gtk_container_add(GTK_CONTAINER(spacer), self->time_millis); gtk_widget_show(self->time_millis); - return (LSComponent *)self; } @@ -70,7 +67,6 @@ static void timer_clear_game(LSComponent *self_) 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) @@ -80,8 +76,7 @@ static void timer_draw(LSComponent *self_, ls_game *game, ls_timer *timer) 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..6e7cd28 100644 --- a/src/component/components.c +++ b/src/component/components.c @@ -8,16 +8,15 @@ 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) diff --git a/src/component/components.h b/src/component/components.h index 71587d5..215f10f 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,13 +12,11 @@ typedef struct _LSComponent LSComponent; typedef struct _LSComponentOps LSComponentOps; typedef struct _LSComponentAvailable LSComponentAvailable; -struct _LSComponent -{ +struct _LSComponent { LSComponentOps *ops; }; -struct _LSComponentOps -{ +struct _LSComponentOps { void (*delete)(LSComponent *self); GtkWidget *(*widget)(LSComponent *self); @@ -34,8 +32,7 @@ struct _LSComponentOps void (*cancel_run)(LSComponent *self, ls_timer *timer); }; -struct _LSComponentAvailable -{ +struct _LSComponentAvailable { char *name; LSComponent *(*new)(); }; diff --git a/src/component/pb.c b/src/component/pb.c index 16e7414..46f83ce 100644 --- a/src/component/pb.c +++ b/src/component/pb.c @@ -1,7 +1,6 @@ #include "components.h" -typedef struct _LSPb -{ +typedef struct _LSPb { LSComponent base; GtkWidget *container; GtkWidget *personal_best; @@ -16,8 +15,7 @@ LSComponent *ls_component_pb_new() GtkWidget *label; self = malloc(sizeof(LSPb)); - if (!self) - { + if (!self) { return NULL; } self->base.ops = &ls_pb_operations; @@ -53,20 +51,17 @@ static GtkWidget *pb_widget(LSComponent *self) } static void pb_show_game(LSComponent *self_, - ls_game *game, ls_timer *timer) + ls_game *game, ls_timer *timer) { 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_) @@ -76,7 +71,7 @@ static void pb_clear_game(LSComponent *self_) } static void pb_draw(LSComponent *self_, ls_game *game, - ls_timer *timer) + ls_timer *timer) { LSPb *self = (LSPb *)self_; char str[256]; @@ -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..f4395f0 100644 --- a/src/component/prev-segment.c +++ b/src/component/prev-segment.c @@ -1,7 +1,6 @@ #include "components.h" -typedef struct _LSPrevSegment -{ +typedef struct _LSPrevSegment { LSComponent base; GtkWidget *container; GtkWidget *previous_segment_label; @@ -10,15 +9,14 @@ typedef struct _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() { 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); @@ -56,7 +54,7 @@ static GtkWidget *prev_segment_widget(LSComponent *self) } static void prev_segment_show_game(LSComponent *self_, - ls_game *game, ls_timer *timer) + ls_game *game, ls_timer *timer) { LSPrevSegment *self = (LSPrevSegment *)self_; remove_class(self->previous_segment, "behind"); @@ -68,19 +66,18 @@ static void prev_segment_clear_game(LSComponent *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) + ls_timer *timer) { 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..596c732 100644 --- a/src/component/splits.c +++ b/src/component/splits.c @@ -1,7 +1,6 @@ #include "components.h" -typedef struct _LSSplits -{ +typedef struct _LSSplits { LSComponent base; int split_count; GtkWidget *container; @@ -22,8 +21,7 @@ LSComponent *ls_component_splits_new() 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); @@ -79,31 +77,26 @@ 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); } } @@ -111,7 +104,7 @@ static void splits_trailer(LSComponent *self_) } static void splits_show_game(LSComponent *self_, ls_game *game, - ls_timer *timer) + ls_timer *timer) { LSSplits *self = (LSSplits *)self_; char str[256]; @@ -122,57 +115,49 @@ static void splits_show_game(LSComponent *self_, ls_game *game, 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])) - { + && 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); } @@ -191,11 +176,10 @@ static void splits_clear_game(LSComponent *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); @@ -211,38 +195,30 @@ static void splits_draw(LSComponent *self_, ls_game *game, ls_timer *timer) 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], @@ -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,26 +312,20 @@ 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); } } @@ -394,8 +345,7 @@ 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..91691d7 100644 --- a/src/component/title.c +++ b/src/component/title.c @@ -1,7 +1,6 @@ #include "components.h" -typedef struct _LSTitle -{ +typedef struct _LSTitle { LSComponent base; GtkWidget *header; GtkWidget *title; @@ -14,8 +13,7 @@ LSComponent *ls_component_title_new() LSTitle *self; self = malloc(sizeof(LSTitle)); - if (!self) - { + if (!self) { return NULL; } self->base.ops = &ls_title_operations; @@ -68,7 +66,7 @@ static void title_resize(LSComponent *self_, int win_width, int win_height) } static void title_show_game(LSComponent *self_, ls_game *game, - ls_timer *timer) + ls_timer *timer) { char str[64]; LSTitle *self = (LSTitle *)self_; @@ -85,8 +83,7 @@ static void title_draw(LSComponent *self_, ls_game *game, ls_timer *timer) 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..988b797 100644 --- a/src/component/wr.c +++ b/src/component/wr.c @@ -1,7 +1,6 @@ #include "components.h" -typedef struct _LSWr -{ +typedef struct _LSWr { LSComponent base; GtkWidget *container; GtkWidget *world_record_label; @@ -16,8 +15,7 @@ LSComponent *ls_component_wr_new() LSWr *self; self = malloc(sizeof(LSWr)); - if (!self) - { + if (!self) { return NULL; } self->base.ops = &ls_wr_operations; @@ -50,13 +48,12 @@ static GtkWidget *wr_widget(LSComponent *self) } static void wr_show_game(LSComponent *self_, - ls_game *game, ls_timer *timer) + ls_game *game, ls_timer *timer) { 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); @@ -73,30 +70,24 @@ static void wr_clear_game(LSComponent *self_) } static void wr_draw(LSComponent *self_, ls_game *game, - ls_timer *timer) + ls_timer *timer) { 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..fb8443b 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,8 +40,7 @@ typedef struct GdkModifierType mods; } Keybind; -struct _LSAppWindow -{ +struct _LSAppWindow { GtkApplicationWindow parent; char data_path[PATH_MAX]; int decorated; @@ -64,8 +61,7 @@ struct _LSAppWindow Keybind keybind_toggle_decorations; }; -struct _LSAppWindowClass -{ +struct _LSAppWindowClass { GtkApplicationWindowClass parent_class; }; @@ -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) { - 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); @@ -118,21 +111,18 @@ static void ls_app_window_clear_game(LSAppWindow *win) gtk_widget_hide(win->box); - for (l = win->components; l != NULL; l = l->next) - { + for (l = win->components; l != NULL; l = l->next) { LSComponent *component = l->data; - if (component->ops->clear_game) - { + 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; } @@ -149,47 +139,36 @@ static gboolean ls_app_window_step(gpointer data) LSAppWindow *win = data; long long now = ls_time_now(); static int set_cursor; - if (win->hide_cursor && !set_cursor) - { + if (win->hide_cursor && !set_cursor) { GdkWindow *gdk_window = gtk_widget_get_window(GTK_WIDGET(win)); - if (gdk_window) - { + 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); } @@ -200,14 +179,13 @@ static gboolean ls_app_window_step(gpointer data) } static int ls_app_window_find_theme(LSAppWindow *win, - const char *theme_name, - const char *theme_variant, - char *str) + 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,8 +202,7 @@ 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; @@ -239,19 +215,17 @@ static void ls_app_window_show_game(LSAppWindow *win) 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) - { + for (l = win->components; l != NULL; l = l->next) { LSComponent *component = l->data; - if (component->ops->show_game) - { + if (component->ops->show_game) { component->ops->show_game(component, win->game, win->timer); } } @@ -276,52 +248,43 @@ static void ls_app_window_show_game(LSAppWindow *win) } static void resize_window(LSAppWindow *win, - int window_width, - int window_height) + int window_width, + int window_height) { GList *l; - for (l = win->components; l != NULL; l = l->next) - { + for (l = win->components; l != NULL; l = l->next) { LSComponent *component = l->data; - if (component->ops->resize) - { + 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) + 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) { - if (win->timer) - { + if (win->timer) { GList *l; - if (!win->timer->running) - { - if (ls_timer_start(win->timer)) - { + 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) - { + for (l = win->components; l != NULL; l = l->next) { LSComponent *component = l->data; - if (component->ops->start_split) - { + if (component->ops->start_split) { component->ops->start_split(component, win->timer); } } @@ -330,20 +293,15 @@ static void timer_start_split(LSAppWindow *win) static void timer_start(LSAppWindow *win) { - if (win->timer) - { + if (win->timer) { GList *l; - if (!win->timer->running) - { - if (ls_timer_start(win->timer)) - { + if (!win->timer->running) { + if (ls_timer_start(win->timer)) { save_game(win->game); } - for (l = win->components; l != NULL; l = l->next) - { + for (l = win->components; l != NULL; l = l->next) { LSComponent *component = l->data; - if (component->ops->start_split) - { + if (component->ops->start_split) { component->ops->start_split(component, win->timer); } } @@ -353,17 +311,13 @@ static void timer_start(LSAppWindow *win) static void timer_split(LSAppWindow *win) { - if (win->timer) - { + if (win->timer) { GList *l; - if (win->timer->running) - { + if (win->timer->running) { ls_timer_split(win->timer); - for (l = win->components; l != NULL; l = l->next) - { + for (l = win->components; l != NULL; l = l->next) { LSComponent *component = l->data; - if (component->ops->start_split) - { + if (component->ops->start_split) { component->ops->start_split(component, win->timer); } } @@ -373,18 +327,14 @@ static void timer_split(LSAppWindow *win) static void timer_stop(LSAppWindow *win) { - if (win->timer) - { + if (win->timer) { GList *l; - if (win->timer->running) - { + if (win->timer->running) { ls_timer_stop(win->timer); } - for (l = win->components; l != NULL; l = l->next) - { + for (l = win->components; l != NULL; l = l->next) { LSComponent *component = l->data; - if (component->ops->stop_reset) - { + if (component->ops->stop_reset) { component->ops->stop_reset(component, win->timer); } } @@ -393,27 +343,20 @@ static void timer_stop(LSAppWindow *win) static void timer_stop_reset(LSAppWindow *win) { - if (win->timer) - { + if (win->timer) { GList *l; - if (win->timer->running) - { + 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) - { + for (l = win->components; l != NULL; l = l->next) { LSComponent *component = l->data; - if (component->ops->stop_reset) - { + if (component->ops->stop_reset) { component->ops->stop_reset(component, win->timer); } } @@ -422,32 +365,25 @@ static void timer_stop_reset(LSAppWindow *win) static void timer_reset(LSAppWindow *win) { - if (win->timer) - { + if (win->timer) { GList *l; - if (win->timer->running) - { + if (win->timer->running) { ls_timer_stop(win->timer); - for (l = win->components; l != NULL; l = l->next) - { + for (l = win->components; l != NULL; l = l->next) { LSComponent *component = l->data; - if (component->ops->stop_reset) - { + 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) - { + for (l = win->components; l != NULL; l = l->next) { LSComponent *component = l->data; - if (component->ops->stop_reset) - { + if (component->ops->stop_reset) { component->ops->stop_reset(component, win->timer); } } @@ -456,38 +392,30 @@ static void timer_reset(LSAppWindow *win) static void timer_cancel_run(LSAppWindow *win) { - if (win->timer) - { + if (win->timer) { GList *l; - if (ls_timer_cancel(win->timer)) - { + 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) - { + for (l = win->components; l != NULL; l = l->next) { LSComponent *component = l->data; - if (component->ops->cancel_run) - { + if (component->ops->cancel_run) { component->ops->cancel_run(component, win->timer); } } } } - static void timer_skip(LSAppWindow *win) { - if (win->timer) - { + if (win->timer) { GList *l; ls_timer_skip(win->timer); - for (l = win->components; l != NULL; l = l->next) - { + for (l = win->components; l != NULL; l = l->next) { LSComponent *component = l->data; - if (component->ops->skip) - { + if (component->ops->skip) { component->ops->skip(component, win->timer); } } @@ -496,22 +424,18 @@ static void timer_skip(LSAppWindow *win) static void timer_unsplit(LSAppWindow *win) { - if (win->timer) - { + if (win->timer) { GList *l; ls_timer_unsplit(win->timer); - for (l = win->components; l != NULL; l = l->next) - { + for (l = win->components; l != NULL; l = l->next) { LSComponent *component = l->data; - if (component->ops->unsplit) - { + if (component->ops->unsplit) { component->ops->unsplit(component, win->timer); } } } } - static void toggle_decorations(LSAppWindow *win) { gtk_window_set_decorated(GTK_WINDOW(win), !win->decorated); @@ -549,32 +473,21 @@ static void keybind_toggle_decorations(const char *str, LSAppWindow *win) } static gboolean ls_app_window_keypress(GtkWidget *widget, - GdkEvent *event, - gpointer data) + 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; @@ -583,24 +496,19 @@ static gboolean ls_app_window_keypress(GtkWidget *widget, static gboolean ls_app_window_draw(gpointer data) { LSAppWindow *win = data; - if (win->timer) - { + if (win->timer) { GList *l; - for (l = win->components; l != NULL; l = l->next) - { + for (l = win->components; l != NULL; l = l->next) { LSComponent *component = l->data; - if (component->ops->draw) - { + 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; } @@ -626,17 +534,17 @@ static void ls_app_window_init(LSAppWindow *win) 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); @@ -655,8 +563,7 @@ static void ls_app_window_init(LSAppWindow *win) // 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++) - { + for (i = 0; ls_components[i].name != NULL; i++) { LSComponent *component = ls_components[i].new(); - if (component) - { + if (component) { GtkWidget *widget = component->ops->widget(component); - if (widget) - { + 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,7 +647,7 @@ 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) @@ -763,68 +664,55 @@ static LSAppWindow *ls_app_window_new(LSApp *app) 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) + GVariant *parameter, + gpointer app) { char splits_path[PATH_MAX]; GList *windows; LSAppWindow *win; GtkWidget *dialog; - struct stat st = {0}; + 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,16 +720,14 @@ 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) - { + if (res == GTK_RESPONSE_ACCEPT) { char *filename; GtkFileChooser *chooser = GTK_FILE_CHOOSER(dialog); filename = gtk_file_chooser_get_filename(chooser); @@ -853,30 +739,26 @@ static void open_activated(GSimpleAction *action, } static void open_auto_splitter(GSimpleAction *action, - GVariant *parameter, - gpointer app) + GVariant *parameter, + gpointer app) { char auto_splitters_path[PATH_MAX]; GList *windows; LSAppWindow *win; GtkWidget *dialog; - struct stat st = {0}; + 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,16 +766,14 @@ 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) - { + if (res == GTK_RESPONSE_ACCEPT) { GtkFileChooser *chooser = GTK_FILE_CHOOSER(dialog); char *filename = gtk_file_chooser_get_filename(chooser); strcpy(auto_splitter_file, filename); @@ -904,27 +784,22 @@ static void open_auto_splitter(GSimpleAction *action, } static void save_activated(GSimpleAction *action, - GVariant *parameter, - gpointer app) + GVariant *parameter, + gpointer app) { GList *windows; LSAppWindow *win; - 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)); } - 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; @@ -935,28 +810,23 @@ static void save_activated(GSimpleAction *action, } static void reload_activated(GSimpleAction *action, - GVariant *parameter, - gpointer app) + GVariant *parameter, + gpointer app) { GList *windows; LSAppWindow *win; char *path; - 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)); } - if (win->game) - { + if (win->game) { path = strdup(win->game->path); ls_app_window_open(win, path); free(path); @@ -964,36 +834,29 @@ static void reload_activated(GSimpleAction *action, } static void close_activated(GSimpleAction *action, - GVariant *parameter, - gpointer app) + GVariant *parameter, + gpointer app) { GList *windows; LSAppWindow *win; - 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)); } - 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; } @@ -1001,8 +864,8 @@ static void close_activated(GSimpleAction *action, } static void quit_activated(GSimpleAction *action, - GVariant *parameter, - gpointer app) + GVariant *parameter, + gpointer app) { exit(0); } @@ -1010,13 +873,10 @@ static void quit_activated(GSimpleAction *action, 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()); } @@ -1025,8 +885,7 @@ static void toggle_auto_splitter(GtkCheckMenuItem *menu_item, gpointer user_data // Create the context menu static gboolean button_right_click(GtkWidget *widget, GdkEventButton *event, gpointer app) { - if (event->button == GDK_BUTTON_SECONDARY) - { + 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"); @@ -1067,48 +926,34 @@ static void ls_app_activate(GApplication *app) 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); } } @@ -1119,25 +964,21 @@ 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; 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)); @@ -1147,9 +988,9 @@ 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) @@ -1161,11 +1002,10 @@ static void ls_app_class_init(LSAppClass *class) 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); } diff --git a/src/memory.c b/src/memory.c index 9806b64..673e32b 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) @@ -54,11 +51,10 @@ READ_MEMORY_FUNCTION(float) READ_MEMORY_FUNCTION(double) READ_MEMORY_FUNCTION(bool) -char* read_memory_string(uint64_t mem_address, int buffer_size) +char *read_memory_string(uint64_t mem_address, int buffer_size) { - char* buffer = (char*)malloc(buffer_size); - if (buffer == NULL) - { + char *buffer = (char *)malloc(buffer_size); + if (buffer == NULL) { // Handle memory allocation failure return NULL; } @@ -69,15 +65,12 @@ char* read_memory_string(uint64_t mem_address, int buffer_size) mem_local.iov_base = buffer; mem_local.iov_len = buffer_size; mem_remote.iov_len = buffer_size; - mem_remote.iov_base = (void*)(uintptr_t)mem_address; + 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,35 +83,43 @@ 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; } -int read_address(lua_State* L) +int read_address(lua_State *L) { memory_error = false; uint64_t address; - const char* value_type = lua_tostring(L, 1); + 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,97 +128,68 @@ 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"); exit(1); } - char* value = read_memory_string(address, buffer_size); + char *value = read_memory_string(address, buffer_size); 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..9b3be18 100644 --- a/src/memory.h +++ b/src/memory.h @@ -2,11 +2,11 @@ #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); -int read_address(lua_State* L); +int read_address(lua_State *L); #endif /* __MEMORY_H__ */ diff --git a/src/process.c b/src/process.c index 29c94c2..a0edc91 100644 --- a/src/process.c +++ b/src/process.c @@ -1,33 +1,31 @@ #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 ProcessMap p_maps_cache[MAPS_CACHE_MAX_SIZE]; uint32_t p_maps_cache_size = 0; -void execute_command(const char* command, char* output) +void execute_command(const char *command, char *output) { char buffer[4096]; - FILE* pipe = popen(command, "r"); - if (!pipe) - { + FILE *pipe = popen(command, "r"); + 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); } @@ -38,12 +36,12 @@ void execute_command(const char* command, char* output) Gets the base address of a module if `module` equals to a nullptr, the main process is used, else it will search for the base addr of the specified module */ -uintptr_t find_base_address(const char* module) +uintptr_t find_base_address(const char *module) { - const char* module_to_grep = module == 0 ? process.name : module; + const char *module_to_grep = module == 0 ? process.name : module; for (int32_t i = 0; i < p_maps_cache_size; i++) { - const char* name = p_maps_cache[i].name; + const char *name = p_maps_cache[i].name; if (strstr(name, module_to_grep) == NULL) { return p_maps_cache[i].start; } @@ -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; @@ -77,20 +75,18 @@ uintptr_t find_base_address(const char* module) return 0; } -void stock_process_id(const char* pid_command) +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; @@ -106,7 +102,7 @@ void stock_process_id(const char* pid_command) process.dll_address = process.base_address; } -int find_process_id(lua_State* L) +int find_process_id(lua_State *L) { process.name = lua_tostring(L, 1); char command[256]; @@ -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..ac5efe2 100644 --- a/src/process.h +++ b/src/process.h @@ -7,8 +7,7 @@ #include -struct game_process -{ +struct game_process { const char *name; int pid; uintptr_t base_address; @@ -22,10 +21,10 @@ typedef struct ProcessMap { } ProcessMap; extern uint32_t p_maps_cache_size; -uintptr_t find_base_address(const char* module); +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); +int find_process_id(lua_State *L); +int getPid(lua_State *L); +bool parseMapsLine(char *line, ProcessMap *map); #endif /* __PROCESS_H__ */ diff --git a/src/settings.c b/src/settings.c index 97ff6ca..04cbf02 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()); - char* XDG_CONFIG_HOME = getenv("XDG_CONFIG_HOME"); - char* base_dir = strcat(pw->pw_dir, "/.config/libresplit"); +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); @@ -32,27 +32,22 @@ void ls_update_setting(const char *setting, json_t *value) // Load existing settings json_t *root = NULL; FILE *file = fopen(settings_path, "r"); - if (file) - { + 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) - { + if (!ls_obj) { ls_obj = json_object(); json_object_set(root, "libresplit", ls_obj); } @@ -60,13 +55,10 @@ void ls_update_setting(const char *setting, json_t *value) // Save updated settings back to the file FILE *output_file = fopen(settings_path, "w"); - if (output_file) - { + 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); } @@ -80,20 +72,16 @@ json_t *load_settings() strcat(settings_path, "/settings.json"); FILE *file = fopen(settings_path, "r"); - if (file) - { + if (file) { json_error_t 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; } @@ -102,22 +90,19 @@ json_t *load_settings() json_t *get_setting_value(const char *section, const char *setting) { json_t *root = load_settings(); - if (!root) - { + if (!root) { return NULL; } json_t *section_obj = json_object_get(root, section); - if (!section_obj) - { + 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) - { + 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..0c95101 100644 --- a/src/settings.h +++ b/src/settings.h @@ -3,7 +3,7 @@ #include -void get_libresplit_folder_path(char* out_path); +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); diff --git a/src/timer.c b/src/timer.c index 7b2d023..167e5ff 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) { @@ -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,77 +41,58 @@ 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) + 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) - { + 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) + long long time) { ls_time_string_format(string, NULL, time, 1, 0, 0); } @@ -142,47 +120,36 @@ void ls_delta_string(char *string, long long time) 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); } } @@ -197,228 +164,185 @@ int ls_game_create(ls_game **game_ptr, const char *path) 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) - { + 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) + 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]; } } @@ -426,10 +350,9 @@ void ls_game_update_splits(ls_game *game, } void ls_game_update_bests(ls_game *game, - const ls_timer *timer) + 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); @@ -444,30 +367,25 @@ int ls_game_save(const ls_game *game) 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) - { + 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,26 +395,21 @@ 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); @@ -505,32 +418,25 @@ int ls_game_save(const ls_game *game) 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); } } @@ -553,18 +459,12 @@ 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; } @@ -577,8 +477,7 @@ int ls_timer_create(ls_timer **timer_ptr, ls_game *game) ls_timer *timer; // allocate timer timer = calloc(1, sizeof(ls_timer)); - if (!timer) - { + if (!timer) { error = 1; goto timer_create_done; } @@ -586,62 +485,52 @@ 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; @@ -650,82 +539,59 @@ int ls_timer_create(ls_timer **timer_ptr, ls_game *game) 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) { - 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; } @@ -736,52 +602,39 @@ int ls_timer_start(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); } @@ -793,10 +646,8 @@ int ls_timer_split(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; @@ -810,20 +661,17 @@ int ls_timer_skip(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; @@ -838,10 +686,8 @@ void ls_timer_stop(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); @@ -852,12 +698,9 @@ int ls_timer_reset(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..98d671d 100644 --- a/src/timer.h +++ b/src/timer.h @@ -1,13 +1,12 @@ #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) +#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 -{ +struct ls_game { char *path; char *title; char *theme; @@ -26,8 +25,7 @@ struct ls_game }; typedef struct ls_game ls_game; -struct ls_timer -{ +struct ls_timer { int started; int running; int loading; @@ -45,7 +43,7 @@ struct ls_timer long long *best_splits; long long *best_segments; const ls_game *game; - int *attempt_count; + int *attempt_count; }; typedef struct ls_timer ls_timer;