From b44037442a818b95084a13e04cb813b148faea89 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Sun, 21 Dec 2025 15:57:19 +0900 Subject: [PATCH] add time widget --- mpanel/CMakeLists.txt | 1 + mpanel/module.c | 2 - mpanel/mpanelrc.in | 6 +++ mpanel/time/CMakeLists.txt | 6 +++ mpanel/time/module.c | 83 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 mpanel/time/CMakeLists.txt create mode 100644 mpanel/time/module.c diff --git a/mpanel/CMakeLists.txt b/mpanel/CMakeLists.txt index 039ab88..5e50e8a 100644 --- a/mpanel/CMakeLists.txt +++ b/mpanel/CMakeLists.txt @@ -14,6 +14,7 @@ endif() add_subdirectory(separator) add_subdirectory(startbutton) +add_subdirectory(time) include(GNUInstallDirs) configure_file(mpanelrc.in mpanelrc) diff --git a/mpanel/module.c b/mpanel/module.c index b33811b..2575556 100644 --- a/mpanel/module.c +++ b/mpanel/module.c @@ -40,8 +40,6 @@ void load_modules(void) { void (*call)(MwWidget box, config_setting_t* setting) = dlsym(lib, "module"); call(box, c); - - dlclose(lib); } free(p); diff --git a/mpanel/mpanelrc.in b/mpanel/mpanelrc.in index effda28..752db92 100644 --- a/mpanel/mpanelrc.in +++ b/mpanel/mpanelrc.in @@ -4,5 +4,11 @@ Modules: ( }, { Type = "Separator"; + }, + { + Type = "Separator"; + }, + { + Type = "Time"; } ); diff --git a/mpanel/time/CMakeLists.txt b/mpanel/time/CMakeLists.txt new file mode 100644 index 0000000..f7cf608 --- /dev/null +++ b/mpanel/time/CMakeLists.txt @@ -0,0 +1,6 @@ +include(MDEBase) +include(MDEPkgConfig) +mde_setup_library(TimeModule ${CMAKE_INSTALL_LIBDIR}/mpanel) + +mde_pkg_config_add(TimeModule libconfig) + diff --git a/mpanel/time/module.c b/mpanel/time/module.c new file mode 100644 index 0000000..74bf367 --- /dev/null +++ b/mpanel/time/module.c @@ -0,0 +1,83 @@ +#include +#include + +#include +#include + +static time_t last; + +static void tick(MwWidget handle, void* user, void* client) { + MwWidget* children = MwGetChildren(handle); + int i; + time_t t = time(NULL); + struct tm* tm; + + if(t == last) return; + last = t; + + tm = localtime(&t); + + for(i = 0; children[i] != NULL; i++) { + const char* n = MwGetName(children[i]); + if(strcmp(n, "seg") == 0) { + char buf[16]; + + sprintf(buf, "%02d:%02d", tm->tm_hour, tm->tm_min); + + MwVaApply(children[i], + MwNtext, buf, + NULL); + } else if(strcmp(n, "date") == 0) { + char buf[16]; + const char* mon[] = { + "Jan", + "Feb", + "Mar", + "Apr", + "May", + "Jun", + "Jul", + "Aug", + "Sep", + "Oct", + "Nov", + "Dec"}; + + sprintf(buf, "%s %.2d", mon[tm->tm_mon], tm->tm_mday); + + MwVaApply(children[i], + MwNtext, buf, + NULL); + } + } + + free(children); +} + +void module(MwWidget box, config_setting_t* setting) { + MwWidget b = MwVaCreateWidget(MwBoxClass, "time", box, 0, 0, 0, 0, + MwNfixedSize, 64, + MwNbackground, "#bebdb2", + MwNforeground, "#484841", + MwNorientation, MwVERTICAL, + MwNhasBorder, 1, + MwNinverted, 1, + MwNborderWidth, 1, + NULL); + MwWidget seg = MwVaCreateWidget(MwLabelClass, "seg", b, 0, 0, 0, 0, + MwNsevenSegment, 1, + MwNtext, "00:00", + MwNratio, 5, + NULL); + MwWidget date = MwVaCreateWidget(MwLabelClass, "date", b, 0, 0, 0, 0, + MwNtext, "??? ??", + MwNratio, 3, + NULL); + + last = 0; + tick(b, NULL, NULL); + + MwAddUserHandler(b, MwNtickHandler, tick, NULL); + + MwAddTickList(b); +}