slock-squares-1.5.diff - sites - public wiki contents of suckless.org
 (HTM) git clone git://git.suckless.org/sites
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       slock-squares-1.5.diff (5088B)
       ---
            1 From 2f6938b87a09abcd41fd6792a40b0bd7b088f41a Mon Sep 17 00:00:00 2001
            2 From: bsuth <bsuth701@gmail.com>
            3 Date: Tue, 27 Dec 2022 12:44:23 +0900
            4 Subject: [PATCH] Use centered squares to indicate lock state
            5 
            6 Instead of changing the color of the entire screen to indicate the
            7 current lock state, draw centered squares on each monitor and change the
            8 square colors.
            9 
           10 This patch requires xrandr to be active and otherwise defaults to the
           11 original slock behavior.
           12 ---
           13  config.def.h |  6 +++++-
           14  slock.c      | 58 +++++++++++++++++++++++++++++++++++++++++++++-------
           15  2 files changed, 56 insertions(+), 8 deletions(-)
           16 
           17 diff --git a/config.def.h b/config.def.h
           18 index 9855e21..e7106fb 100644
           19 --- a/config.def.h
           20 +++ b/config.def.h
           21 @@ -3,10 +3,14 @@ static const char *user  = "nobody";
           22  static const char *group = "nogroup";
           23  
           24  static const char *colorname[NUMCOLS] = {
           25 -        [INIT] =   "black",     /* after initialization */
           26 +        [BG] =     "black",     /* background */
           27 +        [INIT] =   "#4f525c",   /* after initialization */
           28          [INPUT] =  "#005577",   /* during input */
           29          [FAILED] = "#CC3333",   /* wrong password */
           30  };
           31  
           32  /* treat a cleared input like a wrong password (color) */
           33  static const int failonclear = 1;
           34 +
           35 +/* size of square in px */
           36 +static const int squaresize = 50;
           37 diff --git a/slock.c b/slock.c
           38 index 5ae738c..0750768 100644
           39 --- a/slock.c
           40 +++ b/slock.c
           41 @@ -25,6 +25,7 @@
           42  char *argv0;
           43  
           44  enum {
           45 +        BG,
           46          INIT,
           47          INPUT,
           48          FAILED,
           49 @@ -36,6 +37,8 @@ struct lock {
           50          Window root, win;
           51          Pixmap pmap;
           52          unsigned long colors[NUMCOLS];
           53 +        GC gc;
           54 +        XRRScreenResources *rrsr;
           55  };
           56  
           57  struct xrandr {
           58 @@ -124,6 +127,44 @@ gethash(void)
           59          return hash;
           60  }
           61  
           62 +static void
           63 +draw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
           64 +     unsigned int color)
           65 +{
           66 +        int screen, crtc;
           67 +        XRRCrtcInfo* rrci;
           68 +
           69 +        if (rr->active) {
           70 +                for (screen = 0; screen < nscreens; screen++) {
           71 +                        XSetWindowBackground(dpy, locks[screen]->win,locks[screen]->colors[BG]);
           72 +                        XClearWindow(dpy, locks[screen]->win);
           73 +                        XSetForeground(dpy, locks[screen]->gc, locks[screen]->colors[color]);
           74 +                        for (crtc = 0; crtc < locks[screen]->rrsr->ncrtc; ++crtc) {
           75 +                                rrci = XRRGetCrtcInfo(dpy,
           76 +                                                      locks[screen]->rrsr,
           77 +                                                      locks[screen]->rrsr->crtcs[crtc]);
           78 +                                /* skip disabled crtc */
           79 +                                if (rrci->noutput > 0)
           80 +                                        XFillRectangle(dpy,
           81 +                                                       locks[screen]->win,
           82 +                                                       locks[screen]->gc,
           83 +                                                       rrci->x + (rrci->width - squaresize) / 2,
           84 +                                                       rrci->y + (rrci->height - squaresize) / 2,
           85 +                                                       squaresize,
           86 +                                                       squaresize);
           87 +                                XRRFreeCrtcInfo(rrci);
           88 +                        }
           89 +                }
           90 +        } else {
           91 +                for (screen = 0; screen < nscreens; screen++) {
           92 +                        XSetWindowBackground(dpy,
           93 +                                             locks[screen]->win,
           94 +                                             locks[screen]->colors[color]);
           95 +                        XClearWindow(dpy, locks[screen]->win);
           96 +                }
           97 +        }
           98 +}
           99 +
          100  static void
          101  readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
          102         const char *hash)
          103 @@ -189,12 +230,7 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
          104                          }
          105                          color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT);
          106                          if (running && oldc != color) {
          107 -                                for (screen = 0; screen < nscreens; screen++) {
          108 -                                        XSetWindowBackground(dpy,
          109 -                                                             locks[screen]->win,
          110 -                                                             locks[screen]->colors[color]);
          111 -                                        XClearWindow(dpy, locks[screen]->win);
          112 -                                }
          113 +                                draw(dpy, rr, locks, nscreens, color);
          114                                  oldc = color;
          115                          }
          116                  } else if (rr->active && ev.type == rr->evbase + RRScreenChangeNotify) {
          117 @@ -228,6 +264,7 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
          118          XColor color, dummy;
          119          XSetWindowAttributes wa;
          120          Cursor invisible;
          121 +        XGCValues gcvalues;
          122  
          123          if (dpy == NULL || screen < 0 || !(lock = malloc(sizeof(struct lock))))
          124                  return NULL;
          125 @@ -243,7 +280,7 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
          126  
          127          /* init */
          128          wa.override_redirect = 1;
          129 -        wa.background_pixel = lock->colors[INIT];
          130 +        wa.background_pixel = lock->colors[BG];
          131          lock->win = XCreateWindow(dpy, lock->root, 0, 0,
          132                                    DisplayWidth(dpy, lock->screen),
          133                                    DisplayHeight(dpy, lock->screen),
          134 @@ -255,6 +292,10 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
          135          invisible = XCreatePixmapCursor(dpy, lock->pmap, lock->pmap,
          136                                          &color, &color, 0, 0);
          137          XDefineCursor(dpy, lock->win, invisible);
          138 +        lock->gc = XCreateGC(dpy, lock->win, 0, &gcvalues);
          139 +        XSetForeground(dpy, lock->gc, lock->colors[INIT]);
          140 +        if (rr->active)
          141 +                lock->rrsr = XRRGetScreenResourcesCurrent(dpy, lock->root);
          142  
          143          /* Try to grab mouse pointer *and* keyboard for 600ms, else fail the lock */
          144          for (i = 0, ptgrab = kbgrab = -1; i < 6; i++) {
          145 @@ -388,6 +429,9 @@ main(int argc, char **argv) {
          146                  }
          147          }
          148  
          149 +        /* draw the initial rectangle */
          150 +        draw(dpy, &rr, locks, nscreens, INIT);
          151 +
          152          /* everything is now blank. Wait for the correct password */
          153          readpw(dpy, &rr, locks, nscreens, hash);
          154  
          155 -- 
          156 2.39.0
          157