console.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  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(dev->id ? : object_get_typename(c->device));
  1200. }
  1201. }
  1202. return g_strdup("VGA");
  1203. } else if (QEMU_IS_TEXT_CONSOLE(con)) {
  1204. const char *label = qemu_text_console_get_label(QEMU_TEXT_CONSOLE(con));
  1205. if (label) {
  1206. return g_strdup(label);
  1207. }
  1208. }
  1209. return g_strdup_printf("vc%d", con->index);
  1210. }
  1211. int qemu_console_get_index(QemuConsole *con)
  1212. {
  1213. return con ? con->index : -1;
  1214. }
  1215. uint32_t qemu_console_get_head(QemuConsole *con)
  1216. {
  1217. if (con == NULL) {
  1218. return -1;
  1219. }
  1220. if (QEMU_IS_GRAPHIC_CONSOLE(con)) {
  1221. return QEMU_GRAPHIC_CONSOLE(con)->head;
  1222. }
  1223. return 0;
  1224. }
  1225. int qemu_console_get_width(QemuConsole *con, int fallback)
  1226. {
  1227. if (con == NULL) {
  1228. return fallback;
  1229. }
  1230. switch (con->scanout.kind) {
  1231. case SCANOUT_DMABUF:
  1232. return qemu_dmabuf_get_width(con->scanout.dmabuf);
  1233. case SCANOUT_TEXTURE:
  1234. return con->scanout.texture.width;
  1235. case SCANOUT_SURFACE:
  1236. return surface_width(con->surface);
  1237. default:
  1238. return fallback;
  1239. }
  1240. }
  1241. int qemu_console_get_height(QemuConsole *con, int fallback)
  1242. {
  1243. if (con == NULL) {
  1244. return fallback;
  1245. }
  1246. switch (con->scanout.kind) {
  1247. case SCANOUT_DMABUF:
  1248. return qemu_dmabuf_get_height(con->scanout.dmabuf);
  1249. case SCANOUT_TEXTURE:
  1250. return con->scanout.texture.height;
  1251. case SCANOUT_SURFACE:
  1252. return surface_height(con->surface);
  1253. default:
  1254. return fallback;
  1255. }
  1256. }
  1257. int qemu_invalidate_text_consoles(void)
  1258. {
  1259. QemuConsole *s;
  1260. int count = 0;
  1261. QTAILQ_FOREACH(s, &consoles, next) {
  1262. if (qemu_console_is_graphic(s) ||
  1263. !qemu_console_is_visible(s)) {
  1264. continue;
  1265. }
  1266. count++;
  1267. graphic_hw_invalidate(s);
  1268. }
  1269. return count;
  1270. }
  1271. void qemu_console_resize(QemuConsole *s, int width, int height)
  1272. {
  1273. DisplaySurface *surface = qemu_console_surface(s);
  1274. assert(QEMU_IS_GRAPHIC_CONSOLE(s));
  1275. if ((s->scanout.kind != SCANOUT_SURFACE ||
  1276. (surface && surface_is_allocated(surface) &&
  1277. !surface_is_placeholder(surface))) &&
  1278. qemu_console_get_width(s, -1) == width &&
  1279. qemu_console_get_height(s, -1) == height) {
  1280. return;
  1281. }
  1282. surface = qemu_create_displaysurface(width, height);
  1283. dpy_gfx_replace_surface(s, surface);
  1284. }
  1285. DisplaySurface *qemu_console_surface(QemuConsole *console)
  1286. {
  1287. switch (console->scanout.kind) {
  1288. case SCANOUT_SURFACE:
  1289. return console->surface;
  1290. default:
  1291. return NULL;
  1292. }
  1293. }
  1294. PixelFormat qemu_default_pixelformat(int bpp)
  1295. {
  1296. pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
  1297. PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
  1298. return pf;
  1299. }
  1300. static QemuDisplay *dpys[DISPLAY_TYPE__MAX];
  1301. void qemu_display_register(QemuDisplay *ui)
  1302. {
  1303. assert(ui->type < DISPLAY_TYPE__MAX);
  1304. dpys[ui->type] = ui;
  1305. }
  1306. bool qemu_display_find_default(DisplayOptions *opts)
  1307. {
  1308. static DisplayType prio[] = {
  1309. #if defined(CONFIG_GTK)
  1310. DISPLAY_TYPE_GTK,
  1311. #endif
  1312. #if defined(CONFIG_SDL)
  1313. DISPLAY_TYPE_SDL,
  1314. #endif
  1315. #if defined(CONFIG_COCOA)
  1316. DISPLAY_TYPE_COCOA
  1317. #endif
  1318. };
  1319. int i;
  1320. for (i = 0; i < (int)ARRAY_SIZE(prio); i++) {
  1321. if (dpys[prio[i]] == NULL) {
  1322. Error *local_err = NULL;
  1323. int rv = ui_module_load(DisplayType_str(prio[i]), &local_err);
  1324. if (rv < 0) {
  1325. error_report_err(local_err);
  1326. }
  1327. }
  1328. if (dpys[prio[i]] == NULL) {
  1329. continue;
  1330. }
  1331. opts->type = prio[i];
  1332. return true;
  1333. }
  1334. return false;
  1335. }
  1336. void qemu_display_early_init(DisplayOptions *opts)
  1337. {
  1338. assert(opts->type < DISPLAY_TYPE__MAX);
  1339. if (opts->type == DISPLAY_TYPE_NONE) {
  1340. return;
  1341. }
  1342. if (dpys[opts->type] == NULL) {
  1343. Error *local_err = NULL;
  1344. int rv = ui_module_load(DisplayType_str(opts->type), &local_err);
  1345. if (rv < 0) {
  1346. error_report_err(local_err);
  1347. }
  1348. }
  1349. if (dpys[opts->type] == NULL) {
  1350. error_report("Display '%s' is not available.",
  1351. DisplayType_str(opts->type));
  1352. exit(1);
  1353. }
  1354. if (dpys[opts->type]->early_init) {
  1355. dpys[opts->type]->early_init(opts);
  1356. }
  1357. }
  1358. void qemu_display_init(DisplayState *ds, DisplayOptions *opts)
  1359. {
  1360. assert(opts->type < DISPLAY_TYPE__MAX);
  1361. if (opts->type == DISPLAY_TYPE_NONE) {
  1362. return;
  1363. }
  1364. assert(dpys[opts->type] != NULL);
  1365. dpys[opts->type]->init(ds, opts);
  1366. }
  1367. const char *qemu_display_get_vc(DisplayOptions *opts)
  1368. {
  1369. #ifdef CONFIG_PIXMAN
  1370. const char *vc = "vc:80Cx24C";
  1371. #else
  1372. const char *vc = NULL;
  1373. #endif
  1374. assert(opts->type < DISPLAY_TYPE__MAX);
  1375. if (dpys[opts->type] && dpys[opts->type]->vc) {
  1376. vc = dpys[opts->type]->vc;
  1377. }
  1378. return vc;
  1379. }
  1380. void qemu_display_help(void)
  1381. {
  1382. int idx;
  1383. printf("Available display backend types:\n");
  1384. printf("none\n");
  1385. for (idx = DISPLAY_TYPE_NONE; idx < DISPLAY_TYPE__MAX; idx++) {
  1386. if (!dpys[idx]) {
  1387. Error *local_err = NULL;
  1388. int rv = ui_module_load(DisplayType_str(idx), &local_err);
  1389. if (rv < 0) {
  1390. error_report_err(local_err);
  1391. }
  1392. }
  1393. if (dpys[idx]) {
  1394. printf("%s\n", DisplayType_str(dpys[idx]->type));
  1395. }
  1396. }
  1397. printf("\n"
  1398. "Some display backends support suboptions, which can be set with\n"
  1399. " -display backend,option=value,option=value...\n"
  1400. "For a short list of the suboptions for each display, see the "
  1401. "top-level -help output; more detail is in the documentation.\n");
  1402. }