console.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  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 "chardev/char-fe.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. assert(!dcl->ds);
  1278. if (dcl->ops->dpy_gl_ctx_create) {
  1279. /* display has opengl support */
  1280. assert(dcl->con);
  1281. if (dcl->con->gl) {
  1282. fprintf(stderr, "can't register two opengl displays (%s, %s)\n",
  1283. dcl->ops->dpy_name, dcl->con->gl->ops->dpy_name);
  1284. exit(1);
  1285. }
  1286. dcl->con->gl = dcl;
  1287. }
  1288. trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
  1289. dcl->ds = get_alloc_displaystate();
  1290. QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
  1291. gui_setup_refresh(dcl->ds);
  1292. if (dcl->con) {
  1293. dcl->con->dcls++;
  1294. con = dcl->con;
  1295. } else {
  1296. con = active_console;
  1297. }
  1298. if (dcl->ops->dpy_gfx_switch) {
  1299. if (con) {
  1300. dcl->ops->dpy_gfx_switch(dcl, con->surface);
  1301. } else {
  1302. if (!dummy) {
  1303. dummy = qemu_create_message_surface(640, 480, nodev);
  1304. }
  1305. dcl->ops->dpy_gfx_switch(dcl, dummy);
  1306. }
  1307. }
  1308. text_console_update_cursor(NULL);
  1309. }
  1310. void update_displaychangelistener(DisplayChangeListener *dcl,
  1311. uint64_t interval)
  1312. {
  1313. DisplayState *ds = dcl->ds;
  1314. dcl->update_interval = interval;
  1315. if (!ds->refreshing && ds->update_interval > interval) {
  1316. timer_mod(ds->gui_timer, ds->last_update + interval);
  1317. }
  1318. }
  1319. void unregister_displaychangelistener(DisplayChangeListener *dcl)
  1320. {
  1321. DisplayState *ds = dcl->ds;
  1322. trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
  1323. if (dcl->con) {
  1324. dcl->con->dcls--;
  1325. }
  1326. QLIST_REMOVE(dcl, next);
  1327. gui_setup_refresh(ds);
  1328. }
  1329. static void dpy_set_ui_info_timer(void *opaque)
  1330. {
  1331. QemuConsole *con = opaque;
  1332. con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
  1333. }
  1334. bool dpy_ui_info_supported(QemuConsole *con)
  1335. {
  1336. return con->hw_ops->ui_info != NULL;
  1337. }
  1338. int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info)
  1339. {
  1340. assert(con != NULL);
  1341. if (!dpy_ui_info_supported(con)) {
  1342. return -1;
  1343. }
  1344. if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
  1345. /* nothing changed -- ignore */
  1346. return 0;
  1347. }
  1348. /*
  1349. * Typically we get a flood of these as the user resizes the window.
  1350. * Wait until the dust has settled (one second without updates), then
  1351. * go notify the guest.
  1352. */
  1353. con->ui_info = *info;
  1354. timer_mod(con->ui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
  1355. return 0;
  1356. }
  1357. void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
  1358. {
  1359. DisplayState *s = con->ds;
  1360. DisplayChangeListener *dcl;
  1361. int width = w;
  1362. int height = h;
  1363. if (con->surface) {
  1364. width = surface_width(con->surface);
  1365. height = surface_height(con->surface);
  1366. }
  1367. x = MAX(x, 0);
  1368. y = MAX(y, 0);
  1369. x = MIN(x, width);
  1370. y = MIN(y, height);
  1371. w = MIN(w, width - x);
  1372. h = MIN(h, height - y);
  1373. if (!qemu_console_is_visible(con)) {
  1374. return;
  1375. }
  1376. QLIST_FOREACH(dcl, &s->listeners, next) {
  1377. if (con != (dcl->con ? dcl->con : active_console)) {
  1378. continue;
  1379. }
  1380. if (dcl->ops->dpy_gfx_update) {
  1381. dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
  1382. }
  1383. }
  1384. }
  1385. void dpy_gfx_replace_surface(QemuConsole *con,
  1386. DisplaySurface *surface)
  1387. {
  1388. DisplayState *s = con->ds;
  1389. DisplaySurface *old_surface = con->surface;
  1390. DisplayChangeListener *dcl;
  1391. assert(old_surface != surface);
  1392. con->surface = surface;
  1393. QLIST_FOREACH(dcl, &s->listeners, next) {
  1394. if (con != (dcl->con ? dcl->con : active_console)) {
  1395. continue;
  1396. }
  1397. if (dcl->ops->dpy_gfx_switch) {
  1398. dcl->ops->dpy_gfx_switch(dcl, surface);
  1399. }
  1400. }
  1401. qemu_free_displaysurface(old_surface);
  1402. }
  1403. bool dpy_gfx_check_format(QemuConsole *con,
  1404. pixman_format_code_t format)
  1405. {
  1406. DisplayChangeListener *dcl;
  1407. DisplayState *s = con->ds;
  1408. QLIST_FOREACH(dcl, &s->listeners, next) {
  1409. if (dcl->con && dcl->con != con) {
  1410. /* dcl bound to another console -> skip */
  1411. continue;
  1412. }
  1413. if (dcl->ops->dpy_gfx_check_format) {
  1414. if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
  1415. return false;
  1416. }
  1417. } else {
  1418. /* default is to whitelist native 32 bpp only */
  1419. if (format != qemu_default_pixman_format(32, true)) {
  1420. return false;
  1421. }
  1422. }
  1423. }
  1424. return true;
  1425. }
  1426. /*
  1427. * Safe DPY refresh for TCG guests. We use the exclusive mechanism to
  1428. * ensure the TCG vCPUs are quiescent so we can avoid races between
  1429. * dirty page tracking for direct frame-buffer access by the guest.
  1430. *
  1431. * This is a temporary stopgap until we've fixed the dirty tracking
  1432. * races in display adapters.
  1433. */
  1434. static void do_safe_dpy_refresh(DisplayChangeListener *dcl)
  1435. {
  1436. qemu_mutex_unlock_iothread();
  1437. start_exclusive();
  1438. qemu_mutex_lock_iothread();
  1439. dcl->ops->dpy_refresh(dcl);
  1440. qemu_mutex_unlock_iothread();
  1441. end_exclusive();
  1442. qemu_mutex_lock_iothread();
  1443. }
  1444. static void dpy_refresh(DisplayState *s)
  1445. {
  1446. DisplayChangeListener *dcl;
  1447. QLIST_FOREACH(dcl, &s->listeners, next) {
  1448. if (dcl->ops->dpy_refresh) {
  1449. if (tcg_enabled()) {
  1450. do_safe_dpy_refresh(dcl);
  1451. } else {
  1452. dcl->ops->dpy_refresh(dcl);
  1453. }
  1454. }
  1455. }
  1456. }
  1457. void dpy_text_cursor(QemuConsole *con, int x, int y)
  1458. {
  1459. DisplayState *s = con->ds;
  1460. DisplayChangeListener *dcl;
  1461. if (!qemu_console_is_visible(con)) {
  1462. return;
  1463. }
  1464. QLIST_FOREACH(dcl, &s->listeners, next) {
  1465. if (con != (dcl->con ? dcl->con : active_console)) {
  1466. continue;
  1467. }
  1468. if (dcl->ops->dpy_text_cursor) {
  1469. dcl->ops->dpy_text_cursor(dcl, x, y);
  1470. }
  1471. }
  1472. }
  1473. void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
  1474. {
  1475. DisplayState *s = con->ds;
  1476. DisplayChangeListener *dcl;
  1477. if (!qemu_console_is_visible(con)) {
  1478. return;
  1479. }
  1480. QLIST_FOREACH(dcl, &s->listeners, next) {
  1481. if (con != (dcl->con ? dcl->con : active_console)) {
  1482. continue;
  1483. }
  1484. if (dcl->ops->dpy_text_update) {
  1485. dcl->ops->dpy_text_update(dcl, x, y, w, h);
  1486. }
  1487. }
  1488. }
  1489. void dpy_text_resize(QemuConsole *con, int w, int h)
  1490. {
  1491. DisplayState *s = con->ds;
  1492. DisplayChangeListener *dcl;
  1493. if (!qemu_console_is_visible(con)) {
  1494. return;
  1495. }
  1496. QLIST_FOREACH(dcl, &s->listeners, next) {
  1497. if (con != (dcl->con ? dcl->con : active_console)) {
  1498. continue;
  1499. }
  1500. if (dcl->ops->dpy_text_resize) {
  1501. dcl->ops->dpy_text_resize(dcl, w, h);
  1502. }
  1503. }
  1504. }
  1505. void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
  1506. {
  1507. DisplayState *s = con->ds;
  1508. DisplayChangeListener *dcl;
  1509. if (!qemu_console_is_visible(con)) {
  1510. return;
  1511. }
  1512. QLIST_FOREACH(dcl, &s->listeners, next) {
  1513. if (con != (dcl->con ? dcl->con : active_console)) {
  1514. continue;
  1515. }
  1516. if (dcl->ops->dpy_mouse_set) {
  1517. dcl->ops->dpy_mouse_set(dcl, x, y, on);
  1518. }
  1519. }
  1520. }
  1521. void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
  1522. {
  1523. DisplayState *s = con->ds;
  1524. DisplayChangeListener *dcl;
  1525. if (!qemu_console_is_visible(con)) {
  1526. return;
  1527. }
  1528. QLIST_FOREACH(dcl, &s->listeners, next) {
  1529. if (con != (dcl->con ? dcl->con : active_console)) {
  1530. continue;
  1531. }
  1532. if (dcl->ops->dpy_cursor_define) {
  1533. dcl->ops->dpy_cursor_define(dcl, cursor);
  1534. }
  1535. }
  1536. }
  1537. bool dpy_cursor_define_supported(QemuConsole *con)
  1538. {
  1539. DisplayState *s = con->ds;
  1540. DisplayChangeListener *dcl;
  1541. QLIST_FOREACH(dcl, &s->listeners, next) {
  1542. if (dcl->ops->dpy_cursor_define) {
  1543. return true;
  1544. }
  1545. }
  1546. return false;
  1547. }
  1548. QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
  1549. struct QEMUGLParams *qparams)
  1550. {
  1551. assert(con->gl);
  1552. return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
  1553. }
  1554. void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
  1555. {
  1556. assert(con->gl);
  1557. con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
  1558. }
  1559. int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
  1560. {
  1561. assert(con->gl);
  1562. return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
  1563. }
  1564. QEMUGLContext dpy_gl_ctx_get_current(QemuConsole *con)
  1565. {
  1566. assert(con->gl);
  1567. return con->gl->ops->dpy_gl_ctx_get_current(con->gl);
  1568. }
  1569. void dpy_gl_scanout_disable(QemuConsole *con)
  1570. {
  1571. assert(con->gl);
  1572. if (con->gl->ops->dpy_gl_scanout_disable) {
  1573. con->gl->ops->dpy_gl_scanout_disable(con->gl);
  1574. } else {
  1575. con->gl->ops->dpy_gl_scanout_texture(con->gl, 0, false, 0, 0,
  1576. 0, 0, 0, 0);
  1577. }
  1578. }
  1579. void dpy_gl_scanout_texture(QemuConsole *con,
  1580. uint32_t backing_id,
  1581. bool backing_y_0_top,
  1582. uint32_t backing_width,
  1583. uint32_t backing_height,
  1584. uint32_t x, uint32_t y,
  1585. uint32_t width, uint32_t height)
  1586. {
  1587. assert(con->gl);
  1588. con->gl->ops->dpy_gl_scanout_texture(con->gl, backing_id,
  1589. backing_y_0_top,
  1590. backing_width, backing_height,
  1591. x, y, width, height);
  1592. }
  1593. void dpy_gl_update(QemuConsole *con,
  1594. uint32_t x, uint32_t y, uint32_t w, uint32_t h)
  1595. {
  1596. assert(con->gl);
  1597. con->gl->ops->dpy_gl_update(con->gl, x, y, w, h);
  1598. }
  1599. /***********************************************************/
  1600. /* register display */
  1601. /* console.c internal use only */
  1602. static DisplayState *get_alloc_displaystate(void)
  1603. {
  1604. if (!display_state) {
  1605. display_state = g_new0(DisplayState, 1);
  1606. cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
  1607. text_console_update_cursor, NULL);
  1608. }
  1609. return display_state;
  1610. }
  1611. /*
  1612. * Called by main(), after creating QemuConsoles
  1613. * and before initializing ui (sdl/vnc/...).
  1614. */
  1615. DisplayState *init_displaystate(void)
  1616. {
  1617. gchar *name;
  1618. int i;
  1619. get_alloc_displaystate();
  1620. for (i = 0; i < nb_consoles; i++) {
  1621. if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
  1622. consoles[i]->ds == NULL) {
  1623. text_console_do_init(consoles[i]->chr, display_state);
  1624. }
  1625. /* Hook up into the qom tree here (not in new_console()), once
  1626. * all QemuConsoles are created and the order / numbering
  1627. * doesn't change any more */
  1628. name = g_strdup_printf("console[%d]", i);
  1629. object_property_add_child(container_get(object_get_root(), "/backend"),
  1630. name, OBJECT(consoles[i]), &error_abort);
  1631. g_free(name);
  1632. }
  1633. return display_state;
  1634. }
  1635. void graphic_console_set_hwops(QemuConsole *con,
  1636. const GraphicHwOps *hw_ops,
  1637. void *opaque)
  1638. {
  1639. con->hw_ops = hw_ops;
  1640. con->hw = opaque;
  1641. }
  1642. QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
  1643. const GraphicHwOps *hw_ops,
  1644. void *opaque)
  1645. {
  1646. static const char noinit[] =
  1647. "Guest has not initialized the display (yet).";
  1648. int width = 640;
  1649. int height = 480;
  1650. QemuConsole *s;
  1651. DisplayState *ds;
  1652. ds = get_alloc_displaystate();
  1653. trace_console_gfx_new();
  1654. s = new_console(ds, GRAPHIC_CONSOLE, head);
  1655. s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, dpy_set_ui_info_timer, s);
  1656. graphic_console_set_hwops(s, hw_ops, opaque);
  1657. if (dev) {
  1658. object_property_set_link(OBJECT(s), OBJECT(dev), "device",
  1659. &error_abort);
  1660. }
  1661. s->surface = qemu_create_message_surface(width, height, noinit);
  1662. return s;
  1663. }
  1664. QemuConsole *qemu_console_lookup_by_index(unsigned int index)
  1665. {
  1666. if (index >= nb_consoles) {
  1667. return NULL;
  1668. }
  1669. return consoles[index];
  1670. }
  1671. QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
  1672. {
  1673. Object *obj;
  1674. uint32_t h;
  1675. int i;
  1676. for (i = 0; i < nb_consoles; i++) {
  1677. if (!consoles[i]) {
  1678. continue;
  1679. }
  1680. obj = object_property_get_link(OBJECT(consoles[i]),
  1681. "device", &error_abort);
  1682. if (DEVICE(obj) != dev) {
  1683. continue;
  1684. }
  1685. h = object_property_get_int(OBJECT(consoles[i]),
  1686. "head", &error_abort);
  1687. if (h != head) {
  1688. continue;
  1689. }
  1690. return consoles[i];
  1691. }
  1692. return NULL;
  1693. }
  1694. QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
  1695. uint32_t head, Error **errp)
  1696. {
  1697. DeviceState *dev;
  1698. QemuConsole *con;
  1699. dev = qdev_find_recursive(sysbus_get_default(), device_id);
  1700. if (dev == NULL) {
  1701. error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
  1702. "Device '%s' not found", device_id);
  1703. return NULL;
  1704. }
  1705. con = qemu_console_lookup_by_device(dev, head);
  1706. if (con == NULL) {
  1707. error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
  1708. device_id, head);
  1709. return NULL;
  1710. }
  1711. return con;
  1712. }
  1713. bool qemu_console_is_visible(QemuConsole *con)
  1714. {
  1715. return (con == active_console) || (con->dcls > 0);
  1716. }
  1717. bool qemu_console_is_graphic(QemuConsole *con)
  1718. {
  1719. if (con == NULL) {
  1720. con = active_console;
  1721. }
  1722. return con && (con->console_type == GRAPHIC_CONSOLE);
  1723. }
  1724. bool qemu_console_is_fixedsize(QemuConsole *con)
  1725. {
  1726. if (con == NULL) {
  1727. con = active_console;
  1728. }
  1729. return con && (con->console_type != TEXT_CONSOLE);
  1730. }
  1731. bool qemu_console_is_gl_blocked(QemuConsole *con)
  1732. {
  1733. assert(con != NULL);
  1734. return con->gl_block;
  1735. }
  1736. char *qemu_console_get_label(QemuConsole *con)
  1737. {
  1738. if (con->console_type == GRAPHIC_CONSOLE) {
  1739. if (con->device) {
  1740. return g_strdup(object_get_typename(con->device));
  1741. }
  1742. return g_strdup("VGA");
  1743. } else {
  1744. if (con->chr && con->chr->label) {
  1745. return g_strdup(con->chr->label);
  1746. }
  1747. return g_strdup_printf("vc%d", con->index);
  1748. }
  1749. }
  1750. int qemu_console_get_index(QemuConsole *con)
  1751. {
  1752. if (con == NULL) {
  1753. con = active_console;
  1754. }
  1755. return con ? con->index : -1;
  1756. }
  1757. uint32_t qemu_console_get_head(QemuConsole *con)
  1758. {
  1759. if (con == NULL) {
  1760. con = active_console;
  1761. }
  1762. return con ? con->head : -1;
  1763. }
  1764. QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con)
  1765. {
  1766. assert(con != NULL);
  1767. return &con->ui_info;
  1768. }
  1769. int qemu_console_get_width(QemuConsole *con, int fallback)
  1770. {
  1771. if (con == NULL) {
  1772. con = active_console;
  1773. }
  1774. return con ? surface_width(con->surface) : fallback;
  1775. }
  1776. int qemu_console_get_height(QemuConsole *con, int fallback)
  1777. {
  1778. if (con == NULL) {
  1779. con = active_console;
  1780. }
  1781. return con ? surface_height(con->surface) : fallback;
  1782. }
  1783. static void vc_chr_set_echo(Chardev *chr, bool echo)
  1784. {
  1785. VCChardev *drv = VC_CHARDEV(chr);
  1786. QemuConsole *s = drv->console;
  1787. s->echo = echo;
  1788. }
  1789. static void text_console_update_cursor_timer(void)
  1790. {
  1791. timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
  1792. + CONSOLE_CURSOR_PERIOD / 2);
  1793. }
  1794. static void text_console_update_cursor(void *opaque)
  1795. {
  1796. QemuConsole *s;
  1797. int i, count = 0;
  1798. cursor_visible_phase = !cursor_visible_phase;
  1799. for (i = 0; i < nb_consoles; i++) {
  1800. s = consoles[i];
  1801. if (qemu_console_is_graphic(s) ||
  1802. !qemu_console_is_visible(s)) {
  1803. continue;
  1804. }
  1805. count++;
  1806. graphic_hw_invalidate(s);
  1807. }
  1808. if (count) {
  1809. text_console_update_cursor_timer();
  1810. }
  1811. }
  1812. static const GraphicHwOps text_console_ops = {
  1813. .invalidate = text_console_invalidate,
  1814. .text_update = text_console_update,
  1815. };
  1816. static void text_console_do_init(Chardev *chr, DisplayState *ds)
  1817. {
  1818. VCChardev *drv = VC_CHARDEV(chr);
  1819. QemuConsole *s = drv->console;
  1820. int g_width = 80 * FONT_WIDTH;
  1821. int g_height = 24 * FONT_HEIGHT;
  1822. s->out_fifo.buf = s->out_fifo_buf;
  1823. s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
  1824. s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
  1825. s->ds = ds;
  1826. s->y_displayed = 0;
  1827. s->y_base = 0;
  1828. s->total_height = DEFAULT_BACKSCROLL;
  1829. s->x = 0;
  1830. s->y = 0;
  1831. if (!s->surface) {
  1832. if (active_console && active_console->surface) {
  1833. g_width = surface_width(active_console->surface);
  1834. g_height = surface_height(active_console->surface);
  1835. }
  1836. s->surface = qemu_create_displaysurface(g_width, g_height);
  1837. }
  1838. s->hw_ops = &text_console_ops;
  1839. s->hw = s;
  1840. /* Set text attribute defaults */
  1841. s->t_attrib_default.bold = 0;
  1842. s->t_attrib_default.uline = 0;
  1843. s->t_attrib_default.blink = 0;
  1844. s->t_attrib_default.invers = 0;
  1845. s->t_attrib_default.unvisible = 0;
  1846. s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
  1847. s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
  1848. /* set current text attributes to default */
  1849. s->t_attrib = s->t_attrib_default;
  1850. text_console_resize(s);
  1851. if (chr->label) {
  1852. char msg[128];
  1853. int len;
  1854. s->t_attrib.bgcol = QEMU_COLOR_BLUE;
  1855. len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
  1856. vc_chr_write(chr, (uint8_t *)msg, len);
  1857. s->t_attrib = s->t_attrib_default;
  1858. }
  1859. qemu_chr_be_event(chr, CHR_EVENT_OPENED);
  1860. }
  1861. static void vc_chr_open(Chardev *chr,
  1862. ChardevBackend *backend,
  1863. bool *be_opened,
  1864. Error **errp)
  1865. {
  1866. ChardevVC *vc = backend->u.vc.data;
  1867. VCChardev *drv = VC_CHARDEV(chr);
  1868. QemuConsole *s;
  1869. unsigned width = 0;
  1870. unsigned height = 0;
  1871. if (vc->has_width) {
  1872. width = vc->width;
  1873. } else if (vc->has_cols) {
  1874. width = vc->cols * FONT_WIDTH;
  1875. }
  1876. if (vc->has_height) {
  1877. height = vc->height;
  1878. } else if (vc->has_rows) {
  1879. height = vc->rows * FONT_HEIGHT;
  1880. }
  1881. trace_console_txt_new(width, height);
  1882. if (width == 0 || height == 0) {
  1883. s = new_console(NULL, TEXT_CONSOLE, 0);
  1884. } else {
  1885. s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
  1886. s->surface = qemu_create_displaysurface(width, height);
  1887. }
  1888. if (!s) {
  1889. error_setg(errp, "cannot create text console");
  1890. return;
  1891. }
  1892. s->chr = chr;
  1893. drv->console = s;
  1894. if (display_state) {
  1895. text_console_do_init(chr, display_state);
  1896. }
  1897. /* console/chardev init sometimes completes elsewhere in a 2nd
  1898. * stage, so defer OPENED events until they are fully initialized
  1899. */
  1900. *be_opened = false;
  1901. }
  1902. void qemu_console_resize(QemuConsole *s, int width, int height)
  1903. {
  1904. DisplaySurface *surface;
  1905. assert(s->console_type == GRAPHIC_CONSOLE);
  1906. if (s->surface && (s->surface->flags & QEMU_ALLOCATED_FLAG) &&
  1907. pixman_image_get_width(s->surface->image) == width &&
  1908. pixman_image_get_height(s->surface->image) == height) {
  1909. return;
  1910. }
  1911. surface = qemu_create_displaysurface(width, height);
  1912. dpy_gfx_replace_surface(s, surface);
  1913. }
  1914. DisplaySurface *qemu_console_surface(QemuConsole *console)
  1915. {
  1916. return console->surface;
  1917. }
  1918. PixelFormat qemu_default_pixelformat(int bpp)
  1919. {
  1920. pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
  1921. PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
  1922. return pf;
  1923. }
  1924. void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp)
  1925. {
  1926. int val;
  1927. ChardevVC *vc;
  1928. backend->type = CHARDEV_BACKEND_KIND_VC;
  1929. vc = backend->u.vc.data = g_new0(ChardevVC, 1);
  1930. qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
  1931. val = qemu_opt_get_number(opts, "width", 0);
  1932. if (val != 0) {
  1933. vc->has_width = true;
  1934. vc->width = val;
  1935. }
  1936. val = qemu_opt_get_number(opts, "height", 0);
  1937. if (val != 0) {
  1938. vc->has_height = true;
  1939. vc->height = val;
  1940. }
  1941. val = qemu_opt_get_number(opts, "cols", 0);
  1942. if (val != 0) {
  1943. vc->has_cols = true;
  1944. vc->cols = val;
  1945. }
  1946. val = qemu_opt_get_number(opts, "rows", 0);
  1947. if (val != 0) {
  1948. vc->has_rows = true;
  1949. vc->rows = val;
  1950. }
  1951. }
  1952. static const TypeInfo qemu_console_info = {
  1953. .name = TYPE_QEMU_CONSOLE,
  1954. .parent = TYPE_OBJECT,
  1955. .instance_size = sizeof(QemuConsole),
  1956. .class_size = sizeof(QemuConsoleClass),
  1957. };
  1958. static void char_vc_class_init(ObjectClass *oc, void *data)
  1959. {
  1960. ChardevClass *cc = CHARDEV_CLASS(oc);
  1961. cc->parse = qemu_chr_parse_vc;
  1962. cc->open = vc_chr_open;
  1963. cc->chr_write = vc_chr_write;
  1964. cc->chr_set_echo = vc_chr_set_echo;
  1965. }
  1966. static const TypeInfo char_vc_type_info = {
  1967. .name = TYPE_CHARDEV_VC,
  1968. .parent = TYPE_CHARDEV,
  1969. .instance_size = sizeof(VCChardev),
  1970. .class_init = char_vc_class_init,
  1971. };
  1972. void qemu_console_early_init(void)
  1973. {
  1974. /* set the default vc driver */
  1975. if (!object_class_by_name(TYPE_CHARDEV_VC)) {
  1976. type_register(&char_vc_type_info);
  1977. }
  1978. }
  1979. static void register_types(void)
  1980. {
  1981. type_register_static(&qemu_console_info);
  1982. }
  1983. type_init(register_types);