console.c 77 KB

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