timrowledge
Posts: 1348
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island
Contact: Website

open browser on URL from code

Tue Apr 15, 2014 1:40 am

I'm trying to find a nice, clean, decently safe way to open a URL from C code. I have some dumb-but-functional code that cats 'xdg-open ' and the url string as passed by the application, then does a system call; but that's not the best way to do things really. Surely there is a better way? RISC OS has the rather nice uri_dispatch() OS call. What's the equivalent in Raspbian?
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: open browser on URL from code

Tue Apr 15, 2014 7:08 am

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
topguy
Posts: 6527
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: open browser on URL from code

Tue Apr 15, 2014 11:57 am

"xdg-open" is probably the only way that would be portable between different linux-desktops.
xdg-open is a desktop-independent tool for configuring the default applications of a user. Many applications invoke the xdg-open command internally.
I've also seen mentioned "sensible-browser" script for debian-derivatives, havent checked if its part of standard raspbian.

timrowledge
Posts: 1348
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island
Contact: Website

Re: open browser on URL from code

Tue Apr 15, 2014 8:06 pm

topguy wrote:"xdg-open" is probably the only way that would be portable between different linux-desktops.
xdg-open is a desktop-independent tool for configuring the default applications of a user. Many applications invoke the xdg-open command internally.
So a full-scale system call is actually the 'right' way to do it? Ouch. I did spot the assorted 'exec()' functions but they all claim to replace the current process; not quite what I'm after.
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

User avatar
topguy
Posts: 6527
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: open browser on URL from code

Wed Apr 16, 2014 12:09 pm

timrowledge wrote: So a full-scale system call is actually the 'right' way to do it? Ouch. I did spot the assorted 'exec()' functions but they all claim to replace the current process; not quite what I'm after.
You haven't really explained your requirements good enough for us to pick a "right" way.

If your program is only meant to run in a Raspbian environment, with the LXDE desktop and with the standard browser (midori?).
Then a system() call is well enough. If your only requirement is an unknown X-windows desktop where you have no information about installed software then "xdg-open" sound like something that would work well for many systems.

timrowledge
Posts: 1348
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island
Contact: Website

Re: open browser on URL from code

Fri Apr 18, 2014 3:41 am

topguy wrote:You haven't really explained your requirements good enough for us to pick a "right" way.
OK, I'll be more precise, as well as I can anyway.

Scratch needs to launch a web-browser to access online help. In RISC OS I'd just use the uri_dispatch call and it would just happen. In unix I'd naively use system() but I've been scolded in past times for doing so on the grounds of... something or other about too heavy-weight, dangerous, likely to cause spots or bad breath or something. If it really is the right way to do this, then I'll just do it.
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

User avatar
topguy
Posts: 6527
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: open browser on URL from code

Tue Apr 22, 2014 2:31 pm

I guess RISC OS is an OS with a well integrated desktop environment.
Linux is not, that is why this "simple" task is quite ugly, and gets even uglier when you realize that "xdg-open" is a big bash-script.

plugwash
Forum Moderator
Forum Moderator
Posts: 3630
Joined: Wed Dec 28, 2011 11:45 pm

Re: open browser on URL from code

Wed Apr 23, 2014 3:51 pm

timrowledge wrote:
topguy wrote:You haven't really explained your requirements good enough for us to pick a "right" way.
OK, I'll be more precise, as well as I can anyway.

Scratch needs to launch a web-browser to access online help. In RISC OS I'd just use the uri_dispatch call and it would just happen. In unix I'd naively use system() but I've been scolded in past times for doing so on the grounds of... something or other about too heavy-weight, dangerous, likely to cause spots or bad breath or something. If it really is the right way to do this, then I'll just do it.
system() is probablly not the right call for the job since it will wait until whatever process it runs exits which is probablly not what you want.

Normally the way you run programs in *nix is to use fork followed by exec.

Tarcas
Posts: 741
Joined: Thu Jan 09, 2014 5:38 am
Location: USA

Re: open browser on URL from code

Wed Apr 23, 2014 8:11 pm

system() is probablly not the right call for the job since it will wait until whatever process it runs exits which is probablly not what you want.
Why not call it as a background process? Then it won't wait.

Return to “Raspberry Pi OS”