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 done;
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 done:
76     rc = 0;
77 
78 out:
79     libxl_device_vkb_dispose(&vkb);
80     return rc;
81 }
82 
main_vkblist(int argc,char ** argv)83 int main_vkblist(int argc, char **argv)
84 {
85     int opt;
86     libxl_device_vkb *vkbs;
87     libxl_vkbinfo vkbinfo;
88     int nb, i;
89 
90     SWITCH_FOREACH_OPT(opt, "", NULL, "vkb-list", 1) {
91         /* No options */
92     }
93 
94     /*      Idx  BE   Hdl  Sta  evch ref ID    BE-type BE-path */
95     printf("%-3s %-2s %-6s %-5s %-6s %6s %-10s %-10s %-30s\n",
96            "Idx", "BE", "handle", "state", "evt-ch", "ref",
97            "ID", "BE-type", "BE-path");
98     for (argv += optind, argc -= optind; argc > 0; --argc, ++argv) {
99         uint32_t domid = find_domain(*argv);
100         vkbs = libxl_device_vkb_list(ctx, domid, &nb);
101         if (!vkbs) {
102             continue;
103         }
104         for (i = 0; i < nb; ++i) {
105             if (libxl_device_vkb_getinfo(ctx, domid, &vkbs[i], &vkbinfo) == 0) {
106                 printf("%-3d %-2d %6d %5d %6d %6d %-10s %-10s %-30s\n",
107                        vkbinfo.devid, vkbinfo.backend_id,
108                        vkbinfo.devid, vkbinfo.state, vkbinfo.evtch,
109                        vkbinfo.rref, vkbs[i].unique_id,
110                        libxl_vkb_backend_to_string(vkbs[i].backend_type),
111                        vkbinfo.backend);
112                 libxl_vkbinfo_dispose(&vkbinfo);
113             }
114         }
115         libxl_device_vkb_list_free(vkbs, nb);
116     }
117     return 0;
118 }
119 
main_vkbdetach(int argc,char ** argv)120 int main_vkbdetach(int argc, char **argv)
121 {
122     uint32_t domid, devid;
123     int opt, rc;
124     libxl_device_vkb vkb;
125 
126     SWITCH_FOREACH_OPT(opt, "", NULL, "vkb-detach", 2) {
127         /* No options */
128     }
129 
130     domid = find_domain(argv[optind++]);
131     devid = atoi(argv[optind++]);
132 
133     libxl_device_vkb_init(&vkb);
134 
135     if (libxl_devid_to_device_vkb(ctx, domid, devid, &vkb)) {
136         fprintf(stderr, "Error: Device %d not connected.\n", devid);
137         rc = ERROR_FAIL;
138         goto out;
139     }
140 
141     rc = libxl_device_vkb_remove(ctx, domid, &vkb, 0);
142     if (rc) {
143         fprintf(stderr, "libxl_device_vkb_remove failed.\n");
144         rc = ERROR_FAIL;
145         goto out;
146     }
147 
148     rc = 0;
149 
150 out:
151     libxl_device_vkb_dispose(&vkb);
152     return rc;
153 }
154 
155 
156 /*
157  * Local variables:
158  * mode: C
159  * c-basic-offset: 4
160  * indent-tabs-mode: nil
161  * End:
162  */
163