Guest User

slock show time xft patch

a guest
Sep 2nd, 2025
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 8.30 KB | Source Code | 0 0
  1. --- config.mk.orig  2025-08-09 16:01:01.880269593 +0300
  2. +++ config.mk   2025-09-01 17:30:00.518124652 +0300
  3. @@ -11,8 +11,8 @@
  4.  X11LIB = /usr/X11R6/lib
  5.  
  6.  # includes and libs
  7. -INCS = -I. -I/usr/include -I${X11INC}
  8. -LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr
  9. +INCS = -I. -I/usr/include -I${X11INC} -I/usr/include/freetype2
  10. +LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr -lXft -lfontconfig -lfreetype
  11.  
  12.  # flags
  13.  CPPFLAGS = -DVERSION=\"${VERSION}\" -D_DEFAULT_SOURCE -DHAVE_SHADOW_H
  14. --- config.def.h.orig   2025-08-09 16:01:01.880269593 +0300
  15. +++ config.def.h    2025-09-02 11:55:07.990470262 +0300
  16. @@ -1,6 +1,6 @@
  17.  /* user and group to drop privileges to */
  18. -static const char *user  = "nobody";
  19. -static const char *group = "nogroup";
  20. +static const char *user  = "nobody";
  21. +static const char *group = "nogroup";
  22.  
  23.  static const char *colorname[NUMCOLS] = {
  24.     [INIT] =   "black",     /* after initialization */
  25. @@ -10,3 +10,20 @@
  26.  
  27.  /* treat a cleared input like a wrong password (color) */
  28.  static const int failonclear = 1;
  29. +
  30. +// Time display settings
  31. +static const char *time_font = "Liberation Sans:style=Bold:size=80";
  32. +static const char *time_color = "#ffffff";
  33. +static const char *time_format = "%H:%M";
  34. +static const int time_y_off = 0; // pixels relative to center
  35. +
  36. +// Date display settings
  37. +static const char *date_font = "Liberation Sans:style=Regular:size=26";
  38. +static const char *date_color = "#cccccc";
  39. +static const char *date_format = "%a %d.%m.%Y";
  40. +static const int date_y_off = 47; // pixels relative to center
  41. +
  42. +// Refresh intervals (in seconds)
  43. +static const int tw_refr_int = 10;   // thread_wrapper() interval. Should be the lowest one.
  44. +static const int tm_refr_int = 10;   // interval for draw_time()
  45. +static const int dt_refr_int = 3600; // interval for draw_date()
  46. --- slock.c.orig    2025-08-09 16:01:01.880269593 +0300
  47. +++ slock.c 2025-09-02 12:01:07.023991761 +0300
  48. @@ -5,6 +5,7 @@
  49.  #endif
  50.  
  51.  #include <ctype.h>
  52. +#include <crypt.h>
  53.  #include <errno.h>
  54.  #include <grp.h>
  55.  #include <pwd.h>
  56. @@ -12,6 +13,7 @@
  57.  #include <stdlib.h>
  58.  #include <stdio.h>
  59.  #include <string.h>
  60. +#include <time.h>
  61.  #include <unistd.h>
  62.  #include <spawn.h>
  63.  #include <sys/types.h>
  64. @@ -19,6 +21,12 @@
  65.  #include <X11/keysym.h>
  66.  #include <X11/Xlib.h>
  67.  #include <X11/Xutil.h>
  68. +#include <X11/Xft/Xft.h>
  69. +
  70. +#include <pthread.h>
  71. +static XftFont *timefont = NULL;
  72. +static XftFont *datefont = NULL;
  73. +static pthread_mutex_t mutex= PTHREAD_MUTEX_INITIALIZER;
  74.  
  75.  #include "arg.h"
  76.  #include "util.h"
  77. @@ -37,6 +45,9 @@
  78.     Window root, win;
  79.     Pixmap pmap;
  80.     unsigned long colors[NUMCOLS];
  81. +    XftDraw *xftdraw;
  82. +    int w, h;
  83. +    XftColor tmcol, dtcol;
  84.  };
  85.  
  86.  struct xrandr {
  87. @@ -125,6 +136,70 @@
  88.     return hash;
  89.  }
  90.  
  91. +static void draw_time(Display *dpy, struct lock *lock) {
  92. +    time_t now = time(NULL);
  93. +    struct tm *tm_info = localtime(&now);
  94. +    char timestr[64];
  95. +    strftime(timestr, sizeof(timestr), time_format, tm_info);
  96. +
  97. +    XGlyphInfo ext;
  98. +    XftTextExtentsUtf8(dpy, timefont, (FcChar8*)timestr, strlen(timestr), &ext);
  99. +    int time_x = (lock->w - ext.xOff) / 2;
  100. +    int time_y = lock->h/2 + time_y_off;
  101. +    XClearArea(dpy, lock->win, time_x, time_y - timefont->ascent, ext.xOff,
  102. +               timefont->ascent + timefont->descent, False);
  103. +    XftDrawStringUtf8(lock->xftdraw, &lock->tmcol, timefont, time_x, time_y,
  104. +                      (FcChar8*)timestr, strlen(timestr));
  105. +    XFlush(dpy);
  106. +}
  107. +
  108. +static void draw_date(Display *dpy, struct lock *lock) {
  109. +    time_t now = time(NULL);
  110. +    struct tm *tm_info = localtime(&now);
  111. +    char datestr[64];
  112. +    strftime(datestr, sizeof(datestr), date_format, tm_info);
  113. +
  114. +    XGlyphInfo ext;
  115. +    XftTextExtentsUtf8(dpy, datefont, (FcChar8*)datestr, strlen(datestr), &ext);
  116. +    int date_x = (lock->w - ext.xOff) / 2;
  117. +    int date_y = lock->h/2 + date_y_off;
  118. +    XClearArea(dpy, lock->win, date_x, date_y - datefont->ascent, ext.xOff,
  119. +               datefont->ascent + datefont->descent, False);
  120. +    XftDrawStringUtf8(lock->xftdraw, &lock->dtcol, datefont, date_x, date_y,
  121. +                      (FcChar8*)datestr, strlen(datestr));
  122. +    XFlush(dpy);
  123. +}
  124. +
  125. +struct thw_args {
  126. +        Display *dpy;
  127. +        int nscreens;
  128. +        struct lock **lock;
  129. +};
  130. +
  131. +static void* thread_wrapper(void* arg) {
  132. +    struct thw_args *args = (struct thw_args*)arg;
  133. +    long long tm_tmr = 0, dt_tmr = 0;
  134. +    while (1) {
  135. +        pthread_mutex_lock(&mutex);
  136. +        for (int i = 0; i < args->nscreens; ++i) {
  137. +            if (tm_tmr <= 0) {
  138. +                draw_time(args->dpy, args->lock[i]);
  139. +                tm_tmr = tm_refr_int;
  140. +            };
  141. +            if (dt_tmr <= 0) {
  142. +                draw_date(args->dpy, args->lock[i]);
  143. +                dt_tmr = dt_refr_int;
  144. +            };
  145. +        };
  146. +        pthread_mutex_unlock(&mutex);
  147. +        sleep(tw_refr_int);
  148. +        tm_tmr -= tw_refr_int;
  149. +        dt_tmr -= tw_refr_int;
  150. +    };
  151. +
  152. +    return NULL;
  153. +}
  154. +
  155.  static void
  156.  readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
  157.         const char *hash)
  158. @@ -190,12 +265,17 @@
  159.             }
  160.             color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT);
  161.             if (running && oldc != color) {
  162. +                pthread_mutex_lock(&mutex);
  163.                 for (screen = 0; screen < nscreens; screen++) {
  164.                     XSetWindowBackground(dpy,
  165.                                          locks[screen]->win,
  166.                                          locks[screen]->colors[color]);
  167.                     XClearWindow(dpy, locks[screen]->win);
  168. +                   /* Redraw time  and date after screen cleared */
  169. +                    draw_time(dpy, locks[screen]);
  170. +                    draw_date(dpy, locks[screen]);
  171.                 }
  172. +                pthread_mutex_unlock(&mutex);
  173.                 oldc = color;
  174.             }
  175.         } else if (rr->active && ev.type == rr->evbase + RRScreenChangeNotify) {
  176. @@ -213,6 +293,7 @@
  177.                     break;
  178.                 }
  179.             }
  180. +            pthread_mutex_unlock(&mutex);
  181.         } else {
  182.             for (screen = 0; screen < nscreens; screen++)
  183.                 XRaiseWindow(dpy, locks[screen]->win);
  184. @@ -252,6 +333,21 @@
  185.                               CopyFromParent,
  186.                               DefaultVisual(dpy, lock->screen),
  187.                               CWOverrideRedirect | CWBackPixel, &wa);
  188. +
  189. +    lock->xftdraw = XftDrawCreate(dpy, lock->win,
  190. +                              DefaultVisual(dpy, lock->screen),
  191. +                              DefaultColormap(dpy, lock->screen));
  192. +    if (!lock->xftdraw)
  193. +        return NULL;
  194. +    lock->w = DisplayWidth(dpy, lock->screen);
  195. +    lock->h = DisplayHeight(dpy, lock->screen);
  196. +    Colormap colormap = DefaultColormap(dpy, lock->screen);
  197. +   Visual *visual = DefaultVisual(dpy, lock->screen);
  198. +   if (!XftColorAllocName(dpy, visual, colormap, time_color, &lock->tmcol))
  199. +               return NULL;
  200. +   if (!XftColorAllocName(dpy, visual, colormap, date_color, &lock->dtcol))
  201. +               return NULL;
  202. +
  203.     lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
  204.     invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap,
  205.                                     &color, &color, 0, 0);
  206. @@ -345,6 +441,8 @@
  207.     if (!crypt("", hash))
  208.         die("slock: crypt: %s\n", strerror(errno));
  209.  
  210. +    XInitThreads();
  211. +
  212.     if (!(dpy = XOpenDisplay(NULL)))
  213.         die("slock: cannot open display\n");
  214.  
  215. @@ -386,8 +484,32 @@
  216.         }
  217.     }
  218.  
  219. +    timefont = XftFontOpenName(dpy, DefaultScreen(dpy), time_font);
  220. +    datefont = XftFontOpenName(dpy, DefaultScreen(dpy), date_font);
  221. +    if (!timefont || !datefont) {
  222. +        fprintf(stderr, "slock: failed to load fonts\n");
  223. +        return 1;
  224. +    };
  225. +
  226.     /* everything is now blank. Wait for the correct password */
  227. +   pthread_t thread_id;
  228. +    struct thw_args args = {dpy, nscreens, locks};
  229. +   pthread_create(&thread_id, NULL, thread_wrapper, &args);
  230. +
  231.     readpw(dpy, &rr, locks, nscreens, hash);
  232. +  
  233. +   /* Cleanup */
  234. +    if (timefont) XftFontClose(dpy, timefont);
  235. +    if (datefont) XftFontClose(dpy, datefont);
  236. +    for (s = 0; s < nscreens; s++) {
  237. +        if (locks[s]->xftdraw)
  238. +            XftDrawDestroy(locks[s]->xftdraw);
  239. +
  240. +        Colormap colormap = DefaultColormap(dpy, locks[s]->screen);
  241. +        Visual *visual = DefaultVisual(dpy, locks[s]->screen);
  242. +        XftColorFree(dpy, visual, colormap, &locks[s]->tmcol);
  243. +        XftColorFree(dpy, visual, colormap, &locks[s]->dtcol);
  244. +    }
  245.  
  246.     return 0;
  247.  }
  248.  
Tags: slock
Advertisement
Add Comment
Please, Sign In to add comment