FreeRTOS Emulator with SDL2 Based Graphics and Input Library  1.0
A POSIX wrapper to run FreeRTOS on an x86 machine with some basic input and output libraries aimed at making teaching FreeRTOS simpler.
gfx_utils.c File Reference

Utilities required by other gfx_XXX files. More...

#include <assert.h>
#include <dirent.h>
#include <libgen.h>
#include <pthread.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include "gfx_print.h"
#include "gfx_utils.h"
#include "EmulatorConfig.h"
#include <stdatomic.h>

Classes

struct  ring_buf
 

Macros

#define _GNU_SOURCE
 
#define CAST_RBUF(rbuf)   ((struct ring_buf*)rbuf)
 
#define INCLUDE_DIR_NAMES   0b1
 

Functions

int gfxUtilIsCurGLThread (void)
 Checks if the calling thread is the thread that currently holds the GL context. More...
 
void gfxUtilSetGLThread (void)
 The calling thread is registered as holding the current GL context. More...
 
char * gfxUtilPrependPath (const char *path, char *file)
 Prepends a path string to a filename. More...
 
char * gfxUtilGetBinFolderPath (char *bin_path)
 Gets the execution folder of the current program, assumes that program is executing from a folder "bin". More...
 
const char * gfxUtilFindResourceDirectory (void)
 Returns the lopcation of the resource directory. More...
 
FILE * gfxUtilFindResource (char *resource_name, const char *mode)
 Searches for a file in the RESOURCES_DIRECTORY and returns a FILE * if found. More...
 
char * gfxUtilFindResourcePath (char *resource_name)
 Similar to gfxUtilFindResource() only returning the file's path instead of the opened FILE's reference. More...
 
rbuf_handle_t gfxRbufInit (size_t item_size, size_t item_count)
 Initialized a ring buffer object with a certain number of objects of a given size. More...
 
rbuf_handle_t gfxRbufInitStatic (size_t item_size, size_t item_count, void *buffer)
 Initialized a ring buffer object with a certain number of objects of a given size into a statically allocated buffer. More...
 
void gfxRbufFree (rbuf_handle_t rbuf)
 Frees a ring buffer. More...
 
void gfxRbufReset (rbuf_handle_t rbuf)
 Resets the ring buffer to it's initial state. More...
 
int gfxRbufPutBuffer (rbuf_handle_t rbuf)
 Used when a reference to the next buffer item is already filled, incrementing the next buffer item should an item need to be retrieved. More...
 
int gfxRbufPut (rbuf_handle_t rbuf, void *data)
 Fills the next available buffer slot, if a slot is free. More...
 
int gfxRbufFPut (rbuf_handle_t rbuf, void *data)
 Fills the next available buffer, overwriting data if the ring buffer is full. More...
 
void * gfxRbufGetBuffer (rbuf_handle_t rbuf)
 Returns a reference to the data of the next ring buffer entry. More...
 
int gfxRbufGet (rbuf_handle_t rbuf, void *data)
 Returns a copy of the next buffer item's data. More...
 
unsigned char gfxRbufEmpty (rbuf_handle_t rbuf)
 Checks if the buffer is empty or not. More...
 
unsigned char gfxRbufFull (rbuf_handle_t rbuf)
 Checks if the buffer is full. More...
 
size_t gfxRbufSize (rbuf_handle_t rbuf)
 Returns the number of elements currently stored in the ring buffer. More...
 
size_t gfxRbufCapacity (rbuf_handle_t rbuf)
 Returns the maximum number of elements that the ring buffer can store. More...
 

Detailed Description

Utilities required by other gfx_XXX files.

Author
Alex Hoffman
Date
27 August 2019
   ----------------------------------------------------------------------
    Copyright (C) Alexander Hoffman, 2019
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
   ----------------------------------------------------------------------

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

◆ CAST_RBUF

#define CAST_RBUF (   rbuf)    ((struct ring_buf*)rbuf)

◆ INCLUDE_DIR_NAMES

#define INCLUDE_DIR_NAMES   0b1

Function Documentation

◆ gfxRbufCapacity()

size_t gfxRbufCapacity ( rbuf_handle_t  rbuf)

Returns the maximum number of elements that the ring buffer can store.

Parameters
rbufHandle to the ring buffer
Returns
The maximum number of elements that the ring buffer can store

◆ gfxRbufEmpty()

unsigned char gfxRbufEmpty ( rbuf_handle_t  rbuf)

Checks if the buffer is empty or not.

Parameters
rbufHandle to the ring buffer
Returns
1 if the buffer is empty, 0 otherwise

◆ gfxRbufFPut()

int gfxRbufFPut ( rbuf_handle_t  rbuf,
void *  data 
)

Fills the next available buffer, overwriting data if the ring buffer is full.

Parameters
rbufHandle to the ring buffer
dataReference to the data to be copied into the buffer
Returns
0 on success

