476 lines
14 KiB
C
476 lines
14 KiB
C
#include "base_context_cracking.h"
|
|
#include "base_core.h"
|
|
|
|
#include <errno.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
#include <pwd.h>
|
|
#include <sys/stat.h>
|
|
#include <stdio.h>
|
|
#include <time.h>
|
|
#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;
|
|
} 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;
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
activity activities[100];
|
|
memset(activities, 0, sizeof(activities));
|
|
U32 current_activity = 0;
|
|
|
|
Activity_Stat activities_stats[activity_type_COUNT];
|
|
memset(activities_stats, 0, sizeof(activities_stats));
|
|
|
|
U64 secs = now();
|
|
struct tm *t = localtime((time_t *)(&secs));
|
|
t->tm_hour = 0;
|
|
t->tm_min = 0;
|
|
t->tm_sec = 0;
|
|
|
|
time_t ts = mktime(t);
|
|
if(ts == (time_t)-1) {
|
|
fprintf(stderr, "mktime failed.\n");
|
|
return 1;
|
|
}
|
|
|
|
// TODO: Clean up.
|
|
// TODO: Clean up.
|
|
// TODO: Clean up.
|
|
|
|
// Check if 2025-02-10 available
|
|
// build array
|
|
// then, save the file periodically (every ~5 seconds)
|
|
// TODO: Make global constant or in config file
|
|
char date_buffer[16];
|
|
char dir_buffer[128];
|
|
char file_buffer[256];
|
|
|
|
strftime(date_buffer, sizeof(date_buffer), "%Y_%m_%d", t);
|
|
snprintf(dir_buffer, sizeof(dir_buffer), "%s/.timetracker",
|
|
get_home_dir());
|
|
snprintf(file_buffer, sizeof(file_buffer), "%s/%s", dir_buffer,
|
|
date_buffer);
|
|
|
|
if(!is_directory(dir_buffer)) {
|
|
mkdir(dir_buffer, 0700); // WARNING: Might fail, what then?
|
|
}
|
|
|
|
// TODO: Fix error handling.
|
|
// WARNING: Not bullet proof, yet.
|
|
FILE *f;
|
|
if(path_exists(file_buffer)) {
|
|
f = fopen(file_buffer, "r");
|
|
if(!f) {
|
|
fprintf(stderr, "fopen(%s) failed: %s\n", file_buffer,
|
|
strerror(errno));
|
|
return 1;
|
|
}
|
|
} else {
|
|
f = fopen(file_buffer, "w+");
|
|
}
|
|
|
|
char line[4096];
|
|
if(file_size(f) < 1) {
|
|
printf("EMPTY\n");
|
|
} else {
|
|
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 = (U64)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.
|
|
// NOTE: We can potentially make the default a non-state, so the users
|
|
// choose themselves.
|
|
activities[current_activity].type = other;
|
|
activities[current_activity].color = RED;
|
|
activities[current_activity].began = now();
|
|
activities[current_activity].ended = lower_bound_s;
|
|
|
|
// TODO: Move into game state.
|
|
int font_size = 30;
|
|
|
|
// DisableEventWaiting();
|
|
// EnableEventWaiting();
|
|
while(!WindowShouldClose()) {
|
|
U64 now_s = now();
|
|
|
|
// NOTE: WARNING: TODO: Temporary solution; to be cleaned up.
|
|
if(now_s >= upper_bound_s) {
|
|
U64 secs = now();
|
|
|
|
t = localtime((time_t *)(&secs));
|
|
t->tm_hour = 0;
|
|
t->tm_min = 0;
|
|
t->tm_sec = 0;
|
|
|
|
time_t ts = mktime(t);
|
|
if(ts == (time_t)-1) {
|
|
fprintf(stderr, "mktime failed.\n");
|
|
return 1;
|
|
}
|
|
|
|
lower_bound_s = (U64)ts;
|
|
upper_bound_s = lower_bound_s + 86400;
|
|
|
|
fclose(f);
|
|
|
|
strftime(date_buffer, sizeof(date_buffer), "%Y_%m_%d", t);
|
|
snprintf(file_buffer, sizeof(file_buffer), "%s/%s", dir_buffer,
|
|
date_buffer);
|
|
|
|
f = fopen(file_buffer, "w+");
|
|
if(!f) {
|
|
fprintf(stderr, "Failed to create file: %s\n", file_buffer);
|
|
return 1;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
// TODO: CLEAN UP.
|
|
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, 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 },
|
|
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;
|
|
}
|
|
|
|
// WARNING: This is error prone.
|
|
// Save the state every 5 seconds.
|
|
if((now_s - last_save) >= 5) {
|
|
f = freopen(file_buffer, "w", f); // WARNING: TODO: Might fail.
|
|
|
|
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)
|
|
{
|
|
perror("fwrite");
|
|
}
|
|
}
|
|
|
|
if(fflush(f) != 0)
|
|
perror("fflush");
|
|
|
|
last_save = now_s;
|
|
}
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
CloseWindow();
|
|
|
|
return 0;
|
|
}
|