aboutsummaryrefslogtreecommitdiff
path: root/st.c
diff options
context:
space:
mode:
authorChristoph Lohmann <20h@r-36.net>2013-02-15 19:10:22 +0100
committerChristoph Lohmann <20h@r-36.net>2013-02-15 19:10:22 +0100
commit95033753be32e93915ddce14ea41b8765b665771 (patch)
tree5c4f36908a54eaff9db4b03d87df96401ddef245 /st.c
parentb7261c84aa3af984d5a7e5f5239c4173255a215d (diff)
downloadst-95033753be32e93915ddce14ea41b8765b665771.tar.gz
st-95033753be32e93915ddce14ea41b8765b665771.zip
Adding a more efficient drawing code.
Thanks Mihail Zenkov <mihail.zenkov@gmail.com> for giving the hint!
Diffstat (limited to 'st.c')
-rw-r--r--st.c65
1 files changed, 38 insertions, 27 deletions
diff --git a/st.c b/st.c
index 8206001..f4b419e 100644
--- a/st.c
+++ b/st.c
@@ -3166,10 +3166,12 @@ void
3166run(void) { 3166run(void) {
3167 XEvent ev; 3167 XEvent ev;
3168 fd_set rfd; 3168 fd_set rfd;
3169 int xfd = XConnectionNumber(xw.dpy), i; 3169 int xfd = XConnectionNumber(xw.dpy);
3170 struct timeval drawtimeout, *tv = NULL; 3170 struct timeval drawtimeout, *tv = NULL, now, last;
3171 3171
3172 for(i = 0;; i++) { 3172 gettimeofday(&last, NULL);
3173
3174 for(;;) {
3173 FD_ZERO(&rfd); 3175 FD_ZERO(&rfd);
3174 FD_SET(cmdfd, &rfd); 3176 FD_SET(cmdfd, &rfd);
3175 FD_SET(xfd, &rfd); 3177 FD_SET(xfd, &rfd);
@@ -3179,35 +3181,44 @@ run(void) {
3179 die("select failed: %s\n", SERRNO); 3181 die("select failed: %s\n", SERRNO);
3180 } 3182 }
3181 3183
3182 /* 3184 gettimeofday(&now, NULL);
3183 * Stop after a certain number of reads so the user does not 3185 /* usecs until (next) frame */
3184 * feel like the system is stuttering. 3186 drawtimeout.tv_sec = 0;
3185 */ 3187 drawtimeout.tv_usec = \
3186 if(i < 1000 && FD_ISSET(cmdfd, &rfd)) { 3188 ((1000/framespersecond) - TIMEDIFF(now, last)) * 1000;
3187 ttyread();
3188 3189
3189 /* 3190 /* Let us draw a frame. */
3190 * Just wait a bit so it isn't disturbing the 3191 if(drawtimeout.tv_usec <= 0) {
3191 * user and the system is able to write something. 3192 draw();
3192 */ 3193 XFlush(xw.dpy);
3193 drawtimeout.tv_sec = 0; 3194
3194 drawtimeout.tv_usec = 5; 3195 last = now;
3195 tv = &drawtimeout; 3196 tv = NULL;
3196 continue;
3197 } 3197 }
3198 i = 0;
3199 tv = NULL;
3200 3198
3201 while(XPending(xw.dpy)) { 3199 if(FD_ISSET(cmdfd, &rfd))
3202 XNextEvent(xw.dpy, &ev); 3200 ttyread();
3203 if(XFilterEvent(&ev, None)) 3201
3204 continue; 3202 if(FD_ISSET(xfd, &rfd)) {
3205 if(handler[ev.type]) 3203 while(XPending(xw.dpy)) {
3206 (handler[ev.type])(&ev); 3204 XNextEvent(xw.dpy, &ev);
3205 if(XFilterEvent(&ev, None))
3206 continue;
3207 if(handler[ev.type])
3208 (handler[ev.type])(&ev);
3209 }
3210
3211 if(drawtimeout.tv_usec <= 0) {
3212 draw();
3213 XFlush(xw.dpy);
3214 }
3207 } 3215 }
3208 3216
3209 draw(); 3217 /* There is still some time to wait until next frame. */
3210 XFlush(xw.dpy); 3218 if(drawtimeout.tv_usec > 0) {
3219 tv = &drawtimeout;
3220 continue;
3221 }
3211 } 3222 }
3212} 3223}
3213 3224