GNOME Bugzilla – Bug 668210
Add g_application_command_line_get_stdin()
Last modified: 2012-12-27 16:01:14 UTC
gedit wants this, and it's not nearly insane as the general-purpose fd passing case (plus, it may be possible to do it in a portable way).
Created attachment 205577 [details] [review] Add g_application_command_line_get_stdin() This returns a GInputStream corresponding to the stdin on the commandline that caused this invocation.
Review of attachment 205577 [details] [review]: Where is the test ? When we commit this, it should probably be mentioned here: https://live.gnome.org/GTK+/GApplication-dbus-apis ::: gio/gapplicationcommandline.c @@ +540,3 @@ + * @cmdline: a #GApplicationCommandLine + * + * Gets the stdin of the invoking process. It would be really good to mention why this might be useful. ::: gio/gapplicationimpl-dbus.c @@ +556,3 @@ + GError *error = NULL; + + /* send along the stdin incase 'in case', two words, no ?
Calling it stdin is very Unix specific I guess, maybe another name? Also see bug #621187
Seems to work here for gedit.
Actually it does not properly work. We need the input stream to not be null if it has some data or we need a way to know that it has some data. something like: if (!isatty (STDIN_FILENO)) { struct stat sbuffer; if (fstat (STDIN_FILENO, &sbuffer) == 0 && (S_ISREG (sbuffer.st_mode) || S_ISFIFO (sbuffer.st_mode) || S_ISLNK (sbuffer.st_mode))) { fd_list = g_unix_fd_list_new (); g_unix_fd_list_append (fd_list, 0, &error); } }
This feature is insane.... echo foo | gedit I would have expected that I need at least to do echo foo | gedit - I can imagine cases where gedit has stdin that is not a terminal and that it should not expect to read from it....
If "gedit -" makes thinks easier, I am all for it (as a matter of fact, that's how I would have liked it to be in the first place)... the current syntax is what we inherited since gedit 0.X days That in turns means that we will need to move some the insanity in special casing the a file named "-", but I guess we can survive :-)
What about a more general solution to handle any file descriptors, not only stdin? It could be useful for a process to pass its stdout/stderr or any other file descriptor for more complex uses. Also, I think it's better to not associate file descriptors with GApplicationCommandLine. The fact that stdin, stdout and stderr can be _redirected_ from the command line should not be seen as _being part_ of the command line. And what about other file descriptors that are created by the process? So an idea would be to implement another GAction, named for example GUnixFDAction. As a side note, I thought G_VARIANT_TYPE_HANDLE could be used in a GSimpleAction to automagically pass file descriptors over D-Bus. But it's not the case, G_VARIANT_TYPE_HANDLE represents an index of an fd, not the fd itself.
Hum, forget my previous post :) I was a bit lost with all the interactions of the interfaces and classes involved with GApplication.
Created attachment 230117 [details] [review] rebased patch I rebased desrt's patch since it didn't apply anymore and also addressed the two comments by Matthias (not the unit test though). However testing this with gedit does not seem to work: it works for the first instance but not the following ones
FYI the testing code in gedit is available on the wip/gtkapp branch http://git.gnome.org/browse/gedit/commit/?h=wip/gtkapp&id=068560016d0370c5136d9ca2898aaf547d3d5a34 (you need to uncomment the actual line calling this function obviously)
Actually after talking with desrt on irc and tested again and it works great. I guess I was doing something silly like running the second instance out of the jhbuild shell...