console.c 61 KB

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