BaCon + HUG execute and 'detach'?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
PaulR
Posts: 249
Joined: Wed 04 May 2005, 18:45
Location: UK

BaCon + HUG execute and 'detach'?

#1 Post by PaulR »

I've written a tiny BaCon/HUG program as a learning exercise. One of the callbacks starts another program but my program window remains 'unavailable' until the executed program terminates.

Is there any way to 'detach' the program I'm calling in the same way that <command> & or (<command> &) works in a terminal? so my program can continue?

I'm guessing that the callback isn't returning control to DISPLAY until the called program completes.

It occurs to me that I might be able to accomplish this by a TIMEOUT if I can read the button state rather than use a callback

Any help appreciated :)

Paul

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#2 Post by vovchik »

Dear PaulR,

I was going to say welcome to the kennels, but I see you have been here longer than I have :) As for BaCon, I am certain one of us can give you some advice. Not a bad idea to post the source, so what we can see where the problem is.

With kind regards,
vovchik

PaulR
Posts: 249
Joined: Wed 04 May 2005, 18:45
Location: UK

#3 Post by PaulR »

Hi vovchik

Yes I've been a member for quite sometime but not an active user for ages, however I'm really enjoying using Puppy again!

The program works as expected generally, these are the relevant bits...

Code: Select all

....
CALLBACK(searchButton, subSearch)
DISPLAY

SUB subSearch
...
commandLine$ = GRAB$(entrySearch)
result$ = EXEC$(commandLine$)
...
END SUB
The program being called is another GUI program not just a terminal command.

I've checked the contents of commandLine$ and the called program executes as intended; as I mentioned it just 'freezes' my program window (ie it is not receiving any 'paint' instructions) until execution of the called program terminates. My program continues to work as intended once the called program returns.

I don't think there's any error in my code (it is uber-simple after all), it may be that processes started in this way have to complete before the program can resume.

Many thanks

Paul

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#4 Post by vovchik »

Dear PaulR,

This should work for invoking a separate prog in a separate thread:

Code: Select all

commandLine$ = GRAB$(entrySearch) & " &"
or

Code: Select all

result$ = EXEC$(commandLine$ &  " &") 
I am not certain that your callback will work the way you imagine, but maybe it can. I don't see all the code. In any case, you usually define a CALLBACK for a widget, e.g.:

Code: Select all

SUB MYQUIT()
     PRINT "Quitting prog"
     QUIT
END SUB

' make button
but1 = BUTTON("quit", 90, 30)
' fix it to a window
ATTACH(win, but1, 10, 10)
' define action that pressing that button will do
CALLBACK(but, MYQUIT)
DISPLAY
If you want arguments passed, use CALLBACKX. You seem to be doing that normally.

With kind regards,
vovchik

PaulR
Posts: 249
Joined: Wed 04 May 2005, 18:45
Location: UK

#5 Post by PaulR »

Hi vovchik

Thanks for the assistance - I'm sure I tried appending an ampersand (as per my earlier post re the terminal) but I must have done something wrong.

Anyway, I used the syntax you suggested and it worked fine ie the program was launching in it's own thread and not tying up my program.

However, since I added some more code (the part dealing with postfix$ in the callback) it has reverted to the original problem.

Anyway, the program is designed to speed up searching on the net for Puppy related stuff - enter search terms, choose the search target and hit search. I just got tired of typing 'puppy linux' for every search!

I've attached the full source, perhaps you could cast an eye over it?

Many thanks

Paul

PS Is there a way to make the search button the default event when someone hits enter?

PPS I know there's a warning I need to sort out regarding a label/caption :)

EDIT: I've fixed the warning and edited the code below - the xalign property requires a value of '0.0' rather than '0' ... quirky! (personally I would have used -1/0/1 to indicate aligment, or better yet have LEFT, RIGHT, CENTER #defined - I'm going to do that now!).

Code: Select all

' Simple utility to google (with firefox) for Puppy Linux terms
' (hard-coded for Firefox/Google but easy to change for other browser/search engine)
' By Paul Robinson 2013 

'include the BaCon GUI library
INCLUDE "/usr/share/BaCon/hug_imports.bac"
'this is required when using library above
INIT

versionNo$ = "1.0"

'create the window and widgets
'set window title once name has been supplied?
appWindow = WINDOW(CONCAT$("Psearch ", versionNo$), 220, 180)

'label and text box for user's search terms
markSearchLabel=MARK("Search for", 100, 15)
PROPERTY(markSearchLabel, "xalign", 0.0)
ATTACH(appWindow, markSearchLabel, 10, 10)
entrySearchTerms=ENTRY("", 130, 15)
ATTACH(appWindow, entrySearchTerms, 80, 10)

'radio buttons for search target choice
'put in a frame
rbFrame = FRAME(200, 100)
TEXT(rbFrame, "Target: ")
ATTACH(appWindow, rbFrame, 8, 35)
radioG = RADIO("Google", 200, 15, radioG)
ATTACH(appWindow, radioG, 10, 55)
radioP = RADIO("Puppylinux.org", 200, 15, radioG)
ATTACH(appWindow, radioP, 10, 80)
radioF = RADIO("Puppy Linux Forum", 200, 15, radioG)
ATTACH(appWindow, radioF, 10, 105)

