Skip to content

Commit

Permalink
Github #211: Support opening files in existing running instance via c…
Browse files Browse the repository at this point in the history
…ommand line '-r'

Use Unix sockets to listen for commands, thus not available on Windows.
As not expected typical usage on Windows, hence not planned to implemented a solution for this OS.
  • Loading branch information
rnorris committed Dec 21, 2023
1 parent be16eb5 commit 341e89b
Show file tree
Hide file tree
Showing 9 changed files with 407 additions and 1 deletion.
10 changes: 10 additions & 0 deletions help/C/commandline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
<arg choice="plain"><option>-e</option></arg>
<arg choice="plain"><option>--external</option></arg>
</group>
<group choice="opt">
<arg choice="plain"><option>-r</option></arg>
<arg choice="plain"><option>--running-instance</option></arg>
</group>
<sbr/>
<arg rep="repeat"><replaceable>file</replaceable></arg>
</cmdsynopsis>
Expand Down Expand Up @@ -161,6 +165,12 @@ This also enables some extra information features in the GUI itself, primarily o
</entry>
</row>
<row>
<entry>-r</entry>
<entry>--running-instance</entry>
<entry>Opens the specified files in an already running instance of &appname;. If no instance is available than the files will not be opened.
Positional and map option parameters are not used or passed on to the running instance.
<note><para>This option is not available on <trademark>Windows</trademark></para></note>
</entry>
</row>
</tbody>
</tgroup>
Expand Down
13 changes: 13 additions & 0 deletions help/viking.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ and docbook-xsl in your Build-Depends control field.
<arg choice="plain"><option>-e</option></arg>
<arg choice="plain"><option>--external</option></arg>
</group>
<group choice="opt">
<arg choice="plain"><option>-r</option></arg>
<arg choice="plain"><option>--running-instance</option></arg>
</group>
<sbr/>
<group choice="plain">
<arg rep="repeat"><replaceable>file</replaceable></arg>
Expand Down Expand Up @@ -255,6 +259,15 @@ and docbook-xsl in your Build-Depends control field.
<para>This is in contrast to importing the data and storing it in the Viking file.</para>
</listitem>
</varlistentry>
<varlistentry>
<term><option>-r</option></term>
<term><option>--running-instance</option></term>
<listitem>
<para>Opens the specified files in an already running instance of &appname;. If no instance is available than the files will not be opened.</para>
<para>Positional and map option parameters are not used or passed on to the running instance.</para>
<note><para>This option is not available on <trademark>Windows</trademark></para></note>
</listitem>
</varlistentry>
</variablelist>

</refsect1>
Expand Down
1 change: 1 addition & 0 deletions po/POTFILES.in
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ src/main.c
src/osm.c
src/osm-traces.c
src/preferences.c
src/socket.c
src/tcx.c
src/toolbar.c
src/viklayer_defaults.c
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ libviking_a_SOURCES = \
kmz.c kmz.h \
viklayer_defaults.c viklayer_defaults.h \
settings.c settings.h \
socket.c socket.h \
preferences.c preferences.h \
misc/heatmap.c misc/heatmap.h \
misc/fpconv.c misc/fpconv.h misc/powers.h \
Expand Down
16 changes: 16 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "viktrwlayer_export.h"
#include "modules.h"
#include "dir.h"
#include "socket.h"

/* FIXME LOCALEDIR must be configured by ./configure --localedir */
/* But something does not work actually. */
Expand All @@ -58,6 +59,7 @@ static gint zoom_level_osm = -1;
static gint map_id = G_MININT;
static gboolean external = FALSE;
static gchar *confdir = NULL;
static gboolean running_instance = FALSE;

/* Options */
static GOptionEntry entries[] =
Expand All @@ -71,6 +73,9 @@ static GOptionEntry entries[] =
{ "zoom", 'z', 0, G_OPTION_ARG_INT, &zoom_level_osm, N_("Zoom Level (OSM). Value can be 0 - 22"), NULL },
{ "map", 'm', 0, G_OPTION_ARG_INT, &map_id, N_("Add a map layer by id value. Use 0 for the default map."), NULL },
{ "external", 'e', 0, G_OPTION_ARG_NONE, &external, N_("Load all GPX files in external mode."), NULL },
#ifdef G_OS_UNIX
{ "running-instance", 'r', 0, G_OPTION_ARG_NONE, &running_instance, N_("Open file(s) in an existing running instance"), NULL },
#endif
{ NULL }
};

Expand Down Expand Up @@ -156,6 +161,13 @@ int main( int argc, char *argv[] )
return EXIT_SUCCESS;
}

if ( running_instance ) {
// NB as gtk has processed (and removed any options present) argv,
// it only contains the remaining list of files
gboolean sof = socket_open_files ( argc, argv, external );
return sof ? EXIT_SUCCESS : EXIT_FAILURE;
}

// Mainly for testing,
// but also could be useful for running a particular session with a specific configuration
if ( confdir )
Expand Down Expand Up @@ -289,6 +301,8 @@ int main( int argc, char *argv[] )

vu_command_line ( first_window, latitude, longitude, zoom_level_osm, map_id );

(void)socket_init ( first_window );

gtk_main ();

vik_trwlayer_uninit ();
Expand Down Expand Up @@ -318,6 +332,8 @@ int main( int argc, char *argv[] )

a_vik_preferences_uninit ();

socket_uninit();

#ifdef G_OS_WIN32
g_strfreev ( argv );
#endif
Expand Down
Loading

0 comments on commit 341e89b

Please sign in to comment.