2
0

console.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047
  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 "ui/console.h"
  26. #include "hw/qdev-core.h"
  27. #include "qemu/timer.h"
  28. #include "qmp-commands.h"
  29. #include "sysemu/char.h"
  30. #include "trace.h"
  31. #define DEFAULT_BACKSCROLL 512
  32. #define MAX_CONSOLES 12
  33. #define CONSOLE_CURSOR_PERIOD 500
  34. typedef struct TextAttributes {
  35. uint8_t fgcol:4;
  36. uint8_t bgcol:4;
  37. uint8_t bold:1;
  38. uint8_t uline:1;
  39. uint8_t blink:1;
  40. uint8_t invers:1;
  41. uint8_t unvisible:1;
  42. } TextAttributes;
  43. typedef struct TextCell {
  44. uint8_t ch;
  45. TextAttributes t_attrib;
  46. } TextCell;
  47. #define MAX_ESC_PARAMS 3
  48. enum TTYState {
  49. TTY_STATE_NORM,
  50. TTY_STATE_ESC,
  51. TTY_STATE_CSI,
  52. };
  53. typedef struct QEMUFIFO {
  54. uint8_t *buf;
  55. int buf_size;
  56. int count, wptr, rptr;
  57. } QEMUFIFO;
  58. static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1)
  59. {
  60. int l, len;
  61. l = f->buf_size - f->count;
  62. if (len1 > l)
  63. len1 = l;
  64. len = len1;
  65. while (len > 0) {
  66. l = f->buf_size - f->wptr;
  67. if (l > len)
  68. l = len;
  69. memcpy(f->buf + f->wptr, buf, l);
  70. f->wptr += l;
  71. if (f->wptr >= f->buf_size)
  72. f->wptr = 0;
  73. buf += l;
  74. len -= l;
  75. }
  76. f->count += len1;
  77. return len1;
  78. }
  79. static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1)
  80. {
  81. int l, len;
  82. if (len1 > f->count)
  83. len1 = f->count;
  84. len = len1;
  85. while (len > 0) {
  86. l = f->buf_size - f->rptr;
  87. if (l > len)
  88. l = len;
  89. memcpy(buf, f->buf + f->rptr, l);
  90. f->rptr += l;
  91. if (f->rptr >= f->buf_size)
  92. f->rptr = 0;
  93. buf += l;
  94. len -= l;
  95. }
  96. f->count -= len1;
  97. return len1;
  98. }
  99. typedef enum {
  100. GRAPHIC_CONSOLE,
  101. TEXT_CONSOLE,
  102. TEXT_CONSOLE_FIXED_SIZE
  103. } console_type_t;
  104. struct QemuConsole {
  105. Object parent;
  106. int index;
  107. console_type_t console_type;
  108. DisplayState *ds;
  109. DisplaySurface *surface;
  110. int dcls;
  111. /* Graphic console state. */
  112. Object *device;
  113. uint32_t head;
  114. QemuUIInfo ui_info;
  115. const GraphicHwOps *hw_ops;
  116. void *hw;
  117. /* Text console state */
  118. int width;
  119. int height;
  120. int total_height;
  121. int backscroll_height;
  122. int x, y;
  123. int x_saved, y_saved;
  124. int y_displayed;
  125. int y_base;
  126. TextAttributes t_attrib_default; /* default text attributes */
  127. TextAttributes t_attrib; /* currently active text attributes */
  128. TextCell *cells;
  129. int text_x[2], text_y[2], cursor_invalidate;
  130. int echo;
  131. bool cursor_visible_phase;
  132. QEMUTimer *cursor_timer;
  133. int update_x0;
  134. int update_y0;
  135. int update_x1;
  136. int update_y1;
  137. enum TTYState state;
  138. int esc_params[MAX_ESC_PARAMS];
  139. int nb_esc_params;
  140. CharDriverState *chr;
  141. /* fifo for key pressed */
  142. QEMUFIFO out_fifo;
  143. uint8_t out_fifo_buf[16];
  144. QEMUTimer *kbd_timer;
  145. };
  146. struct DisplayState {
  147. QEMUTimer *gui_timer;
  148. uint64_t last_update;
  149. uint64_t update_interval;
  150. bool refreshing;
  151. bool have_gfx;
  152. bool have_text;
  153. QLIST_HEAD(, DisplayChangeListener) listeners;
  154. };
  155. static DisplayState *display_state;
  156. static QemuConsole *active_console;
  157. static QemuConsole *consoles[MAX_CONSOLES];
  158. static int nb_consoles = 0;
  159. static void text_console_do_init(CharDriverState *chr, DisplayState *ds);
  160. static void dpy_refresh(DisplayState *s);
  161. static DisplayState *get_alloc_displaystate(void);
  162. static void gui_update(void *opaque)
  163. {
  164. uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
  165. uint64_t dcl_interval;
  166. DisplayState *ds = opaque;
  167. DisplayChangeListener *dcl;
  168. int i;
  169. ds->refreshing = true;
  170. dpy_refresh(ds);
  171. ds->refreshing = false;
  172. QLIST_FOREACH(dcl, &ds->listeners, next) {
  173. dcl_interval = dcl->update_interval ?
  174. dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
  175. if (interval > dcl_interval) {
  176. interval = dcl_interval;
  177. }
  178. }
  179. if (ds->update_interval != interval) {
  180. ds->update_interval = interval;
  181. for (i = 0; i < nb_consoles; i++) {
  182. if (consoles[i]->hw_ops->update_interval) {
  183. consoles[i]->hw_ops->update_interval(consoles[i]->hw, interval);
  184. }
  185. }
  186. trace_console_refresh(interval);
  187. }
  188. ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  189. timer_mod(ds->gui_timer, ds->last_update + interval);
  190. }
  191. static void gui_setup_refresh(DisplayState *ds)
  192. {
  193. DisplayChangeListener *dcl;
  194. bool need_timer = false;
  195. bool have_gfx = false;
  196. bool have_text = false;
  197. QLIST_FOREACH(dcl, &ds->listeners, next) {
  198. if (dcl->ops->dpy_refresh != NULL) {
  199. need_timer = true;
  200. }
  201. if (dcl->ops->dpy_gfx_update != NULL) {
  202. have_gfx = true;
  203. }
  204. if (dcl->ops->dpy_text_update != NULL) {
  205. have_text = true;
  206. }
  207. }
  208. if (need_timer && ds->gui_timer == NULL) {
  209. ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
  210. timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
  211. }
  212. if (!need_timer && ds->gui_timer != NULL) {
  213. timer_del(ds->gui_timer);
  214. timer_free(ds->gui_timer);
  215. ds->gui_timer = NULL;
  216. }
  217. ds->have_gfx = have_gfx;
  218. ds->have_text = have_text;
  219. }
  220. void graphic_hw_update(QemuConsole *con)
  221. {
  222. if (!con) {
  223. con = active_console;
  224. }
  225. if (con && con->hw_ops->gfx_update) {
  226. con->hw_ops->gfx_update(con->hw);
  227. }
  228. }
  229. void graphic_hw_invalidate(QemuConsole *con)
  230. {
  231. if (!con) {
  232. con = active_console;
  233. }
  234. if (con && con->hw_ops->invalidate) {
  235. con->hw_ops->invalidate(con->hw);
  236. }
  237. }
  238. static void ppm_save(const char *filename, struct DisplaySurface *ds,
  239. Error **errp)
  240. {
  241. int width = pixman_image_get_width(ds->image);
  242. int height = pixman_image_get_height(ds->image);
  243. int fd;
  244. FILE *f;
  245. int y;
  246. int ret;
  247. pixman_image_t *linebuf;
  248. trace_ppm_save(filename, ds);
  249. fd = qemu_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
  250. if (fd == -1) {
  251. error_setg(errp, "failed to open file '%s': %s", filename,
  252. strerror(errno));
  253. return;
  254. }
  255. f = fdopen(fd, "wb");
  256. ret = fprintf(f, "P6\n%d %d\n%d\n", width, height, 255);
  257. if (ret < 0) {
  258. linebuf = NULL;
  259. goto write_err;
  260. }
  261. linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
  262. for (y = 0; y < height; y++) {
  263. qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
  264. clearerr(f);
  265. ret = fwrite(pixman_image_get_data(linebuf), 1,
  266. pixman_image_get_stride(linebuf), f);
  267. (void)ret;
  268. if (ferror(f)) {
  269. goto write_err;
  270. }
  271. }
  272. out:
  273. qemu_pixman_image_unref(linebuf);
  274. fclose(f);
  275. return;
  276. write_err:
  277. error_setg(errp, "failed to write to file '%s': %s", filename,
  278. strerror(errno));
  279. unlink(filename);
  280. goto out;
  281. }
  282. void qmp_screendump(const char *filename, Error **errp)
  283. {
  284. QemuConsole *con = qemu_console_lookup_by_index(0);
  285. DisplaySurface *surface;
  286. if (con == NULL) {
  287. error_setg(errp, "There is no QemuConsole I can screendump from.");
  288. return;
  289. }
  290. graphic_hw_update(con);
  291. surface = qemu_console_surface(con);
  292. ppm_save(filename, surface, errp);
  293. }
  294. void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
  295. {
  296. if (!con) {
  297. con = active_console;
  298. }
  299. if (con && con->hw_ops->text_update) {
  300. con->hw_ops->text_update(con->hw, chardata);
  301. }
  302. }
  303. static void vga_fill_rect(QemuConsole *con,
  304. int posx, int posy, int width, int height,
  305. pixman_color_t color)
  306. {
  307. DisplaySurface *surface = qemu_console_surface(con);
  308. pixman_rectangle16_t rect = {
  309. .x = posx, .y = posy, .width = width, .height = height
  310. };
  311. pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
  312. &color, 1, &rect);
  313. }
  314. /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
  315. static void vga_bitblt(QemuConsole *con,
  316. int xs, int ys, int xd, int yd, int w, int h)
  317. {
  318. DisplaySurface *surface = qemu_console_surface(con);
  319. pixman_image_composite(PIXMAN_OP_SRC,
  320. surface->image, NULL, surface->image,
  321. xs, ys, 0, 0, xd, yd, w, h);
  322. }
  323. /***********************************************************/
  324. /* basic char display */
  325. #define FONT_HEIGHT 16
  326. #define FONT_WIDTH 8
  327. #include "vgafont.h"
  328. #ifndef CONFIG_CURSES
  329. enum color_names {
  330. COLOR_BLACK = 0,
  331. COLOR_RED = 1,
  332. COLOR_GREEN = 2,
  333. COLOR_YELLOW = 3,
  334. COLOR_BLUE = 4,
  335. COLOR_MAGENTA = 5,
  336. COLOR_CYAN = 6,
  337. COLOR_WHITE = 7
  338. };
  339. #endif
  340. #define QEMU_RGB(r, g, b) \
  341. { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
  342. static const pixman_color_t color_table_rgb[2][8] = {
  343. { /* dark */
  344. QEMU_RGB(0x00, 0x00, 0x00), /* black */
  345. QEMU_RGB(0xaa, 0x00, 0x00), /* red */
  346. QEMU_RGB(0x00, 0xaa, 0x00), /* green */
  347. QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
  348. QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
  349. QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
  350. QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
  351. QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
  352. },
  353. { /* bright */
  354. QEMU_RGB(0x00, 0x00, 0x00), /* black */
  355. QEMU_RGB(0xff, 0x00, 0x00), /* red */
  356. QEMU_RGB(0x00, 0xff, 0x00), /* green */
  357. QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
  358. QEMU_RGB(0x00, 0x00, 0xff), /* blue */
  359. QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
  360. QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
  361. QEMU_RGB(0xff, 0xff, 0xff), /* white */
  362. }
  363. };
  364. static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
  365. TextAttributes *t_attrib)
  366. {
  367. static pixman_image_t *glyphs[256];
  368. DisplaySurface *surface = qemu_console_surface(s);
  369. pixman_color_t fgcol, bgcol;
  370. if (t_attrib->invers) {
  371. bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
  372. fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
  373. } else {
  374. fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
  375. bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
  376. }
  377. if (!glyphs[ch]) {
  378. glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
  379. }
  380. qemu_pixman_glyph_render(glyphs[ch], surface->image,
  381. &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
  382. }
  383. static void text_console_resize(QemuConsole *s)
  384. {
  385. TextCell *cells, *c, *c1;
  386. int w1, x, y, last_width;
  387. last_width = s->width;
  388. s->width = surface_width(s->surface) / FONT_WIDTH;
  389. s->height = surface_height(s->surface) / FONT_HEIGHT;
  390. w1 = last_width;
  391. if (s->width < w1)
  392. w1 = s->width;
  393. cells = g_malloc(s->width * s->total_height * sizeof(TextCell));
  394. for(y = 0; y < s->total_height; y++) {
  395. c = &cells[y * s->width];
  396. if (w1 > 0) {
  397. c1 = &s->cells[y * last_width];
  398. for(x = 0; x < w1; x++) {
  399. *c++ = *c1++;
  400. }
  401. }
  402. for(x = w1; x < s->width; x++) {
  403. c->ch = ' ';
  404. c->t_attrib = s->t_attrib_default;
  405. c++;
  406. }
  407. }
  408. g_free(s->cells);
  409. s->cells = cells;
  410. }
  411. static inline void text_update_xy(QemuConsole *s, int x, int y)
  412. {
  413. s->text_x[0] = MIN(s->text_x[0], x);
  414. s->text_x[1] = MAX(s->text_x[1], x);
  415. s->text_y[0] = MIN(s->text_y[0], y);
  416. s->text_y[1] = MAX(s->text_y[1], y);
  417. }
  418. static void invalidate_xy(QemuConsole *s, int x, int y)
  419. {
  420. if (s->update_x0 > x * FONT_WIDTH)
  421. s->update_x0 = x * FONT_WIDTH;
  422. if (s->update_y0 > y * FONT_HEIGHT)
  423. s->update_y0 = y * FONT_HEIGHT;
  424. if (s->update_x1 < (x + 1) * FONT_WIDTH)
  425. s->update_x1 = (x + 1) * FONT_WIDTH;
  426. if (s->update_y1 < (y + 1) * FONT_HEIGHT)
  427. s->update_y1 = (y + 1) * FONT_HEIGHT;
  428. }
  429. static void update_xy(QemuConsole *s, int x, int y)
  430. {
  431. TextCell *c;
  432. int y1, y2;
  433. if (!qemu_console_is_visible(s)) {
  434. return;
  435. }
  436. if (s->ds->have_text) {
  437. text_update_xy(s, x, y);
  438. }
  439. if (s->ds->have_gfx) {
  440. y1 = (s->y_base + y) % s->total_height;
  441. y2 = y1 - s->y_displayed;
  442. if (y2 < 0)
  443. y2 += s->total_height;
  444. if (y2 < s->height) {
  445. c = &s->cells[y1 * s->width + x];
  446. vga_putcharxy(s, x, y2, c->ch,
  447. &(c->t_attrib));
  448. invalidate_xy(s, x, y2);
  449. }
  450. }
  451. }
  452. static void console_show_cursor(QemuConsole *s, int show)
  453. {
  454. TextCell *c;
  455. int y, y1;
  456. int x = s->x;
  457. if (!qemu_console_is_visible(s)) {
  458. return;
  459. }
  460. if (s->ds->have_text) {
  461. s->cursor_invalidate = 1;
  462. }
  463. if (s->ds->have_gfx) {
  464. if (x >= s->width) {
  465. x = s->width - 1;
  466. }
  467. y1 = (s->y_base + s->y) % s->total_height;
  468. y = y1 - s->y_displayed;
  469. if (y < 0)
  470. y += s->total_height;
  471. if (y < s->height) {
  472. c = &s->cells[y1 * s->width + x];
  473. if (show && s->cursor_visible_phase) {
  474. TextAttributes t_attrib = s->t_attrib_default;
  475. t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
  476. vga_putcharxy(s, x, y, c->ch, &t_attrib);
  477. } else {
  478. vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
  479. }
  480. invalidate_xy(s, x, y);
  481. }
  482. }
  483. }
  484. static void console_refresh(QemuConsole *s)
  485. {
  486. DisplaySurface *surface = qemu_console_surface(s);
  487. TextCell *c;
  488. int x, y, y1;
  489. if (!qemu_console_is_visible(s)) {
  490. return;
  491. }
  492. if (s->ds->have_text) {
  493. s->text_x[0] = 0;
  494. s->text_y[0] = 0;
  495. s->text_x[1] = s->width - 1;
  496. s->text_y[1] = s->height - 1;
  497. s->cursor_invalidate = 1;
  498. }
  499. if (s->ds->have_gfx) {
  500. vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
  501. color_table_rgb[0][COLOR_BLACK]);
  502. y1 = s->y_displayed;
  503. for (y = 0; y < s->height; y++) {
  504. c = s->cells + y1 * s->width;
  505. for (x = 0; x < s->width; x++) {
  506. vga_putcharxy(s, x, y, c->ch,
  507. &(c->t_attrib));
  508. c++;
  509. }
  510. if (++y1 == s->total_height) {
  511. y1 = 0;
  512. }
  513. }
  514. console_show_cursor(s, 1);
  515. dpy_gfx_update(s, 0, 0,
  516. surface_width(surface), surface_height(surface));
  517. }
  518. }
  519. static void console_scroll(QemuConsole *s, int ydelta)
  520. {
  521. int i, y1;
  522. if (ydelta > 0) {
  523. for(i = 0; i < ydelta; i++) {
  524. if (s->y_displayed == s->y_base)
  525. break;
  526. if (++s->y_displayed == s->total_height)
  527. s->y_displayed = 0;
  528. }
  529. } else {
  530. ydelta = -ydelta;
  531. i = s->backscroll_height;
  532. if (i > s->total_height - s->height)
  533. i = s->total_height - s->height;
  534. y1 = s->y_base - i;
  535. if (y1 < 0)
  536. y1 += s->total_height;
  537. for(i = 0; i < ydelta; i++) {
  538. if (s->y_displayed == y1)
  539. break;
  540. if (--s->y_displayed < 0)
  541. s->y_displayed = s->total_height - 1;
  542. }
  543. }
  544. console_refresh(s);
  545. }
  546. static void console_put_lf(QemuConsole *s)
  547. {
  548. TextCell *c;
  549. int x, y1;
  550. s->y++;
  551. if (s->y >= s->height) {
  552. s->y = s->height - 1;
  553. if (s->y_displayed == s->y_base) {
  554. if (++s->y_displayed == s->total_height)
  555. s->y_displayed = 0;
  556. }
  557. if (++s->y_base == s->total_height)
  558. s->y_base = 0;
  559. if (s->backscroll_height < s->total_height)
  560. s->backscroll_height++;
  561. y1 = (s->y_base + s->height - 1) % s->total_height;
  562. c = &s->cells[y1 * s->width];
  563. for(x = 0; x < s->width; x++) {
  564. c->ch = ' ';
  565. c->t_attrib = s->t_attrib_default;
  566. c++;
  567. }
  568. if (qemu_console_is_visible(s) && s->y_displayed == s->y_base) {
  569. if (s->ds->have_text) {
  570. s->text_x[0] = 0;
  571. s->text_y[0] = 0;
  572. s->text_x[1] = s->width - 1;
  573. s->text_y[1] = s->height - 1;
  574. }
  575. if (s->ds->have_gfx) {
  576. vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
  577. s->width * FONT_WIDTH,
  578. (s->height - 1) * FONT_HEIGHT);
  579. vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
  580. s->width * FONT_WIDTH, FONT_HEIGHT,
  581. color_table_rgb[0][s->t_attrib_default.bgcol]);
  582. s->update_x0 = 0;
  583. s->update_y0 = 0;
  584. s->update_x1 = s->width * FONT_WIDTH;
  585. s->update_y1 = s->height * FONT_HEIGHT;
  586. }
  587. }
  588. }
  589. }
  590. /* Set console attributes depending on the current escape codes.
  591. * NOTE: I know this code is not very efficient (checking every color for it
  592. * self) but it is more readable and better maintainable.
  593. */
  594. static void console_handle_escape(QemuConsole *s)
  595. {
  596. int i;
  597. for (i=0; i<s->nb_esc_params; i++) {
  598. switch (s->esc_params[i]) {
  599. case 0: /* reset all console attributes to default */
  600. s->t_attrib = s->t_attrib_default;
  601. break;
  602. case 1:
  603. s->t_attrib.bold = 1;
  604. break;
  605. case 4:
  606. s->t_attrib.uline = 1;
  607. break;
  608. case 5:
  609. s->t_attrib.blink = 1;
  610. break;
  611. case 7:
  612. s->t_attrib.invers = 1;
  613. break;
  614. case 8:
  615. s->t_attrib.unvisible = 1;
  616. break;
  617. case 22:
  618. s->t_attrib.bold = 0;
  619. break;
  620. case 24:
  621. s->t_attrib.uline = 0;
  622. break;
  623. case 25:
  624. s->t_attrib.blink = 0;
  625. break;
  626. case 27:
  627. s->t_attrib.invers = 0;
  628. break;
  629. case 28:
  630. s->t_attrib.unvisible = 0;
  631. break;
  632. /* set foreground color */
  633. case 30:
  634. s->t_attrib.fgcol=COLOR_BLACK;
  635. break;
  636. case 31:
  637. s->t_attrib.fgcol=COLOR_RED;
  638. break;
  639. case 32:
  640. s->t_attrib.fgcol=COLOR_GREEN;
  641. break;
  642. case 33:
  643. s->t_attrib.fgcol=COLOR_YELLOW;
  644. break;
  645. case 34:
  646. s->t_attrib.fgcol=COLOR_BLUE;
  647. break;
  648. case 35:
  649. s->t_attrib.fgcol=COLOR_MAGENTA;
  650. break;
  651. case 36:
  652. s->t_attrib.fgcol=COLOR_CYAN;
  653. break;
  654. case 37:
  655. s->t_attrib.fgcol=COLOR_WHITE;
  656. break;
  657. /* set background color */
  658. case 40:
  659. s->t_attrib.bgcol=COLOR_BLACK;
  660. break;
  661. case 41:
  662. s->t_attrib.bgcol=COLOR_RED;
  663. break;
  664. case 42:
  665. s->t_attrib.bgcol=COLOR_GREEN;
  666. break;
  667. case 43:
  668. s->t_attrib.bgcol=COLOR_YELLOW;
  669. break;
  670. case 44:
  671. s->t_attrib.bgcol=COLOR_BLUE;
  672. break;
  673. case 45:
  674. s->t_attrib.bgcol=COLOR_MAGENTA;
  675. break;
  676. case 46:
  677. s->t_attrib.bgcol=COLOR_CYAN;
  678. break;
  679. case 47:
  680. s->t_attrib.bgcol=COLOR_WHITE;
  681. break;
  682. }
  683. }
  684. }
  685. static void console_clear_xy(QemuConsole *s, int x, int y)
  686. {
  687. int y1 = (s->y_base + y) % s->total_height;
  688. TextCell *c = &s->cells[y1 * s->width + x];
  689. c->ch = ' ';
  690. c->t_attrib = s->t_attrib_default;
  691. update_xy(s, x, y);
  692. }
  693. /* set cursor, checking bounds */
  694. static void set_cursor(QemuConsole *s, int x, int y)
  695. {
  696. if (x < 0) {
  697. x = 0;
  698. }
  699. if (y < 0) {
  700. y = 0;
  701. }
  702. if (y >= s->height) {
  703. y = s->height - 1;
  704. }
  705. if (x >= s->width) {
  706. x = s->width - 1;
  707. }
  708. s->x = x;
  709. s->y = y;
  710. }
  711. static void console_putchar(QemuConsole *s, int ch)
  712. {
  713. TextCell *c;
  714. int y1, i;
  715. int x, y;
  716. switch(s->state) {
  717. case TTY_STATE_NORM:
  718. switch(ch) {
  719. case '\r': /* carriage return */
  720. s->x = 0;
  721. break;
  722. case '\n': /* newline */
  723. console_put_lf(s);
  724. break;
  725. case '\b': /* backspace */
  726. if (s->x > 0)
  727. s->x--;
  728. break;
  729. case '\t': /* tabspace */
  730. if (s->x + (8 - (s->x % 8)) > s->width) {
  731. s->x = 0;
  732. console_put_lf(s);
  733. } else {
  734. s->x = s->x + (8 - (s->x % 8));
  735. }
  736. break;
  737. case '\a': /* alert aka. bell */
  738. /* TODO: has to be implemented */
  739. break;
  740. case 14:
  741. /* SI (shift in), character set 0 (ignored) */
  742. break;
  743. case 15:
  744. /* SO (shift out), character set 1 (ignored) */
  745. break;
  746. case 27: /* esc (introducing an escape sequence) */
  747. s->state = TTY_STATE_ESC;
  748. break;
  749. default:
  750. if (s->x >= s->width) {
  751. /* line wrap */
  752. s->x = 0;
  753. console_put_lf(s);
  754. }
  755. y1 = (s->y_base + s->y) % s->total_height;
  756. c = &s->cells[y1 * s->width + s->x];
  757. c->ch = ch;
  758. c->t_attrib = s->t_attrib;
  759. update_xy(s, s->x, s->y);
  760. s->x++;
  761. break;
  762. }
  763. break;
  764. case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
  765. if (ch == '[') {
  766. for(i=0;i<MAX_ESC_PARAMS;i++)
  767. s->esc_params[i] = 0;
  768. s->nb_esc_params = 0;
  769. s->state = TTY_STATE_CSI;
  770. } else {
  771. s->state = TTY_STATE_NORM;
  772. }
  773. break;
  774. case TTY_STATE_CSI: /* handle escape sequence parameters */
  775. if (ch >= '0' && ch <= '9') {
  776. if (s->nb_esc_params < MAX_ESC_PARAMS) {
  777. int *param = &s->esc_params[s->nb_esc_params];
  778. int digit = (ch - '0');
  779. *param = (*param <= (INT_MAX - digit) / 10) ?
  780. *param * 10 + digit : INT_MAX;
  781. }
  782. } else {
  783. if (s->nb_esc_params < MAX_ESC_PARAMS)
  784. s->nb_esc_params++;
  785. if (ch == ';')
  786. break;
  787. trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
  788. ch, s->nb_esc_params);
  789. s->state = TTY_STATE_NORM;
  790. switch(ch) {
  791. case 'A':
  792. /* move cursor up */
  793. if (s->esc_params[0] == 0) {
  794. s->esc_params[0] = 1;
  795. }
  796. set_cursor(s, s->x, s->y - s->esc_params[0]);
  797. break;
  798. case 'B':
  799. /* move cursor down */
  800. if (s->esc_params[0] == 0) {
  801. s->esc_params[0] = 1;
  802. }
  803. set_cursor(s, s->x, s->y + s->esc_params[0]);
  804. break;
  805. case 'C':
  806. /* move cursor right */
  807. if (s->esc_params[0] == 0) {
  808. s->esc_params[0] = 1;
  809. }
  810. set_cursor(s, s->x + s->esc_params[0], s->y);
  811. break;
  812. case 'D':
  813. /* move cursor left */
  814. if (s->esc_params[0] == 0) {
  815. s->esc_params[0] = 1;
  816. }
  817. set_cursor(s, s->x - s->esc_params[0], s->y);
  818. break;
  819. case 'G':
  820. /* move cursor to column */
  821. set_cursor(s, s->esc_params[0] - 1, s->y);
  822. break;
  823. case 'f':
  824. case 'H':
  825. /* move cursor to row, column */
  826. set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
  827. break;
  828. case 'J':
  829. switch (s->esc_params[0]) {
  830. case 0:
  831. /* clear to end of screen */
  832. for (y = s->y; y < s->height; y++) {
  833. for (x = 0; x < s->width; x++) {
  834. if (y == s->y && x < s->x) {
  835. continue;
  836. }
  837. console_clear_xy(s, x, y);
  838. }
  839. }
  840. break;
  841. case 1:
  842. /* clear from beginning of screen */
  843. for (y = 0; y <= s->y; y++) {
  844. for (x = 0; x < s->width; x++) {
  845. if (y == s->y && x > s->x) {
  846. break;
  847. }
  848. console_clear_xy(s, x, y);
  849. }
  850. }
  851. break;
  852. case 2:
  853. /* clear entire screen */
  854. for (y = 0; y <= s->height; y++) {
  855. for (x = 0; x < s->width; x++) {
  856. console_clear_xy(s, x, y);
  857. }
  858. }
  859. break;
  860. }
  861. break;
  862. case 'K':
  863. switch (s->esc_params[0]) {
  864. case 0:
  865. /* clear to eol */
  866. for(x = s->x; x < s->width; x++) {
  867. console_clear_xy(s, x, s->y);
  868. }
  869. break;
  870. case 1:
  871. /* clear from beginning of line */
  872. for (x = 0; x <= s->x; x++) {
  873. console_clear_xy(s, x, s->y);
  874. }
  875. break;
  876. case 2:
  877. /* clear entire line */
  878. for(x = 0; x < s->width; x++) {
  879. console_clear_xy(s, x, s->y);
  880. }
  881. break;
  882. }
  883. break;
  884. case 'm':
  885. console_handle_escape(s);
  886. break;
  887. case 'n':
  888. /* report cursor position */
  889. /* TODO: send ESC[row;colR */
  890. break;
  891. case 's':
  892. /* save cursor position */
  893. s->x_saved = s->x;
  894. s->y_saved = s->y;
  895. break;
  896. case 'u':
  897. /* restore cursor position */
  898. s->x = s->x_saved;
  899. s->y = s->y_saved;
  900. break;
  901. default:
  902. trace_console_putchar_unhandled(ch);
  903. break;
  904. }
  905. break;
  906. }
  907. }
  908. }
  909. void console_select(unsigned int index)
  910. {
  911. DisplayChangeListener *dcl;
  912. QemuConsole *s;
  913. if (index >= MAX_CONSOLES)
  914. return;
  915. trace_console_select(index);
  916. s = qemu_console_lookup_by_index(index);
  917. if (s) {
  918. DisplayState *ds = s->ds;
  919. if (active_console && active_console->cursor_timer) {
  920. timer_del(active_console->cursor_timer);
  921. }
  922. active_console = s;
  923. if (ds->have_gfx) {
  924. QLIST_FOREACH(dcl, &ds->listeners, next) {
  925. if (dcl->con != NULL) {
  926. continue;
  927. }
  928. if (dcl->ops->dpy_gfx_switch) {
  929. dcl->ops->dpy_gfx_switch(dcl, s->surface);
  930. }
  931. }
  932. dpy_gfx_update(s, 0, 0, surface_width(s->surface),
  933. surface_height(s->surface));
  934. }
  935. if (ds->have_text) {
  936. dpy_text_resize(s, s->width, s->height);
  937. }
  938. if (s->cursor_timer) {
  939. timer_mod(s->cursor_timer,
  940. qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + CONSOLE_CURSOR_PERIOD / 2);
  941. }
  942. }
  943. }
  944. static int console_puts(CharDriverState *chr, const uint8_t *buf, int len)
  945. {
  946. QemuConsole *s = chr->opaque;
  947. int i;
  948. s->update_x0 = s->width * FONT_WIDTH;
  949. s->update_y0 = s->height * FONT_HEIGHT;
  950. s->update_x1 = 0;
  951. s->update_y1 = 0;
  952. console_show_cursor(s, 0);
  953. for(i = 0; i < len; i++) {
  954. console_putchar(s, buf[i]);
  955. }
  956. console_show_cursor(s, 1);
  957. if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
  958. dpy_gfx_update(s, s->update_x0, s->update_y0,
  959. s->update_x1 - s->update_x0,
  960. s->update_y1 - s->update_y0);
  961. }
  962. return len;
  963. }
  964. static void kbd_send_chars(void *opaque)
  965. {
  966. QemuConsole *s = opaque;
  967. int len;
  968. uint8_t buf[16];
  969. len = qemu_chr_be_can_write(s->chr);
  970. if (len > s->out_fifo.count)
  971. len = s->out_fifo.count;
  972. if (len > 0) {
  973. if (len > sizeof(buf))
  974. len = sizeof(buf);
  975. qemu_fifo_read(&s->out_fifo, buf, len);
  976. qemu_chr_be_write(s->chr, buf, len);
  977. }
  978. /* characters are pending: we send them a bit later (XXX:
  979. horrible, should change char device API) */
  980. if (s->out_fifo.count > 0) {
  981. timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
  982. }
  983. }
  984. /* called when an ascii key is pressed */
  985. void kbd_put_keysym(int keysym)
  986. {
  987. QemuConsole *s;
  988. uint8_t buf[16], *q;
  989. int c;
  990. s = active_console;
  991. if (!s || (s->console_type == GRAPHIC_CONSOLE))
  992. return;
  993. switch(keysym) {
  994. case QEMU_KEY_CTRL_UP:
  995. console_scroll(s, -1);
  996. break;
  997. case QEMU_KEY_CTRL_DOWN:
  998. console_scroll(s, 1);
  999. break;
  1000. case QEMU_KEY_CTRL_PAGEUP:
  1001. console_scroll(s, -10);
  1002. break;
  1003. case QEMU_KEY_CTRL_PAGEDOWN:
  1004. console_scroll(s, 10);
  1005. break;
  1006. default:
  1007. /* convert the QEMU keysym to VT100 key string */
  1008. q = buf;
  1009. if (keysym >= 0xe100 && keysym <= 0xe11f) {
  1010. *q++ = '\033';
  1011. *q++ = '[';
  1012. c = keysym - 0xe100;
  1013. if (c >= 10)
  1014. *q++ = '0' + (c / 10);
  1015. *q++ = '0' + (c % 10);
  1016. *q++ = '~';
  1017. } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
  1018. *q++ = '\033';
  1019. *q++ = '[';
  1020. *q++ = keysym & 0xff;
  1021. } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
  1022. console_puts(s->chr, (const uint8_t *) "\r", 1);
  1023. *q++ = '\n';
  1024. } else {
  1025. *q++ = keysym;
  1026. }
  1027. if (s->echo) {
  1028. console_puts(s->chr, buf, q - buf);
  1029. }
  1030. if (s->chr->chr_read) {
  1031. qemu_fifo_write(&s->out_fifo, buf, q - buf);
  1032. kbd_send_chars(s);
  1033. }
  1034. break;
  1035. }
  1036. }
  1037. static void text_console_invalidate(void *opaque)
  1038. {
  1039. QemuConsole *s = (QemuConsole *) opaque;
  1040. if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
  1041. text_console_resize(s);
  1042. }
  1043. console_refresh(s);
  1044. }
  1045. static void text_console_update(void *opaque, console_ch_t *chardata)
  1046. {
  1047. QemuConsole *s = (QemuConsole *) opaque;
  1048. int i, j, src;
  1049. if (s->text_x[0] <= s->text_x[1]) {
  1050. src = (s->y_base + s->text_y[0]) * s->width;
  1051. chardata += s->text_y[0] * s->width;
  1052. for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
  1053. for (j = 0; j < s->width; j ++, src ++)
  1054. console_write_ch(chardata ++, s->cells[src].ch |
  1055. (s->cells[src].t_attrib.fgcol << 12) |
  1056. (s->cells[src].t_attrib.bgcol << 8) |
  1057. (s->cells[src].t_attrib.bold << 21));
  1058. dpy_text_update(s, s->text_x[0], s->text_y[0],
  1059. s->text_x[1] - s->text_x[0], i - s->text_y[0]);
  1060. s->text_x[0] = s->width;
  1061. s->text_y[0] = s->height;
  1062. s->text_x[1] = 0;
  1063. s->text_y[1] = 0;
  1064. }
  1065. if (s->cursor_invalidate) {
  1066. dpy_text_cursor(s, s->x, s->y);
  1067. s->cursor_invalidate = 0;
  1068. }
  1069. }
  1070. static QemuConsole *new_console(DisplayState *ds, console_type_t console_type)
  1071. {
  1072. Error *local_err = NULL;
  1073. Object *obj;
  1074. QemuConsole *s;
  1075. int i;
  1076. if (nb_consoles >= MAX_CONSOLES)
  1077. return NULL;
  1078. obj = object_new(TYPE_QEMU_CONSOLE);
  1079. s = QEMU_CONSOLE(obj);
  1080. object_property_add_link(obj, "device", TYPE_DEVICE,
  1081. (Object **)&s->device,
  1082. object_property_allow_set_link,
  1083. OBJ_PROP_LINK_UNREF_ON_RELEASE,
  1084. &local_err);
  1085. object_property_add_uint32_ptr(obj, "head",
  1086. &s->head, &local_err);
  1087. if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
  1088. (console_type == GRAPHIC_CONSOLE))) {
  1089. active_console = s;
  1090. }
  1091. s->ds = ds;
  1092. s->console_type = console_type;
  1093. if (console_type != GRAPHIC_CONSOLE) {
  1094. s->index = nb_consoles;
  1095. consoles[nb_consoles++] = s;
  1096. } else {
  1097. /* HACK: Put graphical consoles before text consoles. */
  1098. for (i = nb_consoles; i > 0; i--) {
  1099. if (consoles[i - 1]->console_type == GRAPHIC_CONSOLE)
  1100. break;
  1101. consoles[i] = consoles[i - 1];
  1102. consoles[i]->index = i;
  1103. }
  1104. s->index = i;
  1105. consoles[i] = s;
  1106. nb_consoles++;
  1107. }
  1108. return s;
  1109. }
  1110. static void qemu_alloc_display(DisplaySurface *surface, int width, int height,
  1111. int linesize, PixelFormat pf, int newflags)
  1112. {
  1113. surface->pf = pf;
  1114. qemu_pixman_image_unref(surface->image);
  1115. surface->image = NULL;
  1116. surface->format = qemu_pixman_get_format(&pf);
  1117. assert(surface->format != 0);
  1118. surface->image = pixman_image_create_bits(surface->format,
  1119. width, height,
  1120. NULL, linesize);
  1121. assert(surface->image != NULL);
  1122. surface->flags = newflags | QEMU_ALLOCATED_FLAG;
  1123. #ifdef HOST_WORDS_BIGENDIAN
  1124. surface->flags |= QEMU_BIG_ENDIAN_FLAG;
  1125. #endif
  1126. }
  1127. DisplaySurface *qemu_create_displaysurface(int width, int height)
  1128. {
  1129. DisplaySurface *surface = g_new0(DisplaySurface, 1);
  1130. int linesize = width * 4;
  1131. trace_displaysurface_create(surface, width, height);
  1132. qemu_alloc_display(surface, width, height, linesize,
  1133. qemu_default_pixelformat(32), 0);
  1134. return surface;
  1135. }
  1136. DisplaySurface *qemu_create_displaysurface_from(int width, int height, int bpp,
  1137. int linesize, uint8_t *data,
  1138. bool byteswap)
  1139. {
  1140. DisplaySurface *surface = g_new0(DisplaySurface, 1);
  1141. trace_displaysurface_create_from(surface, width, height, bpp, byteswap);
  1142. if (byteswap) {
  1143. surface->pf = qemu_different_endianness_pixelformat(bpp);
  1144. } else {
  1145. surface->pf = qemu_default_pixelformat(bpp);
  1146. }
  1147. surface->format = qemu_pixman_get_format(&surface->pf);
  1148. assert(surface->format != 0);
  1149. surface->image = pixman_image_create_bits(surface->format,
  1150. width, height,
  1151. (void *)data, linesize);
  1152. assert(surface->image != NULL);
  1153. #ifdef HOST_WORDS_BIGENDIAN
  1154. surface->flags = QEMU_BIG_ENDIAN_FLAG;
  1155. #endif
  1156. return surface;
  1157. }
  1158. static DisplaySurface *qemu_create_dummy_surface(void)
  1159. {
  1160. static const char msg[] =
  1161. "This VM has no graphic display device.";
  1162. DisplaySurface *surface = qemu_create_displaysurface(640, 480);
  1163. pixman_color_t bg = color_table_rgb[0][COLOR_BLACK];
  1164. pixman_color_t fg = color_table_rgb[0][COLOR_WHITE];
  1165. pixman_image_t *glyph;
  1166. int len, x, y, i;
  1167. len = strlen(msg);
  1168. x = (640/FONT_WIDTH - len) / 2;
  1169. y = (480/FONT_HEIGHT - 1) / 2;
  1170. for (i = 0; i < len; i++) {
  1171. glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
  1172. qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
  1173. x+i, y, FONT_WIDTH, FONT_HEIGHT);
  1174. qemu_pixman_image_unref(glyph);
  1175. }
  1176. return surface;
  1177. }
  1178. void qemu_free_displaysurface(DisplaySurface *surface)
  1179. {
  1180. if (surface == NULL) {
  1181. return;
  1182. }
  1183. trace_displaysurface_free(surface);
  1184. qemu_pixman_image_unref(surface->image);
  1185. g_free(surface);
  1186. }
  1187. void register_displaychangelistener(DisplayChangeListener *dcl)
  1188. {
  1189. static DisplaySurface *dummy;
  1190. QemuConsole *con;
  1191. trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
  1192. dcl->ds = get_alloc_displaystate();
  1193. QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
  1194. gui_setup_refresh(dcl->ds);
  1195. if (dcl->con) {
  1196. dcl->con->dcls++;
  1197. con = dcl->con;
  1198. } else {
  1199. con = active_console;
  1200. }
  1201. if (dcl->ops->dpy_gfx_switch) {
  1202. if (con) {
  1203. dcl->ops->dpy_gfx_switch(dcl, con->surface);
  1204. } else {
  1205. if (!dummy) {
  1206. dummy = qemu_create_dummy_surface();
  1207. }
  1208. dcl->ops->dpy_gfx_switch(dcl, dummy);
  1209. }
  1210. }
  1211. }
  1212. void update_displaychangelistener(DisplayChangeListener *dcl,
  1213. uint64_t interval)
  1214. {
  1215. DisplayState *ds = dcl->ds;
  1216. dcl->update_interval = interval;
  1217. if (!ds->refreshing && ds->update_interval > interval) {
  1218. timer_mod(ds->gui_timer, ds->last_update + interval);
  1219. }
  1220. }
  1221. void unregister_displaychangelistener(DisplayChangeListener *dcl)
  1222. {
  1223. DisplayState *ds = dcl->ds;
  1224. trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
  1225. if (dcl->con) {
  1226. dcl->con->dcls--;
  1227. }
  1228. QLIST_REMOVE(dcl, next);
  1229. gui_setup_refresh(ds);
  1230. }
  1231. int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info)
  1232. {
  1233. assert(con != NULL);
  1234. con->ui_info = *info;
  1235. if (con->hw_ops->ui_info) {
  1236. return con->hw_ops->ui_info(con->hw, con->head, info);
  1237. }
  1238. return -1;
  1239. }
  1240. void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
  1241. {
  1242. DisplayState *s = con->ds;
  1243. DisplayChangeListener *dcl;
  1244. int width = surface_width(con->surface);
  1245. int height = surface_height(con->surface);
  1246. x = MAX(x, 0);
  1247. y = MAX(y, 0);
  1248. x = MIN(x, width);
  1249. y = MIN(y, height);
  1250. w = MIN(w, width - x);
  1251. h = MIN(h, height - y);
  1252. if (!qemu_console_is_visible(con)) {
  1253. return;
  1254. }
  1255. QLIST_FOREACH(dcl, &s->listeners, next) {
  1256. if (con != (dcl->con ? dcl->con : active_console)) {
  1257. continue;
  1258. }
  1259. if (dcl->ops->dpy_gfx_update) {
  1260. dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
  1261. }
  1262. }
  1263. }
  1264. void dpy_gfx_replace_surface(QemuConsole *con,
  1265. DisplaySurface *surface)
  1266. {
  1267. DisplayState *s = con->ds;
  1268. DisplaySurface *old_surface = con->surface;
  1269. DisplayChangeListener *dcl;
  1270. con->surface = surface;
  1271. QLIST_FOREACH(dcl, &s->listeners, next) {
  1272. if (con != (dcl->con ? dcl->con : active_console)) {
  1273. continue;
  1274. }
  1275. if (dcl->ops->dpy_gfx_switch) {
  1276. dcl->ops->dpy_gfx_switch(dcl, surface);
  1277. }
  1278. }
  1279. qemu_free_displaysurface(old_surface);
  1280. }
  1281. void dpy_refresh(DisplayState *s)
  1282. {
  1283. DisplayChangeListener *dcl;
  1284. QLIST_FOREACH(dcl, &s->listeners, next) {
  1285. if (dcl->ops->dpy_refresh) {
  1286. dcl->ops->dpy_refresh(dcl);
  1287. }
  1288. }
  1289. }
  1290. void dpy_gfx_copy(QemuConsole *con, int src_x, int src_y,
  1291. int dst_x, int dst_y, int w, int h)
  1292. {
  1293. DisplayState *s = con->ds;
  1294. DisplayChangeListener *dcl;
  1295. if (!qemu_console_is_visible(con)) {
  1296. return;
  1297. }
  1298. QLIST_FOREACH(dcl, &s->listeners, next) {
  1299. if (con != (dcl->con ? dcl->con : active_console)) {
  1300. continue;
  1301. }
  1302. if (dcl->ops->dpy_gfx_copy) {
  1303. dcl->ops->dpy_gfx_copy(dcl, src_x, src_y, dst_x, dst_y, w, h);
  1304. } else { /* TODO */
  1305. dcl->ops->dpy_gfx_update(dcl, dst_x, dst_y, w, h);
  1306. }
  1307. }
  1308. }
  1309. void dpy_text_cursor(QemuConsole *con, int x, int y)
  1310. {
  1311. DisplayState *s = con->ds;
  1312. DisplayChangeListener *dcl;
  1313. if (!qemu_console_is_visible(con)) {
  1314. return;
  1315. }
  1316. QLIST_FOREACH(dcl, &s->listeners, next) {
  1317. if (con != (dcl->con ? dcl->con : active_console)) {
  1318. continue;
  1319. }
  1320. if (dcl->ops->dpy_text_cursor) {
  1321. dcl->ops->dpy_text_cursor(dcl, x, y);
  1322. }
  1323. }
  1324. }
  1325. void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
  1326. {
  1327. DisplayState *s = con->ds;
  1328. DisplayChangeListener *dcl;
  1329. if (!qemu_console_is_visible(con)) {
  1330. return;
  1331. }
  1332. QLIST_FOREACH(dcl, &s->listeners, next) {
  1333. if (con != (dcl->con ? dcl->con : active_console)) {
  1334. continue;
  1335. }
  1336. if (dcl->ops->dpy_text_update) {
  1337. dcl->ops->dpy_text_update(dcl, x, y, w, h);
  1338. }
  1339. }
  1340. }
  1341. void dpy_text_resize(QemuConsole *con, int w, int h)
  1342. {
  1343. DisplayState *s = con->ds;
  1344. struct DisplayChangeListener *dcl;
  1345. if (!qemu_console_is_visible(con)) {
  1346. return;
  1347. }
  1348. QLIST_FOREACH(dcl, &s->listeners, next) {
  1349. if (con != (dcl->con ? dcl->con : active_console)) {
  1350. continue;
  1351. }
  1352. if (dcl->ops->dpy_text_resize) {
  1353. dcl->ops->dpy_text_resize(dcl, w, h);
  1354. }
  1355. }
  1356. }
  1357. void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
  1358. {
  1359. DisplayState *s = con->ds;
  1360. DisplayChangeListener *dcl;
  1361. if (!qemu_console_is_visible(con)) {
  1362. return;
  1363. }
  1364. QLIST_FOREACH(dcl, &s->listeners, next) {
  1365. if (con != (dcl->con ? dcl->con : active_console)) {
  1366. continue;
  1367. }
  1368. if (dcl->ops->dpy_mouse_set) {
  1369. dcl->ops->dpy_mouse_set(dcl, x, y, on);
  1370. }
  1371. }
  1372. }
  1373. void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
  1374. {
  1375. DisplayState *s = con->ds;
  1376. DisplayChangeListener *dcl;
  1377. if (!qemu_console_is_visible(con)) {
  1378. return;
  1379. }
  1380. QLIST_FOREACH(dcl, &s->listeners, next) {
  1381. if (con != (dcl->con ? dcl->con : active_console)) {
  1382. continue;
  1383. }
  1384. if (dcl->ops->dpy_cursor_define) {
  1385. dcl->ops->dpy_cursor_define(dcl, cursor);
  1386. }
  1387. }
  1388. }
  1389. bool dpy_cursor_define_supported(QemuConsole *con)
  1390. {
  1391. DisplayState *s = con->ds;
  1392. DisplayChangeListener *dcl;
  1393. QLIST_FOREACH(dcl, &s->listeners, next) {
  1394. if (dcl->ops->dpy_cursor_define) {
  1395. return true;
  1396. }
  1397. }
  1398. return false;
  1399. }
  1400. /***********************************************************/
  1401. /* register display */
  1402. /* console.c internal use only */
  1403. static DisplayState *get_alloc_displaystate(void)
  1404. {
  1405. if (!display_state) {
  1406. display_state = g_new0(DisplayState, 1);
  1407. }
  1408. return display_state;
  1409. }
  1410. /*
  1411. * Called by main(), after creating QemuConsoles
  1412. * and before initializing ui (sdl/vnc/...).
  1413. */
  1414. DisplayState *init_displaystate(void)
  1415. {
  1416. Error *local_err = NULL;
  1417. gchar *name;
  1418. int i;
  1419. if (!display_state) {
  1420. display_state = g_new0(DisplayState, 1);
  1421. }
  1422. for (i = 0; i < nb_consoles; i++) {
  1423. if (consoles[i]->console_type != GRAPHIC_CONSOLE &&
  1424. consoles[i]->ds == NULL) {
  1425. text_console_do_init(consoles[i]->chr, display_state);
  1426. }
  1427. /* Hook up into the qom tree here (not in new_console()), once
  1428. * all QemuConsoles are created and the order / numbering
  1429. * doesn't change any more */
  1430. name = g_strdup_printf("console[%d]", i);
  1431. object_property_add_child(container_get(object_get_root(), "/backend"),
  1432. name, OBJECT(consoles[i]), &local_err);
  1433. g_free(name);
  1434. }
  1435. return display_state;
  1436. }
  1437. QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
  1438. const GraphicHwOps *hw_ops,
  1439. void *opaque)
  1440. {
  1441. Error *local_err = NULL;
  1442. int width = 640;
  1443. int height = 480;
  1444. QemuConsole *s;
  1445. DisplayState *ds;
  1446. ds = get_alloc_displaystate();
  1447. trace_console_gfx_new();
  1448. s = new_console(ds, GRAPHIC_CONSOLE);
  1449. s->hw_ops = hw_ops;
  1450. s->hw = opaque;
  1451. if (dev) {
  1452. object_property_set_link(OBJECT(s), OBJECT(dev),
  1453. "device", &local_err);
  1454. object_property_set_int(OBJECT(s), head,
  1455. "head", &local_err);
  1456. }
  1457. s->surface = qemu_create_displaysurface(width, height);
  1458. return s;
  1459. }
  1460. QemuConsole *qemu_console_lookup_by_index(unsigned int index)
  1461. {
  1462. if (index >= MAX_CONSOLES) {
  1463. return NULL;
  1464. }
  1465. return consoles[index];
  1466. }
  1467. QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
  1468. {
  1469. Error *local_err = NULL;
  1470. Object *obj;
  1471. uint32_t h;
  1472. int i;
  1473. for (i = 0; i < nb_consoles; i++) {
  1474. if (!consoles[i]) {
  1475. continue;
  1476. }
  1477. obj = object_property_get_link(OBJECT(consoles[i]),
  1478. "device", &local_err);
  1479. if (DEVICE(obj) != dev) {
  1480. continue;
  1481. }
  1482. h = object_property_get_int(OBJECT(consoles[i]),
  1483. "head", &local_err);
  1484. if (h != head) {
  1485. continue;
  1486. }
  1487. return consoles[i];
  1488. }
  1489. return NULL;
  1490. }
  1491. bool qemu_console_is_visible(QemuConsole *con)
  1492. {
  1493. return (con == active_console) || (con->dcls > 0);
  1494. }
  1495. bool qemu_console_is_graphic(QemuConsole *con)
  1496. {
  1497. if (con == NULL) {
  1498. con = active_console;
  1499. }
  1500. return con && (con->console_type == GRAPHIC_CONSOLE);
  1501. }
  1502. bool qemu_console_is_fixedsize(QemuConsole *con)
  1503. {
  1504. if (con == NULL) {
  1505. con = active_console;
  1506. }
  1507. return con && (con->console_type != TEXT_CONSOLE);
  1508. }
  1509. int qemu_console_get_index(QemuConsole *con)
  1510. {
  1511. if (con == NULL) {
  1512. con = active_console;
  1513. }
  1514. return con ? con->index : -1;
  1515. }
  1516. uint32_t qemu_console_get_head(QemuConsole *con)
  1517. {
  1518. if (con == NULL) {
  1519. con = active_console;
  1520. }
  1521. return con ? con->head : -1;
  1522. }
  1523. QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con)
  1524. {
  1525. assert(con != NULL);
  1526. return &con->ui_info;
  1527. }
  1528. int qemu_console_get_width(QemuConsole *con, int fallback)
  1529. {
  1530. if (con == NULL) {
  1531. con = active_console;
  1532. }
  1533. return con ? surface_width(con->surface) : fallback;
  1534. }
  1535. int qemu_console_get_height(QemuConsole *con, int fallback)
  1536. {
  1537. if (con == NULL) {
  1538. con = active_console;
  1539. }
  1540. return con ? surface_height(con->surface) : fallback;
  1541. }
  1542. static void text_console_set_echo(CharDriverState *chr, bool echo)
  1543. {
  1544. QemuConsole *s = chr->opaque;
  1545. s->echo = echo;
  1546. }
  1547. static void text_console_update_cursor(void *opaque)
  1548. {
  1549. QemuConsole *s = opaque;
  1550. s->cursor_visible_phase = !s->cursor_visible_phase;
  1551. graphic_hw_invalidate(s);
  1552. timer_mod(s->cursor_timer,
  1553. qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + CONSOLE_CURSOR_PERIOD / 2);
  1554. }
  1555. static const GraphicHwOps text_console_ops = {
  1556. .invalidate = text_console_invalidate,
  1557. .text_update = text_console_update,
  1558. };
  1559. static void text_console_do_init(CharDriverState *chr, DisplayState *ds)
  1560. {
  1561. QemuConsole *s;
  1562. int g_width = 80 * FONT_WIDTH;
  1563. int g_height = 24 * FONT_HEIGHT;
  1564. s = chr->opaque;
  1565. chr->chr_write = console_puts;
  1566. s->out_fifo.buf = s->out_fifo_buf;
  1567. s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
  1568. s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
  1569. s->ds = ds;
  1570. s->y_displayed = 0;
  1571. s->y_base = 0;
  1572. s->total_height = DEFAULT_BACKSCROLL;
  1573. s->x = 0;
  1574. s->y = 0;
  1575. if (!s->surface) {
  1576. if (active_console && active_console->surface) {
  1577. g_width = surface_width(active_console->surface);
  1578. g_height = surface_height(active_console->surface);
  1579. }
  1580. s->surface = qemu_create_displaysurface(g_width, g_height);
  1581. }
  1582. s->cursor_timer =
  1583. timer_new_ms(QEMU_CLOCK_REALTIME, text_console_update_cursor, s);
  1584. s->hw_ops = &text_console_ops;
  1585. s->hw = s;
  1586. /* Set text attribute defaults */
  1587. s->t_attrib_default.bold = 0;
  1588. s->t_attrib_default.uline = 0;
  1589. s->t_attrib_default.blink = 0;
  1590. s->t_attrib_default.invers = 0;
  1591. s->t_attrib_default.unvisible = 0;
  1592. s->t_attrib_default.fgcol = COLOR_WHITE;
  1593. s->t_attrib_default.bgcol = COLOR_BLACK;
  1594. /* set current text attributes to default */
  1595. s->t_attrib = s->t_attrib_default;
  1596. text_console_resize(s);
  1597. if (chr->label) {
  1598. char msg[128];
  1599. int len;
  1600. s->t_attrib.bgcol = COLOR_BLUE;
  1601. len = snprintf(msg, sizeof(msg), "%s console\r\n", chr->label);
  1602. console_puts(chr, (uint8_t*)msg, len);
  1603. s->t_attrib = s->t_attrib_default;
  1604. }
  1605. qemu_chr_be_generic_open(chr);
  1606. if (chr->init)
  1607. chr->init(chr);
  1608. }
  1609. static CharDriverState *text_console_init(ChardevVC *vc)
  1610. {
  1611. CharDriverState *chr;
  1612. QemuConsole *s;
  1613. unsigned width = 0;
  1614. unsigned height = 0;
  1615. chr = g_malloc0(sizeof(CharDriverState));
  1616. if (vc->has_width) {
  1617. width = vc->width;
  1618. } else if (vc->has_cols) {
  1619. width = vc->cols * FONT_WIDTH;
  1620. }
  1621. if (vc->has_height) {
  1622. height = vc->height;
  1623. } else if (vc->has_rows) {
  1624. height = vc->rows * FONT_HEIGHT;
  1625. }
  1626. trace_console_txt_new(width, height);
  1627. if (width == 0 || height == 0) {
  1628. s = new_console(NULL, TEXT_CONSOLE);
  1629. } else {
  1630. s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE);
  1631. s->surface = qemu_create_displaysurface(width, height);
  1632. }
  1633. if (!s) {
  1634. g_free(chr);
  1635. return NULL;
  1636. }
  1637. s->chr = chr;
  1638. chr->opaque = s;
  1639. chr->chr_set_echo = text_console_set_echo;
  1640. /* console/chardev init sometimes completes elsewhere in a 2nd
  1641. * stage, so defer OPENED events until they are fully initialized
  1642. */
  1643. chr->explicit_be_open = true;
  1644. if (display_state) {
  1645. text_console_do_init(chr, display_state);
  1646. }
  1647. return chr;
  1648. }
  1649. static VcHandler *vc_handler = text_console_init;
  1650. CharDriverState *vc_init(ChardevVC *vc)
  1651. {
  1652. return vc_handler(vc);
  1653. }
  1654. void register_vc_handler(VcHandler *handler)
  1655. {
  1656. vc_handler = handler;
  1657. }
  1658. void qemu_console_resize(QemuConsole *s, int width, int height)
  1659. {
  1660. DisplaySurface *surface;
  1661. assert(s->console_type == GRAPHIC_CONSOLE);
  1662. surface = qemu_create_displaysurface(width, height);
  1663. dpy_gfx_replace_surface(s, surface);
  1664. }
  1665. void qemu_console_copy(QemuConsole *con, int src_x, int src_y,
  1666. int dst_x, int dst_y, int w, int h)
  1667. {
  1668. assert(con->console_type == GRAPHIC_CONSOLE);
  1669. dpy_gfx_copy(con, src_x, src_y, dst_x, dst_y, w, h);
  1670. }
  1671. DisplaySurface *qemu_console_surface(QemuConsole *console)
  1672. {
  1673. return console->surface;
  1674. }
  1675. DisplayState *qemu_console_displaystate(QemuConsole *console)
  1676. {
  1677. return console->ds;
  1678. }
  1679. PixelFormat qemu_different_endianness_pixelformat(int bpp)
  1680. {
  1681. PixelFormat pf;
  1682. memset(&pf, 0x00, sizeof(PixelFormat));
  1683. pf.bits_per_pixel = bpp;
  1684. pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
  1685. pf.depth = bpp == 32 ? 24 : bpp;
  1686. switch (bpp) {
  1687. case 24:
  1688. pf.rmask = 0x000000FF;
  1689. pf.gmask = 0x0000FF00;
  1690. pf.bmask = 0x00FF0000;
  1691. pf.rmax = 255;
  1692. pf.gmax = 255;
  1693. pf.bmax = 255;
  1694. pf.rshift = 0;
  1695. pf.gshift = 8;
  1696. pf.bshift = 16;
  1697. pf.rbits = 8;
  1698. pf.gbits = 8;
  1699. pf.bbits = 8;
  1700. break;
  1701. case 32:
  1702. pf.rmask = 0x0000FF00;
  1703. pf.gmask = 0x00FF0000;
  1704. pf.bmask = 0xFF000000;
  1705. pf.amask = 0x00000000;
  1706. pf.amax = 255;
  1707. pf.rmax = 255;
  1708. pf.gmax = 255;
  1709. pf.bmax = 255;
  1710. pf.ashift = 0;
  1711. pf.rshift = 8;
  1712. pf.gshift = 16;
  1713. pf.bshift = 24;
  1714. pf.rbits = 8;
  1715. pf.gbits = 8;
  1716. pf.bbits = 8;
  1717. pf.abits = 8;
  1718. break;
  1719. default:
  1720. break;
  1721. }
  1722. return pf;
  1723. }
  1724. PixelFormat qemu_default_pixelformat(int bpp)
  1725. {
  1726. PixelFormat pf;
  1727. memset(&pf, 0x00, sizeof(PixelFormat));
  1728. pf.bits_per_pixel = bpp;
  1729. pf.bytes_per_pixel = DIV_ROUND_UP(bpp, 8);
  1730. pf.depth = bpp == 32 ? 24 : bpp;
  1731. switch (bpp) {
  1732. case 15:
  1733. pf.bits_per_pixel = 16;
  1734. pf.rmask = 0x00007c00;
  1735. pf.gmask = 0x000003E0;
  1736. pf.bmask = 0x0000001F;
  1737. pf.rmax = 31;
  1738. pf.gmax = 31;
  1739. pf.bmax = 31;
  1740. pf.rshift = 10;
  1741. pf.gshift = 5;
  1742. pf.bshift = 0;
  1743. pf.rbits = 5;
  1744. pf.gbits = 5;
  1745. pf.bbits = 5;
  1746. break;
  1747. case 16:
  1748. pf.rmask = 0x0000F800;
  1749. pf.gmask = 0x000007E0;
  1750. pf.bmask = 0x0000001F;
  1751. pf.rmax = 31;
  1752. pf.gmax = 63;
  1753. pf.bmax = 31;
  1754. pf.rshift = 11;
  1755. pf.gshift = 5;
  1756. pf.bshift = 0;
  1757. pf.rbits = 5;
  1758. pf.gbits = 6;
  1759. pf.bbits = 5;
  1760. break;
  1761. case 24:
  1762. pf.rmask = 0x00FF0000;
  1763. pf.gmask = 0x0000FF00;
  1764. pf.bmask = 0x000000FF;
  1765. pf.rmax = 255;
  1766. pf.gmax = 255;
  1767. pf.bmax = 255;
  1768. pf.rshift = 16;
  1769. pf.gshift = 8;
  1770. pf.bshift = 0;
  1771. pf.rbits = 8;
  1772. pf.gbits = 8;
  1773. pf.bbits = 8;
  1774. break;
  1775. case 32:
  1776. pf.rmask = 0x00FF0000;
  1777. pf.gmask = 0x0000FF00;
  1778. pf.bmask = 0x000000FF;
  1779. pf.rmax = 255;
  1780. pf.gmax = 255;
  1781. pf.bmax = 255;
  1782. pf.rshift = 16;
  1783. pf.gshift = 8;
  1784. pf.bshift = 0;
  1785. pf.rbits = 8;
  1786. pf.gbits = 8;
  1787. pf.bbits = 8;
  1788. break;
  1789. default:
  1790. break;
  1791. }
  1792. return pf;
  1793. }
  1794. static void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend,
  1795. Error **errp)
  1796. {
  1797. int val;
  1798. backend->vc = g_new0(ChardevVC, 1);
  1799. val = qemu_opt_get_number(opts, "width", 0);
  1800. if (val != 0) {
  1801. backend->vc->has_width = true;
  1802. backend->vc->width = val;
  1803. }
  1804. val = qemu_opt_get_number(opts, "height", 0);
  1805. if (val != 0) {
  1806. backend->vc->has_height = true;
  1807. backend->vc->height = val;
  1808. }
  1809. val = qemu_opt_get_number(opts, "cols", 0);
  1810. if (val != 0) {
  1811. backend->vc->has_cols = true;
  1812. backend->vc->cols = val;
  1813. }
  1814. val = qemu_opt_get_number(opts, "rows", 0);
  1815. if (val != 0) {
  1816. backend->vc->has_rows = true;
  1817. backend->vc->rows = val;
  1818. }
  1819. }
  1820. static const TypeInfo qemu_console_info = {
  1821. .name = TYPE_QEMU_CONSOLE,
  1822. .parent = TYPE_OBJECT,
  1823. .instance_size = sizeof(QemuConsole),
  1824. .class_size = sizeof(QemuConsoleClass),
  1825. };
  1826. static void register_types(void)
  1827. {
  1828. type_register_static(&qemu_console_info);
  1829. register_char_driver_qapi("vc", CHARDEV_BACKEND_KIND_VC,
  1830. qemu_chr_parse_vc);
  1831. }
  1832. type_init(register_types);