[c-host] linux: updated, but not working yet :)

This commit is contained in:
Geoffrey McRae 2019-03-04 17:08:49 +11:00
parent 8120913acb
commit 40a1b860bf
2 changed files with 70 additions and 15 deletions

View File

@ -19,6 +19,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include "capture/interface.h" #include "capture/interface.h"
#include "debug.h" #include "debug.h"
#include "app.h"
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
@ -36,6 +37,7 @@ struct xcb
uint32_t seg; uint32_t seg;
int shmID; int shmID;
void * data; void * data;
osEventHandle * frameEvent;
unsigned int width; unsigned int width;
unsigned int height; unsigned int height;
@ -65,6 +67,15 @@ static bool xcb_create()
this = (struct xcb *)calloc(sizeof(struct xcb), 1); this = (struct xcb *)calloc(sizeof(struct xcb), 1);
this->shmID = -1; this->shmID = -1;
this->data = (void *)-1; this->data = (void *)-1;
this->frameEvent = os_createEvent(true);
if (!this->frameEvent)
{
DEBUG_ERROR("Failed to create the frame event");
free(this);
return false;
}
return true; return true;
} }
@ -73,6 +84,8 @@ static bool xcb_init()
assert(this); assert(this);
assert(!this->initialized); assert(!this->initialized);
os_resetEvent(this->frameEvent);
this->xcb = xcb_connect(NULL, NULL); this->xcb = xcb_connect(NULL, NULL);
if (!this->xcb || xcb_connection_has_error(this->xcb)) if (!this->xcb || xcb_connection_has_error(this->xcb))
{ {
@ -145,6 +158,7 @@ static bool xcb_deinit()
static void xcb_free() static void xcb_free()
{ {
os_freeEvent(this->frameEvent);
free(this); free(this);
this = NULL; this = NULL;
} }
@ -174,6 +188,7 @@ static CaptureResult xcb_capture(bool * hasFrameUpdate, bool * hasPointerUpdate)
*hasFrameUpdate = true; *hasFrameUpdate = true;
this->hasFrame = true; this->hasFrame = true;
os_signalEvent(this->frameEvent);
} }
return CAPTURE_RESULT_OK; return CAPTURE_RESULT_OK;
@ -186,11 +201,7 @@ static bool xcb_getFrame(CaptureFrame * frame)
assert(frame); assert(frame);
assert(frame->data); assert(frame->data);
if (!this->hasFrame) os_waitEvent(this->frameEvent, TIMEOUT_INFINITE);
{
DEBUG_ERROR("No frame to get");
return false;
}
xcb_shm_get_image_reply_t * img; xcb_shm_get_image_reply_t * img;
img = xcb_shm_get_image_reply(this->xcb, this->imgC, NULL); img = xcb_shm_get_image_reply(this->xcb, this->imgC, NULL);
@ -206,8 +217,8 @@ static bool xcb_getFrame(CaptureFrame * frame)
frame->stride = this->width; frame->stride = this->width;
frame->format = CAPTURE_FMT_BGRA; frame->format = CAPTURE_FMT_BGRA;
memcpy(frame->data, this->data, this->width * this->height * 4); memcpy(frame->data, this->data, this->width * this->height * 4);
free(img); free(img);
this->hasFrame = false; this->hasFrame = false;
return true; return true;
} }

View File

@ -29,6 +29,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include <string.h> #include <string.h>
#include <pthread.h> #include <pthread.h>
#include <signal.h> #include <signal.h>
#include <errno.h>
struct app struct app
{ {
@ -251,9 +252,10 @@ struct osEventHandle
pthread_mutex_t mutex; pthread_mutex_t mutex;
pthread_cond_t cond; pthread_cond_t cond;
bool flag; bool flag;
bool autoReset;
}; };
osEventHandle * os_createEvent() osEventHandle * os_createEvent(bool autoReset)
{ {
osEventHandle * handle = (osEventHandle *)calloc(sizeof(osEventHandle), 1); osEventHandle * handle = (osEventHandle *)calloc(sizeof(osEventHandle), 1);
if (!handle) if (!handle)
@ -276,6 +278,8 @@ osEventHandle * os_createEvent()
return NULL; return NULL;
} }
handle->autoReset = autoReset;
return handle; return handle;
} }
@ -288,7 +292,7 @@ void os_freeEvent(osEventHandle * handle)
free(handle); free(handle);
} }
bool os_waitEvent(osEventHandle * handle) bool os_waitEvent(osEventHandle * handle, unsigned int timeout)
{ {
assert(handle); assert(handle);
@ -299,6 +303,8 @@ bool os_waitEvent(osEventHandle * handle)
} }
while(!handle->flag) while(!handle->flag)
{
if (timeout == TIMEOUT_INFINITE)
{ {
if (pthread_cond_wait(&handle->cond, &handle->mutex) != 0) if (pthread_cond_wait(&handle->cond, &handle->mutex) != 0)
{ {
@ -306,7 +312,24 @@ bool os_waitEvent(osEventHandle * handle)
return false; return false;
} }
} }
else
{
struct timespec ts;
ts.tv_sec = timeout / 1000;
ts.tv_nsec = (timeout % 1000) * 1000000;
switch(pthread_cond_timedwait(&handle->cond, &handle->mutex, &ts))
{
case ETIMEDOUT:
return false;
default:
DEBUG_ERROR("Timed wait failed");
return false;
}
}
}
if (handle->autoReset)
handle->flag = false; handle->flag = false;
if (pthread_mutex_unlock(&handle->mutex) != 0) if (pthread_mutex_unlock(&handle->mutex) != 0)
@ -344,3 +367,24 @@ bool os_signalEvent(osEventHandle * handle)
return true; return true;
} }
bool os_resetEvent(osEventHandle * handle)
{
assert(handle);
if (pthread_mutex_lock(&handle->mutex) != 0)
{
DEBUG_ERROR("Failed to lock the mutex");
return false;
}
handle->flag = false;
if (pthread_mutex_unlock(&handle->mutex) != 0)
{
DEBUG_ERROR("Failed to unlock the mutex");
return false;
}
return true;
}