1 /*
2  * Copyright (C) 2016 EPAM Systems Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; version 2.1 only. with the special
7  * exception on linking described in file LICENSE.
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 Lesser General Public License for more details.
13  */
14 
15 #include <stdlib.h>
16 
17 #include <libxl.h>
18 #include <libxl_utils.h>
19 #include <libxlutil.h>
20 
21 #include <xen/io/kbdif.h>
22 
23 #include "xl.h"
24 #include "xl_utils.h"
25 #include "xl_parse.h"
26 
main_vkbattach(int argc,char ** argv)27 int main_vkbattach(int argc, char **argv)
28 {
29     int opt;
30     int rc;
31     uint32_t domid;
32     libxl_device_vkb vkb;
33 
34     SWITCH_FOREACH_OPT(opt, "", NULL, "vkb-attach", 2) {
35         /* No options */
36     }
37 
38     libxl_device_vkb_init(&vkb);
39     domid = find_domain(argv[optind++]);
40 
41     for (argv += optind, argc -= optind; argc > 0; ++argv, --argc) {
42         rc = parse_vkb_config(&vkb, *argv);
43         if (rc) goto out;
44     }
45 
46     if (vkb.backend_type == LIBXL_VKB_BACKEND_UNKNOWN) {
47         fprintf(stderr, "backend-type should be set\n");
48         rc = ERROR_FAIL; goto out;
49     }
50 
51     if (vkb.multi_touch_height || vkb.multi_touch_width ||
52         vkb.multi_touch_num_contacts) {
53         vkb.feature_multi_touch = true;
54     }
55 
56     if (vkb.feature_multi_touch && !(vkb.multi_touch_height ||
57         vkb.multi_touch_width || vkb.multi_touch_num_contacts)) {
58         fprintf(stderr, XENKBD_FIELD_MT_WIDTH", "XENKBD_FIELD_MT_HEIGHT", "
59                         XENKBD_FIELD_MT_NUM_CONTACTS" should be set\n");
60         rc = ERROR_FAIL; goto out;
61     }
62 
63     if (dryrun_only) {
64         char *json = libxl_device_vkb_to_json(ctx, &vkb);
65         printf("vkb: %s\n", json);
66         free(json);
67         goto out;
68     }
69 
70     if (libxl_device_vkb_add(ctx, domid, &vkb, 0)) {
71         fprintf(stderr, "libxl_device_vkb_add failed.\n");
72         rc = ERROR_FAIL; goto out;
73     }
74 
75     rc = 0;
76 
77 out:
78     libxl_device_vkb_dispose(&vkb);
79     return rc;
80 }
81 
main_vkblist(int argc,char ** argv)82 int main_vkblist(int argc, char **argv)
83 {
84     int opt;
85     libxl_device_vkb *vkbs;
86     libxl_vkbinfo vkbinfo;
87     int nb, i;
88 
89     SWITCH_FOREACH_OPT(opt, "", NULL, "vkb-list", 1) {
90         /* No options */
91     }
92 
93     /*      Idx  BE   Hdl  Sta  evch ref ID    BE-type BE-path */
94     printf("%-3s %-2s %-6s %-5s %-6s %6s %-10s %-10s %-30s\n",
95            "Idx", "BE", "handle", "state", "evt-ch", "ref",
96            "ID", "BE-type", "BE-path");
97     for (argv += optind, argc -= optind; argc > 0; --argc, ++argv) {
98         uint32_t domid = find_domain(*argv);
99         vkbs = libxl_device_vkb_list(ctx, domid, &nb);
100         if (!vkbs) {
101             continue;
102         }
103         for (i = 0; i < nb; ++i) {
104             if (libxl_device_vkb_getinfo(ctx, domid, &vkbs[i], &vkbinfo) == 0) {
105                 printf("%-3d %-2d %6d %5d %6d %6d %-10s %-10s %-30s\n",
106                        vkbinfo.devid, vkbinfo.backend_id,
107                        vkbinfo.devid, vkbinfo.state, vkbinfo.evtch,
108                        vkbinfo.rref, vkbs[i].unique_id,
109                        libxl_vkb_backend_to_string(vkbs[i].backend_type),
110                        vkbinfo.backend);
111                 libxl_vkbinfo_dispose(&vkbinfo);
112             }
113         }
114         libxl_device_vkb_list_free(vkbs, nb);
115     }
116     return 0;
117 }
118 
main_vkbdetach(int argc,char ** argv)119 int main_vkbdetach(int argc, char **argv)
120 {
121     uint32_t domid, devid;
122     int opt, rc;
123     libxl_device_vkb vkb;
124 
125     SWITCH_FOREACH_OPT(opt, "", NULL, "vkb-detach", 2) {
126         /* No options */
127     }
128 
129     domid = find_domain(argv[optind++]);
130     devid = atoi(argv[optind++]);
131 
132     libxl_device_vkb_init(&vkb);
133 
134     if (libxl_devid_to_device_vkb(ctx, domid, devid, &vkb)) {
135         fprintf(stderr, "Error: Device %d not connected.\n", devid);
136         rc = ERROR_FAIL;
137         goto out;
138     }
139 
140     rc = libxl_device_vkb_remove(ctx, domid, &vkb, 0);
141     if (rc) {
142         fprintf(stderr, "libxl_device_vkb_remove failed.\n");
143         rc = ERROR_FAIL;
144         goto out;
145     }
146 
147     rc = 0;
148 
149 out:
150     libxl_device_vkb_dispose(&vkb);
151     return rc;
152 }
153 
154 
155 /*
156  * Local variables:
157  * mode: C
158  * c-basic-offset: 4
159  * indent-tabs-mode: nil
160  * End:
161  */
162