#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
+#include <libgen.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
MODE_CRLF = 16,
MODE_MOUSEBTN = 32,
MODE_MOUSEMOTION = 64,
- MODE_MOUSE = 32|64,
MODE_REVERSE = 128,
MODE_KBDLOCK = 256,
MODE_HIDE = 512,
MODE_8BIT = 8192,
MODE_BLINK = 16384,
MODE_FBLINK = 32768,
+ MODE_FOCUS = 65536,
+ MODE_MOUSEX10 = 131072,
+ MODE_MOUSEMANY = 262144,
+ MODE_MOUSE = MODE_MOUSEBTN|MODE_MOUSEMOTION|MODE_MOUSEX10\
+ |MODE_MOUSEMANY,
};
enum escape_state {
SNAP_LINE = 2
};
-/* bit macro */
-#undef B0
-enum { B0=1, B1=2, B2=4, B3=8, B4=16, B5=32, B6=64, B7=128 };
-
typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
XIC xic;
Draw draw;
Visual *vis;
+ XSetWindowAttributes attrs;
int scr;
bool isfixed; /* is fixed geometry? */
int fx, fy, fw, fh; /* fixed geometry */
signed char crlf; /* crlf mode */
} Key;
-/* TODO: use better name for vars... */
typedef struct {
int mode;
int type;
static int xloadfontset(Font *);
static void xsettitle(char *);
static void xresettitle(void);
+static void xsetpointermotion(int);
static void xseturgency(int);
static void xsetsel(char*);
static void xtermclear(int, int, int, int);
static char *opt_embed = NULL;
static char *opt_class = NULL;
static char *opt_font = NULL;
+static int oldbutton = 3; /* button event on startup: 3 = release */
static char *usedfont = NULL;
static int usedfontsize = 0;
rtn = 1;
c = *s;
- if(~c & B7) { /* 0xxxxxxx */
+ if(~c & 0x80) { /* 0xxxxxxx */
*u = c;
return rtn;
- } else if((c & (B7|B6|B5)) == (B7|B6)) { /* 110xxxxx */
- *u = c&(B4|B3|B2|B1|B0);
+ } else if((c & 0xE0) == 0xC0) { /* 110xxxxx */
+ *u = c & 0x1F;
n = 1;
- } else if((c & (B7|B6|B5|B4)) == (B7|B6|B5)) { /* 1110xxxx */
- *u = c&(B3|B2|B1|B0);
+ } else if((c & 0xF0) == 0xE0) { /* 1110xxxx */
+ *u = c & 0x0F;
n = 2;
- } else if((c & (B7|B6|B5|B4|B3)) == (B7|B6|B5|B4)) { /* 11110xxx */
- *u = c & (B2|B1|B0);
+ } else if((c & 0xF8) == 0xF0) { /* 11110xxx */
+ *u = c & 0x07;
n = 3;
} else {
goto invalid;
for(i = n, ++s; i > 0; --i, ++rtn, ++s) {
c = *s;
- if((c & (B7|B6)) != B7) /* 10xxxxxx */
+ if((c & 0xC0) != 0x80) /* 10xxxxxx */
goto invalid;
*u <<= 6;
- *u |= c & (B5|B4|B3|B2|B1|B0);
+ *u |= c & 0x3F;
}
if((n == 1 && *u < 0x80) ||
*sp = uc; /* 0xxxxxxx */
return 1;
} else if(*u < 0x800) {
- *sp = (uc >> 6) | (B7|B6); /* 110xxxxx */
+ *sp = (uc >> 6) | 0xC0; /* 110xxxxx */
n = 1;
} else if(uc < 0x10000) {
- *sp = (uc >> 12) | (B7|B6|B5); /* 1110xxxx */
+ *sp = (uc >> 12) | 0xE0; /* 1110xxxx */
n = 2;
} else if(uc <= 0x10FFFF) {
- *sp = (uc >> 18) | (B7|B6|B5|B4); /* 11110xxx */
+ *sp = (uc >> 18) | 0xF0; /* 11110xxx */
n = 3;
} else {
goto invalid;
}
for(i=n,++sp; i>0; --i,++sp)
- *sp = ((uc >> 6*(i-1)) & (B5|B4|B3|B2|B1|B0)) | B7; /* 10xxxxxx */
+ *sp = ((uc >> 6*(i-1)) & 0x3F) | 0x80; /* 10xxxxxx */
return n+1;
invalid:
c3 = (uchar *)++s;
if(b < 1) {
return 0;
- } else if((*c1&(B7|B6|B5)) == (B7|B6) && b == 1) {
+ } else if((*c1 & 0xE0) == 0xC0 && b == 1) {
return 0;
- } else if((*c1&(B7|B6|B5|B4)) == (B7|B6|B5) &&
+ } else if((*c1 & 0xF0) == 0xE0 &&
((b == 1) ||
- ((b == 2) && (*c2&(B7|B6)) == B7))) {
+ ((b == 2) && (*c2 & 0xC0) == 0x80))) {
return 0;
- } else if((*c1&(B7|B6|B5|B4|B3)) == (B7|B6|B5|B4) &&
+ } else if((*c1 & 0xF8) == 0xF0 &&
((b == 1) ||
- ((b == 2) && (*c2&(B7|B6)) == B7) ||
- ((b == 3) && (*c2&(B7|B6)) == B7 && (*c3&(B7|B6)) == B7))) {
+ ((b == 2) && (*c2 & 0xC0) == 0x80) ||
+ ((b == 3) && (*c2 & 0xC0) == 0x80 && (*c3 & 0xC0) == 0x80))) {
return 0;
} else {
return 1;
utf8size(char *s) {
uchar c = *s;
- if(~c&B7) {
+ if(~c & 0x80) {
return 1;
- } else if((c&(B7|B6|B5)) == (B7|B6)) {
+ } else if((c & 0xE0) == 0xC0) {
return 2;
- } else if((c&(B7|B6|B5|B4)) == (B7|B6|B5)) {
+ } else if((c & 0xF0) == 0xE0) {
return 3;
} else {
return 4;
sel.oe.x = x2col(e->xbutton.x);
sel.oe.y = y2row(e->xbutton.y);
- if (sel.ob.y < sel.oe.y
+ if(sel.ob.y < sel.oe.y
|| (sel.ob.y == sel.oe.y && sel.ob.x < sel.oe.x)) {
selsnap(sel.snap, &sel.ob.x, &sel.ob.y, -1);
selsnap(sel.snap, &sel.oe.x, &sel.oe.y, +1);
selsnap(sel.snap, &sel.oe.x, &sel.oe.y, -1);
selsnap(sel.snap, &sel.ob.x, &sel.ob.y, +1);
}
-
selsort();
sel.type = SEL_REGULAR;
button = e->xbutton.button, state = e->xbutton.state,
len;
char buf[40];
- static int ob, ox, oy;
+ static int ox, oy;
/* from urxvt */
if(e->xbutton.type == MotionNotify) {
- if(!IS_SET(MODE_MOUSEMOTION) || (x == ox && y == oy))
+ if(x == ox && y == oy)
+ return;
+ if(!IS_SET(MODE_MOUSEMOTION) && !IS_SET(MODE_MOUSEMANY))
return;
- button = ob + 32;
+ /* 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)
if(button >= 3)
button += 64 - 3;
if(e->xbutton.type == ButtonPress) {
- ob = button;
+ oldbutton = button;
ox = x;
oy = y;
}
}
- button += (state & ShiftMask ? 4 : 0)
- + (state & Mod4Mask ? 8 : 0)
- + (state & ControlMask ? 16 : 0);
+ if(!IS_SET(MODE_MOUSEX10)) {
+ button += (state & ShiftMask ? 4 : 0)
+ + (state & Mod4Mask ? 8 : 0)
+ + (state & ControlMask ? 16 : 0);
+ }
len = 0;
if(IS_SET(MODE_MOUSESGR)) {
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);
+ IS_SET(MODE_MOUSEX10)? button-1 : 32+button,
+ 32+x+1, 32+y+1);
} else {
return;
}
gettimeofday(&now, NULL);
/* Clear previous selection, logically and visually. */
- if(sel.ob.x != -1) {
- sel.ob.x = -1;
- tsetdirt(sel.nb.y, sel.ne.y);
- draw();
- }
+ selclear(NULL);
sel.mode = 1;
sel.type = SEL_REGULAR;
sel.oe.x = sel.ob.x = x2col(e->xbutton.x);
if(sel.snap != 0) {
sel.mode++;
tsetdirt(sel.nb.y, sel.ne.y);
- draw();
}
sel.tclick2 = sel.tclick1;
sel.tclick1 = now;
selpaste(NULL);
} else if(e->xbutton.button == Button1) {
if(sel.mode < 2) {
- sel.ob.x = -1;
+ selclear(NULL);
} else {
getbuttoninfo(e);
selcopy();
}
sel.mode = 0;
- term.dirty[sel.oe.y] = 1;
+ tsetdirt(sel.nb.y, sel.ne.y);
}
}
if(BETWEEN(sel.ob.y, orig, term.bot) || BETWEEN(sel.oe.y, orig, term.bot)) {
if((sel.ob.y += n) > term.bot || (sel.oe.y += n) < term.top) {
- sel.ob.x = -1;
+ selclear(NULL);
return;
}
if(sel.type == SEL_RECTANGULAR) {
case 25: /* DECTCEM -- Text Cursor Enable Mode */
MODBIT(term.mode, !set, MODE_HIDE);
break;
- case 1000: /* 1000,1002: enable xterm mouse report */
+ case 9: /* X10 mouse compatibility mode */
+ xsetpointermotion(0);
+ MODBIT(term.mode, 0, MODE_MOUSE);
+ MODBIT(term.mode, set, MODE_MOUSEX10);
+ break;
+ case 1000: /* 1000: report button press */
+ xsetpointermotion(0);
+ MODBIT(term.mode, 0, MODE_MOUSE);
MODBIT(term.mode, set, MODE_MOUSEBTN);
- MODBIT(term.mode, 0, MODE_MOUSEMOTION);
break;
- case 1002:
+ case 1002: /* 1002: report motion on button press */
+ xsetpointermotion(0);
+ MODBIT(term.mode, 0, MODE_MOUSE);
MODBIT(term.mode, set, MODE_MOUSEMOTION);
- MODBIT(term.mode, 0, MODE_MOUSEBTN);
break;
- case 1006:
+ case 1003: /* 1003: enable all mouse motions */
+ xsetpointermotion(set);
+ MODBIT(term.mode, 0, MODE_MOUSE);
+ MODBIT(term.mode, set, MODE_MOUSEMANY);
+ break;
+ case 1004: /* 1004: send focus events to tty */
+ MODBIT(term.mode, set, MODE_FOCUS);
+ break;
+ case 1006: /* 1006: extended reporting mode */
MODBIT(term.mode, set, MODE_MOUSESGR);
break;
case 1034:
case 1048:
tcursor((set) ? CURSOR_SAVE : CURSOR_LOAD);
break;
+ /* Not implemented mouse modes. See comments there. */
+ case 1001: /* mouse highlight mode; can hang the
+ terminal by design when implemented. */
+ case 1005: /* UTF-8 mouse mode; will confuse
+ applications not supporting UTF-8
+ and luit. */
+ case 1015: /* urxvt mangled mouse mode; incompatible
+ and can be mistaken for other control
+ codes. */
default:
fprintf(stderr,
"erresc: unknown private set/reset mode %d\n",
}
}
}
-#undef MODBIT
-
void
csihandle(void) {
tputtab(1);
break;
case 'J': /* ED -- Clear screen */
- sel.ob.x = -1;
+ selclear(NULL);
switch(csiescseq.arg[0]) {
case 0: /* below */
tclearregion(term.c.x, term.c.y, term.col-1, term.c.y);
if(control && !(term.c.attr.mode & ATTR_GFX))
return;
if(sel.ob.x != -1 && BETWEEN(term.c.y, sel.ob.y, sel.oe.y))
- sel.ob.x = -1;
+ selclear(NULL);
if(IS_SET(MODE_WRAP) && (term.c.state & CURSOR_WRAPNEXT)) {
term.line[term.c.y][term.c.x].mode |= ATTR_WRAP;
tnewline(1);
void
xinit(void) {
- XSetWindowAttributes attrs;
XGCValues gcvalues;
Cursor cursor;
Window parent;
}
/* Events */
- attrs.background_pixel = dc.col[defaultbg].pixel;
- attrs.border_pixel = dc.col[defaultbg].pixel;
- attrs.bit_gravity = NorthWestGravity;
- attrs.event_mask = FocusChangeMask | KeyPressMask
+ xw.attrs.background_pixel = dc.col[defaultbg].pixel;
+ xw.attrs.border_pixel = dc.col[defaultbg].pixel;
+ xw.attrs.bit_gravity = NorthWestGravity;
+ xw.attrs.event_mask = FocusChangeMask | KeyPressMask
| ExposureMask | VisibilityChangeMask | StructureNotifyMask
| ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
- attrs.colormap = xw.cmap;
+ xw.attrs.colormap = xw.cmap;
parent = opt_embed ? strtol(opt_embed, NULL, 0) : \
XRootWindow(xw.dpy, xw.scr);
xw.win = XCreateWindow(xw.dpy, parent, xw.fx, xw.fy,
xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
- xw.vis,
- CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
- | CWColormap,
- &attrs);
+ xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
+ | CWEventMask | CWColormap, &xw.attrs);
memset(&gcvalues, 0, sizeof(gcvalues));
gcvalues.graphics_exposures = False;
xw.draw = XftDrawCreate(xw.dpy, xw.buf, xw.vis, xw.cmap);
/* input methods */
- if((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
+ if((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
XSetLocaleModifiers("@im=local");
if((xw.xim = XOpenIM(xw.dpy, NULL, NULL, NULL)) == NULL) {
XSetLocaleModifiers("@im=");
xw.state &= ~WIN_VISIBLE;
}
+void
+xsetpointermotion(int set) {
+ MODBIT(xw.attrs.event_mask, set, PointerMotionMask);
+ XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs);
+}
+
void
xseturgency(int add) {
XWMHints *h = XGetWMHints(xw.dpy, xw.win);
XSetICFocus(xw.xic);
xw.state |= WIN_FOCUSED;
xseturgency(0);
+ if(IS_SET(MODE_FOCUS))
+ ttywrite("\033[I", 3);
} else {
XUnsetICFocus(xw.xic);
xw.state &= ~WIN_FOCUSED;
+ if(IS_SET(MODE_FOCUS))
+ ttywrite("\033[O", 3);
}
}
-inline bool
+static inline bool
match(uint mask, uint state) {
- state &= ~(ignoremod);
+ state &= ~ignoremod;
if(mask == XK_NO_MOD && state)
return false;
if(mask != XK_ANY_MOD && mask != XK_NO_MOD && !state)
return false;
- if((state & mask) != state)
- return false;
- return true;
+ if(mask == XK_ANY_MOD)
+ return true;
+ return state == mask;
}
void
char*
kmap(KeySym k, uint state) {
- uint mask;
Key *kp;
int i;
}
for(kp = key; kp < key + LEN(key); kp++) {
- mask = kp->mask;
-
if(kp->k != k)
continue;
- if(!match(mask, state))
+ if(!match(kp->mask, state))
continue;
if(kp->appkey > 0) {
if(len == 1 && e->state & Mod1Mask) {
if(IS_SET(MODE_8BIT)) {
if(*xstr < 0177) {
- c = *xstr | B7;
+ c = *xstr | 0x80;
ret = utf8encode(&c, cp);
cp += ret;
len = 0;
void
run(void) {
XEvent ev;
+ int w = xw.w, h = xw.h;
fd_set rfd;
int xfd = XConnectionNumber(xw.dpy), xev, blinkset = 0, dodraw = 0;
struct timeval drawtimeout, *tv = NULL, now, last, lastblink;
+ /* Waiting for window mapping */
+ while(1) {
+ XNextEvent(xw.dpy, &ev);
+ if(ev.type == ConfigureNotify) {
+ w = ev.xconfigure.width;
+ h = ev.xconfigure.height;
+ } else if(ev.type == MapNotify) {
+ break;
+ }
+ }
+
+ if(!xw.isfixed)
+ cresize(w, h);
+ else
+ cresize(xw.fw, xw.fh);
+ ttynew();
+
gettimeofday(&lastblink, NULL);
gettimeofday(&last, NULL);
main(int argc, char *argv[]) {
int bitm, xr, yr;
uint wr, hr;
+ char *titles;
xw.fw = xw.fh = xw.fx = xw.fy = 0;
xw.isfixed = False;
break;
case 'e':
/* eat all remaining arguments */
- if(argc > 1)
+ if(argc > 1) {
opt_cmd = &argv[1];
+ if(argv[1] != NULL && opt_title == NULL) {
+ titles = strdup(argv[1]);
+ opt_title = basename(titles);
+ }
+ }
goto run;
case 'f':
opt_font = EARGF(usage());
XSetLocaleModifiers("");
tnew(80, 24);
xinit();
- ttynew();
selinit();
- if(xw.isfixed)
- cresize(xw.h, xw.w);
run();
return 0;