Xinqi Bao's Git
projects
/
st.git
/ blobdiff
summary
|
log
|
commit
|
diff
|
tree
raw
|
inline
| side by side
use typedef'd unsigned type and regular bool type.
[st.git]
/
st.c
diff --git
a/st.c
b/st.c
index
d7ca875
..
35f6f16
100644
(file)
--- a/
st.c
+++ b/
st.c
@@
-48,7
+48,7
@@
#define ESC_ARG_SIZ 16
#define STR_BUF_SIZ 256
#define STR_ARG_SIZ 16
#define ESC_ARG_SIZ 16
#define STR_BUF_SIZ 256
#define STR_ARG_SIZ 16
-#define DRAW_BUF_SIZ 1024
+#define DRAW_BUF_SIZ
20*
1024
#define UTF_SIZ 4
#define XK_NO_MOD UINT_MAX
#define XK_ANY_MOD 0
#define UTF_SIZ 4
#define XK_NO_MOD UINT_MAX
#define XK_ANY_MOD 0
@@
-196,8
+196,9
@@
typedef struct {
XIM xim;
XIC xic;
int scr;
XIM xim;
XIC xic;
int scr;
-
B
ool isfixed; /* is fixed geometry? */
+
b
ool isfixed; /* is fixed geometry? */
int fx, fy, fw, fh; /* fixed geometry */
int fx, fy, fw, fh; /* fixed geometry */
+ int tw, th; /* tty width and height */
int w; /* window width */
int h; /* window height */
int ch; /* char height */
int w; /* window width */
int h; /* window height */
int ch; /* char height */
@@
-288,7
+289,6
@@
static void ttywrite(const char *, size_t);
static void xdraws(char *, Glyph, int, int, int, int);
static void xhints(void);
static void xclear(int, int, int, int);
static void xdraws(char *, Glyph, int, int, int, int);
static void xhints(void);
static void xclear(int, int, int, int);
-static void xcopy(void);
static void xdrawcursor(void);
static void xinit(void);
static void xloadcols(void);
static void xdrawcursor(void);
static void xinit(void);
static void xloadcols(void);
@@
-325,6
+325,7
@@
static int isfullutf8(char *, int);
static void *xmalloc(size_t);
static void *xrealloc(void *, size_t);
static void *xmalloc(size_t);
static void *xrealloc(void *, size_t);
+static void *xcalloc(size_t nmemb, size_t size);
static void (*handler[LASTEvent])(XEvent *) = {
[KeyPress] = kpress,
static void (*handler[LASTEvent])(XEvent *) = {
[KeyPress] = kpress,
@@
-363,14
+364,22
@@
void *
xmalloc(size_t len) {
void *p = malloc(len);
if(!p)
xmalloc(size_t len) {
void *p = malloc(len);
if(!p)
- die("Out of memory");
+ die("Out of memory
\n
");
return p;
}
void *
xrealloc(void *p, size_t len) {
if((p = realloc(p, len)) == NULL)
return p;
}
void *
xrealloc(void *p, size_t len) {
if((p = realloc(p, len)) == NULL)
- die("Out of memory");
+ die("Out of memory\n");
+ return p;
+}
+
+void *
+xcalloc(size_t nmemb, size_t size) {
+ void *p = calloc(nmemb, size);
+ if(!p)
+ die("Out of memory\n");
return p;
}
return p;
}
@@
-562,8
+571,11
@@
bpress(XEvent *e) {
if(IS_SET(MODE_MOUSE))
mousereport(e);
else if(e->xbutton.button == Button1) {
if(IS_SET(MODE_MOUSE))
mousereport(e);
else if(e->xbutton.button == Button1) {
- if(sel.bx != -1)
+ if(sel.bx != -1) {
+ sel.bx = -1;
tsetdirt(sel.b.y, sel.e.y);
tsetdirt(sel.b.y, sel.e.y);
+ draw();
+ }
sel.mode = 1;
sel.ex = sel.bx = X2COL(e->xbutton.x);
sel.ey = sel.by = Y2ROW(e->xbutton.y);
sel.mode = 1;
sel.ex = sel.bx = X2COL(e->xbutton.x);
sel.ey = sel.by = Y2ROW(e->xbutton.y);
@@
-585,14
+597,17
@@
selcopy(void) {
/* append every set & selected glyph to the selection */
for(y = 0; y < term.row; y++) {
for(x = 0; x < term.col; x++) {
/* append every set & selected glyph to the selection */
for(y = 0; y < term.row; y++) {
for(x = 0; x < term.col; x++) {
- is_selected = selected(x, y);
- if((term.line[y][x].state & GLYPH_SET) && is_selected) {
- int size = utf8size(term.line[y][x].c);
- memcpy(ptr, term.line[y][x].c, size);
- ptr += size;
- }
+ int size;
+ char *p;
+ Glyph *gp = &term.line[y][x];
+
+ if(!(is_selected = selected(x, y)))
+ continue;
+ p = (gp->state & GLYPH_SET) ? gp->c : " ";
+ size = utf8size(p);
+ memcpy(ptr, p, size);
+ ptr += size;
}
}
-
/* \n at the end of every selected line except for the last one */
if(is_selected && y < sel.e.y)
*ptr++ = '\n';
/* \n at the end of every selected line except for the last one */
if(is_selected && y < sel.e.y)
*ptr++ = '\n';
@@
-635,7
+650,6
@@
void selclear(XEvent *e) {
return;
sel.bx = -1;
tsetdirt(sel.b.y, sel.e.y);
return;
sel.bx = -1;
tsetdirt(sel.b.y, sel.e.y);
- draw();
}
void
}
void
@@
-685,8
+699,6
@@
xsetsel(char *str) {
clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
clipboard = XInternAtom(xw.dpy, "CLIPBOARD", 0);
XSetSelectionOwner(xw.dpy, clipboard, xw.win, CurrentTime);
-
- XFlush(xw.dpy);
}
void
}
void
@@
-729,7
+741,6
@@
brelease(XEvent *e) {
}
memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
gettimeofday(&sel.tclick1, NULL);
}
memcpy(&sel.tclick2, &sel.tclick1, sizeof(struct timeval));
gettimeofday(&sel.tclick1, NULL);
- draw();
}
void
}
void
@@
-746,7
+757,6
@@
bmotion(XEvent *e) {
int starty = MIN(oldey, sel.ey);
int endy = MAX(oldey, sel.ey);
tsetdirt(starty, endy);
int starty = MIN(oldey, sel.ey);
int endy = MAX(oldey, sel.ey);
tsetdirt(starty, endy);
- draw();
}
}
}
}
}
}
@@
-885,8
+895,8
@@
ttyresize(int x, int y) {
w.ws_row = term.row;
w.ws_col = term.col;
w.ws_row = term.row;
w.ws_col = term.col;
- w.ws_xpixel = xw.w;
- w.ws_ypixel = xw.h;
+ w.ws_xpixel = xw.
t
w;
+ w.ws_ypixel = xw.
t
h;
if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
}
if(ioctl(cmdfd, TIOCSWINSZ, &w) < 0)
fprintf(stderr, "Couldn't set window size: %s\n", SERRNO);
}
@@
-921,7
+931,7
@@
tcursor(int mode) {
void
treset(void) {
void
treset(void) {
- u
nsigned
i;
+ u
int
i;
term.c = (TCursor){{
.mode = ATTR_NULL,
.fg = DefaultFG,
term.c = (TCursor){{
.mode = ATTR_NULL,
.fg = DefaultFG,
@@
-1584,7
+1594,7
@@
strreset(void) {
void
tputtab(bool forward) {
void
tputtab(bool forward) {
- u
nsigned
x = term.c.x;
+ u
int
x = term.c.x;
if(forward) {
if(x == term.col)
if(forward) {
if(x == term.col)
@@
-1804,8
+1814,8
@@
tresize(int col, int row) {
/* allocate any new rows */
for(/* i == minrow */; i < row; i++) {
term.dirty[i] = 1;
/* allocate any new rows */
for(/* i == minrow */; i < row; i++) {
term.dirty[i] = 1;
- term.line[i] = calloc(col, sizeof(Glyph));
- term.alt [i] = calloc(col, sizeof(Glyph));
+ term.line[i] =
x
calloc(col, sizeof(Glyph));
+ term.alt [i] =
x
calloc(col, sizeof(Glyph));
}
if(col > term.col) {
bool *bp = term.tabs + term.col;
}
if(col > term.col) {
bool *bp = term.tabs + term.col;
@@
-1828,8
+1838,8
@@
tresize(int col, int row) {
void
xresize(int col, int row) {
void
xresize(int col, int row) {
- xw.w = MAX(1, 2*BORDER + col * xw.cw);
- xw.h = MAX(1, 2*BORDER + row * xw.ch);
+ xw.
t
w = MAX(1, 2*BORDER + col * xw.cw);
+ xw.
t
h = MAX(1, 2*BORDER + row * xw.ch);
}
void
}
void
@@
-2091,13
+2101,6
@@
xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
}
XDrawLine(xw.dpy, xw.buf, dc.gc, winx, winy+1, winx+width-1, winy+1);
}
-/* copy buffer pixmap to screen pixmap */
-void
-xcopy() {
- XdbeSwapInfo swpinfo[1] = {{xw.win, XdbeCopied}};
- XdbeSwapBuffers(xw.dpy, swpinfo, 1);
-}
-
void
xdrawcursor(void) {
static int oldx = 0;
void
xdrawcursor(void) {
static int oldx = 0;
@@
-2118,8
+2121,6
@@
xdrawcursor(void) {
} else
xclear(oldx, oldy, oldx, oldy);
} else
xclear(oldx, oldy, oldx, oldy);
- xcopy();
-
/* draw the new one */
if(!(term.c.state & CURSOR_HIDE)) {
if(!(xw.state & WIN_FOCUSED))
/* draw the new one */
if(!(term.c.state & CURSOR_HIDE)) {
if(!(xw.state & WIN_FOCUSED))
@@
-2132,8
+2133,6
@@
xdrawcursor(void) {
xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
oldx = term.c.x, oldy = term.c.y;
}
xdraws(g.c, g, term.c.x, term.c.y, 1, sl);
oldx = term.c.x, oldy = term.c.y;
}
-
- xcopy();
}
void
}
void
@@
-2144,6
+2143,8
@@
xresettitle(void) {
void
redraw(void) {
struct timespec tv = {0, REDRAW_TIMEOUT * 1000};
void
redraw(void) {
struct timespec tv = {0, REDRAW_TIMEOUT * 1000};
+
+ xclear(0, 0, xw.w, xw.h);
tfulldirt();
draw();
XSync(xw.dpy, False); /* necessary for a good tput flash */
tfulldirt();
draw();
XSync(xw.dpy, False); /* necessary for a good tput flash */
@@
-2152,8
+2153,10
@@
redraw(void) {
void
draw() {
void
draw() {
+ XdbeSwapInfo swpinfo[1] = {{xw.win, XdbeCopied}};
+
drawregion(0, 0, term.col, term.row);
drawregion(0, 0, term.col, term.row);
-
xcopy(
);
+
XdbeSwapBuffers(xw.dpy, swpinfo, 1
);
}
void
}
void
@@
-2208,7
+2211,6
@@
expose(XEvent *ev) {
if(!e->count)
xw.state &= ~WIN_REDRAW;
}
if(!e->count)
xw.state &= ~WIN_REDRAW;
}
- xcopy();
}
void
}
void
@@
-2241,7
+2243,6
@@
focus(XEvent *ev) {
xseturgency(0);
} else
xw.state &= ~WIN_FOCUSED;
xseturgency(0);
} else
xw.state &= ~WIN_FOCUSED;
- draw();
}
char*
}
char*
@@
-2317,7
+2318,6
@@
cmessage(XEvent *e) {
} else if(e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
xw.state &= ~WIN_FOCUSED;
}
} else if(e->xclient.data.l[1] == XEMBED_FOCUS_OUT) {
xw.state &= ~WIN_FOCUSED;
}
- draw();
}
}
}
}
@@
-2334,8
+2334,9
@@
resize(XEvent *e) {
row = (xw.h - 2*BORDER) / xw.ch;
if(col == term.col && row == term.row)
return;
row = (xw.h - 2*BORDER) / xw.ch;
if(col == term.col && row == term.row)
return;
- if(tresize(col, row))
- draw();
+
+ xclear(0, 0, xw.w, xw.h);
+ tresize(col, row);
xresize(col, row);
ttyresize(col, row);
}
xresize(col, row);
ttyresize(col, row);
}
@@
-2344,21
+2345,37
@@
void
run(void) {
XEvent ev;
fd_set rfd;
run(void) {
XEvent ev;
fd_set rfd;
- int xfd = XConnectionNumber(xw.dpy);
+ int xfd = XConnectionNumber(xw.dpy), i;
+ struct timeval drawtimeout, *tv = NULL;
- for(
;;
) {
+ for(
i = 0;; i++
) {
FD_ZERO(&rfd);
FD_SET(cmdfd, &rfd);
FD_SET(xfd, &rfd);
FD_ZERO(&rfd);
FD_SET(cmdfd, &rfd);
FD_SET(xfd, &rfd);
- if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL,
NULL
) < 0) {
+ if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL,
tv
) < 0) {
if(errno == EINTR)
continue;
die("select failed: %s\n", SERRNO);
}
if(errno == EINTR)
continue;
die("select failed: %s\n", SERRNO);
}
- if(FD_ISSET(cmdfd, &rfd))
+
+ /*
+ * Stop after a certain number of reads so the user does not
+ * feel like the system is stuttering.
+ */
+ if(i < 1000 && FD_ISSET(cmdfd, &rfd)) {
ttyread();
ttyread();
- draw();
+ /*
+ * Just wait a bit so it isn't disturbing the
+ * user and the system is able to write something.
+ */
+ drawtimeout.tv_sec = 0;
+ drawtimeout.tv_usec = 5;
+ tv = &drawtimeout;
+ continue;
+ }
+ i = 0;
+ tv = NULL;
while(XPending(xw.dpy)) {
XNextEvent(xw.dpy, &ev);
while(XPending(xw.dpy)) {
XNextEvent(xw.dpy, &ev);
@@
-2367,13
+2384,16
@@
run(void) {
if(handler[ev.type])
(handler[ev.type])(&ev);
}
if(handler[ev.type])
(handler[ev.type])(&ev);
}
+
+ draw();
+ XFlush(xw.dpy);
}
}
int
main(int argc, char *argv[]) {
int i, bitm, xr, yr;
}
}
int
main(int argc, char *argv[]) {
int i, bitm, xr, yr;
- u
nsigned
int wr, hr;
+ uint wr, hr;
xw.fw = xw.fh = xw.fx = xw.fy = 0;
xw.isfixed = False;
xw.fw = xw.fh = xw.fx = xw.fy = 0;
xw.isfixed = False;