From 44f6e9e1ca9813b61167bd8fa4020f5930dc61e3 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Sat, 29 Nov 2025 22:25:52 +0900 Subject: [PATCH] Add mclock Co-Authored-By: comdlg32 <80153347+comdlg32@users.noreply.github.com> --- CMakeLists.txt | 1 + mauplay/db.c | 7 +- mclock/CMakeLists.txt | 6 ++ mclock/clock.c | 167 ++++++++++++++++++++++++++++++++++++++++++ mclock/mclock.h | 21 ++++++ 5 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 mclock/CMakeLists.txt create mode 100644 mclock/clock.c create mode 100644 mclock/mclock.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 80421fc..c20dee6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,3 +6,4 @@ project( add_subdirectory(mbiff) add_subdirectory(mauplay) add_subdirectory(mcompass) +add_subdirectory(mclock) diff --git a/mauplay/db.c b/mauplay/db.c index 8c84f42..f088390 100644 --- a/mauplay/db.c +++ b/mauplay/db.c @@ -70,6 +70,7 @@ static int db_bind(sqlite3_stmt* stmt, const char* path) { const char* s_genre; int s_length; int s_track = 0; + char* str; struct stat s; if(sound == NULL) return 1; @@ -80,7 +81,11 @@ static int db_bind(sqlite3_stmt* stmt, const char* path) { s_track = sound->context->track; s_length = sound->context->frames / sound->context->sample_rate; - if(s_title == NULL) s_title = path; +#ifdef _WIN32 + if(s_title == NULL) s_title = (str = strrchr(path, '\\')) != NULL ? (str + 1) : path; +#else + if(s_title == NULL) s_title = (str = strrchr(path, '/')) != NULL ? (str + 1) : path; +#endif if(s_artist == NULL) s_artist = ""; if(s_album == NULL) s_album = ""; if(s_genre == NULL) s_genre = ""; diff --git a/mclock/CMakeLists.txt b/mclock/CMakeLists.txt new file mode 100644 index 0000000..ce2bd1a --- /dev/null +++ b/mclock/CMakeLists.txt @@ -0,0 +1,6 @@ +include(MDEBase) +include(MDEOpenGL) +setup_project(mclock ${CMAKE_INSTALL_BINDIR}) + +opengl_add(mclock) +math_add(mclock) diff --git a/mclock/clock.c b/mclock/clock.c new file mode 100644 index 0000000..9202ea5 --- /dev/null +++ b/mclock/clock.c @@ -0,0 +1,167 @@ +#include "mclock.h" + +#include + +MwWidget window, opengl; +int ww, wh; +unsigned long last_tick; +MwLLPixmap pxclock; + +static void resize(MwWidget handle, void* user, void* client) { + ww = MwGetInteger(window, MwNwidth); + wh = MwGetInteger(window, MwNheight); + + MwVaApply(opengl, + MwNwidth, ww, + MwNheight, wh, + NULL); + + glViewport(0, 0, ww, wh); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(-1, 1, -1, 1, -1, 1); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + +static void drawHand(float rad, float wid, float leng, int shadow) { + int i; + + double n = M_PI / 2; + if(shadow) { + glPushMatrix(); + glTranslatef(wid / 1, -(wid / 1), 0); + glColor3f(0.45, 0.45, 0.45); + + glBegin(GL_POLYGON); + glVertex2f(cos(rad) * leng, sin(rad) * leng); + glVertex2f(cos(rad - n) * wid, sin(rad - n) * wid); + glVertex2f(-cos(rad) * leng * .1, -sin(rad) * leng * .1); + glVertex2f(-cos(rad - n) * wid, -sin(rad - n) * wid); + glEnd(); + + glPopMatrix(); + + return; + } + + for(i = 0; i < 2; i++) { + if(i == 0) { + glColor3f(0.65, 0.65, 0.65); + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + } else { + glColor3f(0.2, 0.2, 0.2); + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + } + + glBegin(GL_POLYGON); + glVertex2f(cos(rad) * leng, sin(rad) * leng); + glVertex2f(cos(rad - n) * wid, sin(rad - n) * wid); + glVertex2f(-cos(rad) * leng * .1, -sin(rad) * leng * .1); + glVertex2f(-cos(rad - n) * wid, -sin(rad - n) * wid); + glEnd(); + }; + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + + glColor3f(0.65, 0.65, 0.65); +} + +static void draw(MwWidget handle, void* user, void* client) { + int i; + time_t t; + struct tm* tm_info; + + double srad; + double mrad; + double hrad; + double rad; + + double len; + + glClear(GL_COLOR_BUFFER_BIT); + + glLineWidth(1); + + t = time(NULL); + tm_info = localtime(&t); + + srad = M_PI / 2 - (tm_info->tm_sec / 60.0) * 2 * M_PI; + mrad = M_PI / 2 - ((tm_info->tm_min + tm_info->tm_sec / 60.0) / 60.0) * 2 * M_PI; + hrad = M_PI / 2 - (((tm_info->tm_hour % 12) + tm_info->tm_min / 60.0 + tm_info->tm_sec / 3600.0) / 12.0) * 2 * M_PI; + + drawHand(srad, 0.01, 0.98, 1); + drawHand(mrad, 0.05, 0.98, 1); + drawHand(hrad, 0.05, 0.7, 1); + + glColor3f(0, 0, 0); + for(i = 0; i < 360; i += 6) { + rad = i / 180.0 * M_PI; + if(!(i % 30)) { + glLineWidth(3); + len = 0.8; + } else { + glLineWidth(1); + len = 0.85; + } + + glBegin(GL_LINES); + glVertex2f(cos(rad) * len, sin(rad) * len); + glVertex2f(cos(rad) * 0.9, sin(rad) * 0.9); + glEnd(); + } + + glLineWidth(1); + + drawHand(hrad, 0.05, 0.7, 0); + drawHand(mrad, 0.05, 0.98, 0); + drawHand(srad, 0.02, 0.98, 0); + + MwOpenGLSwapBuffer(opengl); +} + +static void tick(MwWidget handle, void* user, void* client) { + unsigned long tick = MwTimeGetTick(); + if((tick - last_tick) >= 1000) { + draw(opengl, NULL, NULL); + last_tick = tick; + } +} + +static void mouseup(MwWidget handle, void* user, void* client) { + MDEAboutDialog(window, "mclock", VERSION, pxclock); +} + +int main() { + MwLLColor col; + int r, g, b; + + MwLibraryInit(); + + last_tick = MwTimeGetTick(); + + window = MwVaCreateWidget(MwWindowClass, "main", NULL, MwDEFAULT, MwDEFAULT, 150, 150, + MwNtitle, "mclock", + NULL); + + opengl = MwCreateWidget(MwOpenGLClass, "opengl", window, 0, 0, 150, 150); + pxclock = MwLoadImage(window, ICON128DIR "/clock.png"); + + MwOpenGLMakeCurrent(opengl); + + col = MwParseColor(window, MwGetText(window, MwNbackground)); + MwGetColor(col, &r, &g, &b); + MwLLFreeColor(col); + + glClearColor(r / 255.0, g / 255.0, b / 255.0, 1); + + resize(window, NULL, NULL); + + MwAddUserHandler(window, MwNresizeHandler, resize, NULL); + MwAddUserHandler(opengl, MwNdrawHandler, draw, NULL); + MwAddUserHandler(opengl, MwNmouseUpHandler, mouseup, NULL); + MwAddUserHandler(window, MwNtickHandler, tick, NULL); + + draw(opengl, NULL, NULL); + + MwLoop(window); +} diff --git a/mclock/mclock.h b/mclock/mclock.h new file mode 100644 index 0000000..ebbaf57 --- /dev/null +++ b/mclock/mclock.h @@ -0,0 +1,21 @@ +#ifndef __MCLOCK_H__ +#define __MCLOCK_H__ + +#define VERSION "0.0.0" + +#include "config.h" + +/* Milsko */ +#include +#include + +/* External */ + +/* Standard */ +#include + +#ifndef M_PI +#define M_PI 3.14159265 +#endif + +#endif