2
0

console.c 47 KB

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