# I/O operations

> To use the functions described in this page, the file `<hal.h>` should be included.

## Output to the screen

JSLK currently only supports outputting to the screen in text mode, through a 24x80 map. A line is not considered because of the menu bar. The console is started by the kernel entrypoint, in x86 this is done through the `startConsole()` function defined in `vga.c`.

{% tabs %}
{% tab title="kprintf()" %}

#### `int kprintf(string c, ...)`

Used to print a formatted string to the console.

**Arguments**

`string c`: The string to be printed. Use special characters in string to interpolate values.

Optional arguments: The values to be interpolated (in the order specified in the string).

**Special characters**

&#x20;`%i`: Decimal number.

`%s`: String.

`%h`: Hexadecimal number.

#### Return value

Returns 0 if successful.
{% endtab %}

{% tab title="cprintf()" %}

#### `int cprintf(string c, uint8_t color, ...)` <a href="#int-kprintf-string-c" id="int-kprintf-string-c"></a>

Used to print a formatted rich-text string to the console.

**Arguments**

`string c`: The string to be printed. Use special characters in string to interpolate values.

`uint8_t color`: The color of the text to be printed. A color value can be obtained through the `getColor()` function.

Optional arguments: The values to be interpolated (in the order specified in the string).

**Special characters**

&#x20;`%i`: Decimal number.

`%s`: String.

`%h`: Hexadecimal number.

#### Return value <a href="#return-value" id="return-value"></a>

Returns 0 if successful.
{% endtab %}

{% tab title="kputc()" %}

#### `void kputc(char c)`

Used to print a character to the console.

**Arguments**

`char c`: The character to be printed.&#x20;

#### Return value <a href="#return-value" id="return-value"></a>

This function has no return value.
{% endtab %}

{% tab title="consolePutChar()" %}

#### `void consolePutChar(char c, uint8_t color, size_t x, size_t y);`

Used to print a character to the console on the specified position.

**Arguments**

`char c`: The character to be printed.

`uint8_t color`: The color of the text to be printed. A color value can be obtained through the `getColor()` function.

`size_t x`: The x position of the character coordinate.

`size_t y`: The y position of the character coordinate.

#### Return value <a href="#return-value-2" id="return-value-2"></a>

This function has no return value.
{% endtab %}

{% tab title="clear\_console()" %}

#### `void clear_console()`

Used to clear the console.

**Arguments**

This function takes no arguments.

#### Return value <a href="#return-value-2" id="return-value-2"></a>

This function has no return value.
{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="setConsoleColor()" %}

#### `void setConsoleColor(uint8_t color)`

Used to change the console color. `clear_console()` must be called to apply changes to the whole console.

**Arguments**

`uint8_t color`: The color of the text to be printed. A color value can be obtained through the `getColor()` function.

#### Return value

This function has no return value.
{% endtab %}

{% tab title="getColor()" %}

#### `uint8_t getColor(vga_color text, vga_color background)`

Used to get a console compatible color.

**Special Types**

`vga_color`: One of the sixteen possible VGA colors.

{% code title="vga.h" %}

```c
// ...
enum vga_color_t
{
    vga_black = 0,
    vga_blue = 1,
    vga_green = 2,
    vga_cyan = 3,
    vga_red = 4,
    vga_magenta = 5,
    vga_brown = 6,
    vga_light_grey = 7,
    vga_grey = 8,
    vga_light_blue = 9,
    vga_light_green = 10,
    vga_light_cyan = 11,
    vga_light_red = 12,
    vga_light_magenta = 13,
    vga_light_brown = 14,
    vga_white = 15,
};
// ...

```

{% endcode %}

**Arguments**

`vga_color text`: Text color in the console (foreground).

`vga_color background`: Console's background color.

Optional arguments: The values to be interpolated (in the order specified in the string).

**Return value**

Returns a console compatible color value in a `uint8_t`.

#### Sample usage

{% code title="sample.c" %}

```c
uint8_t color = getColor(vga_grey, vga_black); // Get a color value to use later
setConsoleColor(color);  // Example of how you could use the obtained value
```

{% endcode %}
{% endtab %}

{% tab title="setMenubarText()" %}

#### `void setMenubarText(string text)`

Used to change the text in the menu bar.

**Arguments**

`string text`: The string to be displayed in the menu bar.

**Return value**

This function has no return value.
{% endtab %}

{% tab title="debugPrint()" %}

#### `void debugPrint(string msg)`

Used to print a string to the console only if the `KERNEL_DEBUG` flag was set during build.

**Arguments**

`string msg`: The string to be printed.

**Return value**

This function has no return value.
{% endtab %}
{% endtabs %}

## Input from the keyboard

Although there is a keyboard driver provided, there is currently no way of interfacing with it without the need to write auxiliary code. Currently, all input from the keyboard is printed to the screen, and there is one function that will indicate if the enter key was pressed ([`read_key()`](https://github.com/sofferjacob/jslk/blob/master/hal/x86/drivers/keyboard.c#L181)), although it was just written for testing purposes and will be removed in the future. If you are in need to use the keyboard, feel free to write your own interface until one is added in a future release.

## File I/O

[The VFS](https://jslk.gitbook.io/docs/the-vfs)

## I/O with other hardware

The following functions can be used to interface with ports.

{% tabs %}
{% tab title="outb()" %}

#### `void outb(uint16_t port, uint8_t value)` <a href="#void-setconsolecolor-uint-8-_t-color" id="void-setconsolecolor-uint-8-_t-color"></a>

Used to send a byte to a specific port.

**Arguments**

`uint16_t port`: The port to send the byte to.

`uint8_t value`: The value to be sent.

#### Return value <a href="#return-value-3" id="return-value-3"></a>

This function has no return value.
{% endtab %}

{% tab title="inb()" %}

#### `uint8_t inb(uint16_t port)` <a href="#void-setconsolecolor-uint-8-_t-color-1" id="void-setconsolecolor-uint-8-_t-color-1"></a>

Used to read a byte from a specific port.

**Arguments**

`uint16_t port`: The port to read from.

#### Return value <a href="#return-value-3-1" id="return-value-3-1"></a>

The byte read is returned as a `uint8_t`.
{% endtab %}

{% tab title="inw()" %}

#### `uint16_t inw(uint16_t port)` <a href="#void-setconsolecolor-uint-8-_t-color-1" id="void-setconsolecolor-uint-8-_t-color-1"></a>

Used to read a word from a specific port.

**Arguments**

`uint16_t port`: The port to read from.

#### Return value <a href="#return-value-3-1" id="return-value-3-1"></a>

The word read is returned as a `uint16_t`.
{% endtab %}
{% endtabs %}
