diff options
author | Xiao Pan <xyz@flylightning.xyz> | 2025-04-26 21:27:03 -0700 |
---|---|---|
committer | Xiao Pan <xyz@flylightning.xyz> | 2025-04-26 21:27:03 -0700 |
commit | 7b7e26fb444b488bc2fb6167a68079e228504193 (patch) | |
tree | 43c7e1758e2bf806b2d931a00101d332091fddbe /remote_plot.c | |
parent | 02ced6640015d1373420d8a5a6b242471a999576 (diff) |
cli option to set ssh destination
Diffstat (limited to 'remote_plot.c')
-rw-r--r-- | remote_plot.c | 118 |
1 files changed, 66 insertions, 52 deletions
diff --git a/remote_plot.c b/remote_plot.c index c173f3b..c2aa1fc 100644 --- a/remote_plot.c +++ b/remote_plot.c @@ -7,6 +7,7 @@ // https://docs.gtk.org/gtk4/class.DrawingArea.html // https://www.gtk.org/docs/getting-started/hello-world/ // https://gitlab.gnome.org/GNOME/gtk/-/blob/main/demos/print-editor/print-editor.c +// https://gitlab.gnome.org/GNOME/gtk/-/blob/main/demos/gtk-demo/main.c #include <libssh/libssh.h> #include <stdlib.h> @@ -548,96 +549,109 @@ static void activate (GtkApplication *app, gpointer user_data) //printf("after g_timeout_add\n"); } -int main (int argc, char **argv) +// https://gitlab.gnome.org/GNOME/gtk/-/blob/main/demos/gtk-demo/main.c +static int command_line (GApplication *app, GApplicationCommandLine *cmdline, gpointer user_data) { - DATA data; - //ssh_session session; - //sftp_session sftp; + DATA *data=user_data; int rc; - - GtkApplication *app; - int status; - - for(int i=0;i<LEN;i++) - { - data.t[i]=i; - for(int j=0;j<VOLTLEN;j++) - data.volt[i][j]=0; - for(int j=0;j<TEMPLEN;j++) - data.temp[i][j]=0; - } - - //printf("my init test:\n") - //for(int i=0;i<LEN;i++) - //{ - // printf("%g",data.t[i]); - // for(int j=0;j<VOLTLEN;j++) - // printf(",%g",data.volt[i][j]); - // for(int j=0;j<TEMPLEN;j++) - // printf(",%g",data.temp[i][j]); - // putchar('\n'); - //} - //printf("my init test done\n") + GVariantDict *options = g_application_command_line_get_options_dict (cmdline); + // not sure if using char * here is ok + char *dest="Spartan_Racing_Charger@10.0.0.9"; + // https://docs.gtk.org/glib/gvariant-format-strings.html#pointers + // &s copy the pointer + g_variant_dict_lookup (options, "destination", "&s", &dest); // Open session and set options - data.session = ssh_new(); - if (data.session == NULL) + data->session = ssh_new(); + if (data->session == NULL) exit(-1); - ssh_options_set(data.session, SSH_OPTIONS_HOST, "Spartan_Racing_Charger@10.0.0.9"); - // for testing - //ssh_options_set(data.session, SSH_OPTIONS_HOST, "10.0.0.7"); + ssh_options_set(data->session, SSH_OPTIONS_HOST, dest); // Connect to server - rc = ssh_connect(data.session); + rc = ssh_connect(data->session); if (rc != SSH_OK) { - fprintf(stderr, "Error connecting to Spartan_Racing_Charger@10.0.0.9: %s\n", - ssh_get_error(data.session)); - ssh_free(data.session); + fprintf(stderr, "Error connecting to %s: %s\n", + dest, ssh_get_error(data->session)); + ssh_free(data->session); exit(-1); } // Verify the server's identity // For the source code of verify_knownhost(), check previous example - if (verify_knownhost(data.session) < 0) + if (verify_knownhost(data->session) < 0) { - ssh_disconnect(data.session); - ssh_free(data.session); + ssh_disconnect(data->session); + ssh_free(data->session); exit(-1); } // Authenticate ourselves // https://api.libssh.org/stable/libssh_tutor_authentication.html - rc = ssh_userauth_publickey_auto(data.session, NULL, NULL); + rc = ssh_userauth_publickey_auto(data->session, NULL, NULL); if (rc != SSH_AUTH_SUCCESS) { fprintf(stderr, "Error authenticating with key: %s\n", - ssh_get_error(data.session)); - ssh_disconnect(data.session); - ssh_free(data.session); + ssh_get_error(data->session)); + ssh_disconnect(data->session); + ssh_free(data->session); exit(-1); } - - data.sftp = sftp_new(data.session); - if (data.sftp == NULL) + data->sftp = sftp_new(data->session); + if (data->sftp == NULL) { fprintf(stderr, "Error allocating SFTP session: %s\n", - ssh_get_error(data.session)); + ssh_get_error(data->session)); return SSH_ERROR; } - rc = sftp_init(data.sftp); + rc = sftp_init(data->sftp); if (rc != SSH_OK) { fprintf(stderr, "Error initializing SFTP session: code %d.\n", - sftp_get_error(data.sftp)); - sftp_free(data.sftp); + sftp_get_error(data->sftp)); + sftp_free(data->sftp); return rc; } - app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS); + // https://discourse.gnome.org/t/gio-application-commandline-does-trigger-activate-signal/2988/22 + // > If you pass G_APPLICATION_HANDLES_COMMAND_LINE in the flags, the + // > command-line args are passed to the primary instance and ::activate is + // > not emitted. An example again is an something like --hide-window or + // > --do-without-disturbing-user which wouldn’t work if the default were to + // > trigger ::activate and open the window. So just pass + // > G_APPLICATION_HANDLES_COMMAND_LINE and then call + // > g_application_activate() in your ::command-line handler + g_application_activate(app); + + return 0; +} + +int main (int argc, char **argv) +{ + DATA data; + GtkApplication *app; + int status; + + for(int i=0;i<LEN;i++) + { + data.t[i]=i; + for(int j=0;j<VOLTLEN;j++) + data.volt[i][j]=0; + for(int j=0;j<TEMPLEN;j++) + data.temp[i][j]=0; + } + + // if I don't handle command line + //app = gtk_application_new ("org.gtk.example", G_APPLICATION_DEFAULT_FLAGS); + // if I handle command line + app = gtk_application_new ("org.gtk.example", G_APPLICATION_HANDLES_COMMAND_LINE); + + g_application_add_main_option(G_APPLICATION(app),"destination",'d',0,G_OPTION_ARG_STRING,"ssh destination","Spartan_Racing_Charger@10.0.0.9"); + g_signal_connect (app, "activate", G_CALLBACK (activate), &data); + g_signal_connect (app, "command-line", G_CALLBACK (command_line), &data); status = g_application_run (G_APPLICATION (app), argc, argv); g_object_unref (app); |