console.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. /*
  2. * QEMU graphical console
  3. *
  4. * Copyright (c) 2004 Fabrice Bellard
  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 "ui/console.h"
  26. #include "hw/qdev-core.h"
  27. #include "qapi/error.h"
  28. #include "qapi/qapi-commands-ui.h"
  29. #include "qapi/visitor.h"
  30. #include "qemu/coroutine.h"
  31. #include "qemu/error-report.h"
  32. #include "qemu/main-loop.h"
  33. #include "qemu/module.h"
  34. #include "qemu/option.h"
  35. #include "chardev/char.h"
  36. #include "trace.h"
  37. #include "exec/memory.h"
  38. #include "qom/object.h"
  39. #include "qemu/memfd.h"
  40. #include "console-priv.h"
  41. OBJECT_DEFINE_ABSTRACT_TYPE(QemuConsole, qemu_console, QEMU_CONSOLE, OBJECT)
  42. typedef struct QemuGraphicConsole {
  43. QemuConsole parent;
  44. Object *device;
  45. uint32_t head;
  46. QEMUCursor *cursor;
  47. int cursor_x, cursor_y;
  48. bool cursor_on;
  49. } QemuGraphicConsole;
  50. typedef QemuConsoleClass QemuGraphicConsoleClass;
  51. OBJECT_DEFINE_TYPE(QemuGraphicConsole, qemu_graphic_console, QEMU_GRAPHIC_CONSOLE, QEMU_CONSOLE)
  52. struct DisplayState {
  53. QEMUTimer *gui_timer;
  54. uint64_t last_update;
  55. uint64_t update_interval;
  56. bool refreshing;
  57. QLIST_HEAD(, DisplayChangeListener) listeners;
  58. };
  59. static DisplayState *display_state;
  60. static QTAILQ_HEAD(, QemuConsole) consoles =
  61. QTAILQ_HEAD_INITIALIZER(consoles);
  62. static void dpy_refresh(DisplayState *s);
  63. static DisplayState *get_alloc_displaystate(void);
  64. static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl);
  65. static bool console_compatible_with(QemuConsole *con,
  66. DisplayChangeListener *dcl, Error **errp);
  67. static QemuConsole *qemu_graphic_console_lookup_unused(void);
  68. static void dpy_set_ui_info_timer(void *opaque);
  69. static void gui_update(void *opaque)
  70. {
  71. uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
  72. uint64_t dcl_interval;
  73. DisplayState *ds = opaque;
  74. DisplayChangeListener *dcl;
  75. ds->refreshing = true;
  76. dpy_refresh(ds);
  77. ds->refreshing = false;
  78. QLIST_FOREACH(dcl, &ds->listeners, next) {
  79. dcl_interval = dcl->update_interval ?
  80. dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
  81. if (interval > dcl_interval) {
  82. interval = dcl_interval;
  83. }
  84. }
  85. if (ds->update_interval != interval) {
  86. ds->update_interval = interval;
  87. trace_console_refresh(interval);
  88. }
  89. ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  90. timer_mod(ds->gui_timer, ds->last_update + interval);
  91. }
  92. static void gui_setup_refresh(DisplayState *ds)
  93. {
  94. DisplayChangeListener *dcl;
  95. bool need_timer = false;
  96. QLIST_FOREACH(dcl, &ds->listeners, next) {
  97. if (dcl->ops->dpy_refresh != NULL) {
  98. need_timer = true;
  99. }
  100. }
  101. if (need_timer && ds->gui_timer == NULL) {
  102. ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
  103. timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
  104. }
  105. if (!need_timer && ds->gui_timer != NULL) {
  106. timer_free(ds->gui_timer);
  107. ds->gui_timer = NULL;
  108. }
  109. }
  110. void graphic_hw_update_done(QemuConsole *con)
  111. {
  112. if (con) {
  113. qemu_co_enter_all(&con->dump_queue, NULL);
  114. }
  115. }
  116. void graphic_hw_update(QemuConsole *con)
  117. {
  118. bool async = false;
  119. if (!con) {
  120. return;
  121. }
  122. if (con->hw_ops->gfx_update) {
  123. con->hw_ops->gfx_update(con->hw);
  124. async = con->hw_ops->gfx_update_async;
  125. }
  126. if (!async) {
  127. graphic_hw_update_done(con);
  128. }
  129. }
  130. static void graphic_hw_update_bh(void *con)
  131. {
  132. graphic_hw_update(con);
  133. }
  134. void qemu_console_co_wait_update(QemuConsole *con)
  135. {
  136. if (qemu_co_queue_empty(&con->dump_queue)) {
  137. /* Defer the update, it will restart the pending coroutines */
  138. aio_bh_schedule_oneshot(qemu_get_aio_context(),
  139. graphic_hw_update_bh, con);
  140. }
  141. qemu_co_queue_wait(&con->dump_queue, NULL);
  142. }
  143. static void graphic_hw_gl_unblock_timer(void *opaque)
  144. {
  145. warn_report("console: no gl-unblock within one second");
  146. }
  147. void graphic_hw_gl_block(QemuConsole *con, bool block)
  148. {
  149. uint64_t timeout;
  150. assert(con != NULL);
  151. if (block) {
  152. con->gl_block++;
  153. } else {
  154. con->gl_block--;
  155. }
  156. assert(con->gl_block >= 0);
  157. if (!con->hw_ops->gl_block) {
  158. return;
  159. }
  160. if ((block && con->gl_block != 1) || (!block && con->gl_block != 0)) {
  161. return;
  162. }
  163. con->hw_ops->gl_block(con->hw, block);
  164. if (block) {
  165. timeout = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  166. timeout += 1000; /* one sec */
  167. timer_mod(con->gl_unblock_timer, timeout);
  168. } else {
  169. timer_del(con->gl_unblock_timer);
  170. }
  171. }
  172. int qemu_console_get_window_id(QemuConsole *con)
  173. {
  174. return con->window_id;
  175. }
  176. void qemu_console_set_window_id(QemuConsole *con, int window_id)
  177. {
  178. con->window_id = window_id;
  179. }
  180. void graphic_hw_invalidate(QemuConsole *con)
  181. {
  182. if (con && con->hw_ops->invalidate) {
  183. con->hw_ops->invalidate(con->hw);
  184. }
  185. }
  186. void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
  187. {
  188. if (con && con->hw_ops->text_update) {
  189. con->hw_ops->text_update(con->hw, chardata);
  190. }
  191. }
  192. static void displaychangelistener_gfx_switch(DisplayChangeListener *dcl,
  193. struct DisplaySurface *new_surface,
  194. bool update)
  195. {
  196. if (dcl->ops->dpy_gfx_switch) {
  197. dcl->ops->dpy_gfx_switch(dcl, new_surface);
  198. }
  199. if (update && dcl->ops->dpy_gfx_update) {
  200. dcl->ops->dpy_gfx_update(dcl, 0, 0,
  201. surface_width(new_surface),
  202. surface_height(new_surface));
  203. }
  204. }
  205. static void dpy_gfx_create_texture(QemuConsole *con, DisplaySurface *surface)
  206. {
  207. if (con->gl && con->gl->ops->dpy_gl_ctx_create_texture) {
  208. con->gl->ops->dpy_gl_ctx_create_texture(con->gl, surface);
  209. }
  210. }
  211. static void dpy_gfx_destroy_texture(QemuConsole *con, DisplaySurface *surface)
  212. {
  213. if (con->gl && con->gl->ops->dpy_gl_ctx_destroy_texture) {
  214. con->gl->ops->dpy_gl_ctx_destroy_texture(con->gl, surface);
  215. }
  216. }
  217. static void dpy_gfx_update_texture(QemuConsole *con, DisplaySurface *surface,
  218. int x, int y, int w, int h)
  219. {
  220. if (con->gl && con->gl->ops->dpy_gl_ctx_update_texture) {
  221. con->gl->ops->dpy_gl_ctx_update_texture(con->gl, surface, x, y, w, h);
  222. }
  223. }
  224. static void displaychangelistener_display_console(DisplayChangeListener *dcl,
  225. Error **errp)
  226. {
  227. static const char nodev[] =
  228. "This VM has no graphic display device.";
  229. static DisplaySurface *dummy;
  230. QemuConsole *con = dcl->con;
  231. if (!con || !console_compatible_with(con, dcl, errp)) {
  232. if (!dummy) {
  233. dummy = qemu_create_placeholder_surface(640, 480, nodev);
  234. }
  235. if (con) {
  236. dpy_gfx_create_texture(con, dummy);
  237. }
  238. displaychangelistener_gfx_switch(dcl, dummy, TRUE);
  239. return;
  240. }
  241. dpy_gfx_create_texture(con, con->surface);
  242. displaychangelistener_gfx_switch(dcl, con->surface,
  243. con->scanout.kind == SCANOUT_SURFACE);
  244. if (con->scanout.kind == SCANOUT_DMABUF &&
  245. displaychangelistener_has_dmabuf(dcl)) {
  246. dcl->ops->dpy_gl_scanout_dmabuf(dcl, con->scanout.dmabuf);
  247. } else if (con->scanout.kind == SCANOUT_TEXTURE &&
  248. dcl->ops->dpy_gl_scanout_texture) {
  249. dcl->ops->dpy_gl_scanout_texture(dcl,
  250. con->scanout.texture.backing_id,
  251. con->scanout.texture.backing_borrow,
  252. con->scanout.texture.x,
  253. con->scanout.texture.y,
  254. con->scanout.texture.width,
  255. con->scanout.texture.height);
  256. }
  257. }
  258. void qemu_text_console_put_keysym(QemuTextConsole *s, int keysym)
  259. {
  260. qemu_text_console_handle_keysym(s, keysym);
  261. }
  262. static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
  263. [Q_KEY_CODE_UP] = QEMU_KEY_UP,
  264. [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
  265. [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
  266. [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
  267. [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
  268. [Q_KEY_CODE_END] = QEMU_KEY_END,
  269. [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
  270. [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
  271. [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
  272. [Q_KEY_CODE_TAB] = QEMU_KEY_TAB,
  273. [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
  274. };
  275. static const int ctrl_qcode_to_keysym[Q_KEY_CODE__MAX] = {
  276. [Q_KEY_CODE_UP] = QEMU_KEY_CTRL_UP,
  277. [Q_KEY_CODE_DOWN] = QEMU_KEY_CTRL_DOWN,
  278. [Q_KEY_CODE_RIGHT] = QEMU_KEY_CTRL_RIGHT,
  279. [Q_KEY_CODE_LEFT] = QEMU_KEY_CTRL_LEFT,
  280. [Q_KEY_CODE_HOME] = QEMU_KEY_CTRL_HOME,
  281. [Q_KEY_CODE_END] = QEMU_KEY_CTRL_END,
  282. [Q_KEY_CODE_PGUP] = QEMU_KEY_CTRL_PAGEUP,
  283. [Q_KEY_CODE_PGDN] = QEMU_KEY_CTRL_PAGEDOWN,
  284. };
  285. bool qemu_text_console_put_qcode(QemuTextConsole *s, int qcode, bool ctrl)
  286. {
  287. int keysym;
  288. keysym = ctrl ? ctrl_qcode_to_keysym[qcode] : qcode_to_keysym[qcode];
  289. if (keysym == 0) {
  290. return false;
  291. }
  292. qemu_text_console_put_keysym(s, keysym);
  293. return true;
  294. }
  295. void qemu_text_console_put_string(QemuTextConsole *s, const char *str, int len)
  296. {
  297. int i;
  298. for (i = 0; i < len && str[i]; i++) {
  299. qemu_text_console_put_keysym(s, str[i]);
  300. }
  301. }
  302. static void
  303. qemu_console_register(QemuConsole *c)
  304. {
  305. int i;
  306. if (QTAILQ_EMPTY(&consoles)) {
  307. c->index = 0;
  308. QTAILQ_INSERT_TAIL(&consoles, c, next);
  309. } else if (!QEMU_IS_GRAPHIC_CONSOLE(c) || phase_check(PHASE_MACHINE_READY)) {
  310. QemuConsole *last = QTAILQ_LAST(&consoles);
  311. c->index = last->index + 1;
  312. QTAILQ_INSERT_TAIL(&consoles, c, next);
  313. } else {
  314. /*
  315. * HACK: Put graphical consoles before text consoles.
  316. *
  317. * Only do that for coldplugged devices. After initial device
  318. * initialization we will not renumber the consoles any more.
  319. */
  320. QemuConsole *it = QTAILQ_FIRST(&consoles);
  321. while (QTAILQ_NEXT(it, next) != NULL && QEMU_IS_GRAPHIC_CONSOLE(it)) {
  322. it = QTAILQ_NEXT(it, next);
  323. }
  324. if (QEMU_IS_GRAPHIC_CONSOLE(it)) {
  325. /* have no text consoles */
  326. c->index = it->index + 1;
  327. QTAILQ_INSERT_AFTER(&consoles, it, c, next);
  328. } else {
  329. c->index = it->index;
  330. QTAILQ_INSERT_BEFORE(it, c, next);
  331. /* renumber text consoles */
  332. for (i = c->index + 1; it != NULL; it = QTAILQ_NEXT(it, next), i++) {
  333. it->index = i;
  334. }
  335. }
  336. }
  337. }
  338. static void
  339. qemu_console_finalize(Object *obj)
  340. {
  341. QemuConsole *c = QEMU_CONSOLE(obj);
  342. /* TODO: check this code path, and unregister from consoles */
  343. g_clear_pointer(&c->surface, qemu_free_displaysurface);
  344. g_clear_pointer(&c->gl_unblock_timer, timer_free);
  345. g_clear_pointer(&c->ui_timer, timer_free);
  346. }
  347. static void
  348. qemu_console_class_init(ObjectClass *oc, void *data)
  349. {
  350. }
  351. static void
  352. qemu_console_init(Object *obj)
  353. {
  354. QemuConsole *c = QEMU_CONSOLE(obj);
  355. DisplayState *ds = get_alloc_displaystate();
  356. qemu_co_queue_init(&c->dump_queue);
  357. c->ds = ds;
  358. c->window_id = -1;
  359. c->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
  360. dpy_set_ui_info_timer, c);
  361. qemu_console_register(c);
  362. }
  363. static void
  364. qemu_graphic_console_finalize(Object *obj)
  365. {
  366. QemuGraphicConsole *c = QEMU_GRAPHIC_CONSOLE(obj);
  367. g_clear_pointer(&c->device, object_unref);
  368. }
  369. static void
  370. qemu_graphic_console_prop_get_head(Object *obj, Visitor *v, const char *name,
  371. void *opaque, Error **errp)
  372. {
  373. QemuGraphicConsole *c = QEMU_GRAPHIC_CONSOLE(obj);
  374. visit_type_uint32(v, name, &c->head, errp);
  375. }
  376. static void
  377. qemu_graphic_console_class_init(ObjectClass *oc, void *data)
  378. {
  379. object_class_property_add_link(oc, "device", TYPE_DEVICE,
  380. offsetof(QemuGraphicConsole, device),
  381. object_property_allow_set_link,
  382. OBJ_PROP_LINK_STRONG);
  383. object_class_property_add(oc, "head", "uint32",
  384. qemu_graphic_console_prop_get_head,
  385. NULL, NULL, NULL);
  386. }
  387. static void
  388. qemu_graphic_console_init(Object *obj)
  389. {
  390. }
  391. void qemu_displaysurface_set_share_handle(DisplaySurface *surface,
  392. qemu_pixman_shareable handle,
  393. uint32_t offset)
  394. {
  395. assert(surface->share_handle == SHAREABLE_NONE);
  396. surface->share_handle = handle;
  397. surface->share_handle_offset = offset;
  398. }
  399. DisplaySurface *qemu_create_displaysurface(int width, int height)
  400. {
  401. trace_displaysurface_create(width, height);
  402. return qemu_create_displaysurface_from(
  403. width, height,
  404. PIXMAN_x8r8g8b8,
  405. width * 4, NULL
  406. );
  407. }
  408. DisplaySurface *qemu_create_displaysurface_from(int width, int height,
  409. pixman_format_code_t format,
  410. int linesize, uint8_t *data)
  411. {
  412. DisplaySurface *surface = g_new0(DisplaySurface, 1);
  413. trace_displaysurface_create_from(surface, width, height, format);
  414. surface->share_handle = SHAREABLE_NONE;
  415. if (data) {
  416. surface->image = pixman_image_create_bits(format,
  417. width, height,
  418. (void *)data, linesize);
  419. } else {
  420. qemu_pixman_image_new_shareable(&surface->image,
  421. &surface->share_handle,
  422. "displaysurface",
  423. format,
  424. width,
  425. height,
  426. linesize,
  427. &error_abort);
  428. surface->flags = QEMU_ALLOCATED_FLAG;
  429. }
  430. assert(surface->image != NULL);
  431. return surface;
  432. }
  433. DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
  434. {
  435. DisplaySurface *surface = g_new0(DisplaySurface, 1);
  436. trace_displaysurface_create_pixman(surface);
  437. surface->share_handle = SHAREABLE_NONE;
  438. surface->image = pixman_image_ref(image);
  439. return surface;
  440. }
  441. DisplaySurface *qemu_create_placeholder_surface(int w, int h,
  442. const char *msg)
  443. {
  444. DisplaySurface *surface = qemu_create_displaysurface(w, h);
  445. #ifdef CONFIG_PIXMAN
  446. pixman_color_t bg = QEMU_PIXMAN_COLOR_BLACK;
  447. pixman_color_t fg = QEMU_PIXMAN_COLOR_GRAY;
  448. pixman_image_t *glyph;
  449. int len, x, y, i;
  450. len = strlen(msg);
  451. x = (w / FONT_WIDTH - len) / 2;
  452. y = (h / FONT_HEIGHT - 1) / 2;
  453. for (i = 0; i < len; i++) {
  454. glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
  455. qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
  456. x+i, y, FONT_WIDTH, FONT_HEIGHT);
  457. qemu_pixman_image_unref(glyph);
  458. }
  459. #endif
  460. surface->flags |= QEMU_PLACEHOLDER_FLAG;
  461. return surface;
  462. }
  463. void qemu_free_displaysurface(DisplaySurface *surface)
  464. {
  465. if (surface == NULL) {
  466. return;
  467. }
  468. trace_displaysurface_free(surface);
  469. qemu_pixman_image_unref(surface->image);
  470. g_free(surface);
  471. }
  472. bool console_has_gl(QemuConsole *con)
  473. {
  474. return con->gl != NULL;
  475. }
  476. static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl)
  477. {
  478. if (dcl->ops->dpy_has_dmabuf) {
  479. return dcl->ops->dpy_has_dmabuf(dcl);
  480. }
  481. if (dcl->ops->dpy_gl_scanout_dmabuf) {
  482. return true;
  483. }
  484. return false;
  485. }
  486. static bool console_compatible_with(QemuConsole *con,
  487. DisplayChangeListener *dcl, Error **errp)
  488. {
  489. int flags;
  490. flags = con->hw_ops->get_flags ? con->hw_ops->get_flags(con->hw) : 0;
  491. if (console_has_gl(con) &&
  492. !con->gl->ops->dpy_gl_ctx_is_compatible_dcl(con->gl, dcl)) {
  493. error_setg(errp, "Display %s is incompatible with the GL context",
  494. dcl->ops->dpy_name);
  495. return false;
  496. }
  497. if (flags & GRAPHIC_FLAGS_GL &&
  498. !console_has_gl(con)) {
  499. error_setg(errp, "The console requires a GL context.");
  500. return false;
  501. }
  502. if (flags & GRAPHIC_FLAGS_DMABUF &&
  503. !displaychangelistener_has_dmabuf(dcl)) {
  504. error_setg(errp, "The console requires display DMABUF support.");
  505. return false;
  506. }
  507. return true;
  508. }
  509. void console_handle_touch_event(QemuConsole *con,
  510. struct touch_slot touch_slots[INPUT_EVENT_SLOTS_MAX],
  511. uint64_t num_slot,
  512. int width, int height,
  513. double x, double y,
  514. InputMultiTouchType type,
  515. Error **errp)
  516. {
  517. struct touch_slot *slot;
  518. bool needs_sync = false;
  519. int update;
  520. int i;
  521. if (num_slot >= INPUT_EVENT_SLOTS_MAX) {
  522. error_setg(errp,
  523. "Unexpected touch slot number: % " PRId64" >= %d",
  524. num_slot, INPUT_EVENT_SLOTS_MAX);
  525. return;
  526. }
  527. slot = &touch_slots[num_slot];
  528. slot->x = x;
  529. slot->y = y;
  530. if (type == INPUT_MULTI_TOUCH_TYPE_BEGIN) {
  531. slot->tracking_id = num_slot;
  532. }
  533. for (i = 0; i < INPUT_EVENT_SLOTS_MAX; ++i) {
  534. if (i == num_slot) {
  535. update = type;
  536. } else {
  537. update = INPUT_MULTI_TOUCH_TYPE_UPDATE;
  538. }
  539. slot = &touch_slots[i];
  540. if (slot->tracking_id == -1) {
  541. continue;
  542. }
  543. if (update == INPUT_MULTI_TOUCH_TYPE_END) {
  544. slot->tracking_id = -1;
  545. qemu_input_queue_mtt(con, update, i, slot->tracking_id);
  546. needs_sync = true;
  547. } else {
  548. qemu_input_queue_mtt(con, update, i, slot->tracking_id);
  549. qemu_input_queue_btn(con, INPUT_BUTTON_TOUCH, true);
  550. qemu_input_queue_mtt_abs(con,
  551. INPUT_AXIS_X, (int) slot->x,
  552. 0, width,
  553. i, slot->tracking_id);
  554. qemu_input_queue_mtt_abs(con,
  555. INPUT_AXIS_Y, (int) slot->y,
  556. 0, height,
  557. i, slot->tracking_id);
  558. needs_sync = true;
  559. }
  560. }
  561. if (needs_sync) {
  562. qemu_input_event_sync();
  563. }
  564. }
  565. void qemu_console_set_display_gl_ctx(QemuConsole *con, DisplayGLCtx *gl)
  566. {
  567. /* display has opengl support */
  568. assert(con);
  569. if (con->gl) {
  570. error_report("The console already has an OpenGL context.");
  571. exit(1);
  572. }
  573. con->gl = gl;
  574. }
  575. static void
  576. dcl_set_graphic_cursor(DisplayChangeListener *dcl, QemuGraphicConsole *con)
  577. {
  578. if (con && con->cursor && dcl->ops->dpy_cursor_define) {
  579. dcl->ops->dpy_cursor_define(dcl, con->cursor);
  580. }
  581. if (con && dcl->ops->dpy_mouse_set) {
  582. dcl->ops->dpy_mouse_set(dcl, con->cursor_x, con->cursor_y, con->cursor_on);
  583. }
  584. }
  585. void register_displaychangelistener(DisplayChangeListener *dcl)
  586. {
  587. assert(!dcl->ds);
  588. trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
  589. dcl->ds = get_alloc_displaystate();
  590. QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
  591. gui_setup_refresh(dcl->ds);
  592. if (dcl->con) {
  593. dcl->con->dcls++;
  594. }
  595. displaychangelistener_display_console(dcl, &error_fatal);
  596. if (QEMU_IS_GRAPHIC_CONSOLE(dcl->con)) {
  597. dcl_set_graphic_cursor(dcl, QEMU_GRAPHIC_CONSOLE(dcl->con));
  598. } else if (QEMU_IS_TEXT_CONSOLE(dcl->con)) {
  599. qemu_text_console_update_size(QEMU_TEXT_CONSOLE(dcl->con));
  600. }
  601. qemu_text_console_update_cursor();
  602. }
  603. void update_displaychangelistener(DisplayChangeListener *dcl,
  604. uint64_t interval)
  605. {
  606. DisplayState *ds = dcl->ds;
  607. dcl->update_interval = interval;
  608. if (!ds->refreshing && ds->update_interval > interval) {
  609. timer_mod(ds->gui_timer, ds->last_update + interval);
  610. }
  611. }
  612. void unregister_displaychangelistener(DisplayChangeListener *dcl)
  613. {
  614. DisplayState *ds = dcl->ds;
  615. trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
  616. if (dcl->con) {
  617. dcl->con->dcls--;
  618. }
  619. QLIST_REMOVE(dcl, next);
  620. dcl->ds = NULL;
  621. gui_setup_refresh(ds);
  622. }
  623. static void dpy_set_ui_info_timer(void *opaque)
  624. {
  625. QemuConsole *con = opaque;
  626. uint32_t head = qemu_console_get_head(con);
  627. con->hw_ops->ui_info(con->hw, head, &con->ui_info);
  628. }
  629. bool dpy_ui_info_supported(const QemuConsole *con)
  630. {
  631. if (con == NULL) {
  632. return false;
  633. }
  634. return con->hw_ops->ui_info != NULL;
  635. }
  636. const QemuUIInfo *dpy_get_ui_info(const QemuConsole *con)
  637. {
  638. assert(dpy_ui_info_supported(con));
  639. return &con->ui_info;
  640. }
  641. int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info, bool delay)
  642. {
  643. if (!dpy_ui_info_supported(con)) {
  644. return -1;
  645. }
  646. if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
  647. /* nothing changed -- ignore */
  648. return 0;
  649. }
  650. /*
  651. * Typically we get a flood of these as the user resizes the window.
  652. * Wait until the dust has settled (one second without updates), then
  653. * go notify the guest.
  654. */
  655. con->ui_info = *info;
  656. timer_mod(con->ui_timer,
  657. qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + (delay ? 1000 : 0));
  658. return 0;
  659. }
  660. void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
  661. {
  662. DisplayState *s = con->ds;
  663. DisplayChangeListener *dcl;
  664. int width = qemu_console_get_width(con, x + w);
  665. int height = qemu_console_get_height(con, y + h);
  666. x = MAX(x, 0);
  667. y = MAX(y, 0);
  668. x = MIN(x, width);
  669. y = MIN(y, height);
  670. w = MIN(w, width - x);
  671. h = MIN(h, height - y);
  672. if (!qemu_console_is_visible(con)) {
  673. return;
  674. }
  675. dpy_gfx_update_texture(con, con->surface, x, y, w, h);
  676. QLIST_FOREACH(dcl, &s->listeners, next) {
  677. if (con != dcl->con) {
  678. continue;
  679. }
  680. if (dcl->ops->dpy_gfx_update) {
  681. dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
  682. }
  683. }
  684. }
  685. void dpy_gfx_update_full(QemuConsole *con)
  686. {
  687. int w = qemu_console_get_width(con, 0);
  688. int h = qemu_console_get_height(con, 0);
  689. dpy_gfx_update(con, 0, 0, w, h);
  690. }
  691. void dpy_gfx_replace_surface(QemuConsole *con,
  692. DisplaySurface *surface)
  693. {
  694. static const char placeholder_msg[] = "Display output is not active.";
  695. DisplayState *s = con->ds;
  696. DisplaySurface *old_surface = con->surface;
  697. DisplaySurface *new_surface = surface;
  698. DisplayChangeListener *dcl;
  699. int width;
  700. int height;
  701. if (!surface) {
  702. if (old_surface) {
  703. width = surface_width(old_surface);
  704. height = surface_height(old_surface);
  705. } else {
  706. width = 640;
  707. height = 480;
  708. }
  709. new_surface = qemu_create_placeholder_surface(width, height, placeholder_msg);
  710. }
  711. assert(old_surface != new_surface);
  712. con->scanout.kind = SCANOUT_SURFACE;
  713. con->surface = new_surface;
  714. dpy_gfx_create_texture(con, new_surface);
  715. QLIST_FOREACH(dcl, &s->listeners, next) {
  716. if (con != dcl->con) {
  717. continue;
  718. }
  719. displaychangelistener_gfx_switch(dcl, new_surface, surface ? FALSE : TRUE);
  720. }
  721. dpy_gfx_destroy_texture(con, old_surface);
  722. qemu_free_displaysurface(old_surface);
  723. }
  724. bool dpy_gfx_check_format(QemuConsole *con,
  725. pixman_format_code_t format)
  726. {
  727. DisplayChangeListener *dcl;
  728. DisplayState *s = con->ds;
  729. QLIST_FOREACH(dcl, &s->listeners, next) {
  730. if (dcl->con && dcl->con != con) {
  731. /* dcl bound to another console -> skip */
  732. continue;
  733. }
  734. if (dcl->ops->dpy_gfx_check_format) {
  735. if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
  736. return false;
  737. }
  738. } else {
  739. /* default is to allow native 32 bpp only */
  740. if (format != qemu_default_pixman_format(32, true)) {
  741. return false;
  742. }
  743. }
  744. }
  745. return true;
  746. }
  747. static void dpy_refresh(DisplayState *s)
  748. {
  749. DisplayChangeListener *dcl;
  750. QLIST_FOREACH(dcl, &s->listeners, next) {
  751. if (dcl->ops->dpy_refresh) {
  752. dcl->ops->dpy_refresh(dcl);
  753. }
  754. }
  755. }
  756. void dpy_text_cursor(QemuConsole *con, int x, int y)
  757. {
  758. DisplayState *s = con->ds;
  759. DisplayChangeListener *dcl;
  760. if (!qemu_console_is_visible(con)) {
  761. return;
  762. }
  763. QLIST_FOREACH(dcl, &s->listeners, next) {
  764. if (con != dcl->con) {
  765. continue;
  766. }
  767. if (dcl->ops->dpy_text_cursor) {
  768. dcl->ops->dpy_text_cursor(dcl, x, y);
  769. }
  770. }
  771. }
  772. void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
  773. {
  774. DisplayState *s = con->ds;
  775. DisplayChangeListener *dcl;
  776. if (!qemu_console_is_visible(con)) {
  777. return;
  778. }
  779. QLIST_FOREACH(dcl, &s->listeners, next) {
  780. if (con != dcl->con) {
  781. continue;
  782. }
  783. if (dcl->ops->dpy_text_update) {
  784. dcl->ops->dpy_text_update(dcl, x, y, w, h);
  785. }
  786. }
  787. }
  788. void dpy_text_resize(QemuConsole *con, int w, int h)
  789. {
  790. DisplayState *s = con->ds;
  791. DisplayChangeListener *dcl;
  792. if (!qemu_console_is_visible(con)) {
  793. return;
  794. }
  795. QLIST_FOREACH(dcl, &s->listeners, next) {
  796. if (con != dcl->con) {
  797. continue;
  798. }
  799. if (dcl->ops->dpy_text_resize) {
  800. dcl->ops->dpy_text_resize(dcl, w, h);
  801. }
  802. }
  803. }
  804. void dpy_mouse_set(QemuConsole *c, int x, int y, bool on)
  805. {
  806. QemuGraphicConsole *con = QEMU_GRAPHIC_CONSOLE(c);
  807. DisplayState *s = c->ds;
  808. DisplayChangeListener *dcl;
  809. con->cursor_x = x;
  810. con->cursor_y = y;
  811. con->cursor_on = on;
  812. if (!qemu_console_is_visible(c)) {
  813. return;
  814. }
  815. QLIST_FOREACH(dcl, &s->listeners, next) {
  816. if (c != dcl->con) {
  817. continue;
  818. }
  819. if (dcl->ops->dpy_mouse_set) {
  820. dcl->ops->dpy_mouse_set(dcl, x, y, on);
  821. }
  822. }
  823. }
  824. void dpy_cursor_define(QemuConsole *c, QEMUCursor *cursor)
  825. {
  826. QemuGraphicConsole *con = QEMU_GRAPHIC_CONSOLE(c);
  827. DisplayState *s = c->ds;
  828. DisplayChangeListener *dcl;
  829. cursor_unref(con->cursor);
  830. con->cursor = cursor_ref(cursor);
  831. if (!qemu_console_is_visible(c)) {
  832. return;
  833. }
  834. QLIST_FOREACH(dcl, &s->listeners, next) {
  835. if (c != dcl->con) {
  836. continue;
  837. }
  838. if (dcl->ops->dpy_cursor_define) {
  839. dcl->ops->dpy_cursor_define(dcl, cursor);
  840. }
  841. }
  842. }
  843. QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
  844. struct QEMUGLParams *qparams)
  845. {
  846. assert(con->gl);
  847. return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
  848. }
  849. void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
  850. {
  851. assert(con->gl);
  852. con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
  853. }
  854. int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
  855. {
  856. assert(con->gl);
  857. return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
  858. }
  859. void dpy_gl_scanout_disable(QemuConsole *con)
  860. {
  861. DisplayState *s = con->ds;
  862. DisplayChangeListener *dcl;
  863. if (con->scanout.kind != SCANOUT_SURFACE) {
  864. con->scanout.kind = SCANOUT_NONE;
  865. }
  866. QLIST_FOREACH(dcl, &s->listeners, next) {
  867. if (con != dcl->con) {
  868. continue;
  869. }
  870. if (dcl->ops->dpy_gl_scanout_disable) {
  871. dcl->ops->dpy_gl_scanout_disable(dcl);
  872. }
  873. }
  874. }
  875. void dpy_gl_scanout_texture(QemuConsole *con,
  876. uint32_t backing_id,
  877. DisplayGLTextureBorrower backing_borrow,
  878. uint32_t x, uint32_t y,
  879. uint32_t width, uint32_t height)
  880. {
  881. DisplayState *s = con->ds;
  882. DisplayChangeListener *dcl;
  883. con->scanout.kind = SCANOUT_TEXTURE;
  884. con->scanout.texture = (ScanoutTexture) {
  885. backing_id, backing_borrow,
  886. x, y, width, height
  887. };
  888. QLIST_FOREACH(dcl, &s->listeners, next) {
  889. if (con != dcl->con) {
  890. continue;
  891. }
  892. if (dcl->ops->dpy_gl_scanout_texture) {
  893. dcl->ops->dpy_gl_scanout_texture(dcl, backing_id, backing_borrow,
  894. x, y, width, height);
  895. }
  896. }
  897. }
  898. void dpy_gl_scanout_dmabuf(QemuConsole *con,
  899. QemuDmaBuf *dmabuf)
  900. {
  901. DisplayState *s = con->ds;
  902. DisplayChangeListener *dcl;
  903. con->scanout.kind = SCANOUT_DMABUF;
  904. con->scanout.dmabuf = dmabuf;
  905. QLIST_FOREACH(dcl, &s->listeners, next) {
  906. if (con != dcl->con) {
  907. continue;
  908. }
  909. if (dcl->ops->dpy_gl_scanout_dmabuf) {
  910. dcl->ops->dpy_gl_scanout_dmabuf(dcl, dmabuf);
  911. }
  912. }
  913. }
  914. void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
  915. bool have_hot, uint32_t hot_x, uint32_t hot_y)
  916. {
  917. DisplayState *s = con->ds;
  918. DisplayChangeListener *dcl;
  919. QLIST_FOREACH(dcl, &s->listeners, next) {
  920. if (con != dcl->con) {
  921. continue;
  922. }
  923. if (dcl->ops->dpy_gl_cursor_dmabuf) {
  924. dcl->ops->dpy_gl_cursor_dmabuf(dcl, dmabuf,
  925. have_hot, hot_x, hot_y);
  926. }
  927. }
  928. }
  929. void dpy_gl_cursor_position(QemuConsole *con,
  930. uint32_t pos_x, uint32_t pos_y)
  931. {
  932. DisplayState *s = con->ds;
  933. DisplayChangeListener *dcl;
  934. QLIST_FOREACH(dcl, &s->listeners, next) {
  935. if (con != dcl->con) {
  936. continue;
  937. }
  938. if (dcl->ops->dpy_gl_cursor_position) {
  939. dcl->ops->dpy_gl_cursor_position(dcl, pos_x, pos_y);
  940. }
  941. }
  942. }
  943. void dpy_gl_release_dmabuf(QemuConsole *con,
  944. QemuDmaBuf *dmabuf)
  945. {
  946. DisplayState *s = con->ds;
  947. DisplayChangeListener *dcl;
  948. QLIST_FOREACH(dcl, &s->listeners, next) {
  949. if (con != dcl->con) {
  950. continue;
  951. }
  952. if (dcl->ops->dpy_gl_release_dmabuf) {
  953. dcl->ops->dpy_gl_release_dmabuf(dcl, dmabuf);
  954. }
  955. }
  956. }
  957. void dpy_gl_update(QemuConsole *con,
  958. uint32_t x, uint32_t y, uint32_t w, uint32_t h)
  959. {
  960. DisplayState *s = con->ds;
  961. DisplayChangeListener *dcl;
  962. assert(con->gl);
  963. graphic_hw_gl_block(con, true);
  964. QLIST_FOREACH(dcl, &s->listeners, next) {
  965. if (con != dcl->con) {
  966. continue;
  967. }
  968. if (dcl->ops->dpy_gl_update) {
  969. dcl->ops->dpy_gl_update(dcl, x, y, w, h);
  970. }
  971. }
  972. graphic_hw_gl_block(con, false);
  973. }
  974. /***********************************************************/
  975. /* register display */
  976. /* console.c internal use only */
  977. static DisplayState *get_alloc_displaystate(void)
  978. {
  979. if (!display_state) {
  980. display_state = g_new0(DisplayState, 1);
  981. }
  982. return display_state;
  983. }
  984. /*
  985. * Called by main(), after creating QemuConsoles
  986. * and before initializing ui (sdl/vnc/...).
  987. */
  988. DisplayState *init_displaystate(void)
  989. {
  990. gchar *name;
  991. QemuConsole *con;
  992. QTAILQ_FOREACH(con, &consoles, next) {
  993. /* Hook up into the qom tree here (not in object_new()), once
  994. * all QemuConsoles are created and the order / numbering
  995. * doesn't change any more */
  996. name = g_strdup_printf("console[%d]", con->index);
  997. object_property_add_child(object_get_container("backend"),
  998. name, OBJECT(con));
  999. g_free(name);
  1000. }
  1001. return display_state;
  1002. }
  1003. void graphic_console_set_hwops(QemuConsole *con,
  1004. const GraphicHwOps *hw_ops,
  1005. void *opaque)
  1006. {
  1007. con->hw_ops = hw_ops;
  1008. con->hw = opaque;
  1009. }
  1010. QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
  1011. const GraphicHwOps *hw_ops,
  1012. void *opaque)
  1013. {
  1014. static const char noinit[] =
  1015. "Guest has not initialized the display (yet).";
  1016. int width = 640;
  1017. int height = 480;
  1018. QemuConsole *s;
  1019. DisplaySurface *surface;
  1020. s = qemu_graphic_console_lookup_unused();
  1021. if (s) {
  1022. trace_console_gfx_reuse(s->index);
  1023. width = qemu_console_get_width(s, 0);
  1024. height = qemu_console_get_height(s, 0);
  1025. } else {
  1026. trace_console_gfx_new();
  1027. s = (QemuConsole *)object_new(TYPE_QEMU_GRAPHIC_CONSOLE);
  1028. }
  1029. QEMU_GRAPHIC_CONSOLE(s)->head = head;
  1030. graphic_console_set_hwops(s, hw_ops, opaque);
  1031. if (dev) {
  1032. object_property_set_link(OBJECT(s), "device", OBJECT(dev),
  1033. &error_abort);
  1034. }
  1035. surface = qemu_create_placeholder_surface(width, height, noinit);
  1036. dpy_gfx_replace_surface(s, surface);
  1037. s->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
  1038. graphic_hw_gl_unblock_timer, s);
  1039. return s;
  1040. }
  1041. static const GraphicHwOps unused_ops = {
  1042. /* no callbacks */
  1043. };
  1044. void graphic_console_close(QemuConsole *con)
  1045. {
  1046. static const char unplugged[] =
  1047. "Guest display has been unplugged";
  1048. DisplaySurface *surface;
  1049. int width = qemu_console_get_width(con, 640);
  1050. int height = qemu_console_get_height(con, 480);
  1051. trace_console_gfx_close(con->index);
  1052. object_property_set_link(OBJECT(con), "device", NULL, &error_abort);
  1053. graphic_console_set_hwops(con, &unused_ops, NULL);
  1054. if (con->gl) {
  1055. dpy_gl_scanout_disable(con);
  1056. }
  1057. surface = qemu_create_placeholder_surface(width, height, unplugged);
  1058. dpy_gfx_replace_surface(con, surface);
  1059. }
  1060. QemuConsole *qemu_console_lookup_default(void)
  1061. {
  1062. QemuConsole *con;
  1063. QTAILQ_FOREACH(con, &consoles, next) {
  1064. if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
  1065. return con;
  1066. }
  1067. }
  1068. return QTAILQ_FIRST(&consoles);
  1069. }
  1070. QemuConsole *qemu_console_lookup_by_index(unsigned int index)
  1071. {
  1072. QemuConsole *con;
  1073. QTAILQ_FOREACH(con, &consoles, next) {
  1074. if (con->index == index) {
  1075. return con;
  1076. }
  1077. }
  1078. return NULL;
  1079. }
  1080. QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
  1081. {
  1082. QemuConsole *con;
  1083. Object *obj;
  1084. uint32_t h;
  1085. QTAILQ_FOREACH(con, &consoles, next) {
  1086. obj = object_property_get_link(OBJECT(con),
  1087. "device", &error_abort);
  1088. if (DEVICE(obj) != dev) {
  1089. continue;
  1090. }
  1091. h = object_property_get_uint(OBJECT(con),
  1092. "head", &error_abort);
  1093. if (h != head) {
  1094. continue;
  1095. }
  1096. return con;
  1097. }
  1098. return NULL;
  1099. }
  1100. QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
  1101. uint32_t head, Error **errp)
  1102. {
  1103. DeviceState *dev;
  1104. QemuConsole *con;
  1105. dev = qdev_find_recursive(sysbus_get_default(), device_id);
  1106. if (dev == NULL) {
  1107. error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
  1108. "Device '%s' not found", device_id);
  1109. return NULL;
  1110. }
  1111. con = qemu_console_lookup_by_device(dev, head);
  1112. if (con == NULL) {
  1113. error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
  1114. device_id, head);
  1115. return NULL;
  1116. }
  1117. return con;
  1118. }
  1119. static QemuConsole *qemu_graphic_console_lookup_unused(void)
  1120. {
  1121. QemuConsole *con;
  1122. Object *obj;
  1123. QTAILQ_FOREACH(con, &consoles, next) {
  1124. if (!QEMU_IS_GRAPHIC_CONSOLE(con) || con->hw_ops != &unused_ops) {
  1125. continue;
  1126. }
  1127. obj = object_property_get_link(OBJECT(con),
  1128. "device", &error_abort);
  1129. if (obj != NULL) {
  1130. continue;
  1131. }
  1132. return con;
  1133. }
  1134. return NULL;
  1135. }
  1136. QEMUCursor *qemu_console_get_cursor(QemuConsole *con)
  1137. {
  1138. return QEMU_IS_GRAPHIC_CONSOLE(con) ? QEMU_GRAPHIC_CONSOLE(con)->cursor : NULL;
  1139. }
  1140. bool qemu_console_is_visible(QemuConsole *con)
  1141. {
  1142. return con->dcls > 0;
  1143. }
  1144. bool qemu_console_is_graphic(QemuConsole *con)
  1145. {
  1146. return con && QEMU_IS_GRAPHIC_CONSOLE(con);
  1147. }
  1148. bool qemu_console_is_fixedsize(QemuConsole *con)
  1149. {
  1150. return con && (QEMU_IS_GRAPHIC_CONSOLE(con) || QEMU_IS_FIXED_TEXT_CONSOLE(con));
  1151. }
  1152. bool qemu_console_is_gl_blocked(QemuConsole *con)
  1153. {
  1154. assert(con != NULL);
  1155. return con->gl_block;
  1156. }
  1157. static bool qemu_graphic_console_is_multihead(QemuGraphicConsole *c)
  1158. {
  1159. QemuConsole *con;
  1160. QTAILQ_FOREACH(con, &consoles, next) {
  1161. QemuGraphicConsole *candidate;
  1162. if (!QEMU_IS_GRAPHIC_CONSOLE(con)) {
  1163. continue;
  1164. }
  1165. candidate = QEMU_GRAPHIC_CONSOLE(con);
  1166. if (candidate->device != c->device) {
  1167. continue;
  1168. }
  1169. if (candidate->head != c->head) {
  1170. return true;
  1171. }
  1172. }
  1173. return false;
  1174. }
  1175. char *qemu_console_get_label(QemuConsole *con)
  1176. {
  1177. if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
  1178. QemuGraphicConsole *c = QEMU_GRAPHIC_CONSOLE(con);
  1179. if (c->device) {
  1180. DeviceState *dev;
  1181. bool multihead;
  1182. dev = DEVICE(c->device);
  1183. multihead = qemu_graphic_console_is_multihead(c);
  1184. if (multihead) {
  1185. return g_strdup_printf("%s.%d", dev->id ?
  1186. dev->id :
  1187. object_get_typename(c->device),
  1188. c->head);
  1189. } else {
  1190. return g_strdup(dev->id ? : object_get_typename(c->device));
  1191. }
  1192. }
  1193. return g_strdup("VGA");
  1194. } else if (QEMU_IS_TEXT_CONSOLE(con)) {
  1195. const char *label = qemu_text_console_get_label(QEMU_TEXT_CONSOLE(con));
  1196. if (label) {
  1197. return g_strdup(label);
  1198. }
  1199. }
  1200. return g_strdup_printf("vc%d", con->index);
  1201. }
  1202. int qemu_console_get_index(QemuConsole *con)
  1203. {
  1204. return con ? con->index : -1;
  1205. }
  1206. uint32_t qemu_console_get_head(QemuConsole *con)
  1207. {
  1208. if (con == NULL) {
  1209. return -1;
  1210. }
  1211. if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
  1212. return QEMU_GRAPHIC_CONSOLE(con)->head;
  1213. }
  1214. return 0;
  1215. }
  1216. int qemu_console_get_width(QemuConsole *con, int fallback)
  1217. {
  1218. if (con == NULL) {
  1219. return fallback;
  1220. }
  1221. switch (con->scanout.kind) {
  1222. case SCANOUT_DMABUF:
  1223. return qemu_dmabuf_get_width(con->scanout.dmabuf);
  1224. case SCANOUT_TEXTURE:
  1225. return con->scanout.texture.width;
  1226. case SCANOUT_SURFACE:
  1227. return surface_width(con->surface);
  1228. default:
  1229. return fallback;
  1230. }
  1231. }
  1232. int qemu_console_get_height(QemuConsole *con, int fallback)
  1233. {
  1234. if (con == NULL) {
  1235. return fallback;
  1236. }
  1237. switch (con->scanout.kind) {
  1238. case SCANOUT_DMABUF:
  1239. return qemu_dmabuf_get_height(con->scanout.dmabuf);
  1240. case SCANOUT_TEXTURE:
  1241. return con->scanout.texture.height;
  1242. case SCANOUT_SURFACE:
  1243. return surface_height(con->surface);
  1244. default:
  1245. return fallback;
  1246. }
  1247. }
  1248. int qemu_invalidate_text_consoles(void)
  1249. {
  1250. QemuConsole *s;
  1251. int count = 0;
  1252. QTAILQ_FOREACH(s, &consoles, next) {
  1253. if (qemu_console_is_graphic(s) ||
  1254. !qemu_console_is_visible(s)) {
  1255. continue;
  1256. }
  1257. count++;
  1258. graphic_hw_invalidate(s);
  1259. }
  1260. return count;
  1261. }
  1262. void qemu_console_resize(QemuConsole *s, int width, int height)
  1263. {
  1264. DisplaySurface *surface = qemu_console_surface(s);
  1265. assert(QEMU_IS_GRAPHIC_CONSOLE(s));
  1266. if ((s->scanout.kind != SCANOUT_SURFACE ||
  1267. (surface && surface_is_allocated(surface) &&
  1268. !surface_is_placeholder(surface))) &&
  1269. qemu_console_get_width(s, -1) == width &&
  1270. qemu_console_get_height(s, -1) == height) {
  1271. return;
  1272. }
  1273. surface = qemu_create_displaysurface(width, height);
  1274. dpy_gfx_replace_surface(s, surface);
  1275. }
  1276. DisplaySurface *qemu_console_surface(QemuConsole *console)
  1277. {
  1278. return console->surface;
  1279. }
  1280. PixelFormat qemu_default_pixelformat(int bpp)
  1281. {
  1282. pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
  1283. PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
  1284. return pf;
  1285. }
  1286. static QemuDisplay *dpys[DISPLAY_TYPE__MAX];
  1287. void qemu_display_register(QemuDisplay *ui)
  1288. {
  1289. assert(ui->type < DISPLAY_TYPE__MAX);
  1290. dpys[ui->type] = ui;
  1291. }
  1292. bool qemu_display_find_default(DisplayOptions *opts)
  1293. {
  1294. static DisplayType prio[] = {
  1295. #if defined(CONFIG_GTK)
  1296. DISPLAY_TYPE_GTK,
  1297. #endif
  1298. #if defined(CONFIG_SDL)
  1299. DISPLAY_TYPE_SDL,
  1300. #endif
  1301. #if defined(CONFIG_COCOA)
  1302. DISPLAY_TYPE_COCOA
  1303. #endif
  1304. };
  1305. int i;
  1306. for (i = 0; i < (int)ARRAY_SIZE(prio); i++) {
  1307. if (dpys[prio[i]] == NULL) {
  1308. Error *local_err = NULL;
  1309. int rv = ui_module_load(DisplayType_str(prio[i]), &local_err);
  1310. if (rv < 0) {
  1311. error_report_err(local_err);
  1312. }
  1313. }
  1314. if (dpys[prio[i]] == NULL) {
  1315. continue;
  1316. }
  1317. opts->type = prio[i];
  1318. return true;
  1319. }
  1320. return false;
  1321. }
  1322. void qemu_display_early_init(DisplayOptions *opts)
  1323. {
  1324. assert(opts->type < DISPLAY_TYPE__MAX);
  1325. if (opts->type == DISPLAY_TYPE_NONE) {
  1326. return;
  1327. }
  1328. if (dpys[opts->type] == NULL) {
  1329. Error *local_err = NULL;
  1330. int rv = ui_module_load(DisplayType_str(opts->type), &local_err);
  1331. if (rv < 0) {
  1332. error_report_err(local_err);
  1333. }
  1334. }
  1335. if (dpys[opts->type] == NULL) {
  1336. error_report("Display '%s' is not available.",
  1337. DisplayType_str(opts->type));
  1338. exit(1);
  1339. }
  1340. if (dpys[opts->type]->early_init) {
  1341. dpys[opts->type]->early_init(opts);
  1342. }
  1343. }
  1344. void qemu_display_init(DisplayState *ds, DisplayOptions *opts)
  1345. {
  1346. assert(opts->type < DISPLAY_TYPE__MAX);
  1347. if (opts->type == DISPLAY_TYPE_NONE) {
  1348. return;
  1349. }
  1350. assert(dpys[opts->type] != NULL);
  1351. dpys[opts->type]->init(ds, opts);
  1352. }
  1353. const char *qemu_display_get_vc(DisplayOptions *opts)
  1354. {
  1355. #ifdef CONFIG_PIXMAN
  1356. const char *vc = "vc:80Cx24C";
  1357. #else
  1358. const char *vc = NULL;
  1359. #endif
  1360. assert(opts->type < DISPLAY_TYPE__MAX);
  1361. if (dpys[opts->type] && dpys[opts->type]->vc) {
  1362. vc = dpys[opts->type]->vc;
  1363. }
  1364. return vc;
  1365. }
  1366. void qemu_display_help(void)
  1367. {
  1368. int idx;
  1369. printf("Available display backend types:\n");
  1370. printf("none\n");
  1371. for (idx = DISPLAY_TYPE_NONE; idx < DISPLAY_TYPE__MAX; idx++) {
  1372. if (!dpys[idx]) {
  1373. Error *local_err = NULL;
  1374. int rv = ui_module_load(DisplayType_str(idx), &local_err);
  1375. if (rv < 0) {
  1376. error_report_err(local_err);
  1377. }
  1378. }
  1379. if (dpys[idx]) {
  1380. printf("%s\n", DisplayType_str(dpys[idx]->type));
  1381. }
  1382. }
  1383. printf("\n"
  1384. "Some display backends support suboptions, which can be set with\n"
  1385. " -display backend,option=value,option=value...\n"
  1386. "For a short list of the suboptions for each display, see the "
  1387. "top-level -help output; more detail is in the documentation.\n");
  1388. }