aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/term/x.c
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2020-06-06 13:59:25 -0700
committerNicholas Noll <nbnoll@eml.cc>2020-06-06 13:59:25 -0700
commit254258b48b4ba761eae0a1563549e4f324564456 (patch)
tree721a3ca12d87da563a9f9bb5aed85398c629ddfb /sys/cmd/term/x.c
parentb3ea2b804fbd35babac86c83c718a5491542db7c (diff)
opacity
Diffstat (limited to 'sys/cmd/term/x.c')
-rw-r--r--sys/cmd/term/x.c220
1 files changed, 118 insertions, 102 deletions
diff --git a/sys/cmd/term/x.c b/sys/cmd/term/x.c
index 74f3752..460bf7f 100644
--- a/sys/cmd/term/x.c
+++ b/sys/cmd/term/x.c
@@ -1,4 +1,6 @@
/* See LICENSE for license details. */
+#include "term.h"
+
#include <locale.h>
#include <time.h>
@@ -9,32 +11,34 @@
#include <X11/Xft/Xft.h>
#include <X11/XKBlib.h>
-#include "term.h"
-
/* types used in config.h */
-typedef struct {
+typedef struct Shortcut Shortcut;
+typedef struct MouseShortcut MouseShortcut;
+typedef struct Key Key;
+
+struct Shortcut{
uint mod;
KeySym keysym;
void (*func)(Arg *);
Arg arg;
-} Shortcut;
+};
-typedef struct {
+struct MouseShortcut {
uint mod;
uint button;
void (*func)(Arg *);
Arg arg;
uint release;
-} MouseShortcut;
+};
-typedef struct {
+struct Key {
KeySym k;
uint mask;
char *s;
/* three-valued logic variables: 0 indifferent, 1 on, -1 off */
schar appkey; /* application keypad */
schar appcursor; /* application cursor */
-} Key;
+};
/* X modifiers */
#define XK_ANY_MOD UINT_MAX
@@ -70,15 +74,6 @@ typedef XftGlyphFontSpec GlyphFontSpec;
/* Purely graphic info */
typedef struct {
- int tw, th; /* tty width and height */
- int w, h; /* window width and height */
- int ch; /* char height */
- int cw; /* char width */
- int mode; /* window state/mode flags */
- int cursor; /* cursor style */
-} TermWindow;
-
-typedef struct {
Display *dpy;
Colormap cmap;
Window win;
@@ -95,9 +90,10 @@ typedef struct {
Visual *vis;
XSetWindowAttributes attrs;
int scr;
- int isfixed; /* is fixed geometry? */
- int l, t; /* left and top offset */
- int gm; /* geometry mask */
+ int isfixed; /* is fixed geometry? */
+ int depth; /* color depth */
+ int l, t; /* left and top offset */
+ int gm; /* geometry mask */
} XWindow;
typedef struct {
@@ -132,53 +128,53 @@ typedef struct {
} DC;
static inline ushort sixd_to_16bit(int);
-static int xmakeglyphfontspecs(XftGlyphFontSpec *, Letter *, int, int, int);
-static void xdrawglyphfontspecs(XftGlyphFontSpec *, Letter, int, int, int);
-static void xdrawglyph(Letter, int, int);
-static void xclear(int, int, int, int);
-static int xgeommasktogravity(int);
-static int ximopen(Display *);
-static void ximinstantiate(Display *, XPointer, XPointer);
-static void ximdestroy(XIM, XPointer, XPointer);
-static int xicdestroy(XIC, XPointer, XPointer);
-static void xinit(int, int);
-static void cresize(int, int);
-static void xresize(int, int);
-static void xhints(void);
-static int xloadcolor(int, char *, Color *);
-static int xloadfont(Font *, FcPattern *);
-static void xloadfonts(char *, double);
-static void xunloadfont(Font *);
-static void xunloadfonts(void);
-static void xsetenv(void);
-static void xseturgency(int);
-static int evcol(XEvent *);
-static int evrow(XEvent *);
-
-static void expose(XEvent *);
-static void visibility(XEvent *);
-static void unmap(XEvent *);
-static void kpress(XEvent *);
-static void cmessage(XEvent *);
-static void resize(XEvent *);
-static void focus(XEvent *);
-static uint buttonmask(uint);
-static int mouseaction(XEvent *, uint);
-static void brelease(XEvent *);
-static void bpress(XEvent *);
-static void bmotion(XEvent *);
-static void propnotify(XEvent *);
-static void selnotify(XEvent *);
-static void selclear_(XEvent *);
-static void selrequest(XEvent *);
-static void setsel(char *, Time);
-static void mousesel(XEvent *, int);
-static void mousereport(XEvent *);
-static char *kmap(KeySym, uint);
-static int match(uint, uint);
-
-static void run(void);
-static void usage(void);
+static int xmakeglyphfontspecs(XftGlyphFontSpec *, Letter *, int, int, int);
+static void xdrawglyphfontspecs(XftGlyphFontSpec *, Letter, int, int, int);
+static void xdrawglyph(Letter, int, int);
+static void xclear(int, int, int, int);
+static int xgeommasktogravity(int);
+static int ximopen(Display *);
+static void ximinstantiate(Display *, XPointer, XPointer);
+static void ximdestroy(XIM, XPointer, XPointer);
+static int xicdestroy(XIC, XPointer, XPointer);
+static void xinit(int, int);
+static void cresize(int, int);
+static void xresize(int, int);
+static void xhints(void);
+static int xloadcolor(int, char *, Color *);
+static int xloadfont(Font *, FcPattern *);
+static void xloadfonts(char *, double);
+static void xunloadfont(Font *);
+static void xunloadfonts(void);
+static void xsetenv(void);
+static void xseturgency(int);
+static int evcol(XEvent *);
+static int evrow(XEvent *);
+
+static void expose(XEvent *);
+static void visibility(XEvent *);
+static void unmap(XEvent *);
+static void kpress(XEvent *);
+static void cmessage(XEvent *);
+static void resize(XEvent *);
+static void focus(XEvent *);
+static uint buttonmask(uint);
+static int mouseaction(XEvent *, uint);
+static void brelease(XEvent *);
+static void bpress(XEvent *);
+static void bmotion(XEvent *);
+static void propnotify(XEvent *);
+static void selnotify(XEvent *);
+static void selclear_(XEvent *);
+static void selrequest(XEvent *);
+static void setsel(char *, Time);
+static void mousesel(XEvent *, int);
+static void mousereport(XEvent *);
+static char *kmap(KeySym, uint);
+static int match(uint, uint);
+
+static void run(void);
+static void usage(void);
static void (*handler[LASTEvent])(XEvent *) = {
[KeyPress] = kpress,
@@ -192,11 +188,7 @@ static void (*handler[LASTEvent])(XEvent *) = {
[MotionNotify] = bmotion,
[ButtonPress] = bpress,
[ButtonRelease] = brelease,
-/*
- * Uncomment if you want the selection to disappear when you select something
- * different in another window.
- */
-/* [SelectionClear] = selclear_, */
+ [SelectionClear] = selclear_,
[SelectionNotify] = selnotify,
/*
* PropertyNotify is only turned on when there is some INCR transfer happening
@@ -228,12 +220,13 @@ typedef struct {
/* Fontcache is an array now. A new font will be appended to the array. */
static Fontcache *frc = nil;
-static int frclen = 0;
-static int frccap = 0;
+static int frclen = 0;
+static int frccap = 0;
static char *usedfont = nil;
static double usedfontsize = 0;
static double defaultfontsize = 0;
+static char *opt_alpha = nil;
static char *opt_class = nil;
static char **opt_cmd = nil;
static char *opt_embed = nil;
@@ -724,8 +717,8 @@ xresize(int col, int row)
win.th = row * win.ch;
XFreePixmap(xw.dpy, xw.buf);
- xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
- DefaultDepth(xw.dpy, xw.scr));
+ xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h, xw.depth);
+
XftDrawChange(xw.draw, xw.buf);
xclear(0, 0, win.w, win.h);
@@ -781,10 +774,17 @@ xloadcols(void)
for (i = 0; i < dc.collen; i++)
if (!xloadcolor(i, nil, &dc.col[i])) {
if (colorname[i])
- die("could not allocate color '%s'\n", colorname[i]);
+ fatal("could not allocate color '%s'\n", colorname[i]);
else
- die("could not allocate color %d\n", i);
+ fatal("could not allocate color %d\n", i);
}
+
+ if (opt_alpha)
+ alpha = strtof(opt_alpha, nil);
+
+ dc.col[defaultbg].color.alpha = (ushort)(0xffff * alpha);
+ dc.col[defaultbg].pixel &= 0x00ffffff;
+ dc.col[defaultbg].pixel |= (uchar)(0xff*alpha) << 24;
loaded = 1;
}
@@ -952,7 +952,7 @@ xloadfonts(char *fontstr, double fontsize)
pattern = FcNameParse((FcChar8 *)fontstr);
if (!pattern)
- die("can't open font %s\n", fontstr);
+ fatal("can't open font %s\n", fontstr);
if (fontsize > 1) {
FcPatternDel(pattern, FC_PIXEL_SIZE);
@@ -978,7 +978,7 @@ xloadfonts(char *fontstr, double fontsize)
}
if (xloadfont(&dc.font, pattern))
- die("can't open font %s\n", fontstr);
+ fatal("can't open font %s\n", fontstr);
if (usedfontsize < 0) {
FcPatternGetDouble(dc.font.match->pattern,
@@ -995,17 +995,17 @@ xloadfonts(char *fontstr, double fontsize)
FcPatternDel(pattern, FC_SLANT);
FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
if (xloadfont(&dc.ifont, pattern))
- die("can't open font %s\n", fontstr);
+ fatal("can't open font %s\n", fontstr);
FcPatternDel(pattern, FC_WEIGHT);
FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
if (xloadfont(&dc.ibfont, pattern))
- die("can't open font %s\n", fontstr);
+ fatal("can't open font %s\n", fontstr);
FcPatternDel(pattern, FC_SLANT);
FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
if (xloadfont(&dc.bfont, pattern))
- die("can't open font %s\n", fontstr);
+ fatal("can't open font %s\n", fontstr);
FcPatternDestroy(pattern);
}
@@ -1090,25 +1090,37 @@ void
xinit(int cols, int rows)
{
XGCValues gcvalues;
- Cursor cursor;
- Window parent;
+ Cursor cursor;
+ Window parent;
+ XColor xmousefg, xmousebg;
+ XWindowAttributes attr;
+ XVisualInfo vis;
pid_t thispid = getpid();
- XColor xmousefg, xmousebg;
if (!(xw.dpy = XOpenDisplay(nil)))
- die("can't open display\n");
+ fatal("can't open display\n");
+
+ if (!(opt_embed && (parent == strtol(opt_embed, nil, 0)))) {
+ parent = XRootWindow(xw.dpy, xw.scr);
+ xw.depth = 32;
+ } else {
+ XGetWindowAttributes(xw.dpy, parent, &attr);
+ xw.depth = attr.depth;
+ }
+
+ XMatchVisualInfo(xw.dpy, xw.scr, xw.depth, TrueColor, &vis);
+ xw.vis = vis.visual;
xw.scr = XDefaultScreen(xw.dpy);
- xw.vis = XDefaultVisual(xw.dpy, xw.scr);
/* font */
if (!FcInit())
- die("could not init fontconfig.\n");
+ fatal("could not init fontconfig.\n");
usedfont = (opt_font == nil)? font : opt_font;
xloadfonts(usedfont, 0);
/* colors */
- xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
+ xw.cmap = XCreateColormap(xw.dpy, parent, xw.vis, None);
xloadcols();
/* adjust fixed window geometry */
@@ -1128,19 +1140,17 @@ xinit(int cols, int rows)
| ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
xw.attrs.colormap = xw.cmap;
- if (!(opt_embed && (parent = strtol(opt_embed, nil, 0))))
- parent = XRootWindow(xw.dpy, xw.scr);
xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
- win.w, win.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
+ win.w, win.h, 0, xw.depth, InputOutput,
xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
| CWEventMask | CWColormap, &xw.attrs);
memset(&gcvalues, 0, sizeof(gcvalues));
gcvalues.graphics_exposures = False;
- dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
- &gcvalues);
- xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
- DefaultDepth(xw.dpy, xw.scr));
+
+ xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h, xw.depth);
+ dc.gc = XCreateGC(xw.dpy, xw.buf, GCGraphicsExposures, &gcvalues);
+
XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
@@ -1305,7 +1315,7 @@ xmakeglyphfontspecs(XftGlyphFontSpec *specs, Letter *glyphs, int len, int x, int
frc[frclen].font = XftFontOpenPattern(xw.dpy,
fontpattern);
if (!frc[frclen].font)
- die("XftFontOpenPattern failed seeking fallback font: %s\n",
+ fatal("XftFontOpenPattern failed seeking fallback font: %s\n",
strerror(errno));
frc[frclen].flags = frcflags;
frc[frclen].unicodep = r;
@@ -1896,7 +1906,7 @@ run(void)
if (pselect(MAX(xfd, ttyfd)+1, &rfd, nil, nil, tv, nil) < 0) {
if (errno == EINTR)
continue;
- die("select failed: %s\n", strerror(errno));
+ fatal("select failed: %s\n", strerror(errno));
}
clock_gettime(CLOCK_MONOTONIC, &now);
@@ -1958,7 +1968,7 @@ run(void)
void
usage(void)
{
- die("usage: %s [-aiv] [-c class] [-f font] [-g geometry]"
+ fatal("usage: %s [-aiv] [-c class] [-f font] [-A alpha] [-g geometry]"
" [-n name] [-o file]\n"
" [-T title] [-t title] [-w windowid]"
" [[-e] command [args ...]]\n"
@@ -1979,6 +1989,9 @@ main(int argc, char *argv[])
case 'a':
allowaltscreen = 0;
break;
+ case 'A':
+ opt_alpha = EARGF(usage());
+ break;
case 'c':
opt_class = EARGF(usage());
break;
@@ -1997,7 +2010,7 @@ main(int argc, char *argv[])
xw.isfixed = 1;
break;
case 'o':
- opt_io = EARGF(usage());
+ opt_io = EARGF(usage());
break;
case 'l':
opt_line = EARGF(usage());
@@ -2013,7 +2026,7 @@ main(int argc, char *argv[])
opt_embed = EARGF(usage());
break;
case 'v':
- die("%s " VERSION "\n", argv0);
+ fatal("%s " VERSION "\n", argv0);
break;
default:
usage();
@@ -2028,12 +2041,15 @@ run:
setlocale(LC_CTYPE, "");
XSetLocaleModifiers("");
+
cols = MAX(cols, 1);
rows = MAX(rows, 1);
+
tnew(cols, rows);
xinit(cols, rows);
xsetenv();
selinit();
+
run();
return 0;