2
0

spice-app.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * QEMU external Spice client display driver
  3. *
  4. * Copyright (c) 2018 Marc-André Lureau <marcandre.lureau@redhat.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include <gio/gio.h>
  26. #include "ui/console.h"
  27. #include "qemu/config-file.h"
  28. #include "qemu/option.h"
  29. #include "qemu/cutils.h"
  30. #include "qemu/module.h"
  31. #include "qapi/error.h"
  32. #include "io/channel-command.h"
  33. #include "chardev/spice.h"
  34. #include "sysemu/sysemu.h"
  35. static const char *tmp_dir;
  36. static char *app_dir;
  37. static char *sock_path;
  38. typedef struct VCChardev {
  39. SpiceChardev parent;
  40. } VCChardev;
  41. #define TYPE_CHARDEV_VC "chardev-vc"
  42. #define VC_CHARDEV(obj) OBJECT_CHECK(VCChardev, (obj), TYPE_CHARDEV_VC)
  43. static ChardevBackend *
  44. chr_spice_backend_new(void)
  45. {
  46. ChardevBackend *be = g_new0(ChardevBackend, 1);
  47. be->type = CHARDEV_BACKEND_KIND_SPICEPORT;
  48. be->u.spiceport.data = g_new0(ChardevSpicePort, 1);
  49. return be;
  50. }
  51. static void vc_chr_open(Chardev *chr,
  52. ChardevBackend *backend,
  53. bool *be_opened,
  54. Error **errp)
  55. {
  56. ChardevBackend *be;
  57. const char *fqdn = NULL;
  58. if (strstart(chr->label, "serial", NULL)) {
  59. fqdn = "org.qemu.console.serial.0";
  60. } else if (strstart(chr->label, "parallel", NULL)) {
  61. fqdn = "org.qemu.console.parallel.0";
  62. } else if (strstart(chr->label, "compat_monitor", NULL)) {
  63. fqdn = "org.qemu.monitor.hmp.0";
  64. }
  65. be = chr_spice_backend_new();
  66. be->u.spiceport.data->fqdn = fqdn ?
  67. g_strdup(fqdn) : g_strdup_printf("org.qemu.console.%s", chr->label);
  68. qemu_chr_open_spice_port(chr, be, be_opened, errp);
  69. qapi_free_ChardevBackend(be);
  70. }
  71. static void vc_chr_set_echo(Chardev *chr, bool echo)
  72. {
  73. /* TODO: set echo for frontends QMP and qtest */
  74. }
  75. static void char_vc_class_init(ObjectClass *oc, void *data)
  76. {
  77. ChardevClass *cc = CHARDEV_CLASS(oc);
  78. cc->parse = qemu_chr_parse_vc;
  79. cc->open = vc_chr_open;
  80. cc->chr_set_echo = vc_chr_set_echo;
  81. }
  82. static const TypeInfo char_vc_type_info = {
  83. .name = TYPE_CHARDEV_VC,
  84. .parent = TYPE_CHARDEV_SPICEPORT,
  85. .instance_size = sizeof(VCChardev),
  86. .class_init = char_vc_class_init,
  87. };
  88. static void spice_app_atexit(void)
  89. {
  90. if (sock_path) {
  91. unlink(sock_path);
  92. }
  93. if (tmp_dir) {
  94. rmdir(tmp_dir);
  95. }
  96. g_free(sock_path);
  97. g_free(app_dir);
  98. }
  99. static void spice_app_display_early_init(DisplayOptions *opts)
  100. {
  101. QemuOpts *qopts;
  102. ChardevBackend *be = chr_spice_backend_new();
  103. GError *err = NULL;
  104. if (opts->has_full_screen) {
  105. error_report("spice-app full-screen isn't supported yet.");
  106. exit(1);
  107. }
  108. if (opts->has_window_close) {
  109. error_report("spice-app window-close isn't supported yet.");
  110. exit(1);
  111. }
  112. atexit(spice_app_atexit);
  113. if (qemu_name) {
  114. app_dir = g_build_filename(g_get_user_runtime_dir(),
  115. "qemu", qemu_name, NULL);
  116. if (g_mkdir_with_parents(app_dir, S_IRWXU) < -1) {
  117. error_report("Failed to create directory %s: %s",
  118. app_dir, strerror(errno));
  119. exit(1);
  120. }
  121. } else {
  122. app_dir = g_dir_make_tmp(NULL, &err);
  123. tmp_dir = app_dir;
  124. if (err) {
  125. error_report("Failed to create temporary directory: %s",
  126. err->message);
  127. exit(1);
  128. }
  129. }
  130. type_register(&char_vc_type_info);
  131. sock_path = g_strjoin("", app_dir, "/", "spice.sock", NULL);
  132. qopts = qemu_opts_create(qemu_find_opts("spice"), NULL, 0, &error_abort);
  133. qemu_opt_set(qopts, "disable-ticketing", "on", &error_abort);
  134. qemu_opt_set(qopts, "unix", "on", &error_abort);
  135. qemu_opt_set(qopts, "addr", sock_path, &error_abort);
  136. qemu_opt_set(qopts, "image-compression", "off", &error_abort);
  137. qemu_opt_set(qopts, "streaming-video", "off", &error_abort);
  138. #ifdef CONFIG_OPENGL
  139. qemu_opt_set(qopts, "gl", opts->has_gl ? "on" : "off", &error_abort);
  140. display_opengl = opts->has_gl;
  141. #endif
  142. be->u.spiceport.data->fqdn = g_strdup("org.qemu.monitor.qmp.0");
  143. qemu_chardev_new("org.qemu.monitor.qmp", TYPE_CHARDEV_SPICEPORT,
  144. be, NULL, &error_abort);
  145. qopts = qemu_opts_create(qemu_find_opts("mon"),
  146. NULL, 0, &error_fatal);
  147. qemu_opt_set(qopts, "chardev", "org.qemu.monitor.qmp", &error_abort);
  148. qemu_opt_set(qopts, "mode", "control", &error_abort);
  149. qapi_free_ChardevBackend(be);
  150. }
  151. static void spice_app_display_init(DisplayState *ds, DisplayOptions *opts)
  152. {
  153. GError *err = NULL;
  154. gchar *uri;
  155. uri = g_strjoin("", "spice+unix://", app_dir, "/", "spice.sock", NULL);
  156. info_report("Launching display with URI: %s", uri);
  157. g_app_info_launch_default_for_uri(uri, NULL, &err);
  158. if (err) {
  159. error_report("Failed to launch %s URI: %s", uri, err->message);
  160. error_report("You need a capable Spice client, "
  161. "such as virt-viewer 8.0");
  162. exit(1);
  163. }
  164. g_free(uri);
  165. }
  166. static QemuDisplay qemu_display_spice_app = {
  167. .type = DISPLAY_TYPE_SPICE_APP,
  168. .early_init = spice_app_display_early_init,
  169. .init = spice_app_display_init,
  170. };
  171. static void register_spice_app(void)
  172. {
  173. qemu_display_register(&qemu_display_spice_app);
  174. }
  175. type_init(register_spice_app);