video_console.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. * The 16 colors supported by the console
  14. */
  15. enum color_idx {
  16. VID_BLACK = 0,
  17. VID_RED,
  18. VID_GREEN,
  19. VID_BROWN,
  20. VID_BLUE,
  21. VID_MAGENTA,
  22. VID_CYAN,
  23. VID_LIGHT_GRAY,
  24. VID_GRAY,
  25. VID_LIGHT_RED,
  26. VID_LIGTH_GREEN,
  27. VID_YELLOW,
  28. VID_LIGHT_BLUE,
  29. VID_LIGHT_MAGENTA,
  30. VID_LIGHT_CYAN,
  31. VID_WHITE,
  32. VID_COLOR_COUNT
  33. };
  34. /**
  35. * struct vidconsole_priv - uclass-private data about a console device
  36. *
  37. * Drivers must set up @rows, @cols, @x_charsize, @y_charsize in their probe()
  38. * method. Drivers may set up @xstart_frac if desired.
  39. *
  40. * @sdev: stdio device, acting as an output sink
  41. * @xcur_frac: Current X position, in fractional units (VID_TO_POS(x))
  42. * @ycur: Current Y position in pixels (0=top)
  43. * @rows: Number of text rows
  44. * @cols: Number of text columns
  45. * @x_charsize: Character width in pixels
  46. * @y_charsize: Character height in pixels
  47. * @tab_width_frac: Tab width in fractional units
  48. * @xsize_frac: Width of the display in fractional units
  49. * @xstart_frac: Left margin for the text console in fractional units
  50. * @last_ch: Last character written to the text console on this line
  51. * @escape: TRUE if currently accumulating an ANSI escape sequence
  52. * @escape_len: Length of accumulated escape sequence so far
  53. * @col_saved: Saved X position, in fractional units (VID_TO_POS(x))
  54. * @row_saved: Saved Y position in pixels (0=top)
  55. * @escape_buf: Buffer to accumulate escape sequence
  56. */
  57. struct vidconsole_priv {
  58. struct stdio_dev sdev;
  59. int xcur_frac;
  60. int ycur;
  61. int rows;
  62. int cols;
  63. int x_charsize;
  64. int y_charsize;
  65. int tab_width_frac;
  66. int xsize_frac;
  67. int xstart_frac;
  68. int last_ch;
  69. /*
  70. * ANSI escape sequences are accumulated character by character,
  71. * starting after the ESC char (0x1b) until the entire sequence
  72. * is consumed at which point it is acted upon.
  73. */
  74. int escape;
  75. int escape_len;
  76. int row_saved;
  77. int col_saved;
  78. char escape_buf[32];
  79. };
  80. /**
  81. * struct vidconsole_ops - Video console operations
  82. *
  83. * These operations work on either an absolute console position (measured
  84. * in pixels) or a text row number (measured in rows, where each row consists
  85. * of an entire line of text - typically 16 pixels).
  86. */
  87. struct vidconsole_ops {
  88. /**
  89. * putc_xy() - write a single character to a position
  90. *
  91. * @dev: Device to write to
  92. * @x_frac: Fractional pixel X position (0=left-most pixel) which
  93. * is the X position multipled by VID_FRAC_DIV.
  94. * @y: Pixel Y position (0=top-most pixel)
  95. * @ch: Character to write
  96. * @return number of fractional pixels that the cursor should move,
  97. * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
  98. * on error
  99. */
  100. int (*putc_xy)(struct udevice *dev, uint x_frac, uint y, char ch);
  101. /**
  102. * move_rows() - Move text rows from one place to another
  103. *
  104. * @dev: Device to adjust
  105. * @rowdst: Destination text row (0=top)
  106. * @rowsrc: Source start text row
  107. * @count: Number of text rows to move
  108. * @return 0 if OK, -ve on error
  109. */
  110. int (*move_rows)(struct udevice *dev, uint rowdst, uint rowsrc,
  111. uint count);
  112. /**
  113. * set_row() - Set the colour of a text row
  114. *
  115. * Every pixel contained within the text row is adjusted
  116. *
  117. * @dev: Device to adjust
  118. * @row: Text row to adjust (0=top)
  119. * @clr: Raw colour (pixel value) to write to each pixel
  120. * @return 0 if OK, -ve on error
  121. */
  122. int (*set_row)(struct udevice *dev, uint row, int clr);
  123. /**
  124. * entry_start() - Indicate that text entry is starting afresh
  125. *
  126. * Consoles which use proportional fonts need to track the position of
  127. * each character output so that backspace will return to the correct
  128. * place. This method signals to the console driver that a new entry
  129. * line is being start (e.g. the user pressed return to start a new
  130. * command). The driver can use this signal to empty its list of
  131. * positions.
  132. */
  133. int (*entry_start)(struct udevice *dev);
  134. /**
  135. * backspace() - Handle erasing the last character
  136. *
  137. * With proportional fonts the vidconsole uclass cannot itself erase
  138. * the previous character. This optional method will be called when
  139. * a backspace is needed. The driver should erase the previous
  140. * character and update the cursor position (xcur_frac, ycur) to the
  141. * start of the previous character.
  142. *
  143. * If not implement, default behaviour will work for fixed-width
  144. * characters.
  145. */
  146. int (*backspace)(struct udevice *dev);
  147. };
  148. /* Get a pointer to the driver operations for a video console device */
  149. #define vidconsole_get_ops(dev) ((struct vidconsole_ops *)(dev)->driver->ops)
  150. /**
  151. * vidconsole_putc_xy() - write a single character to a position
  152. *
  153. * @dev: Device to write to
  154. * @x_frac: Fractional pixel X position (0=left-most pixel) which
  155. * is the X position multipled by VID_FRAC_DIV.
  156. * @y: Pixel Y position (0=top-most pixel)
  157. * @ch: Character to write
  158. * @return number of fractional pixels that the cursor should move,
  159. * if all is OK, -EAGAIN if we ran out of space on this line, other -ve
  160. * on error
  161. */
  162. int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch);
  163. /**
  164. * vidconsole_move_rows() - Move text rows from one place to another
  165. *
  166. * @dev: Device to adjust
  167. * @rowdst: Destination text row (0=top)
  168. * @rowsrc: Source start text row
  169. * @count: Number of text rows to move
  170. * @return 0 if OK, -ve on error
  171. */
  172. int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
  173. uint count);
  174. /**
  175. * vidconsole_set_row() - Set the colour of a text row
  176. *
  177. * Every pixel contained within the text row is adjusted
  178. *
  179. * @dev: Device to adjust
  180. * @row: Text row to adjust (0=top)
  181. * @clr: Raw colour (pixel value) to write to each pixel
  182. * @return 0 if OK, -ve on error
  183. */
  184. int vidconsole_set_row(struct udevice *dev, uint row, int clr);
  185. /**
  186. * vidconsole_put_char() - Output a character to the current console position
  187. *
  188. * Outputs a character to the console and advances the cursor. This function
  189. * handles wrapping to new lines and scrolling the console. Special
  190. * characters are handled also: \n, \r, \b and \t.
  191. *
  192. * The device always starts with the cursor at position 0,0 (top left). It
  193. * can be adjusted manually using vidconsole_position_cursor().
  194. *
  195. * @dev: Device to adjust
  196. * @ch: Character to write
  197. * @return 0 if OK, -ve on error
  198. */
  199. int vidconsole_put_char(struct udevice *dev, char ch);
  200. /**
  201. * vidconsole_put_string() - Output a string to the current console position
  202. *
  203. * Outputs a string to the console and advances the cursor. This function
  204. * handles wrapping to new lines and scrolling the console. Special
  205. * characters are handled also: \n, \r, \b and \t.
  206. *
  207. * The device always starts with the cursor at position 0,0 (top left). It
  208. * can be adjusted manually using vidconsole_position_cursor().
  209. *
  210. * @dev: Device to adjust
  211. * @str: String to write
  212. * @return 0 if OK, -ve on error
  213. */
  214. int vidconsole_put_string(struct udevice *dev, const char *str);
  215. /**
  216. * vidconsole_position_cursor() - Move the text cursor
  217. *
  218. * @dev: Device to adjust
  219. * @col: New cursor text column
  220. * @row: New cursor text row
  221. * @return 0 if OK, -ve on error
  222. */
  223. void vidconsole_position_cursor(struct udevice *dev, unsigned col,
  224. unsigned row);
  225. /**
  226. * vid_console_color() - convert a color code to a pixel's internal
  227. * representation
  228. *
  229. * The caller has to guarantee that the color index is less than
  230. * VID_COLOR_COUNT.
  231. *
  232. * @priv private data of the console device
  233. * @idx color index
  234. * @return color value
  235. */
  236. u32 vid_console_color(struct video_priv *priv, unsigned int idx);
  237. #ifdef CONFIG_VIDEO_COPY
  238. /**
  239. * vidconsole_sync_copy() - Sync back to the copy framebuffer
  240. *
  241. * This ensures that the copy framebuffer has the same data as the framebuffer
  242. * for a particular region. It should be called after the framebuffer is updated
  243. *
  244. * @from and @to can be in either order. The region between them is synced.
  245. *
  246. * @dev: Vidconsole device being updated
  247. * @from: Start/end address within the framebuffer (->fb)
  248. * @to: Other address within the frame buffer
  249. * @return 0 if OK, -EFAULT if the start address is before the start of the
  250. * frame buffer start
  251. */
  252. int vidconsole_sync_copy(struct udevice *dev, void *from, void *to);
  253. /**
  254. * vidconsole_memmove() - Perform a memmove() within the frame buffer
  255. *
  256. * This handles a memmove(), e.g. for scrolling. It also updates the copy
  257. * framebuffer.
  258. *
  259. * @dev: Vidconsole device being updated
  260. * @dst: Destination address within the framebuffer (->fb)
  261. * @src: Source address within the framebuffer (->fb)
  262. * @size: Number of bytes to transfer
  263. * @return 0 if OK, -EFAULT if the start address is before the start of the
  264. * frame buffer start
  265. */
  266. int vidconsole_memmove(struct udevice *dev, void *dst, const void *src,
  267. int size);
  268. #else
  269. static inline int vidconsole_sync_copy(struct udevice *dev, void *from,
  270. void *to)
  271. {
  272. return 0;
  273. }
  274. static inline int vidconsole_memmove(struct udevice *dev, void *dst,
  275. const void *src, int size)
  276. {
  277. memmove(dst, src, size);
  278. return 0;
  279. }
  280. #endif
  281. #endif