2022-10-29 23:15:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022 Jim Ramsay
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "ctl-client.h"
|
|
|
|
|
|
|
|
#define MAYBE_UNUSED __attribute__((unused))
|
|
|
|
|
|
|
|
struct wayvncctl {
|
|
|
|
bool do_exit;
|
|
|
|
|
|
|
|
struct ctl_client* ctl;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int wayvncctl_usage(FILE* stream, int rc)
|
|
|
|
{
|
|
|
|
static const char* usage =
|
|
|
|
"Usage: wayvncctl [options] [command [--param1=value1 ...]]\n"
|
|
|
|
"\n"
|
2022-11-06 02:42:24 +00:00
|
|
|
"Connects to and interacts with a running wayvnc instance."
|
2022-10-29 23:15:29 +00:00
|
|
|
"\n"
|
2022-11-06 02:42:24 +00:00
|
|
|
"Try 'wayvncctl help' for a list of available commands.\n"
|
2022-10-29 23:15:29 +00:00
|
|
|
"\n"
|
2022-11-06 02:42:24 +00:00
|
|
|
"Options:\n"
|
|
|
|
" -S,--socket=<path> Wayvnc control socket path.\n"
|
|
|
|
" Default: $XDG_RUNTIME_DIR/wayvncctl\n"
|
2022-11-15 09:57:26 +00:00
|
|
|
" -w,--wait Wait for wayvnc to start up if it's\n"
|
|
|
|
" not already running.\n"
|
2022-11-16 00:57:16 +00:00
|
|
|
" -r,--reconnect If disconnected while waiting for\n"
|
|
|
|
" events, wait for wayvnc to restart.\n"
|
2022-11-15 09:56:59 +00:00
|
|
|
" -j,--json Output json on stdout.\n"
|
2022-11-06 02:42:24 +00:00
|
|
|
" -V,--version Show version info.\n"
|
|
|
|
" -v,--verbose Be more verbose.\n"
|
|
|
|
" -h,--help Get help (this text).\n"
|
2022-10-29 23:15:29 +00:00
|
|
|
"\n";
|
|
|
|
|
|
|
|
fprintf(stream, "%s", usage);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int show_version(void)
|
|
|
|
{
|
|
|
|
printf("wayvnc: %s\n", wayvnc_version);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
struct wayvncctl self = { 0 };
|
|
|
|
|
2022-11-16 00:57:16 +00:00
|
|
|
static const char* shortopts = "+S:hVvjwr";
|
2022-10-29 23:15:29 +00:00
|
|
|
|
|
|
|
bool verbose = false;
|
|
|
|
const char* socket_path = NULL;
|
2022-11-15 09:57:26 +00:00
|
|
|
int wait_for_socket = 0;
|
2022-10-29 23:15:29 +00:00
|
|
|
|
2022-11-05 13:53:17 +00:00
|
|
|
unsigned flags = 0;
|
|
|
|
|
2022-10-29 23:15:29 +00:00
|
|
|
static const struct option longopts[] = {
|
|
|
|
{ "socket", required_argument, NULL, 'S' },
|
2022-11-15 09:57:26 +00:00
|
|
|
{ "wait", no_argument, NULL, 'w' },
|
2022-11-16 00:57:16 +00:00
|
|
|
{ "reconnect", no_argument, NULL, 'r' },
|
2022-11-05 13:53:17 +00:00
|
|
|
{ "json", no_argument, NULL, 'j' },
|
2022-10-29 23:15:29 +00:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, 'V' },
|
|
|
|
{ "verbose", no_argument, NULL, 'v' },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int c = getopt_long(argc, argv, shortopts, longopts, NULL);
|
|
|
|
if (c < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'S':
|
|
|
|
socket_path = optarg;
|
|
|
|
break;
|
2022-11-15 09:57:26 +00:00
|
|
|
case 'w':
|
|
|
|
wait_for_socket = -1;
|
|
|
|
break;
|
2022-11-16 00:57:16 +00:00
|
|
|
case 'r':
|
|
|
|
flags |= RECONNECT;
|
|
|
|
break;
|
2022-11-05 13:53:17 +00:00
|
|
|
case 'j':
|
|
|
|
flags |= PRINT_JSON;
|
|
|
|
break;
|
2022-10-29 23:15:29 +00:00
|
|
|
case 'v':
|
|
|
|
verbose = true;
|
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
return show_version();
|
|
|
|
case 'h':
|
|
|
|
return wayvncctl_usage(stdout, 0);
|
|
|
|
default:
|
|
|
|
return wayvncctl_usage(stderr, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
2022-11-22 19:02:09 +00:00
|
|
|
if (argc <= 0)
|
|
|
|
return wayvncctl_usage(stderr, 1);
|
2022-10-29 23:15:29 +00:00
|
|
|
argv = &argv[optind];
|
|
|
|
|
|
|
|
ctl_client_debug_log(verbose);
|
2022-11-15 09:57:26 +00:00
|
|
|
|
2022-10-29 23:15:29 +00:00
|
|
|
self.ctl = ctl_client_new(socket_path, &self);
|
|
|
|
if (!self.ctl)
|
|
|
|
goto ctl_client_failure;
|
|
|
|
|
2022-11-15 09:57:26 +00:00
|
|
|
int result = ctl_client_connect(self.ctl, wait_for_socket);
|
|
|
|
if (result != 0)
|
|
|
|
goto socket_failure;
|
|
|
|
|
|
|
|
result = ctl_client_run_command(self.ctl, argc, argv, flags);
|
2022-10-29 23:15:29 +00:00
|
|
|
|
2022-11-15 09:57:26 +00:00
|
|
|
socket_failure:
|
2022-10-29 23:15:29 +00:00
|
|
|
ctl_client_destroy(self.ctl);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
ctl_client_failure:
|
|
|
|
return 1;
|
|
|
|
}
|