console.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433
  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/osdep.h"
  25. #include "ui/console.h"
  26. #include "hw/qdev-core.h"
  27. #include "qapi/error.h"
  28. #include "qapi/qapi-commands-ui.h"
  29. #include "qemu/module.h"
  30. #include "qemu/option.h"
  31. #include "qemu/timer.h"
  32. #include "chardev/char-fe.h"
  33. #include "trace.h"
  34. #include "exec/memory.h"
  35. #include "io/channel-file.h"
  36. #include "qom/object.h"
  37. #define DEFAULT_BACKSCROLL 512
  38. #define CONSOLE_CURSOR_PERIOD 500
  39. typedef struct TextAttributes {
  40. uint8_t fgcol:4;
  41. uint8_t bgcol:4;
  42. uint8_t bold:1;
  43. uint8_t uline:1;
  44. uint8_t blink:1;
  45. uint8_t invers:1;
  46. uint8_t unvisible:1;
  47. } TextAttributes;
  48. typedef struct TextCell {
  49. uint8_t ch;
  50. TextAttributes t_attrib;
  51. } TextCell;
  52. #define MAX_ESC_PARAMS 3
  53. enum TTYState {
  54. TTY_STATE_NORM,
  55. TTY_STATE_ESC,
  56. TTY_STATE_CSI,
  57. };
  58. typedef struct QEMUFIFO {
  59. uint8_t *buf;
  60. int buf_size;
  61. int count, wptr, rptr;
  62. } QEMUFIFO;
  63. static int qemu_fifo_write(QEMUFIFO *f, const uint8_t *buf, int len1)
  64. {
  65. int l, len;
  66. l = f->buf_size - f->count;
  67. if (len1 > l)
  68. len1 = l;
  69. len = len1;
  70. while (len > 0) {
  71. l = f->buf_size - f->wptr;
  72. if (l > len)
  73. l = len;
  74. memcpy(f->buf + f->wptr, buf, l);
  75. f->wptr += l;
  76. if (f->wptr >= f->buf_size)
  77. f->wptr = 0;
  78. buf += l;
  79. len -= l;
  80. }
  81. f->count += len1;
  82. return len1;
  83. }
  84. static int qemu_fifo_read(QEMUFIFO *f, uint8_t *buf, int len1)
  85. {
  86. int l, len;
  87. if (len1 > f->count)
  88. len1 = f->count;
  89. len = len1;
  90. while (len > 0) {
  91. l = f->buf_size - f->rptr;
  92. if (l > len)
  93. l = len;
  94. memcpy(buf, f->buf + f->rptr, l);
  95. f->rptr += l;
  96. if (f->rptr >= f->buf_size)
  97. f->rptr = 0;
  98. buf += l;
  99. len -= l;
  100. }
  101. f->count -= len1;
  102. return len1;
  103. }
  104. typedef enum {
  105. GRAPHIC_CONSOLE,
  106. TEXT_CONSOLE,
  107. TEXT_CONSOLE_FIXED_SIZE
  108. } console_type_t;
  109. struct QemuConsole {
  110. Object parent;
  111. int index;
  112. console_type_t console_type;
  113. DisplayState *ds;
  114. DisplaySurface *surface;
  115. int dcls;
  116. DisplayChangeListener *gl;
  117. bool gl_block;
  118. int window_id;
  119. /* Graphic console state. */
  120. Object *device;
  121. uint32_t head;
  122. QemuUIInfo ui_info;
  123. QEMUTimer *ui_timer;
  124. const GraphicHwOps *hw_ops;
  125. void *hw;
  126. /* Text console state */
  127. int width;
  128. int height;
  129. int total_height;
  130. int backscroll_height;
  131. int x, y;
  132. int x_saved, y_saved;
  133. int y_displayed;
  134. int y_base;
  135. TextAttributes t_attrib_default; /* default text attributes */
  136. TextAttributes t_attrib; /* currently active text attributes */
  137. TextCell *cells;
  138. int text_x[2], text_y[2], cursor_invalidate;
  139. int echo;
  140. int update_x0;
  141. int update_y0;
  142. int update_x1;
  143. int update_y1;
  144. enum TTYState state;
  145. int esc_params[MAX_ESC_PARAMS];
  146. int nb_esc_params;
  147. Chardev *chr;
  148. /* fifo for key pressed */
  149. QEMUFIFO out_fifo;
  150. uint8_t out_fifo_buf[16];
  151. QEMUTimer *kbd_timer;
  152. QTAILQ_ENTRY(QemuConsole) next;
  153. };
  154. struct DisplayState {
  155. QEMUTimer *gui_timer;
  156. uint64_t last_update;
  157. uint64_t update_interval;
  158. bool refreshing;
  159. bool have_gfx;
  160. bool have_text;
  161. QLIST_HEAD(, DisplayChangeListener) listeners;
  162. };
  163. static DisplayState *display_state;
  164. static QemuConsole *active_console;
  165. static QTAILQ_HEAD(, QemuConsole) consoles =
  166. QTAILQ_HEAD_INITIALIZER(consoles);
  167. static bool cursor_visible_phase;
  168. static QEMUTimer *cursor_timer;
  169. static void text_console_do_init(Chardev *chr, DisplayState *ds);
  170. static void dpy_refresh(DisplayState *s);
  171. static DisplayState *get_alloc_displaystate(void);
  172. static void text_console_update_cursor_timer(void);
  173. static void text_console_update_cursor(void *opaque);
  174. static bool ppm_save(int fd, DisplaySurface *ds, Error **errp);
  175. static void gui_update(void *opaque)
  176. {
  177. uint64_t interval = GUI_REFRESH_INTERVAL_IDLE;
  178. uint64_t dcl_interval;
  179. DisplayState *ds = opaque;
  180. DisplayChangeListener *dcl;
  181. QemuConsole *con;
  182. ds->refreshing = true;
  183. dpy_refresh(ds);
  184. ds->refreshing = false;
  185. QLIST_FOREACH(dcl, &ds->listeners, next) {
  186. dcl_interval = dcl->update_interval ?
  187. dcl->update_interval : GUI_REFRESH_INTERVAL_DEFAULT;
  188. if (interval > dcl_interval) {
  189. interval = dcl_interval;
  190. }
  191. }
  192. if (ds->update_interval != interval) {
  193. ds->update_interval = interval;
  194. QTAILQ_FOREACH(con, &consoles, next) {
  195. if (con->hw_ops->update_interval) {
  196. con->hw_ops->update_interval(con->hw, interval);
  197. }
  198. }
  199. trace_console_refresh(interval);
  200. }
  201. ds->last_update = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
  202. timer_mod(ds->gui_timer, ds->last_update + interval);
  203. }
  204. static void gui_setup_refresh(DisplayState *ds)
  205. {
  206. DisplayChangeListener *dcl;
  207. bool need_timer = false;
  208. bool have_gfx = false;
  209. bool have_text = false;
  210. QLIST_FOREACH(dcl, &ds->listeners, next) {
  211. if (dcl->ops->dpy_refresh != NULL) {
  212. need_timer = true;
  213. }
  214. if (dcl->ops->dpy_gfx_update != NULL) {
  215. have_gfx = true;
  216. }
  217. if (dcl->ops->dpy_text_update != NULL) {
  218. have_text = true;
  219. }
  220. }
  221. if (need_timer && ds->gui_timer == NULL) {
  222. ds->gui_timer = timer_new_ms(QEMU_CLOCK_REALTIME, gui_update, ds);
  223. timer_mod(ds->gui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
  224. }
  225. if (!need_timer && ds->gui_timer != NULL) {
  226. timer_del(ds->gui_timer);
  227. timer_free(ds->gui_timer);
  228. ds->gui_timer = NULL;
  229. }
  230. ds->have_gfx = have_gfx;
  231. ds->have_text = have_text;
  232. }
  233. void graphic_hw_update_done(QemuConsole *con)
  234. {
  235. }
  236. void graphic_hw_update(QemuConsole *con)
  237. {
  238. bool async = false;
  239. if (!con) {
  240. con = active_console;
  241. }
  242. if (con && con->hw_ops->gfx_update) {
  243. con->hw_ops->gfx_update(con->hw);
  244. async = con->hw_ops->gfx_update_async;
  245. }
  246. if (!async) {
  247. graphic_hw_update_done(con);
  248. }
  249. }
  250. void graphic_hw_gl_block(QemuConsole *con, bool block)
  251. {
  252. assert(con != NULL);
  253. con->gl_block = block;
  254. if (con->hw_ops->gl_block) {
  255. con->hw_ops->gl_block(con->hw, block);
  256. }
  257. }
  258. int qemu_console_get_window_id(QemuConsole *con)
  259. {
  260. return con->window_id;
  261. }
  262. void qemu_console_set_window_id(QemuConsole *con, int window_id)
  263. {
  264. con->window_id = window_id;
  265. }
  266. void graphic_hw_invalidate(QemuConsole *con)
  267. {
  268. if (!con) {
  269. con = active_console;
  270. }
  271. if (con && con->hw_ops->invalidate) {
  272. con->hw_ops->invalidate(con->hw);
  273. }
  274. }
  275. static bool ppm_save(int fd, DisplaySurface *ds, Error **errp)
  276. {
  277. int width = pixman_image_get_width(ds->image);
  278. int height = pixman_image_get_height(ds->image);
  279. g_autoptr(Object) ioc = OBJECT(qio_channel_file_new_fd(fd));
  280. g_autofree char *header = NULL;
  281. g_autoptr(pixman_image_t) linebuf = NULL;
  282. int y;
  283. trace_ppm_save(fd, ds);
  284. header = g_strdup_printf("P6\n%d %d\n%d\n", width, height, 255);
  285. if (qio_channel_write_all(QIO_CHANNEL(ioc),
  286. header, strlen(header), errp) < 0) {
  287. return false;
  288. }
  289. linebuf = qemu_pixman_linebuf_create(PIXMAN_BE_r8g8b8, width);
  290. for (y = 0; y < height; y++) {
  291. qemu_pixman_linebuf_fill(linebuf, ds->image, width, 0, y);
  292. if (qio_channel_write_all(QIO_CHANNEL(ioc),
  293. (char *)pixman_image_get_data(linebuf),
  294. pixman_image_get_stride(linebuf), errp) < 0) {
  295. return false;
  296. }
  297. }
  298. return true;
  299. }
  300. void qmp_screendump(const char *filename, bool has_device, const char *device,
  301. bool has_head, int64_t head, Error **errp)
  302. {
  303. QemuConsole *con;
  304. DisplaySurface *surface;
  305. int fd;
  306. if (has_device) {
  307. con = qemu_console_lookup_by_device_name(device, has_head ? head : 0,
  308. errp);
  309. if (!con) {
  310. return;
  311. }
  312. } else {
  313. if (has_head) {
  314. error_setg(errp, "'head' must be specified together with 'device'");
  315. return;
  316. }
  317. con = qemu_console_lookup_by_index(0);
  318. if (!con) {
  319. error_setg(errp, "There is no console to take a screendump from");
  320. return;
  321. }
  322. }
  323. graphic_hw_update(con);
  324. surface = qemu_console_surface(con);
  325. if (!surface) {
  326. error_setg(errp, "no surface");
  327. return;
  328. }
  329. fd = qemu_open_old(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
  330. if (fd == -1) {
  331. error_setg(errp, "failed to open file '%s': %s", filename,
  332. strerror(errno));
  333. return;
  334. }
  335. if (!ppm_save(fd, surface, errp)) {
  336. qemu_unlink(filename);
  337. }
  338. }
  339. void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata)
  340. {
  341. if (!con) {
  342. con = active_console;
  343. }
  344. if (con && con->hw_ops->text_update) {
  345. con->hw_ops->text_update(con->hw, chardata);
  346. }
  347. }
  348. static void vga_fill_rect(QemuConsole *con,
  349. int posx, int posy, int width, int height,
  350. pixman_color_t color)
  351. {
  352. DisplaySurface *surface = qemu_console_surface(con);
  353. pixman_rectangle16_t rect = {
  354. .x = posx, .y = posy, .width = width, .height = height
  355. };
  356. pixman_image_fill_rectangles(PIXMAN_OP_SRC, surface->image,
  357. &color, 1, &rect);
  358. }
  359. /* copy from (xs, ys) to (xd, yd) a rectangle of size (w, h) */
  360. static void vga_bitblt(QemuConsole *con,
  361. int xs, int ys, int xd, int yd, int w, int h)
  362. {
  363. DisplaySurface *surface = qemu_console_surface(con);
  364. pixman_image_composite(PIXMAN_OP_SRC,
  365. surface->image, NULL, surface->image,
  366. xs, ys, 0, 0, xd, yd, w, h);
  367. }
  368. /***********************************************************/
  369. /* basic char display */
  370. #define FONT_HEIGHT 16
  371. #define FONT_WIDTH 8
  372. #include "vgafont.h"
  373. #define QEMU_RGB(r, g, b) \
  374. { .red = r << 8, .green = g << 8, .blue = b << 8, .alpha = 0xffff }
  375. static const pixman_color_t color_table_rgb[2][8] = {
  376. { /* dark */
  377. [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
  378. [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xaa), /* blue */
  379. [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xaa, 0x00), /* green */
  380. [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xaa, 0xaa), /* cyan */
  381. [QEMU_COLOR_RED] = QEMU_RGB(0xaa, 0x00, 0x00), /* red */
  382. [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xaa, 0x00, 0xaa), /* magenta */
  383. [QEMU_COLOR_YELLOW] = QEMU_RGB(0xaa, 0xaa, 0x00), /* yellow */
  384. [QEMU_COLOR_WHITE] = QEMU_RGB(0xaa, 0xaa, 0xaa), /* white */
  385. },
  386. { /* bright */
  387. [QEMU_COLOR_BLACK] = QEMU_RGB(0x00, 0x00, 0x00), /* black */
  388. [QEMU_COLOR_BLUE] = QEMU_RGB(0x00, 0x00, 0xff), /* blue */
  389. [QEMU_COLOR_GREEN] = QEMU_RGB(0x00, 0xff, 0x00), /* green */
  390. [QEMU_COLOR_CYAN] = QEMU_RGB(0x00, 0xff, 0xff), /* cyan */
  391. [QEMU_COLOR_RED] = QEMU_RGB(0xff, 0x00, 0x00), /* red */
  392. [QEMU_COLOR_MAGENTA] = QEMU_RGB(0xff, 0x00, 0xff), /* magenta */
  393. [QEMU_COLOR_YELLOW] = QEMU_RGB(0xff, 0xff, 0x00), /* yellow */
  394. [QEMU_COLOR_WHITE] = QEMU_RGB(0xff, 0xff, 0xff), /* white */
  395. }
  396. };
  397. static void vga_putcharxy(QemuConsole *s, int x, int y, int ch,
  398. TextAttributes *t_attrib)
  399. {
  400. static pixman_image_t *glyphs[256];
  401. DisplaySurface *surface = qemu_console_surface(s);
  402. pixman_color_t fgcol, bgcol;
  403. if (t_attrib->invers) {
  404. bgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
  405. fgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
  406. } else {
  407. fgcol = color_table_rgb[t_attrib->bold][t_attrib->fgcol];
  408. bgcol = color_table_rgb[t_attrib->bold][t_attrib->bgcol];
  409. }
  410. if (!glyphs[ch]) {
  411. glyphs[ch] = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, ch);
  412. }
  413. qemu_pixman_glyph_render(glyphs[ch], surface->image,
  414. &fgcol, &bgcol, x, y, FONT_WIDTH, FONT_HEIGHT);
  415. }
  416. static void text_console_resize(QemuConsole *s)
  417. {
  418. TextCell *cells, *c, *c1;
  419. int w1, x, y, last_width;
  420. last_width = s->width;
  421. s->width = surface_width(s->surface) / FONT_WIDTH;
  422. s->height = surface_height(s->surface) / FONT_HEIGHT;
  423. w1 = last_width;
  424. if (s->width < w1)
  425. w1 = s->width;
  426. cells = g_new(TextCell, s->width * s->total_height + 1);
  427. for(y = 0; y < s->total_height; y++) {
  428. c = &cells[y * s->width];
  429. if (w1 > 0) {
  430. c1 = &s->cells[y * last_width];
  431. for(x = 0; x < w1; x++) {
  432. *c++ = *c1++;
  433. }
  434. }
  435. for(x = w1; x < s->width; x++) {
  436. c->ch = ' ';
  437. c->t_attrib = s->t_attrib_default;
  438. c++;
  439. }
  440. }
  441. g_free(s->cells);
  442. s->cells = cells;
  443. }
  444. static inline void text_update_xy(QemuConsole *s, int x, int y)
  445. {
  446. s->text_x[0] = MIN(s->text_x[0], x);
  447. s->text_x[1] = MAX(s->text_x[1], x);
  448. s->text_y[0] = MIN(s->text_y[0], y);
  449. s->text_y[1] = MAX(s->text_y[1], y);
  450. }
  451. static void invalidate_xy(QemuConsole *s, int x, int y)
  452. {
  453. if (!qemu_console_is_visible(s)) {
  454. return;
  455. }
  456. if (s->update_x0 > x * FONT_WIDTH)
  457. s->update_x0 = x * FONT_WIDTH;
  458. if (s->update_y0 > y * FONT_HEIGHT)
  459. s->update_y0 = y * FONT_HEIGHT;
  460. if (s->update_x1 < (x + 1) * FONT_WIDTH)
  461. s->update_x1 = (x + 1) * FONT_WIDTH;
  462. if (s->update_y1 < (y + 1) * FONT_HEIGHT)
  463. s->update_y1 = (y + 1) * FONT_HEIGHT;
  464. }
  465. static void update_xy(QemuConsole *s, int x, int y)
  466. {
  467. TextCell *c;
  468. int y1, y2;
  469. if (s->ds->have_text) {
  470. text_update_xy(s, x, y);
  471. }
  472. y1 = (s->y_base + y) % s->total_height;
  473. y2 = y1 - s->y_displayed;
  474. if (y2 < 0) {
  475. y2 += s->total_height;
  476. }
  477. if (y2 < s->height) {
  478. if (x >= s->width) {
  479. x = s->width - 1;
  480. }
  481. c = &s->cells[y1 * s->width + x];
  482. vga_putcharxy(s, x, y2, c->ch,
  483. &(c->t_attrib));
  484. invalidate_xy(s, x, y2);
  485. }
  486. }
  487. static void console_show_cursor(QemuConsole *s, int show)
  488. {
  489. TextCell *c;
  490. int y, y1;
  491. int x = s->x;
  492. if (s->ds->have_text) {
  493. s->cursor_invalidate = 1;
  494. }
  495. if (x >= s->width) {
  496. x = s->width - 1;
  497. }
  498. y1 = (s->y_base + s->y) % s->total_height;
  499. y = y1 - s->y_displayed;
  500. if (y < 0) {
  501. y += s->total_height;
  502. }
  503. if (y < s->height) {
  504. c = &s->cells[y1 * s->width + x];
  505. if (show && cursor_visible_phase) {
  506. TextAttributes t_attrib = s->t_attrib_default;
  507. t_attrib.invers = !(t_attrib.invers); /* invert fg and bg */
  508. vga_putcharxy(s, x, y, c->ch, &t_attrib);
  509. } else {
  510. vga_putcharxy(s, x, y, c->ch, &(c->t_attrib));
  511. }
  512. invalidate_xy(s, x, y);
  513. }
  514. }
  515. static void console_refresh(QemuConsole *s)
  516. {
  517. DisplaySurface *surface = qemu_console_surface(s);
  518. TextCell *c;
  519. int x, y, y1;
  520. if (s->ds->have_text) {
  521. s->text_x[0] = 0;
  522. s->text_y[0] = 0;
  523. s->text_x[1] = s->width - 1;
  524. s->text_y[1] = s->height - 1;
  525. s->cursor_invalidate = 1;
  526. }
  527. vga_fill_rect(s, 0, 0, surface_width(surface), surface_height(surface),
  528. color_table_rgb[0][QEMU_COLOR_BLACK]);
  529. y1 = s->y_displayed;
  530. for (y = 0; y < s->height; y++) {
  531. c = s->cells + y1 * s->width;
  532. for (x = 0; x < s->width; x++) {
  533. vga_putcharxy(s, x, y, c->ch,
  534. &(c->t_attrib));
  535. c++;
  536. }
  537. if (++y1 == s->total_height) {
  538. y1 = 0;
  539. }
  540. }
  541. console_show_cursor(s, 1);
  542. dpy_gfx_update(s, 0, 0,
  543. surface_width(surface), surface_height(surface));
  544. }
  545. static void console_scroll(QemuConsole *s, int ydelta)
  546. {
  547. int i, y1;
  548. if (ydelta > 0) {
  549. for(i = 0; i < ydelta; i++) {
  550. if (s->y_displayed == s->y_base)
  551. break;
  552. if (++s->y_displayed == s->total_height)
  553. s->y_displayed = 0;
  554. }
  555. } else {
  556. ydelta = -ydelta;
  557. i = s->backscroll_height;
  558. if (i > s->total_height - s->height)
  559. i = s->total_height - s->height;
  560. y1 = s->y_base - i;
  561. if (y1 < 0)
  562. y1 += s->total_height;
  563. for(i = 0; i < ydelta; i++) {
  564. if (s->y_displayed == y1)
  565. break;
  566. if (--s->y_displayed < 0)
  567. s->y_displayed = s->total_height - 1;
  568. }
  569. }
  570. console_refresh(s);
  571. }
  572. static void console_put_lf(QemuConsole *s)
  573. {
  574. TextCell *c;
  575. int x, y1;
  576. s->y++;
  577. if (s->y >= s->height) {
  578. s->y = s->height - 1;
  579. if (s->y_displayed == s->y_base) {
  580. if (++s->y_displayed == s->total_height)
  581. s->y_displayed = 0;
  582. }
  583. if (++s->y_base == s->total_height)
  584. s->y_base = 0;
  585. if (s->backscroll_height < s->total_height)
  586. s->backscroll_height++;
  587. y1 = (s->y_base + s->height - 1) % s->total_height;
  588. c = &s->cells[y1 * s->width];
  589. for(x = 0; x < s->width; x++) {
  590. c->ch = ' ';
  591. c->t_attrib = s->t_attrib_default;
  592. c++;
  593. }
  594. if (s->y_displayed == s->y_base) {
  595. if (s->ds->have_text) {
  596. s->text_x[0] = 0;
  597. s->text_y[0] = 0;
  598. s->text_x[1] = s->width - 1;
  599. s->text_y[1] = s->height - 1;
  600. }
  601. vga_bitblt(s, 0, FONT_HEIGHT, 0, 0,
  602. s->width * FONT_WIDTH,
  603. (s->height - 1) * FONT_HEIGHT);
  604. vga_fill_rect(s, 0, (s->height - 1) * FONT_HEIGHT,
  605. s->width * FONT_WIDTH, FONT_HEIGHT,
  606. color_table_rgb[0][s->t_attrib_default.bgcol]);
  607. s->update_x0 = 0;
  608. s->update_y0 = 0;
  609. s->update_x1 = s->width * FONT_WIDTH;
  610. s->update_y1 = s->height * FONT_HEIGHT;
  611. }
  612. }
  613. }
  614. /* Set console attributes depending on the current escape codes.
  615. * NOTE: I know this code is not very efficient (checking every color for it
  616. * self) but it is more readable and better maintainable.
  617. */
  618. static void console_handle_escape(QemuConsole *s)
  619. {
  620. int i;
  621. for (i=0; i<s->nb_esc_params; i++) {
  622. switch (s->esc_params[i]) {
  623. case 0: /* reset all console attributes to default */
  624. s->t_attrib = s->t_attrib_default;
  625. break;
  626. case 1:
  627. s->t_attrib.bold = 1;
  628. break;
  629. case 4:
  630. s->t_attrib.uline = 1;
  631. break;
  632. case 5:
  633. s->t_attrib.blink = 1;
  634. break;
  635. case 7:
  636. s->t_attrib.invers = 1;
  637. break;
  638. case 8:
  639. s->t_attrib.unvisible = 1;
  640. break;
  641. case 22:
  642. s->t_attrib.bold = 0;
  643. break;
  644. case 24:
  645. s->t_attrib.uline = 0;
  646. break;
  647. case 25:
  648. s->t_attrib.blink = 0;
  649. break;
  650. case 27:
  651. s->t_attrib.invers = 0;
  652. break;
  653. case 28:
  654. s->t_attrib.unvisible = 0;
  655. break;
  656. /* set foreground color */
  657. case 30:
  658. s->t_attrib.fgcol = QEMU_COLOR_BLACK;
  659. break;
  660. case 31:
  661. s->t_attrib.fgcol = QEMU_COLOR_RED;
  662. break;
  663. case 32:
  664. s->t_attrib.fgcol = QEMU_COLOR_GREEN;
  665. break;
  666. case 33:
  667. s->t_attrib.fgcol = QEMU_COLOR_YELLOW;
  668. break;
  669. case 34:
  670. s->t_attrib.fgcol = QEMU_COLOR_BLUE;
  671. break;
  672. case 35:
  673. s->t_attrib.fgcol = QEMU_COLOR_MAGENTA;
  674. break;
  675. case 36:
  676. s->t_attrib.fgcol = QEMU_COLOR_CYAN;
  677. break;
  678. case 37:
  679. s->t_attrib.fgcol = QEMU_COLOR_WHITE;
  680. break;
  681. /* set background color */
  682. case 40:
  683. s->t_attrib.bgcol = QEMU_COLOR_BLACK;
  684. break;
  685. case 41:
  686. s->t_attrib.bgcol = QEMU_COLOR_RED;
  687. break;
  688. case 42:
  689. s->t_attrib.bgcol = QEMU_COLOR_GREEN;
  690. break;
  691. case 43:
  692. s->t_attrib.bgcol = QEMU_COLOR_YELLOW;
  693. break;
  694. case 44:
  695. s->t_attrib.bgcol = QEMU_COLOR_BLUE;
  696. break;
  697. case 45:
  698. s->t_attrib.bgcol = QEMU_COLOR_MAGENTA;
  699. break;
  700. case 46:
  701. s->t_attrib.bgcol = QEMU_COLOR_CYAN;
  702. break;
  703. case 47:
  704. s->t_attrib.bgcol = QEMU_COLOR_WHITE;
  705. break;
  706. }
  707. }
  708. }
  709. static void console_clear_xy(QemuConsole *s, int x, int y)
  710. {
  711. int y1 = (s->y_base + y) % s->total_height;
  712. if (x >= s->width) {
  713. x = s->width - 1;
  714. }
  715. TextCell *c = &s->cells[y1 * s->width + x];
  716. c->ch = ' ';
  717. c->t_attrib = s->t_attrib_default;
  718. update_xy(s, x, y);
  719. }
  720. static void console_put_one(QemuConsole *s, int ch)
  721. {
  722. TextCell *c;
  723. int y1;
  724. if (s->x >= s->width) {
  725. /* line wrap */
  726. s->x = 0;
  727. console_put_lf(s);
  728. }
  729. y1 = (s->y_base + s->y) % s->total_height;
  730. c = &s->cells[y1 * s->width + s->x];
  731. c->ch = ch;
  732. c->t_attrib = s->t_attrib;
  733. update_xy(s, s->x, s->y);
  734. s->x++;
  735. }
  736. static void console_respond_str(QemuConsole *s, const char *buf)
  737. {
  738. while (*buf) {
  739. console_put_one(s, *buf);
  740. buf++;
  741. }
  742. }
  743. /* set cursor, checking bounds */
  744. static void set_cursor(QemuConsole *s, int x, int y)
  745. {
  746. if (x < 0) {
  747. x = 0;
  748. }
  749. if (y < 0) {
  750. y = 0;
  751. }
  752. if (y >= s->height) {
  753. y = s->height - 1;
  754. }
  755. if (x >= s->width) {
  756. x = s->width - 1;
  757. }
  758. s->x = x;
  759. s->y = y;
  760. }
  761. static void console_putchar(QemuConsole *s, int ch)
  762. {
  763. int i;
  764. int x, y;
  765. char response[40];
  766. switch(s->state) {
  767. case TTY_STATE_NORM:
  768. switch(ch) {
  769. case '\r': /* carriage return */
  770. s->x = 0;
  771. break;
  772. case '\n': /* newline */
  773. console_put_lf(s);
  774. break;
  775. case '\b': /* backspace */
  776. if (s->x > 0)
  777. s->x--;
  778. break;
  779. case '\t': /* tabspace */
  780. if (s->x + (8 - (s->x % 8)) > s->width) {
  781. s->x = 0;
  782. console_put_lf(s);
  783. } else {
  784. s->x = s->x + (8 - (s->x % 8));
  785. }
  786. break;
  787. case '\a': /* alert aka. bell */
  788. /* TODO: has to be implemented */
  789. break;
  790. case 14:
  791. /* SI (shift in), character set 0 (ignored) */
  792. break;
  793. case 15:
  794. /* SO (shift out), character set 1 (ignored) */
  795. break;
  796. case 27: /* esc (introducing an escape sequence) */
  797. s->state = TTY_STATE_ESC;
  798. break;
  799. default:
  800. console_put_one(s, ch);
  801. break;
  802. }
  803. break;
  804. case TTY_STATE_ESC: /* check if it is a terminal escape sequence */
  805. if (ch == '[') {
  806. for(i=0;i<MAX_ESC_PARAMS;i++)
  807. s->esc_params[i] = 0;
  808. s->nb_esc_params = 0;
  809. s->state = TTY_STATE_CSI;
  810. } else {
  811. s->state = TTY_STATE_NORM;
  812. }
  813. break;
  814. case TTY_STATE_CSI: /* handle escape sequence parameters */
  815. if (ch >= '0' && ch <= '9') {
  816. if (s->nb_esc_params < MAX_ESC_PARAMS) {
  817. int *param = &s->esc_params[s->nb_esc_params];
  818. int digit = (ch - '0');
  819. *param = (*param <= (INT_MAX - digit) / 10) ?
  820. *param * 10 + digit : INT_MAX;
  821. }
  822. } else {
  823. if (s->nb_esc_params < MAX_ESC_PARAMS)
  824. s->nb_esc_params++;
  825. if (ch == ';' || ch == '?') {
  826. break;
  827. }
  828. trace_console_putchar_csi(s->esc_params[0], s->esc_params[1],
  829. ch, s->nb_esc_params);
  830. s->state = TTY_STATE_NORM;
  831. switch(ch) {
  832. case 'A':
  833. /* move cursor up */
  834. if (s->esc_params[0] == 0) {
  835. s->esc_params[0] = 1;
  836. }
  837. set_cursor(s, s->x, s->y - s->esc_params[0]);
  838. break;
  839. case 'B':
  840. /* move cursor down */
  841. if (s->esc_params[0] == 0) {
  842. s->esc_params[0] = 1;
  843. }
  844. set_cursor(s, s->x, s->y + s->esc_params[0]);
  845. break;
  846. case 'C':
  847. /* move cursor right */
  848. if (s->esc_params[0] == 0) {
  849. s->esc_params[0] = 1;
  850. }
  851. set_cursor(s, s->x + s->esc_params[0], s->y);
  852. break;
  853. case 'D':
  854. /* move cursor left */
  855. if (s->esc_params[0] == 0) {
  856. s->esc_params[0] = 1;
  857. }
  858. set_cursor(s, s->x - s->esc_params[0], s->y);
  859. break;
  860. case 'G':
  861. /* move cursor to column */
  862. set_cursor(s, s->esc_params[0] - 1, s->y);
  863. break;
  864. case 'f':
  865. case 'H':
  866. /* move cursor to row, column */
  867. set_cursor(s, s->esc_params[1] - 1, s->esc_params[0] - 1);
  868. break;
  869. case 'J':
  870. switch (s->esc_params[0]) {
  871. case 0:
  872. /* clear to end of screen */
  873. for (y = s->y; y < s->height; y++) {
  874. for (x = 0; x < s->width; x++) {
  875. if (y == s->y && x < s->x) {
  876. continue;
  877. }
  878. console_clear_xy(s, x, y);
  879. }
  880. }
  881. break;
  882. case 1:
  883. /* clear from beginning of screen */
  884. for (y = 0; y <= s->y; y++) {
  885. for (x = 0; x < s->width; x++) {
  886. if (y == s->y && x > s->x) {
  887. break;
  888. }
  889. console_clear_xy(s, x, y);
  890. }
  891. }
  892. break;
  893. case 2:
  894. /* clear entire screen */
  895. for (y = 0; y <= s->height; y++) {
  896. for (x = 0; x < s->width; x++) {
  897. console_clear_xy(s, x, y);
  898. }
  899. }
  900. break;
  901. }
  902. break;
  903. case 'K':
  904. switch (s->esc_params[0]) {
  905. case 0:
  906. /* clear to eol */
  907. for(x = s->x; x < s->width; x++) {
  908. console_clear_xy(s, x, s->y);
  909. }
  910. break;
  911. case 1:
  912. /* clear from beginning of line */
  913. for (x = 0; x <= s->x && x < s->width; x++) {
  914. console_clear_xy(s, x, s->y);
  915. }
  916. break;
  917. case 2:
  918. /* clear entire line */
  919. for(x = 0; x < s->width; x++) {
  920. console_clear_xy(s, x, s->y);
  921. }
  922. break;
  923. }
  924. break;
  925. case 'm':
  926. console_handle_escape(s);
  927. break;
  928. case 'n':
  929. switch (s->esc_params[0]) {
  930. case 5:
  931. /* report console status (always succeed)*/
  932. console_respond_str(s, "\033[0n");
  933. break;
  934. case 6:
  935. /* report cursor position */
  936. sprintf(response, "\033[%d;%dR",
  937. (s->y_base + s->y) % s->total_height + 1,
  938. s->x + 1);
  939. console_respond_str(s, response);
  940. break;
  941. }
  942. break;
  943. case 's':
  944. /* save cursor position */
  945. s->x_saved = s->x;
  946. s->y_saved = s->y;
  947. break;
  948. case 'u':
  949. /* restore cursor position */
  950. s->x = s->x_saved;
  951. s->y = s->y_saved;
  952. break;
  953. default:
  954. trace_console_putchar_unhandled(ch);
  955. break;
  956. }
  957. break;
  958. }
  959. }
  960. }
  961. void console_select(unsigned int index)
  962. {
  963. DisplayChangeListener *dcl;
  964. QemuConsole *s;
  965. trace_console_select(index);
  966. s = qemu_console_lookup_by_index(index);
  967. if (s) {
  968. DisplayState *ds = s->ds;
  969. active_console = s;
  970. if (ds->have_gfx) {
  971. QLIST_FOREACH(dcl, &ds->listeners, next) {
  972. if (dcl->con != NULL) {
  973. continue;
  974. }
  975. if (dcl->ops->dpy_gfx_switch) {
  976. dcl->ops->dpy_gfx_switch(dcl, s->surface);
  977. }
  978. }
  979. if (s->surface) {
  980. dpy_gfx_update(s, 0, 0, surface_width(s->surface),
  981. surface_height(s->surface));
  982. }
  983. }
  984. if (ds->have_text) {
  985. dpy_text_resize(s, s->width, s->height);
  986. }
  987. text_console_update_cursor(NULL);
  988. }
  989. }
  990. struct VCChardev {
  991. Chardev parent;
  992. QemuConsole *console;
  993. };
  994. typedef struct VCChardev VCChardev;
  995. #define TYPE_CHARDEV_VC "chardev-vc"
  996. DECLARE_INSTANCE_CHECKER(VCChardev, VC_CHARDEV,
  997. TYPE_CHARDEV_VC)
  998. static int vc_chr_write(Chardev *chr, const uint8_t *buf, int len)
  999. {
  1000. VCChardev *drv = VC_CHARDEV(chr);
  1001. QemuConsole *s = drv->console;
  1002. int i;
  1003. if (!s->ds) {
  1004. return 0;
  1005. }
  1006. s->update_x0 = s->width * FONT_WIDTH;
  1007. s->update_y0 = s->height * FONT_HEIGHT;
  1008. s->update_x1 = 0;
  1009. s->update_y1 = 0;
  1010. console_show_cursor(s, 0);
  1011. for(i = 0; i < len; i++) {
  1012. console_putchar(s, buf[i]);
  1013. }
  1014. console_show_cursor(s, 1);
  1015. if (s->ds->have_gfx && s->update_x0 < s->update_x1) {
  1016. dpy_gfx_update(s, s->update_x0, s->update_y0,
  1017. s->update_x1 - s->update_x0,
  1018. s->update_y1 - s->update_y0);
  1019. }
  1020. return len;
  1021. }
  1022. static void kbd_send_chars(void *opaque)
  1023. {
  1024. QemuConsole *s = opaque;
  1025. int len;
  1026. uint8_t buf[16];
  1027. len = qemu_chr_be_can_write(s->chr);
  1028. if (len > s->out_fifo.count)
  1029. len = s->out_fifo.count;
  1030. if (len > 0) {
  1031. if (len > sizeof(buf))
  1032. len = sizeof(buf);
  1033. qemu_fifo_read(&s->out_fifo, buf, len);
  1034. qemu_chr_be_write(s->chr, buf, len);
  1035. }
  1036. /* characters are pending: we send them a bit later (XXX:
  1037. horrible, should change char device API) */
  1038. if (s->out_fifo.count > 0) {
  1039. timer_mod(s->kbd_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1);
  1040. }
  1041. }
  1042. /* called when an ascii key is pressed */
  1043. void kbd_put_keysym_console(QemuConsole *s, int keysym)
  1044. {
  1045. uint8_t buf[16], *q;
  1046. CharBackend *be;
  1047. int c;
  1048. if (!s || (s->console_type == GRAPHIC_CONSOLE))
  1049. return;
  1050. switch(keysym) {
  1051. case QEMU_KEY_CTRL_UP:
  1052. console_scroll(s, -1);
  1053. break;
  1054. case QEMU_KEY_CTRL_DOWN:
  1055. console_scroll(s, 1);
  1056. break;
  1057. case QEMU_KEY_CTRL_PAGEUP:
  1058. console_scroll(s, -10);
  1059. break;
  1060. case QEMU_KEY_CTRL_PAGEDOWN:
  1061. console_scroll(s, 10);
  1062. break;
  1063. default:
  1064. /* convert the QEMU keysym to VT100 key string */
  1065. q = buf;
  1066. if (keysym >= 0xe100 && keysym <= 0xe11f) {
  1067. *q++ = '\033';
  1068. *q++ = '[';
  1069. c = keysym - 0xe100;
  1070. if (c >= 10)
  1071. *q++ = '0' + (c / 10);
  1072. *q++ = '0' + (c % 10);
  1073. *q++ = '~';
  1074. } else if (keysym >= 0xe120 && keysym <= 0xe17f) {
  1075. *q++ = '\033';
  1076. *q++ = '[';
  1077. *q++ = keysym & 0xff;
  1078. } else if (s->echo && (keysym == '\r' || keysym == '\n')) {
  1079. vc_chr_write(s->chr, (const uint8_t *) "\r", 1);
  1080. *q++ = '\n';
  1081. } else {
  1082. *q++ = keysym;
  1083. }
  1084. if (s->echo) {
  1085. vc_chr_write(s->chr, buf, q - buf);
  1086. }
  1087. be = s->chr->be;
  1088. if (be && be->chr_read) {
  1089. qemu_fifo_write(&s->out_fifo, buf, q - buf);
  1090. kbd_send_chars(s);
  1091. }
  1092. break;
  1093. }
  1094. }
  1095. static const int qcode_to_keysym[Q_KEY_CODE__MAX] = {
  1096. [Q_KEY_CODE_UP] = QEMU_KEY_UP,
  1097. [Q_KEY_CODE_DOWN] = QEMU_KEY_DOWN,
  1098. [Q_KEY_CODE_RIGHT] = QEMU_KEY_RIGHT,
  1099. [Q_KEY_CODE_LEFT] = QEMU_KEY_LEFT,
  1100. [Q_KEY_CODE_HOME] = QEMU_KEY_HOME,
  1101. [Q_KEY_CODE_END] = QEMU_KEY_END,
  1102. [Q_KEY_CODE_PGUP] = QEMU_KEY_PAGEUP,
  1103. [Q_KEY_CODE_PGDN] = QEMU_KEY_PAGEDOWN,
  1104. [Q_KEY_CODE_DELETE] = QEMU_KEY_DELETE,
  1105. [Q_KEY_CODE_BACKSPACE] = QEMU_KEY_BACKSPACE,
  1106. };
  1107. static const int ctrl_qcode_to_keysym[Q_KEY_CODE__MAX] = {
  1108. [Q_KEY_CODE_UP] = QEMU_KEY_CTRL_UP,
  1109. [Q_KEY_CODE_DOWN] = QEMU_KEY_CTRL_DOWN,
  1110. [Q_KEY_CODE_RIGHT] = QEMU_KEY_CTRL_RIGHT,
  1111. [Q_KEY_CODE_LEFT] = QEMU_KEY_CTRL_LEFT,
  1112. [Q_KEY_CODE_HOME] = QEMU_KEY_CTRL_HOME,
  1113. [Q_KEY_CODE_END] = QEMU_KEY_CTRL_END,
  1114. [Q_KEY_CODE_PGUP] = QEMU_KEY_CTRL_PAGEUP,
  1115. [Q_KEY_CODE_PGDN] = QEMU_KEY_CTRL_PAGEDOWN,
  1116. };
  1117. bool kbd_put_qcode_console(QemuConsole *s, int qcode, bool ctrl)
  1118. {
  1119. int keysym;
  1120. keysym = ctrl ? ctrl_qcode_to_keysym[qcode] : qcode_to_keysym[qcode];
  1121. if (keysym == 0) {
  1122. return false;
  1123. }
  1124. kbd_put_keysym_console(s, keysym);
  1125. return true;
  1126. }
  1127. void kbd_put_string_console(QemuConsole *s, const char *str, int len)
  1128. {
  1129. int i;
  1130. for (i = 0; i < len && str[i]; i++) {
  1131. kbd_put_keysym_console(s, str[i]);
  1132. }
  1133. }
  1134. void kbd_put_keysym(int keysym)
  1135. {
  1136. kbd_put_keysym_console(active_console, keysym);
  1137. }
  1138. static void text_console_invalidate(void *opaque)
  1139. {
  1140. QemuConsole *s = (QemuConsole *) opaque;
  1141. if (s->ds->have_text && s->console_type == TEXT_CONSOLE) {
  1142. text_console_resize(s);
  1143. }
  1144. console_refresh(s);
  1145. }
  1146. static void text_console_update(void *opaque, console_ch_t *chardata)
  1147. {
  1148. QemuConsole *s = (QemuConsole *) opaque;
  1149. int i, j, src;
  1150. if (s->text_x[0] <= s->text_x[1]) {
  1151. src = (s->y_base + s->text_y[0]) * s->width;
  1152. chardata += s->text_y[0] * s->width;
  1153. for (i = s->text_y[0]; i <= s->text_y[1]; i ++)
  1154. for (j = 0; j < s->width; j++, src++) {
  1155. console_write_ch(chardata ++,
  1156. ATTR2CHTYPE(s->cells[src].ch,
  1157. s->cells[src].t_attrib.fgcol,
  1158. s->cells[src].t_attrib.bgcol,
  1159. s->cells[src].t_attrib.bold));
  1160. }
  1161. dpy_text_update(s, s->text_x[0], s->text_y[0],
  1162. s->text_x[1] - s->text_x[0], i - s->text_y[0]);
  1163. s->text_x[0] = s->width;
  1164. s->text_y[0] = s->height;
  1165. s->text_x[1] = 0;
  1166. s->text_y[1] = 0;
  1167. }
  1168. if (s->cursor_invalidate) {
  1169. dpy_text_cursor(s, s->x, s->y);
  1170. s->cursor_invalidate = 0;
  1171. }
  1172. }
  1173. static QemuConsole *new_console(DisplayState *ds, console_type_t console_type,
  1174. uint32_t head)
  1175. {
  1176. Object *obj;
  1177. QemuConsole *s;
  1178. int i;
  1179. obj = object_new(TYPE_QEMU_CONSOLE);
  1180. s = QEMU_CONSOLE(obj);
  1181. s->head = head;
  1182. object_property_add_link(obj, "device", TYPE_DEVICE,
  1183. (Object **)&s->device,
  1184. object_property_allow_set_link,
  1185. OBJ_PROP_LINK_STRONG);
  1186. object_property_add_uint32_ptr(obj, "head", &s->head,
  1187. OBJ_PROP_FLAG_READ);
  1188. if (!active_console || ((active_console->console_type != GRAPHIC_CONSOLE) &&
  1189. (console_type == GRAPHIC_CONSOLE))) {
  1190. active_console = s;
  1191. }
  1192. s->ds = ds;
  1193. s->console_type = console_type;
  1194. s->window_id = -1;
  1195. if (QTAILQ_EMPTY(&consoles)) {
  1196. s->index = 0;
  1197. QTAILQ_INSERT_TAIL(&consoles, s, next);
  1198. } else if (console_type != GRAPHIC_CONSOLE || qdev_hotplug) {
  1199. QemuConsole *last = QTAILQ_LAST(&consoles);
  1200. s->index = last->index + 1;
  1201. QTAILQ_INSERT_TAIL(&consoles, s, next);
  1202. } else {
  1203. /*
  1204. * HACK: Put graphical consoles before text consoles.
  1205. *
  1206. * Only do that for coldplugged devices. After initial device
  1207. * initialization we will not renumber the consoles any more.
  1208. */
  1209. QemuConsole *c = QTAILQ_FIRST(&consoles);
  1210. while (QTAILQ_NEXT(c, next) != NULL &&
  1211. c->console_type == GRAPHIC_CONSOLE) {
  1212. c = QTAILQ_NEXT(c, next);
  1213. }
  1214. if (c->console_type == GRAPHIC_CONSOLE) {
  1215. /* have no text consoles */
  1216. s->index = c->index + 1;
  1217. QTAILQ_INSERT_AFTER(&consoles, c, s, next);
  1218. } else {
  1219. s->index = c->index;
  1220. QTAILQ_INSERT_BEFORE(c, s, next);
  1221. /* renumber text consoles */
  1222. for (i = s->index + 1; c != NULL; c = QTAILQ_NEXT(c, next), i++) {
  1223. c->index = i;
  1224. }
  1225. }
  1226. }
  1227. return s;
  1228. }
  1229. static void qemu_alloc_display(DisplaySurface *surface, int width, int height)
  1230. {
  1231. qemu_pixman_image_unref(surface->image);
  1232. surface->image = NULL;
  1233. surface->format = PIXMAN_x8r8g8b8;
  1234. surface->image = pixman_image_create_bits(surface->format,
  1235. width, height,
  1236. NULL, width * 4);
  1237. assert(surface->image != NULL);
  1238. surface->flags = QEMU_ALLOCATED_FLAG;
  1239. }
  1240. DisplaySurface *qemu_create_displaysurface(int width, int height)
  1241. {
  1242. DisplaySurface *surface = g_new0(DisplaySurface, 1);
  1243. trace_displaysurface_create(surface, width, height);
  1244. qemu_alloc_display(surface, width, height);
  1245. return surface;
  1246. }
  1247. DisplaySurface *qemu_create_displaysurface_from(int width, int height,
  1248. pixman_format_code_t format,
  1249. int linesize, uint8_t *data)
  1250. {
  1251. DisplaySurface *surface = g_new0(DisplaySurface, 1);
  1252. trace_displaysurface_create_from(surface, width, height, format);
  1253. surface->format = format;
  1254. surface->image = pixman_image_create_bits(surface->format,
  1255. width, height,
  1256. (void *)data, linesize);
  1257. assert(surface->image != NULL);
  1258. return surface;
  1259. }
  1260. DisplaySurface *qemu_create_displaysurface_pixman(pixman_image_t *image)
  1261. {
  1262. DisplaySurface *surface = g_new0(DisplaySurface, 1);
  1263. trace_displaysurface_create_pixman(surface);
  1264. surface->format = pixman_image_get_format(image);
  1265. surface->image = pixman_image_ref(image);
  1266. return surface;
  1267. }
  1268. DisplaySurface *qemu_create_message_surface(int w, int h,
  1269. const char *msg)
  1270. {
  1271. DisplaySurface *surface = qemu_create_displaysurface(w, h);
  1272. pixman_color_t bg = color_table_rgb[0][QEMU_COLOR_BLACK];
  1273. pixman_color_t fg = color_table_rgb[0][QEMU_COLOR_WHITE];
  1274. pixman_image_t *glyph;
  1275. int len, x, y, i;
  1276. len = strlen(msg);
  1277. x = (w / FONT_WIDTH - len) / 2;
  1278. y = (h / FONT_HEIGHT - 1) / 2;
  1279. for (i = 0; i < len; i++) {
  1280. glyph = qemu_pixman_glyph_from_vgafont(FONT_HEIGHT, vgafont16, msg[i]);
  1281. qemu_pixman_glyph_render(glyph, surface->image, &fg, &bg,
  1282. x+i, y, FONT_WIDTH, FONT_HEIGHT);
  1283. qemu_pixman_image_unref(glyph);
  1284. }
  1285. return surface;
  1286. }
  1287. void qemu_free_displaysurface(DisplaySurface *surface)
  1288. {
  1289. if (surface == NULL) {
  1290. return;
  1291. }
  1292. trace_displaysurface_free(surface);
  1293. qemu_pixman_image_unref(surface->image);
  1294. g_free(surface);
  1295. }
  1296. bool console_has_gl(QemuConsole *con)
  1297. {
  1298. return con->gl != NULL;
  1299. }
  1300. bool console_has_gl_dmabuf(QemuConsole *con)
  1301. {
  1302. return con->gl != NULL && con->gl->ops->dpy_gl_scanout_dmabuf != NULL;
  1303. }
  1304. void register_displaychangelistener(DisplayChangeListener *dcl)
  1305. {
  1306. static const char nodev[] =
  1307. "This VM has no graphic display device.";
  1308. static DisplaySurface *dummy;
  1309. QemuConsole *con;
  1310. assert(!dcl->ds);
  1311. if (dcl->ops->dpy_gl_ctx_create) {
  1312. /* display has opengl support */
  1313. assert(dcl->con);
  1314. if (dcl->con->gl) {
  1315. fprintf(stderr, "can't register two opengl displays (%s, %s)\n",
  1316. dcl->ops->dpy_name, dcl->con->gl->ops->dpy_name);
  1317. exit(1);
  1318. }
  1319. dcl->con->gl = dcl;
  1320. }
  1321. trace_displaychangelistener_register(dcl, dcl->ops->dpy_name);
  1322. dcl->ds = get_alloc_displaystate();
  1323. QLIST_INSERT_HEAD(&dcl->ds->listeners, dcl, next);
  1324. gui_setup_refresh(dcl->ds);
  1325. if (dcl->con) {
  1326. dcl->con->dcls++;
  1327. con = dcl->con;
  1328. } else {
  1329. con = active_console;
  1330. }
  1331. if (dcl->ops->dpy_gfx_switch) {
  1332. if (con) {
  1333. dcl->ops->dpy_gfx_switch(dcl, con->surface);
  1334. } else {
  1335. if (!dummy) {
  1336. dummy = qemu_create_message_surface(640, 480, nodev);
  1337. }
  1338. dcl->ops->dpy_gfx_switch(dcl, dummy);
  1339. }
  1340. }
  1341. text_console_update_cursor(NULL);
  1342. }
  1343. void update_displaychangelistener(DisplayChangeListener *dcl,
  1344. uint64_t interval)
  1345. {
  1346. DisplayState *ds = dcl->ds;
  1347. dcl->update_interval = interval;
  1348. if (!ds->refreshing && ds->update_interval > interval) {
  1349. timer_mod(ds->gui_timer, ds->last_update + interval);
  1350. }
  1351. }
  1352. void unregister_displaychangelistener(DisplayChangeListener *dcl)
  1353. {
  1354. DisplayState *ds = dcl->ds;
  1355. trace_displaychangelistener_unregister(dcl, dcl->ops->dpy_name);
  1356. if (dcl->con) {
  1357. dcl->con->dcls--;
  1358. }
  1359. QLIST_REMOVE(dcl, next);
  1360. dcl->ds = NULL;
  1361. gui_setup_refresh(ds);
  1362. }
  1363. static void dpy_set_ui_info_timer(void *opaque)
  1364. {
  1365. QemuConsole *con = opaque;
  1366. con->hw_ops->ui_info(con->hw, con->head, &con->ui_info);
  1367. }
  1368. bool dpy_ui_info_supported(QemuConsole *con)
  1369. {
  1370. return con->hw_ops->ui_info != NULL;
  1371. }
  1372. const QemuUIInfo *dpy_get_ui_info(const QemuConsole *con)
  1373. {
  1374. assert(con != NULL);
  1375. return &con->ui_info;
  1376. }
  1377. int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info)
  1378. {
  1379. assert(con != NULL);
  1380. if (!dpy_ui_info_supported(con)) {
  1381. return -1;
  1382. }
  1383. if (memcmp(&con->ui_info, info, sizeof(con->ui_info)) == 0) {
  1384. /* nothing changed -- ignore */
  1385. return 0;
  1386. }
  1387. /*
  1388. * Typically we get a flood of these as the user resizes the window.
  1389. * Wait until the dust has settled (one second without updates), then
  1390. * go notify the guest.
  1391. */
  1392. con->ui_info = *info;
  1393. timer_mod(con->ui_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
  1394. return 0;
  1395. }
  1396. void dpy_gfx_update(QemuConsole *con, int x, int y, int w, int h)
  1397. {
  1398. DisplayState *s = con->ds;
  1399. DisplayChangeListener *dcl;
  1400. int width = w;
  1401. int height = h;
  1402. if (con->surface) {
  1403. width = surface_width(con->surface);
  1404. height = surface_height(con->surface);
  1405. }
  1406. x = MAX(x, 0);
  1407. y = MAX(y, 0);
  1408. x = MIN(x, width);
  1409. y = MIN(y, height);
  1410. w = MIN(w, width - x);
  1411. h = MIN(h, height - y);
  1412. if (!qemu_console_is_visible(con)) {
  1413. return;
  1414. }
  1415. QLIST_FOREACH(dcl, &s->listeners, next) {
  1416. if (con != (dcl->con ? dcl->con : active_console)) {
  1417. continue;
  1418. }
  1419. if (dcl->ops->dpy_gfx_update) {
  1420. dcl->ops->dpy_gfx_update(dcl, x, y, w, h);
  1421. }
  1422. }
  1423. }
  1424. void dpy_gfx_update_full(QemuConsole *con)
  1425. {
  1426. if (!con->surface) {
  1427. return;
  1428. }
  1429. dpy_gfx_update(con, 0, 0,
  1430. surface_width(con->surface),
  1431. surface_height(con->surface));
  1432. }
  1433. void dpy_gfx_replace_surface(QemuConsole *con,
  1434. DisplaySurface *surface)
  1435. {
  1436. DisplayState *s = con->ds;
  1437. DisplaySurface *old_surface = con->surface;
  1438. DisplayChangeListener *dcl;
  1439. assert(old_surface != surface || surface == NULL);
  1440. con->surface = surface;
  1441. QLIST_FOREACH(dcl, &s->listeners, next) {
  1442. if (con != (dcl->con ? dcl->con : active_console)) {
  1443. continue;
  1444. }
  1445. if (dcl->ops->dpy_gfx_switch) {
  1446. dcl->ops->dpy_gfx_switch(dcl, surface);
  1447. }
  1448. }
  1449. qemu_free_displaysurface(old_surface);
  1450. }
  1451. bool dpy_gfx_check_format(QemuConsole *con,
  1452. pixman_format_code_t format)
  1453. {
  1454. DisplayChangeListener *dcl;
  1455. DisplayState *s = con->ds;
  1456. QLIST_FOREACH(dcl, &s->listeners, next) {
  1457. if (dcl->con && dcl->con != con) {
  1458. /* dcl bound to another console -> skip */
  1459. continue;
  1460. }
  1461. if (dcl->ops->dpy_gfx_check_format) {
  1462. if (!dcl->ops->dpy_gfx_check_format(dcl, format)) {
  1463. return false;
  1464. }
  1465. } else {
  1466. /* default is to whitelist native 32 bpp only */
  1467. if (format != qemu_default_pixman_format(32, true)) {
  1468. return false;
  1469. }
  1470. }
  1471. }
  1472. return true;
  1473. }
  1474. static void dpy_refresh(DisplayState *s)
  1475. {
  1476. DisplayChangeListener *dcl;
  1477. QLIST_FOREACH(dcl, &s->listeners, next) {
  1478. if (dcl->ops->dpy_refresh) {
  1479. dcl->ops->dpy_refresh(dcl);
  1480. }
  1481. }
  1482. }
  1483. void dpy_text_cursor(QemuConsole *con, int x, int y)
  1484. {
  1485. DisplayState *s = con->ds;
  1486. DisplayChangeListener *dcl;
  1487. if (!qemu_console_is_visible(con)) {
  1488. return;
  1489. }
  1490. QLIST_FOREACH(dcl, &s->listeners, next) {
  1491. if (con != (dcl->con ? dcl->con : active_console)) {
  1492. continue;
  1493. }
  1494. if (dcl->ops->dpy_text_cursor) {
  1495. dcl->ops->dpy_text_cursor(dcl, x, y);
  1496. }
  1497. }
  1498. }
  1499. void dpy_text_update(QemuConsole *con, int x, int y, int w, int h)
  1500. {
  1501. DisplayState *s = con->ds;
  1502. DisplayChangeListener *dcl;
  1503. if (!qemu_console_is_visible(con)) {
  1504. return;
  1505. }
  1506. QLIST_FOREACH(dcl, &s->listeners, next) {
  1507. if (con != (dcl->con ? dcl->con : active_console)) {
  1508. continue;
  1509. }
  1510. if (dcl->ops->dpy_text_update) {
  1511. dcl->ops->dpy_text_update(dcl, x, y, w, h);
  1512. }
  1513. }
  1514. }
  1515. void dpy_text_resize(QemuConsole *con, int w, int h)
  1516. {
  1517. DisplayState *s = con->ds;
  1518. DisplayChangeListener *dcl;
  1519. if (!qemu_console_is_visible(con)) {
  1520. return;
  1521. }
  1522. QLIST_FOREACH(dcl, &s->listeners, next) {
  1523. if (con != (dcl->con ? dcl->con : active_console)) {
  1524. continue;
  1525. }
  1526. if (dcl->ops->dpy_text_resize) {
  1527. dcl->ops->dpy_text_resize(dcl, w, h);
  1528. }
  1529. }
  1530. }
  1531. void dpy_mouse_set(QemuConsole *con, int x, int y, int on)
  1532. {
  1533. DisplayState *s = con->ds;
  1534. DisplayChangeListener *dcl;
  1535. if (!qemu_console_is_visible(con)) {
  1536. return;
  1537. }
  1538. QLIST_FOREACH(dcl, &s->listeners, next) {
  1539. if (con != (dcl->con ? dcl->con : active_console)) {
  1540. continue;
  1541. }
  1542. if (dcl->ops->dpy_mouse_set) {
  1543. dcl->ops->dpy_mouse_set(dcl, x, y, on);
  1544. }
  1545. }
  1546. }
  1547. void dpy_cursor_define(QemuConsole *con, QEMUCursor *cursor)
  1548. {
  1549. DisplayState *s = con->ds;
  1550. DisplayChangeListener *dcl;
  1551. if (!qemu_console_is_visible(con)) {
  1552. return;
  1553. }
  1554. QLIST_FOREACH(dcl, &s->listeners, next) {
  1555. if (con != (dcl->con ? dcl->con : active_console)) {
  1556. continue;
  1557. }
  1558. if (dcl->ops->dpy_cursor_define) {
  1559. dcl->ops->dpy_cursor_define(dcl, cursor);
  1560. }
  1561. }
  1562. }
  1563. bool dpy_cursor_define_supported(QemuConsole *con)
  1564. {
  1565. DisplayState *s = con->ds;
  1566. DisplayChangeListener *dcl;
  1567. QLIST_FOREACH(dcl, &s->listeners, next) {
  1568. if (dcl->ops->dpy_cursor_define) {
  1569. return true;
  1570. }
  1571. }
  1572. return false;
  1573. }
  1574. QEMUGLContext dpy_gl_ctx_create(QemuConsole *con,
  1575. struct QEMUGLParams *qparams)
  1576. {
  1577. assert(con->gl);
  1578. return con->gl->ops->dpy_gl_ctx_create(con->gl, qparams);
  1579. }
  1580. void dpy_gl_ctx_destroy(QemuConsole *con, QEMUGLContext ctx)
  1581. {
  1582. assert(con->gl);
  1583. con->gl->ops->dpy_gl_ctx_destroy(con->gl, ctx);
  1584. }
  1585. int dpy_gl_ctx_make_current(QemuConsole *con, QEMUGLContext ctx)
  1586. {
  1587. assert(con->gl);
  1588. return con->gl->ops->dpy_gl_ctx_make_current(con->gl, ctx);
  1589. }
  1590. QEMUGLContext dpy_gl_ctx_get_current(QemuConsole *con)
  1591. {
  1592. assert(con->gl);
  1593. return con->gl->ops->dpy_gl_ctx_get_current(con->gl);
  1594. }
  1595. void dpy_gl_scanout_disable(QemuConsole *con)
  1596. {
  1597. assert(con->gl);
  1598. if (con->gl->ops->dpy_gl_scanout_disable) {
  1599. con->gl->ops->dpy_gl_scanout_disable(con->gl);
  1600. } else {
  1601. con->gl->ops->dpy_gl_scanout_texture(con->gl, 0, false, 0, 0,
  1602. 0, 0, 0, 0);
  1603. }
  1604. }
  1605. void dpy_gl_scanout_texture(QemuConsole *con,
  1606. uint32_t backing_id,
  1607. bool backing_y_0_top,
  1608. uint32_t backing_width,
  1609. uint32_t backing_height,
  1610. uint32_t x, uint32_t y,
  1611. uint32_t width, uint32_t height)
  1612. {
  1613. assert(con->gl);
  1614. con->gl->ops->dpy_gl_scanout_texture(con->gl, backing_id,
  1615. backing_y_0_top,
  1616. backing_width, backing_height,
  1617. x, y, width, height);
  1618. }
  1619. void dpy_gl_scanout_dmabuf(QemuConsole *con,
  1620. QemuDmaBuf *dmabuf)
  1621. {
  1622. assert(con->gl);
  1623. con->gl->ops->dpy_gl_scanout_dmabuf(con->gl, dmabuf);
  1624. }
  1625. void dpy_gl_cursor_dmabuf(QemuConsole *con, QemuDmaBuf *dmabuf,
  1626. bool have_hot, uint32_t hot_x, uint32_t hot_y)
  1627. {
  1628. assert(con->gl);
  1629. if (con->gl->ops->dpy_gl_cursor_dmabuf) {
  1630. con->gl->ops->dpy_gl_cursor_dmabuf(con->gl, dmabuf,
  1631. have_hot, hot_x, hot_y);
  1632. }
  1633. }
  1634. void dpy_gl_cursor_position(QemuConsole *con,
  1635. uint32_t pos_x, uint32_t pos_y)
  1636. {
  1637. assert(con->gl);
  1638. if (con->gl->ops->dpy_gl_cursor_position) {
  1639. con->gl->ops->dpy_gl_cursor_position(con->gl, pos_x, pos_y);
  1640. }
  1641. }
  1642. void dpy_gl_release_dmabuf(QemuConsole *con,
  1643. QemuDmaBuf *dmabuf)
  1644. {
  1645. assert(con->gl);
  1646. if (con->gl->ops->dpy_gl_release_dmabuf) {
  1647. con->gl->ops->dpy_gl_release_dmabuf(con->gl, dmabuf);
  1648. }
  1649. }
  1650. void dpy_gl_update(QemuConsole *con,
  1651. uint32_t x, uint32_t y, uint32_t w, uint32_t h)
  1652. {
  1653. assert(con->gl);
  1654. con->gl->ops->dpy_gl_update(con->gl, x, y, w, h);
  1655. }
  1656. /***********************************************************/
  1657. /* register display */
  1658. /* console.c internal use only */
  1659. static DisplayState *get_alloc_displaystate(void)
  1660. {
  1661. if (!display_state) {
  1662. display_state = g_new0(DisplayState, 1);
  1663. cursor_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
  1664. text_console_update_cursor, NULL);
  1665. }
  1666. return display_state;
  1667. }
  1668. /*
  1669. * Called by main(), after creating QemuConsoles
  1670. * and before initializing ui (sdl/vnc/...).
  1671. */
  1672. DisplayState *init_displaystate(void)
  1673. {
  1674. gchar *name;
  1675. QemuConsole *con;
  1676. get_alloc_displaystate();
  1677. QTAILQ_FOREACH(con, &consoles, next) {
  1678. if (con->console_type != GRAPHIC_CONSOLE &&
  1679. con->ds == NULL) {
  1680. text_console_do_init(con->chr, display_state);
  1681. }
  1682. /* Hook up into the qom tree here (not in new_console()), once
  1683. * all QemuConsoles are created and the order / numbering
  1684. * doesn't change any more */
  1685. name = g_strdup_printf("console[%d]", con->index);
  1686. object_property_add_child(container_get(object_get_root(), "/backend"),
  1687. name, OBJECT(con));
  1688. g_free(name);
  1689. }
  1690. return display_state;
  1691. }
  1692. void graphic_console_set_hwops(QemuConsole *con,
  1693. const GraphicHwOps *hw_ops,
  1694. void *opaque)
  1695. {
  1696. con->hw_ops = hw_ops;
  1697. con->hw = opaque;
  1698. }
  1699. QemuConsole *graphic_console_init(DeviceState *dev, uint32_t head,
  1700. const GraphicHwOps *hw_ops,
  1701. void *opaque)
  1702. {
  1703. static const char noinit[] =
  1704. "Guest has not initialized the display (yet).";
  1705. int width = 640;
  1706. int height = 480;
  1707. QemuConsole *s;
  1708. DisplayState *ds;
  1709. DisplaySurface *surface;
  1710. ds = get_alloc_displaystate();
  1711. s = qemu_console_lookup_unused();
  1712. if (s) {
  1713. trace_console_gfx_reuse(s->index);
  1714. if (s->surface) {
  1715. width = surface_width(s->surface);
  1716. height = surface_height(s->surface);
  1717. }
  1718. } else {
  1719. trace_console_gfx_new();
  1720. s = new_console(ds, GRAPHIC_CONSOLE, head);
  1721. s->ui_timer = timer_new_ms(QEMU_CLOCK_REALTIME,
  1722. dpy_set_ui_info_timer, s);
  1723. }
  1724. graphic_console_set_hwops(s, hw_ops, opaque);
  1725. if (dev) {
  1726. object_property_set_link(OBJECT(s), "device", OBJECT(dev),
  1727. &error_abort);
  1728. }
  1729. surface = qemu_create_message_surface(width, height, noinit);
  1730. dpy_gfx_replace_surface(s, surface);
  1731. return s;
  1732. }
  1733. static const GraphicHwOps unused_ops = {
  1734. /* no callbacks */
  1735. };
  1736. void graphic_console_close(QemuConsole *con)
  1737. {
  1738. static const char unplugged[] =
  1739. "Guest display has been unplugged";
  1740. DisplaySurface *surface;
  1741. int width = 640;
  1742. int height = 480;
  1743. if (con->surface) {
  1744. width = surface_width(con->surface);
  1745. height = surface_height(con->surface);
  1746. }
  1747. trace_console_gfx_close(con->index);
  1748. object_property_set_link(OBJECT(con), "device", NULL, &error_abort);
  1749. graphic_console_set_hwops(con, &unused_ops, NULL);
  1750. if (con->gl) {
  1751. dpy_gl_scanout_disable(con);
  1752. }
  1753. surface = qemu_create_message_surface(width, height, unplugged);
  1754. dpy_gfx_replace_surface(con, surface);
  1755. }
  1756. QemuConsole *qemu_console_lookup_by_index(unsigned int index)
  1757. {
  1758. QemuConsole *con;
  1759. QTAILQ_FOREACH(con, &consoles, next) {
  1760. if (con->index == index) {
  1761. return con;
  1762. }
  1763. }
  1764. return NULL;
  1765. }
  1766. QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head)
  1767. {
  1768. QemuConsole *con;
  1769. Object *obj;
  1770. uint32_t h;
  1771. QTAILQ_FOREACH(con, &consoles, next) {
  1772. obj = object_property_get_link(OBJECT(con),
  1773. "device", &error_abort);
  1774. if (DEVICE(obj) != dev) {
  1775. continue;
  1776. }
  1777. h = object_property_get_uint(OBJECT(con),
  1778. "head", &error_abort);
  1779. if (h != head) {
  1780. continue;
  1781. }
  1782. return con;
  1783. }
  1784. return NULL;
  1785. }
  1786. QemuConsole *qemu_console_lookup_by_device_name(const char *device_id,
  1787. uint32_t head, Error **errp)
  1788. {
  1789. DeviceState *dev;
  1790. QemuConsole *con;
  1791. dev = qdev_find_recursive(sysbus_get_default(), device_id);
  1792. if (dev == NULL) {
  1793. error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
  1794. "Device '%s' not found", device_id);
  1795. return NULL;
  1796. }
  1797. con = qemu_console_lookup_by_device(dev, head);
  1798. if (con == NULL) {
  1799. error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole",
  1800. device_id, head);
  1801. return NULL;
  1802. }
  1803. return con;
  1804. }
  1805. QemuConsole *qemu_console_lookup_unused(void)
  1806. {
  1807. QemuConsole *con;
  1808. Object *obj;
  1809. QTAILQ_FOREACH(con, &consoles, next) {
  1810. if (con->hw_ops != &unused_ops) {
  1811. continue;
  1812. }
  1813. obj = object_property_get_link(OBJECT(con),
  1814. "device", &error_abort);
  1815. if (obj != NULL) {
  1816. continue;
  1817. }
  1818. return con;
  1819. }
  1820. return NULL;
  1821. }
  1822. bool qemu_console_is_visible(QemuConsole *con)
  1823. {
  1824. return (con == active_console) || (con->dcls > 0);
  1825. }
  1826. bool qemu_console_is_graphic(QemuConsole *con)
  1827. {
  1828. if (con == NULL) {
  1829. con = active_console;
  1830. }
  1831. return con && (con->console_type == GRAPHIC_CONSOLE);
  1832. }
  1833. bool qemu_console_is_fixedsize(QemuConsole *con)
  1834. {
  1835. if (con == NULL) {
  1836. con = active_console;
  1837. }
  1838. return con && (con->console_type != TEXT_CONSOLE);
  1839. }
  1840. bool qemu_console_is_gl_blocked(QemuConsole *con)
  1841. {
  1842. assert(con != NULL);
  1843. return con->gl_block;
  1844. }
  1845. char *qemu_console_get_label(QemuConsole *con)
  1846. {
  1847. if (con->console_type == GRAPHIC_CONSOLE) {
  1848. if (con->device) {
  1849. return g_strdup(object_get_typename(con->device));
  1850. }
  1851. return g_strdup("VGA");
  1852. } else {
  1853. if (con->chr && con->chr->label) {
  1854. return g_strdup(con->chr->label);
  1855. }
  1856. return g_strdup_printf("vc%d", con->index);
  1857. }
  1858. }
  1859. int qemu_console_get_index(QemuConsole *con)
  1860. {
  1861. if (con == NULL) {
  1862. con = active_console;
  1863. }
  1864. return con ? con->index : -1;
  1865. }
  1866. uint32_t qemu_console_get_head(QemuConsole *con)
  1867. {
  1868. if (con == NULL) {
  1869. con = active_console;
  1870. }
  1871. return con ? con->head : -1;
  1872. }
  1873. QemuUIInfo *qemu_console_get_ui_info(QemuConsole *con)
  1874. {
  1875. assert(con != NULL);
  1876. return &con->ui_info;
  1877. }
  1878. int qemu_console_get_width(QemuConsole *con, int fallback)
  1879. {
  1880. if (con == NULL) {
  1881. con = active_console;
  1882. }
  1883. return con ? surface_width(con->surface) : fallback;
  1884. }
  1885. int qemu_console_get_height(QemuConsole *con, int fallback)
  1886. {
  1887. if (con == NULL) {
  1888. con = active_console;
  1889. }
  1890. return con ? surface_height(con->surface) : fallback;
  1891. }
  1892. static void vc_chr_set_echo(Chardev *chr, bool echo)
  1893. {
  1894. VCChardev *drv = VC_CHARDEV(chr);
  1895. QemuConsole *s = drv->console;
  1896. s->echo = echo;
  1897. }
  1898. static void text_console_update_cursor_timer(void)
  1899. {
  1900. timer_mod(cursor_timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME)
  1901. + CONSOLE_CURSOR_PERIOD / 2);
  1902. }
  1903. static void text_console_update_cursor(void *opaque)
  1904. {
  1905. QemuConsole *s;
  1906. int count = 0;
  1907. cursor_visible_phase = !cursor_visible_phase;
  1908. QTAILQ_FOREACH(s, &consoles, next) {
  1909. if (qemu_console_is_graphic(s) ||
  1910. !qemu_console_is_visible(s)) {
  1911. continue;
  1912. }
  1913. count++;
  1914. graphic_hw_invalidate(s);
  1915. }
  1916. if (count) {
  1917. text_console_update_cursor_timer();
  1918. }
  1919. }
  1920. static const GraphicHwOps text_console_ops = {
  1921. .invalidate = text_console_invalidate,
  1922. .text_update = text_console_update,
  1923. };
  1924. static void text_console_do_init(Chardev *chr, DisplayState *ds)
  1925. {
  1926. VCChardev *drv = VC_CHARDEV(chr);
  1927. QemuConsole *s = drv->console;
  1928. int g_width = 80 * FONT_WIDTH;
  1929. int g_height = 24 * FONT_HEIGHT;
  1930. s->out_fifo.buf = s->out_fifo_buf;
  1931. s->out_fifo.buf_size = sizeof(s->out_fifo_buf);
  1932. s->kbd_timer = timer_new_ms(QEMU_CLOCK_REALTIME, kbd_send_chars, s);
  1933. s->ds = ds;
  1934. s->y_displayed = 0;
  1935. s->y_base = 0;
  1936. s->total_height = DEFAULT_BACKSCROLL;
  1937. s->x = 0;
  1938. s->y = 0;
  1939. if (!s->surface) {
  1940. if (active_console && active_console->surface) {
  1941. g_width = surface_width(active_console->surface);
  1942. g_height = surface_height(active_console->surface);
  1943. }
  1944. s->surface = qemu_create_displaysurface(g_width, g_height);
  1945. }
  1946. s->hw_ops = &text_console_ops;
  1947. s->hw = s;
  1948. /* Set text attribute defaults */
  1949. s->t_attrib_default.bold = 0;
  1950. s->t_attrib_default.uline = 0;
  1951. s->t_attrib_default.blink = 0;
  1952. s->t_attrib_default.invers = 0;
  1953. s->t_attrib_default.unvisible = 0;
  1954. s->t_attrib_default.fgcol = QEMU_COLOR_WHITE;
  1955. s->t_attrib_default.bgcol = QEMU_COLOR_BLACK;
  1956. /* set current text attributes to default */
  1957. s->t_attrib = s->t_attrib_default;
  1958. text_console_resize(s);
  1959. if (chr->label) {
  1960. char *msg;
  1961. s->t_attrib.bgcol = QEMU_COLOR_BLUE;
  1962. msg = g_strdup_printf("%s console\r\n", chr->label);
  1963. vc_chr_write(chr, (uint8_t *)msg, strlen(msg));
  1964. g_free(msg);
  1965. s->t_attrib = s->t_attrib_default;
  1966. }
  1967. qemu_chr_be_event(chr, CHR_EVENT_OPENED);
  1968. }
  1969. static void vc_chr_open(Chardev *chr,
  1970. ChardevBackend *backend,
  1971. bool *be_opened,
  1972. Error **errp)
  1973. {
  1974. ChardevVC *vc = backend->u.vc.data;
  1975. VCChardev *drv = VC_CHARDEV(chr);
  1976. QemuConsole *s;
  1977. unsigned width = 0;
  1978. unsigned height = 0;
  1979. if (vc->has_width) {
  1980. width = vc->width;
  1981. } else if (vc->has_cols) {
  1982. width = vc->cols * FONT_WIDTH;
  1983. }
  1984. if (vc->has_height) {
  1985. height = vc->height;
  1986. } else if (vc->has_rows) {
  1987. height = vc->rows * FONT_HEIGHT;
  1988. }
  1989. trace_console_txt_new(width, height);
  1990. if (width == 0 || height == 0) {
  1991. s = new_console(NULL, TEXT_CONSOLE, 0);
  1992. } else {
  1993. s = new_console(NULL, TEXT_CONSOLE_FIXED_SIZE, 0);
  1994. s->surface = qemu_create_displaysurface(width, height);
  1995. }
  1996. if (!s) {
  1997. error_setg(errp, "cannot create text console");
  1998. return;
  1999. }
  2000. s->chr = chr;
  2001. drv->console = s;
  2002. if (display_state) {
  2003. text_console_do_init(chr, display_state);
  2004. }
  2005. /* console/chardev init sometimes completes elsewhere in a 2nd
  2006. * stage, so defer OPENED events until they are fully initialized
  2007. */
  2008. *be_opened = false;
  2009. }
  2010. void qemu_console_resize(QemuConsole *s, int width, int height)
  2011. {
  2012. DisplaySurface *surface;
  2013. assert(s->console_type == GRAPHIC_CONSOLE);
  2014. if (s->surface && (s->surface->flags & QEMU_ALLOCATED_FLAG) &&
  2015. pixman_image_get_width(s->surface->image) == width &&
  2016. pixman_image_get_height(s->surface->image) == height) {
  2017. return;
  2018. }
  2019. surface = qemu_create_displaysurface(width, height);
  2020. dpy_gfx_replace_surface(s, surface);
  2021. }
  2022. DisplaySurface *qemu_console_surface(QemuConsole *console)
  2023. {
  2024. return console->surface;
  2025. }
  2026. PixelFormat qemu_default_pixelformat(int bpp)
  2027. {
  2028. pixman_format_code_t fmt = qemu_default_pixman_format(bpp, true);
  2029. PixelFormat pf = qemu_pixelformat_from_pixman(fmt);
  2030. return pf;
  2031. }
  2032. static QemuDisplay *dpys[DISPLAY_TYPE__MAX];
  2033. void qemu_display_register(QemuDisplay *ui)
  2034. {
  2035. assert(ui->type < DISPLAY_TYPE__MAX);
  2036. dpys[ui->type] = ui;
  2037. }
  2038. bool qemu_display_find_default(DisplayOptions *opts)
  2039. {
  2040. static DisplayType prio[] = {
  2041. DISPLAY_TYPE_GTK,
  2042. DISPLAY_TYPE_SDL,
  2043. DISPLAY_TYPE_COCOA
  2044. };
  2045. int i;
  2046. for (i = 0; i < ARRAY_SIZE(prio); i++) {
  2047. if (dpys[prio[i]] == NULL) {
  2048. ui_module_load_one(DisplayType_str(prio[i]));
  2049. }
  2050. if (dpys[prio[i]] == NULL) {
  2051. continue;
  2052. }
  2053. opts->type = prio[i];
  2054. return true;
  2055. }
  2056. return false;
  2057. }
  2058. void qemu_display_early_init(DisplayOptions *opts)
  2059. {
  2060. assert(opts->type < DISPLAY_TYPE__MAX);
  2061. if (opts->type == DISPLAY_TYPE_NONE) {
  2062. return;
  2063. }
  2064. if (dpys[opts->type] == NULL) {
  2065. ui_module_load_one(DisplayType_str(opts->type));
  2066. }
  2067. if (dpys[opts->type] == NULL) {
  2068. error_report("Display '%s' is not available.",
  2069. DisplayType_str(opts->type));
  2070. exit(1);
  2071. }
  2072. if (dpys[opts->type]->early_init) {
  2073. dpys[opts->type]->early_init(opts);
  2074. }
  2075. }
  2076. void qemu_display_init(DisplayState *ds, DisplayOptions *opts)
  2077. {
  2078. assert(opts->type < DISPLAY_TYPE__MAX);
  2079. if (opts->type == DISPLAY_TYPE_NONE) {
  2080. return;
  2081. }
  2082. assert(dpys[opts->type] != NULL);
  2083. dpys[opts->type]->init(ds, opts);
  2084. }
  2085. void qemu_display_help(void)
  2086. {
  2087. int idx;
  2088. printf("Available display backend types:\n");
  2089. printf("none\n");
  2090. for (idx = DISPLAY_TYPE_NONE; idx < DISPLAY_TYPE__MAX; idx++) {
  2091. if (!dpys[idx]) {
  2092. ui_module_load_one(DisplayType_str(idx));
  2093. }
  2094. if (dpys[idx]) {
  2095. printf("%s\n", DisplayType_str(dpys[idx]->type));
  2096. }
  2097. }
  2098. }
  2099. void qemu_chr_parse_vc(QemuOpts *opts, ChardevBackend *backend, Error **errp)
  2100. {
  2101. int val;
  2102. ChardevVC *vc;
  2103. backend->type = CHARDEV_BACKEND_KIND_VC;
  2104. vc = backend->u.vc.data = g_new0(ChardevVC, 1);
  2105. qemu_chr_parse_common(opts, qapi_ChardevVC_base(vc));
  2106. val = qemu_opt_get_number(opts, "width", 0);
  2107. if (val != 0) {
  2108. vc->has_width = true;
  2109. vc->width = val;
  2110. }
  2111. val = qemu_opt_get_number(opts, "height", 0);
  2112. if (val != 0) {
  2113. vc->has_height = true;
  2114. vc->height = val;
  2115. }
  2116. val = qemu_opt_get_number(opts, "cols", 0);
  2117. if (val != 0) {
  2118. vc->has_cols = true;
  2119. vc->cols = val;
  2120. }
  2121. val = qemu_opt_get_number(opts, "rows", 0);
  2122. if (val != 0) {
  2123. vc->has_rows = true;
  2124. vc->rows = val;
  2125. }
  2126. }
  2127. static const TypeInfo qemu_console_info = {
  2128. .name = TYPE_QEMU_CONSOLE,
  2129. .parent = TYPE_OBJECT,
  2130. .instance_size = sizeof(QemuConsole),
  2131. .class_size = sizeof(QemuConsoleClass),
  2132. };
  2133. static void char_vc_class_init(ObjectClass *oc, void *data)
  2134. {
  2135. ChardevClass *cc = CHARDEV_CLASS(oc);
  2136. cc->parse = qemu_chr_parse_vc;
  2137. cc->open = vc_chr_open;
  2138. cc->chr_write = vc_chr_write;
  2139. cc->chr_set_echo = vc_chr_set_echo;
  2140. }
  2141. static const TypeInfo char_vc_type_info = {
  2142. .name = TYPE_CHARDEV_VC,
  2143. .parent = TYPE_CHARDEV,
  2144. .instance_size = sizeof(VCChardev),
  2145. .class_init = char_vc_class_init,
  2146. };
  2147. void qemu_console_early_init(void)
  2148. {
  2149. /* set the default vc driver */
  2150. if (!object_class_by_name(TYPE_CHARDEV_VC)) {
  2151. type_register(&char_vc_type_info);
  2152. }
  2153. }
  2154. static void register_types(void)
  2155. {
  2156. type_register_static(&qemu_console_info);
  2157. }
  2158. type_init(register_types);