◆ gfxRbufFree()

void gfxRbufFree ( rbuf_handle_t  rbuf)

Frees a ring buffer.

Parameters
rbufHandle to the ring buffer

◆ gfxRbufFull()

unsigned char gfxRbufFull ( rbuf_handle_t  rbuf)

Checks if the buffer is full.

Parameters
rbufHandle to the ring buffer
Returns
1 if the buffer is full, 0 otherwise

◆ gfxRbufGet()

int gfxRbufGet ( rbuf_handle_t  rbuf,
void *  data 
)

Returns a copy of the next buffer item's data.

Parameters
rbufHandle to the ring buffer
dataA reference to the allocated memory region into which the data should be copied
Returns
0 on success

◆ gfxRbufGetBuffer()

void* gfxRbufGetBuffer ( rbuf_handle_t  rbuf)

Returns a reference to the data of the next ring buffer entry.

Because only a reference is returned the contents of the buffer entry cannot be guarenteed

Parameters
rbufHandle to the ring buffer
Returns
A reference to the next item in the buffer's data

◆ gfxRbufInit()

rbuf_handle_t gfxRbufInit ( size_t  item_size,
size_t  item_count 
)

Initialized a ring buffer object with a certain number of objects of a given size.

Parameters
item_sizeThe size, in bytes, of each ring buffer item
item_countThe maximum number of items to be stored in the ring buffer
Returns
A handle to the created ring buffer, else NULL

◆ gfxRbufInitStatic()

rbuf_handle_t gfxRbufInitStatic ( size_t  item_size,
size_t  item_count,
void *  buffer 
)

Initialized a ring buffer object with a certain number of objects of a given size into a statically allocated buffer.

Parameters
item_sizeThe size, in bytes, of each ring buffer item
item_countThe maximum number of items to be stored in the ring buffer
bufferReference to the statically allocated memory region that is to be used for storing the ring buffer
Returns
A handle to the created ring buffer, else NULL

◆ gfxRbufPut()

int gfxRbufPut ( rbuf_handle_t  rbuf,
void *  data 
)

Fills the next available buffer slot, if a slot is free.

Parameters
rbufHandle to the ring buffer
dataReference to the data to be copied into the buffer
Returns
0 on success

◆ gfxRbufPutBuffer()

int gfxRbufPutBuffer ( rbuf_handle_t  rbuf)

Used when a reference to the next buffer item is already filled, incrementing the next buffer item should an item need to be retrieved.

Parameters
rbufHandle to the ring buffer
Returns
0 on success

◆ gfxRbufReset()

void gfxRbufReset ( rbuf_handle_t  rbuf)

Resets the ring buffer to it's initial state.

Parameters
rbufHandle to the ring buffer

◆ gfxRbufSize()

size_t gfxRbufSize ( rbuf_handle_t  rbuf)

Returns the number of elements currently stored in the ring buffer.

Parameters
rbufHandle to the ring buffer
Returns
Number of elements stored in the ring buffer

◆ gfxUtilFindResource()

FILE* gfxUtilFindResource ( char *  resource_name,
const char *  mode 
)

Searches for a file in the RESOURCES_DIRECTORY and returns a FILE * if found.

Parameters
resource_nameName of the file to be found
modeThe reading mode to be used when opening the file, eg. "rw"
Returns
FILE reference if found, otherwise NULL

◆ gfxUtilFindResourceDirectory()

const char* gfxUtilFindResourceDirectory ( void  )

Returns the lopcation of the resource directory.

Returns
String reference if found, otherwise NULL

◆ gfxUtilFindResourcePath()

char* gfxUtilFindResourcePath ( char *  resource_name)

Similar to gfxUtilFindResource() only returning the file's path instead of the opened FILE's reference.

The found filename is stored in a statically allocated buffer and can be overwritten by subsequent calls to the functions

Parameters
resource_nameName of the file to be found
Returns
Reference to the statically allocated filename, else NULL

◆ gfxUtilGetBinFolderPath()

char* gfxUtilGetBinFolderPath ( char *  bin_path)

Gets the execution folder of the current program, assumes that program is executing from a folder "bin".

Parameters
bin_pathThe program's binary's location, usually argv[0]
Returns
char * String of the folder's absolute location

◆ gfxUtilIsCurGLThread()

int gfxUtilIsCurGLThread ( void  )

Checks if the calling thread is the thread that currently holds the GL context.

Returns
0 if the current thread does hold the GL context, -1 otherwise.

◆ gfxUtilPrependPath()

char* gfxUtilPrependPath ( const char *  path,
char *  file 
)

Prepends a path string to a filename.

Parameters
pathPath string to be prepended
fileFilename to which the path string should be prepended
Returns
char * to the complete compiled path

◆ gfxUtilSetGLThread()

void gfxUtilSetGLThread ( void  )

The calling thread is registered as holding the current GL context.