Added disappearing cursor

dev
Jef Roosens 2020-08-22 17:01:04 +02:00
parent d391a7831a
commit 9537484ded
3 changed files with 138 additions and 6 deletions

View File

@ -5,9 +5,9 @@
* *
* font: see http://freedesktop.org/software/fontconfig/fontconfig-user.html * font: see http://freedesktop.org/software/fontconfig/fontconfig-user.html
*/ */
static char *font = "Fira Code:size=11"; static char *font = "Fira Code:size=11:antialias=true;autohint=true";
static char *font2[] = { static char *font2[] = {
"Noto Color Emoji:size=10" "Noto Color Emoji:size=10:antialias=true;autohint=true"
}; };
static int borderpx = 2; static int borderpx = 2;
float alpha = 0.8; float alpha = 0.8;

39
x.c
View File

@ -105,6 +105,11 @@ typedef struct {
Draw draw; Draw draw;
Visual *vis; Visual *vis;
XSetWindowAttributes attrs; XSetWindowAttributes attrs;
/* Here, we use the term *pointer* to differentiate the cursor
* one sees when hovering the mouse over the terminal from, e.g.,
* a green rectangle where text would be entered. */
Cursor vpointer, bpointer; /* visible and hidden pointers */
int pointerisvisible;
int scr; int scr;
int isfixed; /* is fixed geometry? */ int isfixed; /* is fixed geometry? */
int depth; /* bit depth */ int depth; /* bit depth */
@ -692,6 +697,13 @@ xsetsel(char *str)
void void
brelease(XEvent *e) brelease(XEvent *e)
{ {
if (!xw.pointerisvisible) {
XDefineCursor(xw.dpy, xw.win, xw.vpointer);
xw.pointerisvisible = 1;
if (!IS_SET(MODE_MOUSEMANY))
xsetpointermotion(0);
}
if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) { if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
mousereport(e); mousereport(e);
return; return;
@ -706,6 +718,13 @@ brelease(XEvent *e)
void void
bmotion(XEvent *e) bmotion(XEvent *e)
{ {
if (!xw.pointerisvisible) {
XDefineCursor(xw.dpy, xw.win, xw.vpointer);
xw.pointerisvisible = 1;
if (!IS_SET(MODE_MOUSEMANY))
xsetpointermotion(0);
}
if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) { if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
mousereport(e); mousereport(e);
return; return;
@ -1215,10 +1234,10 @@ void
xinit(int cols, int rows) xinit(int cols, int rows)
{ {
XGCValues gcvalues; XGCValues gcvalues;
Cursor cursor;
Window parent; Window parent;
pid_t thispid = getpid(); pid_t thispid = getpid();
XColor xmousefg, xmousebg; XColor xmousefg, xmousebg;
Pixmap blankpm;
XWindowAttributes attr; XWindowAttributes attr;
XVisualInfo vis; XVisualInfo vis;
@ -1293,8 +1312,9 @@ xinit(int cols, int rows)
} }
/* white cursor, black outline */ /* white cursor, black outline */
cursor = XCreateFontCursor(xw.dpy, mouseshape); xw.pointerisvisible = 1;
XDefineCursor(xw.dpy, xw.win, cursor); xw.vpointer = XCreateFontCursor(xw.dpy, mouseshape);
XDefineCursor(xw.dpy, xw.win, xw.vpointer);
if (XParseColor(xw.dpy, xw.cmap, colorname[mousefg], &xmousefg) == 0) { if (XParseColor(xw.dpy, xw.cmap, colorname[mousefg], &xmousefg) == 0) {
xmousefg.red = 0xffff; xmousefg.red = 0xffff;
@ -1308,7 +1328,10 @@ xinit(int cols, int rows)
xmousebg.blue = 0x0000; xmousebg.blue = 0x0000;
} }
XRecolorCursor(xw.dpy, cursor, &xmousefg, &xmousebg); XRecolorCursor(xw.dpy, xw.vpointer, &xmousefg, &xmousebg);
blankpm = XCreateBitmapFromData(xw.dpy, xw.win, &(char){0}, 1, 1);
xw.bpointer = XCreatePixmapCursor(xw.dpy, blankpm, blankpm,
&xmousefg, &xmousebg, 0, 0);
xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False); xw.xembed = XInternAtom(xw.dpy, "_XEMBED", False);
xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False); xw.wmdeletewin = XInternAtom(xw.dpy, "WM_DELETE_WINDOW", False);
@ -1818,6 +1841,8 @@ unmap(XEvent *ev)
void void
xsetpointermotion(int set) xsetpointermotion(int set)
{ {
if (!set && !xw.pointerisvisible)
return;
MODBIT(xw.attrs.event_mask, set, PointerMotionMask); MODBIT(xw.attrs.event_mask, set, PointerMotionMask);
XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs); XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs);
} }
@ -1937,6 +1962,12 @@ kpress(XEvent *ev)
Status status; Status status;
Shortcut *bp; Shortcut *bp;
if (xw.pointerisvisible) {
XDefineCursor(xw.dpy, xw.win, xw.bpointer);
xsetpointermotion(1);
xw.pointerisvisible = 0;
}
if (IS_SET(MODE_KBDLOCK)) if (IS_SET(MODE_KBDLOCK))
return; return;

101
x.c.orig
View File

@ -160,6 +160,8 @@ static void xhints(void);
static int xloadcolor(int, const char *, Color *); static int xloadcolor(int, const char *, Color *);
static int xloadfont(Font *, FcPattern *); static int xloadfont(Font *, FcPattern *);
static void xloadfonts(char *, double); static void xloadfonts(char *, double);
static int xloadsparefont(FcPattern *, int);
static void xloadsparefonts(void);
static void xunloadfont(Font *); static void xunloadfont(Font *);
static void xunloadfonts(void); static void xunloadfonts(void);
static void xsetenv(void); static void xsetenv(void);
@ -310,6 +312,7 @@ zoomabs(const Arg *arg)
{ {
xunloadfonts(); xunloadfonts();
xloadfonts(usedfont, arg->f); xloadfonts(usedfont, arg->f);
xloadsparefonts();
cresize(0, 0); cresize(0, 0);
redraw(); redraw();
xhints(); xhints();
@ -1034,6 +1037,101 @@ xloadfonts(char *fontstr, double fontsize)
FcPatternDestroy(pattern); FcPatternDestroy(pattern);
} }
int
xloadsparefont(FcPattern *pattern, int flags)
{
FcPattern *match;
FcResult result;
match = FcFontMatch(NULL, pattern, &result);
if (!match) {
return 1;
}
if (!(frc[frclen].font = XftFontOpenPattern(xw.dpy, match))) {
FcPatternDestroy(match);
return 1;
}
frc[frclen].flags = flags;
/* Believe U+0000 glyph will present in each default font */
frc[frclen].unicodep = 0;
frclen++;
return 0;
}
void
xloadsparefonts(void)
{
FcPattern *pattern;
double sizeshift, fontval;
int fc;
char **fp;
if (frclen != 0)
die("can't embed spare fonts. cache isn't empty");
/* Calculate count of spare fonts */
fc = sizeof(font2) / sizeof(*font2);
if (fc == 0)
return;
/* Allocate memory for cache entries. */
if (frccap < 4 * fc) {
frccap += 4 * fc - frccap;
frc = xrealloc(frc, frccap * sizeof(Fontcache));
}
for (fp = font2; fp - font2 < fc; ++fp) {
if (**fp == '-')
pattern = XftXlfdParse(*fp, False, False);
else
pattern = FcNameParse((FcChar8 *)*fp);
if (!pattern)
die("can't open spare font %s\n", *fp);
if (defaultfontsize > 0) {
sizeshift = usedfontsize - defaultfontsize;
if (sizeshift != 0 &&
FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &fontval) ==
FcResultMatch) {
fontval += sizeshift;
FcPatternDel(pattern, FC_PIXEL_SIZE);
FcPatternDel(pattern, FC_SIZE);
FcPatternAddDouble(pattern, FC_PIXEL_SIZE, fontval);
}
}
FcPatternAddBool(pattern, FC_SCALABLE, 1);
FcConfigSubstitute(NULL, pattern, FcMatchPattern);
XftDefaultSubstitute(xw.dpy, xw.scr, pattern);
if (xloadsparefont(pattern, FRC_NORMAL))
die("can't open spare font %s\n", *fp);
FcPatternDel(pattern, FC_SLANT);
FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ITALIC);
if (xloadsparefont(pattern, FRC_ITALIC))
die("can't open spare font %s\n", *fp);
FcPatternDel(pattern, FC_WEIGHT);
FcPatternAddInteger(pattern, FC_WEIGHT, FC_WEIGHT_BOLD);
if (xloadsparefont(pattern, FRC_ITALICBOLD))
die("can't open spare font %s\n", *fp);
FcPatternDel(pattern, FC_SLANT);
FcPatternAddInteger(pattern, FC_SLANT, FC_SLANT_ROMAN);
if (xloadsparefont(pattern, FRC_BOLD))
die("can't open spare font %s\n", *fp);
FcPatternDestroy(pattern);
}
}
void void
xunloadfont(Font *f) xunloadfont(Font *f)
{ {
@ -1146,6 +1244,9 @@ xinit(int cols, int rows)
usedfont = (opt_font == NULL)? font : opt_font; usedfont = (opt_font == NULL)? font : opt_font;
xloadfonts(usedfont, 0); xloadfonts(usedfont, 0);
/* spare fonts */
xloadsparefonts();
/* colors */ /* colors */
xw.cmap = XCreateColormap(xw.dpy, parent, xw.vis, None); xw.cmap = XCreateColormap(xw.dpy, parent, xw.vis, None);
xloadcols(); xloadcols();