"Cool CPU during config" patch

Annick et Jean-Philippe <[email protected]>
Newsgroups gmane.games.torcs.devel
Message-ID <[email protected]>
Hi, all.

Since I've been playing with Torcs, I've always been asking why
my CPU fan was making so much noise (after a while) while I was only
moving around in the configuration screens ... OK during the race, to get
as many frames as possible per second, but in config screens ...

So this is a small patch that puts the CPU to sleep for a (very short) while
each time it executes the glut idle functions that are used in config screens.
Of course (I had a hard time to make it, though ;-), no such rest
in the race screen !

I find it usefull in the following ways :
- more silence during configuration screens,
- globally cooler CPU during game, as it can have a breath
   between races, even for some small seconds,
- energy efficiency (in these days of global warming ... ;-)

To apply it :

cd <the directory that contains src>
patch -p1 < <...>/coolCPUDuringConfig.patch

Tested under Linux (x86-64 GCC 4.2) and Windows XP SP2 (VC 6 SP 6).

Please, let me know if you find it usefull, and tell me about improvements
you would think of ... And especially what you think about the sleep delay
I put in libs/tgfclient/gui.cpp::GfuiIdleCPUCooler (and other Idle* functions
in libs/confscreens/... and the fact that it is a hard-coded constant :
of course, it's bad, ... but, to go further, should it be variable
for any reason ? I can't see any but I may miss something.

Thanks in advance.

Jean-Philippe.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

_______________________________________________
Torcs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/torcs-devel
coolCPUDuringConfig.patch (text/x-patch, 9.5 KB)
diff -Naur current/src/libs/confscreens/controlconfig.cpp coolCPUDuringConfig/src/libs/confscreens/controlconfig.cpp
--- current/src/libs/confscreens/controlconfig.cpp	2008-03-21 22:09:11.000000000 +0100
+++ coolCPUDuringConfig/src/libs/confscreens/controlconfig.cpp	2008-03-24 11:15:42.000000000 +0100
@@ -208,7 +208,7 @@
 	GfParmSetStr(PrefHdle, CurrentSection, Cmd[CurrentCmd].name, name);
     }
 
-    glutIdleFunc(GfuiIdle);
+    GfuiSetScreenIdleFunc(scrHandle, GfuiIdleCPUCooler);
     InputWaited = 0;
     updateButtonText();
     return 1;
@@ -227,7 +227,7 @@
     Cmd[CurrentCmd].ref.type = GFCTRL_TYPE_SKEYBOARD;
     GfParmSetStr(PrefHdle, CurrentSection, Cmd[CurrentCmd].name, name);
 
-    glutIdleFunc(GfuiIdle);
+    GfuiSetScreenIdleFunc(scrHandle, GfuiIdleCPUCooler);
     InputWaited = 0;
     updateButtonText();
     return 1;
@@ -258,12 +258,14 @@
     char	*str;
     int		axis;
 
