[PATCH] desktop-shell: Make clock format configurable

Martin Minarik minarik11 at student.fiit.stuba.sk
Tue Jun 12 17:45:11 PDT 2012


This allows the user to specify the format of the displayed time.
It is possible to set the period of redraw (in seconds).
---
 clients/desktop-shell.c |   49 +++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/clients/desktop-shell.c b/clients/desktop-shell.c
index 9a1b502..ff0afcb 100644
--- a/clients/desktop-shell.c
+++ b/clients/desktop-shell.c
@@ -97,6 +97,8 @@ struct panel_clock {
 	struct panel *panel;
 	struct task clock_task;
 	int clock_fd;
+	char *time_format;
+	unsigned int interval;
 };
 
 struct unlock_dialog {
@@ -114,7 +116,10 @@ static uint32_t key_panel_color = 0xaa000000;
 static uint32_t key_background_color = 0xff002244;
 static char *key_launcher_icon;
 static char *key_launcher_path;
+static char *key_clock_format = NULL;
+static unsigned int *key_clock_interval = 0;
 static void launcher_section_done(void *data);
+static void clock_section_done(void *data);
 static int key_locking = 1;
 
 static const struct config_key shell_config_keys[] = {
@@ -130,12 +135,20 @@ static const struct config_key launcher_config_keys[] = {
 	{ "path", CONFIG_KEY_STRING, &key_launcher_path },
 };
 
+static const struct config_key clock_config_keys[] = {
+	{ "format", CONFIG_KEY_STRING, &key_clock_format },
+	{ "interval", CONFIG_KEY_UNSIGNED_INTEGER, &key_clock_interval },
+};
+
 static const struct config_section config_sections[] = {
 	{ "shell",
 	  shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
 	{ "launcher",
 	  launcher_config_keys, ARRAY_LENGTH(launcher_config_keys),
-	  launcher_section_done }
+	  launcher_section_done },
+	{ "clock",
+	  clock_config_keys, ARRAY_LENGTH(clock_config_keys),
+	  clock_section_done }
 };
 
 static void
@@ -319,7 +332,7 @@ panel_clock_redraw_handler(struct widget *widget, void *data)
 
 	time(&rawtime);
 	timeinfo = localtime(&rawtime);
-	strftime(string, sizeof string, "%a %b %d, %I:%M %p", timeinfo);
+	strftime(string, sizeof string, clock->time_format, timeinfo);
 
 	widget_get_allocation(widget, &allocation);
 	if (allocation.width == 0)
@@ -349,9 +362,9 @@ clock_timer_reset(struct panel_clock *clock)
 {
 	struct itimerspec its;
 
-	its.it_interval.tv_sec = 60;
+	its.it_interval.tv_sec = clock->interval;
 	its.it_interval.tv_nsec = 0;
-	its.it_value.tv_sec = 60;
+	its.it_value.tv_sec = clock->interval;
 	its.it_value.tv_nsec = 0;
 	if (timerfd_settime(clock->clock_fd, 0, &its, NULL) < 0) {
 		fprintf(stderr, "could not set timerfd\n: %m");
@@ -361,6 +374,9 @@ clock_timer_reset(struct panel_clock *clock)
 	return 0;
 }
 
+static const char *clock_default_time_format = "DFLT%a %b %d, %p %I:%M:%S";
+static const unsigned int clock_default_interval = 1; 
+
 static void
 panel_add_clock(struct panel *panel)
 {
@@ -378,6 +394,8 @@ panel_add_clock(struct panel *panel)
 	clock->panel = panel;
 	panel->clock = clock;
 	clock->clock_fd = timerfd;
+	clock->time_format = clock_default_time_format;
+	clock->interval = clock_default_interval;
 
 	clock->clock_task.run = clock_func;
 	display_watch_fd(window_get_display(panel->window), clock->clock_fd,
@@ -875,6 +893,29 @@ launcher_section_done(void *data)
 }
 
 static void
+clock_section_done(void *data)
+{
+	struct desktop *desktop = data;
+	struct output *output;
+	struct panel_clock *clock;
+
+	char * new_format = malloc ((strlen(key_clock_format) + 2) * sizeof(char));
+	if (new_format == NULL)
+		return;
+	sprintf(new_format, "%s", key_clock_format);
+
+	wl_list_for_each(output, &desktop->outputs, link) {
+		clock = output->panel->clock;
+		clock->time_format = new_format;
+		if (clock->interval > 0)
+			clock->interval = key_clock_interval;
+	}
+
+	free(key_clock_format);
+	key_clock_format = NULL;
+}
+
+static void
 add_default_launcher(struct desktop *desktop)
 {
 	struct output *output;
-- 
1.7.5.4



More information about the wayland-devel mailing list