2
0

console.c 47 KB

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