slock-dwmlogo-20210324.diff - sites - public wiki contents of suckless.org
 (HTM) git clone git://git.suckless.org/sites
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       slock-dwmlogo-20210324.diff (7294B)
       ---
            1 diff --git a/config.def.h b/config.def.h
            2 index 9855e21..0940fb8 100644
            3 --- a/config.def.h
            4 +++ b/config.def.h
            5 @@ -3,10 +3,30 @@ static const char *user  = "nobody";
            6  static const char *group = "nogroup";
            7  
            8  static const char *colorname[NUMCOLS] = {
            9 -        [INIT] =   "black",     /* after initialization */
           10 +        [BACKGROUND] =   "black",     /* after initialization */
           11 +        [INIT] =   "#2d2d2d",     /* after initialization */
           12          [INPUT] =  "#005577",   /* during input */
           13          [FAILED] = "#CC3333",   /* wrong password */
           14  };
           15  
           16  /* treat a cleared input like a wrong password (color) */
           17  static const int failonclear = 1;
           18 +
           19 +/* insert grid pattern with scale 1:1, the size can be changed with logosize */
           20 +static const int logosize = 75;
           21 +static const int logow = 12;        /* grid width and height for right center alignment*/
           22 +static const int logoh = 6;
           23 +
           24 +static XRectangle rectangles[9] = {
           25 +        /* x        y        w        h */
           26 +        { 0,        3,        1,        3 },
           27 +        { 1,        3,        2,        1 },
           28 +        { 0,        5,        8,        1 },
           29 +        { 3,        0,        1,        5 },
           30 +        { 5,        3,        1,        2 },
           31 +        { 7,        3,        1,        2 },
           32 +        { 8,        3,        4,        1 },
           33 +        { 9,        4,        1,        2 },
           34 +        { 11,        4,        1,        2 },
           35 +
           36 +};
           37 diff --git a/config.mk b/config.mk
           38 index 74429ae..08356e8 100644
           39 --- a/config.mk
           40 +++ b/config.mk
           41 @@ -10,12 +10,20 @@ MANPREFIX = ${PREFIX}/share/man
           42  X11INC = /usr/X11R6/include
           43  X11LIB = /usr/X11R6/lib
           44  
           45 +# Xinerama, comment if you don't want it
           46 +XINERAMALIBS  = -lXinerama
           47 +XINERAMAFLAGS = -DXINERAMA
           48 +
           49 +# freetype
           50 +FREETYPELIBS = -lXft
           51 +FREETYPEINC = /usr/include/freetype2
           52 +
           53  # includes and libs
           54 -INCS = -I. -I/usr/include -I${X11INC}
           55 -LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 -lXext -lXrandr
           56 +INCS = -I. -I/usr/include -I${X11INC} -I${FREETYPEINC}
           57 +LIBS = -L/usr/lib -lc -lcrypt -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS} -lXext -lXrandr
           58  
           59  # flags
           60 -CPPFLAGS = -DVERSION=\"${VERSION}\" -D_DEFAULT_SOURCE -DHAVE_SHADOW_H
           61 +CPPFLAGS = -DVERSION=\"${VERSION}\" -D_DEFAULT_SOURCE -DHAVE_SHADOW_H ${XINERAMAFLAGS}
           62  CFLAGS = -std=c99 -pedantic -Wall -Os ${INCS} ${CPPFLAGS}
           63  LDFLAGS = -s ${LIBS}
           64  COMPATSRC = explicit_bzero.c
           65 diff --git a/slock.c b/slock.c
           66 index 5ae738c..3ea9b7f 100644
           67 --- a/slock.c
           68 +++ b/slock.c
           69 @@ -1,5 +1,6 @@
           70  /* See LICENSE file for license details. */
           71 -#define _XOPEN_SOURCE 500
           72 +#define _XOPEN_SOURCE   500
           73 +#define LENGTH(X)       (sizeof X / sizeof X[0])
           74  #if HAVE_SHADOW_H
           75  #include <shadow.h>
           76  #endif
           77 @@ -15,9 +16,13 @@
           78  #include <unistd.h>
           79  #include <sys/types.h>
           80  #include <X11/extensions/Xrandr.h>
           81 +#ifdef XINERAMA
           82 +#include <X11/extensions/Xinerama.h>
           83 +#endif
           84  #include <X11/keysym.h>
           85  #include <X11/Xlib.h>
           86  #include <X11/Xutil.h>
           87 +#include <X11/Xft/Xft.h>
           88  
           89  #include "arg.h"
           90  #include "util.h"
           91 @@ -25,17 +30,25 @@
           92  char *argv0;
           93  
           94  enum {
           95 +        BACKGROUND,
           96          INIT,
           97          INPUT,
           98          FAILED,
           99          NUMCOLS
          100  };
          101  
          102 +#include "config.h"
          103 +
          104  struct lock {
          105          int screen;
          106          Window root, win;
          107          Pixmap pmap;
          108          unsigned long colors[NUMCOLS];
          109 +        unsigned int x, y;
          110 +        unsigned int xoff, yoff, mw, mh;
          111 +        Drawable drawable;
          112 +        GC gc;
          113 +        XRectangle rectangles[LENGTH(rectangles)];
          114  };
          115  
          116  struct xrandr {
          117 @@ -44,8 +57,6 @@ struct xrandr {
          118          int errbase;
          119  };
          120  
          121 -#include "config.h"
          122 -
          123  static void
          124  die(const char *errstr, ...)
          125  {
          126 @@ -124,6 +135,32 @@ gethash(void)
          127          return hash;
          128  }
          129  
          130 +static void
          131 +resizerectangles(struct lock *lock)
          132 +{
          133 +        int i;
          134 +
          135 +        for (i = 0; i < LENGTH(rectangles); i++){
          136 +                lock->rectangles[i].x = (rectangles[i].x * logosize)
          137 +                                + lock->xoff + ((lock->mw) / 2) - (logow / 2 * logosize);
          138 +                lock->rectangles[i].y = (rectangles[i].y * logosize)
          139 +                                + lock->yoff + ((lock->mh) / 2) - (logoh / 2 * logosize);
          140 +                lock->rectangles[i].width = rectangles[i].width * logosize;
          141 +                lock->rectangles[i].height = rectangles[i].height * logosize;
          142 +        }
          143 +}
          144 +
          145 +static void
          146 +drawlogo(Display *dpy, struct lock *lock, int color)
          147 +{
          148 +        XSetForeground(dpy, lock->gc, lock->colors[BACKGROUND]);
          149 +        XFillRectangle(dpy, lock->drawable, lock->gc, 0, 0, lock->x, lock->y);
          150 +        XSetForeground(dpy, lock->gc, lock->colors[color]);
          151 +        XFillRectangles(dpy, lock->drawable, lock->gc, lock->rectangles, LENGTH(rectangles));
          152 +        XCopyArea(dpy, lock->drawable, lock->win, lock->gc, 0, 0, lock->x, lock->y, 0, 0);
          153 +        XSync(dpy, False);
          154 +}
          155 +
          156  static void
          157  readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
          158         const char *hash)
          159 @@ -190,10 +227,7 @@ readpw(Display *dpy, struct xrandr *rr, struct lock **locks, int nscreens,
          160                          color = len ? INPUT : ((failure || failonclear) ? FAILED : INIT);
          161                          if (running && oldc != color) {
          162                                  for (screen = 0; screen < nscreens; screen++) {
          163 -                                        XSetWindowBackground(dpy,
          164 -                                                             locks[screen]->win,
          165 -                                                             locks[screen]->colors[color]);
          166 -                                        XClearWindow(dpy, locks[screen]->win);
          167 +                                        drawlogo(dpy, locks[screen], color);
          168                                  }
          169                                  oldc = color;
          170                          }
          171 @@ -228,6 +262,10 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
          172          XColor color, dummy;
          173          XSetWindowAttributes wa;
          174          Cursor invisible;
          175 +#ifdef XINERAMA
          176 +        XineramaScreenInfo *info;
          177 +        int n;
          178 +#endif
          179  
          180          if (dpy == NULL || screen < 0 || !(lock = malloc(sizeof(struct lock))))
          181                  return NULL;
          182 @@ -241,12 +279,31 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
          183                  lock->colors[i] = color.pixel;
          184          }
          185  
          186 +        lock->x = DisplayWidth(dpy, lock->screen);
          187 +        lock->y = DisplayHeight(dpy, lock->screen);
          188 +#ifdef XINERAMA
          189 +        if ((info = XineramaQueryScreens(dpy, &n))) {
          190 +                lock->xoff = info[0].x_org;
          191 +                lock->yoff = info[0].y_org;
          192 +                lock->mw = info[0].width;
          193 +                lock->mh = info[0].height;
          194 +        } else
          195 +#endif
          196 +        {
          197 +                lock->xoff = lock->yoff = 0;
          198 +                lock->mw = lock->x;
          199 +                lock->mh = lock->y;
          200 +        }
          201 +        lock->drawable = XCreatePixmap(dpy, lock->root,
          202 +            lock->x, lock->y, DefaultDepth(dpy, screen));
          203 +        lock->gc = XCreateGC(dpy, lock->root, 0, NULL);
          204 +        XSetLineAttributes(dpy, lock->gc, 1, LineSolid, CapButt, JoinMiter);
          205 +
          206          /* init */
          207          wa.override_redirect = 1;
          208 -        wa.background_pixel = lock->colors[INIT];
          209 +        wa.background_pixel = lock->colors[BACKGROUND];
          210          lock->win = XCreateWindow(dpy, lock->root, 0, 0,
          211 -                                  DisplayWidth(dpy, lock->screen),
          212 -                                  DisplayHeight(dpy, lock->screen),
          213 +                                  lock->x, lock->y,
          214                                    0, DefaultDepth(dpy, lock->screen),
          215                                    CopyFromParent,
          216                                    DefaultVisual(dpy, lock->screen),
          217 @@ -256,6 +313,8 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
          218                                          &color, &color, 0, 0);
          219          XDefineCursor(dpy, lock->win, invisible);
          220  
          221 +        resizerectangles(lock);
          222 +
          223          /* Try to grab mouse pointer *and* keyboard for 600ms, else fail the lock */
          224          for (i = 0, ptgrab = kbgrab = -1; i < 6; i++) {
          225                  if (ptgrab != GrabSuccess) {
          226 @@ -276,6 +335,7 @@ lockscreen(Display *dpy, struct xrandr *rr, int screen)
          227                                  XRRSelectInput(dpy, lock->win, RRScreenChangeNotifyMask);
          228  
          229                          XSelectInput(dpy, lock->root, SubstructureNotifyMask);
          230 +                        drawlogo(dpy, lock, INIT);
          231                          return lock;
          232                  }
          233  
          234 @@ -391,5 +451,12 @@ main(int argc, char **argv) {
          235          /* everything is now blank. Wait for the correct password */
          236          readpw(dpy, &rr, locks, nscreens, hash);
          237  
          238 +        for (nlocks = 0, s = 0; s < nscreens; s++) {
          239 +                XFreePixmap(dpy, locks[s]->drawable);
          240 +                XFreeGC(dpy, locks[s]->gc);
          241 +        }
          242 +
          243 +        XSync(dpy, 0);
          244 +        XCloseDisplay(dpy);
          245          return 0;
          246  }