X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=st.c;h=46490798e9b867821c2b7dfb954c7325e16308f1;hb=d5f3d120eae2377b8a2f80c94fbb5e4bd7a6189d;hp=a4aca513222cda4c58661ee9eecc67d80a3d57ed;hpb=d59f92d6f19a4a914ba99168fdc42d1783eee1ba;p=st.git diff --git a/st.c b/st.c index a4aca51..4649079 100644 --- a/st.c +++ b/st.c @@ -35,7 +35,7 @@ #define USAGE \ "st-" VERSION ", (c) 2010-2011 st engineers\n" \ - "usage: st [-t title] [-c class] [-v] [-e command...]\n" + "usage: st [-t title] [-c class] [-w windowid] [-v] [-e command...]\n" /* Arbitrary sizes */ #define ESC_TITLE_SIZ 256 @@ -64,7 +64,7 @@ enum { CURSOR_UP, CURSOR_DOWN, CURSOR_LEFT, CURSOR_RIGHT, enum { CURSOR_DEFAULT = 0, CURSOR_HIDE = 1, CURSOR_WRAPNEXT = 2 }; enum { GLYPH_SET=1, GLYPH_DIRTY=2 }; enum { MODE_WRAP=1, MODE_INSERT=2, MODE_APPKEYPAD=4, MODE_ALTSCREEN=8, - MODE_CRLF=16, MODE_MOUSE=32, MODE_REVERSE=64 }; + MODE_CRLF=16, MODE_MOUSEBTN=32, MODE_MOUSEMOTION=64, MODE_MOUSE=32|64, MODE_REVERSE=128 }; enum { ESC_START=1, ESC_CSI=2, ESC_OSC=4, ESC_TITLE=8, ESC_ALTCHARSET=16 }; enum { WIN_VISIBLE=1, WIN_REDRAW=2, WIN_FOCUSED=4 }; @@ -241,6 +241,8 @@ static void (*handler[LASTEvent])(XEvent *) = { [VisibilityNotify] = visibility, [UnmapNotify] = unmap, [Expose] = expose, + [EnterNotify] = focus, + [LeaveNotify] = focus, [FocusIn] = focus, [FocusOut] = focus, [MotionNotify] = bmotion, @@ -260,6 +262,7 @@ static pid_t pid; static Selection sel; static char **opt_cmd = NULL; static char *opt_title = NULL; +static char *opt_embed = NULL; static char *opt_class = NULL; int @@ -418,17 +421,24 @@ mousereport(XEvent *e) { int button = e->xbutton.button; int state = e->xbutton.state; char buf[] = { '\033', '[', 'M', 0, 32+x+1, 32+y+1 }; - - if(!IS_SET(MODE_MOUSE)) - return; + static int ob, ox, oy; /* from urxvt */ - if(e->xbutton.type == ButtonRelease || button == AnyButton) + if(e->xbutton.type == MotionNotify) { + if(!IS_SET(MODE_MOUSEMOTION) || (x == ox && y == oy)) + return; + button = ob + 32; + ox = x, oy = y; + } else if(e->xbutton.type == ButtonRelease || button == AnyButton) { button = 3; - else { + } else { button -= Button1; if(button >= 3) button += 64 - 3; + if(e->xbutton.type == ButtonPress) { + ob = button; + ox = x, oy = y; + } } buf[3] = 32 + button + (state & ShiftMask ? 4 : 0) @@ -440,10 +450,13 @@ mousereport(XEvent *e) { void bpress(XEvent *e) { - mousereport(e); - sel.mode = 1; - sel.ex = sel.bx = X2COL(e->xbutton.x); - sel.ey = sel.by = Y2ROW(e->xbutton.y); + if(IS_SET(MODE_MOUSE)) + mousereport(e); + else if(e->xbutton.button == Button1) { + sel.mode = 1; + sel.ex = sel.bx = X2COL(e->xbutton.x); + sel.ey = sel.by = Y2ROW(e->xbutton.y); + } } void @@ -473,8 +486,7 @@ selcopy(void) { void selnotify(XEvent *e) { - unsigned long nitems; - unsigned long ofs, rem; + unsigned long nitems, ofs, rem; int format; unsigned char *data; Atom type; @@ -552,17 +564,18 @@ xsetsel(char *str) { void brelease(XEvent *e) { - int b; - - sel.mode = 0; - getbuttoninfo(e, &b, &sel.ex, &sel.ey); - mousereport(e); - if(sel.bx == sel.ex && sel.by == sel.ey) { - sel.bx = -1; - if(b == 2) - selpaste(); - else if(b == 1) { + if(IS_SET(MODE_MOUSE)) { + mousereport(e); + return; + } + if(e->xbutton.button == Button2) + selpaste(); + else if(e->xbutton.button == Button1) { + sel.mode = 0; + getbuttoninfo(e, NULL, &sel.ex, &sel.ey); + if(sel.bx == sel.ex && sel.by == sel.ey) { struct timeval now; + sel.bx = -1; gettimeofday(&now, NULL); if(TIMEDIFF(now, sel.tclick2) <= TRIPLECLICK_TIMEOUT) { @@ -574,18 +587,18 @@ brelease(XEvent *e) { } else if(TIMEDIFF(now, sel.tclick1) <= DOUBLECLICK_TIMEOUT) { /* double click to select word */ sel.bx = sel.ex; - while(term.line[sel.ey][sel.bx-1].state & GLYPH_SET && + while(sel.bx > 0 && term.line[sel.ey][sel.bx-1].state & GLYPH_SET && term.line[sel.ey][sel.bx-1].c[0] != ' ') sel.bx--; sel.b.x = sel.bx; - while(term.line[sel.ey][sel.ex+1].state & GLYPH_SET && + while(sel.ex < term.col-1 && term.line[sel.ey][sel.ex+1].state & GLYPH_SET && term.line[sel.ey][sel.ex+1].c[0] != ' ') sel.ex++; sel.e.x = sel.ex; sel.b.y = sel.e.y = sel.ey; selcopy(); } - } - } else if(b == 1) - selcopy(); + } else + selcopy(); + } memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval)); gettimeofday(&sel.tclick1, NULL); draw(); @@ -593,15 +606,18 @@ brelease(XEvent *e) { void bmotion(XEvent *e) { + if(IS_SET(MODE_MOUSE)) { + mousereport(e); + return; + } if(sel.mode) { - int oldey = sel.ey, - oldex = sel.ex; + int oldey = sel.ey, oldex = sel.ex; getbuttoninfo(e, NULL, &sel.ex, &sel.ey); if(oldey != sel.ey || oldex != sel.ex) { int starty = MIN(oldey, sel.ey); int endy = MAX(oldey, sel.ey); - drawregion(0, (starty > 0 ? starty : 0), term.col, (sel.ey < term.row ? endy+1 : term.row)); + drawregion(0, (starty > 0 ? starty : 0), term.col, (endy < term.row ? endy+1 : term.row)); } } } @@ -753,7 +769,7 @@ tnew(int col, int row) { term.row = row, term.col = col; term.line = malloc(term.row * sizeof(Line)); term.alt = malloc(term.row * sizeof(Line)); - for(row = 0 ; row < term.row; row++) { + for(row = 0; row < term.row; row++) { term.line[row] = malloc(term.col * sizeof(Glyph)); term.alt [row] = malloc(term.col * sizeof(Glyph)); } @@ -1121,15 +1137,19 @@ csihandle(void) { term.c.state |= CURSOR_HIDE; break; case 1000: /* disable X11 xterm mouse reporting */ - term.mode &= ~MODE_MOUSE; + term.mode &= ~MODE_MOUSEBTN; + break; + case 1002: + term.mode &= ~MODE_MOUSEMOTION; break; case 1049: /* = 1047 and 1048 */ + case 47: case 1047: if(IS_SET(MODE_ALTSCREEN)) { tclearregion(0, 0, term.col-1, term.row-1); tswapscreen(); } - if(escseq.arg[0] == 1047) + if(escseq.arg[0] != 1049) break; case 1048: tcursor(CURSOR_LOAD); @@ -1189,16 +1209,20 @@ csihandle(void) { case 25: term.c.state &= ~CURSOR_HIDE; break; - case 1000: /* enable X11 xterm mouse reporting */ - term.mode |= MODE_MOUSE; + case 1000: /* 1000,1002: enable xterm mouse report */ + term.mode |= MODE_MOUSEBTN; + break; + case 1002: + term.mode |= MODE_MOUSEMOTION; break; case 1049: /* = 1047 and 1048 */ + case 47: case 1047: if(IS_SET(MODE_ALTSCREEN)) tclearregion(0, 0, term.col-1, term.row-1); else tswapscreen(); - if(escseq.arg[0] == 1047) + if(escseq.arg[0] != 1049) break; case 1048: tcursor(CURSOR_SAVE); @@ -1585,6 +1609,7 @@ void xinit(void) { XSetWindowAttributes attrs; Cursor cursor; + Window parent; if(!(xw.dpy = XOpenDisplay(NULL))) die("Can't open display\n"); @@ -1602,8 +1627,8 @@ xinit(void) { xloadcols(); /* window - default size */ - xw.bufh = 24 * xw.ch; - xw.bufw = 80 * xw.cw; + xw.bufh = term.row * xw.ch; + xw.bufw = term.col * xw.cw; xw.h = xw.bufh + 2*BORDER; xw.w = xw.bufw + 2*BORDER; @@ -1612,10 +1637,12 @@ xinit(void) { attrs.bit_gravity = NorthWestGravity; attrs.event_mask = FocusChangeMask | KeyPressMask | ExposureMask | VisibilityChangeMask | StructureNotifyMask - | PointerMotionMask | ButtonPressMask | ButtonReleaseMask; + | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask + | EnterWindowMask | LeaveWindowMask; attrs.colormap = xw.cmap; - xw.win = XCreateWindow(xw.dpy, XRootWindow(xw.dpy, xw.scr), 0, 0, + parent = opt_embed ? strtol(opt_embed, NULL, 0) : XRootWindow(xw.dpy, xw.scr); + xw.win = XCreateWindow(xw.dpy, parent, 0, 0, xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput, XDefaultVisual(xw.dpy, xw.scr), CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask @@ -1795,7 +1822,7 @@ xseturgency(int add) { void focus(XEvent *ev) { - if(ev->type == FocusIn) { + if(ev->type == FocusIn || ev->type == EnterNotify) { xw.state |= WIN_FOCUSED; xseturgency(0); } else @@ -1922,6 +1949,9 @@ main(int argc, char *argv[]) { case 'c': if(++i < argc) opt_class = argv[i]; break; + case 'w': + if(++i < argc) opt_embed = argv[i]; + break; case 'e': /* eat every remaining arguments */ if(++i < argc) opt_cmd = &argv[i];