libnx  v4.7.0
console.h
Go to the documentation of this file.
1 /**
2  * @file console.h
3  * @brief Framebuffer text console.
4  * @author yellows8
5  * @author WinterMute
6  * @copyright libnx Authors
7  *
8  * Provides stdio integration for printing to the Switch screen as well as debug print
9  * functionality provided by stderr.
10  *
11  * General usage is to initialize the console by:
12  * @code
13  * consoleInit(NULL)
14  * @endcode
15  * optionally customizing the console usage by passing a pointer to a custom PrintConsole struct.
16  */
17 #pragma once
18 #include "../../types.h"
19 
20 #define CONSOLE_ESC(x) "\x1b[" #x
21 #define CONSOLE_RESET CONSOLE_ESC(0m)
22 #define CONSOLE_BLACK CONSOLE_ESC(30m)
23 #define CONSOLE_RED CONSOLE_ESC(31;1m)
24 #define CONSOLE_GREEN CONSOLE_ESC(32;1m)
25 #define CONSOLE_YELLOW CONSOLE_ESC(33;1m)
26 #define CONSOLE_BLUE CONSOLE_ESC(34;1m)
27 #define CONSOLE_MAGENTA CONSOLE_ESC(35;1m)
28 #define CONSOLE_CYAN CONSOLE_ESC(36;1m)
29 #define CONSOLE_WHITE CONSOLE_ESC(37;1m)
30 
31 // Forward declaration
32 typedef struct PrintConsole PrintConsole;
33 
34 /// Renderer interface for the console.
35 typedef struct ConsoleRenderer
36 {
37  bool (*init)(PrintConsole* con);
38  void (*deinit)(PrintConsole* con);
39  void (*drawChar)(PrintConsole* con, int x, int y, int c);
40  void (*scrollWindow)(PrintConsole* con);
41  void (*flushAndSwap)(PrintConsole* con);
43 
44 /// A font struct for the console.
45 typedef struct ConsoleFont
46 {
47  const void* gfx; ///< A pointer to the font graphics
48  u16 asciiOffset; ///< Offset to the first valid character in the font table
49  u16 numChars; ///< Number of characters in the font graphics
50  u16 tileWidth;
51  u16 tileHeight;
53 
54 /**
55  * @brief Console structure used to store the state of a console render context.
56  *
57  * Default values from consoleGetDefault();
58  * @code
59  * PrintConsole defaultConsole =
60  * {
61  * //Font:
62  * {
63  * default_font_bin, //font gfx
64  * 0, //first ascii character in the set
65  * 256, //number of characters in the font set
66  * 16, //tile width
67  * 16, //tile height
68  * },
69  * NULL, //renderer
70  * 0,0, //cursorX cursorY
71  * 0,0, //prevcursorX prevcursorY
72  * 80, //console width
73  * 45, //console height
74  * 0, //window x
75  * 0, //window y
76  * 80, //window width
77  * 45, //window height
78  * 3, //tab size
79  * 7, // foreground color
80  * 0, // background color
81  * 0, // flags
82  * false //console initialized
83  * };
84  * @endcode
85  */
87 {
88  ConsoleFont font; ///< Font of the console
89  ConsoleRenderer* renderer; ///< Renderer of the console
90 
91  int cursorX; ///< Current X location of the cursor (as a tile offset by default)
92  int cursorY; ///< Current Y location of the cursor (as a tile offset by default)
93 
94  int prevCursorX; ///< Internal state
95  int prevCursorY; ///< Internal state
96 
97  int consoleWidth; ///< Width of the console hardware layer in characters
98  int consoleHeight; ///< Height of the console hardware layer in characters
99 
100  int windowX; ///< Window X location in characters
101  int windowY; ///< Window Y location in characters
102  int windowWidth; ///< Window width in characters
103  int windowHeight; ///< Window height in characters
104 
105  int tabSize; ///< Size of a tab
106  u16 fg; ///< Foreground color
107  u16 bg; ///< Background color
108  int flags; ///< Reverse/bright flags
109 
110  bool consoleInitialised; ///< True if the console is initialized
111 };
112 
113 #define CONSOLE_COLOR_BOLD (1<<0) ///< Bold text
114 #define CONSOLE_COLOR_FAINT (1<<1) ///< Faint text
115 #define CONSOLE_ITALIC (1<<2) ///< Italic text
116 #define CONSOLE_UNDERLINE (1<<3) ///< Underlined text
117 #define CONSOLE_BLINK_SLOW (1<<4) ///< Slow blinking text
118 #define CONSOLE_BLINK_FAST (1<<5) ///< Fast blinking text
119 #define CONSOLE_COLOR_REVERSE (1<<6) ///< Reversed color text
120 #define CONSOLE_CONCEAL (1<<7) ///< Concealed text
121 #define CONSOLE_CROSSED_OUT (1<<8) ///< Crossed out text
122 #define CONSOLE_FG_CUSTOM (1<<9) ///< Foreground custom color
123 #define CONSOLE_BG_CUSTOM (1<<10) ///< Background custom color
124 
125 /// Console debug devices supported by libnx.
126 typedef enum {
127  debugDevice_NULL, ///< Swallows prints to stderr
128  debugDevice_SVC, ///< Outputs stderr debug statements using svcOutputDebugString, which can then be captured by interactive debuggers
129  debugDevice_CONSOLE, ///< Directs stderr debug statements to Switch console window
130 } debugDevice;
131 
132 /**
133  * @brief Loads the font into the console.
134  * @param console Pointer to the console to update, if NULL it will update the current console.
135  * @param font The font to load.
136  */
138 
139 /**
140  * @brief Sets the print window.
141  * @param console Console to set, if NULL it will set the current console window.
142  * @param x X location of the window.
143  * @param y Y location of the window.
144  * @param width Width of the window.
145  * @param height Height of the window.
146  */
147 void consoleSetWindow(PrintConsole* console, int x, int y, int width, int height);
148 
149 /**
150  * @brief Gets a pointer to the console with the default values.
151  * This should only be used when using a single console or without changing the console that is returned, otherwise use consoleInit().
152  * @return A pointer to the console with the default values.
153  */
155 
156 /**
157  * @brief Make the specified console the render target.
158  * @param console A pointer to the console struct (must have been initialized with consoleInit(PrintConsole* console)).
159  * @return A pointer to the previous console.
160  */
162 
163 /**
164  * @brief Initialise the console.
165  * @param console A pointer to the console data to initialize (if it's NULL, the default console will be used).
166  * @return A pointer to the current console.
167  */
169 
170 /**
171  * @brief Deinitialise the console.
172  * @param console A pointer to the console data to initialize (if it's NULL, the default console will be used).
173  */
174 void consoleExit(PrintConsole* console);
175 
176 /**
177  * @brief Updates the console, submitting a new frame to the display.
178  * @param console A pointer to the console data to initialize (if it's NULL, the default console will be used).
179  * @remark This function should be called periodically. Failure to call this function will result in lack of screen updating.
180  */
182 
183 /**
184  * @brief Initializes debug console output on stderr to the specified device.
185  * @param device The debug device (or devices) to output debug print statements to.
186  */
188 
189 /// Clears the screan by using printf("\x1b[2J");
190 void consoleClear(void);
void consoleExit(PrintConsole *console)
Deinitialise the console.
PrintConsole * consoleGetDefault(void)
Gets a pointer to the console with the default values.
void consoleSetWindow(PrintConsole *console, int x, int y, int width, int height)
Sets the print window.
void consoleSetFont(PrintConsole *console, ConsoleFont *font)
Loads the font into the console.
void consoleUpdate(PrintConsole *console)
Updates the console, submitting a new frame to the display.
debugDevice
Console debug devices supported by libnx.
Definition: console.h:126
@ debugDevice_SVC
Outputs stderr debug statements using svcOutputDebugString, which can then be captured by interactive...
Definition: console.h:128
@ debugDevice_CONSOLE
Directs stderr debug statements to Switch console window.
Definition: console.h:129
@ debugDevice_NULL
Swallows prints to stderr.
Definition: console.h:127
PrintConsole * consoleSelect(PrintConsole *console)
Make the specified console the render target.
void consoleDebugInit(debugDevice device)
Initializes debug console output on stderr to the specified device.
void consoleClear(void)
Clears the screan by using printf("\x1b[2J");.
PrintConsole * consoleInit(PrintConsole *console)
Initialise the console.
A font struct for the console.
Definition: console.h:46
const void * gfx
A pointer to the font graphics.
Definition: console.h:47
u16 asciiOffset
Offset to the first valid character in the font table.
Definition: console.h:48
u16 numChars
Number of characters in the font graphics.
Definition: console.h:49
Renderer interface for the console.
Definition: console.h:36
Console structure used to store the state of a console render context.
Definition: console.h:87
int cursorX
Current X location of the cursor (as a tile offset by default)
Definition: console.h:91
int consoleWidth
Width of the console hardware layer in characters.
Definition: console.h:97
int flags
Reverse/bright flags.
Definition: console.h:108
int windowX
Window X location in characters.
Definition: console.h:100
u16 bg
Background color.
Definition: console.h:107
int tabSize
Size of a tab.
Definition: console.h:105
int cursorY
Current Y location of the cursor (as a tile offset by default)
Definition: console.h:92
int prevCursorX
Internal state.
Definition: console.h:94
int prevCursorY
Internal state.
Definition: console.h:95
int consoleHeight
Height of the console hardware layer in characters.
Definition: console.h:98
ConsoleRenderer * renderer
Renderer of the console.
Definition: console.h:89
int windowWidth
Window width in characters.
Definition: console.h:102
int windowHeight
Window height in characters.
Definition: console.h:103
bool consoleInitialised
True if the console is initialized.
Definition: console.h:110
u16 fg
Foreground color.
Definition: console.h:106
ConsoleFont font
Font of the console.
Definition: console.h:88
int windowY
Window Y location in characters.
Definition: console.h:101
uint16_t u16
16-bit unsigned integer.
Definition: types.h:20