0

I am trying to spawn process with gtkmm but am faced with a problem. Here is the specific snippet of my code:

std::vector<std::string> args, envp;

args.push_back("/usr/libexec/mc/ext.d/doc.sh");
args.push_back("open");
args.push_back("pdf");

envp.push_back("MC_EXT_FILENAME="DATADIR"/bsbguide.pdf");
Glib::spawn_async("", args, envp, Glib::SPAWN_SEARCH_PATH);`

Task is to open a pdf file that is installed in DATADIR (this var is defined with automake). This variable pushed in envp is essential for opening file using Midnight Commander's stuff. The command

MC_EXT_FILENAME="some_file" /usr/libexec/mc/ext.d/doc.sh open pdf 

will exactly open some_file with a PDF viewer.

The program compiles and works, but when I try to invoke programs with these functions I get this:

(zathura:3014): Gtk-WARNING **: cannot open display:

and nothing happens. Same command from console results in opened pdf file with my PDF viewer (Zathura). I found this in Devhelp:

If you are writing a GTK+ application, and the program you are spawning is a graphical application, too, then you may want to use gdk_spawn_on_screen() instead to ensure that the spawned program opens its windows on the right screen.

But no such function I found. Maybe there is a more elegant and correct way to open a file with the user's default PDF, viewer without MC parts? If not, how can I make this works? Even better if it will be in C++ style without gdk_spawn_on_screen.

underscore_d
  • 6,309
  • 3
  • 38
  • 64
Nikita
  • 85
  • 6
  • _"But no such function I found"_. That's because `gdk_spawn_on_screen()` was deprecated in GTK+/GDK version 2 and removed outright in version 3. The GLib documentation that you quoted is extremely outdated and needs to be fixed; there is an existing bug report for this. The real replacement now is to use `GAppInfoContext`, `GdkAppInfoContext`, or at least set the `DISPLAY` env var before spawning if you find that it is required (which might be OS-dependent). – underscore_d Jul 28 '17 at 14:37

1 Answers1

0

So, as a simple workaround I've found is such code

setenv("MC_EXT_FILENAME", DATADIR"/bsbguide.pdf", true);
Glib::spawn_command_line_async("/usr/libexec/mc/ext.d/doc.sh open pdf");
Nikita
  • 85
  • 6