vdagent FD_SET -> FD_ISSET
[vd_agent/hramrach.git] / vdagent.c
blob4d96fd5f4a38059c323ad2436c8a5d293b3ff858
1 /* vdagent.c xorg-client to vdagentd (daemon).
3 Copyright 2010 Red Hat, Inc.
5 Red Hat Authors:
6 Hans de Goede <hdegoede@redhat.com>
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <sys/select.h>
28 #include <spice/vd_agent.h>
30 #include "udscs.h"
31 #include "vdagentd-proto.h"
32 #include "vdagentd-proto-strings.h"
33 #include "vdagent-x11.h"
35 static int verbose = 0;
36 static struct vdagent_x11 *x11 = NULL;
38 int daemon_read_complete(struct udscs_connection *conn,
39 struct udscs_message_header *header, const uint8_t *data)
41 switch (header->type) {
42 case VDAGENTD_MONITORS_CONFIG:
43 vdagent_x11_set_monitor_config(x11, (VDAgentMonitorsConfig *)data);
44 break;
45 case VDAGENTD_CLIPBOARD_REQUEST:
46 vdagent_x11_clipboard_request(x11, header->opaque);
47 break;
48 case VDAGENTD_CLIPBOARD_GRAB:
49 vdagent_x11_clipboard_grab(x11, (uint32_t *)data,
50 header->size / sizeof(uint32_t));
51 break;
52 case VDAGENTD_CLIPBOARD_DATA:
53 vdagent_x11_clipboard_data(x11, header->opaque, data, header->size);
54 break;
55 case VDAGENTD_CLIPBOARD_RELEASE:
56 vdagent_x11_clipboard_release(x11);
57 break;
58 default:
59 if (verbose)
60 fprintf(stderr, "Unknown message from vdagentd type: %d\n",
61 header->type);
63 return 0;
66 static void usage(FILE *fp)
68 fprintf(fp,
69 "vdagent -- spice agent xorg client\n"
70 "options:\n"
71 " -h print this text\n"
72 " -d print debug messages\n");
75 int main(int argc, char *argv[])
77 struct udscs_connection *client = NULL;
78 fd_set readfds, writefds;
79 int c, n, nfds, x11_fd;
81 for (;;) {
82 if (-1 == (c = getopt(argc, argv, "dh")))
83 break;
84 switch (c) {
85 case 'd':
86 verbose++;
87 break;
88 case 'h':
89 usage(stdout);
90 exit(0);
91 default:
92 usage(stderr);
93 exit(1);
97 client = udscs_connect(VDAGENTD_SOCKET, daemon_read_complete, NULL,
98 vdagentd_messages, VDAGENTD_NO_MESSAGES,
99 verbose? stderr:NULL, stderr);
100 if (!client)
101 exit(1);
103 x11 = vdagent_x11_create(client, verbose);
104 if (!x11) {
105 udscs_destroy_connection(&client);
106 exit(1);
109 while (client) {
110 FD_ZERO(&readfds);
111 FD_ZERO(&writefds);
113 nfds = udscs_client_fill_fds(client, &readfds, &writefds);
114 x11_fd = vdagent_x11_get_fd(x11);
115 FD_SET(x11_fd, &readfds);
116 if (x11_fd >= nfds)
117 nfds = x11_fd + 1;
119 n = select(nfds, &readfds, &writefds, NULL, NULL);
120 if (n == -1) {
121 if (errno == EINTR)
122 continue;
123 perror("select");
124 exit(1);
127 if (FD_ISSET(x11_fd, &readfds))
128 vdagent_x11_do_read(x11);
129 udscs_client_handle_fds(&client, &readfds, &writefds);
132 vdagent_x11_destroy(x11);
134 return 0;