console.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771
  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 "qemu/coroutine.h"
  30. #include "qemu/error-report.h"
  31. #include "qemu/fifo8.h"
  32. #include "qemu/main-loop.h"
  33. #include "qemu/module.h"
  34. #include "qemu/option.h"
  35. #include "qemu/timer.h"
  36. #include "chardev/char.h"
  37. #include "trace.h"
  38. #include "exec/memory.h"
  39. #include "io/channel-file.h"
  40. #include "qom/object.h"
  41. #ifdef CONFIG_PNG
  42. #include <png.h>
  43. #endif
  44. #define DEFAULT_BACKSCROLL 512
  45. #define CONSOLE_CURSOR_PERIOD 500
  46. typedef struct TextAttributes {
  47. uint8_t fgcol:4;
  48. uint8_t bgcol:4;
  49. uint8_t bold:1;
  50. uint8_t uline:1;
  51. uint8_t blink:1;
  52. uint8_t invers:1;
  53. uint8_t unvisible:1;
  54. } TextAttributes;
  55. typedef struct TextCell {
  56. uint8_t ch;
  57. TextAttributes t_attrib;
  58. } TextCell;
  59. #define MAX_ESC_PARAMS 3
  60. enum TTYState {
  61. TTY_STATE_NORM,
  62. TTY_STATE_ESC,
  63. TTY_STATE_CSI,
  64. };
  65. typedef enum {
  66. GRAPHIC_CONSOLE,
  67. TEXT_CONSOLE,
  68. TEXT_CONSOLE_FIXED_SIZE
  69. } console_type_t;
  70. struct QemuConsole {
  71. Object parent;
  72. int index;
  73. console_type_t console_type;
  74. DisplayState *ds;
  75. DisplaySurface *surface;
  76. DisplayScanout scanout;
  77. int dcls;
  78. DisplayGLCtx *gl;
  79. int gl_block;
  80. QEMUTimer *gl_unblock_timer;
  81. int window_id;
  82. /* Graphic console state. */
  83. Object *device;
  84. uint32_t head;
  85. QemuUIInfo ui_info;
  86. QEMUTimer *ui_timer;
  87. QEMUCursor *cursor;
  88. int cursor_x, cursor_y, cursor_on;
  89. const GraphicHwOps *hw_ops;
  90. void *hw;
  91. /* Text console state */
  92. int width;
  93. int height;
  94. int total_height;
  95. int backscroll_height;
  96. int x, y;
  97. int x_saved, y_saved;
  98. int y_displayed;
  99. int y_base;
  100. TextAttributes t_attrib_default; /* default text attributes */
  101. TextAttributes t_attrib; /* currently active text attributes */
  102. TextCell *cells;
  103. int text_x[2], text_y[2], cursor_invalidate;
  104. int echo;
  105. int update_x0;
  106. int update_y0;
  107. int update_x1;
  108. int update_y1;
  109. enum TTYState state;
  110. int esc_params[MAX_ESC_PARAMS];
  111. int nb_esc_params;
  112. Chardev *chr;
  113. /* fifo for key pressed */
  114. Fifo8 out_fifo;
  115. CoQueue dump_queue;
  116. QTAILQ_ENTRY(QemuConsole) next;
  117. };
  118. struct DisplayState {
  119. QEMUTimer *gui_timer;
  120. uint64_t last_update;
  121. uint64_t update_interval;
  122. bool refreshing;
  123. bool have_gfx;
  124. bool have_text;
  125. QLIST_HEAD(, DisplayChangeListener) listeners;
  126. };
  127. static DisplayState *display_state;
  128. static QemuConsole *active_console;
  129. static QTAILQ_HEAD(, QemuConsole) consoles =
  130. QTAILQ_HEAD_INITIALIZER(consoles);
  131. static bool cursor_visible_phase;
  132. static QEMUTimer *cursor_timer;
  133. static void text_console_do_init(Chardev *chr, DisplayState *ds);
  134. static void dpy_refresh(DisplayState *s);
  135. static DisplayState *get_alloc_displaystate(void);
  136. static void text_console_update_cursor_timer(void);
  137. static void text_console_update_cursor(void *opaque);
  138. static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl);
  139. static bool console_compatible_with(QemuConsole *con,
  140. DisplayChangeListener *dcl, Error **errp);
  141. static void gui_update(void *opaque)
  142. {
  143. uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
  144. uint64_t dcl_interval;
  145. DisplayState *ds = opaque;
  146. DisplayChangeListener *dcl;
  147. ds->refreshing = true;
  148. dpy_refresh(ds);
  149. ds->refreshing = false;
  150. QLIST_FOREACH(dcl, &ds->listeners, next) {
  151. dcl_interval = dcl->update_interval ?
  152. dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
  153. if (interval > dcl_interval) {
  154. interval = dcl_interval;
  155. }
  156. }
  157. if (ds->update_interval != interval) {
  158. ds->update_interval = interval;
  159. trace_console_refresh(interval);
  160. }
  161. ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  162. timer_mod(ds->gui_timer, ds->last_update + interval);
  163. }
  164. static void gui_setup_refresh(DisplayState *ds)
  165. {
  166. DisplayChangeListener *dcl;
  167. bool need_timer = false;
  168. bool have_gfx = false;
  169. bool have_text = false;
  170. QLIST_FOREACH(dcl, &ds->listeners, next) {
  171. if (dcl->ops->dpy_refresh != NULL) {
  172. need_timer = true;
  173. }
  174. if (dcl->ops->dpy_gfx_update != NULL) {
  175. have_gfx = true;
  176. }
  177. if (dcl->ops->dpy_text_update != NULL) {
  178. have_text = true;
  179. }
  180. }
  181. if (need_timer && ds->gui_timer == NULL) {
  182. ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
  183. timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
  184. }
  185. if (!need_timer && ds->gui_timer != NULL) {
  186. timer_free(ds->gui_timer);
  187. ds->gui_timer = NULL;
  188. }
  189. ds->have_gfx = have_gfx;
  190. ds->have_text = have_text;
  191. }
  192. void graphic_hw_update_done(QemuConsole *con)
  193. {
  194. if (con) {
  195. qemu_co_enter_all(&con->dump_queue, NULL);
  196. }
  197. }
  198. void graphic_hw_update(QemuConsole *con)
  199. {
  200. bool async = false;
  201. con = con ? con : active_console;
  202. if (!con) {
  203. return;
  204. }
  205. if (con->hw_ops->gfx_update) {
  206. con->hw_ops->gfx_update(con->hw);
  207. async = con->hw_ops->gfx_update_async;
  208. }
  209. if (!async) {
  210. graphic_hw_update_done(con);
  211. }
  212. }
  213. static void graphic_hw_gl_unblock_timer(void *opaque)
  214. {
  215. warn_report("console: no gl-unblock within one second");
  216. }
  217. void graphic_hw_gl_block(QemuConsole *con, bool block)
  218. {
  219. uint64_t timeout;
  220. assert(con != NULL);
  221. if (block) {
  222. con->gl_block++;
  223. } else {
  224. con->gl_block--;
  225. }
  226. assert(con->gl_block >= 0);
  227. if (!con->hw_ops->gl_block) {
  228. return;
  229. }
  230. if ((block && con->gl_block != 1) || (!block && con->gl_block != 0)) {
  231. return;
  232. }
  233. con->hw_ops->gl_block(con->hw, block);
  234. if (block) {
  235. timeout = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  236. timeout += 1000; /* one sec */
  237. timer_mod(con->gl_unblock_timer, timeout);
  238. } else {
  239. timer_del(con->gl_unblock_timer);
  240. }
  241. }
  242. int qemu_console_get_window_id(QemuConsole *con)
  243. {
  244. return con->window_id;
  245. }
  246. void qemu_console_set_window_id(QemuConsole *con, int window_id)
  247. {
  248. con->window_id = window_id;
  249. }
  250. void graphic_hw_invalidate(QemuConsole *con)
  251. {
  252. if (!con) {
  253. con = active_console;
  254. }
  255. if (con && con->hw_ops->invalidate) {
  256. con->hw_ops->invalidate(con->hw);
  257. }
  258. }
  259. #ifdef CONFIG_PNG
  260. /**
  261. * png_save: Take a screenshot as PNG
  262. *
  263. * Saves screendump as a PNG file
  264. *
  265. * Returns true for success or false for error.
  266. *
  267. * @fd: File descriptor for PNG file.
  268. * @image: Image data in pixman format.
  269. * @errp: Pointer to an error.
  270. */
  271. static bool png_save(int fd, pixman_image_t *image, Error **errp)
  272. {
  273. int width = pixman_image_get_width(image);
  274. int height = pixman_image_get_height(image);
  275. png_struct *png_ptr;
  276. png_info *info_ptr;
  277. g_autoptr(pixman_image_t) linebuf =
  278. qemu_pixman_linebuf_create(PIXMAN_a8r8g8b8, width);
  279. uint8_t *buf = (uint8_t *)pixman_image_get_data(linebuf);
  280. FILE *f = fdopen(fd, "wb");
  281. int y;
  282. if (!f) {
  283. error_setg_errno(errp, errno,
  284. "Failed to create file from file descriptor");
  285. return false;
  286. }
  287. png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
  288. NULL, NULL);
  289. if (!png_ptr) {
  290. error_setg(errp, "PNG creation failed. Unable to write struct");
  291. fclose(f);
  292. return false;
  293. }
  294. info_ptr = png_create_info_struct(png_ptr);
  295. if (!info_ptr) {
  296. error_setg(errp, "PNG creation failed. Unable to write info");
  297. fclose(f);
  298. png_destroy_write_struct(&png_ptr, &info_ptr);
  299. return false;
  300. }
  301. png_init_io(png_ptr, f);
  302. png_set_IHDR(png_ptr, info_ptr, width, height, 8,
  303. PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
  304. PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
  305. png_write_info(png_ptr, info_ptr);
  306. for (y = 0; y < height; ++y) {
  307. qemu_pixman_linebuf_fill(linebuf, image, width, 0, y);
  308. png_write_row(png_ptr, buf);
  309. }
  310. png_write_end(png_ptr, NULL);
  311. png_destroy_write_struct(&png_ptr, &info_ptr);
  312. if (fclose(f) != 0) {
  313. error_setg_errno(errp, errno,
  314. "PNG creation failed. Unable to close file");
  315. return false;
  316. }
  317. return true;
  318. }
  319. #else /* no png support */
  320. static bool png_save(int fd, pixman_image_t *image, Error **errp)
  321. {
  322. error_setg(errp, "Enable PNG support with libpng for screendump");
  323. return false;
  324. }
  325. #endif /* CONFIG_PNG */
  326. static bool ppm_save(int fd, pixman_image_t *image, Error **errp)
  327. {
  328. int width = pixman_image_get_width(image);
  329. int height = pixman_image_get_height(image);
  330. g_autoptr(Object) ioc = OBJECT(qio_channel_file_new_fd(fd));
  331. g_autofree char *header = NULL;
  332. g_autoptr(pixman_image_t) linebuf = NULL;
  333. int y;
  334. trace_ppm_save(fd, image);
  335. header = g_strdup_printf("P6\n%d %d\n%d\n", width, height, 255);
  336. if (qio_channel_write_all(QIO_CHANNEL(ioc),
  337. header, strlen(header), errp) < 0) {
  338. return false;
  339. }
  340. linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
  341. for (y = 0; y < height; y++) {
  342. qemu_pixman_linebuf_fill(linebuf, image, width, 0, y);
  343. if (qio_channel_write_all(QIO_CHANNEL(ioc),
  344. (char *)pixman_image_get_data(linebuf),
  345. pixman_image_get_stride(linebuf), errp) < 0) {
  346. return false;
  347. }
  348. }
  349. return true;
  350. }
  351. static void graphic_hw_update_bh(void *con)
  352. {
  353. graphic_hw_update(con);
  354. }
  355. /* Safety: coroutine-only, concurrent-coroutine safe, main thread only */
  356. void coroutine_fn
  357. qmp_screendump(const char *filename, const char *device,
  358. bool has_head, int64_t head,
  359. bool has_format, ImageFormat format, Error **errp)
  360. {
  361. g_autoptr(pixman_image_t) image = NULL;
  362. QemuConsole *con;
  363. DisplaySurface *surface;
  364. int fd;
  365. if (device) {
  366. con = qemu_console_lookup_by_device_name(device, has_head ? head : 0,
  367. errp);
  368. if (!con) {
  369. return;
  370. }
  371. } else {
  372. if (has_head) {
  373. error_setg(errp, "'head' must be specified together with 'device'");
  374. return;
  375. }
  376. con = qemu_console_lookup_by_index(0);
  377. if (!con) {
  378. error_setg(errp, "There is no console to take a screendump from");
  379. return;
  380. }
  381. }
  382. if (qemu_co_queue_empty(&con->dump_queue)) {
  383. /* Defer the update, it will restart the pending coroutines */
  384. aio_bh_schedule_oneshot(qemu_get_aio_context(),
  385. graphic_hw_update_bh, con);
  386. }
  387. qemu_co_queue_wait(&con->dump_queue, NULL);
  388. /*
  389. * All pending coroutines are woken up, while the BQL is held. No
  390. * further graphic update are possible until it is released. Take
  391. * an image ref before that.
  392. */
  393. surface = qemu_console_surface(con);
  394. if (!surface) {
  395. error_setg(errp, "no surface");
  396. return;
  397. }
  398. image = pixman_image_ref(surface->image);
  399. fd = qemu_open_old(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
  400. if (fd == -1) {
  401. error_setg(errp, "failed to open file '%s': %s", filename,
  402. strerror(errno));
  403. return;
  404. }
  405. /*
  406. * The image content could potentially be updated as the coroutine
  407. * yields and releases the BQL. It could produce corrupted dump, but
  408. * it should be otherwise safe.
  409. */
  410. if (has_format && format == IMAGE_FORMAT_PNG) {
  411. /* PNG format specified for screendump */
  412. if (!png_save(fd, image, errp)) {
  413. qemu_unlink(filename);
  414. }
  415. } else {
  416. /* PPM format specified/default for screendump */
  417. if (!ppm_save(fd, image, errp)) {
  418. qemu_unlink(filename);
  419. }
  420. }
  421. }
  422. void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
  423. {
  424. if (!con) {
  425. con = active_console;
  426. }
  427. if (con && con->hw_ops->text_update) {
  428. con->hw_ops->text_update(con->hw, chardata);
  429. }
  430. }
  431. static void vga_fill_rect(QemuConsole *con,
  432. int posx, int posy, int width, int height,
  433. pixman_color_t color)
  434. {
  435. DisplaySurface *surface = qemu_console_surface(con);
  436. pixman_rectangle16_t rect = {
  437. .x = posx, .y = posy, .width = width, .height = height
  438. };
  439. pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
  440. &color, 1, &rect);
  441. }
  442. /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
  443. static void vga_bitblt(QemuConsole *con,
  444. int xs, int ys, int xd, int yd, int w, int h)
  445. {
  446. DisplaySurface *surface = qemu_console_surface(con);
  447. pixman_image_composite(PIXMAN_OP_SRC,
  448. surface->image, NULL, surface->image,
  449. xs, ys, 0, 0, xd, yd, w, h);
  450. }
  451. /***********************************************************/
  452. /* basic char display */
  453. #define FONT_HEIGHT 16
  454. #define FONT_WIDTH 8
  455. #include "vgafont.h"
  456. #define QEMU_RGB(r, g, b) \
  457. { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
  458. static const pixman_color_t color_table_rgb[2][8] = {
  459. { /* dark */
  460. [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
  461. [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
  462. [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
  463. [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
  464. [QEMU_COLOR_RED] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
  465. [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
  466. [QEMU_COLOR_YELLOW] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
  467. [QEMU_COLOR_WHITE] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
  468. },
  469. { /* bright */
  470. [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
  471. [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
  472. [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
  473. [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
  474. [QEMU_COLOR_RED] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
  475. [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
  476. [QEMU_COLOR_YELLOW] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
  477. [QEMU_COLOR_WHITE] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
  478. }
  479. };
  480. static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
  481. TextAttributes *t_attrib)
  482. {
  483. static pixman_image_t *glyphs[256];
  484. DisplaySurface *surface = qemu_console_surface(s);
  485. pixman_color_t fgcol, bgcol;
  486. if (t_attrib->invers) {
  487. bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
  488. fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
  489. } else {
  490. fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
  491. bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
  492. }
  493. if (!glyphs[ch]) {
  494. glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
  495. }
  496. qemu_pixman_glyph_render(glyphs[ch], surface->image,
  497. &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
  498. }
  499. static void text_console_resize(QemuConsole *s)
  500. {
  501. TextCell *cells, *c, *c1;
  502. int w1, x, y, last_width;
  503. assert(s->scanout.kind == SCANOUT_SURFACE);
  504. last_width = s->width;
  505. s->width = surface_width(s->surface) / FONT_WIDTH;
  506. s->height = surface_height(s->surface) / FONT_HEIGHT;
  507. w1 = last_width;
  508. if (s->width < w1)
  509. w1 = s->width;
  510. cells = g_new(TextCell, s->width * s->total_height + 1);
  511. for(y = 0; y < s->total_height; y++) {
  512. c = &cells[y * s->width];
  513. if (w1 > 0) {
  514. c1 = &s->cells[y * last_width];
  515. for(x = 0; x < w1; x++) {
  516. *c++ = *c1++;
  517. }
  518. }
  519. for(x = w1; x < s->width; x++) {
  520. c->ch = ' ';
  521. c->t_attrib = s->t_attrib_default;
  522. c++;
  523. }
  524. }
  525. g_free(s->cells);
  526. s->cells = cells;
  527. }
  528. static inline void text_update_xy(QemuConsole *s, int x, int y)
  529. {
  530. s->text_x[0] = MIN(s->text_x[0], x);
  531. s->text_x[1] = MAX(s->text_x[1], x);
  532. s->text_y[0] = MIN(s->text_y[0], y);
  533. s->text_y[1] = MAX(s->text_y[1], y);
  534. }
  535. static void invalidate_xy(QemuConsole *s, int x, int y)
  536. {
  537. if (!qemu_console_is_visible(s)) {
  538. return;
  539. }
  540. if (s->update_x0 > x * FONT_WIDTH)
  541. s->update_x0 = x * FONT_WIDTH;
  542. if (s->update_y0 > y * FONT_HEIGHT)
  543. s->update_y0 = y * FONT_HEIGHT;
  544. if (s->update_x1 < (x + 1) * FONT_WIDTH)
  545. s->update_x1 = (x + 1) * FONT_WIDTH;
  546. if (s->update_y1 < (y + 1) * FONT_HEIGHT)
  547. s->update_y1 = (y + 1) * FONT_HEIGHT;
  548. }
  549. static void update_xy(QemuConsole *s, int x, int y)
  550. {
  551. TextCell *c;
  552. int y1, y2;
  553. if (s->ds->have_text) {
  554. text_update_xy(s, x, y);
  555. }
  556. y1 = (s->y_base + y) % s->total_height;
  557. y2 = y1 - s->y_displayed;
  558. if (y2 < 0) {
  559. y2 += s->total_height;
  560. }
  561. if (y2 < s->height) {
  562. if (x >= s->width) {
  563. x = s->width - 1;
  564. }
  565. c = &s->cells[y1 * s->width + x];
  566. vga_putcharxy(s, x, y2, c->ch,
  567. &(c->t_attrib));
  568. invalidate_xy(s, x, y2);
  569. }
  570. }
  571. static void console_show_cursor(QemuConsole *s, int show)
  572. {
  573. TextCell *c;
  574. int y, y1;
  575. int x = s->x;
  576. if (s->ds->have_text) {
  577. s->cursor_invalidate = 1;
  578. }
  579. if (x >= s->width) {
  580. x = s->width - 1;
  581. }
  582. y1 = (s->y_base + s->y) % s->total_height;
  583. y = y1 - s->y_displayed;
  584. if (y < 0) {
  585. y += s->total_height;
  586. }
  587. if (y < s->height) {
  588. c = &s->cells[y1 * s->width + x];
  589. if (show && cursor_visible_phase) {
  590. TextAttributes t_attrib = s->t_attrib_default;
  591. t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
  592. vga_putcharxy(s, x, y, c->ch, &t_attrib);
  593. } else {
  594. vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
  595. }
  596. invalidate_xy(s, x, y);
  597. }
  598. }
  599. static void console_refresh(QemuConsole *s)
  600. {
  601. DisplaySurface *surface = qemu_console_surface(s);
  602. TextCell *c;
  603. int x, y, y1;
  604. if (s->ds->have_text) {
  605. s->text_x[0] = 0;
  606. s->text_y[0] = 0;
  607. s->text_x[1] = s->width - 1;
  608. s->text_y[1] = s->height - 1;
  609. s->cursor_invalidate = 1;
  610. }
  611. vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
  612. color_table_rgb[0][QEMU_COLOR_BLACK]);
  613. y1 = s->y_displayed;
  614. for (y = 0; y < s->height; y++) {
  615. c = s->cells + y1 * s->width;
  616. for (x = 0; x < s->width; x++) {
  617. vga_putcharxy(s, x, y, c->ch,
  618. &(c->t_attrib));
  619. c++;
  620. }
  621. if (++y1 == s->total_height) {
  622. y1 = 0;
  623. }
  624. }
  625. console_show_cursor(s, 1);
  626. dpy_gfx_update(s, 0, 0,
  627. surface_width(surface), surface_height(surface));
  628. }
  629. static void console_scroll(QemuConsole *s, int ydelta)
  630. {
  631. int i, y1;
  632. if (ydelta > 0) {
  633. for(i = 0; i < ydelta; i++) {
  634. if (s->y_displayed == s->y_base)
  635. break;
  636. if (++s->y_displayed == s->total_height)
  637. s->y_displayed = 0;
  638. }
  639. } else {
  640. ydelta = -ydelta;
  641. i = s->backscroll_height;
  642. if (i > s->total_height - s->height)
  643. i = s->total_height - s->height;
  644. y1 = s->y_base - i;
  645. if (y1 < 0)
  646. y1 += s->total_height;
  647. for(i = 0; i < ydelta; i++) {
  648. if (s->y_displayed == y1)
  649. break;
  650. if (--s->y_displayed < 0)
  651. s->y_displayed = s->total_height - 1;
  652. }
  653. }
  654. console_refresh(s);
  655. }
  656. static void console_put_lf(QemuConsole *s)
  657. {
  658. TextCell *c;
  659. int x, y1;
  660. s->y++;
  661. if (s->y >= s->height) {
  662. s->y = s->height - 1;
  663. if (s->y_displayed == s->y_base) {
  664. if (++s->y_displayed == s->total_height)
  665. s->y_displayed = 0;
  666. }
  667. if (++s->y_base == s->total_height)
  668. s->y_base = 0;
  669. if (s->backscroll_height < s->total_height)
  670. s->backscroll_height++;
  671. y1 = (s->y_base + s->height - 1) % s->total_height;
  672. c = &s->cells[y1 * s->width];
  673. for(x = 0; x < s->width; x++) {
  674. c->ch = ' ';
  675. c->t_attrib = s->t_attrib_default;
  676. c++;
  677. }
  678. if (s->y_displayed == s->y_base) {
  679. if (s->ds->have_text) {
  680. s->text_x[0] = 0;
  681. s->text_y[0] = 0;
  682. s->text_x[1] = s->width - 1;
  683. s->text_y[1] = s->height - 1;
  684. }
  685. vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
  686. s->width * FONT_WIDTH,
  687. (s->height - 1) * FONT_HEIGHT);
  688. vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
  689. s->width * FONT_WIDTH, FONT_HEIGHT,
  690. color_table_rgb[0][s->t_attrib_default.bgcol]);
  691. s->update_x0 = 0;
  692. s->update_y0 = 0;
  693. s->update_x1 = s->width * FONT_WIDTH;
  694. s->update_y1 = s->height * FONT_HEIGHT;
  695. }
  696. }
  697. }
  698. /* Set console attributes depending on the current escape codes.
  699. * NOTE: I know this code is not very efficient (checking every color for it
  700. * self) but it is more readable and better maintainable.
  701. */
  702. static void console_handle_escape(QemuConsole *s)
  703. {
  704. int i;
  705. for (i=0; i<s->nb_esc_params; i++) {
  706. switch (s->esc_params[i]) {
  707. case 0: /* reset all console attributes to default */
  708. s->t_attrib = s->t_attrib_default;
  709. break;
  710. case 1:
  711. s->t_attrib.bold = 1;
  712. break;
  713. case 4:
  714. s->t_attrib.uline = 1;
  715. break;
  716. case 5:
  717. s->t_attrib.blink = 1;
  718. break;
  719. case 7:
  720. s->t_attrib.invers = 1;
  721. break;
  722. case 8:
  723. s->t_attrib.unvisible = 1;
  724. break;
  725. case 22:
  726. s->t_attrib.bold = 0;
  727. break;
  728. case 24:
  729. s->t_attrib.uline = 0;
  730. break;
  731. case 25:
  732. s->t_attrib.blink = 0;
  733. break;
  734. case 27:
  735. s->t_attrib.invers = 0;
  736. break;
  737. case 28:
  738. s->t_attrib.unvisible = 0;
  739. break;
  740. /* set foreground color */
  741. case 30:
  742. s->t_attrib.fgcol = QEMU_COLOR_BLACK;
  743. break;
  744. case 31:
  745. s->t_attrib.fgcol = QEMU_COLOR_RED;
  746. break;
  747. case 32:
  748. s->t_attrib.fgcol = QEMU_COLOR_GREEN;
  749. break;
  750. case 33:
  751. s->t_attrib.fgcol = QEMU_COLOR_YELLOW;
  752. break;
  753. case 34:
  754. s->t_attrib.fgcol = QEMU_COLOR_BLUE;
  755. break;
  756. case 35:
  757. s->t_attrib.fgcol = QEMU_COLOR_MAGENTA;
  758. break;
  759. case 36:
  760. s->t_attrib.fgcol = QEMU_COLOR_CYAN;
  761. break;
  762. case 37:
  763. s->t_attrib.fgcol = QEMU_COLOR_WHITE;
  764. break;
  765. /* set background color */
  766. case 40:
  767. s->t_attrib.bgcol = QEMU_COLOR_BLACK;
  768. break;
  769. case 41:
  770. s->t_attrib.bgcol = QEMU_COLOR_RED;
  771. break;
  772. case 42:
  773. s->t_attrib.bgcol = QEMU_COLOR_GREEN;
  774. break;
  775. case 43:
  776. s->t_attrib.bgcol = QEMU_COLOR_YELLOW;
  777. break;
  778. case 44:
  779. s->t_attrib.bgcol = QEMU_COLOR_BLUE;
  780. break;
  781. case 45:
  782. s->t_attrib.bgcol = QEMU_COLOR_MAGENTA;
  783. break;
  784. case 46:
  785. s->t_attrib.bgcol = QEMU_COLOR_CYAN;
  786. break;
  787. case 47:
  788. s->t_attrib.bgcol = QEMU_COLOR_WHITE;
  789. break;
  790. }
  791. }
  792. }
  793. static void console_clear_xy(QemuConsole *s, int x, int y)
  794. {
  795. int y1 = (s->y_base + y) % s->total_height;
  796. if (x >= s->width) {
  797. x = s->width - 1;
  798. }
  799. TextCell *c = &s->cells[y1 * s->width + x];
  800. c->ch = ' ';
  801. c->t_attrib = s->t_attrib_default;
  802. update_xy(s, x, y);
  803. }
  804. static void console_put_one(QemuConsole *s, int ch)
  805. {
  806. TextCell *c;
  807. int y1;
  808. if (s->x >= s->width) {
  809. /* line wrap */
  810. s->x = 0;
  811. console_put_lf(s);
  812. }
  813. y1 = (s->y_base + s->y) % s->total_height;
  814. c = &s->cells[y1 * s->width + s->x];
  815. c->ch = ch;
  816. c->t_attrib = s->t_attrib;
  817. update_xy(s, s->x, s->y);
  818. s->x++;
  819. }
  820. static void console_respond_str(QemuConsole *s, const char *buf)
  821. {
  822. while (*buf) {
  823. console_put_one(s, *buf);
  824. buf++;
  825. }
  826. }
  827. /* set cursor, checking bounds */
  828. static void set_cursor(QemuConsole *s, int x, int y)
  829. {
  830. if (x < 0) {
  831. x = 0;
  832. }
  833. if (y < 0) {
  834. y = 0;
  835. }
  836. if (y >= s->height) {
  837. y = s->height - 1;
  838. }
  839. if (x >= s->width) {
  840. x = s->width - 1;
  841. }
  842. s->x = x;
  843. s->y = y;
  844. }
  845. static void console_putchar(QemuConsole *s, int ch)
  846. {
  847. int i;
  848. int x, y;
  849. char response[40];
  850. switch(s->state) {
  851. case TTY_STATE_NORM:
  852. switch(ch) {
  853. case '\r': /* carriage return */
  854. s->x = 0;
  855. break;
  856. case '\n': /* newline */
  857. console_put_lf(s);
  858. break;
  859. case '\b': /* backspace */
  860. if (s->x > 0)
  861. s->x--;
  862. break;
  863. case '\t': /* tabspace */
  864. if (s->x + (8 - (s->x % 8)) > s->width) {
  865. s->x = 0;
  866. console_put_lf(s);
  867. } else {
  868. s->x = s->x + (8 - (s->x % 8));
  869. }
  870. break;
  871. case '\a': /* alert aka. bell */
  872. /* TODO: has to be implemented */
  873. break;
  874. case 14:
  875. /* SI (shift in), character set 0 (ignored) */
  876. break;
  877. case 15:
  878. /* SO (shift out), character set 1 (ignored) */
  879. break;
  880. case 27: /* esc (introducing an escape sequence) */
  881. s->state = TTY_STATE_ESC;
  882. break;
  883. default:
  884. console_put_one(s, ch);
  885. break;
  886. }
  887. break;
  888. case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
  889. if (ch == '[') {
  890. for(i=0;i<MAX_ESC_PARAMS;i++)
  891. s->esc_params[i] = 0;
  892. s->nb_esc_params = 0;
  893. s->state = TTY_STATE_CSI;
  894. } else {
  895. s->state = TTY_STATE_NORM;
  896. }
  897. break;
  898. case TTY_STATE_CSI: /* handle escape sequence parameters */
  899. if (ch >= '0' && ch <= '9') {
  900. if (s->nb_esc_params < MAX_ESC_PARAMS) {
  901. int *param = &s->esc_params[s->nb_esc_params];
  902. int digit = (ch - '0');
  903. *param = (*param <= (INT_MAX - digit) / 10) ?
  904. *param * 10 + digit : INT_MAX;
  905. }
  906. } else {
  907. if (s->nb_esc_params < MAX_ESC_PARAMS)
  908. s->nb_esc_params++;
  909. if (ch == ';' || ch == '?') {
  910. break;
  911. }
  912. trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
  913. ch, s->nb_esc_params);
  914. s->state = TTY_STATE_NORM;
  915. switch(ch) {
  916. case 'A':
  917. /* move cursor up */
  918. if (s->esc_params[0] == 0) {
  919. s->esc_params[0] = 1;
  920. }
  921. set_cursor(s, s->x, s->y - s->esc_params[0]);
  922. break;
  923. case 'B':
  924. /* move cursor down */
  925. if (s->esc_params[0] == 0) {
  926. s->esc_params[0] = 1;
  927. }
  928. set_cursor(s, s->x, s->y + s->esc_params[0]);
  929. break;
  930. case 'C':
  931. /* move cursor right */
  932. if (s->esc_params[0] == 0) {
  933. s->esc_params[0] = 1;
  934. }
  935. set_cursor(s, s->x + s->esc_params[0], s->y);
  936. break;
  937. case 'D':
  938. /* move cursor left */
  939. if (s->esc_params[0] == 0) {
  940. s->esc_params[0] = 1;
  941. }
  942. set_cursor(s, s->x - s->esc_params[0], s->y);
  943. break;
  944. case 'G':
  945. /* move cursor to column */
  946. set_cursor(s, s->esc_params[0] - 1, s->y);
  947. break;
  948. case 'f':
  949. case 'H':
  950. /* move cursor to row, column */
  951. set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
  952. break;
  953. case 'J':
  954. switch (s->esc_params[0]) {
  955. case 0:
  956. /* clear to end of screen */
  957. for (y = s->y; y < s->height; y++) {
  958. for (x = 0; x < s->width; x++) {
  959. if (y == s->y && x < s->x) {
  960. continue;
  961. }
  962. console_clear_xy(s, x, y);
  963. }
  964. }
  965. break;
  966. case 1:
  967. /* clear from beginning of screen */
  968. for (y = 0; y <= s->y; y++) {
  969. for (x = 0; x < s->width; x++) {
  970. if (y == s->y && x > s->x) {
  971. break;
  972. }
  973. console_clear_xy(s, x, y);
  974. }
  975. }
  976. break;
  977. case 2:
  978. /* clear entire screen */
  979. for (y = 0; y <= s->height; y++) {
  980. for (x = 0; x < s->width; x++) {
  981. console_clear_xy(s, x, y);
  982. }
  983. }
  984. break;
  985. }
  986. break;
  987. case 'K':
  988. switch (s->esc_params[0]) {
  989. case 0:
  990. /* clear to eol */
  991. for(x = s->x; x < s->width; x++) {
  992. console_clear_xy(s, x, s->y);
  993. }
  994. break;
  995. case 1:
  996. /* clear from beginning of line */
  997. for (x = 0; x <= s->x && x < s->width; x++) {
  998. console_clear_xy(s, x, s->y);
  999. }
  1000. break;
  1001. case 2:
  1002. /* clear entire line */
  1003. for(x = 0; x < s->width; x++) {
  1004. console_clear_xy(s, x, s->y);
  1005. }
  1006. break;
  1007. }
  1008. break;
  1009. case 'm':
  1010. console_handle_escape(s);
  1011. break;
  1012. case 'n':
  1013. switch (s->esc_params[0]) {
  1014. case 5:
  1015. /* report console status (always succeed)*/
  1016. console_respond_str(s, "\033[0n");
  1017. break;
  1018. case 6:
  1019. /* report cursor position */
  1020. sprintf(response, "\033[%d;%dR",
  1021. (s->y_base + s->y) % s->total_height + 1,
  1022. s->x + 1);
  1023. console_respond_str(s, response);
  1024. break;
  1025. }
  1026. break;
  1027. case 's':
  1028. /* save cursor position */
  1029. s->x_saved = s->x;
  1030. s->y_saved = s->y;
  1031. break;
  1032. case 'u':
  1033. /* restore cursor position */
  1034. s->x = s->x_saved;
  1035. s->y = s->y_saved;
  1036. break;
  1037. default:
  1038. trace_console_putchar_unhandled(ch);
  1039. break;
  1040. }
  1041. break;
  1042. }
  1043. }
  1044. }
  1045. static void displaychangelistener_gfx_switch(DisplayChangeListener *dcl,
  1046. struct DisplaySurface *new_surface,
  1047. bool update)
  1048. {
  1049. if (dcl->ops->dpy_gfx_switch) {
  1050. dcl->ops->dpy_gfx_switch(dcl, new_surface);
  1051. }
  1052. if (update && dcl->ops->dpy_gfx_update) {
  1053. dcl->ops->dpy_gfx_update(dcl, 0, 0,
  1054. surface_width(new_surface),
  1055. surface_height(new_surface));
  1056. }
  1057. }
  1058. static void dpy_gfx_create_texture(QemuConsole *con, DisplaySurface *surface)
  1059. {
  1060. if (con->gl && con->gl->ops->dpy_gl_ctx_create_texture) {
  1061. con->gl->ops->dpy_gl_ctx_create_texture(con->gl, surface);
  1062. }
  1063. }
  1064. static void dpy_gfx_destroy_texture(QemuConsole *con, DisplaySurface *surface)
  1065. {
  1066. if (con->gl && con->gl->ops->dpy_gl_ctx_destroy_texture) {
  1067. con->gl->ops->dpy_gl_ctx_destroy_texture(con->gl, surface);
  1068. }
  1069. }
  1070. static void dpy_gfx_update_texture(QemuConsole *con, DisplaySurface *surface,
  1071. int x, int y, int w, int h)
  1072. {
  1073. if (con->gl && con->gl->ops->dpy_gl_ctx_update_texture) {
  1074. con->gl->ops->dpy_gl_ctx_update_texture(con->gl, surface, x, y, w, h);
  1075. }
  1076. }
  1077. static void displaychangelistener_display_console(DisplayChangeListener *dcl,
  1078. QemuConsole *con,
  1079. Error **errp)
  1080. {
  1081. static const char nodev[] =
  1082. "This VM has no graphic display device.";
  1083. static DisplaySurface *dummy;
  1084. if (!con || !console_compatible_with(con, dcl, errp)) {
  1085. if (!dummy) {
  1086. dummy = qemu_create_placeholder_surface(640, 480, nodev);
  1087. }
  1088. if (con) {
  1089. dpy_gfx_create_texture(con, dummy);
  1090. }
  1091. displaychangelistener_gfx_switch(dcl, dummy, TRUE);
  1092. return;
  1093. }
  1094. dpy_gfx_create_texture(con, con->surface);
  1095. displaychangelistener_gfx_switch(dcl, con->surface,
  1096. con->scanout.kind == SCANOUT_SURFACE);
  1097. if (con->scanout.kind == SCANOUT_DMABUF &&
  1098. displaychangelistener_has_dmabuf(dcl)) {
  1099. dcl->ops->dpy_gl_scanout_dmabuf(dcl, con->scanout.dmabuf);
  1100. } else if (con->scanout.kind == SCANOUT_TEXTURE &&
  1101. dcl->ops->dpy_gl_scanout_texture) {
  1102. dcl->ops->dpy_gl_scanout_texture(dcl,
  1103. con->scanout.texture.backing_id,
  1104. con->scanout.texture.backing_borrow,
  1105. con->scanout.texture.x,
  1106. con->scanout.texture.y,
  1107. con->scanout.texture.width,
  1108. con->scanout.texture.height);
  1109. }
  1110. }
  1111. struct VCChardev {
  1112. Chardev parent;
  1113. QemuConsole *console;
  1114. };
  1115. typedef struct VCChardev VCChardev;
  1116. #define TYPE_CHARDEV_VC "chardev-vc"
  1117. DECLARE_INSTANCE_CHECKER(VCChardev, VC_CHARDEV,
  1118. TYPE_CHARDEV_VC)
  1119. static int vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
  1120. {
  1121. VCChardev *drv = VC_CHARDEV(chr);
  1122. QemuConsole *s = drv->console;
  1123. int i;
  1124. if (!s->ds) {
  1125. return 0;
  1126. }
  1127. s->update_x0 = s->width * FONT_WIDTH;
  1128. s->update_y0 = s->height * FONT_HEIGHT;
  1129. s->update_x1 = 0;
  1130. s->update_y1 = 0;
  1131. console_show_cursor(s, 0);
  1132. for(i = 0; i < len; i++) {
  1133. console_putchar(s, buf[i]);
  1134. }
  1135. console_show_cursor(s, 1);
  1136. if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
  1137. dpy_gfx_update(s, s->update_x0, s->update_y0,
  1138. s->update_x1 - s->update_x0,
  1139. s->update_y1 - s->update_y0);
  1140. }
  1141. return len;
  1142. }
  1143. static void kbd_send_chars(QemuConsole *s)
  1144. {
  1145. uint32_t len, avail;
  1146. len = qemu_chr_be_can_write(s->chr);
  1147. avail = fifo8_num_used(&s->out_fifo);
  1148. while (len > 0 && avail > 0) {
  1149. const uint8_t *buf;
  1150. uint32_t size;
  1151. buf = fifo8_pop_buf(&s->out_fifo, MIN(len, avail), &size);
  1152. qemu_chr_be_write(s->chr, buf, size);
  1153. len = qemu_chr_be_can_write(s->chr);
  1154. avail -= size;
  1155. }
  1156. }
  1157. /* called when an ascii key is pressed */
  1158. void kbd_put_keysym_console(QemuConsole *s, int keysym)
  1159. {
  1160. uint8_t buf[16], *q;
  1161. int c;
  1162. uint32_t num_free;
  1163. if (!s || (s->console_type == GRAPHIC_CONSOLE))
  1164. return;
  1165. switch(keysym) {
  1166. case QEMU_KEY_CTRL_UP:
  1167. console_scroll(s, -1);
  1168. break;
  1169. case QEMU_KEY_CTRL_DOWN:
  1170. console_scroll(s, 1);
  1171. break;
  1172. case QEMU_KEY_CTRL_PAGEUP:
  1173. console_scroll(s, -10);
  1174. break;
  1175. case QEMU_KEY_CTRL_PAGEDOWN:
  1176. console_scroll(s, 10);
  1177. break;
  1178. default:
  1179. /* convert the QEMU keysym to VT100 key string */
  1180. q = buf;
  1181. if (keysym >= 0xe100 && keysym <= 0xe11f) {
  1182. *q++ = '\033';
  1183. *q++ = '[';
  1184. c = keysym - 0xe100;
  1185. if (c >= 10)
  1186. *q++ = '0' + (c / 10);
  1187. *q++ = '0' + (c % 10);
  1188. *q++ = '~';
  1189. } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
  1190. *q++ = '\033';
  1191. *q++ = '[';
  1192. *q++ = keysym & 0xff;
  1193. } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
  1194. vc_chr_write(s->chr, (const uint8_t *) "\r", 1);
  1195. *q++ = '\n';
  1196. } else {
  1197. *q++ = keysym;
  1198. }
  1199. if (s->echo) {
  1200. vc_chr_write(s->chr, buf, q - buf);
  1201. }
  1202. num_free = fifo8_num_free(&s->out_fifo);
  1203. fifo8_push_all(&s->out_fifo, buf, MIN(num_free, q - buf));
  1204. kbd_send_chars(s);
  1205. break;
  1206. }
  1207. }
  1208. static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
  1209. [Q_KEY_CODE_UP] = QEMU_KEY_UP,
  1210. [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
  1211. [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
  1212. [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
  1213. [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
  1214. [Q_KEY_CODE_END] = QEMU_KEY_END,
  1215. [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
  1216. [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
  1217. [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
  1218. [Q_KEY_CODE_TAB] = QEMU_KEY_TAB,
  1219. [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
  1220. };
  1221. static const int ctrl_qcode_to_keysym[Q_KEY_CODE__MAX] = {
  1222. [Q_KEY_CODE_UP] = QEMU_KEY_CTRL_UP,
  1223. [Q_KEY_CODE_DOWN] = QEMU_KEY_CTRL_DOWN,
  1224. [Q_KEY_CODE_RIGHT] = QEMU_KEY_CTRL_RIGHT,
  1225. [Q_KEY_CODE_LEFT] = QEMU_KEY_CTRL_LEFT,
  1226. [Q_KEY_CODE_HOME] = QEMU_KEY_CTRL_HOME,
  1227. [Q_KEY_CODE_END] = QEMU_KEY_CTRL_END,
  1228. [Q_KEY_CODE_PGUP] = QEMU_KEY_CTRL_PAGEUP,
  1229. [Q_KEY_CODE_PGDN] = QEMU_KEY_CTRL_PAGEDOWN,
  1230. };
  1231. bool kbd_put_qcode_console(QemuConsole *s, int qcode, bool ctrl)
  1232. {
  1233. int keysym;
  1234. keysym = ctrl ? ctrl_qcode_to_keysym[qcode] : qcode_to_keysym[qcode];
  1235. if (keysym == 0) {
  1236. return false;
  1237. }
  1238. kbd_put_keysym_console(s, keysym);
  1239. return true;
  1240. }
  1241. void kbd_put_string_console(QemuConsole *s, const char *str, int len)
  1242. {
  1243. int i;
  1244. for (i = 0; i < len && str[i]; i++) {
  1245. kbd_put_keysym_console(s, str[i]);
  1246. }
  1247. }
  1248. static void text_console_invalidate(void *opaque)
  1249. {
  1250. QemuConsole *s = (QemuConsole *) opaque;
  1251. if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
  1252. text_console_resize(s);
  1253. }
  1254. console_refresh(s);
  1255. }
  1256. static void text_console_update(void *opaque, console_ch_t *chardata)
  1257. {
  1258. QemuConsole *s = (QemuConsole *) opaque;
  1259. int i, j, src;
  1260. if (s->text_x[0] <= s->text_x[1]) {
  1261. src = (s->y_base + s->text_y[0]) * s->width;
  1262. chardata += s->text_y[0] * s->width;
  1263. for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
  1264. for (j = 0; j < s->width; j++, src++) {
  1265. console_write_ch(chardata ++,
  1266. ATTR2CHTYPE(s->cells[src].ch,
  1267. s->cells[src].t_attrib.fgcol,
  1268. s->cells[src].t_attrib.bgcol,
  1269. s->cells[src].t_attrib.bold));
  1270. }
  1271. dpy_text_update(s, s->text_x[0], s->text_y[0],
  1272. s->text_x[1] - s->text_x[0], i - s->text_y[0]);
  1273. s->text_x[0] = s->width;
  1274. s->text_y[0] = s->height;
  1275. s->text_x[1] = 0;
  1276. s->text_y[1] = 0;
  1277. }
  1278. if (s->cursor_invalidate) {
  1279. dpy_text_cursor(s, s->x, s->y);
  1280. s->cursor_invalidate = 0;
  1281. }
  1282. }
  1283. static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
  1284. uint32_t head)
  1285. {
  1286. Object *obj;
  1287. QemuConsole *s;
  1288. int i;
  1289. obj = object_new(TYPE_QEMU_CONSOLE);
  1290. s = QEMU_CONSOLE(obj);
  1291. qemu_co_queue_init(&s->dump_queue);
  1292. s->head = head;
  1293. object_property_add_link(obj, "device", TYPE_DEVICE,
  1294. (Object **)&s->device,
  1295. object_property_allow_set_link,
  1296. OBJ_PROP_LINK_STRONG);
  1297. object_property_add_uint32_ptr(obj, "head", &s->head,
  1298. OBJ_PROP_FLAG_READ);
  1299. if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
  1300. (console_type == GRAPHIC_CONSOLE))) {
  1301. active_console = s;
  1302. }
  1303. s->ds = ds;
  1304. s->console_type = console_type;
  1305. s->window_id = -1;
  1306. if (QTAILQ_EMPTY(&consoles)) {
  1307. s->index = 0;
  1308. QTAILQ_INSERT_TAIL(&consoles, s, next);
  1309. } else if (console_type != GRAPHIC_CONSOLE || phase_check(PHASE_MACHINE_READY)) {
  1310. QemuConsole *last = QTAILQ_LAST(&consoles);
  1311. s->index = last->index + 1;
  1312. QTAILQ_INSERT_TAIL(&consoles, s, next);
  1313. } else {
  1314. /*
  1315. * HACK: Put graphical consoles before text consoles.
  1316. *
  1317. * Only do that for coldplugged devices. After initial device
  1318. * initialization we will not renumber the consoles any more.
  1319. */
  1320. QemuConsole *c = QTAILQ_FIRST(&consoles);
  1321. while (QTAILQ_NEXT(c, next) != NULL &&
  1322. c->console_type == GRAPHIC_CONSOLE) {
  1323. c = QTAILQ_NEXT(c, next);
  1324. }
  1325. if (c->console_type == GRAPHIC_CONSOLE) {
  1326. /* have no text consoles */
  1327. s->index = c->index + 1;
  1328. QTAILQ_INSERT_AFTER(&consoles, c, s, next);
  1329. } else {
  1330. s->index = c->index;
  1331. QTAILQ_INSERT_BEFORE(c, s, next);
  1332. /* renumber text consoles */
  1333. for (i = s->index + 1; c != NULL; c = QTAILQ_NEXT(c, next), i++) {
  1334. c->index = i;
  1335. }
  1336. }
  1337. }
  1338. return s;
  1339. }
  1340. DisplaySurface *qemu_create_displaysurface(int width, int height)
  1341. {
  1342. DisplaySurface *surface = g_new0(DisplaySurface, 1);
  1343. trace_displaysurface_create(surface, width, height);
  1344. surface->format = PIXMAN_x8r8g8b8;
  1345. surface->image = pixman_image_create_bits(surface->format,
  1346. width, height,
  1347. NULL, width * 4);
  1348. assert(surface->image != NULL);
  1349. surface->flags = QEMU_ALLOCATED_FLAG;
  1350. return surface;
  1351. }
  1352. DisplaySurface *qemu_create_displaysurface_from(int width, int height,
  1353. pixman_format_code_t format,
  1354. int linesize, uint8_t *data)
  1355. {
  1356. DisplaySurface *surface = g_new0(DisplaySurface, 1);
  1357. trace_displaysurface_create_from(surface, width, height, format);
  1358. surface->format = format;
  1359. surface->image = pixman_image_create_bits(surface->format,
  1360. width, height,
  1361. (void *)data, linesize);
  1362. assert(surface->image != NULL);
  1363. return surface;
  1364. }
  1365. DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
  1366. {
  1367. DisplaySurface *surface = g_new0(DisplaySurface, 1);
  1368. trace_displaysurface_create_pixman(surface);
  1369. surface->format = pixman_image_get_format(image);
  1370. surface->image = pixman_image_ref(image);
  1371. return surface;
  1372. }
  1373. DisplaySurface *qemu_create_placeholder_surface(int w, int h,
  1374. const char *msg)
  1375. {
  1376. DisplaySurface *surface = qemu_create_displaysurface(w, h);
  1377. pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK];
  1378. pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE];
  1379. pixman_image_t *glyph;
  1380. int len, x, y, i;
  1381. len = strlen(msg);
  1382. x = (w / FONT_WIDTH - len) / 2;
  1383. y = (h / FONT_HEIGHT - 1) / 2;
  1384. for (i = 0; i < len; i++) {
  1385. glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
  1386. qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
  1387. x+i, y, FONT_WIDTH, FONT_HEIGHT);
  1388. qemu_pixman_image_unref(glyph);
  1389. }
  1390. surface->flags |= QEMU_PLACEHOLDER_FLAG;
  1391. return surface;
  1392. }
  1393. void qemu_free_displaysurface(DisplaySurface *surface)
  1394. {
  1395. if (surface == NULL) {
  1396. return;
  1397. }
  1398. trace_displaysurface_free(surface);
  1399. qemu_pixman_image_unref(surface->image);
  1400. g_free(surface);
  1401. }
  1402. bool console_has_gl(QemuConsole *con)
  1403. {
  1404. return con->gl != NULL;
  1405. }
  1406. static bool displaychangelistener_has_dmabuf(DisplayChangeListener *dcl)
  1407. {
  1408. if (dcl->ops->dpy_has_dmabuf) {
  1409. return dcl->ops->dpy_has_dmabuf(dcl);
  1410. }
  1411. if (dcl->ops->dpy_gl_scanout_dmabuf) {
  1412. return true;
  1413. }
  1414. return false;
  1415. }
  1416. static bool console_compatible_with(QemuConsole *con,
  1417. DisplayChangeListener *dcl, Error **errp)
  1418. {
  1419. int flags;
  1420. flags = con->hw_ops->get_flags ? con->hw_ops->get_flags(con->hw) : 0;
  1421. if (console_has_gl(con) &&
  1422. !con->gl->ops->dpy_gl_ctx_is_compatible_dcl(con->gl, dcl)) {
  1423. error_setg(errp, "Display %s is incompatible with the GL context",
  1424. dcl->ops->dpy_name);
  1425. return false;
  1426. }
  1427. if (flags & GRAPHIC_FLAGS_GL &&
  1428. !console_has_gl(con)) {
  1429. error_setg(errp, "The console requires a GL context.");
  1430. return false;
  1431. }
  1432. if (flags & GRAPHIC_FLAGS_DMABUF &&
  1433. !displaychangelistener_has_dmabuf(dcl)) {
  1434. error_setg(errp, "The console requires display DMABUF support.");
  1435. return false;
  1436. }
  1437. return true;
  1438. }
  1439. void qemu_console_set_display_gl_ctx(QemuConsole *con, DisplayGLCtx *gl)
  1440. {
  1441. /* display has opengl support */
  1442. assert(con);
  1443. if (con->gl) {
  1444. error_report("The console already has an OpenGL context.");
  1445. exit(1);
  1446. }
  1447. con->gl = gl;
  1448. }
  1449. void register_displaychangelistener(DisplayChangeListener *dcl)
  1450. {
  1451. QemuConsole *con;
  1452. assert(!dcl->ds);
  1453. trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
  1454. dcl->ds = get_alloc_displaystate();
  1455. QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
  1456. gui_setup_refresh(dcl->ds);
  1457. if (dcl->con) {
  1458. dcl->con->dcls++;
  1459. con = dcl->con;
  1460. } else {
  1461. con = active_console;
  1462. }
  1463. displaychangelistener_display_console(dcl, con, dcl->con ? &error_fatal : NULL);
  1464. if (con && con->cursor && dcl->ops->dpy_cursor_define) {
  1465. dcl->ops->dpy_cursor_define(dcl, con->cursor);
  1466. }
  1467. if (con && dcl->ops->dpy_mouse_set) {
  1468. dcl->ops->dpy_mouse_set(dcl, con->cursor_x, con->cursor_y, con->cursor_on);
  1469. }
  1470. if (dcl->ds->have_text) {
  1471. dpy_text_resize(dcl->con, dcl->con->width, dcl->con->height);
  1472. }
  1473. text_console_update_cursor(NULL);
  1474. }
  1475. void update_displaychangelistener(DisplayChangeListener *dcl,
  1476. uint64_t interval)
  1477. {
  1478. DisplayState *ds = dcl->ds;
  1479. dcl->update_interval = interval;
  1480. if (!ds->refreshing && ds->update_interval > interval) {
  1481. timer_mod(ds->gui_timer, ds->last_update + interval);
  1482. }
  1483. }
  1484. void unregister_displaychangelistener(DisplayChangeListener *dcl)
  1485. {
  1486. DisplayState *ds = dcl->ds;
  1487. trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
  1488. if (dcl->con) {
  1489. dcl->con->dcls--;
  1490. }
  1491. QLIST_REMOVE(dcl, next);
  1492. dcl->ds = NULL;
  1493. gui_setup_refresh(ds);
  1494. }
  1495. static void dpy_set_ui_info_timer(void *opaque)
  1496. {
  1497. QemuConsole *con = opaque;
  1498. con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
  1499. }
  1500. bool dpy_ui_info_supported(QemuConsole *con)
  1501. {
  1502. if (con == NULL) {
  1503. con = active_console;
  1504. }
  1505. return con->hw_ops->ui_info != NULL;
  1506. }
  1507. const QemuUIInfo *dpy_get_ui_info(const QemuConsole *con)
  1508. {
  1509. if (con == NULL) {
  1510. con = active_console;
  1511. }
  1512. return &con->ui_info;
  1513. }
  1514. int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info, bool delay)
  1515. {
  1516. if (con == NULL) {
  1517. con = active_console;
  1518. }
  1519. if (!dpy_ui_info_supported(con)) {
  1520. return -1;
  1521. }
  1522. if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
  1523. /* nothing changed -- ignore */
  1524. return 0;
  1525. }
  1526. /*
  1527. * Typically we get a flood of these as the user resizes the window.
  1528. * Wait until the dust has settled (one second without updates), then
  1529. * go notify the guest.
  1530. */
  1531. con->ui_info = *info;
  1532. timer_mod(con->ui_timer,
  1533. qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + (delay ? 1000 : 0));
  1534. return 0;
  1535. }
  1536. void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
  1537. {
  1538. DisplayState *s = con->ds;
  1539. DisplayChangeListener *dcl;
  1540. int width = qemu_console_get_width(con, x + w);
  1541. int height = qemu_console_get_height(con, y + h);
  1542. x = MAX(x, 0);
  1543. y = MAX(y, 0);
  1544. x = MIN(x, width);
  1545. y = MIN(y, height);
  1546. w = MIN(w, width - x);
  1547. h = MIN(h, height - y);
  1548. if (!qemu_console_is_visible(con)) {
  1549. return;
  1550. }
  1551. dpy_gfx_update_texture(con, con->surface, x, y, w, h);
  1552. QLIST_FOREACH(dcl, &s->listeners, next) {
  1553. if (con != (dcl->con ? dcl->con : active_console)) {
  1554. continue;
  1555. }
  1556. if (dcl->ops->dpy_gfx_update) {
  1557. dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
  1558. }
  1559. }
  1560. }
  1561. void dpy_gfx_update_full(QemuConsole *con)
  1562. {
  1563. int w = qemu_console_get_width(con, 0);
  1564. int h = qemu_console_get_height(con, 0);
  1565. dpy_gfx_update(con, 0, 0, w, h);
  1566. }
  1567. void dpy_gfx_replace_surface(QemuConsole *con,
  1568. DisplaySurface *surface)
  1569. {
  1570. static const char placeholder_msg[] = "Display output is not active.";
  1571. DisplayState *s = con->ds;
  1572. DisplaySurface *old_surface = con->surface;
  1573. DisplayChangeListener *dcl;
  1574. int width;
  1575. int height;
  1576. if (!surface) {
  1577. if (old_surface) {
  1578. width = surface_width(old_surface);
  1579. height = surface_height(old_surface);
  1580. } else {
  1581. width = 640;
  1582. height = 480;
  1583. }
  1584. surface = qemu_create_placeholder_surface(width, height, placeholder_msg);
  1585. }
  1586. assert(old_surface != surface);
  1587. con->scanout.kind = SCANOUT_SURFACE;
  1588. con->surface = surface;
  1589. dpy_gfx_create_texture(con, surface);
  1590. QLIST_FOREACH(dcl, &s->listeners, next) {
  1591. if (con != (dcl->con ? dcl->con : active_console)) {
  1592. continue;
  1593. }
  1594. displaychangelistener_gfx_switch(dcl, surface, FALSE);
  1595. }
  1596. dpy_gfx_destroy_texture(con, old_surface);
  1597. qemu_free_displaysurface(old_surface);
  1598. }
  1599. bool dpy_gfx_check_format(QemuConsole *con,
  1600. pixman_format_code_t format)
  1601. {
  1602. DisplayChangeListener *dcl;
  1603. DisplayState *s = con->ds;
  1604. QLIST_FOREACH(dcl, &s->listeners, next) {
  1605. if (dcl->con && dcl->con != con) {
  1606. /* dcl bound to another console -> skip */
  1607. continue;
  1608. }
  1609. if (dcl->ops->dpy_gfx_check_format) {
  1610. if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
  1611. return false;
  1612. }
  1613. } else {
  1614. /* default is to allow native 32 bpp only */
  1615. if (format != qemu_default_pixman_format(32, true)) {
  1616. return false;
  1617. }
  1618. }
  1619. }
  1620. return true;
  1621. }
  1622. static void dpy_refresh(DisplayState *s)
  1623. {
  1624. DisplayChangeListener *dcl;
  1625. QLIST_FOREACH(dcl, &s->listeners, next) {
  1626. if (dcl->ops->dpy_refresh) {
  1627. dcl->ops->dpy_refresh(dcl);
  1628. }
  1629. }
  1630. }
  1631. void dpy_text_cursor(QemuConsole *con, int x, int y)
  1632. {
  1633. DisplayState *s = con->ds;
  1634. DisplayChangeListener *dcl;
  1635. if (!qemu_console_is_visible(con)) {
  1636. return;
  1637. }
  1638. QLIST_FOREACH(dcl, &s->listeners, next) {
  1639. if (con != (dcl->con ? dcl->con : active_console)) {
  1640. continue;
  1641. }
  1642. if (dcl->ops->dpy_text_cursor) {
  1643. dcl->ops->dpy_text_cursor(dcl, x, y);
  1644. }
  1645. }
  1646. }
  1647. void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
  1648. {
  1649. DisplayState *s = con->ds;
  1650. DisplayChangeListener *dcl;
  1651. if (!qemu_console_is_visible(con)) {
  1652. return;
  1653. }
  1654. QLIST_FOREACH(dcl, &s->listeners, next) {
  1655. if (con != (dcl->con ? dcl->con : active_console)) {
  1656. continue;
  1657. }
  1658. if (dcl->ops->dpy_text_update) {
  1659. dcl->ops->dpy_text_update(dcl, x, y, w, h);
  1660. }
  1661. }
  1662. }
  1663. void dpy_text_resize(QemuConsole *con, int w, int h)
  1664. {
  1665. DisplayState *s = con->ds;
  1666. DisplayChangeListener *dcl;
  1667. if (!qemu_console_is_visible(con)) {
  1668. return;
  1669. }
  1670. QLIST_FOREACH(dcl, &s->listeners, next) {
  1671. if (con != (dcl->con ? dcl->con : active_console)) {
  1672. continue;
  1673. }
  1674. if (dcl->ops->dpy_text_resize) {
  1675. dcl->ops->dpy_text_resize(dcl, w, h);
  1676. }
  1677. }
  1678. }
  1679. void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
  1680. {
  1681. DisplayState *s = con->ds;
  1682. DisplayChangeListener *dcl;
  1683. con->cursor_x = x;
  1684. con->cursor_y = y;
  1685. con->cursor_on = on;
  1686. if (!qemu_console_is_visible(con)) {
  1687. return;
  1688. }
  1689. QLIST_FOREACH(dcl, &s->listeners, next) {
  1690. if (con != (dcl->con ? dcl->con : active_console)) {
  1691. continue;
  1692. }
  1693. if (dcl->ops->dpy_mouse_set) {
  1694. dcl->ops->dpy_mouse_set(dcl, x, y, on);
  1695. }
  1696. }
  1697. }
  1698. void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
  1699. {
  1700. DisplayState *s = con->ds;
  1701. DisplayChangeListener *dcl;
  1702. cursor_unref(con->cursor);
  1703. con->cursor = cursor_ref(cursor);
  1704. if (!qemu_console_is_visible(con)) {
  1705. return;
  1706. }
  1707. QLIST_FOREACH(dcl, &s->listeners, next) {
  1708. if (con != (dcl->con ? dcl->con : active_console)) {
  1709. continue;
  1710. }
  1711. if (dcl->ops->dpy_cursor_define) {
  1712. dcl->ops->dpy_cursor_define(dcl, cursor);
  1713. }
  1714. }
  1715. }
  1716. bool dpy_cursor_define_supported(QemuConsole *con)
  1717. {
  1718. DisplayState *s = con->ds;
  1719. DisplayChangeListener *dcl;
  1720. QLIST_FOREACH(dcl, &s->listeners, next) {
  1721. if (dcl->ops->dpy_cursor_define) {
  1722. return true;
  1723. }
  1724. }
  1725. return false;
  1726. }
  1727. QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
  1728. struct QEMUGLParams *qparams)
  1729. {
  1730. assert(con->gl);
  1731. return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
  1732. }
  1733. void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
  1734. {
  1735. assert(con->gl);
  1736. con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
  1737. }
  1738. int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
  1739. {
  1740. assert(con->gl);
  1741. return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
  1742. }
  1743. void dpy_gl_scanout_disable(QemuConsole *con)
  1744. {
  1745. DisplayState *s = con->ds;
  1746. DisplayChangeListener *dcl;
  1747. if (con->scanout.kind != SCANOUT_SURFACE) {
  1748. con->scanout.kind = SCANOUT_NONE;
  1749. }
  1750. QLIST_FOREACH(dcl, &s->listeners, next) {
  1751. if (con != (dcl->con ? dcl->con : active_console)) {
  1752. continue;
  1753. }
  1754. if (dcl->ops->dpy_gl_scanout_disable) {
  1755. dcl->ops->dpy_gl_scanout_disable(dcl);
  1756. }
  1757. }
  1758. }
  1759. void dpy_gl_scanout_texture(QemuConsole *con,
  1760. uint32_t backing_id,
  1761. DisplayGLTextureBorrower backing_borrow,
  1762. uint32_t x, uint32_t y,
  1763. uint32_t width, uint32_t height)
  1764. {
  1765. DisplayState *s = con->ds;
  1766. DisplayChangeListener *dcl;
  1767. con->scanout.kind = SCANOUT_TEXTURE;
  1768. con->scanout.texture = (ScanoutTexture) {
  1769. backing_id, backing_borrow,
  1770. x, y, width, height
  1771. };
  1772. QLIST_FOREACH(dcl, &s->listeners, next) {
  1773. if (con != (dcl->con ? dcl->con : active_console)) {
  1774. continue;
  1775. }
  1776. if (dcl->ops->dpy_gl_scanout_texture) {
  1777. dcl->ops->dpy_gl_scanout_texture(dcl, backing_id, backing_borrow,
  1778. x, y, width, height);
  1779. }
  1780. }
  1781. }
  1782. void dpy_gl_scanout_dmabuf(QemuConsole *con,
  1783. QemuDmaBuf *dmabuf)
  1784. {
  1785. DisplayState *s = con->ds;
  1786. DisplayChangeListener *dcl;
  1787. con->scanout.kind = SCANOUT_DMABUF;
  1788. con->scanout.dmabuf = dmabuf;
  1789. QLIST_FOREACH(dcl, &s->listeners, next) {
  1790. if (con != (dcl->con ? dcl->con : active_console)) {
  1791. continue;
  1792. }
  1793. if (dcl->ops->dpy_gl_scanout_dmabuf) {
  1794. dcl->ops->dpy_gl_scanout_dmabuf(dcl, dmabuf);
  1795. }
  1796. }
  1797. }
  1798. void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
  1799. bool have_hot, uint32_t hot_x, uint32_t hot_y)
  1800. {
  1801. DisplayState *s = con->ds;
  1802. DisplayChangeListener *dcl;
  1803. QLIST_FOREACH(dcl, &s->listeners, next) {
  1804. if (con != (dcl->con ? dcl->con : active_console)) {
  1805. continue;
  1806. }
  1807. if (dcl->ops->dpy_gl_cursor_dmabuf) {
  1808. dcl->ops->dpy_gl_cursor_dmabuf(dcl, dmabuf,
  1809. have_hot, hot_x, hot_y);
  1810. }
  1811. }
  1812. }
  1813. void dpy_gl_cursor_position(QemuConsole *con,
  1814. uint32_t pos_x, uint32_t pos_y)
  1815. {
  1816. DisplayState *s = con->ds;
  1817. DisplayChangeListener *dcl;
  1818. QLIST_FOREACH(dcl, &s->listeners, next) {
  1819. if (con != (dcl->con ? dcl->con : active_console)) {
  1820. continue;
  1821. }
  1822. if (dcl->ops->dpy_gl_cursor_position) {
  1823. dcl->ops->dpy_gl_cursor_position(dcl, pos_x, pos_y);
  1824. }
  1825. }
  1826. }
  1827. void dpy_gl_release_dmabuf(QemuConsole *con,
  1828. QemuDmaBuf *dmabuf)
  1829. {
  1830. DisplayState *s = con->ds;
  1831. DisplayChangeListener *dcl;
  1832. QLIST_FOREACH(dcl, &s->listeners, next) {
  1833. if (con != (dcl->con ? dcl->con : active_console)) {
  1834. continue;
  1835. }
  1836. if (dcl->ops->dpy_gl_release_dmabuf) {
  1837. dcl->ops->dpy_gl_release_dmabuf(dcl, dmabuf);
  1838. }
  1839. }
  1840. }
  1841. void dpy_gl_update(QemuConsole *con,
  1842. uint32_t x, uint32_t y, uint32_t w, uint32_t h)
  1843. {
  1844. DisplayState *s = con->ds;
  1845. DisplayChangeListener *dcl;
  1846. assert(con->gl);
  1847. graphic_hw_gl_block(con, true);
  1848. QLIST_FOREACH(dcl, &s->listeners, next) {
  1849. if (con != (dcl->con ? dcl->con : active_console)) {
  1850. continue;
  1851. }
  1852. if (dcl->ops->dpy_gl_update) {
  1853. dcl->ops->dpy_gl_update(dcl, x, y, w, h);
  1854. }
  1855. }
  1856. graphic_hw_gl_block(con, false);
  1857. }
  1858. /***********************************************************/
  1859. /* register display */
  1860. /* console.c internal use only */
  1861. static DisplayState *get_alloc_displaystate(void)
  1862. {
  1863. if (!display_state) {
  1864. display_state = g_new0(DisplayState, 1);
  1865. cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
  1866. text_console_update_cursor, NULL);
  1867. }
  1868. return display_state;
  1869. }
  1870. /*
  1871. * Called by main(), after creating QemuConsoles
  1872. * and before initializing ui (sdl/vnc/...).
  1873. */
  1874. DisplayState *init_displaystate(void)
  1875. {
  1876. gchar *name;
  1877. QemuConsole *con;
  1878. get_alloc_displaystate();
  1879. QTAILQ_FOREACH(con, &consoles, next) {
  1880. if (con->console_type != GRAPHIC_CONSOLE &&
  1881. con->ds == NULL) {
  1882. text_console_do_init(con->chr, display_state);
  1883. }
  1884. /* Hook up into the qom tree here (not in new_console()), once
  1885. * all QemuConsoles are created and the order / numbering
  1886. * doesn't change any more */
  1887. name = g_strdup_printf("console[%d]", con->index);
  1888. object_property_add_child(container_get(object_get_root(), "/backend"),
  1889. name, OBJECT(con));
  1890. g_free(name);
  1891. }
  1892. return display_state;
  1893. }
  1894. void graphic_console_set_hwops(QemuConsole *con,
  1895. const GraphicHwOps *hw_ops,
  1896. void *opaque)
  1897. {
  1898. con->hw_ops = hw_ops;
  1899. con->hw = opaque;
  1900. }
  1901. QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
  1902. const GraphicHwOps *hw_ops,
  1903. void *opaque)
  1904. {
  1905. static const char noinit[] =
  1906. "Guest has not initialized the display (yet).";
  1907. int width = 640;
  1908. int height = 480;
  1909. QemuConsole *s;
  1910. DisplayState *ds;
  1911. DisplaySurface *surface;
  1912. ds = get_alloc_displaystate();
  1913. s = qemu_console_lookup_unused();
  1914. if (s) {
  1915. trace_console_gfx_reuse(s->index);
  1916. width = qemu_console_get_width(s, 0);
  1917. height = qemu_console_get_height(s, 0);
  1918. } else {
  1919. trace_console_gfx_new();
  1920. s = new_console(ds, GRAPHIC_CONSOLE, head);
  1921. s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
  1922. dpy_set_ui_info_timer, s);
  1923. }
  1924. graphic_console_set_hwops(s, hw_ops, opaque);
  1925. if (dev) {
  1926. object_property_set_link(OBJECT(s), "device", OBJECT(dev),
  1927. &error_abort);
  1928. }
  1929. surface = qemu_create_placeholder_surface(width, height, noinit);
  1930. dpy_gfx_replace_surface(s, surface);
  1931. s->gl_unblock_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
  1932. graphic_hw_gl_unblock_timer, s);
  1933. return s;
  1934. }
  1935. static const GraphicHwOps unused_ops = {
  1936. /* no callbacks */
  1937. };
  1938. void graphic_console_close(QemuConsole *con)
  1939. {
  1940. static const char unplugged[] =
  1941. "Guest display has been unplugged";
  1942. DisplaySurface *surface;
  1943. int width = qemu_console_get_width(con, 640);
  1944. int height = qemu_console_get_height(con, 480);
  1945. trace_console_gfx_close(con->index);
  1946. object_property_set_link(OBJECT(con), "device", NULL, &error_abort);
  1947. graphic_console_set_hwops(con, &unused_ops, NULL);
  1948. if (con->gl) {
  1949. dpy_gl_scanout_disable(con);
  1950. }
  1951. surface = qemu_create_placeholder_surface(width, height, unplugged);
  1952. dpy_gfx_replace_surface(con, surface);
  1953. }
  1954. QemuConsole *qemu_console_lookup_first_graphic_console(void)
  1955. {
  1956. QemuConsole *con;
  1957. QTAILQ_FOREACH(con, &consoles, next) {
  1958. if (con->console_type == GRAPHIC_CONSOLE) {
  1959. return con;
  1960. }
  1961. }
  1962. return NULL;
  1963. }
  1964. QemuConsole *qemu_console_lookup_by_index(unsigned int index)
  1965. {
  1966. QemuConsole *con;
  1967. QTAILQ_FOREACH(con, &consoles, next) {
  1968. if (con->index == index) {
  1969. return con;
  1970. }
  1971. }
  1972. return NULL;
  1973. }
  1974. QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
  1975. {
  1976. QemuConsole *con;
  1977. Object *obj;
  1978. uint32_t h;
  1979. QTAILQ_FOREACH(con, &consoles, next) {
  1980. obj = object_property_get_link(OBJECT(con),
  1981. "device", &error_abort);
  1982. if (DEVICE(obj) != dev) {
  1983. continue;
  1984. }
  1985. h = object_property_get_uint(OBJECT(con),
  1986. "head", &error_abort);
  1987. if (h != head) {
  1988. continue;
  1989. }
  1990. return con;
  1991. }
  1992. return NULL;
  1993. }
  1994. QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
  1995. uint32_t head, Error **errp)
  1996. {
  1997. DeviceState *dev;
  1998. QemuConsole *con;
  1999. dev = qdev_find_recursive(sysbus_get_default(), device_id);
  2000. if (dev == NULL) {
  2001. error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
  2002. "Device '%s' not found", device_id);
  2003. return NULL;
  2004. }
  2005. con = qemu_console_lookup_by_device(dev, head);
  2006. if (con == NULL) {
  2007. error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
  2008. device_id, head);
  2009. return NULL;
  2010. }
  2011. return con;
  2012. }
  2013. QemuConsole *qemu_console_lookup_unused(void)
  2014. {
  2015. QemuConsole *con;
  2016. Object *obj;
  2017. QTAILQ_FOREACH(con, &consoles, next) {
  2018. if (con->hw_ops != &unused_ops) {
  2019. continue;
  2020. }
  2021. obj = object_property_get_link(OBJECT(con),
  2022. "device", &error_abort);
  2023. if (obj != NULL) {
  2024. continue;
  2025. }
  2026. return con;
  2027. }
  2028. return NULL;
  2029. }
  2030. QEMUCursor *qemu_console_get_cursor(QemuConsole *con)
  2031. {
  2032. if (con == NULL) {
  2033. con = active_console;
  2034. }
  2035. return con->cursor;
  2036. }
  2037. bool qemu_console_is_visible(QemuConsole *con)
  2038. {
  2039. return (con == active_console) || (con->dcls > 0);
  2040. }
  2041. bool qemu_console_is_graphic(QemuConsole *con)
  2042. {
  2043. if (con == NULL) {
  2044. con = active_console;
  2045. }
  2046. return con && (con->console_type == GRAPHIC_CONSOLE);
  2047. }
  2048. bool qemu_console_is_fixedsize(QemuConsole *con)
  2049. {
  2050. if (con == NULL) {
  2051. con = active_console;
  2052. }
  2053. return con && (con->console_type != TEXT_CONSOLE);
  2054. }
  2055. bool qemu_console_is_gl_blocked(QemuConsole *con)
  2056. {
  2057. assert(con != NULL);
  2058. return con->gl_block;
  2059. }
  2060. bool qemu_console_is_multihead(DeviceState *dev)
  2061. {
  2062. QemuConsole *con;
  2063. Object *obj;
  2064. uint32_t f = 0xffffffff;
  2065. uint32_t h;
  2066. QTAILQ_FOREACH(con, &consoles, next) {
  2067. obj = object_property_get_link(OBJECT(con),
  2068. "device", &error_abort);
  2069. if (DEVICE(obj) != dev) {
  2070. continue;
  2071. }
  2072. h = object_property_get_uint(OBJECT(con),
  2073. "head", &error_abort);
  2074. if (f == 0xffffffff) {
  2075. f = h;
  2076. } else if (h != f) {
  2077. return true;
  2078. }
  2079. }
  2080. return false;
  2081. }
  2082. char *qemu_console_get_label(QemuConsole *con)
  2083. {
  2084. if (con->console_type == GRAPHIC_CONSOLE) {
  2085. if (con->device) {
  2086. DeviceState *dev;
  2087. bool multihead;
  2088. dev = DEVICE(con->device);
  2089. multihead = qemu_console_is_multihead(dev);
  2090. if (multihead) {
  2091. return g_strdup_printf("%s.%d", dev->id ?
  2092. dev->id :
  2093. object_get_typename(con->device),
  2094. con->head);
  2095. } else {
  2096. return g_strdup_printf("%s", dev->id ?
  2097. dev->id :
  2098. object_get_typename(con->device));
  2099. }
  2100. }
  2101. return g_strdup("VGA");
  2102. } else {
  2103. if (con->chr && con->chr->label) {
  2104. return g_strdup(con->chr->label);
  2105. }
  2106. return g_strdup_printf("vc%d", con->index);
  2107. }
  2108. }
  2109. int qemu_console_get_index(QemuConsole *con)
  2110. {
  2111. if (con == NULL) {
  2112. con = active_console;
  2113. }
  2114. return con ? con->index : -1;
  2115. }
  2116. uint32_t qemu_console_get_head(QemuConsole *con)
  2117. {
  2118. if (con == NULL) {
  2119. con = active_console;
  2120. }
  2121. return con ? con->head : -1;
  2122. }
  2123. int qemu_console_get_width(QemuConsole *con, int fallback)
  2124. {
  2125. if (con == NULL) {
  2126. con = active_console;
  2127. }
  2128. if (con == NULL) {
  2129. return fallback;
  2130. }
  2131. switch (con->scanout.kind) {
  2132. case SCANOUT_DMABUF:
  2133. return con->scanout.dmabuf->width;
  2134. case SCANOUT_TEXTURE:
  2135. return con->scanout.texture.width;
  2136. case SCANOUT_SURFACE:
  2137. return surface_width(con->surface);
  2138. default:
  2139. return fallback;
  2140. }
  2141. }
  2142. int qemu_console_get_height(QemuConsole *con, int fallback)
  2143. {
  2144. if (con == NULL) {
  2145. con = active_console;
  2146. }
  2147. if (con == NULL) {
  2148. return fallback;
  2149. }
  2150. switch (con->scanout.kind) {
  2151. case SCANOUT_DMABUF:
  2152. return con->scanout.dmabuf->height;
  2153. case SCANOUT_TEXTURE:
  2154. return con->scanout.texture.height;
  2155. case SCANOUT_SURFACE:
  2156. return surface_height(con->surface);
  2157. default:
  2158. return fallback;
  2159. }
  2160. }
  2161. static void vc_chr_accept_input(Chardev *chr)
  2162. {
  2163. VCChardev *drv = VC_CHARDEV(chr);
  2164. QemuConsole *s = drv->console;
  2165. kbd_send_chars(s);
  2166. }
  2167. static void vc_chr_set_echo(Chardev *chr, bool echo)
  2168. {
  2169. VCChardev *drv = VC_CHARDEV(chr);
  2170. QemuConsole *s = drv->console;
  2171. s->echo = echo;
  2172. }
  2173. static void text_console_update_cursor_timer(void)
  2174. {
  2175. timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
  2176. + CONSOLE_CURSOR_PERIOD / 2);
  2177. }
  2178. static void text_console_update_cursor(void *opaque)
  2179. {
  2180. QemuConsole *s;
  2181. int count = 0;
  2182. cursor_visible_phase = !cursor_visible_phase;
  2183. QTAILQ_FOREACH(s, &consoles, next) {
  2184. if (qemu_console_is_graphic(s) ||
  2185. !qemu_console_is_visible(s)) {
  2186. continue;
  2187. }
  2188. count++;
  2189. graphic_hw_invalidate(s);
  2190. }
  2191. if (count) {
  2192. text_console_update_cursor_timer();
  2193. }
  2194. }
  2195. static const GraphicHwOps text_console_ops = {
  2196. .invalidate = text_console_invalidate,
  2197. .text_update = text_console_update,
  2198. };
  2199. static void text_console_do_init(Chardev *chr, DisplayState *ds)
  2200. {
  2201. VCChardev *drv = VC_CHARDEV(chr);
  2202. QemuConsole *s = drv->console;
  2203. int g_width = 80 * FONT_WIDTH;
  2204. int g_height = 24 * FONT_HEIGHT;
  2205. fifo8_create(&s->out_fifo, 16);
  2206. s->ds = ds;
  2207. s->y_displayed = 0;
  2208. s->y_base = 0;
  2209. s->total_height = DEFAULT_BACKSCROLL;
  2210. s->x = 0;
  2211. s->y = 0;
  2212. if (s->scanout.kind != SCANOUT_SURFACE) {
  2213. if (active_console && active_console->scanout.kind == SCANOUT_SURFACE) {
  2214. g_width = qemu_console_get_width(active_console, g_width);
  2215. g_height = qemu_console_get_height(active_console, g_height);
  2216. }
  2217. s->surface = qemu_create_displaysurface(g_width, g_height);
  2218. s->scanout.kind = SCANOUT_SURFACE;
  2219. }
  2220. s->hw_ops = &text_console_ops;
  2221. s->hw = s;
  2222. /* Set text attribute defaults */
  2223. s->t_attrib_default.bold = 0;
  2224. s->t_attrib_default.uline = 0;
  2225. s->t_attrib_default.blink = 0;
  2226. s->t_attrib_default.invers = 0;
  2227. s->t_attrib_default.unvisible = 0;
  2228. s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
  2229. s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
  2230. /* set current text attributes to default */
  2231. s->t_attrib = s->t_attrib_default;
  2232. text_console_resize(s);
  2233. if (chr->label) {
  2234. char *msg;
  2235. s->t_attrib.bgcol = QEMU_COLOR_BLUE;
  2236. msg = g_strdup_printf("%s console\r\n", chr->label);
  2237. vc_chr_write(chr, (uint8_t *)msg, strlen(msg));
  2238. g_free(msg);
  2239. s->t_attrib = s->t_attrib_default;
  2240. }
  2241. qemu_chr_be_event(chr, CHR_EVENT_OPENED);
  2242. }
  2243. static void vc_chr_open(Chardev *chr,
  2244. ChardevBackend *backend,
  2245. bool *be_opened,
  2246. Error **errp)
  2247. {
  2248. ChardevVC *vc = backend->u.vc.data;
  2249. VCChardev *drv = VC_CHARDEV(chr);
  2250. QemuConsole *s;
  2251. unsigned width = 0;
  2252. unsigned height = 0;
  2253. if (vc->has_width) {
  2254. width = vc->width;
  2255. } else if (vc->has_cols) {
  2256. width = vc->cols * FONT_WIDTH;
  2257. }
  2258. if (vc->has_height) {
  2259. height = vc->height;
  2260. } else if (vc->has_rows) {
  2261. height = vc->rows * FONT_HEIGHT;
  2262. }
  2263. trace_console_txt_new(width, height);
  2264. if (width == 0 || height == 0) {
  2265. s = new_console(NULL, TEXT_CONSOLE, 0);
  2266. } else {
  2267. s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
  2268. s->scanout.kind = SCANOUT_SURFACE;
  2269. s->surface = qemu_create_displaysurface(width, height);
  2270. }
  2271. if (!s) {
  2272. error_setg(errp, "cannot create text console");
  2273. return;
  2274. }
  2275. s->chr = chr;
  2276. drv->console = s;
  2277. if (display_state) {
  2278. text_console_do_init(chr, display_state);
  2279. }
  2280. /* console/chardev init sometimes completes elsewhere in a 2nd
  2281. * stage, so defer OPENED events until they are fully initialized
  2282. */
  2283. *be_opened = false;
  2284. }
  2285. void qemu_console_resize(QemuConsole *s, int width, int height)
  2286. {
  2287. DisplaySurface *surface = qemu_console_surface(s);
  2288. assert(s->console_type == GRAPHIC_CONSOLE);
  2289. if ((s->scanout.kind != SCANOUT_SURFACE ||
  2290. (surface && surface->flags & QEMU_ALLOCATED_FLAG)) &&
  2291. qemu_console_get_width(s, -1) == width &&
  2292. qemu_console_get_height(s, -1) == height) {
  2293. return;
  2294. }
  2295. surface = qemu_create_displaysurface(width, height);
  2296. dpy_gfx_replace_surface(s, surface);
  2297. }
  2298. DisplaySurface *qemu_console_surface(QemuConsole *console)
  2299. {
  2300. return console->surface;
  2301. }
  2302. PixelFormat qemu_default_pixelformat(int bpp)
  2303. {
  2304. pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
  2305. PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
  2306. return pf;
  2307. }
  2308. static QemuDisplay *dpys[DISPLAY_TYPE__MAX];
  2309. void qemu_display_register(QemuDisplay *ui)
  2310. {
  2311. assert(ui->type < DISPLAY_TYPE__MAX);
  2312. dpys[ui->type] = ui;
  2313. }
  2314. bool qemu_display_find_default(DisplayOptions *opts)
  2315. {
  2316. static DisplayType prio[] = {
  2317. #if defined(CONFIG_GTK)
  2318. DISPLAY_TYPE_GTK,
  2319. #endif
  2320. #if defined(CONFIG_SDL)
  2321. DISPLAY_TYPE_SDL,
  2322. #endif
  2323. #if defined(CONFIG_COCOA)
  2324. DISPLAY_TYPE_COCOA
  2325. #endif
  2326. };
  2327. int i;
  2328. for (i = 0; i < (int)ARRAY_SIZE(prio); i++) {
  2329. if (dpys[prio[i]] == NULL) {
  2330. Error *local_err = NULL;
  2331. int rv = ui_module_load(DisplayType_str(prio[i]), &local_err);
  2332. if (rv < 0) {
  2333. error_report_err(local_err);
  2334. }
  2335. }
  2336. if (dpys[prio[i]] == NULL) {
  2337. continue;
  2338. }
  2339. opts->type = prio[i];
  2340. return true;
  2341. }
  2342. return false;
  2343. }
  2344. void qemu_display_early_init(DisplayOptions *opts)
  2345. {
  2346. assert(opts->type < DISPLAY_TYPE__MAX);
  2347. if (opts->type == DISPLAY_TYPE_NONE) {
  2348. return;
  2349. }
  2350. if (dpys[opts->type] == NULL) {
  2351. Error *local_err = NULL;
  2352. int rv = ui_module_load(DisplayType_str(opts->type), &local_err);
  2353. if (rv < 0) {
  2354. error_report_err(local_err);
  2355. }
  2356. }
  2357. if (dpys[opts->type] == NULL) {
  2358. error_report("Display '%s' is not available.",
  2359. DisplayType_str(opts->type));
  2360. exit(1);
  2361. }
  2362. if (dpys[opts->type]->early_init) {
  2363. dpys[opts->type]->early_init(opts);
  2364. }
  2365. }
  2366. void qemu_display_init(DisplayState *ds, DisplayOptions *opts)
  2367. {
  2368. assert(opts->type < DISPLAY_TYPE__MAX);
  2369. if (opts->type == DISPLAY_TYPE_NONE) {
  2370. return;
  2371. }
  2372. assert(dpys[opts->type] != NULL);
  2373. dpys[opts->type]->init(ds, opts);
  2374. }
  2375. void qemu_display_help(void)
  2376. {
  2377. int idx;
  2378. printf("Available display backend types:\n");
  2379. printf("none\n");
  2380. for (idx = DISPLAY_TYPE_NONE; idx < DISPLAY_TYPE__MAX; idx++) {
  2381. if (!dpys[idx]) {
  2382. Error *local_err = NULL;
  2383. int rv = ui_module_load(DisplayType_str(idx), &local_err);
  2384. if (rv < 0) {
  2385. error_report_err(local_err);
  2386. }
  2387. }
  2388. if (dpys[idx]) {
  2389. printf("%s\n", DisplayType_str(dpys[idx]->type));
  2390. }
  2391. }
  2392. }
  2393. void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp)
  2394. {
  2395. int val;
  2396. ChardevVC *vc;
  2397. backend->type = CHARDEV_BACKEND_KIND_VC;
  2398. vc = backend->u.vc.data = g_new0(ChardevVC, 1);
  2399. qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
  2400. val = qemu_opt_get_number(opts, "width", 0);
  2401. if (val != 0) {
  2402. vc->has_width = true;
  2403. vc->width = val;
  2404. }
  2405. val = qemu_opt_get_number(opts, "height", 0);
  2406. if (val != 0) {
  2407. vc->has_height = true;
  2408. vc->height = val;
  2409. }
  2410. val = qemu_opt_get_number(opts, "cols", 0);
  2411. if (val != 0) {
  2412. vc->has_cols = true;
  2413. vc->cols = val;
  2414. }
  2415. val = qemu_opt_get_number(opts, "rows", 0);
  2416. if (val != 0) {
  2417. vc->has_rows = true;
  2418. vc->rows = val;
  2419. }
  2420. }
  2421. static const TypeInfo qemu_console_info = {
  2422. .name = TYPE_QEMU_CONSOLE,
  2423. .parent = TYPE_OBJECT,
  2424. .instance_size = sizeof(QemuConsole),
  2425. .class_size = sizeof(QemuConsoleClass),
  2426. };
  2427. static void char_vc_class_init(ObjectClass *oc, void *data)
  2428. {
  2429. ChardevClass *cc = CHARDEV_CLASS(oc);
  2430. cc->parse = qemu_chr_parse_vc;
  2431. cc->open = vc_chr_open;
  2432. cc->chr_write = vc_chr_write;
  2433. cc->chr_accept_input = vc_chr_accept_input;
  2434. cc->chr_set_echo = vc_chr_set_echo;
  2435. }
  2436. static const TypeInfo char_vc_type_info = {
  2437. .name = TYPE_CHARDEV_VC,
  2438. .parent = TYPE_CHARDEV,
  2439. .instance_size = sizeof(VCChardev),
  2440. .class_init = char_vc_class_init,
  2441. };
  2442. void qemu_console_early_init(void)
  2443. {
  2444. /* set the default vc driver */
  2445. if (!object_class_by_name(TYPE_CHARDEV_VC)) {
  2446. type_register(&char_vc_type_info);
  2447. }
  2448. }
  2449. static void register_types(void)
  2450. {
  2451. type_register_static(&qemu_console_info);
  2452. }
  2453. type_init(register_types);