libnx  v4.2.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  int fg; ///< Foreground color
107  int 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 
123 /// Console debug devices supported by libnx.
124 typedef enum {
125  debugDevice_NULL, ///< Swallows prints to stderr
126  debugDevice_SVC, ///< Outputs stderr debug statements using svcOutputDebugString, which can then be captured by interactive debuggers
127  debugDevice_CONSOLE, ///< Directs stderr debug statements to Switch console window
128 } debugDevice;
129 
130 /**
131  * @brief Loads the font into the console.
132  * @param console Pointer to the console to update, if NULL it will update the current console.
133  * @param font The font to load.
134  */
135 void consoleSetFont(PrintConsole* console, ConsoleFont* font);
136 
137 /**
138  * @brief Sets the print window.
139  * @param console Console to set, if NULL it will set the current console window.
140  * @param x X location of the window.
141  * @param y Y location of the window.
142  * @param width Width of the window.
143  * @param height Height of the window.
144  */
145 void consoleSetWindow(PrintConsole* console, int x, int y, int width, int height);
146 
147 /**
148  * @brief Gets a pointer to the console with the default values.
149  * This should only be used when using a single console or without changing the console that is returned, otherwise use consoleInit().
150  * @return A pointer to the console with the default values.
151  */
153 
154 /**
155  * @brief Make the specified console the render target.
156  * @param console A pointer to the console struct (must have been initialized with consoleInit(PrintConsole* console)).
157  * @return A pointer to the previous console.
158  */
160 
161 /**
162  * @brief Initialise the console.
163  * @param console A pointer to the console data to initialize (if it's NULL, the default console will be used).
164  * @return A pointer to the current console.
165  */
167 
168 /**
169  * @brief Deinitialise the console.
170  * @param console A pointer to the console data to initialize (if it's NULL, the default console will be used).
171  */
172 void consoleExit(PrintConsole* console);
173 
174 /**
175  * @brief Updates the console, submitting a new frame to the display.
176  * @param console A pointer to the console data to initialize (if it's NULL, the default console will be used).
177  * @remark This function should be called periodically. Failure to call this function will result in lack of screen updating.
178  */
179 void consoleUpdate(PrintConsole* console);
180 
181 /**
182  * @brief Initializes debug console output on stderr to the specified device.
183  * @param device The debug device (or devices) to output debug print statements to.
184  */
185 void consoleDebugInit(debugDevice device);
186 
187 /// Clears the screan by using printf("\x1b[2J");
188 void consoleClear(void);
PrintConsole::renderer
ConsoleRenderer * renderer
Renderer of the console.
Definition: console.h:89
PrintConsole::consoleWidth
int consoleWidth
Width of the console hardware layer in characters.
Definition: console.h:97
consoleSelect
PrintConsole * consoleSelect(PrintConsole *console)
Make the specified console the render target.
consoleUpdate
void consoleUpdate(PrintConsole *console)
Updates the console, submitting a new frame to the display.
PrintConsole::flags
int flags
Reverse/bright flags.
Definition: console.h:108
PrintConsole::windowY
int windowY
Window Y location in characters.
Definition: console.h:101
PrintConsole::tabSize
int tabSize
Size of a tab.
Definition: console.h:105
ConsoleFont::asciiOffset
u16 asciiOffset
Offset to the first valid character in the font table.
Definition: console.h:48
PrintConsole::prevCursorY
int prevCursorY
Internal state.
Definition: console.h:95
debugDevice_CONSOLE
@ debugDevice_CONSOLE
Directs stderr debug statements to Switch console window.
Definition: console.h:127
PrintConsole::font
ConsoleFont font
Font of the console.
Definition: console.h:88
PrintConsole::prevCursorX
int prevCursorX
Internal state.
Definition: console.h:94
ConsoleFont::gfx
const void * gfx
A pointer to the font graphics.
Definition: console.h:47
debugDevice_SVC
@ debugDevice_SVC
Outputs stderr debug statements using svcOutputDebugString, which can then be captured by interactive...
Definition: console.h:126
PrintConsole::windowHeight
int windowHeight
Window height in characters.
Definition: console.h:103
consoleDebugInit
void consoleDebugInit(debugDevice device)
Initializes debug console output on stderr to the specified device.
PrintConsole::fg
int fg
Foreground color.
Definition: console.h:106
ConsoleFont
A font struct for the console.
Definition: console.h:45
consoleExit
void consoleExit(PrintConsole *console)
Deinitialise the console.
debugDevice
debugDevice
Console debug devices supported by libnx.
Definition: console.h:124
ConsoleFont::numChars
u16 numChars
Number of characters in the font graphics.
Definition: console.h:49
PrintConsole::consoleInitialised
bool consoleInitialised
True if the console is initialized.
Definition: console.h:110
debugDevice_NULL
@ debugDevice_NULL
Swallows prints to stderr.
Definition: console.h:125
consoleSetFont
void consoleSetFont(PrintConsole *console, ConsoleFont *font)
Loads the font into the console.
PrintConsole::bg
int bg
Background color.
Definition: console.h:107
PrintConsole::windowWidth
int windowWidth
Window width in characters.
Definition: console.h:102
consoleSetWindow
void consoleSetWindow(PrintConsole *console, int x, int y, int width, int height)
Sets the print window.
PrintConsole::cursorX
int cursorX
Current X location of the cursor (as a tile offset by default)
Definition: console.h:91
PrintConsole::cursorY
int cursorY
Current Y location of the cursor (as a tile offset by default)
Definition: console.h:92
consoleInit
PrintConsole * consoleInit(PrintConsole *console)
Initialise the console.
consoleClear
void consoleClear(void)
Clears the screan by using printf("\x1b[2J");.
consoleGetDefault
PrintConsole * consoleGetDefault(void)
Gets a pointer to the console with the default values.
PrintConsole
Console structure used to store the state of a console render context.
Definition: console.h:86
PrintConsole::consoleHeight
int consoleHeight
Height of the console hardware layer in characters.
Definition: console.h:98
ConsoleRenderer
Renderer interface for the console.
Definition: console.h:35
u16
uint16_t u16
16-bit unsigned integer.
Definition: types.h:20
PrintConsole::windowX
int windowX
Window X location in characters.
Definition: console.h:100