diff -uNr rss-glx-0.8.1/debian/changelog rss-glx-0.8.1-fix-fps-limiter/debian/changelog --- rss-glx-0.8.1/debian/changelog 2008-08-27 15:47:43.000000000 +0300 +++ rss-glx-0.8.1-fix-fps-limiter/debian/changelog 2008-08-27 16:29:37.382945078 +0300 @@ -1,3 +1,9 @@ +rss-glx (0.8.1-8ubuntu4-jk1) hardy; urgency=low + + * Fix high CPU usage. + + -- Jussi Kivilinna Wed, 27 Aug 2008 15:49:11 +0200 + rss-glx (0.8.1-8ubuntu4) hardy; urgency=low * No-change rebuild against libmagick10. diff -uNr rss-glx-0.8.1/src/driver.cpp rss-glx-0.8.1-fix-fps-limiter/src/driver.cpp --- rss-glx-0.8.1/src/driver.cpp 2006-02-25 22:16:15.000000000 +0200 +++ rss-glx-0.8.1-fix-fps-limiter/src/driver.cpp 2008-08-27 20:00:43.431069739 +0300 @@ -39,12 +39,14 @@ extern char *hack_name; +#define DEFAULT_FPS 50 + /* * display parameters */ int rootWindow = False; -int frameTime = 0; -int be_nice = 0; +int frameTime = (DEFAULT_FPS == 0) ? 0 : (1000000 / DEFAULT_FPS); +int be_nice = (DEFAULT_FPS != 0) ? True : False; int signalled = 0; void createWindow (int argc, char **argv) @@ -148,13 +150,21 @@ } } +static double getSeconds(void) +{ + struct timeval now; + + gettimeofday (&now, NULL); + return (double)now.tv_sec + now.tv_usec / 1000000.0; +} + void mainLoop (void) { int bFPS = False; XEvent event; Atom XA_WM_PROTOCOLS = XInternAtom (XStuff->display, "WM_PROTOCOLS", False); Atom XA_WM_DELETE_WINDOW = XInternAtom (XStuff->display, "WM_DELETE_WINDOW", False); - struct timeval then, now, fps_time; + double currFrameTime, currFrameTimeSoFar, currFrameStartTime, prevFrameStartTime, now, fps_time; int fps = 0; if (!rootWindow) { @@ -163,10 +173,16 @@ clearBuffers(); - gettimeofday (&now, NULL); - int frameTimeSoFar = 0; + currFrameStartTime = getSeconds(); while (!signalled) { - hack_draw (XStuff, (double)now.tv_sec + now.tv_usec / 1000000.0f, frameTimeSoFar / 1000000.0f); + prevFrameStartTime = currFrameStartTime; + currFrameStartTime = getSeconds(); + currFrameTime = currFrameStartTime - prevFrameStartTime; + + if (currFrameTime < 0.0) + currFrameTime = 0.01; + + hack_draw (XStuff, currFrameStartTime, currFrameTime); glXSwapBuffers (XStuff->display, XStuff->window); @@ -174,16 +190,14 @@ if (fps != -1) fps++; - gettimeofday (&now, NULL); - - if (now.tv_sec > fps_time.tv_sec) { + now = getSeconds(); + if (now >= fps_time + 1.0) { if (fps != -1) { - printf ("%d fps\n", fps); + printf ("%.4f fps\n", fps * (now - fps_time)); } fps = 0; - fps_time.tv_sec = now.tv_sec; - fps_time.tv_usec = now.tv_usec; + fps_time = now; } } @@ -212,7 +226,7 @@ if (bFPS) { fps = -1; - gettimeofday (&fps_time, NULL); + fps_time = getSeconds(); } } @@ -232,12 +246,10 @@ } } - then = now; - gettimeofday (&now, NULL); - frameTimeSoFar = (now.tv_sec - then.tv_sec) * 1000000 + now.tv_usec - then.tv_usec; + currFrameTimeSoFar = getSeconds() - currFrameStartTime; - if (frameTime) { - while (frameTimeSoFar < frameTime) { + if (frameTime > 0) { + while (currFrameTimeSoFar * 1000000.0 < frameTime) { if (be_nice) { /* nanosleep on Linux/i386 seems completely ineffective for idling for < 20ms */ /* @@ -245,25 +257,27 @@ struct timespec hundreth; hundreth.tv_sec = 0; - hundreth.tv_nsec = frameTime - frameTimeSoFar; + hundreth.tv_nsec = frameTime - currFrameTimeSoFar * 1000000.0; nanosleep(&hundreth, NULL); #endif */ /* - usleep(frameTime - frameTimeSoFar); + usleep(frameTime - currFrameTimeSoFar * 1000000.0); */ struct timeval tv; tv.tv_sec = 0; - tv.tv_usec = frameTime - frameTimeSoFar; + tv.tv_usec = frameTime - currFrameTimeSoFar * 1000000.0; select (0, 0, 0, 0, &tv); } - gettimeofday (&now, NULL); - frameTimeSoFar = (now.tv_sec - then.tv_sec) * 1000000 + now.tv_usec - then.tv_usec; + currFrameTimeSoFar = getSeconds() - currFrameStartTime; + + if (signalled) + return; } } else if (be_nice) { struct timeval tv; @@ -283,13 +297,15 @@ break; case 'x': - c = strtol_minmaxdef (optarg, 10, 1, 10000, 1, 100, "--maxfps: "); + c = strtol_minmaxdef (optarg, 10, 0, 10000, 1, 100, "--maxfps: "); - frameTime = 1000000 / c; + frameTime = c ? 1000000 / c : 0; break; case 'n': - be_nice = 1; + c = strtol_minmaxdef (optarg, 10, 0, 1, 1, 1, NULL); + + be_nice = c; break; } @@ -297,7 +313,7 @@ int strtol_minmaxdef (char *optarg, int base, int min, int max, int type, int def, char *errmsg) { - int result = strtol (optarg, (char **)NULL, base); + int result = optarg ? strtol (optarg, (char **)NULL, base) : def; if (result < min) { if (errmsg) { diff -uNr rss-glx-0.8.1/src/driver.h rss-glx-0.8.1-fix-fps-limiter/src/driver.h --- rss-glx-0.8.1/src/driver.h 2005-04-10 01:02:03.000000000 +0300 +++ rss-glx-0.8.1-fix-fps-limiter/src/driver.h 2008-08-27 19:45:19.040249654 +0300 @@ -39,7 +39,7 @@ } xstuff_t; #define DRIVER_OPTIONS_LONG {"root", 0, 0, 'r'}, {"maxfps", 1, 0, 'x'}, {"nice", 0, 0, 'n'}, -#define DRIVER_OPTIONS_SHORT "rx:n" +#define DRIVER_OPTIONS_SHORT "rx:n:" #define DRIVER_OPTIONS_HELP "\t--root/-r\n" "\t--maxfps/-x \n" "\t--nice/-n\n" #define DRIVER_OPTIONS_CASES case 'r': case 'x': case 'n': handle_global_opts(c); break;