Refactor the mainloop
Try to separate the different concerns of the main loop into separate,
simpler functions. I think it's a useful step, but I also think that
more should be done.
cheers
aes
From 015e8b45b18690d81e2505309753efaa8af9799d Mon Sep 17 00:00:00 2001
From: Anders Eurenius <[email protected]>
Date: Tue, 24 Jun 2014 23:34:06 +0200
Subject: [PATCH 6/8] Refactor the mainloop
Try to separate the different concerns of the main loop into separate,
simpler functions. I think it's a useful step, but I also think that
more should be done.
---
st.c | 114 ++++++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 61 insertions(+), 53 deletions(-)
diff --git a/st.c b/st.c
index 1621352..fc3af74 100644
--- a/st.c
+++ b/st.c
@@ -3752,12 +3752,10 @@ resize(XEvent *e) {
}
void
-run(void) {
+init(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) {
@@ -3772,32 +3770,55 @@ run(void) {
ttynew();
cresize(w, h);
+}
+
+const int xev_activity = 1;
+const int cmd_activity = 2;
+
+int do_select(struct timeval *tv)
+{
+ int xfd = XConnectionNumber(xw.dpy);
+ fd_set rfd;
+
+ FD_ZERO(&rfd);
+ FD_SET(cmdfd, &rfd);
+ FD_SET(xfd, &rfd);
+
+ while(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv) < 0) {
+ if(errno == EINTR)
+ continue;
+ die("select failed: %s\n", strerror(errno));
+ }
+
+ return (FD_ISSET(xfd, &rfd)? xev_activity: 0) |
+ (FD_ISSET(cmdfd, &rfd)? cmd_activity: 0);
+}
+
+void
+run(void) {
+ XEvent ev;
+ int s, xev, blinkset = 0, dodraw = 0;
+ struct timeval drawtimeout, *tv = NULL, now, last;
+
+ init();
gettimeofday(&last, NULL);
- lastblink = last;
+ xev = actionfps;
- for(xev = actionfps;;) {
+ for(;;) {
long deltatime;
- FD_ZERO(&rfd);
- FD_SET(cmdfd, &rfd);
- FD_SET(xfd, &rfd);
+ s = do_select(tv);
- if(select(MAX(xfd, cmdfd)+1, &rfd, NULL, NULL, tv) < 0) {
- if(errno == EINTR)
- continue;
- die("select failed: %s\n", strerror(errno));
- }
- if(FD_ISSET(cmdfd, &rfd)) {
+ if(s & cmd_activity) {
ttyread();
if(blinktimeout) {
- blinkset = tattrset(ATTR_BLINK);
- if(!blinkset)
- MODBIT(term.mode, 0, MODE_BLINK);
+ blinkset = tattrset(ATTR_BLINK) |
+ tattrset(ATTR_FASTBLINK);
}
}
- if(FD_ISSET(xfd, &rfd))
+ if(s & xev_activity)
xev = actionfps;
gettimeofday(&now, NULL);
@@ -3806,12 +3827,6 @@ run(void) {
tv = &drawtimeout;
dodraw = 0;
- if(blinktimeout && TIMEDIFF(now, lastblink) > blinktimeout) {
- tsetdirtattr(ATTR_BLINK);
- term.mode ^= MODE_BLINK;
- lastblink = now;
- dodraw = 1;
- }
deltatime = TIMEDIFF(now, last);
if(deltatime > (xev? (1000/xfps) : (1000/actionfps))
|| deltatime < 0) {
@@ -3819,35 +3834,28 @@ run(void) {
last = now;
}
- if(dodraw) {
- while(XPending(xw.dpy)) {
- XNextEvent(xw.dpy, &ev);
- if(XFilterEvent(&ev, None))
- continue;
- if(handler[ev.type])
- (handler[ev.type])(&ev);
- }
+ if(!dodraw)
+ continue;
- draw();
- XFlush(xw.dpy);
-
- if(xev && !FD_ISSET(xfd, &rfd))
- xev--;
- if(!FD_ISSET(cmdfd, &rfd) && !FD_ISSET(xfd, &rfd)) {
- if(blinkset) {
- if(TIMEDIFF(now, lastblink) \
- > blinktimeout) {
- drawtimeout.tv_usec = 1;
- } else {
- drawtimeout.tv_usec = (1000 * \
- (blinktimeout - \
- TIMEDIFF(now,
- lastblink)));
- }
- } else {
- tv = NULL;
- }
- }
+ while(XPending(xw.dpy)) {
+ XNextEvent(xw.dpy, &ev);
+ if(XFilterEvent(&ev, None))
+ continue;
+ if(handler[ev.type])
+ (handler[ev.type])(&ev);
+ }
+
+ draw();
+ XFlush(xw.dpy);
+
+ if(xev && !(s & xev_activity))
+ xev--;
+ if(blinkset) {
+ drawtimeout.tv_sec = 0;
+ drawtimeout.tv_usec = blinktimeout / 2;
+ tv = &drawtimeout;
+ } else {
+ tv = NULL;
}
}
}
--
2.0.0