DungeonCrawl
Loading...
Searching...
No Matches
io_handler.c File Reference

Implementation of the handler for handling Input/Output. More...

#include "io_handler.h"
#include "../common.h"
#include "../logging/logger.h"
#include "../thread/thread_handler.h"
#include "input/input_handler.h"
#include "output/common/output_handler.h"
#include "output/media/media_output.h"
#include "output/media/media_output_handler.h"
#include "output/specific/wait_output.h"
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

Go to the source code of this file.

Functions

int init_io_handler (void)
 Initialize the IO handler.
bool run_background_task (void(*callback)(void))
 Execute a function in a background thread.
void shutdown_io_handler (void)
 Shutdown the IO handler.

Variables

struct notcurses * nc = NULL
struct ncplane * stdplane = NULL

Detailed Description

Implementation of the handler for handling Input/Output.

Definition in file io_handler.c.

Function Documentation

◆ init_io_handler()

int init_io_handler ( void )

Initialize the IO handler.

Sets up all input and output subsystems.

Returns
COMMON_SUCCESS on success, non-zero value on failure

Definition at line 29 of file io_handler.c.

29 {
30 //initialize the Notcurses instance and standard plane
31 notcurses_options ncopt;
32 memset(&ncopt, 0, sizeof(ncopt));
33
34 nc = notcurses_init(&ncopt, stdout);
35 if (nc == NULL) {
36 log_msg(ERROR, "io_handler", "Failed to initialize notcurses");
37 return 1;// Error code
38 }
39
40 stdplane = notcurses_stdplane(nc);
41 if (!stdplane) {
42 log_msg(ERROR, "io_handler", "Failed to get standard plane");
43 notcurses_stop(nc);
44 nc = NULL;
45 return 2;// Error code
46 }
47
48 ncplane_set_bg_rgb(stdplane, 0x281D10);
49
50 // Initialize input handler (which starts its own thread)
51 if (!init_input_handler(nc)) {
52 log_msg(ERROR, "io_handler", "Failed to initialize input handler");
53 return 3;// Error code
54 }
55
56 // Initialize common output handler
57 if (!init_output_handler()) {
58 log_msg(ERROR, "io_handler", "Failed to initialize output handler");
60 return 4;// Error code
61 }
62
63 // Initialize media output handler
64 if (!init_media_output()) {
65 log_msg(ERROR, "io_handler", "Failed to initialize media output handler");
66 // Continue without media support, not a fatal error
67 }
68
69 return COMMON_SUCCESS;// 0
70}
#define COMMON_SUCCESS
Common success return value.
Definition common.h:146
bool init_input_handler(struct notcurses *notcurses_ptr)
Initialize the input handler.
void shutdown_input_handler(void)
Shutdown the input handler.
void log_msg(const log_level_t level, const char *module, const char *format,...)
Logs a formatted message with a specified log level and module.
Definition logger.c:246
bool init_media_output(void)
Initialize the media output handler.
bool init_output_handler()
Initialize the output handler.

◆ run_background_task()

bool run_background_task ( void(* callback )(void))

Execute a function in a background thread.

This function executes the provided callback in a background thread.

Parameters
callbackFunction to execute in the background
Returns
true if the thread started successfully, false otherwise

Definition at line 73 of file io_handler.c.

73 {
74 if (!callback) {
75 log_msg(ERROR, "io_handler", "Invalid callback for background task");
76 return false;
77 }
78
79 // Start a thread to execute the callback
80 start_simple_thread(callback);
81 return true;
82}
void start_simple_thread(void(*thread_func)(void))
Starts a new thread with the given function.

◆ shutdown_io_handler()

void shutdown_io_handler ( void )

Shutdown the IO handler.

Cleans up all input and output subsystems.

Definition at line 84 of file io_handler.c.

84 {
85 // Shutdown subsystems in reverse order of initialization
88
89 // Shutdown notcurses
90 if (nc) {
91 notcurses_stop(nc);
92 nc = NULL;
93 stdplane = NULL;
94 }
95}
void shutdown_output_handler(void)
Shutdown the output handler.

Variable Documentation

◆ nc

struct notcurses* nc = NULL

Definition at line 26 of file io_handler.c.

◆ stdplane

struct ncplane* stdplane = NULL

Definition at line 27 of file io_handler.c.