st-focus-20200731-43a395a.diff - sites - public wiki contents of suckless.org
 (HTM) git clone git://git.suckless.org/sites
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       st-focus-20200731-43a395a.diff (7493B)
       ---
            1 From 10fa2b6aebceb3a12219ef25376297b063638bd9 Mon Sep 17 00:00:00 2001
            2 From: Julius Huelsmann <juliusHuelsmann@gmail.com>
            3 Date: Fri, 31 Jul 2020 10:13:35 +0200
            4 Subject: [PATCH] patch: focus
            5 
            6 ---
            7  config.def.h |  5 ++++
            8  config.mk    |  2 +-
            9  st.c         |  1 -
           10  st.h         |  2 ++
           11  x.c          | 72 +++++++++++++++++++++++++++++++++++++++-------------
           12  5 files changed, 62 insertions(+), 20 deletions(-)
           13 
           14 diff --git a/config.def.h b/config.def.h
           15 index 0895a1f..577d1f1 100644
           16 --- a/config.def.h
           17 +++ b/config.def.h
           18 @@ -84,6 +84,9 @@ char *termname = "st-256color";
           19   */
           20  unsigned int tabspaces = 8;
           21  
           22 +/* bg opacity */
           23 +float alpha = 0.8, alphaUnfocused = 0.6;
           24 +
           25  /* Terminal colors (16 first used in escape sequence) */
           26  static const char *colorname[] = {
           27          /* 8 normal colors */
           28 @@ -111,6 +114,7 @@ static const char *colorname[] = {
           29          /* more colors can be added after 255 to use with DefaultXX */
           30          "#cccccc",
           31          "#555555",
           32 +        "black",
           33  };
           34  
           35  
           36 @@ -122,6 +126,7 @@ unsigned int defaultfg = 7;
           37  unsigned int defaultbg = 0;
           38  static unsigned int defaultcs = 256;
           39  static unsigned int defaultrcs = 257;
           40 +unsigned int bg = 17, bgUnfocused = 16;
           41  
           42  /*
           43   * Default shape of cursor
           44 diff --git a/config.mk b/config.mk
           45 index beafc35..ddc65ae 100644
           46 --- a/config.mk
           47 +++ b/config.mk
           48 @@ -16,7 +16,7 @@ PKG_CONFIG = pkg-config
           49  INCS = -I$(X11INC) \
           50         `$(PKG_CONFIG) --cflags fontconfig` \
           51         `$(PKG_CONFIG) --cflags freetype2`
           52 -LIBS = -L$(X11LIB) -lm -lrt -lX11 -lutil -lXft \
           53 +LIBS = -L$(X11LIB) -lm -lrt -lX11 -lutil -lXft -lXrender\
           54         `$(PKG_CONFIG) --libs fontconfig` \
           55         `$(PKG_CONFIG) --libs freetype2`
           56  
           57 diff --git a/st.c b/st.c
           58 index 0ce6ac2..c7f40c8 100644
           59 --- a/st.c
           60 +++ b/st.c
           61 @@ -194,7 +194,6 @@ static void tsetscroll(int, int);
           62  static void tswapscreen(void);
           63  static void tsetmode(int, int, int *, int);
           64  static int twrite(const char *, int, int);
           65 -static void tfulldirt(void);
           66  static void tcontrolcode(uchar );
           67  static void tdectest(char );
           68  static void tdefutf8(char);
           69 diff --git a/st.h b/st.h
           70 index d978458..44cb3fd 100644
           71 --- a/st.h
           72 +++ b/st.h
           73 @@ -79,6 +79,7 @@ typedef union {
           74  
           75  void die(const char *, ...);
           76  void redraw(void);
           77 +void tfulldirt(void);
           78  void draw(void);
           79  
           80  void printscreen(const Arg *);
           81 @@ -122,3 +123,4 @@ extern char *termname;
           82  extern unsigned int tabspaces;
           83  extern unsigned int defaultfg;
           84  extern unsigned int defaultbg;
           85 +extern float alpha, alphaUnfocused;
           86 diff --git a/x.c b/x.c
           87 index e5f1737..a2e820f 100644
           88 --- a/x.c
           89 +++ b/x.c
           90 @@ -105,6 +105,7 @@ typedef struct {
           91          XSetWindowAttributes attrs;
           92          int scr;
           93          int isfixed; /* is fixed geometry? */
           94 +        int depth; /* bit depth */
           95          int l, t; /* left and top offset */
           96          int gm; /* geometry mask */
           97  } XWindow;
           98 @@ -243,6 +244,7 @@ static char *usedfont = NULL;
           99  static double usedfontsize = 0;
          100  static double defaultfontsize = 0;
          101  
          102 +static char *opt_alpha = NULL;
          103  static char *opt_class = NULL;
          104  static char **opt_cmd  = NULL;
          105  static char *opt_embed = NULL;
          106 @@ -252,6 +254,8 @@ static char *opt_line  = NULL;
          107  static char *opt_name  = NULL;
          108  static char *opt_title = NULL;
          109  
          110 +static int focused = 0;
          111 +
          112  static int oldbutton = 3; /* button event on startup: 3 = release */
          113  
          114  void
          115 @@ -734,7 +738,7 @@ xresize(int col, int row)
          116  
          117          XFreePixmap(xw.dpy, xw.buf);
          118          xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
          119 -                        DefaultDepth(xw.dpy, xw.scr));
          120 +                        xw.depth);
          121          XftDrawChange(xw.draw, xw.buf);
          122          xclear(0, 0, win.w, win.h);
          123  
          124 @@ -772,28 +776,38 @@ xloadcolor(int i, const char *name, Color *ncolor)
          125          return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
          126  }
          127  
          128 +void
          129 +xloadalpha(void)
          130 +{
          131 +        float const usedAlpha = focused ? alpha : alphaUnfocused;
          132 +        if (opt_alpha) alpha = strtof(opt_alpha, NULL);
          133 +        dc.col[defaultbg].color.alpha = (unsigned short)(0xffff * usedAlpha);
          134 +        dc.col[defaultbg].pixel &= 0x00FFFFFF;
          135 +        dc.col[defaultbg].pixel |= (unsigned char)(0xff * usedAlpha) << 24;
          136 +}
          137 +
          138  void
          139  xloadcols(void)
          140  {
          141 -        int i;
          142          static int loaded;
          143          Color *cp;
          144  
          145 -        if (loaded) {
          146 -                for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp)
          147 -                        XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
          148 -        } else {
          149 -                dc.collen = MAX(LEN(colorname), 256);
          150 -                dc.col = xmalloc(dc.collen * sizeof(Color));
          151 +        if (!loaded) {
          152 +                dc.collen = 1 + (defaultbg = MAX(LEN(colorname), 256));
          153 +                dc.col = xmalloc((dc.collen) * sizeof(Color));
          154          }
          155  
          156 -        for (i = 0; i < dc.collen; i++)
          157 +        for (int i = 0; i+1 < dc.collen; ++i)
          158                  if (!xloadcolor(i, NULL, &dc.col[i])) {
          159                          if (colorname[i])
          160                                  die("could not allocate color '%s'\n", colorname[i]);
          161                          else
          162                                  die("could not allocate color %d\n", i);
          163                  }
          164 +        if (dc.collen) // cannot die, as the color is already loaded.
          165 +                xloadcolor(focused ?bg :bgUnfocused, NULL, &dc.col[defaultbg]);
          166 +
          167 +        xloadalpha();
          168          loaded = 1;
          169  }
          170  
          171 @@ -1103,11 +1117,23 @@ xinit(int cols, int rows)
          172          Window parent;
          173          pid_t thispid = getpid();
          174          XColor xmousefg, xmousebg;
          175 +        XWindowAttributes attr;
          176 +        XVisualInfo vis;
          177  
          178          if (!(xw.dpy = XOpenDisplay(NULL)))
          179                  die("can't open display\n");
          180          xw.scr = XDefaultScreen(xw.dpy);
          181 -        xw.vis = XDefaultVisual(xw.dpy, xw.scr);
          182 +
          183 +        if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0)))) {
          184 +                parent = XRootWindow(xw.dpy, xw.scr);
          185 +                xw.depth = 32;
          186 +        } else {
          187 +                XGetWindowAttributes(xw.dpy, parent, &attr);
          188 +                xw.depth = attr.depth;
          189 +        }
          190 +
          191 +        XMatchVisualInfo(xw.dpy, xw.scr, xw.depth, TrueColor, &vis);
          192 +        xw.vis = vis.visual;
          193  
          194          /* font */
          195          if (!FcInit())
          196 @@ -1117,7 +1143,7 @@ xinit(int cols, int rows)
          197          xloadfonts(usedfont, 0);
          198  
          199          /* colors */
          200 -        xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
          201 +        xw.cmap = XCreateColormap(xw.dpy, parent, xw.vis, None);
          202          xloadcols();
          203  
          204          /* adjust fixed window geometry */
          205 @@ -1137,19 +1163,15 @@ xinit(int cols, int rows)
          206                  | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
          207          xw.attrs.colormap = xw.cmap;
          208  
          209 -        if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
          210 -                parent = XRootWindow(xw.dpy, xw.scr);
          211          xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
          212 -                        win.w, win.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
          213 +                        win.w, win.h, 0, xw.depth, InputOutput,
          214                          xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
          215                          | CWEventMask | CWColormap, &xw.attrs);
          216  
          217          memset(&gcvalues, 0, sizeof(gcvalues));
          218          gcvalues.graphics_exposures = False;
          219 -        dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
          220 -                        &gcvalues);
          221 -        xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
          222 -                        DefaultDepth(xw.dpy, xw.scr));
          223 +        xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h, xw.depth);
          224 +        dc.gc = XCreateGC(xw.dpy, xw.buf, GCGraphicsExposures, &gcvalues);
          225          XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
          226          XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
          227  
          228 @@ -1730,12 +1752,22 @@ focus(XEvent *ev)
          229                  xseturgency(0);
          230                  if (IS_SET(MODE_FOCUS))
          231                          ttywrite("\033[I", 3, 0);
          232 +                if (!focused) {
          233 +                        focused = 1;
          234 +                        xloadcols();
          235 +                        tfulldirt();
          236 +                }
          237          } else {
          238                  if (xw.ime.xic)
          239                          XUnsetICFocus(xw.ime.xic);
          240                  win.mode &= ~MODE_FOCUSED;
          241                  if (IS_SET(MODE_FOCUS))
          242                          ttywrite("\033[O", 3, 0);
          243 +                if (focused) {
          244 +                        focused = 0;
          245 +                        xloadcols();
          246 +                        tfulldirt();
          247 +                }
          248          }
          249  }
          250  
          251 @@ -1994,6 +2026,9 @@ main(int argc, char *argv[])
          252          case 'a':
          253                  allowaltscreen = 0;
          254                  break;
          255 +        case 'A':
          256 +                opt_alpha = EARGF(usage());
          257 +                break;
          258          case 'c':
          259                  opt_class = EARGF(usage());
          260                  break;
          261 @@ -2045,6 +2080,7 @@ run:
          262          XSetLocaleModifiers("");
          263          cols = MAX(cols, 1);
          264          rows = MAX(rows, 1);
          265 +        defaultbg = MAX(LEN(colorname), 256);
          266          tnew(cols, rows);
          267          xinit(cols, rows);
          268          xsetenv();
          269 -- 
          270 2.28.0
          271