2
0

ui-hmp-cmds.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * HMP commands related to UI
  3. *
  4. * Copyright IBM, Corp. 2011
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. * Contributions after 2012-01-13 are licensed under the terms of the
  13. * GNU GPL, version 2 or (at your option) any later version.
  14. */
  15. #include "qemu/osdep.h"
  16. #ifdef CONFIG_SPICE
  17. #include <spice/enums.h>
  18. #endif
  19. #include "monitor/hmp.h"
  20. #include "monitor/monitor-internal.h"
  21. #include "qapi/error.h"
  22. #include "qapi/qapi-commands-ui.h"
  23. #include "qapi/qmp/qdict.h"
  24. #include "qemu/cutils.h"
  25. #include "ui/console.h"
  26. #include "ui/input.h"
  27. static int mouse_button_state;
  28. void hmp_mouse_move(Monitor *mon, const QDict *qdict)
  29. {
  30. int dx, dy, dz, button;
  31. const char *dx_str = qdict_get_str(qdict, "dx_str");
  32. const char *dy_str = qdict_get_str(qdict, "dy_str");
  33. const char *dz_str = qdict_get_try_str(qdict, "dz_str");
  34. dx = strtol(dx_str, NULL, 0);
  35. dy = strtol(dy_str, NULL, 0);
  36. qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
  37. qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
  38. if (dz_str) {
  39. dz = strtol(dz_str, NULL, 0);
  40. if (dz != 0) {
  41. button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
  42. qemu_input_queue_btn(NULL, button, true);
  43. qemu_input_event_sync();
  44. qemu_input_queue_btn(NULL, button, false);
  45. }
  46. }
  47. qemu_input_event_sync();
  48. }
  49. void hmp_mouse_button(Monitor *mon, const QDict *qdict)
  50. {
  51. static uint32_t bmap[INPUT_BUTTON__MAX] = {
  52. [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
  53. [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
  54. [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
  55. };
  56. int button_state = qdict_get_int(qdict, "button_state");
  57. if (mouse_button_state == button_state) {
  58. return;
  59. }
  60. qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
  61. qemu_input_event_sync();
  62. mouse_button_state = button_state;
  63. }
  64. void hmp_mouse_set(Monitor *mon, const QDict *qdict)
  65. {
  66. Error *err = NULL;
  67. qemu_mouse_set(qdict_get_int(qdict, "index"), &err);
  68. hmp_handle_error(mon, err);
  69. }
  70. void hmp_info_mice(Monitor *mon, const QDict *qdict)
  71. {
  72. MouseInfoList *mice_list, *mouse;
  73. mice_list = qmp_query_mice(NULL);
  74. if (!mice_list) {
  75. monitor_printf(mon, "No mouse devices connected\n");
  76. return;
  77. }
  78. for (mouse = mice_list; mouse; mouse = mouse->next) {
  79. monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
  80. mouse->value->current ? '*' : ' ',
  81. mouse->value->index, mouse->value->name,
  82. mouse->value->absolute ? " (absolute)" : "");
  83. }
  84. qapi_free_MouseInfoList(mice_list);
  85. }
  86. #ifdef CONFIG_VNC
  87. /* Helper for hmp_info_vnc_clients, _servers */
  88. static void hmp_info_VncBasicInfo(Monitor *mon, VncBasicInfo *info,
  89. const char *name)
  90. {
  91. monitor_printf(mon, " %s: %s:%s (%s%s)\n",
  92. name,
  93. info->host,
  94. info->service,
  95. NetworkAddressFamily_str(info->family),
  96. info->websocket ? " (Websocket)" : "");
  97. }
  98. /* Helper displaying and auth and crypt info */
  99. static void hmp_info_vnc_authcrypt(Monitor *mon, const char *indent,
  100. VncPrimaryAuth auth,
  101. VncVencryptSubAuth *vencrypt)
  102. {
  103. monitor_printf(mon, "%sAuth: %s (Sub: %s)\n", indent,
  104. VncPrimaryAuth_str(auth),
  105. vencrypt ? VncVencryptSubAuth_str(*vencrypt) : "none");
  106. }
  107. static void hmp_info_vnc_clients(Monitor *mon, VncClientInfoList *client)
  108. {
  109. while (client) {
  110. VncClientInfo *cinfo = client->value;
  111. hmp_info_VncBasicInfo(mon, qapi_VncClientInfo_base(cinfo), "Client");
  112. monitor_printf(mon, " x509_dname: %s\n",
  113. cinfo->x509_dname ?: "none");
  114. monitor_printf(mon, " sasl_username: %s\n",
  115. cinfo->sasl_username ?: "none");
  116. client = client->next;
  117. }
  118. }
  119. static void hmp_info_vnc_servers(Monitor *mon, VncServerInfo2List *server)
  120. {
  121. while (server) {
  122. VncServerInfo2 *sinfo = server->value;
  123. hmp_info_VncBasicInfo(mon, qapi_VncServerInfo2_base(sinfo), "Server");
  124. hmp_info_vnc_authcrypt(mon, " ", sinfo->auth,
  125. sinfo->has_vencrypt ? &sinfo->vencrypt : NULL);
  126. server = server->next;
  127. }
  128. }
  129. void hmp_info_vnc(Monitor *mon, const QDict *qdict)
  130. {
  131. VncInfo2List *info2l, *info2l_head;
  132. Error *err = NULL;
  133. info2l = qmp_query_vnc_servers(&err);
  134. info2l_head = info2l;
  135. if (hmp_handle_error(mon, err)) {
  136. return;
  137. }
  138. if (!info2l) {
  139. monitor_printf(mon, "None\n");
  140. return;
  141. }
  142. while (info2l) {
  143. VncInfo2 *info = info2l->value;
  144. monitor_printf(mon, "%s:\n", info->id);
  145. hmp_info_vnc_servers(mon, info->server);
  146. hmp_info_vnc_clients(mon, info->clients);
  147. if (!info->server) {
  148. /*
  149. * The server entry displays its auth, we only need to
  150. * display in the case of 'reverse' connections where
  151. * there's no server.
  152. */
  153. hmp_info_vnc_authcrypt(mon, " ", info->auth,
  154. info->has_vencrypt ? &info->vencrypt : NULL);
  155. }
  156. if (info->display) {
  157. monitor_printf(mon, " Display: %s\n", info->display);
  158. }
  159. info2l = info2l->next;
  160. }
  161. qapi_free_VncInfo2List(info2l_head);
  162. }
  163. #endif
  164. #ifdef CONFIG_SPICE
  165. void hmp_info_spice(Monitor *mon, const QDict *qdict)
  166. {
  167. SpiceChannelList *chan;
  168. SpiceInfo *info;
  169. const char *channel_name;
  170. static const char *const channel_names[] = {
  171. [SPICE_CHANNEL_MAIN] = "main",
  172. [SPICE_CHANNEL_DISPLAY] = "display",
  173. [SPICE_CHANNEL_INPUTS] = "inputs",
  174. [SPICE_CHANNEL_CURSOR] = "cursor",
  175. [SPICE_CHANNEL_PLAYBACK] = "playback",
  176. [SPICE_CHANNEL_RECORD] = "record",
  177. [SPICE_CHANNEL_TUNNEL] = "tunnel",
  178. [SPICE_CHANNEL_SMARTCARD] = "smartcard",
  179. [SPICE_CHANNEL_USBREDIR] = "usbredir",
  180. [SPICE_CHANNEL_PORT] = "port",
  181. [SPICE_CHANNEL_WEBDAV] = "webdav",
  182. };
  183. info = qmp_query_spice(NULL);
  184. if (!info->enabled) {
  185. monitor_printf(mon, "Server: disabled\n");
  186. goto out;
  187. }
  188. monitor_printf(mon, "Server:\n");
  189. if (info->has_port) {
  190. monitor_printf(mon, " address: %s:%" PRId64 "\n",
  191. info->host, info->port);
  192. }
  193. if (info->has_tls_port) {
  194. monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
  195. info->host, info->tls_port);
  196. }
  197. monitor_printf(mon, " migrated: %s\n",
  198. info->migrated ? "true" : "false");
  199. monitor_printf(mon, " auth: %s\n", info->auth);
  200. monitor_printf(mon, " compiled: %s\n", info->compiled_version);
  201. monitor_printf(mon, " mouse-mode: %s\n",
  202. SpiceQueryMouseMode_str(info->mouse_mode));
  203. if (!info->has_channels || info->channels == NULL) {
  204. monitor_printf(mon, "Channels: none\n");
  205. } else {
  206. for (chan = info->channels; chan; chan = chan->next) {
  207. monitor_printf(mon, "Channel:\n");
  208. monitor_printf(mon, " address: %s:%s%s\n",
  209. chan->value->host, chan->value->port,
  210. chan->value->tls ? " [tls]" : "");
  211. monitor_printf(mon, " session: %" PRId64 "\n",
  212. chan->value->connection_id);
  213. monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
  214. chan->value->channel_type, chan->value->channel_id);
  215. channel_name = "unknown";
  216. if (chan->value->channel_type > 0 &&
  217. chan->value->channel_type < ARRAY_SIZE(channel_names) &&
  218. channel_names[chan->value->channel_type]) {
  219. channel_name = channel_names[chan->value->channel_type];
  220. }
  221. monitor_printf(mon, " channel name: %s\n", channel_name);
  222. }
  223. }
  224. out:
  225. qapi_free_SpiceInfo(info);
  226. }
  227. #endif
  228. void hmp_set_password(Monitor *mon, const QDict *qdict)
  229. {
  230. const char *protocol = qdict_get_str(qdict, "protocol");
  231. const char *password = qdict_get_str(qdict, "password");
  232. const char *display = qdict_get_try_str(qdict, "display");
  233. const char *connected = qdict_get_try_str(qdict, "connected");
  234. Error *err = NULL;
  235. SetPasswordOptions opts = {
  236. .password = (char *)password,
  237. .has_connected = !!connected,
  238. };
  239. opts.connected = qapi_enum_parse(&SetPasswordAction_lookup, connected,
  240. SET_PASSWORD_ACTION_KEEP, &err);
  241. if (err) {
  242. goto out;
  243. }
  244. opts.protocol = qapi_enum_parse(&DisplayProtocol_lookup, protocol,
  245. DISPLAY_PROTOCOL_VNC, &err);
  246. if (err) {
  247. goto out;
  248. }
  249. if (opts.protocol == DISPLAY_PROTOCOL_VNC) {
  250. opts.u.vnc.display = (char *)display;
  251. }
  252. qmp_set_password(&opts, &err);
  253. out:
  254. hmp_handle_error(mon, err);
  255. }
  256. void hmp_expire_password(Monitor *mon, const QDict *qdict)
  257. {
  258. const char *protocol = qdict_get_str(qdict, "protocol");
  259. const char *whenstr = qdict_get_str(qdict, "time");
  260. const char *display = qdict_get_try_str(qdict, "display");
  261. Error *err = NULL;
  262. ExpirePasswordOptions opts = {
  263. .time = (char *)whenstr,
  264. };
  265. opts.protocol = qapi_enum_parse(&DisplayProtocol_lookup, protocol,
  266. DISPLAY_PROTOCOL_VNC, &err);
  267. if (err) {
  268. goto out;
  269. }
  270. if (opts.protocol == DISPLAY_PROTOCOL_VNC) {
  271. opts.u.vnc.display = (char *)display;
  272. }
  273. qmp_expire_password(&opts, &err);
  274. out:
  275. hmp_handle_error(mon, err);
  276. }
  277. #ifdef CONFIG_VNC
  278. static void hmp_change_read_arg(void *opaque, const char *password,
  279. void *readline_opaque)
  280. {
  281. qmp_change_vnc_password(password, NULL);
  282. monitor_read_command(opaque, 1);
  283. }
  284. void hmp_change_vnc(Monitor *mon, const char *device, const char *target,
  285. const char *arg, const char *read_only, bool force,
  286. Error **errp)
  287. {
  288. if (read_only) {
  289. error_setg(errp, "Parameter 'read-only-mode' is invalid for VNC");
  290. return;
  291. }
  292. if (strcmp(target, "passwd") && strcmp(target, "password")) {
  293. error_setg(errp, "Expected 'password' after 'vnc'");
  294. return;
  295. }
  296. if (!arg) {
  297. MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
  298. monitor_read_password(hmp_mon, hmp_change_read_arg, NULL);
  299. } else {
  300. qmp_change_vnc_password(arg, errp);
  301. }
  302. }
  303. #endif
  304. void hmp_sendkey(Monitor *mon, const QDict *qdict)
  305. {
  306. const char *keys = qdict_get_str(qdict, "keys");
  307. KeyValue *v = NULL;
  308. KeyValueList *head = NULL, **tail = &head;
  309. int has_hold_time = qdict_haskey(qdict, "hold-time");
  310. int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
  311. Error *err = NULL;
  312. const char *separator;
  313. int keyname_len;
  314. while (1) {
  315. separator = qemu_strchrnul(keys, '-');
  316. keyname_len = separator - keys;
  317. /* Be compatible with old interface, convert user inputted "<" */
  318. if (keys[0] == '<' && keyname_len == 1) {
  319. keys = "less";
  320. keyname_len = 4;
  321. }
  322. v = g_malloc0(sizeof(*v));
  323. if (strstart(keys, "0x", NULL)) {
  324. const char *endp;
  325. int value;
  326. if (qemu_strtoi(keys, &endp, 0, &value) < 0) {
  327. goto err_out;
  328. }
  329. assert(endp <= keys + keyname_len);
  330. if (endp != keys + keyname_len) {
  331. goto err_out;
  332. }
  333. v->type = KEY_VALUE_KIND_NUMBER;
  334. v->u.number.data = value;
  335. } else {
  336. int idx = index_from_key(keys, keyname_len);
  337. if (idx == Q_KEY_CODE__MAX) {
  338. goto err_out;
  339. }
  340. v->type = KEY_VALUE_KIND_QCODE;
  341. v->u.qcode.data = idx;
  342. }
  343. QAPI_LIST_APPEND(tail, v);
  344. v = NULL;
  345. if (!*separator) {
  346. break;
  347. }
  348. keys = separator + 1;
  349. }
  350. qmp_send_key(head, has_hold_time, hold_time, &err);
  351. hmp_handle_error(mon, err);
  352. out:
  353. qapi_free_KeyValue(v);
  354. qapi_free_KeyValueList(head);
  355. return;
  356. err_out:
  357. monitor_printf(mon, "invalid parameter: %.*s\n", keyname_len, keys);
  358. goto out;
  359. }
  360. void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
  361. {
  362. int i;
  363. char *sep;
  364. size_t len;
  365. if (nb_args != 2) {
  366. return;
  367. }
  368. sep = strrchr(str, '-');
  369. if (sep) {
  370. str = sep + 1;
  371. }
  372. len = strlen(str);
  373. readline_set_completion_index(rs, len);
  374. for (i = 0; i < Q_KEY_CODE__MAX; i++) {
  375. if (!strncmp(str, QKeyCode_str(i), len)) {
  376. readline_add_completion(rs, QKeyCode_str(i));
  377. }
  378. }
  379. }
  380. void coroutine_fn
  381. hmp_screendump(Monitor *mon, const QDict *qdict)
  382. {
  383. const char *filename = qdict_get_str(qdict, "filename");
  384. const char *id = qdict_get_try_str(qdict, "device");
  385. int64_t head = qdict_get_try_int(qdict, "head", 0);
  386. const char *input_format = qdict_get_try_str(qdict, "format");
  387. Error *err = NULL;
  388. ImageFormat format;
  389. format = qapi_enum_parse(&ImageFormat_lookup, input_format,
  390. IMAGE_FORMAT_PPM, &err);
  391. if (err) {
  392. goto end;
  393. }
  394. qmp_screendump(filename, id, id != NULL, head,
  395. input_format != NULL, format, &err);
  396. end:
  397. hmp_handle_error(mon, err);
  398. }