Subject: FVWM: PeriodicTasks bug?

From: [email-masked]
Date: Fri, 04 Feb 2005 14:38:59 -0800

While I was playing around with some ideas for FvwmScript, I noticed an
odd behavior that I was unable to explain by reading the man pages. I
suspect its a bug tickled by improper usage, but I'll leave that up to
you folks to decide. I've written and attached a small test script that
shows the problem.

Basically by my understanding, the attached script should advance the
red background one button every 5 seconds. However what actually happens
is that for one second out of 5 the red background cycles as fast as my
machine can make it go.

If I add a "Title" line to each widget, the script works predictably and
only advances once every 5 seconds. Seems like the interpreter doesn't
like empty buttons too well.

I have tested this with the most current build:

fvwm 2.5.13 (snap-20050204) compiled on Feb  4 2005 at 13:33:45
with support for: ReadLine, XPM, PNG, Shape, XShm, SM, Xinerama,
XRender, XFT, NLS

Regards,
Fred.

P.S.: There is a typo in the man page for FvwmScript where
"PeriodicTasks" is spelled "Periodic Task" in the example.

--- Begin script ---

WindowTitle {ScriptSilon}
WindowSize 150 50
WindowPosition 300 300
Font "Shadow=1:xft:Verdana:style=Regular:pixelsize=12:minspace=true"
BackColor {rgb:77/77/77}

Init
 Begin
  Set $NewLight=1
 End

PeriodicTasks
 Begin
   If (RemainderOfDiv (GetTime) 5)==0 Then
   Begin
     Set $OldLight=$NewLight
     Set $NewLight=(Add $NewLight 1)
     If $NewLight==4 Then
       Set $NewLight=1
     ChangeTitle 4 $NewLight
     ChangeBackColor $OldLight {white}
     ChangeBackColor $NewLight {red}
   End
 End

Widget 1
Property
 Size 10 18
 Position 0 0
 Type PushButton
 BackColor {White}
Main
 Case message of
  SingleClic :
  Begin
    ChangeBackColor 1 {rgb:00/00/ff}
  End
End

Widget 2
Property
 Size 10 18
 Position 30 0
 Type PushButton
 BackColor {white}
Main
 Case message of
  SingleClic :
  Begin
    ChangeBackColor 2 {rgb:00/00/ff}
  End
End

Widget 3
Property
 Size 10 18
 Position 60 0
 Type PushButton
 BackColor {white}
Main
 Case message of
  SingleClic :
  Begin
    ChangeBackColor 3 {rgb:00/00/ff}
  End
End

Widget 4
Property
 Size 10 18
 Position 90 0
 Type PushButton
 Title {1}
 BackColor {white}
Main
End

--
Visit the official FVWM web page at <URL: http://www.fvwm.org/>.
To unsubscribe from the list, send "unsubscribe fvwm" in the body of a
message to [email-masked].
To report problems, send mail to [email-masked].

---

From: Bjoern Steinbrink <[email-masked]>
Date: Sat, 5 Feb 2005 03:52:59 +0100

Hello,

> like empty buttons too well.

FvwmScript contains a bug that executes the periodic tasks too often
when there are pending events. The following patch fixes this.

I'm only subscribed to the fvwm list, so please CC me if you're replying
to fvwm-workers only.

Bjoern

Index: modules/ChangeLog
===================================================================
RCS file: /home/cvs/fvwm/fvwm/modules/ChangeLog,v
retrieving revision 1.1148
diff -u -u -r1.1148 ChangeLog
--- modules/ChangeLog	10 Jan 2005 14:59:24 -0000	1.1148
+++ modules/ChangeLog	5 Feb 2005 02:47:19 -0000
@@ -1,3 +1,9 @@
+2005-02-05  Bjoern Steinbrink <[email-masked]>
+
+	* FvwmScript/FvwmScript.c (MainLoop):
+	Fixed periodic tasks being executed too often when there are pending
+	events.
+
 2005-01-10  Olivier Chapuis  <[email-masked]>
 
 	* FvwmIconMan/xmanager.c (set_win_displaystring):
Index: modules/FvwmScript/FvwmScript.c
===================================================================
RCS file: /home/cvs/fvwm/fvwm/modules/FvwmScript/FvwmScript.c,v
retrieving revision 1.71
diff -u -u -r1.71 FvwmScript.c
--- modules/FvwmScript/FvwmScript.c	7 Jun 2004 13:53:50 -0000	1.71
+++ modules/FvwmScript/FvwmScript.c	5 Feb 2005 02:47:22 -0000
@@ -1092,6 +1092,8 @@
   int i;
   struct timeval tv;
   struct timeval *ptv;
+  struct timeval now, last_periodic;
+  long delta;
   fd_set_size_t fd_width = fd[1];
   Bool fsm_pending = False;
 
@@ -1102,7 +1104,10 @@
   tv.tv_usec = 0;
   ptv = NULL;
   if (x11base->periodictasks != NULL)
+  {
+    gettimeofday(&last_periodic, NULL);
     ptv = &tv;
+  }
 
   while ( !isTerminated )
   {
@@ -1114,6 +1119,10 @@
     FD_SET(fd[1],&in_fdset);
     fsm_fdset(&in_fdset);
 
+    gettimeofday(&now, NULL);
+    delta = (now.tv_sec - last_periodic.tv_sec) * 1000000;
+    delta += now.tv_usec - last_periodic.tv_usec;
+
     if (fsm_pending)
     {
 	    tv.tv_sec  = 0;
@@ -1121,8 +1130,10 @@
     }
     else
     {
-	    tv.tv_sec = 1;
-	    tv.tv_usec = 0;
+	    tv.tv_usec = 1000000 - delta;
+	    if (tv.tv_usec < 0)
+	      tv.tv_usec = 0;
+	    tv.tv_sec = delta ? 0 : 1;
     }
 
     if (fvwmSelect(fd_width, &in_fdset, NULL, NULL, ptv) > 0)
@@ -1245,9 +1256,11 @@
       fsm_pending = fsm_process(&in_fdset);
     }
 
-    if (!isTerminated && x11base->periodictasks!=NULL)
+    if (!isTerminated && x11base->periodictasks!=NULL && delta >= 1000000)
     {
       /* Execution des taches periodics */
+      last_periodic.tv_sec = now.tv_sec;
+      last_periodic.tv_usec = now.tv_usec;
       ExecBloc(x11base->periodictasks);
       usleep(10000);
     }

---

From: Dan Espen <[email-masked]>
Date: Sat, 05 Feb 2005 11:41:12 -0500

> when there are pending events. The following patch fixes this.
...
> > "PeriodicTasks" is spelled "Periodic Task" in the example.

The patch has been applied and the man page corrected.

Thanks.
-- 
Dan Espen                           E-mail: [email-masked]