gtkdialog timer file-monitor doesn't work

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
User avatar
BarryK
Posts: 2316
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 98 times
Been thanked: 583 times

gtkdialog timer file-monitor doesn't work

Post by BarryK »

Or rather, it doesn't work for me, the way I am trying to do it, which, as far as I can see, is exactly as the documentations says.

I have reported the problem today on my blog:

https://bkhome.org/news/202010/problem- ... idget.html

Here is the code:

Code: Select all

  <timer visible="false" file-monitor="true" interval="1">
    <variable>TIMERNEW</variable>
    <input file>/tmp/bluepup/found-new-devices</input>
    <action signal="file-changed">clear:BTNEWDEVICE</action>
    <action signal="file-changed">refresh:BTNEWDEVICE</action>
    <action signal="file-changed">clear:BTREGDEVICE</action>
    <action signal="file-changed">refresh:BTREGDEVICE</action>
  </timer>
I have an outside program, that I want to be able to affect widgets in the gtkdialog GUI, so the timer seemed like a good way to achieve this. The outside program just has to write to the file /tmp/bluepup/found-new-devices, and then that will trigger the actions in the timer.

The weird thing is that it does work -- once. The timer variable is still "true" when the GUI is exited, so seems to still be running. It would seem that "file-monitor" is not doing what I think it should be doing.

Has anyone had experience with using "file-monitor", is there something wrong with the syntax the way I am trying to do it?

Or, is there some other way for my outside script to tell the GUI to do things?

Any thoughts welcome!

Oh, that code is part of BluePup, that I am currently working on. Some early snapshots here:

https://bkhome.org/news/202010/bluepup- ... urned.html
User avatar
MochiMoppel
Posts: 1137
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 18 times
Been thanked: 372 times

Re: gtkdialog timer file-monitor doesn't work

Post by MochiMoppel »

BarryK wrote: Fri Oct 30, 2020 7:44 amHas anyone had experience with using "file-monitor", is there something wrong with the syntax the way I am trying to do it?
Nothing wrong. In principle your code works for me.
The only problem: <action signal="file-changed"> always fires twice. Don't know how to prevent this, but depending on what the action does it may produce unwanted results.

There is also no need for a timer widget because "file-monitor" acts already as a timer (scans the monitored file every second). It may be more efficient to use one of the other widgets that are capable of file input (text, edit, entry,pixmap,button)

Here a demo that should increase the number in the text widgets by 1 every time the monitored file changes but since unfortunately TEXT is refreshed twice it increases by 2:

Code: Select all

#!/bin/sh
echo '
<window>
<vbox>
<edit visible="false" file-monitor="true">
	<input file>/tmp/found-new-devices</input>
	<action signal="file-changed">refresh:TEXT</action>
</edit>
<text>
	<variable>TEXT</variable>
	<input>echo $(($TEXT+1))</input>
</text>
<button label="Write to found-new-devices">
	<action>echo blabla > /tmp/found-new-devices </action>
</button>
</vbox>
</window>' | gtkdialog -s
some1
Posts: 71
Joined: Wed Aug 19, 2020 4:32 am
Has thanked: 17 times
Been thanked: 11 times

Re: gtkdialog timer file-monitor doesn't work

Post by some1 »

@MM:The demo seem to be stepping by 1.
User avatar
BarryK
Posts: 2316
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 98 times
Been thanked: 583 times

Re: gtkdialog timer file-monitor doesn't work

Post by BarryK »

@MochiMoppel,
Glad to see that you are still around! You are one of the gtkdialog gurus!

In your original example on the Murga Forum, the reason for the timer firing twice was the <button> widget. It's action fires twice when a button is clicked. I commented on my blog about this, it can cause unexpected behaviour in a gtkdialog gui, and is really an "undocumented feature"

If the file is written to externally, say "echo > /tmp/found-new-devices", then the action in the timer only fires once.

I figured it all out, why it wasn't working for me, and appended to my blog post:

https://bkhome.org/news/202010/problem- ... idget.html

Thanks for the input.
User avatar
MochiMoppel
Posts: 1137
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 18 times
Been thanked: 372 times

Re: gtkdialog timer file-monitor doesn't work

Post by MochiMoppel »

@some1 Yes, it works for me too in fossapup64 but not in a pristine Slacko5.6. Both use gtkdialog 0.8.4 but the builds are different.

@ BarryK No, the <button> widget is not the culprit here. Even without using the button (e.g. external "echo > /tmp/found-new-devices" ) the action fires twice. No way around it. Your gtkdialog may behave differently.

As you noticed using touch to change the modify date doesn't fire the action. Same with editing the file in an editor and save it. Nothing happens. This is plain wrong, also in fossapup64. Your posted wordaround, saving the monitored file to itself, certainly works, but let's face it: it's an ugly solution.

Bottomline for me: file-monitor is inflexible (unchangeable interval, monitors only mtime) and is unreliable. :thumbdown:

What works for me even in Slacko5.6 and is much more flexible: A timer that checks whatever has to be checked (e.g. changed file size, time, permission or even changes in a whole directory) and then triggers one or more actions.

Here an example.The timer periodically stats the monitored file, checking the file's mtime. If different from previous time (saved in text widget SAVEDTIME ) the counter increases by 1 and SAVEDTIME is updated. Touch command and saving from editor work as expected.

Code: Select all

function mtime { stat -c %Y /tmp/found-new-devices ;} # time of last modification, seconds since Epoch
export -f mtime

echo '<window><vbox>
<timer visible="false" interval="1">
	<action condition="command_is_true( echo $(($(mtime) - SAVEDTIME)) )">refresh:COUNTER</action>  
	<action condition="command_is_true( echo $(($(mtime) - SAVEDTIME)) )">refresh:SAVEDTIME</action>  
</timer>
<text visible="false">
	<variable>SAVEDTIME</variable>
	<input>mtime</input>
</text>
<text>
	<variable>COUNTER</variable>
	<input>echo $(($COUNTER+1))</input>
</text>
<button label="Write to found-new-devices">
	<action>echo blabla > /tmp/found-new-devices </action>
</button>
</vbox></window>' | gtkdialog -cs 
Last edited by MochiMoppel on Sun Aug 08, 2021 4:57 am, edited 1 time in total.
User avatar
MochiMoppel
Posts: 1137
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 18 times
Been thanked: 372 times

Re: gtkdialog timer file-monitor doesn't work

Post by MochiMoppel »

BarryK wrote: Sat Oct 31, 2020 2:02 amIf the file is written to externally, say "echo > /tmp/found-new-devices", then the action in the timer only fires once.
Seems to be more complicated. Here is what I found:

command fires remark
echo > file twice the quoted example, writes 1 character
echo -n > file once writes a null string, creates zero length file
echo bla > file twice writes a string
echo bla >> file once appends a string
User avatar
BarryK
Posts: 2316
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 98 times
Been thanked: 583 times

Re: gtkdialog timer file-monitor doesn't work

Post by BarryK »

MochiMoppel wrote: Sun Nov 01, 2020 7:47 am
BarryK wrote: Sat Oct 31, 2020 2:02 amIf the file is written to externally, say "echo > /tmp/found-new-devices", then the action in the timer only fires once.
Seems to be more complicated. Here is what I found:

command fires remark
echo > file twice the quoted example, writes 1 character
echo -n > file once writes a null string, creates zero length file
echo bla > file twice writes a string
echo bla >> file once appends a string
Very interesting!
Looking in my code, what I actually have in my code is "echo -n bla > /tmp/found-new-devices", so that is why I was getting the firing only once.
Post Reply

Return to “Programming”