libnx
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  * consoleDemoInit()
14  * @endcode
15  * or to customize the console usage by:
16  * @code
17  * consoleInit()
18  * @endcode
19  */
20 #pragma once
21 #include "../../types.h"
22 
23 #define CONSOLE_ESC(x) "\x1b[" #x
24 #define CONSOLE_RESET CONSOLE_ESC(0m)
25 #define CONSOLE_BLACK CONSOLE_ESC(30m)
26 #define CONSOLE_RED CONSOLE_ESC(31;1m)
27 #define CONSOLE_GREEN CONSOLE_ESC(32;1m)
28 #define CONSOLE_YELLOW CONSOLE_ESC(33;1m)
29 #define CONSOLE_BLUE CONSOLE_ESC(34;1m)
30 #define CONSOLE_MAGENTA CONSOLE_ESC(35;1m)
31 #define CONSOLE_CYAN CONSOLE_ESC(36;1m)
32 #define CONSOLE_WHITE CONSOLE_ESC(37;1m)
33 
34 /// A callback for printing a character.
35 typedef bool(*ConsolePrint)(void* con, int c);
36 
37 /// A font struct for the console.
38 typedef struct ConsoleFont
39 {
40  u16* gfx; ///< A pointer to the font graphics
41  u16 asciiOffset; ///< Offset to the first valid character in the font table
42  u16 numChars; ///< Number of characters in the font graphics
44 
45 /**
46  * @brief Console structure used to store the state of a console render context.
47  *
48  * Default values from consoleGetDefault();
49  * @code
50  * PrintConsole defaultConsole =
51  * {
52  * //Font:
53  * {
54  * (u16*)default_font_bin, //font gfx
55  * 0, //first ascii character in the set
56  * 128, //number of characters in the font set
57  * },
58  * 0,0, //cursorX cursorY
59  * 0,0, //prevcursorX prevcursorY
60  * 80, //console width
61  * 45, //console height
62  * 0, //window x
63  * 0, //window y
64  * 80, //window width
65  * 45, //window height
66  * 3, //tab size
67  * 0, //font character offset
68  * 0, //print callback
69  * false //console initialized
70  * };
71  * @endcode
72  */
73 typedef struct PrintConsole
74 {
75  ConsoleFont font; ///< Font of the console
76 
77  u32 *frameBuffer; ///< Framebuffer address
78  u32 *frameBuffer2; ///< Framebuffer address
79 
80  int cursorX; ///< Current X location of the cursor (as a tile offset by default)
81  int cursorY; ///< Current Y location of the cursor (as a tile offset by default)
82 
83  int prevCursorX; ///< Internal state
84  int prevCursorY; ///< Internal state
85 
86  int consoleWidth; ///< Width of the console hardware layer in characters
87  int consoleHeight; ///< Height of the console hardware layer in characters
88 
89  int windowX; ///< Window X location in characters (not implemented)
90  int windowY; ///< Window Y location in characters (not implemented)
91  int windowWidth; ///< Window width in characters (not implemented)
92  int windowHeight; ///< Window height in characters (not implemented)
93 
94  int tabSize; ///< Size of a tab
95  int fg; ///< Foreground color
96  int bg; ///< Background color
97  int flags; ///< Reverse/bright flags
98 
99  ConsolePrint PrintChar; ///< Callback for printing a character. Should return true if it has handled rendering the graphics (else the print engine will attempt to render via tiles).
100 
101  bool consoleInitialised; ///< True if the console is initialized
102 }PrintConsole;
103 
104 #define CONSOLE_COLOR_BOLD (1<<0) ///< Bold text
105 #define CONSOLE_COLOR_FAINT (1<<1) ///< Faint text
106 #define CONSOLE_ITALIC (1<<2) ///< Italic text
107 #define CONSOLE_UNDERLINE (1<<3) ///< Underlined text
108 #define CONSOLE_BLINK_SLOW (1<<4) ///< Slow blinking text
109 #define CONSOLE_BLINK_FAST (1<<5) ///< Fast blinking text
110 #define CONSOLE_COLOR_REVERSE (1<<6) ///< Reversed color text
111 #define CONSOLE_CONCEAL (1<<7) ///< Concealed text
112 #define CONSOLE_CROSSED_OUT (1<<8) ///< Crossed out text
113 
114 /// Console debug devices supported by libnx.
115 typedef enum {
116  debugDevice_NULL, ///< Swallows prints to stderr
117  debugDevice_SVC, ///< Outputs stderr debug statements using svcOutputDebugString, which can then be captured by interactive debuggers
118  debugDevice_CONSOLE, ///< Directs stderr debug statements to Switch console window
119  debugDevice_3DMOO = debugDevice_SVC,
120 } debugDevice;
121 
122 /**
123  * @brief Loads the font into the console.
124  * @param console Pointer to the console to update, if NULL it will update the current console.
125  * @param font The font to load.
126  */
127 void consoleSetFont(PrintConsole* console, ConsoleFont* font);
128 
129 /**
130  * @brief Sets the print window.
131  * @param console Console to set, if NULL it will set the current console window.
132  * @param x X location of the window.
133  * @param y Y location of the window.
134  * @param width Width of the window.
135  * @param height Height of the window.
136  */
137 void consoleSetWindow(PrintConsole* console, int x, int y, int width, int height);
138 
139 /**
140  * @brief Gets a pointer to the console with the default values.
141  * This should only be used when using a single console or without changing the console that is returned, otherwise use consoleInit().
142  * @return A pointer to the console with the default values.
143  */
145 
146 /**
147  * @brief Make the specified console the render target.
148  * @param console A pointer to the console struct (must have been initialized with consoleInit(PrintConsole* console)).
149  * @return A pointer to the previous console.
150  */
152 
153 /**
154  * @brief Initialise the console.
155  * @param console A pointer to the console data to initialize (if it's NULL, the default console will be used).
156  * @return A pointer to the current console.
157  */
159 
160 /**
161  * @brief Initializes debug console output on stderr to the specified device.
162  * @param device The debug device (or devices) to output debug print statements to.
163  */
164 void consoleDebugInit(debugDevice device);
165 
166 /// Clears the screan by using iprintf("\x1b[2J");
167 void consoleClear(void);
A font struct for the console.
Definition: console.h:38
PrintConsole * consoleSelect(PrintConsole *console)
Make the specified console the render target.
bool consoleInitialised
True if the console is initialized.
Definition: console.h:101
ConsoleFont font
Font of the console.
Definition: console.h:75
bool(* ConsolePrint)(void *con, int c)
A callback for printing a character.
Definition: console.h:35
uint16_t u16
16-bit unsigned integer.
Definition: types.h:22
int consoleHeight
Height of the console hardware layer in characters.
Definition: console.h:87
int cursorX
Current X location of the cursor (as a tile offset by default)
Definition: console.h:80
int windowWidth
Window width in characters (not implemented)
Definition: console.h:91
u32 * frameBuffer
Framebuffer address.
Definition: console.h:77
int cursorY
Current Y location of the cursor (as a tile offset by default)
Definition: console.h:81
int windowHeight
Window height in characters (not implemented)
Definition: console.h:92
int flags
Reverse/bright flags.
Definition: console.h:97
int windowY
Window Y location in characters (not implemented)
Definition: console.h:90
u16 asciiOffset
Offset to the first valid character in the font table.
Definition: console.h:41
Directs stderr debug statements to Switch console window.
Definition: console.h:118
uint32_t u32
32-bit unsigned integer.
Definition: types.h:23
Outputs stderr debug statements using svcOutputDebugString, which can then be captured by interactive...
Definition: console.h:117
debugDevice
Console debug devices supported by libnx.
Definition: console.h:115
void consoleDebugInit(debugDevice device)
Initializes debug console output on stderr to the specified device.
ConsolePrint PrintChar
Callback for printing a character. Should return true if it has handled rendering the graphics (else ...
Definition: console.h:99
int prevCursorY
Internal state.
Definition: console.h:84
void consoleSetWindow(PrintConsole *console, int x, int y, int width, int height)
Sets the print window.
int windowX
Window X location in characters (not implemented)
Definition: console.h:89
void consoleSetFont(PrintConsole *console, ConsoleFont *font)
Loads the font into the console.
Swallows prints to stderr.
Definition: console.h:116
int fg
Foreground color.
Definition: console.h:95
int prevCursorX
Internal state.
Definition: console.h:83
u16 * gfx
A pointer to the font graphics.
Definition: console.h:40
int consoleWidth
Width of the console hardware layer in characters.
Definition: console.h:86
int tabSize
Size of a tab.
Definition: console.h:94
u16 numChars
Number of characters in the font graphics.
Definition: console.h:42
void consoleClear(void)
Clears the screan by using iprintf("\x1b[2J");.
PrintConsole * consoleGetDefault(void)
Gets a pointer to the console with the default values.
Console structure used to store the state of a console render context.
Definition: console.h:73
PrintConsole * consoleInit(PrintConsole *console)
Initialise the console.
u32 * frameBuffer2
Framebuffer address.
Definition: console.h:78
int bg
Background color.
Definition: console.h:96