console-vc.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. /*
  2. * SPDX-License-Identifier: MIT
  3. * QEMU VC
  4. */
  5. #include "qemu/osdep.h"
  6. #include "chardev/char.h"
  7. #include "qapi/error.h"
  8. #include "qemu/fifo8.h"
  9. #include "qemu/option.h"
  10. #include "ui/console.h"
  11. #include "trace.h"
  12. #include "console-priv.h"
  13. #define DEFAULT_BACKSCROLL 512
  14. #define CONSOLE_CURSOR_PERIOD 500
  15. typedef struct TextAttributes {
  16. uint8_t fgcol:4;
  17. uint8_t bgcol:4;
  18. uint8_t bold:1;
  19. uint8_t uline:1;
  20. uint8_t blink:1;
  21. uint8_t invers:1;
  22. uint8_t unvisible:1;
  23. } TextAttributes;
  24. #define TEXT_ATTRIBUTES_DEFAULT ((TextAttributes) { \
  25. .fgcol = QEMU_COLOR_WHITE, \
  26. .bgcol = QEMU_COLOR_BLACK \
  27. })
  28. typedef struct TextCell {
  29. uint8_t ch;
  30. TextAttributes t_attrib;
  31. } TextCell;
  32. #define MAX_ESC_PARAMS 3
  33. enum TTYState {
  34. TTY_STATE_NORM,
  35. TTY_STATE_ESC,
  36. TTY_STATE_CSI,
  37. TTY_STATE_G0,
  38. TTY_STATE_G1,
  39. };
  40. typedef struct QemuTextConsole {
  41. QemuConsole parent;
  42. int width;
  43. int height;
  44. int total_height;
  45. int backscroll_height;
  46. int x, y;
  47. int y_displayed;
  48. int y_base;
  49. TextCell *cells;
  50. int text_x[2], text_y[2], cursor_invalidate;
  51. int echo;
  52. int update_x0;
  53. int update_y0;
  54. int update_x1;
  55. int update_y1;
  56. Chardev *chr;
  57. /* fifo for key pressed */
  58. Fifo8 out_fifo;
  59. } QemuTextConsole;
  60. typedef QemuConsoleClass QemuTextConsoleClass;
  61. OBJECT_DEFINE_TYPE(QemuTextConsole, qemu_text_console, QEMU_TEXT_CONSOLE, QEMU_CONSOLE)
  62. typedef struct QemuFixedTextConsole {
  63. QemuTextConsole parent;
  64. } QemuFixedTextConsole;
  65. typedef QemuTextConsoleClass QemuFixedTextConsoleClass;
  66. OBJECT_DEFINE_TYPE(QemuFixedTextConsole, qemu_fixed_text_console, QEMU_FIXED_TEXT_CONSOLE, QEMU_TEXT_CONSOLE)
  67. struct VCChardev {
  68. Chardev parent;
  69. QemuTextConsole *console;
  70. enum TTYState state;
  71. int esc_params[MAX_ESC_PARAMS];
  72. int nb_esc_params;
  73. TextAttributes t_attrib; /* currently active text attributes */
  74. TextAttributes t_attrib_saved;
  75. int x_saved, y_saved;
  76. };
  77. typedef struct VCChardev VCChardev;
  78. static const pixman_color_t color_table_rgb[2][8] = {
  79. { /* dark */
  80. [QEMU_COLOR_BLACK] = QEMU_PIXMAN_COLOR_BLACK,
  81. [QEMU_COLOR_BLUE] = QEMU_PIXMAN_COLOR(0x00, 0x00, 0xaa), /* blue */
  82. [QEMU_COLOR_GREEN] = QEMU_PIXMAN_COLOR(0x00, 0xaa, 0x00), /* green */
  83. [QEMU_COLOR_CYAN] = QEMU_PIXMAN_COLOR(0x00, 0xaa, 0xaa), /* cyan */
  84. [QEMU_COLOR_RED] = QEMU_PIXMAN_COLOR(0xaa, 0x00, 0x00), /* red */
  85. [QEMU_COLOR_MAGENTA] = QEMU_PIXMAN_COLOR(0xaa, 0x00, 0xaa), /* magenta */
  86. [QEMU_COLOR_YELLOW] = QEMU_PIXMAN_COLOR(0xaa, 0xaa, 0x00), /* yellow */
  87. [QEMU_COLOR_WHITE] = QEMU_PIXMAN_COLOR_GRAY,
  88. },
  89. { /* bright */
  90. [QEMU_COLOR_BLACK] = QEMU_PIXMAN_COLOR_BLACK,
  91. [QEMU_COLOR_BLUE] = QEMU_PIXMAN_COLOR(0x00, 0x00, 0xff), /* blue */
  92. [QEMU_COLOR_GREEN] = QEMU_PIXMAN_COLOR(0x00, 0xff, 0x00), /* green */
  93. [QEMU_COLOR_CYAN] = QEMU_PIXMAN_COLOR(0x00, 0xff, 0xff), /* cyan */
  94. [QEMU_COLOR_RED] = QEMU_PIXMAN_COLOR(0xff, 0x00, 0x00), /* red */
  95. [QEMU_COLOR_MAGENTA] = QEMU_PIXMAN_COLOR(0xff, 0x00, 0xff), /* magenta */
  96. [QEMU_COLOR_YELLOW] = QEMU_PIXMAN_COLOR(0xff, 0xff, 0x00), /* yellow */
  97. [QEMU_COLOR_WHITE] = QEMU_PIXMAN_COLOR(0xff, 0xff, 0xff), /* white */
  98. }
  99. };
  100. static bool cursor_visible_phase;
  101. static QEMUTimer *cursor_timer;
  102. const char *
  103. qemu_text_console_get_label(QemuTextConsole *c)
  104. {
  105. return c->chr ? c->chr->label : NULL;
  106. }
  107. static void qemu_console_fill_rect(QemuConsole *con, int posx, int posy,
  108. int width, int height, pixman_color_t color)
  109. {
  110. DisplaySurface *surface = qemu_console_surface(con);
  111. pixman_rectangle16_t rect = {
  112. .x = posx, .y = posy, .width = width, .height = height
  113. };
  114. assert(surface);
  115. pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
  116. &color, 1, &rect);
  117. }
  118. /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
  119. static void qemu_console_bitblt(QemuConsole *con,
  120. int xs, int ys, int xd, int yd, int w, int h)
  121. {
  122. DisplaySurface *surface = qemu_console_surface(con);
  123. assert(surface);
  124. pixman_image_composite(PIXMAN_OP_SRC,
  125. surface->image, NULL, surface->image,
  126. xs, ys, 0, 0, xd, yd, w, h);
  127. }
  128. static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
  129. TextAttributes *t_attrib)
  130. {
  131. static pixman_image_t *glyphs[256];
  132. DisplaySurface *surface = qemu_console_surface(s);
  133. pixman_color_t fgcol, bgcol;
  134. assert(surface);
  135. if (t_attrib->invers) {
  136. bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
  137. fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
  138. } else {
  139. fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
  140. bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
  141. }
  142. if (!glyphs[ch]) {
  143. glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
  144. }
  145. qemu_pixman_glyph_render(glyphs[ch], surface->image,
  146. &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
  147. }
  148. static void invalidate_xy(QemuTextConsole *s, int x, int y)
  149. {
  150. if (!qemu_console_is_visible(QEMU_CONSOLE(s))) {
  151. return;
  152. }
  153. if (s->update_x0 > x * FONT_WIDTH)
  154. s->update_x0 = x * FONT_WIDTH;
  155. if (s->update_y0 > y * FONT_HEIGHT)
  156. s->update_y0 = y * FONT_HEIGHT;
  157. if (s->update_x1 < (x + 1) * FONT_WIDTH)
  158. s->update_x1 = (x + 1) * FONT_WIDTH;
  159. if (s->update_y1 < (y + 1) * FONT_HEIGHT)
  160. s->update_y1 = (y + 1) * FONT_HEIGHT;
  161. }
  162. static void console_show_cursor(QemuTextConsole *s, int show)
  163. {
  164. TextCell *c;
  165. int y, y1;
  166. int x = s->x;
  167. s->cursor_invalidate = 1;
  168. if (x >= s->width) {
  169. x = s->width - 1;
  170. }
  171. y1 = (s->y_base + s->y) % s->total_height;
  172. y = y1 - s->y_displayed;
  173. if (y < 0) {
  174. y += s->total_height;
  175. }
  176. if (y < s->height) {
  177. c = &s->cells[y1 * s->width + x];
  178. if (show && cursor_visible_phase) {
  179. TextAttributes t_attrib = TEXT_ATTRIBUTES_DEFAULT;
  180. t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
  181. vga_putcharxy(QEMU_CONSOLE(s), x, y, c->ch, &t_attrib);
  182. } else {
  183. vga_putcharxy(QEMU_CONSOLE(s), x, y, c->ch, &(c->t_attrib));
  184. }
  185. invalidate_xy(s, x, y);
  186. }
  187. }
  188. static void console_refresh(QemuTextConsole *s)
  189. {
  190. DisplaySurface *surface = qemu_console_surface(QEMU_CONSOLE(s));
  191. TextCell *c;
  192. int x, y, y1;
  193. assert(surface);
  194. s->text_x[0] = 0;
  195. s->text_y[0] = 0;
  196. s->text_x[1] = s->width - 1;
  197. s->text_y[1] = s->height - 1;
  198. s->cursor_invalidate = 1;
  199. qemu_console_fill_rect(QEMU_CONSOLE(s), 0, 0, surface_width(surface), surface_height(surface),
  200. color_table_rgb[0][QEMU_COLOR_BLACK]);
  201. y1 = s->y_displayed;
  202. for (y = 0; y < s->height; y++) {
  203. c = s->cells + y1 * s->width;
  204. for (x = 0; x < s->width; x++) {
  205. vga_putcharxy(QEMU_CONSOLE(s), x, y, c->ch,
  206. &(c->t_attrib));
  207. c++;
  208. }
  209. if (++y1 == s->total_height) {
  210. y1 = 0;
  211. }
  212. }
  213. console_show_cursor(s, 1);
  214. dpy_gfx_update(QEMU_CONSOLE(s), 0, 0,
  215. surface_width(surface), surface_height(surface));
  216. }
  217. static void console_scroll(QemuTextConsole *s, int ydelta)
  218. {
  219. int i, y1;
  220. if (ydelta > 0) {
  221. for(i = 0; i < ydelta; i++) {
  222. if (s->y_displayed == s->y_base)
  223. break;
  224. if (++s->y_displayed == s->total_height)
  225. s->y_displayed = 0;
  226. }
  227. } else {
  228. ydelta = -ydelta;
  229. i = s->backscroll_height;
  230. if (i > s->total_height - s->height)
  231. i = s->total_height - s->height;
  232. y1 = s->y_base - i;
  233. if (y1 < 0)
  234. y1 += s->total_height;
  235. for(i = 0; i < ydelta; i++) {
  236. if (s->y_displayed == y1)
  237. break;
  238. if (--s->y_displayed < 0)
  239. s->y_displayed = s->total_height - 1;
  240. }
  241. }
  242. console_refresh(s);
  243. }
  244. static void kbd_send_chars(QemuTextConsole *s)
  245. {
  246. uint32_t len, avail;
  247. len = qemu_chr_be_can_write(s->chr);
  248. avail = fifo8_num_used(&s->out_fifo);
  249. while (len > 0 && avail > 0) {
  250. const uint8_t *buf;
  251. uint32_t size;
  252. buf = fifo8_pop_bufptr(&s->out_fifo, MIN(len, avail), &size);
  253. qemu_chr_be_write(s->chr, buf, size);
  254. len = qemu_chr_be_can_write(s->chr);
  255. avail -= size;
  256. }
  257. }
  258. /* called when an ascii key is pressed */
  259. void qemu_text_console_handle_keysym(QemuTextConsole *s, int keysym)
  260. {
  261. uint8_t buf[16], *q;
  262. int c;
  263. uint32_t num_free;
  264. switch(keysym) {
  265. case QEMU_KEY_CTRL_UP:
  266. console_scroll(s, -1);
  267. break;
  268. case QEMU_KEY_CTRL_DOWN:
  269. console_scroll(s, 1);
  270. break;
  271. case QEMU_KEY_CTRL_PAGEUP:
  272. console_scroll(s, -10);
  273. break;
  274. case QEMU_KEY_CTRL_PAGEDOWN:
  275. console_scroll(s, 10);
  276. break;
  277. default:
  278. /* convert the QEMU keysym to VT100 key string */
  279. q = buf;
  280. if (keysym >= 0xe100 && keysym <= 0xe11f) {
  281. *q++ = '\033';
  282. *q++ = '[';
  283. c = keysym - 0xe100;
  284. if (c >= 10)
  285. *q++ = '0' + (c / 10);
  286. *q++ = '0' + (c % 10);
  287. *q++ = '~';
  288. } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
  289. *q++ = '\033';
  290. *q++ = '[';
  291. *q++ = keysym & 0xff;
  292. } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
  293. qemu_chr_write(s->chr, (uint8_t *)"\r", 1, true);
  294. *q++ = '\n';
  295. } else {
  296. *q++ = keysym;
  297. }
  298. if (s->echo) {
  299. qemu_chr_write(s->chr, buf, q - buf, true);
  300. }
  301. num_free = fifo8_num_free(&s->out_fifo);
  302. fifo8_push_all(&s->out_fifo, buf, MIN(num_free, q - buf));
  303. kbd_send_chars(s);
  304. break;
  305. }
  306. }
  307. static void text_console_update(void *opaque, console_ch_t *chardata)
  308. {
  309. QemuTextConsole *s = QEMU_TEXT_CONSOLE(opaque);
  310. int i, j, src;
  311. if (s->text_x[0] <= s->text_x[1]) {
  312. src = (s->y_base + s->text_y[0]) * s->width;
  313. chardata += s->text_y[0] * s->width;
  314. for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
  315. for (j = 0; j < s->width; j++, src++) {
  316. console_write_ch(chardata ++,
  317. ATTR2CHTYPE(s->cells[src].ch,
  318. s->cells[src].t_attrib.fgcol,
  319. s->cells[src].t_attrib.bgcol,
  320. s->cells[src].t_attrib.bold));
  321. }
  322. dpy_text_update(QEMU_CONSOLE(s), s->text_x[0], s->text_y[0],
  323. s->text_x[1] - s->text_x[0], i - s->text_y[0]);
  324. s->text_x[0] = s->width;
  325. s->text_y[0] = s->height;
  326. s->text_x[1] = 0;
  327. s->text_y[1] = 0;
  328. }
  329. if (s->cursor_invalidate) {
  330. dpy_text_cursor(QEMU_CONSOLE(s), s->x, s->y);
  331. s->cursor_invalidate = 0;
  332. }
  333. }
  334. static void text_console_resize(QemuTextConsole *t)
  335. {
  336. QemuConsole *s = QEMU_CONSOLE(t);
  337. TextCell *cells, *c, *c1;
  338. int w1, x, y, last_width, w, h;
  339. assert(s->scanout.kind == SCANOUT_SURFACE);
  340. w = surface_width(s->surface) / FONT_WIDTH;
  341. h = surface_height(s->surface) / FONT_HEIGHT;
  342. if (w == t->width && h == t->height) {
  343. return;
  344. }
  345. last_width = t->width;
  346. t->width = w;
  347. t->height = h;
  348. w1 = MIN(t->width, last_width);
  349. cells = g_new(TextCell, t->width * t->total_height + 1);
  350. for (y = 0; y < t->total_height; y++) {
  351. c = &cells[y * t->width];
  352. if (w1 > 0) {
  353. c1 = &t->cells[y * last_width];
  354. for (x = 0; x < w1; x++) {
  355. *c++ = *c1++;
  356. }
  357. }
  358. for (x = w1; x < t->width; x++) {
  359. c->ch = ' ';
  360. c->t_attrib = TEXT_ATTRIBUTES_DEFAULT;
  361. c++;
  362. }
  363. }
  364. g_free(t->cells);
  365. t->cells = cells;
  366. }
  367. static void vc_put_lf(VCChardev *vc)
  368. {
  369. QemuTextConsole *s = vc->console;
  370. TextCell *c;
  371. int x, y1;
  372. s->y++;
  373. if (s->y >= s->height) {
  374. s->y = s->height - 1;
  375. if (s->y_displayed == s->y_base) {
  376. if (++s->y_displayed == s->total_height)
  377. s->y_displayed = 0;
  378. }
  379. if (++s->y_base == s->total_height)
  380. s->y_base = 0;
  381. if (s->backscroll_height < s->total_height)
  382. s->backscroll_height++;
  383. y1 = (s->y_base + s->height - 1) % s->total_height;
  384. c = &s->cells[y1 * s->width];
  385. for(x = 0; x < s->width; x++) {
  386. c->ch = ' ';
  387. c->t_attrib = TEXT_ATTRIBUTES_DEFAULT;
  388. c++;
  389. }
  390. if (s->y_displayed == s->y_base) {
  391. s->text_x[0] = 0;
  392. s->text_y[0] = 0;
  393. s->text_x[1] = s->width - 1;
  394. s->text_y[1] = s->height - 1;
  395. qemu_console_bitblt(QEMU_CONSOLE(s), 0, FONT_HEIGHT, 0, 0,
  396. s->width * FONT_WIDTH,
  397. (s->height - 1) * FONT_HEIGHT);
  398. qemu_console_fill_rect(QEMU_CONSOLE(s), 0, (s->height - 1) * FONT_HEIGHT,
  399. s->width * FONT_WIDTH, FONT_HEIGHT,
  400. color_table_rgb[0][TEXT_ATTRIBUTES_DEFAULT.bgcol]);
  401. s->update_x0 = 0;
  402. s->update_y0 = 0;
  403. s->update_x1 = s->width * FONT_WIDTH;
  404. s->update_y1 = s->height * FONT_HEIGHT;
  405. }
  406. }
  407. }
  408. /* Set console attributes depending on the current escape codes.
  409. * NOTE: I know this code is not very efficient (checking every color for it
  410. * self) but it is more readable and better maintainable.
  411. */
  412. static void vc_handle_escape(VCChardev *vc)
  413. {
  414. int i;
  415. for (i = 0; i < vc->nb_esc_params; i++) {
  416. switch (vc->esc_params[i]) {
  417. case 0: /* reset all console attributes to default */
  418. vc->t_attrib = TEXT_ATTRIBUTES_DEFAULT;
  419. break;
  420. case 1:
  421. vc->t_attrib.bold = 1;
  422. break;
  423. case 4:
  424. vc->t_attrib.uline = 1;
  425. break;
  426. case 5:
  427. vc->t_attrib.blink = 1;
  428. break;
  429. case 7:
  430. vc->t_attrib.invers = 1;
  431. break;
  432. case 8:
  433. vc->t_attrib.unvisible = 1;
  434. break;
  435. case 22:
  436. vc->t_attrib.bold = 0;
  437. break;
  438. case 24:
  439. vc->t_attrib.uline = 0;
  440. break;
  441. case 25:
  442. vc->t_attrib.blink = 0;
  443. break;
  444. case 27:
  445. vc->t_attrib.invers = 0;
  446. break;
  447. case 28:
  448. vc->t_attrib.unvisible = 0;
  449. break;
  450. /* set foreground color */
  451. case 30:
  452. vc->t_attrib.fgcol = QEMU_COLOR_BLACK;
  453. break;
  454. case 31:
  455. vc->t_attrib.fgcol = QEMU_COLOR_RED;
  456. break;
  457. case 32:
  458. vc->t_attrib.fgcol = QEMU_COLOR_GREEN;
  459. break;
  460. case 33:
  461. vc->t_attrib.fgcol = QEMU_COLOR_YELLOW;
  462. break;
  463. case 34:
  464. vc->t_attrib.fgcol = QEMU_COLOR_BLUE;
  465. break;
  466. case 35:
  467. vc->t_attrib.fgcol = QEMU_COLOR_MAGENTA;
  468. break;
  469. case 36:
  470. vc->t_attrib.fgcol = QEMU_COLOR_CYAN;
  471. break;
  472. case 37:
  473. vc->t_attrib.fgcol = QEMU_COLOR_WHITE;
  474. break;
  475. /* set background color */
  476. case 40:
  477. vc->t_attrib.bgcol = QEMU_COLOR_BLACK;
  478. break;
  479. case 41:
  480. vc->t_attrib.bgcol = QEMU_COLOR_RED;
  481. break;
  482. case 42:
  483. vc->t_attrib.bgcol = QEMU_COLOR_GREEN;
  484. break;
  485. case 43:
  486. vc->t_attrib.bgcol = QEMU_COLOR_YELLOW;
  487. break;
  488. case 44:
  489. vc->t_attrib.bgcol = QEMU_COLOR_BLUE;
  490. break;
  491. case 45:
  492. vc->t_attrib.bgcol = QEMU_COLOR_MAGENTA;
  493. break;
  494. case 46:
  495. vc->t_attrib.bgcol = QEMU_COLOR_CYAN;
  496. break;
  497. case 47:
  498. vc->t_attrib.bgcol = QEMU_COLOR_WHITE;
  499. break;
  500. }
  501. }
  502. }
  503. static void vc_update_xy(VCChardev *vc, int x, int y)
  504. {
  505. QemuTextConsole *s = vc->console;
  506. TextCell *c;
  507. int y1, y2;
  508. s->text_x[0] = MIN(s->text_x[0], x);
  509. s->text_x[1] = MAX(s->text_x[1], x);
  510. s->text_y[0] = MIN(s->text_y[0], y);
  511. s->text_y[1] = MAX(s->text_y[1], y);
  512. y1 = (s->y_base + y) % s->total_height;
  513. y2 = y1 - s->y_displayed;
  514. if (y2 < 0) {
  515. y2 += s->total_height;
  516. }
  517. if (y2 < s->height) {
  518. if (x >= s->width) {
  519. x = s->width - 1;
  520. }
  521. c = &s->cells[y1 * s->width + x];
  522. vga_putcharxy(QEMU_CONSOLE(s), x, y2, c->ch,
  523. &(c->t_attrib));
  524. invalidate_xy(s, x, y2);
  525. }
  526. }
  527. static void vc_clear_xy(VCChardev *vc, int x, int y)
  528. {
  529. QemuTextConsole *s = vc->console;
  530. int y1 = (s->y_base + y) % s->total_height;
  531. if (x >= s->width) {
  532. x = s->width - 1;
  533. }
  534. TextCell *c = &s->cells[y1 * s->width + x];
  535. c->ch = ' ';
  536. c->t_attrib = TEXT_ATTRIBUTES_DEFAULT;
  537. vc_update_xy(vc, x, y);
  538. }
  539. static void vc_put_one(VCChardev *vc, int ch)
  540. {
  541. QemuTextConsole *s = vc->console;
  542. TextCell *c;
  543. int y1;
  544. if (s->x >= s->width) {
  545. /* line wrap */
  546. s->x = 0;
  547. vc_put_lf(vc);
  548. }
  549. y1 = (s->y_base + s->y) % s->total_height;
  550. c = &s->cells[y1 * s->width + s->x];
  551. c->ch = ch;
  552. c->t_attrib = vc->t_attrib;
  553. vc_update_xy(vc, s->x, s->y);
  554. s->x++;
  555. }
  556. static void vc_respond_str(VCChardev *vc, const char *buf)
  557. {
  558. QemuTextConsole *s = vc->console;
  559. qemu_chr_be_write(s->chr, (const uint8_t *)buf, strlen(buf));
  560. }
  561. /* set cursor, checking bounds */
  562. static void vc_set_cursor(VCChardev *vc, int x, int y)
  563. {
  564. QemuTextConsole *s = vc->console;
  565. if (x < 0) {
  566. x = 0;
  567. }
  568. if (y < 0) {
  569. y = 0;
  570. }
  571. if (y >= s->height) {
  572. y = s->height - 1;
  573. }
  574. if (x >= s->width) {
  575. x = s->width - 1;
  576. }
  577. s->x = x;
  578. s->y = y;
  579. }
  580. /**
  581. * vc_csi_P() - (DCH) deletes one or more characters from the cursor
  582. * position to the right. As characters are deleted, the remaining
  583. * characters between the cursor and right margin move to the
  584. * left. Character attributes move with the characters.
  585. */
  586. static void vc_csi_P(struct VCChardev *vc, unsigned int nr)
  587. {
  588. QemuTextConsole *s = vc->console;
  589. TextCell *c1, *c2;
  590. unsigned int x1, x2, y;
  591. unsigned int end, len;
  592. if (!nr) {
  593. nr = 1;
  594. }
  595. if (nr > s->width - s->x) {
  596. nr = s->width - s->x;
  597. if (!nr) {
  598. return;
  599. }
  600. }
  601. x1 = s->x;
  602. x2 = s->x + nr;
  603. len = s->width - x2;
  604. if (len) {
  605. y = (s->y_base + s->y) % s->total_height;
  606. c1 = &s->cells[y * s->width + x1];
  607. c2 = &s->cells[y * s->width + x2];
  608. memmove(c1, c2, len * sizeof(*c1));
  609. for (end = x1 + len; x1 < end; x1++) {
  610. vc_update_xy(vc, x1, s->y);
  611. }
  612. }
  613. /* Clear the rest */
  614. for (; x1 < s->width; x1++) {
  615. vc_clear_xy(vc, x1, s->y);
  616. }
  617. }
  618. /**
  619. * vc_csi_at() - (ICH) inserts `nr` blank characters with the default
  620. * character attribute. The cursor remains at the beginning of the
  621. * blank characters. Text between the cursor and right margin moves to
  622. * the right. Characters scrolled past the right margin are lost.
  623. */
  624. static void vc_csi_at(struct VCChardev *vc, unsigned int nr)
  625. {
  626. QemuTextConsole *s = vc->console;
  627. TextCell *c1, *c2;
  628. unsigned int x1, x2, y;
  629. unsigned int end, len;
  630. if (!nr) {
  631. nr = 1;
  632. }
  633. if (nr > s->width - s->x) {
  634. nr = s->width - s->x;
  635. if (!nr) {
  636. return;
  637. }
  638. }
  639. x1 = s->x + nr;
  640. x2 = s->x;
  641. len = s->width - x1;
  642. if (len) {
  643. y = (s->y_base + s->y) % s->total_height;
  644. c1 = &s->cells[y * s->width + x1];
  645. c2 = &s->cells[y * s->width + x2];
  646. memmove(c1, c2, len * sizeof(*c1));
  647. for (end = x1 + len; x1 < end; x1++) {
  648. vc_update_xy(vc, x1, s->y);
  649. }
  650. }
  651. /* Insert blanks */
  652. for (x1 = s->x; x1 < s->x + nr; x1++) {
  653. vc_clear_xy(vc, x1, s->y);
  654. }
  655. }
  656. /**
  657. * vc_save_cursor() - saves cursor position and character attributes.
  658. */
  659. static void vc_save_cursor(VCChardev *vc)
  660. {
  661. QemuTextConsole *s = vc->console;
  662. vc->x_saved = s->x;
  663. vc->y_saved = s->y;
  664. vc->t_attrib_saved = vc->t_attrib;
  665. }
  666. /**
  667. * vc_restore_cursor() - restores cursor position and character
  668. * attributes from saved state.
  669. */
  670. static void vc_restore_cursor(VCChardev *vc)
  671. {
  672. QemuTextConsole *s = vc->console;
  673. s->x = vc->x_saved;
  674. s->y = vc->y_saved;
  675. vc->t_attrib = vc->t_attrib_saved;
  676. }
  677. static void vc_putchar(VCChardev *vc, int ch)
  678. {
  679. QemuTextConsole *s = vc->console;
  680. int i;
  681. int x, y;
  682. g_autofree char *response = NULL;
  683. switch(vc->state) {
  684. case TTY_STATE_NORM:
  685. switch(ch) {
  686. case '\r': /* carriage return */
  687. s->x = 0;
  688. break;
  689. case '\n': /* newline */
  690. vc_put_lf(vc);
  691. break;
  692. case '\b': /* backspace */
  693. if (s->x > 0)
  694. s->x--;
  695. break;
  696. case '\t': /* tabspace */
  697. if (s->x + (8 - (s->x % 8)) > s->width) {
  698. s->x = 0;
  699. vc_put_lf(vc);
  700. } else {
  701. s->x = s->x + (8 - (s->x % 8));
  702. }
  703. break;
  704. case '\a': /* alert aka. bell */
  705. /* TODO: has to be implemented */
  706. break;
  707. case 14:
  708. /* SI (shift in), character set 0 (ignored) */
  709. break;
  710. case 15:
  711. /* SO (shift out), character set 1 (ignored) */
  712. break;
  713. case 27: /* esc (introducing an escape sequence) */
  714. vc->state = TTY_STATE_ESC;
  715. break;
  716. default:
  717. vc_put_one(vc, ch);
  718. break;
  719. }
  720. break;
  721. case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
  722. if (ch == '[') {
  723. for(i=0;i<MAX_ESC_PARAMS;i++)
  724. vc->esc_params[i] = 0;
  725. vc->nb_esc_params = 0;
  726. vc->state = TTY_STATE_CSI;
  727. } else if (ch == '(') {
  728. vc->state = TTY_STATE_G0;
  729. } else if (ch == ')') {
  730. vc->state = TTY_STATE_G1;
  731. } else if (ch == '7') {
  732. vc_save_cursor(vc);
  733. vc->state = TTY_STATE_NORM;
  734. } else if (ch == '8') {
  735. vc_restore_cursor(vc);
  736. vc->state = TTY_STATE_NORM;
  737. } else {
  738. vc->state = TTY_STATE_NORM;
  739. }
  740. break;
  741. case TTY_STATE_CSI: /* handle escape sequence parameters */
  742. if (ch >= '0' && ch <= '9') {
  743. if (vc->nb_esc_params < MAX_ESC_PARAMS) {
  744. int *param = &vc->esc_params[vc->nb_esc_params];
  745. int digit = (ch - '0');
  746. *param = (*param <= (INT_MAX - digit) / 10) ?
  747. *param * 10 + digit : INT_MAX;
  748. }
  749. } else {
  750. if (vc->nb_esc_params < MAX_ESC_PARAMS)
  751. vc->nb_esc_params++;
  752. if (ch == ';' || ch == '?') {
  753. break;
  754. }
  755. trace_console_putchar_csi(vc->esc_params[0], vc->esc_params[1],
  756. ch, vc->nb_esc_params);
  757. vc->state = TTY_STATE_NORM;
  758. switch(ch) {
  759. case 'A':
  760. /* move cursor up */
  761. if (vc->esc_params[0] == 0) {
  762. vc->esc_params[0] = 1;
  763. }
  764. vc_set_cursor(vc, s->x, s->y - vc->esc_params[0]);
  765. break;
  766. case 'B':
  767. /* move cursor down */
  768. if (vc->esc_params[0] == 0) {
  769. vc->esc_params[0] = 1;
  770. }
  771. vc_set_cursor(vc, s->x, s->y + vc->esc_params[0]);
  772. break;
  773. case 'C':
  774. /* move cursor right */
  775. if (vc->esc_params[0] == 0) {
  776. vc->esc_params[0] = 1;
  777. }
  778. vc_set_cursor(vc, s->x + vc->esc_params[0], s->y);
  779. break;
  780. case 'D':
  781. /* move cursor left */
  782. if (vc->esc_params[0] == 0) {
  783. vc->esc_params[0] = 1;
  784. }
  785. vc_set_cursor(vc, s->x - vc->esc_params[0], s->y);
  786. break;
  787. case 'G':
  788. /* move cursor to column */
  789. vc_set_cursor(vc, vc->esc_params[0] - 1, s->y);
  790. break;
  791. case 'f':
  792. case 'H':
  793. /* move cursor to row, column */
  794. vc_set_cursor(vc, vc->esc_params[1] - 1, vc->esc_params[0] - 1);
  795. break;
  796. case 'J':
  797. switch (vc->esc_params[0]) {
  798. case 0:
  799. /* clear to end of screen */
  800. for (y = s->y; y < s->height; y++) {
  801. for (x = 0; x < s->width; x++) {
  802. if (y == s->y && x < s->x) {
  803. continue;
  804. }
  805. vc_clear_xy(vc, x, y);
  806. }
  807. }
  808. break;
  809. case 1:
  810. /* clear from beginning of screen */
  811. for (y = 0; y <= s->y; y++) {
  812. for (x = 0; x < s->width; x++) {
  813. if (y == s->y && x > s->x) {
  814. break;
  815. }
  816. vc_clear_xy(vc, x, y);
  817. }
  818. }
  819. break;
  820. case 2:
  821. /* clear entire screen */
  822. for (y = 0; y <= s->height; y++) {
  823. for (x = 0; x < s->width; x++) {
  824. vc_clear_xy(vc, x, y);
  825. }
  826. }
  827. break;
  828. }
  829. break;
  830. case 'K':
  831. switch (vc->esc_params[0]) {
  832. case 0:
  833. /* clear to eol */
  834. for(x = s->x; x < s->width; x++) {
  835. vc_clear_xy(vc, x, s->y);
  836. }
  837. break;
  838. case 1:
  839. /* clear from beginning of line */
  840. for (x = 0; x <= s->x && x < s->width; x++) {
  841. vc_clear_xy(vc, x, s->y);
  842. }
  843. break;
  844. case 2:
  845. /* clear entire line */
  846. for(x = 0; x < s->width; x++) {
  847. vc_clear_xy(vc, x, s->y);
  848. }
  849. break;
  850. }
  851. break;
  852. case 'P':
  853. vc_csi_P(vc, vc->esc_params[0]);
  854. break;
  855. case 'm':
  856. vc_handle_escape(vc);
  857. break;
  858. case 'n':
  859. switch (vc->esc_params[0]) {
  860. case 5:
  861. /* report console status (always succeed)*/
  862. vc_respond_str(vc, "\033[0n");
  863. break;
  864. case 6:
  865. /* report cursor position */
  866. response = g_strdup_printf("\033[%d;%dR",
  867. s->y + 1, s->x + 1);
  868. vc_respond_str(vc, response);
  869. break;
  870. }
  871. break;
  872. case 's':
  873. vc_save_cursor(vc);
  874. break;
  875. case 'u':
  876. vc_restore_cursor(vc);
  877. break;
  878. case '@':
  879. vc_csi_at(vc, vc->esc_params[0]);
  880. break;
  881. default:
  882. trace_console_putchar_unhandled(ch);
  883. break;
  884. }
  885. break;
  886. }
  887. break;
  888. case TTY_STATE_G0: /* set character sets */
  889. case TTY_STATE_G1: /* set character sets */
  890. switch (ch) {
  891. case 'B':
  892. /* Latin-1 map */
  893. break;
  894. }
  895. vc->state = TTY_STATE_NORM;
  896. break;
  897. }
  898. }
  899. #define TYPE_CHARDEV_VC "chardev-vc"
  900. DECLARE_INSTANCE_CHECKER(VCChardev, VC_CHARDEV,
  901. TYPE_CHARDEV_VC)
  902. static int vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
  903. {
  904. VCChardev *drv = VC_CHARDEV(chr);
  905. QemuTextConsole *s = drv->console;
  906. int i;
  907. s->update_x0 = s->width * FONT_WIDTH;
  908. s->update_y0 = s->height * FONT_HEIGHT;
  909. s->update_x1 = 0;
  910. s->update_y1 = 0;
  911. console_show_cursor(s, 0);
  912. for(i = 0; i < len; i++) {
  913. vc_putchar(drv, buf[i]);
  914. }
  915. console_show_cursor(s, 1);
  916. if (s->update_x0 < s->update_x1) {
  917. dpy_gfx_update(QEMU_CONSOLE(s), s->update_x0, s->update_y0,
  918. s->update_x1 - s->update_x0,
  919. s->update_y1 - s->update_y0);
  920. }
  921. return len;
  922. }
  923. void qemu_text_console_update_cursor(void)
  924. {
  925. cursor_visible_phase = !cursor_visible_phase;
  926. if (qemu_invalidate_text_consoles()) {
  927. timer_mod(cursor_timer,
  928. qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + CONSOLE_CURSOR_PERIOD / 2);
  929. }
  930. }
  931. static void
  932. cursor_timer_cb(void *opaque)
  933. {
  934. qemu_text_console_update_cursor();
  935. }
  936. static void text_console_invalidate(void *opaque)
  937. {
  938. QemuTextConsole *s = QEMU_TEXT_CONSOLE(opaque);
  939. if (!QEMU_IS_FIXED_TEXT_CONSOLE(s)) {
  940. text_console_resize(QEMU_TEXT_CONSOLE(s));
  941. }
  942. console_refresh(s);
  943. }
  944. static void
  945. qemu_text_console_finalize(Object *obj)
  946. {
  947. }
  948. static void
  949. qemu_text_console_class_init(ObjectClass *oc, void *data)
  950. {
  951. if (!cursor_timer) {
  952. cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME, cursor_timer_cb, NULL);
  953. }
  954. }
  955. static const GraphicHwOps text_console_ops = {
  956. .invalidate = text_console_invalidate,
  957. .text_update = text_console_update,
  958. };
  959. static void
  960. qemu_text_console_init(Object *obj)
  961. {
  962. QemuTextConsole *c = QEMU_TEXT_CONSOLE(obj);
  963. fifo8_create(&c->out_fifo, 16);
  964. c->total_height = DEFAULT_BACKSCROLL;
  965. QEMU_CONSOLE(c)->hw_ops = &text_console_ops;
  966. QEMU_CONSOLE(c)->hw = c;
  967. }
  968. static void
  969. qemu_fixed_text_console_finalize(Object *obj)
  970. {
  971. }
  972. static void
  973. qemu_fixed_text_console_class_init(ObjectClass *oc, void *data)
  974. {
  975. }
  976. static void
  977. qemu_fixed_text_console_init(Object *obj)
  978. {
  979. }
  980. static void vc_chr_accept_input(Chardev *chr)
  981. {
  982. VCChardev *drv = VC_CHARDEV(chr);
  983. kbd_send_chars(drv->console);
  984. }
  985. static void vc_chr_set_echo(Chardev *chr, bool echo)
  986. {
  987. VCChardev *drv = VC_CHARDEV(chr);
  988. drv->console->echo = echo;
  989. }
  990. void qemu_text_console_update_size(QemuTextConsole *c)
  991. {
  992. dpy_text_resize(QEMU_CONSOLE(c), c->width, c->height);
  993. }
  994. static void vc_chr_open(Chardev *chr,
  995. ChardevBackend *backend,
  996. bool *be_opened,
  997. Error **errp)
  998. {
  999. ChardevVC *vc = backend->u.vc.data;
  1000. VCChardev *drv = VC_CHARDEV(chr);
  1001. QemuTextConsole *s;
  1002. unsigned width = 0;
  1003. unsigned height = 0;
  1004. if (vc->has_width) {
  1005. width = vc->width;
  1006. } else if (vc->has_cols) {
  1007. width = vc->cols * FONT_WIDTH;
  1008. }
  1009. if (vc->has_height) {
  1010. height = vc->height;
  1011. } else if (vc->has_rows) {
  1012. height = vc->rows * FONT_HEIGHT;
  1013. }
  1014. trace_console_txt_new(width, height);
  1015. if (width == 0 || height == 0) {
  1016. s = QEMU_TEXT_CONSOLE(object_new(TYPE_QEMU_TEXT_CONSOLE));
  1017. width = 80 * FONT_WIDTH;
  1018. height = 24 * FONT_HEIGHT;
  1019. } else {
  1020. s = QEMU_TEXT_CONSOLE(object_new(TYPE_QEMU_FIXED_TEXT_CONSOLE));
  1021. }
  1022. dpy_gfx_replace_surface(QEMU_CONSOLE(s), qemu_create_displaysurface(width, height));
  1023. s->chr = chr;
  1024. drv->console = s;
  1025. /* set current text attributes to default */
  1026. drv->t_attrib = TEXT_ATTRIBUTES_DEFAULT;
  1027. text_console_resize(s);
  1028. if (chr->label) {
  1029. char *msg;
  1030. drv->t_attrib.bgcol = QEMU_COLOR_BLUE;
  1031. msg = g_strdup_printf("%s console\r\n", chr->label);
  1032. qemu_chr_write(chr, (uint8_t *)msg, strlen(msg), true);
  1033. g_free(msg);
  1034. drv->t_attrib = TEXT_ATTRIBUTES_DEFAULT;
  1035. }
  1036. *be_opened = true;
  1037. }
  1038. static void vc_chr_parse(QemuOpts *opts, ChardevBackend *backend, Error **errp)
  1039. {
  1040. int val;
  1041. ChardevVC *vc;
  1042. backend->type = CHARDEV_BACKEND_KIND_VC;
  1043. vc = backend->u.vc.data = g_new0(ChardevVC, 1);
  1044. qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
  1045. val = qemu_opt_get_number(opts, "width", 0);
  1046. if (val != 0) {
  1047. vc->has_width = true;
  1048. vc->width = val;
  1049. }
  1050. val = qemu_opt_get_number(opts, "height", 0);
  1051. if (val != 0) {
  1052. vc->has_height = true;
  1053. vc->height = val;
  1054. }
  1055. val = qemu_opt_get_number(opts, "cols", 0);
  1056. if (val != 0) {
  1057. vc->has_cols = true;
  1058. vc->cols = val;
  1059. }
  1060. val = qemu_opt_get_number(opts, "rows", 0);
  1061. if (val != 0) {
  1062. vc->has_rows = true;
  1063. vc->rows = val;
  1064. }
  1065. }
  1066. static void char_vc_class_init(ObjectClass *oc, void *data)
  1067. {
  1068. ChardevClass *cc = CHARDEV_CLASS(oc);
  1069. cc->parse = vc_chr_parse;
  1070. cc->open = vc_chr_open;
  1071. cc->chr_write = vc_chr_write;
  1072. cc->chr_accept_input = vc_chr_accept_input;
  1073. cc->chr_set_echo = vc_chr_set_echo;
  1074. }
  1075. static const TypeInfo char_vc_type_info = {
  1076. .name = TYPE_CHARDEV_VC,
  1077. .parent = TYPE_CHARDEV,
  1078. .instance_size = sizeof(VCChardev),
  1079. .class_init = char_vc_class_init,
  1080. };
  1081. void qemu_console_early_init(void)
  1082. {
  1083. /* set the default vc driver */
  1084. if (!object_class_by_name(TYPE_CHARDEV_VC)) {
  1085. type_register_static(&char_vc_type_info);
  1086. }
  1087. }