Initial version, --vnc option only working.
[virt-click.git] / main.c
1 /* virt-click
2  * Copyright (C) 2011 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include <glib/gi18n.h>
27
28 #include "click.h"
29
30 gboolean verbose = 0;
31
32 static void parse_click (self_t *self, const char *binary, guint nr_args, gchar **args);
33
34 static void
35 short_usage (const char *binary, const char *error)
36 {
37   if (error)
38     fprintf (stderr, "Error: %s\n\n", error);
39   fprintf
40     (stderr,
41      "Run '%s --help' to see a full list of available command line\n"
42      "options or consult the virt-click(1) manual page.\n",
43      binary);
44 }
45
46 int
47 main (int argc, char **argv)
48 {
49   GOptionContext *context;
50   GError *error = NULL;
51   gboolean version = 0;
52   gchar *vnc = NULL;
53   gchar **args = NULL;
54   guint nr_args;
55   const GOptionEntry options [] = {
56     { "verbose", 'v', 0,  G_OPTION_ARG_NONE,
57       &verbose, "Enables verbose output", NULL },
58     { "version", 'V', 0,  G_OPTION_ARG_NONE,
59       &version, "Display version and exit", NULL },
60     { "vnc", 0, 0,  G_OPTION_ARG_STRING,
61       &vnc, "Connect to VNC server directly", NULL },
62     { G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_STRING_ARRAY, &args,
63       NULL, "(click|...) [args...]" },
64     { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, 0 }
65   };
66   self_t *self;
67
68   g_type_init ();
69
70   /* Command line options */
71   context = g_option_context_new ("- send click events");
72   g_option_context_set_summary
73     (context,
74      "Send mouse click and key press events to a virtual machine, VNC\n"
75      "server, or SPICE server.  See virt-click(1) manual page for complete\n"
76      "information.  This is just a summary.\n"
77      "\n"
78      "Commands:\n"
79      "  virt-click <cmd> where <cmd> is:\n"
80      "    click x y [b]                   Mouse click at (x, y), with button b"
81      );
82   g_option_context_add_main_entries (context, options, NULL);
83   g_option_context_parse (context, &argc, &argv, &error);
84   if (error) {
85     short_usage (argv[0], error->message);
86     g_error_free (error);
87     exit (EXIT_FAILURE);
88   }
89   if (version) {
90     printf ("%s\n", PACKAGE_STRING);
91     exit (EXIT_SUCCESS);
92   }
93
94   /* Initialize structure. */
95   self = g_new0 (self_t, 1);
96   self->loop = g_main_loop_new (g_main_context_default(), FALSE);
97   self->ret = EXIT_FAILURE;
98
99   /* Parse the command in detail. */
100   if (!args || (nr_args = g_strv_length (args)) < 1) {
101     short_usage (argv[0], "missing command");
102     exit (EXIT_FAILURE);
103   }
104
105   if (vc_parse_command (self, args, nr_args) == -1) {
106     short_usage (argv[0], "could not parse the command");
107     exit (EXIT_FAILURE);
108   }
109
110   /* Create VNC/SPICE/... object. */
111   if (vnc)
112     vc_vnc_setup (self, vnc);
113   else {
114     short_usage (argv[0], "no --vnc or --spice option");
115     exit (EXIT_FAILURE);
116   }
117
118   /* Main loop. */
119   g_main_loop_run (self->loop);
120
121   /* Shut down and exit. */
122   self->callbacks->shutdown (self);
123
124   g_free(self);
125
126   exit (self->ret);
127 }