diff options
author | pk33 <pk33@pk33.space> | 2024-11-11 18:06:18 +0100 |
---|---|---|
committer | pk33 <pk33@pk33.space> | 2024-11-11 18:06:18 +0100 |
commit | 5a54774474f43ae29716751d6415563a59b92c7d (patch) | |
tree | 151e865845b09b85992d8be20d8cc771d4110448 /src | |
parent | 146a683b8d8315ef15dc1c9286f0983834f48d88 (diff) | |
download | engine33-5a54774474f43ae29716751d6415563a59b92c7d.tar.gz |
tidying up and basic input
Diffstat (limited to 'src')
-rw-r--r-- | src/clock33.c (renamed from src/clock.c) | 2 | ||||
-rw-r--r-- | src/display33.c (renamed from src/display.c) | 227 | ||||
-rw-r--r-- | src/input33.c | 232 | ||||
-rw-r--r-- | src/logger.c | 103 | ||||
-rw-r--r-- | src/logger33.c | 111 | ||||
-rw-r--r-- | src/main.c | 63 | ||||
-rw-r--r-- | src/strings33.c | 51 | ||||
-rw-r--r-- | src/system33.c (renamed from src/system.c) | 12 | ||||
-rw-r--r-- | src/ui33.c | 54 |
9 files changed, 558 insertions, 297 deletions
diff --git a/src/clock.c b/src/clock33.c index 7cb5ec3..2fe0d3c 100644 --- a/src/clock.c +++ b/src/clock33.c @@ -1,6 +1,6 @@ #include "sys/time.h" -#include "clock.h" +#include "clock33.h" Size clock_get_us( void ) diff --git a/src/display.c b/src/display33.c index bad4d69..928d4e0 100644 --- a/src/display.c +++ b/src/display33.c @@ -9,18 +9,16 @@ #include "dirent.h" #include "termio.h" #include "stdio.h" - #include "libdrm/drm_mode.h" #include "sys/vt.h" #include "sys/kd.h" - #include "sys/ioctl.h" #include "sys/mman.h" -#include "display.h" -#include "system.h" -#include "logger.h" -#include "options.h" +#include "display33.h" +#include "system33.h" +#include "logger33.h" +#include "options33.h" #define REL_SIGNAL SIGUSR1 @@ -29,7 +27,7 @@ #define ACQ_EVENT 2 -Display __display = {0}; +Display33 __display = {0,}; typedef struct { @@ -48,20 +46,20 @@ typedef struct { struct drm_mode_create_dumb drmCreateDumb[2]; struct drm_mode_map_dumb drmMapDumb[2]; struct drm_mode_fb_cmd drmFBCmd[2]; -} DRMData_d; +} DRMData_t; -static DRMData_d drmData = {0,}; +static DRMData_t 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 ); -static err _get_drm_connector( void ); -static err _get_drm_crtc( void ); -static err _get_drm_framebuffer(void ); +static Error _open_graphics_device( void ); +static Error _get_drm_resources( void ); +static Error _get_drm_connector( void ); +static Error _get_drm_crtc( void ); +static Error _get_drm_framebuffer(void ); static Size _init_vt_switch( void ); static void _vt_release( void ); @@ -69,18 +67,18 @@ static void _vt_acquire( void ); static void _vt_switch_sighandler(int sig); -err display_init( void ) +Error display33_init( void ) { struct termios termConfig; if(!isatty(STDIN_FILENO)) { - logf( "stdin is not a terminal" ); + LOGF( "stdin is not a terminal." ); return E33_EXIT_FAILURE; } if( tcgetattr(STDIN_FILENO, &termOldConfig) == -1 ) { - logf( "Could not get terminal attributes" ); + LOGF( "Could not get terminal attributes." ); return E33_EXIT_FAILURE; } @@ -94,40 +92,40 @@ err display_init( void ) termConfig.c_cc[VSTOP] = 0; if( tcsetattr(STDIN_FILENO, TCSANOW, &termConfig) == -1 ) { - logf( "Could not set terminal attributes" ); + LOGF( "Could not set terminal attributes." ); return E33_EXIT_FAILURE; } if( _init_vt_switch() ) { - logw( "Failed to initialize VT switcher. VT switching will not be available" ); + LOGW( "Failed to initialize VT switcher. VT switching will not be available." ); } if( _open_graphics_device() ) { - logf( "Failed to find a suitable graphics device" ); + LOGF( "Failed to find a suitable graphics device." ); return E33_EXIT_FAILURE; } - if( e33_ioctl( __display.devfd, DRM_IOCTL_SET_MASTER, 0 ) == -1 ) + if( e33_ioctl( __display.devFd, DRM_IOCTL_SET_MASTER, 0 ) == -1 ) { - logf( "Failed to set DRM master" ); + LOGF( "Failed to set DRM master." ); return E33_EXIT_FAILURE; } if( _get_drm_resources() ) { - logf( "Failed to get resources" ); + LOGF( "Failed to get resources." ); return E33_EXIT_FAILURE; } if( _get_drm_connector() ) { - logf( "Failed to set connector" ); + LOGF( "Failed to set connector." ); return E33_EXIT_FAILURE; } if( _get_drm_framebuffer() ) { - logf( "Failed to set framebuffer" ); + LOGF( "Failed to set framebuffer." ); return E33_EXIT_FAILURE; } if( _get_drm_crtc() ) { - logf( "Failed to set crtc" ); + LOGF( "Failed to set crtc." ); return E33_EXIT_FAILURE; } @@ -136,7 +134,7 @@ err display_init( void ) __display.surface.h = __display.mode.vdisplay; __display.fb.size = (Size)drmData.drmCreateDumb[0].size; __display.surface.data = __display.fb.map[0]; - __display.active = 1; + __display.active = E33_TRUE; free( drmData.drmRes_fbs ); free( drmData.drmRes_encs ); @@ -145,66 +143,66 @@ err display_init( void ) return E33_EXIT_SUCCESS; } -void display_flip( void ) +void display33_flip( void ) { - static u8 i = 1; + static u8 i_s = 1; struct drm_mode_crtc_page_flip flip = { 0, }; - __display.surface.data = __display.fb.map[i]; - i ^= 1; - __display.crtc.fb_id = __display.fb.id[i]; + __display.surface.data = __display.fb.map[i_s]; + i_s ^= 1; + __display.crtc.fb_id = __display.fb.id[i_s]; - if( e33_ioctl( __display.devfd, (int)DRM_IOCTL_MODE_SETCRTC, &__display.crtc ) == -1 ) { - logw( "Failed to set CRTC for page flip prep. Skipping" ); + if( e33_ioctl( __display.devFd, (int)DRM_IOCTL_MODE_SETCRTC, &__display.crtc ) == -1 ) { + LOGW( "Failed to set CRTC for page flip prep. Skipping." ); return; } - flip.fb_id = __display.fb.id[i]; + flip.fb_id = __display.fb.id[i_s]; flip.crtc_id = __display.crtc.crtc_id; flip.user_data = ((__u64)(&__display.crtc.crtc_id)); flip.flags = DRM_MODE_PAGE_FLIP_EVENT; - e33_ioctl( __display.devfd, (int)DRM_IOCTL_MODE_PAGE_FLIP, &flip ); + e33_ioctl( __display.devFd, (int)DRM_IOCTL_MODE_PAGE_FLIP, &flip ); /* TODO remove later */ memset( __display.surface.data, 0, (uSize)__display.fb.size ); } -void display_term( void ) +void display33_term( void ) { - if( e33_ioctl( __display.devfd, DRM_IOCTL_DROP_MASTER, 0 ) == -1 ) { - logw( "Failed to drop drm master. Oh well" ); + if( e33_ioctl( __display.devFd, DRM_IOCTL_DROP_MASTER, 0 ) == -1 ) { + LOGW( "Failed to drop drm master." ); } 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]." ); } 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]." ); } - if( close( __display.devfd ) ) { - logw( "Failed to close graphics device. Oh well" ); + if( close( __display.devFd ) ) { + LOGW( "Failed to close graphics device." ); } free( drmData.drmRes_crtcs ); - if( ioctl(__display.ttyfd, VT_SETMODE, &vtModeOld) < 0 ) { - logw( "Could not reset VT mode. Oh well" ); + if( ioctl(__display.ttyFd, VT_SETMODE, &vtModeOld) < 0 ) { + LOGW( "Could not reset VT mode." ); } if( tcsetattr(STDIN_FILENO, TCSAFLUSH, &termOldConfig) == -1 ) { - logw( "Failed to set stdin attributes. Oh well" ); + LOGW( "Failed to set stdin attributes." ); } - if( close( __display.ttyfd ) == -1 ) { - logw( "Failed to close TTY. Oh well" ); + if( close( __display.ttyFd ) == -1 ) { + LOGW( "Failed to close TTY." ); } } -void display_vtswitcher_poll( int timeout ) +void display33_vtswitcher_poll( int timeout ) { struct pollfd fds[1]; unsigned char event; @@ -219,7 +217,7 @@ void display_vtswitcher_poll( int timeout ) } if( read(fds[0].fd, &event, sizeof(event)) != sizeof(event) ) { - logw( "Invalid VT switch events read"); + LOGW( "Invalid VT switch events read."); return; } @@ -235,20 +233,20 @@ void display_vtswitcher_poll( int timeout ) -static err _open_graphics_device( void ) +static Error _open_graphics_device( void ) { struct drm_get_cap cap = {0,}; - if( (__display.devfd = open(DRM_DEVICE, O_RDWR )) == -1 ) { - logw( "Failed to open DRM device" ); + if( (__display.devFd = open( E33_DRM_DEVICE, O_RDWR )) == -1 ) { + LOGE( "Failed to open DRM device '%s'.", E33_DRM_DEVICE ); return E33_EXIT_FAILURE; } cap.capability = DRM_CAP_DUMB_BUFFER; - if( e33_ioctl( __display.devfd, DRM_IOCTL_GET_CAP, &cap ) == -1 ) { - logw( "Failed to get DRM device capabilities" ); + if( e33_ioctl( __display.devFd, DRM_IOCTL_GET_CAP, &cap ) == -1 ) { + LOGW( "Failed to get DRM device capabilities." ); return E33_EXIT_FAILURE; } @@ -261,8 +259,8 @@ static err _open_graphics_device( void ) static Size _get_drm_resources( void ) { - if( e33_ioctl(__display.devfd, (int)DRM_IOCTL_MODE_GETRESOURCES, &drmData.drmRes) == -1 ) { - logw( "Failed to init drm resources" ); + if( e33_ioctl(__display.devFd, (int)DRM_IOCTL_MODE_GETRESOURCES, &drmData.drmRes) == -1 ) { + LOGE( "Failed to init drm resources." ); return E33_EXIT_FAILURE; } @@ -283,8 +281,8 @@ static Size _get_drm_resources( void ) drmData.drmRes.encoder_id_ptr = (__u64)drmData.drmRes_encs; /* Second call writes drm resource data using the given pointers */ - if( e33_ioctl( __display.devfd, (int)DRM_IOCTL_MODE_GETRESOURCES, &drmData.drmRes ) == -1 ) { - logw( "Failed to get drm resources" ); + if( e33_ioctl( __display.devFd, (int)DRM_IOCTL_MODE_GETRESOURCES, &drmData.drmRes ) == -1 ) { + LOGE( "Failed to get drm resources." ); return E33_EXIT_FAILURE; } @@ -307,7 +305,7 @@ static Size _get_drm_connector( void ) continue; } - if( e33_ioctl( __display.devfd, (int)DRM_IOCTL_MODE_GETCONNECTOR, connector ) == -1 ) { + if( e33_ioctl( __display.devFd, (int)DRM_IOCTL_MODE_GETCONNECTOR, connector ) == -1 ) { continue; } @@ -320,7 +318,7 @@ static Size _get_drm_connector( void ) connector->prop_values_ptr = (__u64)drmData.connPropVals; connector->encoders_ptr = (__u64)drmData.connEncs; - if( e33_ioctl( __display.devfd, (int)DRM_IOCTL_MODE_GETCONNECTOR, connector) == -1 ) { + if( e33_ioctl( __display.devFd, (int)DRM_IOCTL_MODE_GETCONNECTOR, connector) == -1 ) { continue; } @@ -328,11 +326,11 @@ static Size _get_drm_connector( void ) { struct drm_mode_modeinfo *mode = &drmData.connModes[j]; - if( e33_strcmp( mode->name, DRM_MODE ) && (mode->vrefresh == DRM_RATE) ) + if( e33_strcmp( mode->name, E33_DRM_MODE ) && (mode->vrefresh == E33_DRM_RATE) ) { /* TODO there has to be a better way */ - if( mode->hdisplay % 8 ) { - mode->hdisplay += (mode->hdisplay % 8); + if( mode->hdisplay == 1366 ) { + mode->hdisplay += 10; } __display.mode = *mode; @@ -342,7 +340,7 @@ static Size _get_drm_connector( void ) } } - logw( "Failed to find a valid connector" ); + LOGE( "Failed to find a valid connector." ); return E33_EXIT_FAILURE; } @@ -353,15 +351,15 @@ static Size _get_drm_crtc( void ) encoder.encoder_id = __display.connector.encoder_id; - if( e33_ioctl( __display.devfd, (int)DRM_IOCTL_MODE_GETENCODER, &encoder ) == -1 ) { - loge( "Failed to get encoder" ); + if( e33_ioctl( __display.devFd, (int)DRM_IOCTL_MODE_GETENCODER, &encoder ) == -1 ) { + LOGE( "Failed to get encoder." ); return E33_EXIT_FAILURE; } crtc->crtc_id = encoder.crtc_id; - if( e33_ioctl( __display.devfd, (int)DRM_IOCTL_MODE_GETCRTC, crtc ) == -1 ) { - loge( "Failed to get CRTC" ); + if( e33_ioctl( __display.devFd, (int)DRM_IOCTL_MODE_GETCRTC, crtc ) == -1 ) { + LOGE( "Failed to get CRTC." ); return E33_EXIT_FAILURE; } @@ -371,8 +369,8 @@ static Size _get_drm_crtc( void ) crtc->mode = __display.mode; crtc->mode_valid = 1; - if( e33_ioctl( __display.devfd, (int)DRM_IOCTL_MODE_SETCRTC, crtc ) == -1 ) { - loge( "Could not set CRTC" ); + if( e33_ioctl( __display.devFd, (int)DRM_IOCTL_MODE_SETCRTC, crtc ) == -1 ) { + LOGE( "Could not set CRTC." ); return E33_EXIT_FAILURE; } @@ -393,10 +391,10 @@ static Size _get_drm_framebuffer( void ) drmCreateDumb[i].height = mode->vdisplay; drmCreateDumb[i].bpp = 32; - if( e33_ioctl( __display.devfd, (int)DRM_IOCTL_MODE_CREATE_DUMB, + if( e33_ioctl( __display.devFd, (int)DRM_IOCTL_MODE_CREATE_DUMB, drmCreateDumb + i ) == -1 ) { - loge( "Failed to create dumb buffer #%d", i ); + LOGE( "Failed to create dumb buffer." ); return E33_EXIT_FAILURE; } @@ -407,8 +405,8 @@ static Size _get_drm_framebuffer( void ) drmFBCmd[i].depth = 24; drmFBCmd[i].handle = drmCreateDumb[i].handle; - if( e33_ioctl( __display.devfd, (int)DRM_IOCTL_MODE_ADDFB, drmFBCmd + i ) == -1 ) { - loge( "Failed to add framebuffer #%d", i ); + if( e33_ioctl( __display.devFd, (int)DRM_IOCTL_MODE_ADDFB, drmFBCmd + i ) == -1 ) { + LOGE( "Failed to add framebuffer." ); return E33_EXIT_FAILURE; } @@ -416,18 +414,18 @@ static Size _get_drm_framebuffer( void ) drmMapDumb[i].handle = drmCreateDumb[i].handle; - if( e33_ioctl( __display.devfd, (int)DRM_IOCTL_MODE_MAP_DUMB, drmMapDumb + i ) == -1 ) + if( e33_ioctl( __display.devFd, (int)DRM_IOCTL_MODE_MAP_DUMB, drmMapDumb + i ) == -1 ) { - loge( "Failed to map dumb buffer #%d", i ); + LOGE( "Failed to map dumb buffer." ); return E33_EXIT_FAILURE; } __display.fb.map[i] = mmap( 0, drmCreateDumb[i].size, PROT_READ | PROT_WRITE, - MAP_SHARED, __display.devfd, drmMapDumb[i].offset ); + MAP_SHARED, __display.devFd, drmMapDumb[i].offset ); if( __display.fb.map[i] == MAP_FAILED ) { - loge( "Failed to map framebuffer #%d", i ); + LOGE( "Failed to map framebuffer." ); return E33_EXIT_FAILURE; } } @@ -435,31 +433,16 @@ static Size _get_drm_framebuffer( void ) return E33_EXIT_SUCCESS; } - - - - - - - - - - - - - - - static void _vt_release( void ) { - logi( "VT release signal"); + LOGI( "VT release signal."); - if( ioctl( __display.devfd, DRM_IOCTL_DROP_MASTER, 0 ) ) { - loge( "Could not drop DRM master" ); + if( ioctl( __display.devFd, DRM_IOCTL_DROP_MASTER, 0 ) ) { + LOGW( "Could not drop DRM master." ); } - if( ioctl(__display.ttyfd, VT_RELDISP, 1) < 0 ) { - loge( "Failed to release VT"); + if( ioctl(__display.ttyFd, VT_RELDISP, 1) < 0 ) { + LOGW( "Failed to release VT."); } __display.active = 0; @@ -467,18 +450,18 @@ static void _vt_release( void ) static void _vt_acquire( void ) { - logi( "VT acquire signal"); + LOGI( "VT acquire signal"); - if( ioctl( __display.devfd, DRM_IOCTL_SET_MASTER, 0 ) == -1 ) { - logw( "Could not become DRM master." ); + if( ioctl( __display.devFd, DRM_IOCTL_SET_MASTER, 0 ) == -1 ) { + LOGW( "Could not become DRM master." ); } - if( ioctl(__display.ttyfd, VT_RELDISP, VT_ACKACQ) < 0 ) { - loge( "(vt_acquire) Failed to acquire VT."); + if( ioctl(__display.ttyFd, VT_RELDISP, VT_ACKACQ) < 0 ) { + LOGW( "(vt_acquire) Failed to acquire VT."); } - if( ioctl( __display.devfd, (int)DRM_IOCTL_MODE_SETCRTC, &__display.crtc ) == -1 ) { - loge( "Failed to set crtc on VT switch." ); + if( ioctl( __display.devFd, (int)DRM_IOCTL_MODE_SETCRTC, &__display.crtc ) == -1 ) { + LOGW( "Failed to set crtc on VT switch." ); } __display.active = 1; @@ -492,12 +475,12 @@ static int _set_pipe(int fd) int flags = fcntl(fd, F_GETFD); if (flags == -1) { - logw( "Could not get flags for VT switcher pipe"); + LOGW( "Could not get flags for VT switcher pipe."); return E33_EXIT_FAILURE; } if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) { - logw( "Could not set flags for VT switcher pipe"); + LOGW( "Could not set flags for VT switcher pipe."); return E33_EXIT_FAILURE; } } @@ -523,52 +506,52 @@ static Size _init_vt_switch( void ) sigset_t set; - __display.ttyfd = -1; + __display.ttyFd = -1; if( pipe2(vtPipe, 0) == -1 ) { vtPipe[0] = vtPipe[1] = -1; - loge( "Could not set VT pipes"); + LOGW( "Could not set VT pipes."); return E33_EXIT_FAILURE; } if( _set_pipe(vtPipe[0]) ) { - loge( "Could not set VT read pipe" ); + LOGW( "Could not set VT read pipe." ); return E33_EXIT_FAILURE; } if( _set_pipe(vtPipe[1]) ) { - loge( "Could not set VT write pipe" ); + LOGW( "Could not set VT write pipe." ); return E33_EXIT_FAILURE; } - if( __display.ttyfd = open("/dev/tty", O_RDWR ) < 0 ) { - loge( "Could not open TTY for VT control" ); + if( __display.ttyFd = open("/dev/tty", O_RDWR ) < 0 ) { + LOGW( "Could not open TTY for VT control." ); return E33_EXIT_FAILURE; } if( e33_has_signal(REL_SIGNAL) ) { - loge( "VT release signal is already in use"); + LOGW( "VT release signal is already in use."); return E33_EXIT_FAILURE; } if( e33_has_signal(ACQ_SIGNAL) ) { - loge( "VT acquire signal is already in use"); + LOGW( "VT acquire signal is already in use."); return E33_EXIT_FAILURE; } if( e33_set_signal( REL_SIGNAL, _vt_switch_sighandler ) ) { - loge( "Could not set relese signal handler"); + LOGW( "Could not set relese signal handler."); return E33_EXIT_FAILURE; } if( e33_set_signal( ACQ_SIGNAL, _vt_switch_sighandler ) ) { - loge( "Could not set acquire signal handler"); + LOGW( "Could not set acquire signal handler."); return E33_EXIT_FAILURE; } - if( ioctl(__display.ttyfd, VT_GETMODE, &vtModeOld) < 0 ) { - logw( "Could not get VT mode" ); + if( ioctl(__display.ttyFd, VT_GETMODE, &vtModeOld) < 0 ) { + LOGW( "Could not get VT mode." ); return E33_EXIT_FAILURE; } @@ -578,8 +561,8 @@ static Size _init_vt_switch( void ) vtMode.acqsig = ACQ_SIGNAL; vtMode.frsig = SIGIO; - if( ioctl(__display.ttyfd, VT_SETMODE, &vtMode) < 0 ) { - logw( "Could not set VT mode" ); + if( ioctl(__display.ttyFd, VT_SETMODE, &vtMode) < 0 ) { + LOGW( "Could not set VT mode." ); return E33_EXIT_FAILURE; } diff --git a/src/input33.c b/src/input33.c new file mode 100644 index 0000000..1ba1ff2 --- /dev/null +++ b/src/input33.c @@ -0,0 +1,232 @@ +#include "fcntl.h" +#include "dirent.h" +#include "stdio.h" +#include "stdlib.h" +#include "unistd.h" +#include "string.h" +#include "sys/stat.h" +#include "sys/kd.h" +#include <linux/input-event-codes.h> +#include <linux/input.h> + +#include "input33.h" +#include "logger33.h" +#include "strings33.h" + + +#define _INPUT_DEVICES_MAX 8 +#define _POLL_EVENTS_MAX 16 + + +Input33 __input = {0}; + + +static int _is_char_device( const struct dirent *f ); +static Boolean _is_keyboard( Size fd ); +static Boolean _is_mouse( Size fd ); +static Size _has_key( Size fd, unsigned int event ); +static Size _has_rel( Size fd, unsigned int event ); +static u64 _has_evbits( Size fd, unsigned int type ); + + +Error input33_init( void ) +{ + Size i, deviceCount; + Boolean hasKeyboard = E33_FALSE; + Boolean hasMouse = E33_FALSE; + struct dirent **evfs; + + deviceCount = scandir( "/dev/input", &evfs, _is_char_device, 0 ); + + if( deviceCount <= 0 ) { + LOGE( "No valid character devices found." ); + return E33_EXIT_FAILURE; + } + + for( i = 0; i < deviceCount; ++i ) + { + Size j; + Size devFd; + char path[ _STRING_MAX ]; + + snprintf( path, _STRING_MAX , "%s%s", "/dev/input/", evfs[i]->d_name ); + free( evfs[i] ); + + devFd = open( path, O_RDONLY | O_NONBLOCK ); + if( devFd == -1 ) { + continue; + } + + if( _is_keyboard( devFd ) ) + { + hasKeyboard = E33_TRUE; + __input.devices[ __input.deviceCount++ ] = devFd; + } + else if( _is_mouse(devFd) ) + { + hasMouse = E33_TRUE; + __input.devices[ __input.deviceCount++ ] = devFd; + ioctl( devFd, EVIOCGRAB, 1 ); + } + else { + close( devFd ); + } + + if( __input.deviceCount == _INPUT_DEVICES_MAX ){ + break; + } + } + + free( evfs ); + + if( (hasKeyboard == E33_TRUE) && (hasMouse == E33_TRUE) ) { + return E33_EXIT_SUCCESS; + } + + return E33_EXIT_FAILURE; +} + +void input33_poll( void ) +{ + u16 i; + + for( i = 0; i < __input.deviceCount; ++i ) + { + Size j; + Size readBytes; + Size eventCount; + struct input_event events[ _POLL_EVENTS_MAX ]; + + __input.relStates[REL_X] = 0; + __input.relStates[REL_Y] = 0; + __input.relStates[REL_WHEEL] = 0; + + readBytes = read( __input.devices[i], events, + sizeof(struct input_event) * _POLL_EVENTS_MAX ); + + if( readBytes == -1 ) { + continue; + } + + eventCount = readBytes / sizeof( struct input_event ); + + for( j = 0; j < eventCount; ++j ) + { + switch( events[j].type ) + { + case EV_KEY: + __input.keyStates[ events[j].code ] = events[j].value; + + if( (events[j].value == _PRESS) || (events[j].value == _REPEAT) ) + { + __input.buffer[__input.tail++] = events[j].code; + __input.tail &= (_KBBUFF_LEN - 1); + + if( __input.tail == __input.head ) + { + ++__input.head; + __input.head &= (_KBBUFF_LEN - 1); + } + } + break; + + case EV_REL: + __input.relStates[ events[j].code ] = events[j].value; + break; + } + } + } + + __input.modFlags = 6; + ioctl( 0, TIOCLINUX, &__input.modFlags ); + ioctl( 0, KDGKBLED, &__input.cnsFlags ); +} + +void input33_term( void ) +{ + Size i; + + for( i = 0; i < __input.deviceCount; ++i ) { + close( __input.devices[i] ); + } +} + + +static u64 _has_evbits( Size fd, unsigned int type ) +{ + u64 kbb; + + if( ioctl( fd, EVIOCGBIT( 0, (int)sizeof(kbb) ), &kbb ) == -1 ) { + return 0; + } + + return (kbb & (1 << type)); +} + +static Size _has_key( Size fd, unsigned int event ) +{ + unsigned char bits[ (KEY_MAX >> 3) + 1 ]; + + if( ioctl( fd, EVIOCGBIT(EV_KEY, (int)sizeof(bits)), &bits ) == -1 ) { + return 0; + } + + return ( (bits[event >> 3] & (1 << (event % 8))) > 0 ); +} + +static Size _has_rel( Size fd, unsigned int event ) +{ + unsigned char bits[ (REL_MAX >> 3) + 1 ]; + + if( ioctl( fd, EVIOCGBIT(EV_REL, (int)sizeof(bits)), &bits ) == -1 ) { + return 0; + } + + return ( (bits[event >> 3] & (1 << (event % 8))) > 0 ); +} + +static Boolean _is_mouse( Size fd ) +{ + if( _has_evbits( fd, EV_KEY ) && + _has_evbits( fd, EV_REL ) ) + { + return( _has_rel( fd, REL_X ) & + _has_rel( fd, REL_Y ) & + _has_key( fd, BTN_LEFT ) & + _has_key( fd, BTN_RIGHT ) & 1 ); + } + + return E33_FALSE; +} + +static Boolean _is_keyboard( Size fd ) +{ + if( _has_evbits( fd, EV_KEY ) ) + { + return( _has_key( fd, KEY_W ) & + _has_key( fd, KEY_SPACE ) & + _has_key( fd, KEY_LEFTSHIFT ) & + _has_key( fd, KEY_I ) & 1 ); + } + + return E33_FALSE; +} + +static int _is_char_device( const struct dirent *f ) +{ + struct stat fstat; + char path[512]; + + if( strncmp( f->d_name, "event", 5 ) ) { + return 0; + } + + snprintf( path, 512, "%s%s", "/dev/input/", f->d_name ); + + if( stat( path, &fstat ) ) { + return 0; + } + + return S_ISCHR( fstat.st_mode ); +} + diff --git a/src/logger.c b/src/logger.c deleted file mode 100644 index c90f364..0000000 --- a/src/logger.c +++ /dev/null @@ -1,103 +0,0 @@ -#include <linux/vt.h> - -#include "fcntl.h" -#include "stdarg.h" -#include "errno.h" -#include "time.h" -#include "unistd.h" -#include "stdio.h" -#include "string.h" - -#include "logger.h" -#include "options.h" -#include "ui33.h" - -Logger __logger; - - -static char *ansiColourPrefix[5] = { - "[\x1b[36mD\x1b[37m]", - "[\x1b[32mI\x1b[37m]", - "[\x1b[35mW\x1b[37m]", - "[\x1b[33mE\x1b[37m]", - "[\x1b[31mFl\x1b[37m]", -}; - -static char uiColourPrefix[5][11] = { - { 6, '[', 'D', 'e', 'b', 'u', 'g', ']', ' ', 1, '\0' }, - { 3, '[', 'I', 'n', 'f', 'o', ']', ' ', 1, '\0', ' ' }, - { 5, '[', 'W', 'a', 'r', 'n', ']', ' ', 1, '\0', ' ' }, - { 8, '[', 'E', 'r', 'r', 'o', 'r', ']', ' ', 1, '\0' }, - { 2, '[', 'F', 'a', 't', 'a', 'l', ']', ' ', 1, '\0' } -}; - -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); - } - - __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." ); - return E33_EXIT_FAILURE; - } - - __logger.func = 0; - - ui_add_message( "Welcome to Engine33" ); - dprintf( __logger.fd, "\033[3J\033[1;1HWelcome to Engine33!\r\n\r\n" ); - - return E33_EXIT_SUCCESS; -} - -void logger_term( void ) -{ - logi( "Goodbye!" ); - close( __logger.fd ); -} - -void logger( Size level, const char *fmt, ... ) -{ - static char uiString[256]; - - time_t currentTime; - struct tm *m_time; - - - va_list args; - va_start(args, fmt); - - time(¤tTime); - m_time = localtime(¤tTime); - - snprintf( uiString, 256, "%s%s", uiColourPrefix[level], fmt ); - - dprintf( __logger.fd, "Engine33 [%02d:%02d:%02d] -", - m_time->tm_hour, m_time->tm_min, m_time->tm_sec ); - - dprintf( __logger.fd, " %s ", ansiColourPrefix[level] ); - vdprintf(__logger.fd, fmt, args); - - if( (level > LOG_INFO) && errno ) - { - String strerr = strerror(errno); - dprintf( __logger.fd, ". Reason: %s", strerr ); - snprintf( uiString, 256, "%s >> %s", uiString, strerr ); - errno = 0; - } - - dprintf( __logger.fd, ".\r\n" ); - - ui_add_message( uiString, args ); - - va_end(args); -} - diff --git a/src/logger33.c b/src/logger33.c new file mode 100644 index 0000000..d9b0d8d --- /dev/null +++ b/src/logger33.c @@ -0,0 +1,111 @@ +#include <linux/vt.h> + +#include "fcntl.h" +#include "stdarg.h" +#include "errno.h" +#include "time.h" +#include "unistd.h" +#include "stdio.h" +#include "string.h" + +#include "options33.h" +#include "strings33.h" +#include "logger33.h" +#include "ui33.h" + + +typedef struct { + Size fd; +} Logger; + +Logger logger_t; + + +static char *_ansiColourPrefix[5] = { + "[\x1b[36mD\x1b[37m]", + "[\x1b[32mI\x1b[37m]", + "[\x1b[35mW\x1b[37m]", + "[\x1b[33mE\x1b[37m]", + "[\x1b[31mF\x1b[37m]" +}; + +static char _uiColourPrefix[5][5] = { + { UI33_CYAN, 'D', UI33_WHITE, ':', '\0' }, + { UI33_GREEN, 'I', UI33_WHITE, ':', '\0' }, + { UI33_YELLOW, 'W', UI33_WHITE, ':', '\0' }, + { UI33_ORANGE, 'E', UI33_WHITE, ':', '\0' }, + { UI33_RED, 'F', UI33_WHITE, ':', '\0' } +}; + + +Size logger33_init( void ) +{ + String logFile[ _STRING_MAX ]; + + if( E33_LOG_FILE[0] == '~' ) { + snprintf(logFile, _STRING_MAX , "%s/%s", getenv("HOME"), E33_LOG_FILE+1); + } else { + snprintf(logFile, _STRING_MAX , "%s", E33_LOG_FILE); + } + + logger_t.fd = open(logFile, + O_CREAT | O_TRUNC | O_WRONLY | O_NOCTTY | O_TTY_INIT, + 0644 ); + + if( logger_t.fd < 0 ) { + printf( "Unable to initialize logger. Exiting." ); + return E33_EXIT_FAILURE; + } + + ui33_add_message( "Welcome to Engine33!" ); + dprintf( logger_t.fd, "\033[3J\033[1;1HWelcome to Engine33!\r\n\r\n" ); + + return E33_EXIT_SUCCESS; +} + +void logger33_term( void ) +{ + LOGI( "Goodbye!" ); + close( logger_t.fd ); +} + +void _logger( Size level, String file, int line , String func, const char *fmt, ... ) +{ + char uiStringTemp[ _STRING_MAX ]; + char uiString[ _STRING_MAX ]; + + time_t currentTime; + struct tm *mTime; + + + va_list args; + va_start(args, fmt); + + + time(¤tTime); + mTime = localtime(¤tTime); + + + dprintf( logger_t.fd, "Engine33 [%02d:%02d:%02d] [%s:%d @%s()] -", + mTime->tm_hour, mTime->tm_min, mTime->tm_sec, file, line, func ); + + dprintf( logger_t.fd, " %s ", _ansiColourPrefix[level] ); + vdprintf(logger_t.fd, fmt, args); + + if( (level > _INFO) && errno ) + { + dprintf( logger_t.fd, " [%s]", strerror(errno) ); + errno = 0; + } + dprintf( logger_t.fd, "\r\n" ); + + + snprintf( uiStringTemp, _STRING_MAX , "%s %s", _uiColourPrefix[level], fmt ); + vsnprintf( uiString, _STRING_MAX , uiStringTemp, args ); + + ui33_add_message( uiString ); + + + va_end(args); +} + @@ -1,9 +1,11 @@ -#include "display.h" -#include "clock.h" -#include "logger.h" -#include "system.h" #include "signal.h" + +#include "display33.h" +#include "clock33.h" +#include "logger33.h" +#include "system33.h" #include "strings33.h" +#include "input33.h" #include "ui33.h" static void _term_sighandler( int sig ); @@ -14,37 +16,60 @@ 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"); } + 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; FPS = 0; - if( logger_init() ) { + if( logger33_init() ) { return EXIT_FAILURE; } - if( display_init() ) { + if( input33_init() ) { return EXIT_FAILURE; } - logd( "This is a test." ); + if( display33_init() ) { + return EXIT_FAILURE; + } while(1) { - display_vtswitcher_poll(1); + display33_vtswitcher_poll(1); if( __display.active ) { - qsuprintf( 5, 5, "fps: %d", FPS-1 ); + input33_poll(); + + QPRINTF( 5, 5, "fps: %d", FPS-1 ); + + if( KEYDOWN(KEY_K) ) { ui33_add_message("Pressed '%c'", 'K'); } + + if( MOUSEX ) { + __mouse.x += MOUSEX; + } + if( MOUSEY ) { + __mouse.y += MOUSEY; + } - ui_draw_messages(); - display_flip(); + + if( __mouse.x < 0 ) __mouse.x = 0; + if( __mouse.y < 0 ) __mouse.y = 0; + if( __mouse.x >= __display.surface.w ) __mouse.x = __display.surface.w-1; + if( __mouse.y >= __display.surface.h ) __mouse.y = __display.surface.h-1; + ui33_draw_mouse(); + + QPRINTF( 100, 5, "M: x(%d), y(%d)", __mouse.x, __mouse.y ); + + + ui33_draw_messages(); + display33_flip(); } @@ -60,8 +85,10 @@ int main(int argc, char *argv[]) ++fps; } - display_term(); - logger_term(); + + display33_term(); + logger33_term(); + input33_term(); return EXIT_SUCCESS; @@ -70,8 +97,8 @@ int main(int argc, char *argv[]) static void _term_sighandler( int sig ) { - display_term(); - logger_term(); + display33_term(); + logger33_term(); exit(0); } diff --git a/src/strings33.c b/src/strings33.c index 8613c02..0291393 100644 --- a/src/strings33.c +++ b/src/strings33.c @@ -3,74 +3,69 @@ #include "string.h" #include "strings33.h" -#include "types.h" +#include "types33.h" -extern Font33 defFont; -extern Font33 bigFont; - static Font33 *fonts[2] = { &defFont, &bigFont }; static u32 textColours[9] = { - 0xff000000, - 0xffffffff, - 0xffff0000, - 0xff00ff00, - 0xff0000ff, - 0xffffff00, - 0xff00ffff, - 0xffff00ff, - 0xffffaa00, + BLACK, + RED, + GREEN, + BLUE, + YELLOW, + ORANGE, + MAGENTA, + CYAN, + WHITE }; -void e33_uprintf( Surface *surf, Size f, u32 col, Size x, Size y, const char *fmt, ... ) +void e33_uprintf( Surface33 *surf, Size f, u32 col, Size x, Size y, const char *fmt, ... ) { - static char tmp[512]; + static char tmp[ _STRING_MAX ]; va_list args; va_start(args, fmt); - vsprintf(tmp, fmt, args); + vsnprintf(tmp, _STRING_MAX, fmt, args); e33_uprint( surf, f, col, x, y, tmp ); va_end(args); } -void e33_uprint( Surface *surf, Size f, u32 col, Size x, Size y, const char *str ) +void e33_uprint( Surface33 *surf, Size f, u32 col, Size x, Size y, const char *str ) { Size i, j, ofs; Font33 *font = fonts[f]; Size sWidth = surf->w; Size sHeight = surf->h; FontGlyph33 *glyphs = font->glyphs; - Size fbw = surf->w; - Size fbh = surf->h; u32 *out; - uSize slen = strlen(str); + uSize stringLen = strlen(str); Size strWid = 0; - for( i = 0; i < slen; ++i ) { + for( i = 0; i < stringLen; ++i ) { if( str[i] < 0x20 ) continue; - strWid += glyphs[ str[i] - 0x20 ].width; + strWid += glyphs[ str[i] - 0x20 ].w; } if( (x + strWid) >= sWidth ) { x -= (strWid - (sWidth - x)); } - if( (y + font->height) >= sHeight ) { - y -= (font->height - (sHeight - y)); + if( (y + font->h) >= sHeight ) { + y -= (font->h- (sHeight - y)); } if(x < 0) { x = 0; } if(y < 0) { y = 0; } out = surf->data + (x + (y * surf->w)); - for( i = 0, ofs = 0; i < slen; ++i ) + for( i = 0, ofs = 0; i < stringLen; ++i ) { if( str[i] < 0x20 ) { @@ -79,14 +74,14 @@ void e33_uprint( Surface *surf, Size f, u32 col, Size x, Size y, const char *str } FontGlyph33 *glyph = &glyphs[ str[i] - 0x20 ]; - Size numPlots = glyph->num_plots; - Size gWidth = glyph->width; + Size numPlots = glyph->plotCount; + Size gWidth = glyph->w; u16 *plots = glyph->plots; for( j = 0; j < numPlots; ++j ) { Size plot = plots[j]; - Size offset = (((plot & font->yAnd) * fbw) + (plot >> font->xShift)) + ofs; + Size offset = (((plot & font->yAnd) * sWidth) + (plot >> font->xShift)) + ofs; memcpy( out + offset, &col, sizeof(u32) ); } diff --git a/src/system.c b/src/system33.c index 9a9a80e..c533503 100644 --- a/src/system.c +++ b/src/system33.c @@ -4,13 +4,13 @@ #include "stdarg.h" #include "stdio.h" #include "unistd.h" -#include "string.h" #include "signal.h" +#include "string.h" -#include "types.h" +#include "types33.h" -err e33_ioctl( Size fd, unsigned long req, void *arg ) +Error e33_ioctl( Size fd, unsigned long req, void *arg ) { Size ret; @@ -21,7 +21,7 @@ err e33_ioctl( Size fd, unsigned long req, void *arg ) return ret; } -boolean e33_strcmp( String s1, String s2 ) +Boolean e33_strcmp( String s1, String s2 ) { while( (*s1 != '\0') && (*s2 != '\0') ) { @@ -33,14 +33,14 @@ boolean e33_strcmp( String s1, String s2 ) return E33_TRUE; } -err e33_has_signal(Size signo) +Error 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)) +Error e33_set_signal(Size signo, void(*sig_handler)(int)) { struct sigaction sact = {0}; sact.sa_handler = sig_handler; @@ -1,42 +1,58 @@ -#include "string.h" #include "stdarg.h" #include "stdio.h" +#include "string.h" -#include "types.h" +#include "types33.h" +#include "ui33.h" #include "strings33.h" -#include "logger.h" +#include "logger33.h" + + +#define _MBOX_SIZE 9 +#define _PADDING 4 +#define _SPACING 2 -#define MB_MAX 6 +static char messageBox[ _MBOX_SIZE ][ _STRING_MAX ] = { 0, }; +static Size head = 0; +static Size tail = 0; -static char messageBox[MB_MAX][256] = { 0, }; -static Size A = 0; -static Size P = 0; +Mouse33 __mouse = {0,}; -void ui_add_message( String fmt, ... ) +void ui33_add_message( String fmt, ... ) { va_list args; va_start(args, fmt); - vsnprintf( messageBox[P++], 256, fmt, args ); - if( P == MB_MAX ) P = 0; - if( P == A ) ++A; - if( A == MB_MAX ) A = 0; + vsnprintf( messageBox[tail++], _STRING_MAX, fmt, args ); + if( tail == _MBOX_SIZE ) tail = 0; + if( tail == head ) ++head; + if( head == _MBOX_SIZE ) head = 0; va_end(args); } -void ui_draw_messages( void ) +void ui33_draw_messages( void ) { - Size start = __display.surface.h - 5 - 8; - Size i = P-1; + Size fontHeight = defFont.h; + Size stringCoord = __display.surface.h - _PADDING - fontHeight; + Size i = tail-1; do { - if( i < 0 ) i = MB_MAX-1; - qsuprint( messageBox[i], 5, start ); - start -= 18; - } while( (i--) != A ); + if( i < 0 ) i = _MBOX_SIZE-1; + QPRINT( messageBox[i], _PADDING, stringCoord ); + stringCoord -= (_SPACING + fontHeight); + } while( (i--) != head ); +} + +void ui33_draw_mouse( void ) +{ + Size x = __mouse.x; + Size y = __mouse.y; + Size w = __display.surface.w; + + __display.surface.data[ x + y * w ] = YELLOW; } |