Skip to content

Commit

Permalink
add support for custom time formats
Browse files Browse the repository at this point in the history
  • Loading branch information
jovanlanik committed Jun 28, 2022
1 parent 46abee6 commit 6ef9f57
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
12 changes: 7 additions & 5 deletions gtklock.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ void gtklock_focus_window(struct GtkLock *gtklock, struct Window* win) {
}

void gtklock_update_clocks(struct GtkLock *gtklock) {
time_t now = time(&now);
struct tm *now_tm = localtime(&now);
if(now_tm == NULL) return;
snprintf(gtklock->time, 8, "%02d:%02d", now_tm->tm_hour, now_tm->tm_min);
GDateTime *time = g_date_time_new_now_local();
if(time == NULL) return;
if(gtklock->time) g_free(gtklock->time);
gtklock->time = g_date_time_format(time, gtklock->time_format ? gtklock->time_format : "%R");
g_date_time_unref(time);

for(guint idx = 0; idx < gtklock->windows->len; idx++) {
struct Window *ctx = g_array_index(gtklock->windows, struct Window *, idx);
window_update_clock(ctx);
Expand All @@ -76,7 +78,7 @@ struct GtkLock* create_gtklock(void) {
}

void gtklock_activate(struct GtkLock *gtklock) {
gtklock->draw_clock_source = g_timeout_add_seconds(5, gtklock_update_clocks_handler, gtklock);
gtklock->draw_clock_source = g_timeout_add_seconds(1, gtklock_update_clocks_handler, gtklock);
gtklock_update_clocks(gtklock);
if(gtklock->use_input_inhibit) input_inhibitor_get();
}
Expand Down
3 changes: 2 additions & 1 deletion include/gtklock.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ struct GtkLock {

gboolean use_layer_shell;
gboolean use_input_inhibit;
char time[8];
char *time;
char *time_format;
};

extern struct GtkLock *gtklock;
Expand Down
4 changes: 4 additions & 0 deletions source.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ static char *config_path = NULL;
static char *style_path = NULL;
static char *module_path = NULL;
static char *background_path = NULL;
static char *time_format = NULL;

static GOptionEntry main_entries[] = {
{ "daemonize", 'd', 0, G_OPTION_ARG_NONE, &should_daemonize, "Detach from the controlling terminal after locking", NULL },
Expand All @@ -37,6 +38,7 @@ static GOptionEntry config_entries[] = {
{ "style", 's', 0, G_OPTION_ARG_FILENAME, &style_path, "Load CSS style file", NULL },
{ "module", 'm', 0, G_OPTION_ARG_FILENAME, &module_path, "Load gtklock module", NULL },
{ "background", 'b', 0, G_OPTION_ARG_FILENAME, &background_path, "Load background", NULL },
{ "time-format", 't', 0, G_OPTION_ARG_STRING, &time_format, "Set time format", NULL },
};

static GOptionEntry debug_entries[] = {
Expand Down Expand Up @@ -239,6 +241,8 @@ int main(int argc, char **argv) {
GModule *module = NULL;
if(module_path != NULL) module = module_load(module_path);

gtklock->time_format = time_format;

g_signal_connect(gtklock->app, "activate", G_CALLBACK(activate), NULL);
int status = g_application_run(G_APPLICATION(gtklock->app), argc, argv);

Expand Down

0 comments on commit 6ef9f57

Please sign in to comment.