console.c 48 KB

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