#include "base_context_cracking.h" #include "base_core.h" #include #include #include #include #include #include #include #include "math.h" #include "sourcecodepro.h" #include "third_party/raylib/src/raylib.h" #ifndef true #define true 1 #endif #ifndef false #define false 0 #endif typedef struct { Vector2 mouse_pos; Font font; S32 font_size; } global_state; global_state state; B32 path_exists(const char *path) { struct stat st; return stat(path, &st) == 0; } B32 is_directory(const char *path) { struct stat st; return (stat(path, &st) == 0) && S_ISDIR(st.st_mode); } const char *get_home_dir() { const char *home = getenv("HOME"); if(home && home[0] != '\0') return home; struct passwd *pw = getpwuid(getuid()); if(pw && pw->pw_dir && pw->pw_dir[0] != '\0') return pw->pw_dir; return NULL; } U64 file_size(FILE *f) { fseek(f, 0, SEEK_END); U64 size = ftell(f); fseek(f, 0, SEEK_SET); // TODO: Save the current position, instead? return size; } U64 now() { return (U64)time(NULL); } B32 activity_button(Rectangle rect, char *title, char *subtitle, F32 font_size, Color font_color, Color background_color) { B32 button_pressed = false; // WARNING: Substracting from font size like that is unsafe. F32 subtitle_font_size = font_size - 8.0f; if(subtitle_font_size <= 0.0f) subtitle_font_size = font_size; Vector2 title_font_d = MeasureTextEx(state.font, title, (float)font_size, 2); Vector2 subtitle_font_d = MeasureTextEx(state.font, subtitle, (float)subtitle_font_size, 2); if(CheckCollisionPointRec(state.mouse_pos, rect)) { if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) button_pressed = true; } DrawRectangleRounded(rect, 0.2f, 100, background_color); DrawTextEx(state.font, title, (Vector2){ (rect.x + rect.width / 2.0f - title_font_d.x / 2.0f), (rect.y + rect.height / 2.0f - title_font_d.y / 2.0f) }, font_size, 2, font_color); DrawTextEx(state.font, subtitle, (Vector2){ (rect.x + rect.width / 2.0f - subtitle_font_d.x / 2.0f), (rect.y + rect.height / 2.0f - subtitle_font_d.y / 2.0f) + 20.0f }, subtitle_font_size, 2, font_color); return button_pressed; } typedef enum activity_type { other, studying, programming, projects, gaming, exercise, reading, activity_type_COUNT } activity_type; // TODO: Rename to something shorter, maybe ommit the "repreresentation." char *activity_type_string_representation[activity_type_COUNT] = { "Other", "Studying", "Programming", "Projects", "Gaming", "Exercise", "Reading" }; // TODO: Rename to something shorter, maybe ommit the "repreresentation." Color activity_type_color_representation[activity_type_COUNT] = { RED, GREEN, BLUE, ORANGE, MAROON, PURPLE, BROWN }; typedef struct activity { activity_type type; Color color; U64 began; U64 ended; } activity; typedef struct Activity_Stat { U64 total_seconds; char seconds_str[24]; } Activity_Stat; typedef struct Hours { U64 hours; U64 minutes; U64 seconds; } Hours; Hours break_time(U64 seconds) { Hours hours; hours.hours = seconds / 60 / 60; hours.minutes = seconds / 60 - (60 * hours.hours); hours.seconds = seconds - ((hours.hours * 60 * 60) + (hours.minutes * 60)); return hours; } void switch_activity(activity *activities, U32 *current_activity, activity_type type, U64 time) { if((activities[*current_activity].type != type) && (0 <= type && type < activity_type_COUNT)) { (*current_activity)++; activities[*current_activity].type = type; activities[*current_activity].color = activity_type_color_representation[type]; activities[*current_activity].began = time; activities[*current_activity].ended = time; } } struct tm *today() { U64 secs = now(); struct tm *t = localtime((time_t *)(&secs)); t->tm_hour = 0; t->tm_min = 0; t->tm_sec = 0; return t; } U64 start_of_day_seconds(struct tm *t) { // NOTE: Technically, this can fail only if the year is >2038 on 32-bit // platforms as size_t--a 32-bit--value would overflow. ON 64-bit, // this is a non-issue. time_t ts = mktime(t); return ts; } char *get_dir() { local_persist char buffer[128]; snprintf(buffer, sizeof(buffer), "%s/.timetracker", get_home_dir()); return buffer; } char *get_date_str(struct tm *t) { local_persist char buffer[16]; strftime(buffer, sizeof(buffer), "%Y_%m_%d", t); return buffer; } char *get_file(char *dir, struct tm *t) { local_persist char buffer[256]; snprintf(buffer, sizeof(buffer), "%s/%s", dir, get_date_str(t)); return buffer; } int main(int argc, char *argv[]) { activity activities[256]; // TODO: This should be a dynamic array. memset(activities, 0, sizeof(activities)); U32 current_activity = 0; Activity_Stat activities_stats[activity_type_COUNT]; memset(activities_stats, 0, sizeof(activities_stats)); struct tm *t = today(); U64 ts = start_of_day_seconds(t); char *dir_path = get_dir(); char *file_path = get_file(dir_path, t); if(!is_directory(dir_path)) { if(mkdir(dir_path, 0700) == -1) { fprintf(stderr, "mkdir(%s) failed: %s\n", dir_path, strerror(errno)); return 1; } } FILE *f; f = fopen(file_path, "a+"); if(!f) { fprintf(stderr, "fopen(%s) failed: %s\n", file_path, strerror(errno)); return 1; } char line[4096]; if(file_size(f) > 0) { while(fgets(line, sizeof line, f)) { size_t n = strcspn(line, "\r\n"); line[n] = '\0'; char activity_name[64]; U64 lower; U64 upper; int l = sscanf(line, "%s %llu %llu", activity_name, &lower, &upper); if(l != 3) continue; if(lower > upper) continue; activity_type type = other; if(strcmp(activity_name, activity_type_string_representation[other]) == 0) type = other; else if(strcmp(activity_name, activity_type_string_representation[studying]) == 0) type = studying; else if(strcmp(activity_name, activity_type_string_representation[projects]) == 0) type = projects; else if(strcmp(activity_name, activity_type_string_representation[gaming]) == 0) type = gaming; else if(strcmp(activity_name, activity_type_string_representation[exercise]) == 0) type = exercise; else continue; activities[current_activity].type = type; activities[current_activity].color = activity_type_color_representation[type]; activities[current_activity].began = lower; activities[current_activity].ended = upper; current_activity++; } } if(ferror(f)) { fprintf(stderr, "read error: %s\n", strerror(errno)); } U64 last_save = 0; U64 lower_bound_s = ts; U64 upper_bound_s = lower_bound_s + 86400; // U64 upper_bound_s = lower_bound_s + 3600; S32 window_w = 1400; S32 window_h = 500; InitWindow(window_w, window_h, "Time Tracker"); SetWindowState(FLAG_MSAA_4X_HINT|FLAG_WINDOW_RESIZABLE); SetTargetFPS(30); state.font = LoadFontFromMemory(".ttf", sourcecodepro_ttf, sourcecodepro_ttf_len, 96, NULL, 0); if(!IsFontValid(state.font)) { fprintf(stderr, "Unable to load font\n"); return 1; } GenTextureMipmaps(&state.font.texture); SetTextureFilter(state.font.texture, TEXTURE_FILTER_BILINEAR); // Initialize to default activity at the start. activities[current_activity].type = other; activities[current_activity].color = RED; activities[current_activity].began = now(); activities[current_activity].ended = lower_bound_s; state.font_size = 30; // DisableEventWaiting(); // EnableEventWaiting(); while(!WindowShouldClose()) { U64 now_s = now(); if(now_s >= upper_bound_s) { t = today(); ts = start_of_day_seconds(t); lower_bound_s = ts; upper_bound_s = lower_bound_s + 86400; fclose(f); file_path = get_file(dir_path, t); f = fopen(file_path, "r"); if(!f) { fprintf(stderr, "Failed to create file: %s\n", file_path); return 1; // TODO: REMOVE. } activity_type type = activities[current_activity].type; memset(activities, 0, sizeof(activities)); memset(activities_stats, 0, sizeof(activities_stats)); current_activity = 0; activities[current_activity].type = type; activities[current_activity].color = activity_type_color_representation[type]; activities[current_activity].began = now(); } window_w = GetScreenWidth(); window_h = GetScreenHeight(); state.mouse_pos = GetMousePosition(); if(IsKeyPressed(KEY_Q)) break; for(U32 i = 0; i < activity_type_COUNT; i++) { if(IsKeyPressed(KEY_ONE + i)) { switch_activity(activities, ¤t_activity, i, now_s); } } activities[current_activity].ended = now_s; BeginDrawing(); ClearBackground(BLACK); F32 padding_x = 10.0f; // F32 padding_y = 10.0f; DrawRectangle(0, 0, window_w, 40, GRAY); for(U32 i = 0; i <= current_activity; i++) { F32 start_x = floor((F32)(activities[i].began - lower_bound_s) / (F32)(upper_bound_s - lower_bound_s) * (F32)window_w); F32 end_x = floor(((F32)(activities[i].ended - lower_bound_s) / (F32)(upper_bound_s - lower_bound_s)) * window_w); DrawRectangle(start_x, 0, end_x - start_x, 40, activities[i].color); } F32 width = (window_w-padding_x*(float)(activity_type_COUNT+1)) / (float)activity_type_COUNT; for(U32 i = 0; i <= current_activity; i++) { activities_stats[activities[i].type].total_seconds += activities[i].ended - activities[i].began; } for(U32 i = 0; i < activity_type_COUNT; i ++) { Hours time = break_time(activities_stats[i].total_seconds); snprintf(activities_stats[i].seconds_str, sizeof(activities_stats[i].seconds_str), "%02llu:%02llu:%02llu", time.hours, time.minutes, time.seconds); Rectangle rect = { (padding_x*(i+1))+(width*i), 60, width, (window_h-70) }; if(activity_button(rect, activity_type_string_representation[i], activities_stats[i].seconds_str, state.font_size, (activities[current_activity].type == i ? WHITE : BLACK), activity_type_color_representation[i])) { if(activities[current_activity].type != i) { switch_activity(activities, ¤t_activity, i, now_s); } } if(activities[current_activity].type == i) DrawRectangleRoundedLinesEx(rect, 0.2f, 100, 5.0f, WHITE); char index_buf[8]; snprintf(index_buf, sizeof(index_buf), "%d", i + 1); DrawTextEx(state.font, index_buf, (Vector2){ rect.x + 10.0f, rect.y + 10.0f }, state.font_size - 5.0f, 2, BLACK); } EndDrawing(); // Reset statistics as we will accumulate seconds again. for(U32 i = 0; i <= current_activity; i++) { activities_stats[activities[i].type].total_seconds = 0; } // Save the state every 5 seconds. if((now_s - last_save) >= 5) { f = freopen(file_path, "w+", f); // NOTE: We want to clear file contents before the next write. if(f) { char write_buffer[128]; for(U32 i = 0; i <= current_activity; i++) { snprintf(write_buffer, sizeof(write_buffer), "%s %llu %llu\n", activity_type_string_representation[activities[i].type], activities[i].began, activities[i].ended); size_t write_buffer_len = strlen(write_buffer); if(fwrite(write_buffer, 1, write_buffer_len, f) != write_buffer_len) { // NOTE: We do not care, probably for now, that it might have failed to write some data. // The program will either overwrite everything again or the next launch will ignore the corrupted line. } } if(fflush(f) != 0) perror("fflush"); } last_save = now_s; } } fclose(f); CloseWindow(); return 0; }