2
0

console.c 45 KB

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