'set default target to google
SET(radioG, TRUE)


'button to run search
searchButton=BUTTON("Search", 50, 30)
ATTACH(appWindow, searchButton, 160, 140) 

'button to quit
quitButton=BUTTON("Exit", 50, 30)
ATTACH(appWindow, quitButton, 10, 140)

'define callbacks
'ie functions for widget events
CALLBACK(quitButton, QUIT)
CALLBACK(searchButton, subSearch)


'show the window
DISPLAY






'search button callback
SUB subSearch
	
	'specify new tab in case Firefox is already open
	searchURL$ = "firefox -new-tab http://www.google.com/search?q="
	
	IF GET(radioG) THEN 
		postfix$ = ""
	ELIF GET(radioP) THEN	
		postfix$ = "+site:puppylinux.org"
	ELSE
		postfix$ = "+site:murga-linux.com/puppy"
	END IF
	
	'get user input, replacing any spaces with '+' signs
	searchTerms$ = REPLACE$(GRAB$(entrySearchTerms), " ", "+")
	'TODO add code to remove or escape single/double quotes
	
	'concat the user's search terms to hard-wired "puppy linux"
	searchTerms$ = CONCAT$("puppy+linux+", searchTerms$)
	'add any site specific search requirement
	searchTerms$ = CONCAT$(searchTerms$, postfix$)
	'add that to the basic url string
	commandLine$ = CONCAT$(searchURL$, searchTerms$) 
	'start firefox in it's own thread
	result$ = EXEC$(commandLine$ & " &")

END SUB



User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

psearch

#6 Post by L18L »

Very nice.
At the moment I have running your prog in puppy precise.
Just changed
firefox
to
defaulthtmlviewer
and

Code: Select all

'button to run search
searchButton=BUTTON("Search", 50, 30)
ATTACH(appWindow, searchButton, 160, 140) 

'button to quit
quitButton=BUTTON("Exit", 50, 30)
ATTACH(appWindow, quitButton, 10, 140)
to

Code: Select all

'button to run search
searchButton = STOCK("gtk-find", 90, 30)
ATTACH(appWindow, searchButton, 116, 140)

'button to quit
quitButton = STOCK("gtk-quit", 100, 30)
ATTACH(appWindow, quitButton, 8, 140)
Attachments
Psearch.png
(3.45 KiB) Downloaded 297 times

User avatar
Mobeus
Posts: 94
Joined: Thu 26 Aug 2010, 15:49

#7 Post by Mobeus »

Hello Paul

SYSTEM may work better for you in this case.

Code: Select all

SYSTEM "firefox &"

' r$= EXEC$("firefox &")

FOR i = 1 TO 100
	PRINT i
NEXT
END
Regards,
Mobeus
/root for the home team

PaulR
Posts: 249
Joined: Wed 04 May 2005, 18:45
Location: UK

#8 Post by PaulR »

L18L, glad you think it's useful, I can think of a few features to add...

- Use checkboxes to allow search of multiple sites (just found this is possible by passing site:somesite.com OR site:anothersite.com )

- Add a box to specify browser (and save that setting) but with hindsight your defaulthtmlviewer is probably better/easier. EDIT: not sure how all browsers would cope with the new-tab switch though?

- Allow user-defined sites to search (ie allow editing of the three existing sites)

I'm not keen on all these magic numbers for widget layout either.. now there's a project for someone - a BaCon HUG layout tool! :D

Mobeus, I haven't tried your idea but the BaCon docs about SYSTEM says;

"It causes the BaCon program to hold until the command has been completed."

...and that is the problem I have. Strange that it worked ok for a time but doesn't now. Echoing commandLine$ doesn't help much either...thinking!


Paul

PaulR
Posts: 249
Joined: Wed 04 May 2005, 18:45
Location: UK

#9 Post by PaulR »

Well this is strange - I just modified the source to add the STOCK buttons and repositioned some of the widgets, re-compiled and now the program works properly i.e it seems to run in its own thread allowing re-use without closing the browser!

Paul

Here's the updated code - for anyone experimenting (like me!) you need to compile with the -j option to take account of the #define, for example:

# bacon -j psearch.bac

Code: Select all

' Simple utility to google (with firefox) for Puppy Linux terms
' (hard-coded for Firefox/Google but easy to change for other browser/search engine)
' By Paul Robinson 2013 

#define ALIGNLEFT 0.0

'include the BaCon GUI library
INCLUDE "/usr/share/BaCon/hug_imports.bac"
'this is required when using library above
INIT

versionNo$ = "1.0"

'create the window and widgets
'set window title once name has been supplied?
appWindow = WINDOW(CONCAT$("Psearch ", versionNo$), 216, 176)

'label and text box for user's search terms
markSearchLabel=MARK("Search for", 100, 15)
PROPERTY(markSearchLabel, "xalign", ALIGNLEFT)
ATTACH(appWindow, markSearchLabel, 10, 10)
entrySearchTerms=ENTRY("", 130, 15)
ATTACH(appWindow, entrySearchTerms, 78, 10)

