console.c 44 KB

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