Guest User

slock drw patch

a guest
Sep 7th, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 23.49 KB | Source Code | 0 0
  1. diff -ruN slock-1.6_vanilla/config.def.h slock-1.6/config.def.h
  2. --- slock-1.6_vanilla/config.def.h  2025-08-09 16:01:01.880269593 +0300
  3. +++ slock-1.6/config.def.h  2025-09-07 11:45:56.007236114 +0300
  4. @@ -2,11 +2,21 @@
  5.  static const char *user  = "nobody";
  6.  static const char *group = "nogroup";
  7.  
  8. -static const char *colorname[NUMCOLS] = {
  9. -   [INIT] =   "black",     /* after initialization */
  10. -   [INPUT] =  "#005577",   /* during input */
  11. -   [FAILED] = "#CC3333",   /* wrong password */
  12. +static const char *fonts[]  = { "monospace:size=10" };
  13. +static const char col_wht[] = "#FFFFFF";
  14. +static const char col_blk[] = "#000000";
  15. +static const char col_blu[] = "#005577";
  16. +static const char col_red[] = "#CC3333";
  17. +static const char col_gry[] = "#CCCCCC";
  18. +static const char *colors[][3]      = {
  19. +   /*                    fg       bg       border   */
  20. +   [SchemeInit]   = { col_wht, col_blk, col_blk },
  21. +   [SchemeInput]  = { col_wht, col_blu, col_blu },
  22. +   [SchemeFail]   = { col_wht, col_red, col_red },
  23. +   [SchemeNorm]   = { col_wht, col_blk, col_blk },
  24. +   [SchemeDark]   = { col_gry, col_blk, col_blk },
  25.  };
  26.  
  27. +
  28.  /* treat a cleared input like a wrong password (color) */
  29.  static const int failonclear = 1;
  30. diff -ruN slock-1.6_vanilla/config.mk slock-1.6/config.mk
  31. --- slock-1.6_vanilla/config.mk 2025-08-09 16:01:01.880269593 +0300
  32. +++ slock-1.6/config.mk 2025-09-07 11:28:49.588357082 +0300
  33. @@ -11,14 +11,14 @@
  34.  X11LIB = /usr/X11R6/lib
  35.  
  36.  # includes and libs
  37. -INCS = -I. -I/usr/include -I${X11INC}
  38. -LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr
  39. +INCS = -I. -I/usr/include -I${X11INC} -I/usr/include/freetype2
  40. +LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr -lfontconfig -lXft
  41.  
  42.  # flags
  43.  CPPFLAGS = -DVERSION=\"${VERSION}\" -D_DEFAULT_SOURCE -DHAVE_SHADOW_H
  44. CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
  45. LDFLAGS = -s ${LIBS}
  46. -COMPATSRC = explicit_bzero.c
  47. +COMPATSRC = explicit_bzero.c drw.c util.c
  48.  
  49. # On OpenBSD and Darwin remove -lcrypt from LIBS
  50. #LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 -lXext -lXrandr
  51. diff -ruN slock-1.6_vanilla/drw.c slock-1.6/drw.c
  52. --- slock-1.6_vanilla/drw.c 1970-01-01 03:00:00.000000000 +0300
  53. +++ slock-1.6/drw.c 2025-08-09 16:00:55.740267680 +0300
  54. @@ -0,0 +1,448 @@
  55. +/* See LICENSE file for copyright and license details. */
  56. +#include <stdio.h>
  57. +#include <stdlib.h>
  58. +#include <string.h>
  59. +#include <X11/Xlib.h>
  60. +#include <X11/Xft/Xft.h>
  61. +
  62. +#include "drw.h"
  63. +#include "util.h"
  64. +
  65. +#define UTF_INVALID 0xFFFD
  66. +
  67. +static int
  68. +utf8decode(const char *s_in, long *u, int *err)
  69. +{
  70. +   static const unsigned char lens[] = {
  71. +       /* 0XXXX */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  72. +       /* 10XXX */ 0, 0, 0, 0, 0, 0, 0, 0,  /* invalid */
  73. +       /* 110XX */ 2, 2, 2, 2,
  74. +       /* 1110X */ 3, 3,
  75. +       /* 11110 */ 4,
  76. +       /* 11111 */ 0,  /* invalid */
  77. +   };
  78. +   static const unsigned char leading_mask[] = { 0x7F, 0x1F, 0x0F, 0x07 };
  79. +   static const unsigned int overlong[] = { 0x0, 0x80, 0x0800, 0x10000 };
  80. +
  81. +   const unsigned char *s = (const unsigned char *)s_in;
  82. +   int len = lens[*s >> 3];
  83. +   *u = UTF_INVALID;
  84. +   *err = 1;
  85. +   if (len == 0)
  86. +       return 1;
  87. +
  88. +   long cp = s[0] & leading_mask[len - 1];
  89. +   for (int i = 1; i < len; ++i) {
  90. +       if (s[i] == '\0' || (s[i] & 0xC0) != 0x80)
  91. +           return i;
  92. +       cp = (cp << 6) | (s[i] & 0x3F);
  93. +   }
  94. +   /* out of range, surrogate, overlong encoding */
  95. +   if (cp > 0x10FFFF || (cp >> 11) == 0x1B || cp < overlong[len - 1])
  96. +       return len;
  97. +
  98. +   *err = 0;
  99. +   *u = cp;
  100. +   return len;
  101. +}
  102. +
  103. +Drw *
  104. +drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h)
  105. +{
  106. +   Drw *drw = ecalloc(1, sizeof(Drw));
  107. +
  108. +   drw->dpy = dpy;
  109. +   drw->screen = screen;
  110. +   drw->root = root;
  111. +   drw->w = w;
  112. +   drw->h = h;
  113. +   drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
  114. +   drw->gc = XCreateGC(dpy, root, 0, NULL);
  115. +   XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
  116. +
  117. +   return drw;
  118. +}
  119. +
  120. +void
  121. +drw_resize(Drw *drw, unsigned int w, unsigned int h)
  122. +{
  123. +   if (!drw)
  124. +       return;
  125. +
  126. +   drw->w = w;
  127. +   drw->h = h;
  128. +   if (drw->drawable)
  129. +       XFreePixmap(drw->dpy, drw->drawable);
  130. +   drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
  131. +}
  132. +
  133. +void
  134. +drw_free(Drw *drw)
  135. +{
  136. +   XFreePixmap(drw->dpy, drw->drawable);
  137. +   XFreeGC(drw->dpy, drw->gc);
  138. +   drw_fontset_free(drw->fonts);
  139. +   free(drw);
  140. +}
  141. +
  142. +/* This function is an implementation detail. Library users should use
  143. + * drw_fontset_create instead.
  144. + */
  145. +static Fnt *
  146. +xfont_create(Drw *drw, const char *fontname, FcPattern *fontpattern)
  147. +{
  148. +   Fnt *font;
  149. +   XftFont *xfont = NULL;
  150. +   FcPattern *pattern = NULL;
  151. +
  152. +   if (fontname) {
  153. +       /* Using the pattern found at font->xfont->pattern does not yield the
  154. +        * same substitution results as using the pattern returned by
  155. +        * FcNameParse; using the latter results in the desired fallback
  156. +        * behaviour whereas the former just results in missing-character
  157. +        * rectangles being drawn, at least with some fonts. */
  158. +       if (!(xfont = XftFontOpenName(drw->dpy, drw->screen, fontname))) {
  159. +           fprintf(stderr, "error, cannot load font from name: '%s'\n", fontname);
  160. +           return NULL;
  161. +       }
  162. +       if (!(pattern = FcNameParse((FcChar8 *) fontname))) {
  163. +           fprintf(stderr, "error, cannot parse font name to pattern: '%s'\n", fontname);
  164. +           XftFontClose(drw->dpy, xfont);
  165. +           return NULL;
  166. +       }
  167. +   } else if (fontpattern) {
  168. +       if (!(xfont = XftFontOpenPattern(drw->dpy, fontpattern))) {
  169. +           fprintf(stderr, "error, cannot load font from pattern.\n");
  170. +           return NULL;
  171. +       }
  172. +   } else {
  173. +       die("no font specified.");
  174. +   }
  175. +
  176. +   font = ecalloc(1, sizeof(Fnt));
  177. +   font->xfont = xfont;
  178. +   font->pattern = pattern;
  179. +   font->h = xfont->ascent + xfont->descent;
  180. +   font->dpy = drw->dpy;
  181. +
  182. +   return font;
  183. +}
  184. +
  185. +static void
  186. +xfont_free(Fnt *font)
  187. +{
  188. +   if (!font)
  189. +       return;
  190. +   if (font->pattern)
  191. +       FcPatternDestroy(font->pattern);
  192. +   XftFontClose(font->dpy, font->xfont);
  193. +   free(font);
  194. +}
  195. +
  196. +Fnt*
  197. +drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount)
  198. +{
  199. +   Fnt *cur, *ret = NULL;
  200. +   size_t i;
  201. +
  202. +   if (!drw || !fonts)
  203. +       return NULL;
  204. +
  205. +   for (i = 1; i <= fontcount; i++) {
  206. +       if ((cur = xfont_create(drw, fonts[fontcount - i], NULL))) {
  207. +           cur->next = ret;
  208. +           ret = cur;
  209. +       }
  210. +   }
  211. +   return (drw->fonts = ret);
  212. +}
  213. +
  214. +void
  215. +drw_fontset_free(Fnt *font)
  216. +{
  217. +   if (font) {
  218. +       drw_fontset_free(font->next);
  219. +       xfont_free(font);
  220. +   }
  221. +}
  222. +
  223. +void
  224. +drw_clr_create(Drw *drw, Clr *dest, const char *clrname)
  225. +{
  226. +   if (!drw || !dest || !clrname)
  227. +       return;
  228. +
  229. +   if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
  230. +                          DefaultColormap(drw->dpy, drw->screen),
  231. +                          clrname, dest))
  232. +       die("error, cannot allocate color '%s'", clrname);
  233. +}
  234. +
  235. +/* Wrapper to create color schemes. The caller has to call free(3) on the
  236. + * returned color scheme when done using it. */
  237. +Clr *
  238. +drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
  239. +{
  240. +   size_t i;
  241. +   Clr *ret;
  242. +
  243. +   /* need at least two colors for a scheme */
  244. +   if (!drw || !clrnames || clrcount < 2 || !(ret = ecalloc(clrcount, sizeof(XftColor))))
  245. +       return NULL;
  246. +
  247. +   for (i = 0; i < clrcount; i++)
  248. +       drw_clr_create(drw, &ret[i], clrnames[i]);
  249. +   return ret;
  250. +}
  251. +
  252. +void
  253. +drw_setfontset(Drw *drw, Fnt *set)
  254. +{
  255. +   if (drw)
  256. +       drw->fonts = set;
  257. +}
  258. +
  259. +void
  260. +drw_setscheme(Drw *drw, Clr *scm)
  261. +{
  262. +   if (drw)
  263. +       drw->scheme = scm;
  264. +}
  265. +
  266. +void
  267. +drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert)
  268. +{
  269. +   if (!drw || !drw->scheme)
  270. +       return;
  271. +   XSetForeground(drw->dpy, drw->gc, invert ? drw->scheme[ColBg].pixel : drw->scheme[ColFg].pixel);
  272. +   if (filled)
  273. +       XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  274. +   else
  275. +       XDrawRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w - 1, h - 1);
  276. +}
  277. +
  278. +int
  279. +drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert)
  280. +{
  281. +   int ty, ellipsis_x = 0;
  282. +   unsigned int tmpw, ew, ellipsis_w = 0, ellipsis_len, hash, h0, h1;
  283. +   XftDraw *d = NULL;
  284. +   Fnt *usedfont, *curfont, *nextfont;
  285. +   int utf8strlen, utf8charlen, utf8err, render = x || y || w || h;
  286. +   long utf8codepoint = 0;
  287. +   const char *utf8str;
  288. +   FcCharSet *fccharset;
  289. +   FcPattern *fcpattern;
  290. +   FcPattern *match;
  291. +   XftResult result;
  292. +   int charexists = 0, overflow = 0;
  293. +   /* keep track of a couple codepoints for which we have no match. */
  294. +   static unsigned int nomatches[128], ellipsis_width, invalid_width;
  295. +   static const char invalid[] = "";
  296. +
  297. +   if (!drw || (render && (!drw->scheme || !w)) || !text || !drw->fonts)
  298. +       return 0;
  299. +
  300. +   if (!render) {
  301. +       w = invert ? invert : ~invert;
  302. +   } else {
  303. +       XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
  304. +       XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  305. +       if (w < lpad)
  306. +           return x + w;
  307. +       d = XftDrawCreate(drw->dpy, drw->drawable,
  308. +                         DefaultVisual(drw->dpy, drw->screen),
  309. +                         DefaultColormap(drw->dpy, drw->screen));
  310. +       x += lpad;
  311. +       w -= lpad;
  312. +   }
  313. +
  314. +   usedfont = drw->fonts;
  315. +   if (!ellipsis_width && render)
  316. +       ellipsis_width = drw_fontset_getwidth(drw, "...");
  317. +   if (!invalid_width && render)
  318. +       invalid_width = drw_fontset_getwidth(drw, invalid);
  319. +   while (1) {
  320. +       ew = ellipsis_len = utf8err = utf8charlen = utf8strlen = 0;
  321. +       utf8str = text;
  322. +       nextfont = NULL;
  323. +       while (*text) {
  324. +           utf8charlen = utf8decode(text, &utf8codepoint, &utf8err);
  325. +           for (curfont = drw->fonts; curfont; curfont = curfont->next) {
  326. +               charexists = charexists || XftCharExists(drw->dpy, curfont->xfont, utf8codepoint);
  327. +               if (charexists) {
  328. +                   drw_font_getexts(curfont, text, utf8charlen, &tmpw, NULL);
  329. +                   if (ew + ellipsis_width <= w) {
  330. +                       /* keep track where the ellipsis still fits */
  331. +                       ellipsis_x = x + ew;
  332. +                       ellipsis_w = w - ew;
  333. +                       ellipsis_len = utf8strlen;
  334. +                   }
  335. +
  336. +                   if (ew + tmpw > w) {
  337. +                       overflow = 1;
  338. +                       /* called from drw_fontset_getwidth_clamp():
  339. +                        * it wants the width AFTER the overflow
  340. +                        */
  341. +                       if (!render)
  342. +                           x += tmpw;
  343. +                       else
  344. +                           utf8strlen = ellipsis_len;
  345. +                   } else if (curfont == usedfont) {
  346. +                       text += utf8charlen;
  347. +                       utf8strlen += utf8err ? 0 : utf8charlen;
  348. +                       ew += utf8err ? 0 : tmpw;
  349. +                   } else {
  350. +                       nextfont = curfont;
  351. +                   }
  352. +                   break;
  353. +               }
  354. +           }
  355. +
  356. +           if (overflow || !charexists || nextfont || utf8err)
  357. +               break;
  358. +           else
  359. +               charexists = 0;
  360. +       }
  361. +
  362. +       if (utf8strlen) {
  363. +           if (render) {
  364. +               ty = y + (h - usedfont->h) / 2 + usedfont->xfont->ascent;
  365. +               XftDrawStringUtf8(d, &drw->scheme[invert ? ColBg : ColFg],
  366. +                                 usedfont->xfont, x, ty, (XftChar8 *)utf8str, utf8strlen);
  367. +           }
  368. +           x += ew;
  369. +           w -= ew;
  370. +       }
  371. +       if (utf8err && (!render || invalid_width < w)) {
  372. +           if (render)
  373. +               drw_text(drw, x, y, w, h, 0, invalid, invert);
  374. +           x += invalid_width;
  375. +           w -= invalid_width;
  376. +       }
  377. +       if (render && overflow)
  378. +           drw_text(drw, ellipsis_x, y, ellipsis_w, h, 0, "...", invert);
  379. +
  380. +       if (!*text || overflow) {
  381. +           break;
  382. +       } else if (nextfont) {
  383. +           charexists = 0;
  384. +           usedfont = nextfont;
  385. +       } else {
  386. +           /* Regardless of whether or not a fallback font is found, the
  387. +            * character must be drawn. */
  388. +           charexists = 1;
  389. +
  390. +           hash = (unsigned int)utf8codepoint;
  391. +           hash = ((hash >> 16) ^ hash) * 0x21F0AAAD;
  392. +           hash = ((hash >> 15) ^ hash) * 0xD35A2D97;
  393. +           h0 = ((hash >> 15) ^ hash) % LENGTH(nomatches);
  394. +           h1 = (hash >> 17) % LENGTH(nomatches);
  395. +           /* avoid expensive XftFontMatch call when we know we won't find a match */
  396. +           if (nomatches[h0] == utf8codepoint || nomatches[h1] == utf8codepoint)
  397. +               goto no_match;
  398. +
  399. +           fccharset = FcCharSetCreate();
  400. +           FcCharSetAddChar(fccharset, utf8codepoint);
  401. +
  402. +           if (!drw->fonts->pattern) {
  403. +               /* Refer to the comment in xfont_create for more information. */
  404. +               die("the first font in the cache must be loaded from a font string.");
  405. +           }
  406. +
  407. +           fcpattern = FcPatternDuplicate(drw->fonts->pattern);
  408. +           FcPatternAddCharSet(fcpattern, FC_CHARSET, fccharset);
  409. +           FcPatternAddBool(fcpattern, FC_SCALABLE, FcTrue);
  410. +
  411. +           FcConfigSubstitute(NULL, fcpattern, FcMatchPattern);
  412. +           FcDefaultSubstitute(fcpattern);
  413. +           match = XftFontMatch(drw->dpy, drw->screen, fcpattern, &result);
  414. +
  415. +           FcCharSetDestroy(fccharset);
  416. +           FcPatternDestroy(fcpattern);
  417. +
  418. +           if (match) {
  419. +               usedfont = xfont_create(drw, NULL, match);
  420. +               if (usedfont && XftCharExists(drw->dpy, usedfont->xfont, utf8codepoint)) {
  421. +                   for (curfont = drw->fonts; curfont->next; curfont = curfont->next)
  422. +                       ; /* NOP */
  423. +                   curfont->next = usedfont;
  424. +               } else {
  425. +                   xfont_free(usedfont);
  426. +                   nomatches[nomatches[h0] ? h1 : h0] = utf8codepoint;
  427. +no_match:
  428. +                   usedfont = drw->fonts;
  429. +               }
  430. +           }
  431. +       }
  432. +   }
  433. +   if (d)
  434. +       XftDrawDestroy(d);
  435. +
  436. +   return x + (render ? w : 0);
  437. +}
  438. +
  439. +void
  440. +drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h)
  441. +{
  442. +   if (!drw)
  443. +       return;
  444. +
  445. +   XCopyArea(drw->dpy, drw->drawable, win, drw->gc, x, y, w, h, x, y);
  446. +   XSync(drw->dpy, False);
  447. +}
  448. +
  449. +unsigned int
  450. +drw_fontset_getwidth(Drw *drw, const char *text)
  451. +{
  452. +   if (!drw || !drw->fonts || !text)
  453. +       return 0;
  454. +   return drw_text(drw, 0, 0, 0, 0, 0, text, 0);
  455. +}
  456. +
  457. +unsigned int
  458. +drw_fontset_getwidth_clamp(Drw *drw, const char *text, unsigned int n)
  459. +{
  460. +   unsigned int tmp = 0;
  461. +   if (drw && drw->fonts && text && n)
  462. +       tmp = drw_text(drw, 0, 0, 0, 0, 0, text, n);
  463. +   return MIN(n, tmp);
  464. +}
  465. +
  466. +void
  467. +drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h)
  468. +{
  469. +   XGlyphInfo ext;
  470. +
  471. +   if (!font || !text)
  472. +       return;
  473. +
  474. +   XftTextExtentsUtf8(font->dpy, font->xfont, (XftChar8 *)text, len, &ext);
  475. +   if (w)
  476. +       *w = ext.xOff;
  477. +   if (h)
  478. +       *h = font->h;
  479. +}
  480. +
  481. +Cur *
  482. +drw_cur_create(Drw *drw, int shape)
  483. +{
  484. +   Cur *cur;
  485. +
  486. +   if (!drw || !(cur = ecalloc(1, sizeof(Cur))))
  487. +       return NULL;
  488. +
  489. +   cur->cursor = XCreateFontCursor(drw->dpy, shape);
  490. +
  491. +   return cur;
  492. +}
  493. +
  494. +void
  495. +drw_cur_free(Drw *drw, Cur *cursor)
  496. +{
  497. +   if (!cursor)
  498. +       return;
  499. +
  500. +   XFreeCursor(drw->dpy, cursor->cursor);
  501. +   free(cursor);
  502. +}
  503. diff -ruN slock-1.6_vanilla/drw.h slock-1.6/drw.h
  504. --- slock-1.6_vanilla/drw.h 1970-01-01 03:00:00.000000000 +0300
  505. +++ slock-1.6/drw.h 2025-08-09 16:00:55.740267680 +0300
  506. @@ -0,0 +1,58 @@
  507. +/* See LICENSE file for copyright and license details. */
  508. +
  509. +typedef struct {
  510. +   Cursor cursor;
  511. +} Cur;
  512. +
  513. +typedef struct Fnt {
  514. +   Display *dpy;
  515. +   unsigned int h;
  516. +   XftFont *xfont;
  517. +   FcPattern *pattern;
  518. +   struct Fnt *next;
  519. +} Fnt;
  520. +
  521. +enum { ColFg, ColBg, ColBorder }; /* Clr scheme index */
  522. +typedef XftColor Clr;
  523. +
  524. +typedef struct {
  525. +   unsigned int w, h;
  526. +   Display *dpy;
  527. +   int screen;
  528. +   Window root;
  529. +   Drawable drawable;
  530. +   GC gc;
  531. +   Clr *scheme;
  532. +   Fnt *fonts;
  533. +} Drw;
  534. +
  535. +/* Drawable abstraction */
  536. +Drw *drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h);
  537. +void drw_resize(Drw *drw, unsigned int w, unsigned int h);
  538. +void drw_free(Drw *drw);
  539. +
  540. +/* Fnt abstraction */
  541. +Fnt *drw_fontset_create(Drw* drw, const char *fonts[], size_t fontcount);
  542. +void drw_fontset_free(Fnt* set);
  543. +unsigned int drw_fontset_getwidth(Drw *drw, const char *text);
  544. +unsigned int drw_fontset_getwidth_clamp(Drw *drw, const char *text, unsigned int n);
  545. +void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h);
  546. +
  547. +/* Colorscheme abstraction */
  548. +void drw_clr_create(Drw *drw, Clr *dest, const char *clrname);
  549. +Clr *drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount);
  550. +
  551. +/* Cursor abstraction */
  552. +Cur *drw_cur_create(Drw *drw, int shape);
  553. +void drw_cur_free(Drw *drw, Cur *cursor);
  554. +
  555. +/* Drawing context manipulation */
  556. +void drw_setfontset(Drw *drw, Fnt *set);
  557. +void drw_setscheme(Drw *drw, Clr *scm);
  558. +
  559. +/* Drawing functions */
  560. +void drw_rect(Drw *drw, int x, int y, unsigned int w, unsigned int h, int filled, int invert);
  561. +int drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lpad, const char *text, int invert);
  562. +
  563. +/* Map functions */
  564. +void drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h);
  565. diff -ruN slock-1.6_vanilla/Makefile slock-1.6/Makefile
  566. --- slock-1.6_vanilla/Makefile  2025-08-09 16:01:01.880269593 +0300
  567. +++ slock-1.6/Makefile  2025-09-07 11:25:35.017045470 +0300
  568. @@ -25,7 +25,7 @@
  569. dist: clean
  570.     mkdir -p slock-${VERSION}
  571.     cp -R LICENSE Makefile README slock.1 config.mk \
  572. -       ${SRC} config.def.h arg.h util.h slock-${VERSION}
  573. +       ${SRC} config.def.h arg.h drw.h util.h slock-${VERSION}
  574.     tar -cf slock-${VERSION}.tar slock-${VERSION}
  575.     gzip slock-${VERSION}.tar
  576.     rm -rf slock-${VERSION}
  577. diff -ruN slock-1.6_vanilla/slock.c slock-1.6/slock.c
  578. --- slock-1.6_vanilla/slock.c   2025-08-09 16:01:01.880269593 +0300
  579. +++ slock-1.6/slock.c   2025-09-07 12:49:33.112322994 +0300
  580. @@ -19,24 +19,22 @@
  581. #include <X11/keysym.h>
  582. #include <X11/Xlib.h>
  583. #include <X11/Xutil.h>
  584. +#include <X11/Xft/Xft.h>
  585.  
  586. #include "arg.h"
  587. +#include "drw.h"
  588. #include "util.h"
  589.  
  590. char *argv0;
  591.  
  592. -enum {
  593. -   INIT,
  594. -   INPUT,
  595. -   FAILED,
  596. -   NUMCOLS
  597. -};
  598. +#define TEXTW(X)                (drw_fontset_getwidth(drw, (X)))
  599.  
  600. +enum { SchemeInit, SchemeInput, SchemeFail, SchemeNorm, SchemeDark }; /* color schemes */
  601. +enum { FGCOL, BGCOL, BRCOL }; /* colors in color scheme */
  602. struct lock {
  603. -   int screen;
  604. -   Window root, win;
  605. -   Pixmap pmap;
  606. -   unsigned long colors[NUMCOLS];
  607. +   Drw *drw;
  608. +    Clr **scheme;
  609. +   Window win;
  610. };
  611.  
  612. struct xrandr {
  613. @@ -47,17 +45,6 @@
  614.  
  615. #include "config.h"
  616.  
  617. -static void
  618. -die(const char *errstr, ...)
  619. -{
  620. -   va_list ap;
  621. -
  622. -   va_start(ap, errstr);
  623. -   vfprintf(stderr, errstr, ap);
  624. -   va_end(ap);
  625. -   exit(1);
  626. -}
  627. -
  628. #ifdef __linux__
  629. #include <fcntl.h>
  630. #include <linux/oom.h>
  631. @@ -139,7 +126,7 @@
  632.     len = 0;
  633.     running = 1;
  634.     failure = 0;
  635. -   oldc = INIT;
  636. +   oldc = SchemeInit;
  637.  
  638.     while (running && !XNextEvent(dpy, &ev)) {
  639.         if (ev.type == KeyPress) {
  640. @@ -188,13 +175,18 @@
  641.                 }
  642.                 break;
  643.             }
  644. -           color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT);
  645. +           color = len ? SchemeInput : ((failure || failonclear) ? SchemeFail : SchemeInit);
  646.             if (running && oldc != color) {
  647.                 for (screen = 0; screen < nscreens; screen++) {
  648. -                   XSetWindowBackground(dpy,
  649. +                    XSetWindowBackground(dpy,
  650.                                          locks[screen]->win,
  651. -                                        locks[screen]->colors[color]);
  652. +                                        locks[screen]->drw->scheme[BGCOL].pixel);
  653.                     XClearWindow(dpy, locks[screen]->win);
  654. +                    //drw_setscheme(locks[screen]->drw, locks[screen]->scheme[color]);
  655. +                   //drw_rect(locks[screen]->drw, 0, 0, locks[screen]->drw->w, locks[screen]->drw->h, 1, 1);
  656. +                    //drw_setscheme(locks[screen]->drw, locks[screen]->scheme[SchemeNorm]);
  657. +                    //drw_text(locks[screen]->drw, 100, 150, drw_fontset_getwidth(locks[screen]->drw,"Hello, drw!"), locks[screen]->drw->fonts->h, 0, "Hello, drw!", 0);
  658. +                    //drw_map(locks[screen]->drw, locks[screen]->win, 0, 0, locks[screen]->drw->w,locks[screen]->drw->h);
  659.                 }
  660.                 oldc = color;
  661.             }
  662. @@ -224,49 +216,51 @@
  663. lockscreen(Display *dpy, struct xrandr *rr, int screen)
  664. {
  665.     char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
  666. -   int i, ptgrab, kbgrab;
  667. +   int i, ptgrab, kbgrab, w, h;
  668. +    Window root;
  669. +    Pixmap pmap;
  670.     struct lock *lock;
  671. -   XColor color, dummy;
  672. +    XColor color = {0, 0, 0, DoRed | DoGreen | DoBlue};
  673.     XSetWindowAttributes wa;
  674.     Cursor invisible;
  675.  
  676.     if (dpy == NULL || screen < 0 || !(lock = malloc(sizeof(struct lock))))
  677.         return NULL;
  678.  
  679. -   lock->screen = screen;
  680. -   lock->root = RootWindow(dpy, lock->screen);
  681. -
  682. -   for (i = 0; i < NUMCOLS; i++) {
  683. -       XAllocNamedColor(dpy, DefaultColormap(dpy, lock->screen),
  684. -                        colorname[i], &color, &dummy);
  685. -       lock->colors[i] = color.pixel;
  686. -   }
  687. +   root = RootWindow(dpy, screen);
  688. +    w = DisplayWidth(dpy, screen);
  689. +    h = DisplayHeight(dpy, screen);
  690. +    lock->drw = drw_create(dpy, screen, root, w, h);
  691. +    lock->drw->fonts = drw_fontset_create(lock->drw, fonts, LENGTH(fonts));
  692. +
  693. +    lock->scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
  694. +   for (i = 0; i < LENGTH(colors); i++) {
  695. +       lock->scheme[i] = drw_scm_create(lock->drw, colors[i], 3);
  696. +    };
  697.  
  698.     /* init */
  699.     wa.override_redirect = 1;
  700. -   wa.background_pixel = lock->colors[INIT];
  701. -   lock->win = XCreateWindow(dpy, lock->root, 0, 0,
  702. -                             DisplayWidth(dpy, lock->screen),
  703. -                             DisplayHeight(dpy, lock->screen),
  704. -                             0, DefaultDepth(dpy, lock->screen),
  705. +   wa.background_pixel = lock->scheme[SchemeInit][BGCOL].pixel;
  706. +   lock->win = XCreateWindow(dpy, lock->drw->root, 0, 0, w, h,
  707. +                             0, DefaultDepth(dpy, lock->drw->screen),
  708.                               CopyFromParent,
  709. -                             DefaultVisual(dpy, lock->screen),
  710. +                             DefaultVisual(dpy, lock->drw->screen),
  711.                               CWOverrideRedirect | CWBackPixel, &wa);
  712. -   lock->pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
  713. -   invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap,
  714. +   pmap = XCreateBitmapFromData(dpy, lock->win, curs, 8, 8);
  715. +   invisible = XCreatePixmapCursor(dpy, pmap, pmap,
  716.                                     &color, &color, 0, 0);
  717.     XDefineCursor(dpy, lock->win, invisible);
  718.  
  719.     /* Try to grab mouse pointer *and* keyboard for 600ms, else fail the lock */
  720.     for (i = 0, ptgrab = kbgrab = -1; i < 6; i++) {
  721.         if (ptgrab != GrabSuccess) {
  722. -           ptgrab = XGrabPointer(dpy, lock->root, False,
  723. +           ptgrab = XGrabPointer(dpy, lock->drw->root, False,
  724.                                   ButtonPressMask | ButtonReleaseMask |
  725.                                   PointerMotionMask, GrabModeAsync,
  726. -                                 GrabModeAsync, None, invisible, CurrentTime);
  727. +                                 GrabModeAsync, None, None, CurrentTime);
  728.         }
  729.         if (kbgrab != GrabSuccess) {
  730. -           kbgrab = XGrabKeyboard(dpy, lock->root, True,
  731. +           kbgrab = XGrabKeyboard(dpy, lock->drw->root, True,
  732.                                    GrabModeAsync, GrabModeAsync, CurrentTime);
  733.         }
  734.  
  735. @@ -276,7 +270,7 @@
  736.             if (rr->active)
  737.                 XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
  738.  
  739. -           XSelectInput(dpy, lock->root, SubstructureNotifyMask);
  740. +           XSelectInput(dpy, lock->drw->root, SubstructureNotifyMask);
  741.             return lock;
  742.         }
  743.  
  744. diff -ruN slock-1.6_vanilla/util.c slock-1.6/util.c
  745. --- slock-1.6_vanilla/util.c    1970-01-01 03:00:00.000000000 +0300
  746. +++ slock-1.6/util.c    2025-09-07 11:33:50.282776242 +0300
  747. @@ -0,0 +1,28 @@
  748. +#include <stdarg.h>
  749. +#include <stdio.h>
  750. +#include <stdlib.h>
  751. +#include <string.h>
  752. +
  753. +#include "util.h"
  754. +
  755. +void
  756. +die(const char *errstr, ...)
  757. +{
  758. +   va_list ap;
  759. +
  760. +   va_start(ap, errstr);
  761. +   vfprintf(stderr, errstr, ap);
  762. +   va_end(ap);
  763. +   exit(1);
  764. +}
  765. +
  766. +void *
  767. +ecalloc(size_t nmemb, size_t size)
  768. +{
  769. +   void *p;
  770. +
  771. +   if (!(p = calloc(nmemb, size)))
  772. +       die("calloc:");
  773. +   return p;
  774. +}
  775. +
  776. diff -ruN slock-1.6_vanilla/util.h slock-1.6/util.h
  777. --- slock-1.6_vanilla/util.h    2025-08-09 16:01:01.880269593 +0300
  778. +++ slock-1.6/util.h    2025-09-07 11:33:55.697781967 +0300
  779. @@ -1,2 +1,9 @@
  780. #undef explicit_bzero
  781. void explicit_bzero(void *, size_t);
  782. +void die(const char *errstr, ...);
  783. +void *ecalloc(size_t nmemb, size_t size);
  784. +
  785. +#define MAX(A, B)               ((A) > (B) ? (A) : (B))
  786. +#define MIN(A, B)               ((A) < (B) ? (A) : (B))
  787. +#define BETWEEN(X, A, B)        ((A) <= (X) && (X) <= (B))
  788. +#define LENGTH(X)               (sizeof (X) / sizeof (X)[0])
  789.  
Tags: slock drw
Advertisement
Add Comment
Please, Sign In to add comment