console.c 44 KB

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