Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- config.mk.orig 2025-08-09 16:01:01.880269593 +0300
- +++ config.mk 2025-09-01 17:30:00.518124652 +0300
- @@ -11,8 +11,8 @@
- X11LIB = /usr/X11R6/lib
- # includes and libs
- -INCS = -I. -I/usr/include -I${X11INC}
- -LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr
- +INCS = -I. -I/usr/include -I${X11INC} -I/usr/include/freetype2
- +LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr -lXft -lfontconfig -lfreetype
- # flags
- CPPFLAGS = -DVERSION=\"${VERSION}\" -D_DEFAULT_SOURCE -DHAVE_SHADOW_H
- --- config.def.h.orig 2025-08-09 16:01:01.880269593 +0300
- +++ config.def.h 2025-09-02 11:55:07.990470262 +0300
- @@ -1,6 +1,6 @@
- /* user and group to drop privileges to */
- -static const char *user = "nobody";
- -static const char *group = "nogroup";
- +static const char *user = "nobody";
- +static const char *group = "nogroup";
- static const char *colorname[NUMCOLS] = {
- [INIT] = "black", /* after initialization */
- @@ -10,3 +10,20 @@
- /* treat a cleared input like a wrong password (color) */
- static const int failonclear = 1;
- +
- +// Time display settings
- +static const char *time_font = "Liberation Sans:style=Bold:size=80";
- +static const char *time_color = "#ffffff";
- +static const char *time_format = "%H:%M";
- +static const int time_y_off = 0; // pixels relative to center
- +
- +// Date display settings
- +static const char *date_font = "Liberation Sans:style=Regular:size=26";
- +static const char *date_color = "#cccccc";
- +static const char *date_format = "%a %d.%m.%Y";
- +static const int date_y_off = 47; // pixels relative to center
- +
- +// Refresh intervals (in seconds)
- +static const int tw_refr_int = 10; // thread_wrapper() interval. Should be the lowest one.
- +static const int tm_refr_int = 10; // interval for draw_time()
- +static const int dt_refr_int = 3600; // interval for draw_date()
- --- slock.c.orig 2025-08-09 16:01:01.880269593 +0300
- +++ slock.c 2025-09-02 12:01:07.023991761 +0300
- @@ -5,6 +5,7 @@
- #endif
- #include <ctype.h>
- +#include <crypt.h>
- #include <errno.h>
- #include <grp.h>
- #include <pwd.h>
- @@ -12,6 +13,7 @@
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- +#include <time.h>
- #include <unistd.h>
- #include <spawn.h>
- #include <sys/types.h>
- @@ -19,6 +21,12 @@
- #include <X11/keysym.h>
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- +#include <X11/Xft/Xft.h>
- +
- +#include <pthread.h>
- +static XftFont *timefont = NULL;
- +static XftFont *datefont = NULL;
- +static pthread_mutex_t mutex= PTHREAD_MUTEX_INITIALIZER;
- #include "arg.h"
- #include "util.h"
- @@ -37,6 +45,9 @@
- Window root, win;
- Pixmap pmap;
- unsigned long colors[NUMCOLS];
- + XftDraw *xftdraw;
- + int w, h;
- + XftColor tmcol, dtcol;
- };
- struct xrandr {
- @@ -125,6 +136,70 @@
- return hash;
- }
- +static void draw_time(Display *dpy, struct lock *lock) {
- + time_t now = time(NULL);
- + struct tm *tm_info = localtime(&now);
- + char timestr[64];
- + strftime(timestr, sizeof(timestr), time_format, tm_info);
- +
- + XGlyphInfo ext;
- + XftTextExtentsUtf8(dpy, timefont, (FcChar8*)timestr, strlen(timestr), &ext);
- + int time_x = (lock->w - ext.xOff) / 2;
- + int time_y = lock->h/2 + time_y_off;
- + XClearArea(dpy, lock->win, time_x, time_y - timefont->ascent, ext.xOff,
- + timefont->ascent + timefont->descent, False);
- + XftDrawStringUtf8(lock->xftdraw, &lock->tmcol, timefont, time_x, time_y,
- + (FcChar8*)timestr, strlen(timestr));
- + XFlush(dpy);
- +}
- +
- +static void draw_date(Display *dpy, struct lock *lock) {
- + time_t now = time(NULL);
- + struct tm *tm_info = localtime(&now);
- + char datestr[64];
- + strftime(datestr, sizeof(datestr), date_format, tm_info);
- +
- + XGlyphInfo ext;
- + XftTextExtentsUtf8(dpy, datefont, (FcChar8*)datestr, strlen(datestr), &ext);
- + int date_x = (lock->w - ext.xOff) / 2;
- + int date_y = lock->h/2 + date_y_off;
- + XClearArea(dpy, lock->win, date_x, date_y - datefont->ascent, ext.xOff,
- + datefont->ascent + datefont->descent, False);
- + XftDrawStringUtf8(lock->xftdraw, &lock->dtcol, datefont, date_x, date_y,
- + (FcChar8*)datestr, strlen(datestr));
- + XFlush(dpy);
- +}
- +
- +struct thw_args {
- + Display *dpy;
- + int nscreens;
- + struct lock **lock;
- +};
- +
- +static void* thread_wrapper(void* arg) {
- + struct thw_args *args = (struct thw_args*)arg;
- + long long tm_tmr = 0, dt_tmr = 0;
- + while (1) {
- + pthread_mutex_lock(&mutex);
- + for (int i = 0; i < args->nscreens; ++i) {
- + if (tm_tmr <= 0) {
- + draw_time(args->dpy, args->lock[i]);
- + tm_tmr = tm_refr_int;
- + };
- + if (dt_tmr <= 0) {
- + draw_date(args->dpy, args->lock[i]);
- + dt_tmr = dt_refr_int;
- + };
- + };
- + pthread_mutex_unlock(&mutex);
- + sleep(tw_refr_int);
- + tm_tmr -= tw_refr_int;
- + dt_tmr -= tw_refr_int;
- + };
- +
- + return NULL;
- +}
- +
- static void
- readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
- const char *hash)
- @@ -190,12 +265,17 @@
- }
- color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT);
- if (running && oldc != color) {
- + pthread_mutex_lock(&mutex);
- for (screen = 0; screen < nscreens; screen++) {
- XSetWindowBackground(dpy,
- locks[screen]->win,
- locks[screen]->colors[color]);
- XClearWindow(dpy, locks[screen]->win);
- + /* Redraw time and date after screen cleared */
- + draw_time(dpy, locks[screen]);
- + draw_date(dpy, locks[screen]);
- }
- + pthread_mutex_unlock(&mutex);
- oldc = color;
- }
- } else if (rr->active && ev.type == rr->evbase + RRScreenChangeNotify) {
- @@ -213,6 +293,7 @@
- break;
- }
- }
- + pthread_mutex_unlock(&mutex);
- } else {
- for (screen = 0; screen < nscreens; screen++)
- XRaiseWindow(dpy, locks[screen]->win);
- @@ -252,6 +333,21 @@
- CopyFromParent,
- DefaultVisual(dpy, lock->screen),
- CWOverrideRedirect | CWBackPixel, &wa);
- +
- + lock->xftdraw = XftDrawCreate(dpy, lock->win,
- + DefaultVisual(dpy, lock->screen),
- + DefaultColormap(dpy, lock->screen));
- + if (!lock->xftdraw)
- + return NULL;
- + lock->w = DisplayWidth(dpy, lock->screen);
- + lock->h = DisplayHeight(dpy, lock->screen);
- + Colormap colormap = DefaultColormap(dpy, lock->screen);
- + Visual *visual = DefaultVisual(dpy, lock->screen);
- + if (!XftColorAllocName(dpy, visual, colormap, time_color, &lock->tmcol))
- + return NULL;
- + if (!XftColorAllocName(dpy, visual, colormap, date_color, &lock->dtcol))
- + return NULL;
- +
- lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
- invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap,
- &color, &color, 0, 0);
- @@ -345,6 +441,8 @@
- if (!crypt("", hash))
- die("slock: crypt: %s\n", strerror(errno));
- + XInitThreads();
- +
- if (!(dpy = XOpenDisplay(NULL)))
- die("slock: cannot open display\n");
- @@ -386,8 +484,32 @@
- }
- }
- + timefont = XftFontOpenName(dpy, DefaultScreen(dpy), time_font);
- + datefont = XftFontOpenName(dpy, DefaultScreen(dpy), date_font);
- + if (!timefont || !datefont) {
- + fprintf(stderr, "slock: failed to load fonts\n");
- + return 1;
- + };
- +
- /* everything is now blank. Wait for the correct password */
- + pthread_t thread_id;
- + struct thw_args args = {dpy, nscreens, locks};
- + pthread_create(&thread_id, NULL, thread_wrapper, &args);
- +
- readpw(dpy, &rr, locks, nscreens, hash);
- +
- + /* Cleanup */
- + if (timefont) XftFontClose(dpy, timefont);
- + if (datefont) XftFontClose(dpy, datefont);
- + for (s = 0; s < nscreens; s++) {
- + if (locks[s]->xftdraw)
- + XftDrawDestroy(locks[s]->xftdraw);
- +
- + Colormap colormap = DefaultColormap(dpy, locks[s]->screen);
- + Visual *visual = DefaultVisual(dpy, locks[s]->screen);
- + XftColorFree(dpy, visual, colormap, &locks[s]->tmcol);
- + XftColorFree(dpy, visual, colormap, &locks[s]->dtcol);
- + }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment