2017-01-20 09:06:39 +01:00
|
|
|
/* See LICENSE for license details. */
|
|
|
|
#include <errno.h>
|
2018-03-09 15:36:38 +01:00
|
|
|
#include <math.h>
|
2018-02-24 23:45:42 +01:00
|
|
|
#include <limits.h>
|
2017-01-20 09:06:39 +01:00
|
|
|
#include <locale.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <X11/Xatom.h>
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/cursorfont.h>
|
|
|
|
#include <X11/keysym.h>
|
|
|
|
#include <X11/Xft/Xft.h>
|
|
|
|
#include <X11/XKBlib.h>
|
|
|
|
|
2017-10-10 18:11:27 +02:00
|
|
|
static char *argv0;
|
2017-01-20 09:06:39 +01:00
|
|
|
#include "arg.h"
|
|
|
|
#include "st.h"
|
2017-10-10 18:30:36 +02:00
|
|
|
#include "win.h"
|
2017-01-20 09:06:39 +01:00
|
|
|
|
2017-10-17 23:46:26 +02:00
|
|
|
/* types used in config.h */
|
|
|
|
typedef struct {
|
|
|
|
uint mod;
|
|
|
|
KeySym keysym;
|
|
|
|
void (*func)(const Arg *);
|
|
|
|
const Arg arg;
|
|
|
|
} Shortcut;
|
|
|
|
|
|
|
|
typedef struct {
|
2019-10-10 22:02:26 +02:00
|
|
|
uint mod;
|
|
|
|
uint button;
|
|
|
|
void (*func)(const Arg *);
|
|
|
|
const Arg arg;
|
2017-10-17 23:46:26 +02:00
|
|
|
} MouseShortcut;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
KeySym k;
|
|
|
|
uint mask;
|
|
|
|
char *s;
|
2018-02-22 08:05:12 +01:00
|
|
|
/* three-valued logic variables: 0 indifferent, 1 on, -1 off */
|
2017-10-17 23:46:26 +02:00
|
|
|
signed char appkey; /* application keypad */
|
|
|
|
signed char appcursor; /* application cursor */
|
|
|
|
} Key;
|
|
|
|
|
2017-11-07 01:30:45 +01:00
|
|
|
/* X modifiers */
|
|
|
|
#define XK_ANY_MOD UINT_MAX
|
|
|
|
#define XK_NO_MOD 0
|
|
|
|
#define XK_SWITCH_MOD (1<<13)
|
|
|
|
|
2017-10-17 22:21:04 +02:00
|
|
|
/* function definitions used in config.h */
|
|
|
|
static void clipcopy(const Arg *);
|
|
|
|
static void clippaste(const Arg *);
|
2018-02-23 21:16:52 +01:00
|
|
|
static void numlock(const Arg *);
|
2017-10-17 22:21:04 +02:00
|
|
|
static void selpaste(const Arg *);
|
|
|
|
static void zoom(const Arg *);
|
|
|
|
static void zoomabs(const Arg *);
|
|
|
|
static void zoomreset(const Arg *);
|
2019-10-10 22:02:26 +02:00
|
|
|
static void ttysend(const Arg *);
|
2017-10-17 22:21:04 +02:00
|
|
|
|
|
|
|
/* config.h for applying patches and the configuration. */
|
|
|
|
#include "config.h"
|
|
|
|
|
2017-01-20 09:06:39 +01:00
|
|
|
/* XEMBED messages */
|
|
|
|
#define XEMBED_FOCUS_IN 4
|
|
|
|
#define XEMBED_FOCUS_OUT 5
|
|
|
|
|
|
|
|
/* macros */
|
2018-02-23 21:16:52 +01:00
|
|
|
#define IS_SET(flag) ((win.mode & (flag)) != 0)
|
2017-01-20 09:06:39 +01:00
|
|
|
#define TRUERED(x) (((x) & 0xff0000) >> 8)
|
|
|
|
#define TRUEGREEN(x) (((x) & 0xff00))
|
|
|
|
#define TRUEBLUE(x) (((x) & 0xff) << 8)
|
|
|
|
|
|
|
|
typedef XftDraw *Draw;
|
|
|
|
typedef XftColor Color;
|
2017-10-10 19:17:25 +02:00
|
|
|
typedef XftGlyphFontSpec GlyphFontSpec;
|
2017-01-20 09:06:39 +01:00
|
|
|
|
|
|
|
/* Purely graphic info */
|
2018-02-24 23:32:20 +01:00
|
|
|
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;
|
|
|
|
|
2017-01-20 09:06:39 +01:00
|
|
|
typedef struct {
|
|
|
|
Display *dpy;
|
|
|
|
Colormap cmap;
|
|
|
|
Window win;
|
|
|
|
Drawable buf;
|
2017-10-10 19:17:25 +02:00
|
|
|
GlyphFontSpec *specbuf; /* font spec buffer used for rendering */
|
2017-01-20 09:06:39 +01:00
|
|
|
Atom xembed, wmdeletewin, netwmname, netwmpid;
|
|
|
|
XIM xim;
|
|
|
|
XIC xic;
|
|
|
|
Draw draw;
|
|
|
|
Visual *vis;
|
|
|
|
XSetWindowAttributes attrs;
|
|
|
|
int scr;
|
|
|
|
int isfixed; /* is fixed geometry? */
|
|
|
|
int l, t; /* left and top offset */
|
|
|
|
int gm; /* geometry mask */
|
|
|
|
} XWindow;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
Atom xtarget;
|
2018-02-22 05:56:02 +01:00
|
|
|
char *primary, *clipboard;
|
|
|
|
struct timespec tclick1;
|
|
|
|
struct timespec tclick2;
|
2017-01-20 09:06:39 +01:00
|
|
|
} XSelection;
|
|
|
|
|
|
|
|
/* Font structure */
|
2017-10-10 19:17:25 +02:00
|
|
|
#define Font Font_
|
2017-01-20 09:06:39 +01:00
|
|
|
typedef struct {
|
|
|
|
int height;
|
|
|
|
int width;
|
|
|
|
int ascent;
|
|
|
|
int descent;
|
|
|
|
int badslant;
|
|
|
|
int badweight;
|
|
|
|
short lbearing;
|
|
|
|
short rbearing;
|
|
|
|
XftFont *match;
|
|
|
|
FcFontSet *set;
|
|
|
|
FcPattern *pattern;
|
|
|
|
} Font;
|
|
|
|
|
|
|
|
/* Drawing Context */
|
|
|
|
typedef struct {
|
|
|
|
Color *col;
|
|
|
|
size_t collen;
|
|
|
|
Font font, bfont, ifont, ibfont;
|
|
|
|
GC gc;
|
|
|
|
} DC;
|
|
|
|
|
|
|
|
static inline ushort sixd_to_16bit(int);
|
|
|
|
static int xmakeglyphfontspecs(XftGlyphFontSpec *, const Glyph *, int, int, int);
|
|
|
|
static void xdrawglyphfontspecs(const XftGlyphFontSpec *, Glyph, int, int, int);
|
|
|
|
static void xdrawglyph(Glyph, int, int);
|
|
|
|
static void xclear(int, int, int, int);
|
|
|
|
static int xgeommasktogravity(int);
|
2019-02-12 18:41:41 +01:00
|
|
|
static void ximopen(Display *);
|
|
|
|
static void ximinstantiate(Display *, XPointer, XPointer);
|
|
|
|
static void ximdestroy(XIM, XPointer, XPointer);
|
2018-02-24 22:32:48 +01:00
|
|
|
static void xinit(int, int);
|
2017-10-10 19:46:53 +02:00
|
|
|
static void cresize(int, int);
|
|
|
|
static void xresize(int, int);
|
2018-02-24 23:32:20 +01:00
|
|
|
static void xhints(void);
|
|
|
|
static int xloadcolor(int, const char *, Color *);
|
2017-01-20 09:06:39 +01:00
|
|
|
static int xloadfont(Font *, FcPattern *);
|
2017-10-10 18:30:36 +02:00
|
|
|
static void xloadfonts(char *, double);
|
2017-01-20 09:06:39 +01:00
|
|
|
static void xunloadfont(Font *);
|
2017-10-10 18:30:36 +02:00
|
|
|
static void xunloadfonts(void);
|
2017-10-10 17:30:23 +02:00
|
|
|
static void xsetenv(void);
|
2017-10-10 19:01:18 +02:00
|
|
|
static void xseturgency(int);
|
2018-02-25 00:09:13 +01:00
|
|
|
static int evcol(XEvent *);
|
|
|
|
static int evrow(XEvent *);
|
2017-01-20 09:06:39 +01:00
|
|
|
|
|
|
|
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 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 *);
|
2017-11-07 01:25:58 +01:00
|
|
|
static void setsel(char *, Time);
|
2018-02-22 06:54:29 +01:00
|
|
|
static void mousesel(XEvent *, int);
|
2017-01-20 09:06:39 +01:00
|
|
|
static void mousereport(XEvent *);
|
2017-10-11 15:47:14 +02:00
|
|
|
static char *kmap(KeySym, uint);
|
|
|
|
static int match(uint, uint);
|
2017-01-20 09:06:39 +01:00
|
|
|
|
2017-10-10 18:11:27 +02:00
|
|
|
static void run(void);
|
|
|
|
static void usage(void);
|
|
|
|
|
2017-01-20 09:06:39 +01:00
|
|
|
static void (*handler[LASTEvent])(XEvent *) = {
|
|
|
|
[KeyPress] = kpress,
|
|
|
|
[ClientMessage] = cmessage,
|
|
|
|
[ConfigureNotify] = resize,
|
|
|
|
[VisibilityNotify] = visibility,
|
|
|
|
[UnmapNotify] = unmap,
|
|
|
|
[Expose] = expose,
|
|
|
|
[FocusIn] = focus,
|
|
|
|
[FocusOut] = focus,
|
|
|
|
[MotionNotify] = bmotion,
|
|
|
|
[ButtonPress] = bpress,
|
|
|
|
[ButtonRelease] = brelease,
|
|
|
|
/*
|
|
|
|
* Uncomment if you want the selection to disappear when you select something
|
|
|
|
* different in another window.
|
|
|
|
*/
|
|
|
|
/* [SelectionClear] = selclear_, */
|
|
|
|
[SelectionNotify] = selnotify,
|
|
|
|
/*
|
|
|
|
* PropertyNotify is only turned on when there is some INCR transfer happening
|
|
|
|
* for the selection retrieval.
|
|
|
|
*/
|
|
|
|
[PropertyNotify] = propnotify,
|
|
|
|
[SelectionRequest] = selrequest,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Globals */
|
|
|
|
static DC dc;
|
|
|
|
static XWindow xw;
|
|
|
|
static XSelection xsel;
|
2017-11-07 00:57:45 +01:00
|
|
|
static TermWindow win;
|
2017-01-20 09:06:39 +01:00
|
|
|
|
|
|
|
/* Font Ring Cache */
|
|
|
|
enum {
|
|
|
|
FRC_NORMAL,
|
|
|
|
FRC_ITALIC,
|
|
|
|
FRC_BOLD,
|
|
|
|
FRC_ITALICBOLD
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
XftFont *font;
|
|
|
|
int flags;
|
|
|
|
Rune unicodep;
|
|
|
|
} Fontcache;
|
|
|
|
|
|
|
|
/* Fontcache is an array now. A new font will be appended to the array. */
|
2019-02-28 02:56:01 +01:00
|
|
|
static Fontcache *frc = NULL;
|
2017-01-20 09:06:39 +01:00
|
|
|
static int frclen = 0;
|
2019-02-28 02:56:01 +01:00
|
|
|
static int frccap = 0;
|
2017-10-10 19:17:25 +02:00
|
|
|
static char *usedfont = NULL;
|
|
|
|
static double usedfontsize = 0;
|
|
|
|
static double defaultfontsize = 0;
|
2017-01-20 09:06:39 +01:00
|
|
|
|
2017-10-13 05:25:49 +02:00
|
|
|
static char *opt_class = NULL;
|
|
|
|
static char **opt_cmd = NULL;
|
|
|
|
static char *opt_embed = NULL;
|
|
|
|
static char *opt_font = NULL;
|
|
|
|
static char *opt_io = NULL;
|
|
|
|
static char *opt_line = NULL;
|
|
|
|
static char *opt_name = NULL;
|
|
|
|
static char *opt_title = NULL;
|
|
|
|
|
2018-02-24 23:16:12 +01:00
|
|
|
static int oldbutton = 3; /* button event on startup: 3 = release */
|
|
|
|
|
2017-10-17 22:21:04 +02:00
|
|
|
void
|
|
|
|
clipcopy(const Arg *dummy)
|
|
|
|
{
|
2017-10-17 22:43:32 +02:00
|
|
|
Atom clipboard;
|
|
|
|
|
2018-03-17 13:48:10 +01:00
|
|
|
free(xsel.clipboard);
|
|
|
|
xsel.clipboard = NULL;
|
2017-10-17 22:43:32 +02:00
|
|
|
|
2018-02-22 05:56:02 +01:00
|
|
|
if (xsel.primary != NULL) {
|
|
|
|
xsel.clipboard = xstrdup(xsel.primary);
|
2017-10-17 22:43:32 +02:00
|
|
|
clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
|
|
|
|
XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
|
|
|
|
}
|
2017-10-17 22:21:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
clippaste(const Arg *dummy)
|
|
|
|
{
|
2017-10-17 22:43:32 +02:00
|
|
|
Atom clipboard;
|
|
|
|
|
|
|
|
clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
|
|
|
|
XConvertSelection(xw.dpy, clipboard, xsel.xtarget, clipboard,
|
|
|
|
xw.win, CurrentTime);
|
2017-10-17 22:21:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
selpaste(const Arg *dummy)
|
|
|
|
{
|
2017-10-17 22:43:32 +02:00
|
|
|
XConvertSelection(xw.dpy, XA_PRIMARY, xsel.xtarget, XA_PRIMARY,
|
|
|
|
xw.win, CurrentTime);
|
2017-10-17 22:21:04 +02:00
|
|
|
}
|
|
|
|
|
2018-02-23 21:16:52 +01:00
|
|
|
void
|
|
|
|
numlock(const Arg *dummy)
|
|
|
|
{
|
|
|
|
win.mode ^= MODE_NUMLOCK;
|
|
|
|
}
|
|
|
|
|
2017-10-10 18:30:36 +02:00
|
|
|
void
|
|
|
|
zoom(const Arg *arg)
|
|
|
|
{
|
|
|
|
Arg larg;
|
|
|
|
|
|
|
|
larg.f = usedfontsize + arg->f;
|
|
|
|
zoomabs(&larg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
zoomabs(const Arg *arg)
|
|
|
|
{
|
|
|
|
xunloadfonts();
|
|
|
|
xloadfonts(usedfont, arg->f);
|
|
|
|
cresize(0, 0);
|
|
|
|
redraw();
|
|
|
|
xhints();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
zoomreset(const Arg *arg)
|
|
|
|
{
|
|
|
|
Arg larg;
|
|
|
|
|
|
|
|
if (defaultfontsize > 0) {
|
|
|
|
larg.f = defaultfontsize;
|
|
|
|
zoomabs(&larg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 22:02:26 +02:00
|
|
|
void
|
|
|
|
ttysend(const Arg *arg)
|
|
|
|
{
|
|
|
|
ttywrite(arg->s, strlen(arg->s), 1);
|
|
|
|
}
|
|
|
|
|
2017-10-10 19:46:53 +02:00
|
|
|
int
|
2018-02-25 00:09:13 +01:00
|
|
|
evcol(XEvent *e)
|
2017-10-10 19:46:53 +02:00
|
|
|
{
|
2018-02-25 00:09:13 +01:00
|
|
|
int x = e->xbutton.x - borderpx;
|
2018-02-24 22:32:48 +01:00
|
|
|
LIMIT(x, 0, win.tw - 1);
|
|
|
|
return x / win.cw;
|
2017-10-10 19:46:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-02-25 00:09:13 +01:00
|
|
|
evrow(XEvent *e)
|
2017-10-10 19:46:53 +02:00
|
|
|
{
|
2018-02-25 00:09:13 +01:00
|
|
|
int y = e->xbutton.y - borderpx;
|
2018-02-24 22:32:48 +01:00
|
|
|
LIMIT(y, 0, win.th - 1);
|
|
|
|
return y / win.ch;
|
2017-10-10 19:46:53 +02:00
|
|
|
}
|
|
|
|
|
2017-01-20 09:06:39 +01:00
|
|
|
void
|
2018-02-22 06:54:29 +01:00
|
|
|
mousesel(XEvent *e, int done)
|
2017-01-20 09:06:39 +01:00
|
|
|
{
|
2018-02-22 06:29:41 +01:00
|
|
|
int type, seltype = SEL_REGULAR;
|
2019-10-10 22:42:30 +02:00
|
|
|
uint state = e->xbutton.state & ~(Button1Mask | forcemousemod);
|
2017-01-20 09:06:39 +01:00
|
|
|
|
2017-10-17 22:46:31 +02:00
|
|
|
for (type = 1; type < LEN(selmasks); ++type) {
|
2017-01-20 09:06:39 +01:00
|
|
|
if (match(selmasks[type], state)) {
|
2018-02-22 06:29:41 +01:00
|
|
|
seltype = type;
|
2017-01-20 09:06:39 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-25 00:09:13 +01:00
|
|
|
selextend(evcol(e), evrow(e), seltype, done);
|
2018-02-22 06:54:29 +01:00
|
|
|
if (done)
|
|
|
|
setsel(getsel(), e->xbutton.time);
|
2017-01-20 09:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mousereport(XEvent *e)
|
|
|
|
{
|
2018-02-25 00:09:13 +01:00
|
|
|
int len, x = evcol(e), y = evrow(e),
|
|
|
|
button = e->xbutton.button, state = e->xbutton.state;
|
2017-01-20 09:06:39 +01:00
|
|
|
char buf[40];
|
|
|
|
static int ox, oy;
|
|
|
|
|
|
|
|
/* from urxvt */
|
|
|
|
if (e->xbutton.type == MotionNotify) {
|
|
|
|
if (x == ox && y == oy)
|
|
|
|
return;
|
|
|
|
if (!IS_SET(MODE_MOUSEMOTION) && !IS_SET(MODE_MOUSEMANY))
|
|
|
|
return;
|
|
|
|
/* MOUSE_MOTION: no reporting if no button is pressed */
|
|
|
|
if (IS_SET(MODE_MOUSEMOTION) && oldbutton == 3)
|
|
|
|
return;
|
|
|
|
|
|
|
|
button = oldbutton + 32;
|
|
|
|
ox = x;
|
|
|
|
oy = y;
|
|
|
|
} else {
|
|
|
|
if (!IS_SET(MODE_MOUSESGR) && e->xbutton.type == ButtonRelease) {
|
|
|
|
button = 3;
|
|
|
|
} else {
|
|
|
|
button -= Button1;
|
|
|
|
if (button >= 3)
|
|
|
|
button += 64 - 3;
|
|
|
|
}
|
|
|
|
if (e->xbutton.type == ButtonPress) {
|
|
|
|
oldbutton = button;
|
|
|
|
ox = x;
|
|
|
|
oy = y;
|
|
|
|
} else if (e->xbutton.type == ButtonRelease) {
|
|
|
|
oldbutton = 3;
|
|
|
|
/* MODE_MOUSEX10: no button release reporting */
|
|
|
|
if (IS_SET(MODE_MOUSEX10))
|
|
|
|
return;
|
|
|
|
if (button == 64 || button == 65)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!IS_SET(MODE_MOUSEX10)) {
|
|
|
|
button += ((state & ShiftMask ) ? 4 : 0)
|
|
|
|
+ ((state & Mod4Mask ) ? 8 : 0)
|
|
|
|
+ ((state & ControlMask) ? 16 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IS_SET(MODE_MOUSESGR)) {
|
|
|
|
len = snprintf(buf, sizeof(buf), "\033[<%d;%d;%d%c",
|
|
|
|
button, x+1, y+1,
|
|
|
|
e->xbutton.type == ButtonRelease ? 'm' : 'M');
|
|
|
|
} else if (x < 223 && y < 223) {
|
|
|
|
len = snprintf(buf, sizeof(buf), "\033[M%c%c%c",
|
|
|
|
32+button, 32+x+1, 32+y+1);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-22 07:42:23 +01:00
|
|
|
ttywrite(buf, len, 0);
|
2017-01-20 09:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bpress(XEvent *e)
|
|
|
|
{
|
|
|
|
struct timespec now;
|
|
|
|
MouseShortcut *ms;
|
2018-02-22 06:29:41 +01:00
|
|
|
int snap;
|
2017-01-20 09:06:39 +01:00
|
|
|
|
2019-10-10 22:42:30 +02:00
|
|
|
if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
|
2017-01-20 09:06:39 +01:00
|
|
|
mousereport(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-17 22:46:31 +02:00
|
|
|
for (ms = mshortcuts; ms < mshortcuts + LEN(mshortcuts); ms++) {
|
2019-10-10 22:42:30 +02:00
|
|
|
if (e->xbutton.button == ms->button &&
|
|
|
|
match(ms->mod, e->xbutton.state & ~forcemousemod)) {
|
2019-10-10 22:02:26 +02:00
|
|
|
ms->func(&(ms->arg));
|
2017-01-20 09:06:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e->xbutton.button == Button1) {
|
|
|
|
/*
|
|
|
|
* If the user clicks below predefined timeouts specific
|
|
|
|
* snapping behaviour is exposed.
|
|
|
|
*/
|
2018-02-22 06:29:41 +01:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
2018-02-22 05:56:02 +01:00
|
|
|
if (TIMEDIFF(now, xsel.tclick2) <= tripleclicktimeout) {
|
2018-02-22 06:29:41 +01:00
|
|
|
snap = SNAP_LINE;
|
2018-02-22 05:56:02 +01:00
|
|
|
} else if (TIMEDIFF(now, xsel.tclick1) <= doubleclicktimeout) {
|
2018-02-22 06:29:41 +01:00
|
|
|
snap = SNAP_WORD;
|
2017-01-20 09:06:39 +01:00
|
|
|
} else {
|
2018-02-22 06:29:41 +01:00
|
|
|
snap = 0;
|
2017-01-20 09:06:39 +01:00
|
|
|
}
|
2018-02-22 05:56:02 +01:00
|
|
|
xsel.tclick2 = xsel.tclick1;
|
|
|
|
xsel.tclick1 = now;
|
2018-02-22 06:29:41 +01:00
|
|
|
|
2018-02-25 00:09:13 +01:00
|
|
|
selstart(evcol(e), evrow(e), snap);
|
2017-01-20 09:06:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
propnotify(XEvent *e)
|
|
|
|
{
|
|
|
|
XPropertyEvent *xpev;
|
|
|
|
Atom clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
|
|
|
|
|
|
|
|
xpev = &e->xproperty;
|
|
|
|
if (xpev->state == PropertyNewValue &&
|
|
|
|
(xpev->atom == XA_PRIMARY ||
|
|
|
|
xpev->atom == clipboard)) {
|
|
|
|
selnotify(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
selnotify(XEvent *e)
|
|
|
|
{
|
|
|
|
ulong nitems, ofs, rem;
|
|
|
|
int format;
|
|
|
|
uchar *data, *last, *repl;
|
2018-02-25 00:09:13 +01:00
|
|
|
Atom type, incratom, property = None;
|
2017-01-20 09:06:39 +01:00
|
|
|
|
|
|
|
incratom = XInternAtom(xw.dpy, "INCR", 0);
|
|
|
|
|
|
|
|
ofs = 0;
|
2018-02-25 00:09:13 +01:00
|
|
|
if (e->type == SelectionNotify)
|
2017-01-20 09:06:39 +01:00
|
|
|
property = e->xselection.property;
|
2018-02-25 00:09:13 +01:00
|
|
|
else if (e->type == PropertyNotify)
|
2017-01-20 09:06:39 +01:00
|
|
|
property = e->xproperty.atom;
|
2018-02-25 00:09:13 +01:00
|
|
|
|
2017-01-20 09:06:39 +01:00
|
|
|
if (property == None)
|
|
|
|
return;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (XGetWindowProperty(xw.dpy, xw.win, property, ofs,
|
|
|
|
BUFSIZ/4, False, AnyPropertyType,
|
|
|
|
&type, &format, &nitems, &rem,
|
|
|
|
&data)) {
|
|
|
|
fprintf(stderr, "Clipboard allocation failed\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e->type == PropertyNotify && nitems == 0 && rem == 0) {
|
|
|
|
/*
|
|
|
|
* If there is some PropertyNotify with no data, then
|
|
|
|
* this is the signal of the selection owner that all
|
|
|
|
* data has been transferred. We won't need to receive
|
|
|
|
* PropertyNotify events anymore.
|
|
|
|
*/
|
|
|
|
MODBIT(xw.attrs.event_mask, 0, PropertyChangeMask);
|
|
|
|
XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
|
|
|
|
&xw.attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == incratom) {
|
|
|
|
/*
|
|
|
|
* Activate the PropertyNotify events so we receive
|
|
|
|
* when the selection owner does send us the next
|
|
|
|
* chunk of data.
|
|
|
|
*/
|
|
|
|
MODBIT(xw.attrs.event_mask, 1, PropertyChangeMask);
|
|
|
|
XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask,
|
|
|
|
&xw.attrs);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deleting the property is the transfer start signal.
|
|
|
|
*/
|
|
|
|
XDeleteProperty(xw.dpy, xw.win, (int)property);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* As seen in getsel:
|
|
|
|
* Line endings are inconsistent in the terminal and GUI world
|
|
|
|
* copy and pasting. When receiving some selection data,
|
|
|
|
* replace all '\n' with '\r'.
|
|
|
|
* FIXME: Fix the computer world.
|
|
|
|
*/
|
|
|
|
repl = data;
|
|
|
|
last = data + nitems * format / 8;
|
|
|
|
while ((repl = memchr(repl, '\n', last - repl))) {
|
|
|
|
*repl++ = '\r';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IS_SET(MODE_BRCKTPASTE) && ofs == 0)
|
2018-02-22 07:42:23 +01:00
|
|
|
ttywrite("\033[200~", 6, 0);
|
|
|
|
ttywrite((char *)data, nitems * format / 8, 1);
|
2017-01-20 09:06:39 +01:00
|
|
|
if (IS_SET(MODE_BRCKTPASTE) && rem == 0)
|
2018-02-22 07:42:23 +01:00
|
|
|
ttywrite("\033[201~", 6, 0);
|
2017-01-20 09:06:39 +01:00
|
|
|
XFree(data);
|
|
|
|
/* number of 32-bit chunks returned */
|
|
|
|
ofs += nitems * format / 32;
|
|
|
|
} while (rem > 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Deleting the property again tells the selection owner to send the
|
|
|
|
* next data chunk in the property.
|
|
|
|
*/
|
|
|
|
XDeleteProperty(xw.dpy, xw.win, (int)property);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
xclipcopy(void)
|
|
|
|
{
|
2017-10-17 22:43:32 +02:00
|
|
|
clipcopy(NULL);
|
2017-01-20 09:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
selclear_(XEvent *e)
|
|
|
|
{
|
|
|
|
selclear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
selrequest(XEvent *e)
|
|
|
|
{
|
|
|
|
XSelectionRequestEvent *xsre;
|
|
|
|
XSelectionEvent xev;
|
|
|
|
Atom xa_targets, string, clipboard;
|
|
|
|
char *seltext;
|
|
|
|
|
|
|
|
xsre = (XSelectionRequestEvent *) e;
|
|
|
|
xev.type = SelectionNotify;
|
|
|
|
xev.requestor = xsre->requestor;
|
|
|
|
xev.selection = xsre->selection;
|
|
|
|
xev.target = xsre->target;
|
|
|
|
xev.time = xsre->time;
|
|
|
|
if (xsre->property == None)
|
|
|
|
xsre->property = xsre->target;
|
|
|
|
|
|
|
|
/* reject */
|
|
|
|
xev.property = None;
|
|
|
|
|
|
|
|
xa_targets = XInternAtom(xw.dpy, "TARGETS", 0);
|
|
|
|
if (xsre->target == xa_targets) {
|
|
|
|
/* respond with the supported type */
|
|
|
|
string = xsel.xtarget;
|
|
|
|
XChangeProperty(xsre->display, xsre->requestor, xsre->property,
|
|
|
|
XA_ATOM, 32, PropModeReplace,
|
|
|
|
(uchar *) &string, 1);
|
|
|
|
xev.property = xsre->property;
|
|
|
|
} else if (xsre->target == xsel.xtarget || xsre->target == XA_STRING) {
|
|
|
|
/*
|
|
|
|
* xith XA_STRING non ascii characters may be incorrect in the
|
|
|
|
* requestor. It is not our problem, use utf8.
|
|
|
|
*/
|
|
|
|
clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
|
|
|
|
if (xsre->selection == XA_PRIMARY) {
|
2018-02-22 05:56:02 +01:00
|
|
|
seltext = xsel.primary;
|
2017-01-20 09:06:39 +01:00
|
|
|
} else if (xsre->selection == clipboard) {
|
2018-02-22 05:56:02 +01:00
|
|
|
seltext = xsel.clipboard;
|
2017-01-20 09:06:39 +01:00
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Unhandled clipboard selection 0x%lx\n",
|
|
|
|
xsre->selection);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (seltext != NULL) {
|
|
|
|
XChangeProperty(xsre->display, xsre->requestor,
|
|
|
|
xsre->property, xsre->target,
|
|
|
|
8, PropModeReplace,
|
|
|
|
(uchar *)seltext, strlen(seltext));
|
|
|
|
xev.property = xsre->property;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* all done, send a notification to the listener */
|
|
|
|
if (!XSendEvent(xsre->display, xsre->requestor, 1, 0, (XEvent *) &xev))
|
|
|
|
fprintf(stderr, "Error sending SelectionNotify event\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-11-07 01:25:58 +01:00
|
|
|
setsel(char *str, Time t)
|
2017-01-20 09:06:39 +01:00
|
|
|
{
|
2018-03-20 21:25:30 +01:00
|
|
|
if (!str)
|
|
|
|
return;
|
|
|
|
|
2018-02-22 05:56:02 +01:00
|
|
|
free(xsel.primary);
|
|
|
|
xsel.primary = str;
|
2017-01-20 09:06:39 +01:00
|
|
|
|
|
|
|
XSetSelectionOwner(xw.dpy, XA_PRIMARY, xw.win, t);
|
|
|
|
if (XGetSelectionOwner(xw.dpy, XA_PRIMARY) != xw.win)
|
2018-02-25 00:09:13 +01:00
|
|
|
selclear();
|
2017-01-20 09:06:39 +01:00
|
|
|
}
|
|
|
|
|
2017-11-07 01:25:58 +01:00
|
|
|
void
|
|
|
|
xsetsel(char *str)
|
|
|
|
{
|
|
|
|
setsel(str, CurrentTime);
|
|
|
|
}
|
|
|
|
|
2017-01-20 09:06:39 +01:00
|
|
|
void
|
|
|
|
brelease(XEvent *e)
|
|
|
|
{
|
2019-10-10 22:42:30 +02:00
|
|
|
if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
|
2017-01-20 09:06:39 +01:00
|
|
|
mousereport(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-22 06:54:29 +01:00
|
|
|
if (e->xbutton.button == Button2)
|
2017-10-17 22:43:32 +02:00
|
|
|
selpaste(NULL);
|
2018-02-22 06:54:29 +01:00
|
|
|
else if (e->xbutton.button == Button1)
|
|
|
|
mousesel(e, 1);
|
2017-01-20 09:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bmotion(XEvent *e)
|
|
|
|
{
|
2019-10-10 22:42:30 +02:00
|
|
|
if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
|
2017-01-20 09:06:39 +01:00
|
|
|
mousereport(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-22 06:54:29 +01:00
|
|
|
mousesel(e, 0);
|
2017-01-20 09:06:39 +01:00
|
|
|
}
|
|
|
|
|
2017-10-10 19:46:53 +02:00
|
|
|
void
|
|
|
|
cresize(int width, int height)
|
|
|
|
{
|
|
|
|
int col, row;
|
|
|
|
|
|
|
|
if (width != 0)
|
|
|
|
win.w = width;
|
|
|
|
if (height != 0)
|
|
|
|
win.h = height;
|
|
|
|
|
|
|
|
col = (win.w - 2 * borderpx) / win.cw;
|
|
|
|
row = (win.h - 2 * borderpx) / win.ch;
|
2018-06-27 17:08:30 +02:00
|
|
|
col = MAX(1, col);
|
|
|
|
row = MAX(1, row);
|
2017-10-10 19:46:53 +02:00
|
|
|
|
|
|
|
tresize(col, row);
|
|
|
|
xresize(col, row);
|
2018-02-22 05:28:41 +01:00
|
|
|
ttyresize(win.tw, win.th);
|
2017-10-10 19:46:53 +02:00
|
|
|
}
|
|
|
|
|
2017-01-20 09:06:39 +01:00
|
|
|
void
|
|
|
|
xresize(int col, int row)
|
|
|
|
{
|
2018-06-27 17:08:30 +02:00
|
|
|
win.tw = col * win.cw;
|
|
|
|
win.th = row * win.ch;
|
2017-01-20 09:06:39 +01:00
|
|
|
|
|
|
|
XFreePixmap(xw.dpy, xw.buf);
|
|
|
|
xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
|
|
|
|
DefaultDepth(xw.dpy, xw.scr));
|
|
|
|
XftDrawChange(xw.draw, xw.buf);
|
|
|
|
xclear(0, 0, win.w, win.h);
|
2017-10-10 19:17:25 +02:00
|
|
|
|
|
|
|
/* resize to new width */
|
|
|
|
xw.specbuf = xrealloc(xw.specbuf, col * sizeof(GlyphFontSpec));
|
2017-01-20 09:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ushort
|
|
|
|
sixd_to_16bit(int x)
|
|
|
|
{
|
|
|
|
return x == 0 ? 0 : 0x3737 + 0x2828 * x;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
xloadcolor(int i, const char *name, Color *ncolor)
|
|
|
|
{
|
|
|
|
XRenderColor color = { .alpha = 0xffff };
|
|
|
|
|
|
|
|
if (!name) {
|
|
|
|
if (BETWEEN(i, 16, 255)) { /* 256 color */
|
|
|
|
if (i < 6*6*6+16) { /* same colors as xterm */
|
|
|
|
color.red = sixd_to_16bit( ((i-16)/36)%6 );
|
|
|
|
color.green = sixd_to_16bit( ((i-16)/6) %6 );
|
|
|
|
color.blue = sixd_to_16bit( ((i-16)/1) %6 );
|
|
|
|
} else { /* greyscale */
|
|
|
|
color.red = 0x0808 + 0x0a0a * (i - (6*6*6+16));
|
|
|
|
color.green = color.blue = color.red;
|
|
|
|
}
|
|
|
|
return XftColorAllocValue(xw.dpy, xw.vis,
|
|
|
|
xw.cmap, &color, ncolor);
|
|
|
|
} else
|
|
|
|
name = colorname[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
xloadcols(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
static int loaded;
|
|
|
|
Color *cp;
|
|
|
|
|
|
|
|
if (loaded) {
|
|
|
|
for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp)
|
|
|
|
XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
|
2018-11-04 14:35:07 +01:00
|
|
|
} else {
|
|
|
|
dc.collen = MAX(LEN(colorname), 256);
|
|
|
|
dc.col = xmalloc(dc.collen * sizeof(Color));
|
2017-01-20 09:06:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < dc.collen; i++)
|
|
|
|
if (!xloadcolor(i, NULL, &dc.col[i])) {
|
|
|
|
if (colorname[i])
|
2018-03-29 18:30:05 +02:00
|
|
|
die("could not allocate color '%s'\n", colorname[i]);
|
2017-01-20 09:06:39 +01:00
|
|
|
else
|
2018-03-29 18:30:05 +02:00
|
|
|
die("could not allocate color %d\n", i);
|
2017-01-20 09:06:39 +01:00
|
|
|
}
|
|
|
|
loaded = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
xsetcolorname(int x, const char *name)
|
|
|
|
{
|
|
|
|
Color ncolor;
|
|
|
|
|
|
|
|
if (!BETWEEN(x, 0, dc.collen))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (!xloadcolor(x, name, &ncolor))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
|
|
|
|
dc.col[x] = ncolor;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Absolute coordinates.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
xclear(int x1, int y1, int x2, int y2)
|
|
|
|
{
|
|
|
|
XftDrawRect(xw.draw,
|
|
|
|
&dc.col[IS_SET(MODE_REVERSE)? defaultfg : defaultbg],
|
|
|
|
x1, y1, x2-x1, y2-y1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
xhints(void)
|
|
|
|
{
|
|
|
|
XClassHint class = {opt_name ? opt_name : termname,
|
|
|
|
opt_class ? opt_class : termname};
|
|
|
|
XWMHints wm = {.flags = InputHint, .input = 1};
|
2018-03-09 15:36:25 +01:00
|
|
|
XSizeHints *sizeh;
|
2017-01-20 09:06:39 +01:00
|
|
|
|
|
|
|
sizeh = XAllocSizeHints();
|
|
|
|
|
2018-06-27 17:08:30 +02:00
|
|
|
sizeh->flags = PSize | PResizeInc | PBaseSize | PMinSize;
|
2017-01-20 09:06:39 +01:00
|
|
|
sizeh->height = win.h;
|
|
|
|
sizeh->width = win.w;
|
|
|
|
sizeh->height_inc = win.ch;
|
|
|
|
sizeh->width_inc = win.cw;
|
|
|
|
sizeh->base_height = 2 * borderpx;
|
|
|
|
sizeh->base_width = 2 * borderpx;
|
2018-06-27 17:08:30 +02:00
|
|
|
sizeh->min_height = win.ch + 2 * borderpx;
|
|
|
|
sizeh->min_width = win.cw + 2 * borderpx;
|
2017-01-20 09:06:39 +01:00
|
|
|
if (xw.isfixed) {
|
2018-06-27 17:08:30 +02:00
|
|
|
sizeh->flags |= PMaxSize;
|
2017-01-20 09:06:39 +01:00
|
|
|
sizeh->min_width = sizeh->max_width = win.w;
|
|
|
|
sizeh->min_height = sizeh->max_height = win.h;
|
|
|
|
}
|
|
|
|
if (xw.gm & (XValue|YValue)) {
|
|
|
|
sizeh->flags |= USPosition | PWinGravity;
|
|
|
|
sizeh->x = xw.l;
|
|
|
|
sizeh->y = xw.t;
|
|
|
|
sizeh->win_gravity = xgeommasktogravity(xw.gm);
|
|
|
|
}
|
|
|
|
|
|
|
|
XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm,
|
|
|
|
&class);
|
|
|
|
XFree(sizeh);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
xgeommasktogravity(int mask)
|
|
|
|
{
|
|
|
|
switch (mask & (XNegative|YNegative)) {
|
|
|
|
case 0:
|
|
|
|
return NorthWestGravity;
|
|
|
|
case XNegative:
|
|
|
|
return NorthEastGravity;
|
|
|
|
case YNegative:
|
|
|
|
return SouthWestGravity;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SouthEastGravity;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
xloadfont(Font *f, FcPattern *pattern)
|
|
|
|
{
|
|
|
|
FcPattern *configured;
|
|
|
|
FcPattern *match;
|
|
|
|
FcResult result;
|
|
|
|
XGlyphInfo extents;
|
|
|
|
int wantattr, haveattr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Manually configure instead of calling XftMatchFont
|
|
|
|
* so that we can use the configured pattern for
|
|
|
|
* "missing glyph" lookups.
|
|
|
|
*/
|
|
|
|
configured = FcPatternDuplicate(pattern);
|
|
|
|
if (!configured)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
FcConfigSubstitute(NULL, configured, FcMatchPattern);
|
|
|
|
XftDefaultSubstitute(xw.dpy, xw.scr, configured);
|
|
|
|
|
|
|
|
match = FcFontMatch(NULL, configured, &result);
|
|
|
|
if (!match) {
|
|
|
|
FcPatternDestroy(configured);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if |