Initial version, --vnc option only working.
[virt-click.git] / commands.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 static int
31 parse_click (self_t *self, gchar **args, guint nr_args)
32 {
33   int b;
34
35   if (nr_args < 3 || nr_args > 4)
36     return -1;
37
38   if (sscanf (args[1], "%d", &self->command.click.x) != 1 ||
39       sscanf (args[2], "%d", &self->command.click.y) != 1)
40     return -1;
41
42   b = 1;
43   if (nr_args == 4) {
44     if (strcasecmp (args[3], "left") == 0)
45       b = 1;
46     else if (strcasecmp (args[3], "middle") == 0)
47       b = 2;
48     else if (strcasecmp (args[3], "right") == 0)
49       b = 3;
50     else if (strcasecmp (args[3], "up") == 0)
51       b = 4;
52     else if (strcasecmp (args[3], "down") == 0)
53       b = 5;
54     else if (strcasecmp (args[3], "scrollleft") == 0)
55       b = 6;
56     else if (strcasecmp (args[3], "scrollright") == 0)
57       b = 7;
58     else if (sscanf (args[3], "%d", &b) != 1)
59       return -1;
60   }
61
62   self->command.click.mask = 1 << (b-1);
63   self->command.cmd = CMD_CLICK;
64
65   return 0;
66 }
67
68 int
69 vc_parse_command (self_t *self, gchar **args, guint nr_args)
70 {
71   if (strcasecmp (args[0], "click") == 0)
72     return parse_click (self, args, nr_args);
73
74   return -1;
75 }
76
77 void
78 vc_issue_command (self_t *self)
79 {
80   int r = -1;
81
82   switch (self->command.cmd) {
83   case CMD_CLICK:
84     if (verbose)
85       fprintf (stderr, "click x=%d y=%d mask=0x%x\n",
86                self->command.click.x, self->command.click.y,
87                self->command.click.mask);
88
89     r = self->callbacks->click (self,
90                                 self->command.click.x,
91                                 self->command.click.y,
92                                 self->command.click.mask);
93     break;
94   }
95
96   if (r == -1) {
97     fprintf (stderr, "virt-click: error sending command to remote server\n");
98     self->ret = EXIT_FAILURE;
99   }
100
101   /* XXX Shutdown connection. */
102 }