vmware userland helper
1/*
2 * Copyright (c) 2010 joshua stein <jcs@jcs.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include <stdint.h>
18#include <stdlib.h>
19#include <stdio.h>
20#include <err.h>
21#include <unistd.h>
22
23#include "vmwh.h"
24
25void usage(void);
26
27int debug = 0;
28
29int
30main(int argc, char *argv[])
31{
32 int done_init = 0;
33 int was_grabbed = 0;
34 int handle_mouse = 0;
35 int ch;
36
37 x11_verify_xclip_presence();
38
39 while ((ch = getopt(argc, argv, "dm")) != -1)
40 switch (ch) {
41 case 'd':
42 debug = 1;
43 break;
44 case 'm':
45 handle_mouse = 1;
46 break;
47 default:
48 usage();
49 }
50 argc -= optind;
51 argv += optind;
52
53 vmware_check_version();
54
55 vmware_get_mouse_position();
56
57 if (handle_mouse)
58 x11_init();
59
60 for (;;) {
61 was_grabbed = mouse_grabbed;
62 vmware_get_mouse_position();
63
64 if (mouse_grabbed && (!was_grabbed || !done_init)) {
65 /* transitioned from host -> guest */
66 char *clip = NULL;
67
68 if (debug)
69 printf("transitioned from host -> guest\n");
70
71 if (vmware_get_clipboard(&clip)) {
72 x11_set_clipboard(clip);
73 free(clip);
74 }
75
76 if (handle_mouse)
77 x11_set_mouse_position(host_mouse_x,
78 host_mouse_y);
79 }
80
81 else if (!mouse_grabbed && (was_grabbed || !done_init)) {
82 /* transitioned from guest -> host */
83 char *clip = NULL;
84
85 if (debug)
86 printf("transitioned from guest -> host\n");
87
88 if (x11_get_clipboard(&clip)) {
89 vmware_set_clipboard(clip);
90 free(clip);
91 }
92
93 if (handle_mouse)
94 vmware_set_mouse_position(host_mouse_x,
95 host_mouse_y);
96 }
97
98 if (!done_init)
99 done_init = 1;
100
101 usleep(500);
102 }
103
104 return (0);
105}
106
107void
108usage(void)
109{
110 (void)fprintf(stderr, "usage: vmwh [-d] [-m]\n");
111 exit(1);
112}