'radio buttons for search target choice
'put in a frame
rbFrame = FRAME(200, 100)
TEXT(rbFrame, "Target: ")
ATTACH(appWindow, rbFrame, 8, 35)
radioG = RADIO("Google", 200, 15, radioG)
ATTACH(appWindow, radioG, 10, 55)
radioP = RADIO("Puppylinux.org", 200, 15, radioG)
ATTACH(appWindow, radioP, 10, 80)
radioF = RADIO("Puppy Linux Forum", 200, 15, radioG)
ATTACH(appWindow, radioF, 10, 105)

'set default target to google
SET(radioG, TRUE)

'button to run search
searchButton=STOCK("gtk-find", 90, 30) 
ATTACH(appWindow, searchButton, 118, 142) 

'button to quit
quitButton=STOCK("gtk-quit", 100, 30) 
ATTACH(appWindow, quitButton, 8, 142)

'define callbacks
'ie functions for widget events
CALLBACK(quitButton, QUIT)
CALLBACK(searchButton, subSearch)

'show the window
DISPLAY


'search button callback
SUB subSearch
	
	'specify new tab in case Firefox is already open
	searchURL$ = "firefox -new-tab http://www.google.com/search?q="
	
	IF GET(radioG) THEN 
		postfix$ = ""
	ELIF GET(radioP) THEN	
		postfix$ = "+site:puppylinux.org"
	ELSE
		postfix$ = "+site:murga-linux.com/puppy"
	END IF
	
	'get user input, replacing any spaces with '+' signs
	searchTerms$ = REPLACE$(GRAB$(entrySearchTerms), " ", "+")
	'TODO add code to remove or escape single/double quotes
	
	'concat the user's search terms to hard-wired "puppy linux"
	searchTerms$ = CONCAT$("puppy+linux+", searchTerms$)
	'add any site specific search requirement
	searchTerms$ = CONCAT$(searchTerms$, postfix$)
	'add that to the basic url string
	commandLine$ = CONCAT$(searchURL$, searchTerms$) 
	'start firefox in it's own thread
	result$ = EXEC$(commandLine$ & " &")

END SUB

[/code]
Last edited by PaulR on Fri 22 Mar 2013, 13:37, edited 1 time in total.

User avatar
Mobeus
Posts: 94
Joined: Thu 26 Aug 2010, 15:49

#10 Post by Mobeus »

Paul,

Yes the docs do say that :) but that's not quite what happens. Run the code from the terminal with SYSTEM then with EXEC and you will see the difference.

Regards,
Mobeus
/root for the home team

PaulR
Posts: 249
Joined: Wed 04 May 2005, 18:45
Location: UK

#11 Post by PaulR »

Hi Mobeus

Haha, I'll try that a little later, many thanks (although see my last post the program is now working as intended!).

EDIT: Tried that, there's no discernible difference, both forms seems to work ok now.

Paul

User avatar
Mobeus
Posts: 94
Joined: Thu 26 Aug 2010, 15:49

#12 Post by Mobeus »

Paul,

I tested your app, nice & handy too. Thanks!

It would not build as is on my system. It's Lupu 5.10 and has the old hug.so from 2011.

Just FYI, to get it to build (and work without hanging) I used the latest bacon and hug.bac and made these changes.

Code: Select all

USEH
#define ALIGNLEFT 0.0
END USEH

'include the BaCon GUI library
'INCLUDE "/usr/share/BaCon/hug_imports.bac"
'this is required when using library above
INCLUDE "hug.bac" 
and

Code: Select all

   commandLine$ = CONCAT$(searchURL$, searchTerms$, " &")
   'start firefox in it's own thread
   'result$ = EXEC$(commandLine$)
   SYSTEM commandLine$
Compiled it without the -j option

Regards,
Mobeus
/root for the home team

PaulR
Posts: 249
Joined: Wed 04 May 2005, 18:45
Location: UK

#13 Post by PaulR »

Great, I understand.

I've been working on using checkboxes such that a user can search either Google OR one or both sites. Finally got the checkbox logic figured out (after fighting with nested if statements all afternoon!), should have a modified version ready later today or tomorrow.

Thanks for the help :D

Paul

PaulR
Posts: 249
Joined: Wed 04 May 2005, 18:45
Location: UK

#14 Post by PaulR »

OK the next version is ready :D

Just one thing I need to know:

What will be the effect of the -new-tab switch if I change the called program to defaulthtmlviewer and the browser is something other than Firefox?

I'll build and compress the executable and upload it to some webspace with the source for anyone to access.

Paul

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

#15 Post by L18L »

PaulR wrote:...What will be the effect of the -new-tab switch if I change the called program to defaulthtmlviewer and the browser is something other than Firefox?
seamonkey has worked fine with me.
PaulR wrote:I'll build and compress the executable and upload it to some webspace with the source for anyone to access.
Browsers and Internet in Additional software here on this forum will do it too
:)

PaulR
Posts: 249
Joined: Wed 04 May 2005, 18:45
Location: UK

#16 Post by PaulR »


Post Reply