2
0

console.c 47 KB

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