video_console.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (c) 2015 Google, Inc
  4. */
  5. #ifndef __video_console_h
  6. #define __video_console_h
  7. #include <video.h>
  8. struct video_priv;
  9. #define VID_FRAC_DIV 256
  10. #define VID_TO_PIXEL(x) ((x) / VID_FRAC_DIV)
  11. #define VID_TO_POS(x) ((x) * VID_FRAC_DIV)
  12. /**
  13. * struct vidconsole_priv - uclass-private data about a console device
  14. *
  15. * Drivers must set up @rows, @cols, @x_charsize, @y_charsize in their probe()
  16. * method. Drivers may set up @xstart_frac if desired.
  17. *
  18. * @sdev: stdio device, acting as an output sink
  19. * @xcur_frac: Current X position, in fractional units (VID_TO_POS(x))
  20. * @ycur: Current Y position in pixels (0=top)
  21. * @rows: Number of text rows
  22. * @cols: Number of text columns
  23. * @x_charsize: Character width in pixels
  24. * @y_charsize: Character height in pixels
  25. * @tab_width_frac: Tab width in fractional units
  26. * @xsize_frac: Width of the display in fractional units
  27. * @xstart_frac: Left margin for the text console in fractional units
  28. * @last_ch: Last character written to the text console on this line
  29. * @escape: TRUE if currently accumulating an ANSI escape sequence
  30. * @escape_len: Length of accumulated escape sequence so far
  31. * @col_saved: Saved X position, in fractional units (VID_TO_POS(x))
  32. * @row_saved: Saved Y position in pixels (0=top)
  33. * @escape_buf: Buffer to accumulate escape sequence
  34. */
  35. struct vidconsole_priv {
  36. struct stdio_dev sdev;
  37. int xcur_frac;
  38. int ycur;
  39. int rows;
  40. int cols;
  41. int x_charsize;
  42. int y_charsize;
  43. int tab_width_frac;
  44. int xsize_frac;
  45. int xstart_frac;
  46. int last_ch;
  47. /*
  48. * ANSI escape sequences are accumulated character by character,
  49. * starting after the ESC char (0x1b) until the entire sequence
  50. * is consumed at which point it is acted upon.
  51. */
  52. int escape;
  53. int escape_len;
  54. int row_saved;
  55. int col_saved;
  56. char escape_buf[32];
  57. };
  58. /**
  59. * struct vidfont_info - information about a font
  60. *
  61. * @name: Font name, e.g. nimbus_sans_l_regular
  62. */
  63. struct vidfont_info {
  64. const char *name;
  65. };
  66. /**
  67. * struct vidconsole_colour - Holds colour information
  68. *
  69. * @colour_fg: Foreground colour (pixel value)
  70. * @colour_bg: Background colour (pixel value)
  71. */
  72. struct vidconsole_colour {
  73. u32 colour_fg;
  74. u32 colour_bg;
  75. };
  76. /**
  77. * struct vidconsole_bbox - Bounding box of text
  78. *
  79. * This describes the bounding box of something, measured in pixels. The x0/y0
  80. * pair is inclusive; the x1/y2 pair is exclusive, meaning that it is one pixel
  81. * beyond the extent of the object
  82. *
  83. * @valid: Values are valid (bounding box is known)
  84. * @x0: left x position, in pixels from left side
  85. * @y0: top y position, in pixels from top
  86. * @x1: right x position + 1
  87. * @y1: botton y position + 1
  88. */
  89. struct vidconsole_bbox {
  90. bool valid;
  91. int x0;
  92. int y0;
  93. int x1;
  94. int y1;
  95. };
  96. /**
  97. * struct vidconsole_ops - Video console operations
  98. *
  99. * These operations work on either an absolute console position (measured
  100. * in pixels) or a text row number (measured in rows, where each row consists
  101. * of an entire line of text - typically 16 pixels).
  102. */
  103. struct vidconsole_ops {
  104. /**
  105. * putc_xy() - write a single character to a position
  106. *
  107. * @dev: Device to write to
  108. * @x_frac: Fractional pixel X position (0=left-most pixel) which
  109. * is the X position multipled by VID_FRAC_DIV.
  110. * @y: Pixel Y position (0=top-most pixel)
  111. * @ch: Character to write
  112. * @return number of fractional pixels that the cursor should move,
  113. * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
  114. * on error
  115. */
  116. int (*putc_xy)(struct udevice *dev, uint x_frac, uint y, char ch);
  117. /**
  118. * move_rows() - Move text rows from one place to another
  119. *
  120. * @dev: Device to adjust
  121. * @rowdst: Destination text row (0=top)
  122. * @rowsrc: Source start text row
  123. * @count: Number of text rows to move
  124. * @return 0 if OK, -ve on error
  125. */
  126. int (*move_rows)(struct udevice *dev, uint rowdst, uint rowsrc,
  127. uint count);
  128. /**
  129. * set_row() - Set the colour of a text row
  130. *
  131. * Every pixel contained within the text row is adjusted
  132. *
  133. * @dev: Device to adjust
  134. * @row: Text row to adjust (0=top)
  135. * @clr: Raw colour (pixel value) to write to each pixel
  136. * @return 0 if OK, -ve on error
  137. */
  138. int (*set_row)(struct udevice *dev, uint row, int clr);
  139. /**
  140. * entry_start() - Indicate that text entry is starting afresh
  141. *
  142. * @dev: Device to adjust
  143. * Returns: 0 on success, -ve on error
  144. *
  145. * Consoles which use proportional fonts need to track the position of
  146. * each character output so that backspace will return to the correct
  147. * place. This method signals to the console driver that a new entry
  148. * line is being start (e.g. the user pressed return to start a new
  149. * command). The driver can use this signal to empty its list of
  150. * positions.
  151. */
  152. int (*entry_start)(struct udevice *dev);
  153. /**
  154. * backspace() - Handle erasing the last character
  155. *
  156. * @dev: Device to adjust
  157. * Returns: 0 on success, -ve on error
  158. *
  159. * With proportional fonts the vidconsole uclass cannot itself erase
  160. * the previous character. This optional method will be called when
  161. * a backspace is needed. The driver should erase the previous
  162. * character and update the cursor position (xcur_frac, ycur) to the
  163. * start of the previous character.
  164. *
  165. * If not implement, default behaviour will work for fixed-width
  166. * characters.
  167. */
  168. int (*backspace)(struct udevice *dev);
  169. /**
  170. * get_font() - Obtain information about a font (optional)
  171. *
  172. * @dev: Device to check
  173. * @seq: Font number to query (0=first, 1=second, etc.)
  174. * @info: Returns font information on success
  175. * Returns: 0 on success, -ENOENT if no such font
  176. */
  177. int (*get_font)(struct udevice *dev, int seq,
  178. struct vidfont_info *info);
  179. /**
  180. * get_font_size() - get the current font name and size
  181. *
  182. * @dev: vidconsole device
  183. * @sizep: Place to put the font size (nominal height in pixels)
  184. * Returns: Current font name
  185. */
  186. const char *(*get_font_size)(struct udevice *dev, uint *sizep);
  187. /**
  188. * select_font() - Select a particular font by name / size
  189. *
  190. * @dev: Device to adjust
  191. * @name: Font name to use (NULL to use default)
  192. * @size: Font size to use (0 to use default)
  193. * Returns: 0 on success, -ENOENT if no such font
  194. */
  195. int (*select_font)(struct udevice *dev, const char *name, uint size);
  196. /**
  197. * measure() - Measure the bounds of some text
  198. *
  199. * @dev: Device to adjust
  200. * @name: Font name to use (NULL to use default)
  201. * @size: Font size to use (0 to use default)
  202. * @text: Text to measure
  203. * @bbox: Returns bounding box of text, assuming it is positioned
  204. * at 0,0
  205. * Returns: 0 on success, -ENOENT if no such font
  206. */
  207. int (*measure)(struct udevice *dev, const char *name, uint size,
  208. const char *text, struct vidconsole_bbox *bbox);
  209. };
  210. /* Get a pointer to the driver operations for a video console device */
  211. #define vidconsole_get_ops(dev) ((struct vidconsole_ops *)(dev)->driver->ops)
  212. /**
  213. * vidconsole_get_font() - Obtain information about a font
  214. *
  215. * @dev: Device to check
  216. * @seq: Font number to query (0=first, 1=second, etc.)
  217. * @info: Returns font information on success
  218. * Returns: 0 on success, -ENOENT if no such font, -ENOSYS if there is no such
  219. * method
  220. */
  221. int vidconsole_get_font(struct udevice *dev, int seq,
  222. struct vidfont_info *info);
  223. /**
  224. * vidconsole_select_font() - Select a particular font by name / size
  225. *
  226. * @dev: Device to adjust
  227. * @name: Font name to use (NULL to use default)
  228. * @size: Font size to use (0 to use default)
  229. */
  230. int vidconsole_select_font(struct udevice *dev, const char *name, uint size);
  231. /*
  232. * vidconsole_measure() - Measuring the bounding box of some text
  233. *
  234. * @dev: Console device to use
  235. * @name: Font name, NULL for default
  236. * @size: Font size, ignored if @name is NULL
  237. * @text: Text to measure
  238. * @bbox: Returns nounding box of text
  239. * Returns: 0 if OK, -ve on error
  240. */
  241. int vidconsole_measure(struct udevice *dev, const char *name, uint size,
  242. const char *text, struct vidconsole_bbox *bbox);
  243. /**
  244. * vidconsole_push_colour() - Temporarily change the font colour
  245. *
  246. * @dev: Device to adjust
  247. * @fg: Foreground colour to select
  248. * @bg: Background colour to select
  249. * @old: Place to store the current colour, so it can be restored
  250. */
  251. void vidconsole_push_colour(struct udevice *dev, enum colour_idx fg,
  252. enum colour_idx bg, struct vidconsole_colour *old);
  253. /**
  254. * vidconsole_pop_colour() - Restore the original colour
  255. *
  256. * @dev: Device to adjust
  257. * @old: Old colour to be restored
  258. */
  259. void vidconsole_pop_colour(struct udevice *dev, struct vidconsole_colour *old);
  260. /**
  261. * vidconsole_putc_xy() - write a single character to a position
  262. *
  263. * @dev: Device to write to
  264. * @x_frac: Fractional pixel X position (0=left-most pixel) which
  265. * is the X position multipled by VID_FRAC_DIV.
  266. * @y: Pixel Y position (0=top-most pixel)
  267. * @ch: Character to write
  268. * Return: number of fractional pixels that the cursor should move,
  269. * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
  270. * on error
  271. */
  272. int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch);
  273. /**
  274. * vidconsole_move_rows() - Move text rows from one place to another
  275. *
  276. * @dev: Device to adjust
  277. * @rowdst: Destination text row (0=top)
  278. * @rowsrc: Source start text row
  279. * @count: Number of text rows to move
  280. * Return: 0 if OK, -ve on error
  281. */
  282. int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
  283. uint count);
  284. /**
  285. * vidconsole_set_row() - Set the colour of a text row
  286. *
  287. * Every pixel contained within the text row is adjusted
  288. *
  289. * @dev: Device to adjust
  290. * @row: Text row to adjust (0=top)
  291. * @clr: Raw colour (pixel value) to write to each pixel
  292. * Return: 0 if OK, -ve on error
  293. */
  294. int vidconsole_set_row(struct udevice *dev, uint row, int clr);
  295. /**
  296. * vidconsole_put_char() - Output a character to the current console position
  297. *
  298. * Outputs a character to the console and advances the cursor. This function
  299. * handles wrapping to new lines and scrolling the console. Special
  300. * characters are handled also: \n, \r, \b and \t.
  301. *
  302. * The device always starts with the cursor at position 0,0 (top left). It
  303. * can be adjusted manually using vidconsole_position_cursor().
  304. *
  305. * @dev: Device to adjust
  306. * @ch: Character to write
  307. * Return: 0 if OK, -ve on error
  308. */
  309. int vidconsole_put_char(struct udevice *dev, char ch);
  310. /**
  311. * vidconsole_put_string() - Output a string to the current console position
  312. *
  313. * Outputs a string to the console and advances the cursor. This function
  314. * handles wrapping to new lines and scrolling the console. Special
  315. * characters are handled also: \n, \r, \b and \t.
  316. *
  317. * The device always starts with the cursor at position 0,0 (top left). It
  318. * can be adjusted manually using vidconsole_position_cursor().
  319. *
  320. * @dev: Device to adjust
  321. * @str: String to write
  322. * Return: 0 if OK, -ve on error
  323. */
  324. int vidconsole_put_string(struct udevice *dev, const char *str);
  325. /**
  326. * vidconsole_position_cursor() - Move the text cursor
  327. *
  328. * @dev: Device to adjust
  329. * @col: New cursor text column
  330. * @row: New cursor text row
  331. * Return: 0 if OK, -ve on error
  332. */
  333. void vidconsole_position_cursor(struct udevice *dev, unsigned col,
  334. unsigned row);
  335. /**
  336. * vidconsole_clear_and_reset() - Clear the console and reset the cursor
  337. *
  338. * The cursor is placed at the start of the console
  339. *
  340. * @dev: vidconsole device to adjust
  341. */
  342. int vidconsole_clear_and_reset(struct udevice *dev);
  343. /**
  344. * vidconsole_set_cursor_pos() - set cursor position
  345. *
  346. * The cursor is set to the new position and the start-of-line information is
  347. * updated to the same position, so that a newline will return to @x
  348. *
  349. * @dev: video console device to update
  350. * @x: x position from left in pixels
  351. * @y: y position from top in pixels
  352. */
  353. void vidconsole_set_cursor_pos(struct udevice *dev, int x, int y);
  354. /**
  355. * vidconsole_list_fonts() - List the available fonts
  356. *
  357. * @dev: vidconsole device to check
  358. *
  359. * This shows a list of fonts known by this vidconsole. The list is displayed on
  360. * the console (not necessarily @dev but probably)
  361. */
  362. void vidconsole_list_fonts(struct udevice *dev);
  363. /**
  364. * vidconsole_get_font_size() - get the current font name and size
  365. *
  366. * @dev: vidconsole device
  367. * @sizep: Place to put the font size (nominal height in pixels)
  368. * @name: pointer to font name, a placeholder for result
  369. * Return: 0 if OK, -ENOSYS if not implemented in driver
  370. */
  371. int vidconsole_get_font_size(struct udevice *dev, const char **name, uint *sizep);
  372. #ifdef CONFIG_VIDEO_COPY
  373. /**
  374. * vidconsole_sync_copy() - Sync back to the copy framebuffer
  375. *
  376. * This ensures that the copy framebuffer has the same data as the framebuffer
  377. * for a particular region. It should be called after the framebuffer is updated
  378. *
  379. * @from and @to can be in either order. The region between them is synced.
  380. *
  381. * @dev: Vidconsole device being updated
  382. * @from: Start/end address within the framebuffer (->fb)
  383. * @to: Other address within the frame buffer
  384. * Return: 0 if OK, -EFAULT if the start address is before the start of the
  385. * frame buffer start
  386. */
  387. int vidconsole_sync_copy(struct udevice *dev, void *from, void *to);
  388. /**
  389. * vidconsole_memmove() - Perform a memmove() within the frame buffer
  390. *
  391. * This handles a memmove(), e.g. for scrolling. It also updates the copy
  392. * framebuffer.
  393. *
  394. * @dev: Vidconsole device being updated
  395. * @dst: Destination address within the framebuffer (->fb)
  396. * @src: Source address within the framebuffer (->fb)
  397. * @size: Number of bytes to transfer
  398. * Return: 0 if OK, -EFAULT if the start address is before the start of the
  399. * frame buffer start
  400. */
  401. int vidconsole_memmove(struct udevice *dev, void *dst, const void *src,
  402. int size);
  403. #else
  404. #include <string.h>
  405. static inline int vidconsole_sync_copy(struct udevice *dev, void *from,
  406. void *to)
  407. {
  408. return 0;
  409. }
  410. static inline int vidconsole_memmove(struct udevice *dev, void *dst,
  411. const void *src, int size)
  412. {
  413. memmove(dst, src, size);
  414. return 0;
  415. }
  416. #endif
  417. #endif