+    ulMilliSecondSleep(10); // Let CPU keep cool.
+
     GfctrlMouseGetCurrent(&mouseInfo);
 
     /* Check for a mouse button pressed */
     for (i = 0; i < 3; i++) {
 	if (mouseInfo.edgedn[i]) {
-	    glutIdleFunc(GfuiIdle);
+	    GfuiSetScreenIdleFunc(scrHandle, GfuiIdleCPUCooler);
 	    InputWaited = 0;
 	    str = GfctrlGetNameByRef(GFCTRL_TYPE_MOUSE_BUT, i);
 	    Cmd[CurrentCmd].ref.index = i;
@@ -277,7 +279,7 @@
     /* Check for a mouse axis moved */
     for (i = 0; i < 4; i++) {
 	if (mouseInfo.ax[i] > 20.0) {
-	    glutIdleFunc(GfuiIdle);
+	    GfuiSetScreenIdleFunc(scrHandle, GfuiIdleCPUCooler);
 	    InputWaited = 0;
 	    str = GfctrlGetNameByRef(GFCTRL_TYPE_MOUSE_AXIS, i);
 	    Cmd[CurrentCmd].ref.index = i;
@@ -297,7 +299,7 @@
 	    for (i = 0, mask = 1; i < 32; i++, mask *= 2) {
 		if (((b & mask) != 0) && ((rawb[index] & mask) == 0)) {
 		    /* Button i fired */
-		    glutIdleFunc(GfuiIdle);
+		    GfuiSetScreenIdleFunc(scrHandle, GfuiIdleCPUCooler);
 		    InputWaited = 0;
 		    str = GfctrlGetNameByRef(GFCTRL_TYPE_JOY_BUT, i + 32 * index);
 		    Cmd[CurrentCmd].ref.index = i + 32 * index;
@@ -315,7 +317,7 @@
     /* detect joystick movement */
     axis = getMovedAxis();
     if (axis != -1) {
-	glutIdleFunc(GfuiIdle);
+	GfuiSetScreenIdleFunc(scrHandle, GfuiIdleCPUCooler);
 	InputWaited = 0;
 	Cmd[CurrentCmd].ref.type = GFCTRL_TYPE_JOY_AXIS;
 	Cmd[CurrentCmd].ref.index = axis;
@@ -340,7 +342,8 @@
     if (Cmd[CurrentCmd].keyboardPossible) {
 	InputWaited = 1;
     }
-    glutIdleFunc(Idle);
+    GfuiSetScreenIdleFunc(scrHandle, Idle);
+
     GfctrlMouseInitCenter();
     memset(&mouseInfo, 0, sizeof(mouseInfo));
     GfctrlMouseGetCurrent(&mouseInfo);
diff -Naur current/src/libs/confscreens/joystickconfig.cpp coolCPUDuringConfig/src/libs/confscreens/joystickconfig.cpp
--- current/src/libs/confscreens/joystickconfig.cpp	2008-03-21 22:09:11.000000000 +0100
+++ coolCPUDuringConfig/src/libs/confscreens/joystickconfig.cpp	2008-03-24 11:08:22.000000000 +0100
@@ -137,6 +137,8 @@
     int		b, i;
     int		index;
 
+    ulMilliSecondSleep(10); // Let CPU keep cool.
+
     for (index = 0; index < NUM_JOY; index++) {
 	if (js[index]) {
 	    js[index]->read(&b, &ax[index * MAX_AXES]);
@@ -147,7 +149,7 @@
 		    /* Button fired */
 		    JoyCalAutomaton();
 		    if (CalState >= NB_STEPS) {
-			glutIdleFunc(GfuiIdle);
+			GfuiSetScreenIdleFunc(scrHandle2, GfuiIdleCPUCooler);
 		    }
 		    glutPostRedisplay();
 		    rawb[index] = b;
@@ -169,7 +171,7 @@
     
     CalState = 0;
     GfuiLabelSetText(scrHandle2, InstId, Instructions[CalState]);
-    glutIdleFunc(Idle2);
+    GfuiSetScreenIdleFunc(scrHandle2, Idle2);
     glutPostRedisplay();
     for (index = 0; index < NUM_JOY; index++) {
 	if (js[index]) {
diff -Naur current/src/libs/confscreens/mouseconfig.cpp coolCPUDuringConfig/src/libs/confscreens/mouseconfig.cpp
--- current/src/libs/confscreens/mouseconfig.cpp	2008-03-21 22:09:11.000000000 +0100
+++ coolCPUDuringConfig/src/libs/confscreens/mouseconfig.cpp	2008-03-24 11:07:33.000000000 +0100
@@ -111,9 +111,9 @@
     CalState = GetNextAxis();
     GfuiLabelSetText(scrHandle2, InstId, Instructions[CalState]);
     if (CalState < 4) {
-	glutIdleFunc(Idle2);
+	GfuiSetScreenIdleFunc(scrHandle2, Idle2);
     } else {
-	glutIdleFunc(GfuiIdle);
+	GfuiSetScreenIdleFunc(scrHandle2, GfuiIdleCPUCooler);
     }
 }
 
@@ -122,6 +122,8 @@
 {
     int	i;
 
+    ulMilliSecondSleep(1); // Let CPU keep cool.
+
     GfctrlMouseGetCurrent(&mouseInfo);
 
     /* Check for a mouse button pressed */
@@ -140,7 +142,7 @@
     memset(&mouseInfo, 0, sizeof(mouseInfo));
     GfctrlMouseGetCurrent(&mouseInfo);
     GfctrlMouseInitCenter();
-    glutIdleFunc(Idle2);
+    GfuiSetScreenIdleFunc(scrHandle2, Idle2);
 }
 
 static void
@@ -153,7 +155,7 @@
     GetNextAxis();
     GfuiLabelSetText(scrHandle2, InstId, Instructions[CalState]);
     if (CalState < 4) {
-	glutIdleFunc(IdleMouseInit);
+	GfuiSetScreenIdleFunc(scrHandle2, IdleMouseInit);
 	GfctrlMouseCenter();
     }
 }
diff -Naur current/src/libs/raceengineclient/racegl.cpp coolCPUDuringConfig/src/libs/raceengineclient/racegl.cpp
--- current/src/libs/raceengineclient/racegl.cpp	2008-02-24 17:51:28.000000000 +0100
+++ coolCPUDuringConfig/src/libs/raceengineclient/racegl.cpp	2008-03-24 00:12:56.000000000 +0100
@@ -173,6 +173,10 @@
 
     reScreenHandle = GfuiScreenCreateEx(bgcolor, 0, reScreenActivate, 0, 0, 0);
 
+    // The default glut idle function (GfuiIdleCPUCooler) is not suitable
+    // for race time : we want the CPU to work restlessly !
+    GfuiSetScreenIdleFunc(reScreenHandle, GfuiIdleNoCPURest);
+
     reAddKeys();
 
     reMsgId = GfuiLabelCreateEx(reScreenHandle,
diff -Naur current/src/libs/tgfclient/gui.cpp coolCPUDuringConfig/src/libs/tgfclient/gui.cpp
--- current/src/libs/tgfclient/gui.cpp	2008-02-24 17:51:28.000000000 +0100
+++ coolCPUDuringConfig/src/libs/tgfclient/gui.cpp	2008-03-24 00:02:42.000000000 +0100
@@ -109,11 +109,12 @@
 {
 }
 
-/** Idle function for the GUI to be called during Idle loop of glut.
+/** Idle function for the GUI to be called during Idle loop of glut
+    with mouse events management, and no care about CPU overuse
     @ingroup	gui
  */
 void
-GfuiIdle(void)
+GfuiIdleNoCPURest(void)
 {
 	double		curtime = GfTimeClock();
 	
@@ -129,6 +130,18 @@
 	}
 }
 
+/** Idle function for the GUI to be called during Idle loop of glut
+    with mouse events management, and care about CPU cooling
+    @ingroup	gui
+ */
+void
+GfuiIdleCPUCooler(void)
+{
+	ulMilliSecondSleep(10); // Let CPU keep/become cool.
+
+	GfuiIdleNoCPURest();
+}
+
 /** Display function for the GUI to be called during redisplay of glut.
     @ingroup	gui
 */
@@ -459,7 +472,7 @@
 	glutMouseFunc(gfuiMouse);
 	glutMotionFunc(gfuiMotion);
 	glutPassiveMotionFunc(gfuiPassiveMotion);
-	glutIdleFunc(GfuiIdle);
+	glutIdleFunc(GfuiScreen->idleFunc);
 	
 	if (GfuiScreen->onlyCallback == 0) {
 		if (GfuiScreen->hasFocus == NULL) {
@@ -545,6 +558,8 @@
 	screen->mouseColor[1] = &(GfuiColor[GFUI_MOUSECOLOR2][0]);
 	screen->mouseAllowed = 1;
 	
+	screen->idleFunc = GfuiIdleCPUCooler;
+
 	return (void*)screen;
 }
 
@@ -601,9 +616,28 @@
 	
 	screen->mouseAllowed = mouseAllowed;
 	
+	screen->idleFunc = GfuiIdleCPUCooler;
+
 	return (void*)screen;
 }
 
+/** Set the glut idle function for the given screen
+    and register it in glut if it is active
+    @ingroup	gui
+    @param	screen	Screen to modify
+    @param	idle	The new glut idle function
+ */
+void 
+GfuiSetScreenIdleFunc(void *scr, tfuiIdleFunc idle)
+{
+	tGfuiScreen *screen = (tGfuiScreen*)scr;
+
+	screen->idleFunc = idle;
+	if (GfuiScreenIsActive(screen))
+		glutIdleFunc(screen->idleFunc);
+}
+
+
 /** Release the given screen.
     @ingroup	gui
     @param	scr	Screen to release
@@ -680,6 +714,8 @@
 	screen->userActData = userDataOnActivate;
 	screen->onlyCallback = 1;
 	
+	screen->idleFunc = GfuiIdleCPUCooler;
+
 	return (void*)screen;
 }
 
diff -Naur current/src/libs/tgfclient/gui.h coolCPUDuringConfig/src/libs/tgfclient/gui.h
--- current/src/libs/tgfclient/gui.h	2008-02-24 17:51:28.000000000 +0100
+++ coolCPUDuringConfig/src/libs/tgfclient/gui.h	2008-03-24 10:46:45.000000000 +0100
@@ -228,6 +228,9 @@
     tfuiKeyCallback	onKeyAction;
     tfuiSKeyCallback	onSKeyAction;
 
+    /* glut idle function */
+    tfuiIdleFunc	idleFunc;
+
     /* mouse handling */
     int			mouse;
     int			mouseAllowed;
diff -Naur current/src/libs/tgfclient/tgfclient.h coolCPUDuringConfig/src/libs/tgfclient/tgfclient.h
--- current/src/libs/tgfclient/tgfclient.h	2008-03-08 16:36:34.000000000 +0100
+++ coolCPUDuringConfig/src/libs/tgfclient/tgfclient.h	2008-03-24 10:57:39.000000000 +0100
@@ -100,6 +100,7 @@
     void	*userData;	/**< Associated user data */
 } tScrollBarInfo;
 
+typedef void (*tfuiIdleFunc)(void);
 typedef void (*tfuiCallback)(void * /* userdata */);
 typedef void (*tfuiSBCallback)(tScrollBarInfo *);
 typedef int (*tfuiKeyCallback)(unsigned char key, int modifier, int state); /**< return 1 to prevent normal key computing */
@@ -112,7 +113,8 @@
 /* after a call to GfuiActivateScreen       */
 extern void GfuiDisplay(void);
 extern void GfuiDisplayNothing(void);
-extern void GfuiIdle(void);
+extern void GfuiIdleNoCPURest(void);
+extern void GfuiIdleCPUCooler(void);
 
 /* Screen management */
 extern void *GfuiScreenCreate(void);
@@ -125,6 +127,7 @@
 extern int  GfuiScreenIsActive(void *screen);
 extern void GfuiScreenReplace(void *screen);
 extern void GfuiScreenDeactivate(void);
+extern void GfuiSetScreenIdleFunc(void *screen, tfuiIdleFunc idle);
 extern void *GfuiHookCreate(void *userDataOnActivate, tfuiCallback onActivate);
 extern void GfuiHookRelease(void *hook);
 extern void GfuiAddKey(void *scr, unsigned char key, char *descr, void *userData, tfuiCallback onKeyPressed, tfuiCallback onKeyReleased);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.