console.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729
  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-common.h"
  25. #include "ui/console.h"
  26. #include "qemu/timer.h"
  27. #include "qmp-commands.h"
  28. #include "char/char.h"
  29. //#define DEBUG_CONSOLE
  30. #define DEFAULT_BACKSCROLL 512
  31. #define MAX_CONSOLES 12
  32. #define CONSOLE_CURSOR_PERIOD 500
  33. #define QEMU_RGBA(r, g, b, a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
  34. #define QEMU_RGB(r, g, b) QEMU_RGBA(r, g, b, 0xff)
  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. int index;
  107. console_type_t console_type;
  108. DisplayState *ds;
  109. /* Graphic console state. */
  110. vga_hw_update_ptr hw_update;
  111. vga_hw_invalidate_ptr hw_invalidate;
  112. vga_hw_screen_dump_ptr hw_screen_dump;
  113. vga_hw_text_update_ptr hw_text_update;
  114. void *hw;
  115. int g_width, g_height;
  116. /* Text console state */
  117. int width;
  118. int height;
  119. int total_height;
  120. int backscroll_height;
  121. int x, y;
  122. int x_saved, y_saved;
  123. int y_displayed;
  124. int y_base;
  125. TextAttributes t_attrib_default; /* default text attributes */
  126. TextAttributes t_attrib; /* currently active text attributes */
  127. TextCell *cells;
  128. int text_x[2], text_y[2], cursor_invalidate;
  129. int echo;
  130. bool cursor_visible_phase;
  131. QEMUTimer *cursor_timer;
  132. int update_x0;
  133. int update_y0;
  134. int update_x1;
  135. int update_y1;
  136. enum TTYState state;
  137. int esc_params[MAX_ESC_PARAMS];
  138. int nb_esc_params;
  139. CharDriverState *chr;
  140. /* fifo for key pressed */
  141. QEMUFIFO out_fifo;
  142. uint8_t out_fifo_buf[16];
  143. QEMUTimer *kbd_timer;
  144. };
  145. static DisplayState *display_state;
  146. static QemuConsole *active_console;
  147. static QemuConsole *consoles[MAX_CONSOLES];
  148. static int nb_consoles = 0;
  149. void vga_hw_update(void)
  150. {
  151. if (active_console && active_console->hw_update)
  152. active_console->hw_update(active_console->hw);
  153. }
  154. void vga_hw_invalidate(void)
  155. {
  156. if (active_console && active_console->hw_invalidate)
  157. active_console->hw_invalidate(active_console->hw);
  158. }
  159. void qmp_screendump(const char *filename, Error **errp)
  160. {
  161. QemuConsole *previous_active_console;
  162. bool cswitch;
  163. previous_active_console = active_console;
  164. cswitch = previous_active_console && previous_active_console->index != 0;
  165. /* There is currently no way of specifying which screen we want to dump,
  166. so always dump the first one. */
  167. if (cswitch) {
  168. console_select(0);
  169. }
  170. if (consoles[0] && consoles[0]->hw_screen_dump) {
  171. consoles[0]->hw_screen_dump(consoles[0]->hw, filename, cswitch, errp);
  172. } else {
  173. error_setg(errp, "device doesn't support screendump");
  174. }
  175. if (cswitch) {
  176. console_select(previous_active_console->index);
  177. }
  178. }
  179. void vga_hw_text_update(console_ch_t *chardata)
  180. {
  181. if (active_console && active_console->hw_text_update)
  182. active_console->hw_text_update(active_console->hw, chardata);
  183. }
  184. /* convert a RGBA color to a color index usable in graphic primitives */
  185. static unsigned int vga_get_color(DisplayState *ds, unsigned int rgba)
  186. {
  187. unsigned int r, g, b, color;
  188. switch(ds_get_bits_per_pixel(ds)) {
  189. #if 0
  190. case 8:
  191. r = (rgba >> 16) & 0xff;
  192. g = (rgba >> 8) & 0xff;
  193. b = (rgba) & 0xff;
  194. color = (rgb_to_index[r] * 6 * 6) +
  195. (rgb_to_index[g] * 6) +
  196. (rgb_to_index[b]);
  197. break;
  198. #endif
  199. case 15:
  200. r = (rgba >> 16) & 0xff;
  201. g = (rgba >> 8) & 0xff;
  202. b = (rgba) & 0xff;
  203. color = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
  204. break;
  205. case 16:
  206. r = (rgba >> 16) & 0xff;
  207. g = (rgba >> 8) & 0xff;
  208. b = (rgba) & 0xff;
  209. color = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
  210. break;
  211. case 32:
  212. default:
  213. color = rgba;
  214. break;
  215. }
  216. return color;
  217. }
  218. static void vga_fill_rect (DisplayState *ds,
  219. int posx, int posy, int width, int height, uint32_t color)
  220. {
  221. uint8_t *d, *d1;
  222. int x, y, bpp;
  223. bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
  224. d1 = ds_get_data(ds) +
  225. ds_get_linesize(ds) * posy + bpp * posx;
  226. for (y = 0; y < height; y++) {
  227. d = d1;
  228. switch(bpp) {
  229. case 1:
  230. for (x = 0; x < width; x++) {
  231. *((uint8_t *)d) = color;
  232. d++;
  233. }
  234. break;
  235. case 2:
  236. for (x = 0; x < width; x++) {
  237. *((uint16_t *)d) = color;
  238. d += 2;
  239. }
  240. break;
  241. case 4:
  242. for (x = 0; x < width; x++) {
  243. *((uint32_t *)d) = color;
  244. d += 4;
  245. }
  246. break;
  247. }
  248. d1 += ds_get_linesize(ds);
  249. }
  250. }
  251. /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
  252. static void vga_bitblt(DisplayState *ds, int xs, int ys, int xd, int yd, int w, int h)
  253. {
  254. const uint8_t *s;
  255. uint8_t *d;
  256. int wb, y, bpp;
  257. bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
  258. wb = w * bpp;
  259. if (yd <= ys) {
  260. s = ds_get_data(ds) +
  261. ds_get_linesize(ds) * ys + bpp * xs;
  262. d = ds_get_data(ds) +
  263. ds_get_linesize(ds) * yd + bpp * xd;
  264. for (y = 0; y < h; y++) {
  265. memmove(d, s, wb);
  266. d += ds_get_linesize(ds);
  267. s += ds_get_linesize(ds);
  268. }
  269. } else {
  270. s = ds_get_data(ds) +
  271. ds_get_linesize(ds) * (ys + h - 1) + bpp * xs;
  272. d = ds_get_data(ds) +
  273. ds_get_linesize(ds) * (yd + h - 1) + bpp * xd;
  274. for (y = 0; y < h; y++) {
  275. memmove(d, s, wb);
  276. d -= ds_get_linesize(ds);
  277. s -= ds_get_linesize(ds);
  278. }
  279. }
  280. }
  281. /***********************************************************/
  282. /* basic char display */
  283. #define FONT_HEIGHT 16
  284. #define FONT_WIDTH 8
  285. #include "vgafont.h"
  286. #define cbswap_32(__x) \
  287. ((uint32_t)( \
  288. (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
  289. (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
  290. (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
  291. (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
  292. #ifdef HOST_WORDS_BIGENDIAN
  293. #define PAT(x) x
  294. #else
  295. #define PAT(x) cbswap_32(x)
  296. #endif
  297. static const uint32_t dmask16[16] = {
  298. PAT(0x00000000),
  299. PAT(0x000000ff),
  300. PAT(0x0000ff00),
  301. PAT(0x0000ffff),
  302. PAT(0x00ff0000),
  303. PAT(0x00ff00ff),
  304. PAT(0x00ffff00),
  305. PAT(0x00ffffff),
  306. PAT(0xff000000),
  307. PAT(0xff0000ff),
  308. PAT(0xff00ff00),
  309. PAT(0xff00ffff),
  310. PAT(0xffff0000),
  311. PAT(0xffff00ff),
  312. PAT(0xffffff00),
  313. PAT(0xffffffff),
  314. };
  315. static const uint32_t dmask4[4] = {
  316. PAT(0x00000000),
  317. PAT(0x0000ffff),
  318. PAT(0xffff0000),
  319. PAT(0xffffffff),
  320. };
  321. static uint32_t color_table[2][8];
  322. #ifndef CONFIG_CURSES
  323. enum color_names {
  324. COLOR_BLACK = 0,
  325. COLOR_RED = 1,
  326. COLOR_GREEN = 2,
  327. COLOR_YELLOW = 3,
  328. COLOR_BLUE = 4,
  329. COLOR_MAGENTA = 5,
  330. COLOR_CYAN = 6,
  331. COLOR_WHITE = 7
  332. };
  333. #endif
  334. static const uint32_t color_table_rgb[2][8] = {
  335. { /* dark */
  336. QEMU_RGB(0x00, 0x00, 0x00), /* black */
  337. QEMU_RGB(0xaa, 0x00, 0x00), /* red */
  338. QEMU_RGB(0x00, 0xaa, 0x00), /* green */
  339. QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
  340. QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
  341. QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
  342. QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
  343. QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
  344. },
  345. { /* bright */
  346. QEMU_RGB(0x00, 0x00, 0x00), /* black */
  347. QEMU_RGB(0xff, 0x00, 0x00), /* red */
  348. QEMU_RGB(0x00, 0xff, 0x00), /* green */
  349. QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
  350. QEMU_RGB(0x00, 0x00, 0xff), /* blue */
  351. QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
  352. QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
  353. QEMU_RGB(0xff, 0xff, 0xff), /* white */
  354. }
  355. };
  356. static inline unsigned int col_expand(DisplayState *ds, unsigned int col)
  357. {
  358. switch(ds_get_bits_per_pixel(ds)) {
  359. case 8:
  360. col |= col << 8;
  361. col |= col << 16;
  362. break;
  363. case 15:
  364. case 16:
  365. col |= col << 16;
  366. break;
  367. default:
  368. break;
  369. }
  370. return col;
  371. }
  372. #ifdef DEBUG_CONSOLE
  373. static void console_print_text_attributes(TextAttributes *t_attrib, char ch)
  374. {
  375. if (t_attrib->bold) {
  376. printf("b");
  377. } else {
  378. printf(" ");
  379. }
  380. if (t_attrib->uline) {
  381. printf("u");
  382. } else {
  383. printf(" ");
  384. }
  385. if (t_attrib->blink) {
  386. printf("l");
  387. } else {
  388. printf(" ");
  389. }
  390. if (t_attrib->invers) {
  391. printf("i");
  392. } else {
  393. printf(" ");
  394. }
  395. if (t_attrib->unvisible) {
  396. printf("n");
  397. } else {
  398. printf(" ");
  399. }
  400. printf(" fg: %d bg: %d ch:'%2X' '%c'\n", t_attrib->fgcol, t_attrib->bgcol, ch, ch);
  401. }
  402. #endif
  403. static void vga_putcharxy(DisplayState *ds, int x, int y, int ch,
  404. TextAttributes *t_attrib)
  405. {
  406. uint8_t *d;
  407. const uint8_t *font_ptr;
  408. unsigned int font_data, linesize, xorcol, bpp;
  409. int i;
  410. unsigned int fgcol, bgcol;
  411. #ifdef DEBUG_CONSOLE
  412. printf("x: %2i y: %2i", x, y);
  413. console_print_text_attributes(t_attrib, ch);
  414. #endif
  415. if (t_attrib->invers) {
  416. bgcol = color_table[t_attrib->bold][t_attrib->fgcol];
  417. fgcol = color_table[t_attrib->bold][t_attrib->bgcol];
  418. } else {
  419. fgcol = color_table[t_attrib->bold][t_attrib->fgcol];
  420. bgcol = color_table[t_attrib->bold][t_attrib->bgcol];
  421. }
  422. bpp = (ds_get_bits_per_pixel(ds) + 7) >> 3;
  423. d = ds_get_data(ds) +
  424. ds_get_linesize(ds) * y * FONT_HEIGHT + bpp * x * FONT_WIDTH;
  425. linesize = ds_get_linesize(ds);
  426. font_ptr = vgafont16 + FONT_HEIGHT * ch;
  427. xorcol = bgcol ^ fgcol;
  428. switch(ds_get_bits_per_pixel(ds)) {
  429. case 8:
  430. for(i = 0; i < FONT_HEIGHT; i++) {
  431. font_data = *font_ptr++;
  432. if (t_attrib->uline
  433. && ((i == FONT_HEIGHT - 2) || (i == FONT_HEIGHT - 3))) {
  434. font_data = 0xFF;
  435. }
  436. ((uint32_t *)d)[0] = (dmask16[(font_data >> 4)] & xorcol) ^ bgcol;
  437. ((uint32_t *)d)[1] = (dmask16[(font_data >> 0) & 0xf] & xorcol) ^ bgcol;
  438. d += linesize;
  439. }
  440. break;
  441. case 16:
  442. case 15:
  443. for(i = 0; i < FONT_HEIGHT; i++) {
  444. font_data = *font_ptr++;
  445. if (t_attrib->uline
  446. && ((i == FONT_HEIGHT - 2) || (i == FONT_HEIGHT - 3))) {
  447. font_data = 0xFF;
  448. }
  449. ((uint32_t *)d)[0] = (dmask4[(font_data >> 6)] & xorcol) ^ bgcol;
  450. ((uint32_t *)d)[1] = (dmask4[(font_data >> 4) & 3] & xorcol) ^ bgcol;
  451. ((uint32_t *)d)[2] = (dmask4[(font_data >> 2) & 3] & xorcol) ^ bgcol;
  452. ((uint32_t *)d)[3] = (dmask4[(font_data >> 0) & 3] & xorcol) ^ bgcol;
  453. d += linesize;
  454. }
  455. break;
  456. case 32:
  457. for(i = 0; i < FONT_HEIGHT; i++) {
  458. font_data = *font_ptr++;
  459. if (t_attrib->uline && ((i == FONT_HEIGHT - 2) || (i == FONT_HEIGHT - 3))) {
  460. font_data = 0xFF;
  461. }
  462. ((uint32_t *)d)[0] = (-((font_data >> 7)) & xorcol) ^ bgcol;
  463. ((uint32_t *)d)[1] = (-((font_data >> 6) & 1) & xorcol) ^ bgcol;
  464. ((uint32_t *)d)[2] = (-((font_data >> 5) & 1) & xorcol) ^ bgcol;
  465. ((uint32_t *)d)[3] = (-((font_data >> 4) & 1) & xorcol) ^ bgcol;
  466. ((uint32_t *)d)[4] = (-((font_data >> 3) & 1) & xorcol) ^ bgcol;
  467. ((uint32_t *)d)[5] = (-((font_data >> 2) & 1) & xorcol) ^ bgcol;
  468. ((uint32_t *)d)[6] = (-((font_data >> 1) & 1) & xorcol) ^ bgcol;
  469. ((uint32_t *)d)[7] = (-((font_data >> 0) & 1) & xorcol) ^ bgcol;
  470. d += linesize;
  471. }
  472. break;
  473. }
  474. }
  475. static void text_console_resize(QemuConsole *s)
  476. {
  477. TextCell *cells, *c, *c1;
  478. int w1, x, y, last_width;
  479. last_width = s->width;
  480. s->width = s->g_width / FONT_WIDTH;
  481. s->height = s->g_height / FONT_HEIGHT;
  482. w1 = last_width;
  483. if (s->width < w1)
  484. w1 = s->width;
  485. cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
  486. for(y = 0; y < s->total_height; y++) {
  487. c = &cells[y * s->width];
  488. if (w1 > 0) {
  489. c1 = &s->cells[y * last_width];
  490. for(x = 0; x < w1; x++) {
  491. *c++ = *c1++;
  492. }
  493. }
  494. for(x = w1; x < s->width; x++) {
  495. c->ch = ' ';
  496. c->t_attrib = s->t_attrib_default;
  497. c++;
  498. }
  499. }
  500. g_free(s->cells);
  501. s->cells = cells;
  502. }
  503. static inline void text_update_xy(QemuConsole *s, int x, int y)
  504. {
  505. s->text_x[0] = MIN(s->text_x[0], x);
  506. s->text_x[1] = MAX(s->text_x[1], x);
  507. s->text_y[0] = MIN(s->text_y[0], y);
  508. s->text_y[1] = MAX(s->text_y[1], y);
  509. }
  510. static void invalidate_xy(QemuConsole *s, int x, int y)
  511. {
  512. if (s->update_x0 > x * FONT_WIDTH)
  513. s->update_x0 = x * FONT_WIDTH;
  514. if (s->update_y0 > y * FONT_HEIGHT)
  515. s->update_y0 = y * FONT_HEIGHT;
  516. if (s->update_x1 < (x + 1) * FONT_WIDTH)
  517. s->update_x1 = (x + 1) * FONT_WIDTH;
  518. if (s->update_y1 < (y + 1) * FONT_HEIGHT)
  519. s->update_y1 = (y + 1) * FONT_HEIGHT;
  520. }
  521. static void update_xy(QemuConsole *s, int x, int y)
  522. {
  523. TextCell *c;
  524. int y1, y2;
  525. if (s == active_console) {
  526. if (!ds_get_bits_per_pixel(s->ds)) {
  527. text_update_xy(s, x, y);
  528. return;
  529. }
  530. y1 = (s->y_base + y) % s->total_height;
  531. y2 = y1 - s->y_displayed;
  532. if (y2 < 0)
  533. y2 += s->total_height;
  534. if (y2 < s->height) {
  535. c = &s->cells[y1 * s->width + x];
  536. vga_putcharxy(s->ds, x, y2, c->ch,
  537. &(c->t_attrib));
  538. invalidate_xy(s, x, y2);
  539. }
  540. }
  541. }
  542. static void console_show_cursor(QemuConsole *s, int show)
  543. {
  544. TextCell *c;
  545. int y, y1;
  546. if (s == active_console) {
  547. int x = s->x;
  548. if (!ds_get_bits_per_pixel(s->ds)) {
  549. s->cursor_invalidate = 1;
  550. return;
  551. }
  552. if (x >= s->width) {
  553. x = s->width - 1;
  554. }
  555. y1 = (s->y_base + s->y) % s->total_height;
  556. y = y1 - s->y_displayed;
  557. if (y < 0)
  558. y += s->total_height;
  559. if (y < s->height) {
  560. c = &s->cells[y1 * s->width + x];
  561. if (show && s->cursor_visible_phase) {
  562. TextAttributes t_attrib = s->t_attrib_default;
  563. t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
  564. vga_putcharxy(s->ds, x, y, c->ch, &t_attrib);
  565. } else {
  566. vga_putcharxy(s->ds, x, y, c->ch, &(c->t_attrib));
  567. }
  568. invalidate_xy(s, x, y);
  569. }
  570. }
  571. }
  572. static void console_refresh(QemuConsole *s)
  573. {
  574. TextCell *c;
  575. int x, y, y1;
  576. if (s != active_console)
  577. return;
  578. if (s->ds->have_text) {
  579. s->text_x[0] = 0;
  580. s->text_y[0] = 0;
  581. s->text_x[1] = s->width - 1;
  582. s->text_y[1] = s->height - 1;
  583. s->cursor_invalidate = 1;
  584. }
  585. if (s->ds->have_gfx) {
  586. vga_fill_rect(s->ds, 0, 0, ds_get_width(s->ds), ds_get_height(s->ds),
  587. color_table[0][COLOR_BLACK]);
  588. y1 = s->y_displayed;
  589. for (y = 0; y < s->height; y++) {
  590. c = s->cells + y1 * s->width;
  591. for (x = 0; x < s->width; x++) {
  592. vga_putcharxy(s->ds, x, y, c->ch,
  593. &(c->t_attrib));
  594. c++;
  595. }
  596. if (++y1 == s->total_height) {
  597. y1 = 0;
  598. }
  599. }
  600. console_show_cursor(s, 1);
  601. dpy_gfx_update(s->ds, 0, 0, ds_get_width(s->ds), ds_get_height(s->ds));
  602. }
  603. }
  604. static void console_scroll(int ydelta)
  605. {
  606. QemuConsole *s;
  607. int i, y1;
  608. s = active_console;
  609. if (!s || (s->console_type == GRAPHIC_CONSOLE))
  610. return;
  611. if (ydelta > 0) {
  612. for(i = 0; i < ydelta; i++) {
  613. if (s->y_displayed == s->y_base)
  614. break;
  615. if (++s->y_displayed == s->total_height)
  616. s->y_displayed = 0;
  617. }
  618. } else {
  619. ydelta = -ydelta;
  620. i = s->backscroll_height;
  621. if (i > s->total_height - s->height)
  622. i = s->total_height - s->height;
  623. y1 = s->y_base - i;
  624. if (y1 < 0)
  625. y1 += s->total_height;
  626. for(i = 0; i < ydelta; i++) {
  627. if (s->y_displayed == y1)
  628. break;
  629. if (--s->y_displayed < 0)
  630. s->y_displayed = s->total_height - 1;
  631. }
  632. }
  633. console_refresh(s);
  634. }
  635. static void console_put_lf(QemuConsole *s)
  636. {
  637. TextCell *c;
  638. int x, y1;
  639. s->y++;
  640. if (s->y >= s->height) {
  641. s->y = s->height - 1;
  642. if (s->y_displayed == s->y_base) {
  643. if (++s->y_displayed == s->total_height)
  644. s->y_displayed = 0;
  645. }
  646. if (++s->y_base == s->total_height)
  647. s->y_base = 0;
  648. if (s->backscroll_height < s->total_height)
  649. s->backscroll_height++;
  650. y1 = (s->y_base + s->height - 1) % s->total_height;
  651. c = &s->cells[y1 * s->width];
  652. for(x = 0; x < s->width; x++) {
  653. c->ch = ' ';
  654. c->t_attrib = s->t_attrib_default;
  655. c++;
  656. }
  657. if (s == active_console && s->y_displayed == s->y_base) {
  658. if (!ds_get_bits_per_pixel(s->ds)) {
  659. s->text_x[0] = 0;
  660. s->text_y[0] = 0;
  661. s->text_x[1] = s->width - 1;
  662. s->text_y[1] = s->height - 1;
  663. return;
  664. }
  665. vga_bitblt(s->ds, 0, FONT_HEIGHT, 0, 0,
  666. s->width * FONT_WIDTH,
  667. (s->height - 1) * FONT_HEIGHT);
  668. vga_fill_rect(s->ds, 0, (s->height - 1) * FONT_HEIGHT,
  669. s->width * FONT_WIDTH, FONT_HEIGHT,
  670. color_table[0][s->t_attrib_default.bgcol]);
  671. s->update_x0 = 0;
  672. s->update_y0 = 0;
  673. s->update_x1 = s->width * FONT_WIDTH;
  674. s->update_y1 = s->height * FONT_HEIGHT;
  675. }
  676. }
  677. }
  678. /* Set console attributes depending on the current escape codes.
  679. * NOTE: I know this code is not very efficient (checking every color for it
  680. * self) but it is more readable and better maintainable.
  681. */
  682. static void console_handle_escape(QemuConsole *s)
  683. {
  684. int i;
  685. for (i=0; i<s->nb_esc_params; i++) {
  686. switch (s->esc_params[i]) {
  687. case 0: /* reset all console attributes to default */
  688. s->t_attrib = s->t_attrib_default;
  689. break;
  690. case 1:
  691. s->t_attrib.bold = 1;
  692. break;
  693. case 4:
  694. s->t_attrib.uline = 1;
  695. break;
  696. case 5:
  697. s->t_attrib.blink = 1;
  698. break;
  699. case 7:
  700. s->t_attrib.invers = 1;
  701. break;
  702. case 8:
  703. s->t_attrib.unvisible = 1;
  704. break;
  705. case 22:
  706. s->t_attrib.bold = 0;
  707. break;
  708. case 24:
  709. s->t_attrib.uline = 0;
  710. break;
  711. case 25:
  712. s->t_attrib.blink = 0;
  713. break;
  714. case 27:
  715. s->t_attrib.invers = 0;
  716. break;
  717. case 28:
  718. s->t_attrib.unvisible = 0;
  719. break;
  720. /* set foreground color */
  721. case 30:
  722. s->t_attrib.fgcol=COLOR_BLACK;
  723. break;
  724. case 31:
  725. s->t_attrib.fgcol=COLOR_RED;
  726. break;
  727. case 32:
  728. s->t_attrib.fgcol=COLOR_GREEN;
  729. break;
  730. case 33:
  731. s->t_attrib.fgcol=COLOR_YELLOW;
  732. break;
  733. case 34:
  734. s->t_attrib.fgcol=COLOR_BLUE;
  735. break;
  736. case 35:
  737. s->t_attrib.fgcol=COLOR_MAGENTA;
  738. break;
  739. case 36:
  740. s->t_attrib.fgcol=COLOR_CYAN;
  741. break;
  742. case 37:
  743. s->t_attrib.fgcol=COLOR_WHITE;
  744. break;
  745. /* set background color */
  746. case 40:
  747. s->t_attrib.bgcol=COLOR_BLACK;
  748. break;
  749. case 41:
  750. s->t_attrib.bgcol=COLOR_RED;
  751. break;
  752. case 42:
  753. s->t_attrib.bgcol=COLOR_GREEN;
  754. break;
  755. case 43:
  756. s->t_attrib.bgcol=COLOR_YELLOW;
  757. break;
  758. case 44:
  759. s->t_attrib.bgcol=COLOR_BLUE;
  760. break;
  761. case 45:
  762. s->t_attrib.bgcol=COLOR_MAGENTA;
  763. break;
  764. case 46:
  765. s->t_attrib.bgcol=COLOR_CYAN;
  766. break;
  767. case 47:
  768. s->t_attrib.bgcol=COLOR_WHITE;
  769. break;
  770. }
  771. }
  772. }
  773. static void console_clear_xy(QemuConsole *s, int x, int y)
  774. {
  775. int y1 = (s->y_base + y) % s->total_height;
  776. TextCell *c = &s->cells[y1 * s->width + x];
  777. c->ch = ' ';
  778. c->t_attrib = s->t_attrib_default;
  779. update_xy(s, x, y);
  780. }
  781. /* set cursor, checking bounds */
  782. static void set_cursor(QemuConsole *s, int x, int y)
  783. {
  784. if (x < 0) {
  785. x = 0;
  786. }
  787. if (y < 0) {
  788. y = 0;
  789. }
  790. if (y >= s->height) {
  791. y = s->height - 1;
  792. }
  793. if (x >= s->width) {
  794. x = s->width - 1;
  795. }
  796. s->x = x;
  797. s->y = y;
  798. }
  799. static void console_putchar(QemuConsole *s, int ch)
  800. {
  801. TextCell *c;
  802. int y1, i;
  803. int x, y;
  804. switch(s->state) {
  805. case TTY_STATE_NORM:
  806. switch(ch) {
  807. case '\r': /* carriage return */
  808. s->x = 0;
  809. break;
  810. case '\n': /* newline */
  811. console_put_lf(s);
  812. break;
  813. case '\b': /* backspace */
  814. if (s->x > 0)
  815. s->x--;
  816. break;
  817. case '\t': /* tabspace */
  818. if (s->x + (8 - (s->x % 8)) > s->width) {
  819. s->x = 0;
  820. console_put_lf(s);
  821. } else {
  822. s->x = s->x + (8 - (s->x % 8));
  823. }
  824. break;
  825. case '\a': /* alert aka. bell */
  826. /* TODO: has to be implemented */
  827. break;
  828. case 14:
  829. /* SI (shift in), character set 0 (ignored) */
  830. break;
  831. case 15:
  832. /* SO (shift out), character set 1 (ignored) */
  833. break;
  834. case 27: /* esc (introducing an escape sequence) */
  835. s->state = TTY_STATE_ESC;
  836. break;
  837. default:
  838. if (s->x >= s->width) {
  839. /* line wrap */
  840. s->x = 0;
  841. console_put_lf(s);
  842. }
  843. y1 = (s->y_base + s->y) % s->total_height;
  844. c = &s->cells[y1 * s->width + s->x];
  845. c->ch = ch;
  846. c->t_attrib = s->t_attrib;
  847. update_xy(s, s->x, s->y);
  848. s->x++;
  849. break;
  850. }
  851. break;
  852. case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
  853. if (ch == '[') {
  854. for(i=0;i<MAX_ESC_PARAMS;i++)
  855. s->esc_params[i] = 0;
  856. s->nb_esc_params = 0;
  857. s->state = TTY_STATE_CSI;
  858. } else {
  859. s->state = TTY_STATE_NORM;
  860. }
  861. break;
  862. case TTY_STATE_CSI: /* handle escape sequence parameters */
  863. if (ch >= '0' && ch <= '9') {
  864. if (s->nb_esc_params < MAX_ESC_PARAMS) {
  865. int *param = &s->esc_params[s->nb_esc_params];
  866. int digit = (ch - '0');
  867. *param = (*param <= (INT_MAX - digit) / 10) ?
  868. *param * 10 + digit : INT_MAX;
  869. }
  870. } else {
  871. if (s->nb_esc_params < MAX_ESC_PARAMS)
  872. s->nb_esc_params++;
  873. if (ch == ';')
  874. break;
  875. #ifdef DEBUG_CONSOLE
  876. fprintf(stderr, "escape sequence CSI%d;%d%c, %d parameters\n",
  877. s->esc_params[0], s->esc_params[1], ch, s->nb_esc_params);
  878. #endif
  879. s->state = TTY_STATE_NORM;
  880. switch(ch) {
  881. case 'A':
  882. /* move cursor up */
  883. if (s->esc_params[0] == 0) {
  884. s->esc_params[0] = 1;
  885. }
  886. set_cursor(s, s->x, s->y - s->esc_params[0]);
  887. break;
  888. case 'B':
  889. /* move cursor down */
  890. if (s->esc_params[0] == 0) {
  891. s->esc_params[0] = 1;
  892. }
  893. set_cursor(s, s->x, s->y + s->esc_params[0]);
  894. break;
  895. case 'C':
  896. /* move cursor right */
  897. if (s->esc_params[0] == 0) {
  898. s->esc_params[0] = 1;
  899. }
  900. set_cursor(s, s->x + s->esc_params[0], s->y);
  901. break;
  902. case 'D':
  903. /* move cursor left */
  904. if (s->esc_params[0] == 0) {
  905. s->esc_params[0] = 1;
  906. }
  907. set_cursor(s, s->x - s->esc_params[0], s->y);
  908. break;
  909. case 'G':
  910. /* move cursor to column */
  911. set_cursor(s, s->esc_params[0] - 1, s->y);
  912. break;
  913. case 'f':
  914. case 'H':
  915. /* move cursor to row, column */
  916. set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
  917. break;
  918. case 'J':
  919. switch (s->esc_params[0]) {
  920. case 0:
  921. /* clear to end of screen */
  922. for (y = s->y; y < s->height; y++) {
  923. for (x = 0; x < s->width; x++) {
  924. if (y == s->y && x < s->x) {
  925. continue;
  926. }
  927. console_clear_xy(s, x, y);
  928. }
  929. }
  930. break;
  931. case 1:
  932. /* clear from beginning of screen */
  933. for (y = 0; y <= s->y; y++) {
  934. for (x = 0; x < s->width; x++) {
  935. if (y == s->y && x > s->x) {
  936. break;
  937. }
  938. console_clear_xy(s, x, y);
  939. }
  940. }
  941. break;
  942. case 2:
  943. /* clear entire screen */
  944. for (y = 0; y <= s->height; y++) {
  945. for (x = 0; x < s->width; x++) {
  946. console_clear_xy(s, x, y);
  947. }
  948. }
  949. break;
  950. }
  951. break;
  952. case 'K':
  953. switch (s->esc_params[0]) {
  954. case 0:
  955. /* clear to eol */
  956. for(x = s->x; x < s->width; x++) {
  957. console_clear_xy(s, x, s->y);
  958. }
  959. break;
  960. case 1:
  961. /* clear from beginning of line */
  962. for (x = 0; x <= s->x; x++) {
  963. console_clear_xy(s, x, s->y);
  964. }
  965. break;
  966. case 2:
  967. /* clear entire line */
  968. for(x = 0; x < s->width; x++) {
  969. console_clear_xy(s, x, s->y);
  970. }
  971. break;
  972. }
  973. break;
  974. case 'm':
  975. console_handle_escape(s);
  976. break;
  977. case 'n':
  978. /* report cursor position */
  979. /* TODO: send ESC[row;colR */
  980. break;
  981. case 's':
  982. /* save cursor position */
  983. s->x_saved = s->x;
  984. s->y_saved = s->y;
  985. break;
  986. case 'u':
  987. /* restore cursor position */
  988. s->x = s->x_saved;
  989. s->y = s->y_saved;
  990. break;
  991. default:
  992. #ifdef DEBUG_CONSOLE
  993. fprintf(stderr, "unhandled escape character '%c'\n", ch);
  994. #endif
  995. break;
  996. }
  997. break;
  998. }
  999. }
  1000. }
  1001. void console_select(unsigned int index)
  1002. {
  1003. QemuConsole *s;
  1004. if (index >= MAX_CONSOLES)
  1005. return;
  1006. if (active_console) {
  1007. active_console->g_width = ds_get_width(active_console->ds);
  1008. active_console->g_height = ds_get_height(active_console->ds);
  1009. }
  1010. s = consoles[index];
  1011. if (s) {
  1012. DisplayState *ds = s->ds;
  1013. if (active_console && active_console->cursor_timer) {
  1014. qemu_del_timer(active_console->cursor_timer);
  1015. }
  1016. active_console = s;
  1017. if (ds->have_gfx) {
  1018. ds->surface = qemu_resize_displaysurface(ds, s->g_width, s->g_height);
  1019. dpy_gfx_resize(ds);
  1020. }
  1021. if (ds->have_text) {
  1022. dpy_text_resize(ds, s->width, s->height);
  1023. }
  1024. if (s->cursor_timer) {
  1025. qemu_mod_timer(s->cursor_timer,
  1026. qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
  1027. }
  1028. vga_hw_invalidate();
  1029. }
  1030. }
  1031. static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
  1032. {
  1033. QemuConsole *s = chr->opaque;
  1034. int i;
  1035. s->update_x0 = s->width * FONT_WIDTH;
  1036. s->update_y0 = s->height * FONT_HEIGHT;
  1037. s->update_x1 = 0;
  1038. s->update_y1 = 0;
  1039. console_show_cursor(s, 0);
  1040. for(i = 0; i < len; i++) {
  1041. console_putchar(s, buf[i]);
  1042. }
  1043. console_show_cursor(s, 1);
  1044. if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
  1045. dpy_gfx_update(s->ds, s->update_x0, s->update_y0,
  1046. s->update_x1 - s->update_x0,
  1047. s->update_y1 - s->update_y0);
  1048. }
  1049. return len;
  1050. }
  1051. static void kbd_send_chars(void *opaque)
  1052. {
  1053. QemuConsole *s = opaque;
  1054. int len;
  1055. uint8_t buf[16];
  1056. len = qemu_chr_be_can_write(s->chr);
  1057. if (len > s->out_fifo.count)
  1058. len = s->out_fifo.count;
  1059. if (len > 0) {
  1060. if (len > sizeof(buf))
  1061. len = sizeof(buf);
  1062. qemu_fifo_read(&s->out_fifo, buf, len);
  1063. qemu_chr_be_write(s->chr, buf, len);
  1064. }
  1065. /* characters are pending: we send them a bit later (XXX:
  1066. horrible, should change char device API) */
  1067. if (s->out_fifo.count > 0) {
  1068. qemu_mod_timer(s->kbd_timer, qemu_get_clock_ms(rt_clock) + 1);
  1069. }
  1070. }
  1071. /* called when an ascii key is pressed */
  1072. void kbd_put_keysym(int keysym)
  1073. {
  1074. QemuConsole *s;
  1075. uint8_t buf[16], *q;
  1076. int c;
  1077. s = active_console;
  1078. if (!s || (s->console_type == GRAPHIC_CONSOLE))
  1079. return;
  1080. switch(keysym) {
  1081. case QEMU_KEY_CTRL_UP:
  1082. console_scroll(-1);
  1083. break;
  1084. case QEMU_KEY_CTRL_DOWN:
  1085. console_scroll(1);
  1086. break;
  1087. case QEMU_KEY_CTRL_PAGEUP:
  1088. console_scroll(-10);
  1089. break;
  1090. case QEMU_KEY_CTRL_PAGEDOWN:
  1091. console_scroll(10);
  1092. break;
  1093. default:
  1094. /* convert the QEMU keysym to VT100 key string */
  1095. q = buf;
  1096. if (keysym >= 0xe100 && keysym <= 0xe11f) {
  1097. *q++ = '\033';
  1098. *q++ = '[';
  1099. c = keysym - 0xe100;
  1100. if (c >= 10)
  1101. *q++ = '0' + (c / 10);
  1102. *q++ = '0' + (c % 10);
  1103. *q++ = '~';
  1104. } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
  1105. *q++ = '\033';
  1106. *q++ = '[';
  1107. *q++ = keysym & 0xff;
  1108. } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
  1109. console_puts(s->chr, (const uint8_t *) "\r", 1);
  1110. *q++ = '\n';
  1111. } else {
  1112. *q++ = keysym;
  1113. }
  1114. if (s->echo) {
  1115. console_puts(s->chr, buf, q - buf);
  1116. }
  1117. if (s->chr->chr_read) {
  1118. qemu_fifo_write(&s->out_fifo, buf, q - buf);
  1119. kbd_send_chars(s);
  1120. }
  1121. break;
  1122. }
  1123. }
  1124. static void text_console_invalidate(void *opaque)
  1125. {
  1126. QemuConsole *s = (QemuConsole *) opaque;
  1127. if (!ds_get_bits_per_pixel(s->ds) && s->console_type == TEXT_CONSOLE) {
  1128. s->g_width = ds_get_width(s->ds);
  1129. s->g_height = ds_get_height(s->ds);
  1130. text_console_resize(s);
  1131. }
  1132. console_refresh(s);
  1133. }
  1134. static void text_console_update(void *opaque, console_ch_t *chardata)
  1135. {
  1136. QemuConsole *s = (QemuConsole *) opaque;
  1137. int i, j, src;
  1138. if (s->text_x[0] <= s->text_x[1]) {
  1139. src = (s->y_base + s->text_y[0]) * s->width;
  1140. chardata += s->text_y[0] * s->width;
  1141. for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
  1142. for (j = 0; j < s->width; j ++, src ++)
  1143. console_write_ch(chardata ++, s->cells[src].ch |
  1144. (s->cells[src].t_attrib.fgcol << 12) |
  1145. (s->cells[src].t_attrib.bgcol << 8) |
  1146. (s->cells[src].t_attrib.bold << 21));
  1147. dpy_text_update(s->ds, s->text_x[0], s->text_y[0],
  1148. s->text_x[1] - s->text_x[0], i - s->text_y[0]);
  1149. s->text_x[0] = s->width;
  1150. s->text_y[0] = s->height;
  1151. s->text_x[1] = 0;
  1152. s->text_y[1] = 0;
  1153. }
  1154. if (s->cursor_invalidate) {
  1155. dpy_text_cursor(s->ds, s->x, s->y);
  1156. s->cursor_invalidate = 0;
  1157. }
  1158. }
  1159. static QemuConsole *get_graphic_console(DisplayState *ds)
  1160. {
  1161. int i;
  1162. QemuConsole *s;
  1163. for (i = 0; i < nb_consoles; i++) {
  1164. s = consoles[i];
  1165. if (s->console_type == GRAPHIC_CONSOLE && s->ds == ds)
  1166. return s;
  1167. }
  1168. return NULL;
  1169. }
  1170. static QemuConsole *new_console(DisplayState *ds, console_type_t console_type)
  1171. {
  1172. QemuConsole *s;
  1173. int i;
  1174. if (nb_consoles >= MAX_CONSOLES)
  1175. return NULL;
  1176. s = g_malloc0(sizeof(QemuConsole));
  1177. if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
  1178. (console_type == GRAPHIC_CONSOLE))) {
  1179. active_console = s;
  1180. }
  1181. s->ds = ds;
  1182. s->console_type = console_type;
  1183. if (console_type != GRAPHIC_CONSOLE) {
  1184. s->index = nb_consoles;
  1185. consoles[nb_consoles++] = s;
  1186. } else {
  1187. /* HACK: Put graphical consoles before text consoles. */
  1188. for (i = nb_consoles; i > 0; i--) {
  1189. if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
  1190. break;
  1191. consoles[i] = consoles[i - 1];
  1192. consoles[i]->index = i;
  1193. }
  1194. s->index = i;
  1195. consoles[i] = s;
  1196. nb_consoles++;
  1197. }
  1198. return s;
  1199. }
  1200. static void qemu_alloc_display(DisplaySurface *surface, int width, int height,
  1201. int linesize, PixelFormat pf, int newflags)
  1202. {
  1203. surface->pf = pf;
  1204. qemu_pixman_image_unref(surface->image);
  1205. surface->image = NULL;
  1206. surface->format = qemu_pixman_get_format(&pf);
  1207. assert(surface->format != 0);
  1208. surface->image = pixman_image_create_bits(surface->format,
  1209. width, height,
  1210. NULL, linesize);
  1211. assert(surface->image != NULL);
  1212. surface->flags = newflags | QEMU_ALLOCATED_FLAG;
  1213. #ifdef HOST_WORDS_BIGENDIAN
  1214. surface->flags |= QEMU_BIG_ENDIAN_FLAG;
  1215. #endif
  1216. }
  1217. DisplaySurface *qemu_create_displaysurface(DisplayState *ds,
  1218. int width, int height)
  1219. {
  1220. DisplaySurface *surface = g_new0(DisplaySurface, 1);
  1221. int linesize = width * 4;
  1222. qemu_alloc_display(surface, width, height, linesize,
  1223. qemu_default_pixelformat(32), 0);
  1224. return surface;
  1225. }
  1226. DisplaySurface *qemu_resize_displaysurface(DisplayState *ds,
  1227. int width, int height)
  1228. {
  1229. int linesize = width * 4;
  1230. trace_displaysurface_resize(ds, ds->surface, width, height);
  1231. qemu_alloc_display(ds->surface, width, height, linesize,
  1232. qemu_default_pixelformat(32), 0);
  1233. return ds->surface;
  1234. }
  1235. DisplaySurface *qemu_create_displaysurface_from(int width, int height, int bpp,
  1236. int linesize, uint8_t *data,
  1237. bool byteswap)
  1238. {
  1239. DisplaySurface *surface = g_new0(DisplaySurface, 1);
  1240. if (byteswap) {
  1241. surface->pf = qemu_different_endianness_pixelformat(bpp);
  1242. } else {
  1243. surface->pf = qemu_default_pixelformat(bpp);
  1244. }
  1245. surface->format = qemu_pixman_get_format(&surface->pf);
  1246. assert(surface->format != 0);
  1247. surface->image = pixman_image_create_bits(surface->format,
  1248. width, height,
  1249. (void *)data, linesize);
  1250. assert(surface->image != NULL);
  1251. #ifdef HOST_WORDS_BIGENDIAN
  1252. surface->flags = QEMU_BIG_ENDIAN_FLAG;
  1253. #endif
  1254. return surface;
  1255. }
  1256. void qemu_free_displaysurface(DisplayState *ds)
  1257. {
  1258. trace_displaysurface_free(ds, ds->surface);
  1259. if (ds->surface == NULL) {
  1260. return;
  1261. }
  1262. qemu_pixman_image_unref(ds->surface->image);
  1263. g_free(ds->surface);
  1264. }
  1265. static void dumb_display_init(void)
  1266. {
  1267. DisplayState *ds = g_malloc0(sizeof(DisplayState));
  1268. int width = 640;
  1269. int height = 480;
  1270. if (is_fixedsize_console()) {
  1271. width = active_console->g_width;
  1272. height = active_console->g_height;
  1273. }
  1274. ds->surface = qemu_create_displaysurface(ds, width, height);
  1275. register_displaystate(ds);
  1276. }
  1277. /***********************************************************/
  1278. /* register display */
  1279. void register_displaystate(DisplayState *ds)
  1280. {
  1281. DisplayState **s;
  1282. s = &display_state;
  1283. while (*s != NULL)
  1284. s = &(*s)->next;
  1285. ds->next = NULL;
  1286. *s = ds;
  1287. }
  1288. DisplayState *get_displaystate(void)
  1289. {
  1290. if (!display_state) {
  1291. dumb_display_init ();
  1292. }
  1293. return display_state;
  1294. }
  1295. DisplayState *graphic_console_init(vga_hw_update_ptr update,
  1296. vga_hw_invalidate_ptr invalidate,
  1297. vga_hw_screen_dump_ptr screen_dump,
  1298. vga_hw_text_update_ptr text_update,
  1299. void *opaque)
  1300. {
  1301. QemuConsole *s;
  1302. DisplayState *ds;
  1303. ds = (DisplayState *) g_malloc0(sizeof(DisplayState));
  1304. ds->surface = qemu_create_displaysurface(ds, 640, 480);
  1305. s = new_console(ds, GRAPHIC_CONSOLE);
  1306. if (s == NULL) {
  1307. qemu_free_displaysurface(ds);
  1308. g_free(ds);
  1309. return NULL;
  1310. }
  1311. s->hw_update = update;
  1312. s->hw_invalidate = invalidate;
  1313. s->hw_screen_dump = screen_dump;
  1314. s->hw_text_update = text_update;
  1315. s->hw = opaque;
  1316. register_displaystate(ds);
  1317. return ds;
  1318. }
  1319. int is_graphic_console(void)
  1320. {
  1321. return active_console && active_console->console_type == GRAPHIC_CONSOLE;
  1322. }
  1323. int is_fixedsize_console(void)
  1324. {
  1325. return active_console && active_console->console_type != TEXT_CONSOLE;
  1326. }
  1327. void console_color_init(DisplayState *ds)
  1328. {
  1329. int i, j;
  1330. for (j = 0; j < 2; j++) {
  1331. for (i = 0; i < 8; i++) {
  1332. color_table[j][i] = col_expand(ds,
  1333. vga_get_color(ds, color_table_rgb[j][i]));
  1334. }
  1335. }
  1336. }
  1337. static void text_console_set_echo(CharDriverState *chr, bool echo)
  1338. {
  1339. QemuConsole *s = chr->opaque;
  1340. s->echo = echo;
  1341. }
  1342. static void text_console_update_cursor(void *opaque)
  1343. {
  1344. QemuConsole *s = opaque;
  1345. s->cursor_visible_phase = !s->cursor_visible_phase;
  1346. vga_hw_invalidate();
  1347. qemu_mod_timer(s->cursor_timer,
  1348. qemu_get_clock_ms(rt_clock) + CONSOLE_CURSOR_PERIOD / 2);
  1349. }
  1350. static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
  1351. {
  1352. QemuConsole *s;
  1353. static int color_inited;
  1354. s = chr->opaque;
  1355. chr->chr_write = console_puts;
  1356. s->out_fifo.buf = s->out_fifo_buf;
  1357. s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
  1358. s->kbd_timer = qemu_new_timer_ms(rt_clock, kbd_send_chars, s);
  1359. s->ds = ds;
  1360. if (!color_inited) {
  1361. color_inited = 1;
  1362. console_color_init(s->ds);
  1363. }
  1364. s->y_displayed = 0;
  1365. s->y_base = 0;
  1366. s->total_height = DEFAULT_BACKSCROLL;
  1367. s->x = 0;
  1368. s->y = 0;
  1369. if (s->console_type == TEXT_CONSOLE) {
  1370. s->g_width = ds_get_width(s->ds);
  1371. s->g_height = ds_get_height(s->ds);
  1372. }
  1373. s->cursor_timer =
  1374. qemu_new_timer_ms(rt_clock, text_console_update_cursor, s);
  1375. s->hw_invalidate = text_console_invalidate;
  1376. s->hw_text_update = text_console_update;
  1377. s->hw = s;
  1378. /* Set text attribute defaults */
  1379. s->t_attrib_default.bold = 0;
  1380. s->t_attrib_default.uline = 0;
  1381. s->t_attrib_default.blink = 0;
  1382. s->t_attrib_default.invers = 0;
  1383. s->t_attrib_default.unvisible = 0;
  1384. s->t_attrib_default.fgcol = COLOR_WHITE;
  1385. s->t_attrib_default.bgcol = COLOR_BLACK;
  1386. /* set current text attributes to default */
  1387. s->t_attrib = s->t_attrib_default;
  1388. text_console_resize(s);
  1389. if (chr->label) {
  1390. char msg[128];
  1391. int len;
  1392. s->t_attrib.bgcol = COLOR_BLUE;
  1393. len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
  1394. console_puts(chr, (uint8_t*)msg, len);
  1395. s->t_attrib = s->t_attrib_default;
  1396. }
  1397. qemu_chr_generic_open(chr);
  1398. if (chr->init)
  1399. chr->init(chr);
  1400. }
  1401. CharDriverState *text_console_init(QemuOpts *opts)
  1402. {
  1403. CharDriverState *chr;
  1404. QemuConsole *s;
  1405. unsigned width;
  1406. unsigned height;
  1407. chr = g_malloc0(sizeof(CharDriverState));
  1408. width = qemu_opt_get_number(opts, "width", 0);
  1409. if (width == 0)
  1410. width = qemu_opt_get_number(opts, "cols", 0) * FONT_WIDTH;
  1411. height = qemu_opt_get_number(opts, "height", 0);
  1412. if (height == 0)
  1413. height = qemu_opt_get_number(opts, "rows", 0) * FONT_HEIGHT;
  1414. if (width == 0 || height == 0) {
  1415. s = new_console(NULL, TEXT_CONSOLE);
  1416. } else {
  1417. s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE);
  1418. }
  1419. if (!s) {
  1420. g_free(chr);
  1421. return NULL;
  1422. }
  1423. s->chr = chr;
  1424. s->g_width = width;
  1425. s->g_height = height;
  1426. chr->opaque = s;
  1427. chr->chr_set_echo = text_console_set_echo;
  1428. return chr;
  1429. }
  1430. void text_consoles_set_display(DisplayState *ds)
  1431. {
  1432. int i;
  1433. for (i = 0; i < nb_consoles; i++) {
  1434. if (consoles[i]->console_type != GRAPHIC_CONSOLE) {
  1435. text_console_do_init(consoles[i]->chr, ds);
  1436. }
  1437. }
  1438. }
  1439. void qemu_console_resize(DisplayState *ds, int width, int height)
  1440. {
  1441. QemuConsole *s = get_graphic_console(ds);
  1442. if (!s) return;
  1443. s->g_width = width;
  1444. s->g_height = height;
  1445. if (is_graphic_console()) {
  1446. ds->surface = qemu_resize_displaysurface(ds, width, height);
  1447. dpy_gfx_resize(ds);
  1448. }
  1449. }
  1450. void qemu_console_copy(DisplayState *ds, int src_x, int src_y,
  1451. int dst_x, int dst_y, int w, int h)
  1452. {
  1453. if (is_graphic_console()) {
  1454. dpy_gfx_copy(ds, src_x, src_y, dst_x, dst_y, w, h);
  1455. }
  1456. }
  1457. PixelFormat qemu_different_endianness_pixelformat(int bpp)
  1458. {
  1459. PixelFormat pf;
  1460. memset(&pf, 0x00, sizeof(PixelFormat));
  1461. pf.bits_per_pixel = bpp;
  1462. pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
  1463. pf.depth = bpp == 32 ? 24 : bpp;
  1464. switch (bpp) {
  1465. case 24:
  1466. pf.rmask = 0x000000FF;
  1467. pf.gmask = 0x0000FF00;
  1468. pf.bmask = 0x00FF0000;
  1469. pf.rmax = 255;
  1470. pf.gmax = 255;
  1471. pf.bmax = 255;
  1472. pf.rshift = 0;
  1473. pf.gshift = 8;
  1474. pf.bshift = 16;
  1475. pf.rbits = 8;
  1476. pf.gbits = 8;
  1477. pf.bbits = 8;
  1478. break;
  1479. case 32:
  1480. pf.rmask = 0x0000FF00;
  1481. pf.gmask = 0x00FF0000;
  1482. pf.bmask = 0xFF000000;
  1483. pf.amask = 0x00000000;
  1484. pf.amax = 255;
  1485. pf.rmax = 255;
  1486. pf.gmax = 255;
  1487. pf.bmax = 255;
  1488. pf.ashift = 0;
  1489. pf.rshift = 8;
  1490. pf.gshift = 16;
  1491. pf.bshift = 24;
  1492. pf.rbits = 8;
  1493. pf.gbits = 8;
  1494. pf.bbits = 8;
  1495. pf.abits = 8;
  1496. break;
  1497. default:
  1498. break;
  1499. }
  1500. return pf;
  1501. }
  1502. PixelFormat qemu_default_pixelformat(int bpp)
  1503. {
  1504. PixelFormat pf;
  1505. memset(&pf, 0x00, sizeof(PixelFormat));
  1506. pf.bits_per_pixel = bpp;
  1507. pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
  1508. pf.depth = bpp == 32 ? 24 : bpp;
  1509. switch (bpp) {
  1510. case 15:
  1511. pf.bits_per_pixel = 16;
  1512. pf.rmask = 0x00007c00;
  1513. pf.gmask = 0x000003E0;
  1514. pf.bmask = 0x0000001F;
  1515. pf.rmax = 31;
  1516. pf.gmax = 31;
  1517. pf.bmax = 31;
  1518. pf.rshift = 10;
  1519. pf.gshift = 5;
  1520. pf.bshift = 0;
  1521. pf.rbits = 5;
  1522. pf.gbits = 5;
  1523. pf.bbits = 5;
  1524. break;
  1525. case 16:
  1526. pf.rmask = 0x0000F800;
  1527. pf.gmask = 0x000007E0;
  1528. pf.bmask = 0x0000001F;
  1529. pf.rmax = 31;
  1530. pf.gmax = 63;
  1531. pf.bmax = 31;
  1532. pf.rshift = 11;
  1533. pf.gshift = 5;
  1534. pf.bshift = 0;
  1535. pf.rbits = 5;
  1536. pf.gbits = 6;
  1537. pf.bbits = 5;
  1538. break;
  1539. case 24:
  1540. pf.rmask = 0x00FF0000;
  1541. pf.gmask = 0x0000FF00;
  1542. pf.bmask = 0x000000FF;
  1543. pf.rmax = 255;
  1544. pf.gmax = 255;
  1545. pf.bmax = 255;
  1546. pf.rshift = 16;
  1547. pf.gshift = 8;
  1548. pf.bshift = 0;
  1549. pf.rbits = 8;
  1550. pf.gbits = 8;
  1551. pf.bbits = 8;
  1552. break;
  1553. case 32:
  1554. pf.rmask = 0x00FF0000;
  1555. pf.gmask = 0x0000FF00;
  1556. pf.bmask = 0x000000FF;
  1557. pf.rmax = 255;
  1558. pf.gmax = 255;
  1559. pf.bmax = 255;
  1560. pf.rshift = 16;
  1561. pf.gshift = 8;
  1562. pf.bshift = 0;
  1563. pf.rbits = 8;
  1564. pf.gbits = 8;
  1565. pf.bbits = 8;
  1566. break;
  1567. default:
  1568. break;
  1569. }
  1570. return pf;
  1571. }