aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpk33 <pk33@email.com>2024-11-03 12:07:55 +0100
committerpk33 <pk33@email.com>2024-11-03 12:07:55 +0100
commit844a19f9bfe01286ca9974aff0e6d38693e05877 (patch)
treebeca8efb8300fe3b6c78e9da26560cf674b57595 /src
parent23ab6c4e6483159b77af8b4edf95113887a300d7 (diff)
downloadengine33-844a19f9bfe01286ca9974aff0e6d38693e05877.tar.gz
cleanup code & term handlers
Diffstat (limited to 'src')
-rw-r--r--src/display.c78
-rw-r--r--src/logger.c14
-rw-r--r--src/main.c16
-rw-r--r--src/system.c18
4 files changed, 75 insertions, 51 deletions
diff --git a/src/display.c b/src/display.c
index 25c6437..806b78e 100644
--- a/src/display.c
+++ b/src/display.c
@@ -55,6 +55,7 @@ static DRMData_d drmData = {0,};
static int vtPipe[2] = { -1, -1 };
static struct termios termOldConfig;
+static struct vt_mode vtModeOld = {0,};
static err _open_graphics_device( void );
static err _get_drm_resources( void );
@@ -66,8 +67,6 @@ static Size _init_vt_switch( void );
static void _vt_release( void );
static void _vt_acquire( void );
static void _vt_switch_sighandler(int sig);
-static Size _has_signal(Size signo);
-static Size _set_signal(Size signo, void(*sig_handler)(int));
err display_init( void )
@@ -139,6 +138,9 @@ err display_init( void )
__display.surface.data = __display.fb.map[0];
__display.active = 1;
+ free( drmData.drmRes_fbs );
+ free( drmData.drmRes_encs );
+ free( drmData.drmRes_conns );
return E33_EXIT_SUCCESS;
}
@@ -173,34 +175,32 @@ void display_flip( void )
void display_term( void )
{
if( e33_ioctl( __display.devfd, DRM_IOCTL_DROP_MASTER, 0 ) == -1 ) {
- logw( "Failed to drop drm master. Oh well." );
- }
-
- if( ioctl(__display.ttyfd, VT_RELDISP, 1) == -1 ) {
- logw( "Failed to drop release vt. Oh well." );
+ logw( "Failed to drop drm master. Oh well" );
}
if( munmap( __display.fb.map[0], (uSize)__display.fb.size ) == -1 ) {
- logw( "Failed to unmap framebuffer[0]. Oh well." );
+ logw( "Failed to unmap framebuffer[0]. Oh well" );
}
if( munmap( __display.fb.map[1], (uSize)__display.fb.size ) == -1 ) {
- logw( "Failed to unmap framebuffer[1]. Oh well." );
+ logw( "Failed to unmap framebuffer[1]. Oh well" );
+ }
+
+ if( close( __display.devfd ) ) {
+ logw( "Failed to close graphics device. Oh well" );
}
- free( drmData.drmRes_fbs );
free( drmData.drmRes_crtcs );
- free( drmData.drmRes_encs );
- free( drmData.drmRes_conns );
- if( close( __display.ttyfd ) == -1 ) {
- logw( "Failed to close TTY. Oh well." );
- }
- if( close( __display.devfd ) ) {
- logw( "Failed to close graphics device. Oh well." );
+ if( ioctl(__display.ttyfd, VT_SETMODE, &vtModeOld) < 0 ) {
+ logw( "Could not reset VT mode. Oh well" );
}
if( tcsetattr(STDIN_FILENO, TCSAFLUSH, &termOldConfig) == -1 ) {
- logw( "Failed to set stdin attributes. Oh well." );
+ logw( "Failed to set stdin attributes. Oh well" );
+ }
+
+ if( close( __display.ttyfd ) == -1 ) {
+ logw( "Failed to close TTY. Oh well" );
}
}
@@ -329,7 +329,7 @@ static Size _get_drm_connector( void )
{
struct drm_mode_modeinfo *mode = &drmData.connModes[j];
- if( strcomp( mode->name, DRM_MODE ) && (mode->vrefresh == DRM_RATE) )
+ if( e33_strcmp( mode->name, DRM_MODE ) && (mode->vrefresh == DRM_RATE) )
{
/* TODO there has to be a better way */
if( mode->hdisplay % 8 ) {
@@ -506,21 +506,6 @@ static int _set_pipe(int fd)
return E33_EXIT_SUCCESS;
}
-static Size _has_signal(Size signo)
-{
- struct sigaction sact = {0};
- sigaction(signo, 0, &sact);
- return sact.sa_handler != 0;
-}
-
-static Size _set_signal(Size signo, void(*sig_handler)(int))
-{
- struct sigaction sact = {0};
- sact.sa_handler = sig_handler;
- sigemptyset(&sact.sa_mask);
- sact.sa_flags = SA_RESTART;
- return sigaction(signo, &sact, NULL);
-}
static void _vt_switch_sighandler(int sig)
{
@@ -535,13 +520,13 @@ static void _vt_switch_sighandler(int sig)
static Size _init_vt_switch( void )
{
- struct vt_mode vt_mode = {0,};
+ struct vt_mode vtMode = {0,};
sigset_t set;
__display.ttyfd = -1;
- if( pipe(vtPipe) == -1 )
+ if( pipe2(vtPipe, 0) == -1 )
{
vtPipe[0] = vtPipe[1] = -1;
loge( "Could not set VT pipes");
@@ -564,36 +549,37 @@ static Size _init_vt_switch( void )
}
- if( _has_signal(REL_SIGNAL) ) {
+ if( e33_has_signal(REL_SIGNAL) ) {
loge( "VT release signal is already in use");
return E33_EXIT_FAILURE;
}
- if( _has_signal(ACQ_SIGNAL) ) {
+ if( e33_has_signal(ACQ_SIGNAL) ) {
loge( "VT acquire signal is already in use");
return E33_EXIT_FAILURE;
}
- if( _set_signal( REL_SIGNAL, _vt_switch_sighandler ) ) {
+ if( e33_set_signal( REL_SIGNAL, _vt_switch_sighandler ) ) {
loge( "Could not set relese signal handler");
return E33_EXIT_FAILURE;
}
- if( _set_signal( ACQ_SIGNAL, _vt_switch_sighandler ) ) {
+ if( e33_set_signal( ACQ_SIGNAL, _vt_switch_sighandler ) ) {
loge( "Could not set acquire signal handler");
return E33_EXIT_FAILURE;
}
- if( ioctl(__display.ttyfd, VT_GETMODE, &vt_mode) < 0 ) {
+ if( ioctl(__display.ttyfd, VT_GETMODE, &vtModeOld) < 0 ) {
logw( "Could not get VT mode" );
return E33_EXIT_FAILURE;
}
- vt_mode.mode = VT_PROCESS;
- vt_mode.relsig = REL_SIGNAL;
- vt_mode.acqsig = ACQ_SIGNAL;
- vt_mode.frsig = SIGIO;
+ vtMode = vtModeOld;
+ vtMode.mode = VT_PROCESS;
+ vtMode.relsig = REL_SIGNAL;
+ vtMode.acqsig = ACQ_SIGNAL;
+ vtMode.frsig = SIGIO;
- if( ioctl(__display.ttyfd, VT_SETMODE, &vt_mode) < 0 ) {
+ if( ioctl(__display.ttyfd, VT_SETMODE, &vtMode) < 0 ) {
logw( "Could not set VT mode" );
return E33_EXIT_FAILURE;
}
diff --git a/src/logger.c b/src/logger.c
index c944da8..860311b 100644
--- a/src/logger.c
+++ b/src/logger.c
@@ -9,6 +9,7 @@
#include "string.h"
#include "logger.h"
+#include "options.h"
Logger __logger;
@@ -25,10 +26,15 @@ Size logger_init( void )
{
String logfile[512];
+ if( LOG_PATH[0] == '~' ) {
+ snprintf(logfile, 512, "%s/%s", getenv("HOME"), LOG_PATH+1);
+ } else {
+ snprintf(logfile, 512, "%s", LOG_PATH);
+ }
- snprintf(logfile, 512, "%s/%s", getenv("HOME"), ".engine33.log");
-
- __logger.fd = open(logfile, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH );
+ __logger.fd = open(logfile,
+ O_CREAT | O_TRUNC | O_WRONLY | O_NOCTTY | O_TTY_INIT,
+ 0644 );
if( __logger.fd < 0 ) {
printf( "Unable to initialize logger. Exiting." );
@@ -44,7 +50,7 @@ Size logger_init( void )
void logger_term( void )
{
- dprintf( __logger.fd, "\r\n\r\nGoodbye!" );
+ logi( "Goodbye!" );
close( __logger.fd );
}
diff --git a/src/main.c b/src/main.c
index 150f16c..1b9b2d3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2,6 +2,9 @@
#include "clock.h"
#include "logger.h"
#include "system.h"
+#include "signal.h"
+
+static void _term_sighandler( int sig );
int main(int argc, char *argv[])
@@ -9,6 +12,10 @@ int main(int argc, char *argv[])
Size fps, FPS;
long timeOld, time;
+ if( e33_set_signal( SIGHUP, _term_sighandler ) == -1 ) { logw( "SIGHUP not handled"); }
+ if( e33_set_signal( SIGINT, _term_sighandler ) == -1 ) { logw( "SIGINT not handled"); }
+ if( e33_set_signal( SIGTERM, _term_sighandler ) == -1 ) { logw( "SIGTERM not handled"); }
+
timeOld = clock_get_us();
time = 0;
fps = 0;
@@ -52,3 +59,12 @@ int main(int argc, char *argv[])
return EXIT_SUCCESS;
}
+
+
+static void _term_sighandler( int sig )
+{
+ display_term();
+ logger_term();
+ exit(0);
+}
+
diff --git a/src/system.c b/src/system.c
index cff4f54..9a9a80e 100644
--- a/src/system.c
+++ b/src/system.c
@@ -5,6 +5,7 @@
#include "stdio.h"
#include "unistd.h"
#include "string.h"
+#include "signal.h"
#include "types.h"
@@ -20,7 +21,7 @@ err e33_ioctl( Size fd, unsigned long req, void *arg )
return ret;
}
-boolean strcomp( String s1, String s2 )
+boolean e33_strcmp( String s1, String s2 )
{
while( (*s1 != '\0') && (*s2 != '\0') )
{
@@ -32,3 +33,18 @@ boolean strcomp( String s1, String s2 )
return E33_TRUE;
}
+err e33_has_signal(Size signo)
+{
+ struct sigaction sact = {0};
+ sigaction(signo, 0, &sact);
+ return sact.sa_handler != 0;
+}
+
+err e33_set_signal(Size signo, void(*sig_handler)(int))
+{
+ struct sigaction sact = {0};
+ sact.sa_handler = sig_handler;
+ sigemptyset(&sact.sa_mask);
+ sact.sa_flags = SA_RESTART;
+ return sigaction(signo, &sact, NULL);
+}