1

I use simple script to make dynamic status bar of cpu usage (see below). After starting fvwm it works but after 3-4 minutes the bar disappears! The cpu.sh is simple (see below). Without ChangeSize there is no crashing (but there is no dynamic bar, static only). The cpu.sh gives integer number because ChangeSize needs for integer number. What maybe reason of this crashing? I spent already two days and do not understand reason.

WindowTitle {Status}
WindowSize 120 30

##### Global Style
Font "xft:DejaVu Sans:size=8:bold"

Init
Begin
Set $probarColor = {#cccccc} 
##### Widgets
ChangeBackColor 1  $probarColor
ChangeBackColor 3  $probarColor
End

PeriodicTasks
Begin
##### CPU Status
If (RemainderOfDiv (GetTime) 2) == 0 Then
 Begin
Set $length =  (GetOutput {echo $(bash    $HOME/.fvwm/scripts/StaTux/cpu.sh)} 1 -1) 
ChangeSize 3 $length 3
 End
End

Widget      1
Property
Type        ItemDraw
Size        120 4
Position        0 0
Flags       NoFocus NoReliefString
Main
Case message of
End

Widget      2
Property
Type        ItemDraw
Size        118 2
Position        1 1
Flags       NoFocus NoReliefString
Main
Case message of
End

Widget      3
Property
Type        ItemDraw
Size        118 2
Position        1 1
Flags       NoFocus NoReliefString
Main
Case message of
End

cpu.sh

#!/bin/sh
DELAY=${1:-1}
{ cat /proc/stat; sleep "$DELAY"; cat /proc/stat; } | awk '/^cpu / {usr=$2-usr; sys=$4-sys; idle=$5-idle; iow=$6-iow} END {total=usr+sys+idle+iow; print int((total-idle)*118/total)}'
nail
  • 11
  • 2
  • You are not calling `cpu.sh` with an argument, so DELAY=-1, and `sleep -1` is no sleep at all, so 2 successive cats of `/proc/stat` might give the same results twice, so the total will be 0 and you will have an awk division by 0 with no output on stdout. – meuh Jul 12 '18 at 08:16
  • @meuh I changed "$DELAY" to 1 or "1" with the same result - after some time the bar disappeared. – nail Jul 12 '18 at 10:26
  • @meuh I think I found the reason of crushing. It is crushed if we have zero size: ChangeSize 3 0 2 is crashed. – nail Jul 12 '18 at 14:09
  • Yes, you are right. I was just trying your script and when it crashed cpu.sh had returned 0 and there was an X Error in the stderr of fvwm: *Error: 2 (BadValue (integer parameter out of range for operation)) Major opcode of failed request: 12 (ConfigureWindow)* and a core dump. – meuh Jul 12 '18 at 14:55
  • @meuh Thank you for confirming. I saw this error in .xsession-errors. But I was confused by "integer parameter". I saw discussion in fvwm forum that ChangeSize needs for integer parameter and thought that this error due to this integer parameter. There is also official information "FvwmScript crashes if widgets are accessed that have not been defined." Maybe it means that for zero size the widget is not defined? – nail Jul 13 '18 at 01:00
  • Note, you will probably have less problems if you use a widget type of `HDipstick` which corresponds more to what you want to display, a bar of varying length. If you use it make sure to set the `MinValue` and `MaxValue`, in your case to 0 and 118. – meuh Jul 13 '18 at 15:22
  • @meuh Thank you, I did not know about this widget. But I do not understand how to use it. Variable part of this widget is Value but if I use Value = $var in widget and then try to change it in PeriodicTasks like Set $var = 50 it does not work. – nail Jul 14 '18 at 17:10

1 Answers1

0

As discussed in the comments, it seems FvwmScript crashes due to an X error BadValue when the value returned by the shell script is 0. This is probably because it is trying to configure the window to a size of 0.

An alternative solution is to use the HDipstick widget, which is a horizontal bar in a box of fixed length. Here is a short but complete example:

WindowTitle    {Status}
WindowSize     120 30
WindowPosition 900 1
Init
 Begin
  ChangeBackColor 3 {#000}
 End

PeriodicTasks
Begin
 If (RemainderOfDiv (GetTime) 2) == 0 Then
  Begin
   Set $length = (GetOutput {bash $HOME/.fvwm/scripts/StaTux/cpu.sh} 1 -1)
   ChangeValue 3 $length
 End
End

Widget 3
Property
 Type        HDipstick
 Value       0
 MinValue    0
 MaxValue    200
 ForeColor   {#f00}
 Size        110 20
 Position    5 5
Main
 Case message of
 End

The MinValue and MaxValue set the expected limits on the number you are getting back from your script. I chose 200 arbitrarily. The PeriodicTasks call of your script gets this number and uses ChangeValue to set the Value property to it. It will be scaled by dividing by 200 and multiplying by the width of the horizontal bar, given here in Size as 110 pixels.

When playing with this I noticed a bug in my version of FvwmScript which would draw a full bar for certain low values. For example, setting a MaxValue of 700 meant values 7 to 26 gave a full bar.
enter image description here

meuh
  • 49,672
  • 2
  • 52
  • 114
  • Yes, it works. I did not know that ChangeValue changes parameter Value. I did not find in man of HDipstick how possible to change geometry of bar. If I set Size 118 1 the bar does not change. For any height it has the same form. – nail Jul 14 '18 at 19:03
  • It says the minimum size is 30 by 11. – meuh Jul 14 '18 at 19:07
  • ![Valid XHTML](https://drive.google.com/open?id=1bXqCaot0JrvKv67-8m9LZNZptGBbKzhU) – nail Jul 15 '18 at 21:02
  • Whats a pity. I prefer thin bars ![Valid XHTML](https://drive.google.com/open?id=1bXqCaot0JrvKv67-8m9LZNZptGBbKzhU). Another observation with this script - it must be loaded last. I use dropbox and it is loaded longest and delete bar if they are and I need to restart fvwm do get bars. For this reason I use delay + I Schedule 30000 Module FvwmScript [FVWM_USERDIR]/scripts/StaTux/Status – nail Jul 15 '18 at 21:12
  • 1
    Appears another problem. I use Mathematica ant it occupies 4 cpu for 100% long time and appears division on zero. I solved all problems with zero and division on zero by changing total=usr+sys+idle+iow; print int((total-idle)*118/total) to total=1+usr+sys+idle+iow; print int(1+(total-idle)*117/total). Now it works correct without zero length and division on zero. – nail Jul 22 '18 at 23:33