[c-host] restructure project to use cmake

This commit is contained in:
Geoffrey McRae 2019-04-09 16:28:11 +10:00
parent ccd0fd8902
commit a82b1a2e2f
25 changed files with 237 additions and 99 deletions

View file

@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.0)
project(platform_Linux LANGUAGES C)
include_directories(
${PROJECT_SOURCE_DIR}/include
)
add_library(platform_Linux STATIC
src/platform.c
)
add_subdirectory("capture")
target_link_libraries(platform_Linux
capture
pthread
)
target_include_directories(platform_Linux
PRIVATE
src
)

View file

@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.0)
project(capture LANGUAGES C)
include("PreCapture")
add_capture("XCB")
include("PostCapture")
add_library(capture STATIC ${CAPTURE_C})
target_link_libraries(capture ${CAPTURE_LINK})

View file

@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.0)
project(capture_XCB LANGUAGES C)
add_library(capture_XCB STATIC
src/xcb.c
)
target_link_libraries(capture_XCB
xcb
xcb-shm
Xfixes
)
target_include_directories(capture_XCB
PRIVATE
src
)

View file

@ -0,0 +1,241 @@
/*
Looking Glass - KVM FrameRelay (KVMFR) Client
Copyright (C) 2017-2019 Geoffrey McRae <geoff@hostfission.com>
https://looking-glass.hostfission.com
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 2 of the License, or (at your option) 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, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "capture/interface.h"
#include "debug.h"
#include "app.h"
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <inttypes.h>
#include <xcb/shm.h>
#include <xcb/xfixes.h>
#include <sys/ipc.h>
#include <sys/shm.h>
struct xcb
{
bool initialized;
xcb_connection_t * xcb;
xcb_screen_t * xcbScreen;
uint32_t seg;
int shmID;
void * data;
osEventHandle * frameEvent;
unsigned int width;
unsigned int height;
bool hasFrame;
xcb_shm_get_image_cookie_t imgC;
xcb_xfixes_get_cursor_image_cookie_t curC;
};
struct xcb * this = NULL;
// forwards
static bool xcb_deinit();
static unsigned int xcb_getMaxFrameSize();
// implementation
static const char * xcb_getName()
{
return "XCB";
}
static bool xcb_create()
{
assert(!this);
this = (struct xcb *)calloc(sizeof(struct xcb), 1);
this->shmID = -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;
}
static bool xcb_init()
{
assert(this);
assert(!this->initialized);
os_resetEvent(this->frameEvent);
this->xcb = xcb_connect(NULL, NULL);
if (!this->xcb || xcb_connection_has_error(this->xcb))
{
DEBUG_ERROR("Unable to open the X display");
goto fail;
}
if (!xcb_get_extension_data(this->xcb, &xcb_shm_id)->present)
{
DEBUG_ERROR("Missing the SHM extension");
goto fail;
}
xcb_screen_iterator_t iter;
iter = xcb_setup_roots_iterator(xcb_get_setup(this->xcb));
this->xcbScreen = iter.data;
this->width = iter.data->width_in_pixels;
this->height = iter.data->height_in_pixels;
DEBUG_INFO("Frame Size : %u x %u", this->width, this->height);
this->seg = xcb_generate_id(this->xcb);
this->shmID = shmget(IPC_PRIVATE, xcb_getMaxFrameSize(), IPC_CREAT | 0777);
if (this->shmID == -1)
{
DEBUG_ERROR("shmget failed");
goto fail;
}
xcb_shm_attach(this->xcb, this->seg ,this->shmID, false);
this->data = shmat(this->shmID, NULL, 0);
if ((uintptr_t)this->data == -1)
{
DEBUG_ERROR("shmat failed");
goto fail;
}
DEBUG_INFO("Frame Data : 0x%" PRIXPTR, (uintptr_t)this->data);
this->initialized = true;
return true;
fail:
xcb_deinit();
return false;
}
static bool xcb_deinit()
{
assert(this);
if ((uintptr_t)this->data != -1)
{
shmdt(this->data);
this->data = (void *)-1;
}
if (this->shmID != -1)
{
shmctl(this->shmID, IPC_RMID, NULL);
this->shmID = -1;
}
if (this->xcb)
{
xcb_disconnect(this->xcb);
this->xcb = NULL;
}
this->initialized = false;
return false;
}
static void xcb_free()
{
os_freeEvent(this->frameEvent);
free(this);
this = NULL;
}
static unsigned int xcb_getMaxFrameSize()
{
return this->width * this->height * 4;
}
static CaptureResult xcb_capture()
{
assert(this);
assert(this->initialized);
if (!this->hasFrame)
{
this->imgC = xcb_shm_get_image_unchecked(
this->xcb,
this->xcbScreen->root,
0, 0,
this->width,
this->height,
~0,
XCB_IMAGE_FORMAT_Z_PIXMAP,
this->seg,
0);
this->hasFrame = true;
os_signalEvent(this->frameEvent);
}
return CAPTURE_RESULT_OK;
}
static bool xcb_getFrame(CaptureFrame * frame)
{
assert(this);
assert(this->initialized);
assert(frame);
assert(frame->data);
os_waitEvent(this->frameEvent, TIMEOUT_INFINITE);
xcb_shm_get_image_reply_t * img;
img = xcb_shm_get_image_reply(this->xcb, this->imgC, NULL);
if (!img)
{
DEBUG_ERROR("Failed to get image reply");
return false;
}
frame->width = this->width;
frame->height = this->height;
frame->pitch = this->width * 4;
frame->stride = this->width;
frame->format = CAPTURE_FMT_BGRA;
memcpy(frame->data, this->data, this->width * this->height * 4);
free(img);
this->hasFrame = false;
return true;
}
static bool xcb_getPointer(CapturePointer * pointer)
{
return false;
}
struct CaptureInterface Capture_XCB =
{
.getName = xcb_getName,
.create = xcb_create,
.init = xcb_init,
.deinit = xcb_deinit,
.free = xcb_free,
.getMaxFrameSize = xcb_getMaxFrameSize,
.capture = xcb_capture,
.getFrame = xcb_getFrame,
.getPointer = xcb_getPointer
};

View file

@ -0,0 +1,390 @@
/*
Looking Glass - KVM FrameRelay (KVMFR) Client
Copyright (C) 2017-2019 Geoffrey McRae <geoff@hostfission.com>
https://looking-glass.hostfission.com
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 2 of the License, or (at your option) 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, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "app.h"
#include "debug.h"
#include <assert.h>
#include <getopt.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <errno.h>
struct app
{
unsigned int shmSize;
int shmFD;
void * shmMap;
};
struct params
{
const char * shmDevice;
};
static struct app app;
static struct params params =
{
.shmDevice = "uio0"
};
struct osThreadHandle
{
const char * name;
osThreadFunction function;
void * opaque;
pthread_t handle;
int resultCode;
};
void sigHandler(int signo)
{
DEBUG_INFO("SIGINT");
app_quit();
}
int main(int argc, char * argv[])
{
static struct option longOptions[] =
{
{"shmDevice", required_argument, 0, 'f'},
{0, 0, 0, 0}
};
int optionIndex = 0;
while(true)
{
int c = getopt_long(argc, argv, "f:", longOptions, &optionIndex);
if (c == -1)
break;
switch(c)
{
case 'f':
params.shmDevice = optarg;
break;
}
}
// check the deice name
{
char file[100] = "/sys/class/uio/";
strncat(file, params.shmDevice, sizeof(file) - 1);
strncat(file, "/name" , sizeof(file) - 1);
int fd = open(file, O_RDONLY);
if (fd < 0)
{
DEBUG_ERROR("Failed to open: %s", file);
DEBUG_ERROR("Did you remmeber to modprobe the kvmfr module?");
return -1;
}
char name[32];
int len = read(fd, name, sizeof(name) - 1);
if (len <= 0)
{
DEBUG_ERROR("Failed to read: %s", file);
close(fd);
return -1;
}
name[len] = '\0';
close(fd);
while(len > 0 && name[len-1] == '\n')
{
--len;
name[len] = '\0';
}
if (strcmp(name, "KVMFR") != 0)
{
DEBUG_ERROR("Device is not a KVMFR device \"%s\" reports as: %s", file, name);
return -1;
}
}
// get the device size
{
char file[100] = "/sys/class/uio/";
strncat(file, params.shmDevice , sizeof(file) - 1);
strncat(file, "/maps/map0/size", sizeof(file) - 1);
int fd = open(file, O_RDONLY);
if (fd < 0)
{
DEBUG_ERROR("Failed to open: %s", file);
return -1;
}
char size[32];
int len = read(fd, size, sizeof(size) - 1);
if (len <= 0)
{
DEBUG_ERROR("Failed to read: %s", file);
close(fd);
return -1;
}
size[len] = '\0';
close(fd);
app.shmSize = strtoul(size, NULL, 16);
}
// open the device
{
char file[100] = "/dev/";
strncat(file, params.shmDevice, sizeof(file) - 1);
app.shmFD = open(file, O_RDWR, (mode_t)0600);
app.shmMap = MAP_FAILED;
if (app.shmFD < 0)
{
DEBUG_ERROR("Failed to open: %s", file);
return -1;
}
DEBUG_INFO("KVMFR Device : %s", file);
}
signal(SIGINT, sigHandler);
int result = app_main();
os_shmemUnmap();
close(app.shmFD);
return result;
}
unsigned int os_shmemSize()
{
return app.shmSize;
}
bool os_shmemMmap(void **ptr)
{
if (app.shmMap == MAP_FAILED)
{
app.shmMap = mmap(0, app.shmSize, PROT_READ | PROT_WRITE, MAP_SHARED, app.shmFD, 0);
if (app.shmMap == MAP_FAILED)
{
DEBUG_ERROR("Failed to map the shared memory device: %s", params.shmDevice);
return false;
}
}
*ptr = app.shmMap;
return true;
}
void os_shmemUnmap()
{
if (app.shmMap == MAP_FAILED)
return;
munmap(app.shmMap, app.shmSize);
app.shmMap = MAP_FAILED;
}
static void * threadWrapper(void * opaque)
{
osThreadHandle * handle = (osThreadHandle *)opaque;
handle->resultCode = handle->function(handle->opaque);
return NULL;
}
bool os_createThread(const char * name, osThreadFunction function, void * opaque, osThreadHandle ** handle)
{
*handle = (osThreadHandle*)malloc(sizeof(osThreadHandle));
(*handle)->name = name;
(*handle)->function = function;
(*handle)->opaque = opaque;
if (pthread_create(&(*handle)->handle, NULL, threadWrapper, *handle) != 0)
{
DEBUG_ERROR("pthread_create failed for thread: %s", name);
free(*handle);
*handle = NULL;
return false;
}
return true;
}
bool os_joinThread(osThreadHandle * handle, int * resultCode)
{
if (pthread_join(handle->handle, NULL) != 0)
{
DEBUG_ERROR("pthread_join failed for thread: %s", handle->name);
free(handle);
return false;
}
if (resultCode)
*resultCode = handle->resultCode;
free(handle);
return true;
}
struct osEventHandle
{
pthread_mutex_t mutex;
pthread_cond_t cond;
bool flag;
bool autoReset;
};
osEventHandle * os_createEvent(bool autoReset)
{
osEventHandle * handle = (osEventHandle *)calloc(sizeof(osEventHandle), 1);
if (!handle)
{
DEBUG_ERROR("Failed to allocate memory");
return NULL;
}
if (pthread_mutex_init(&handle->mutex, NULL) != 0)
{
DEBUG_ERROR("Failed to create the mutex");
free(handle);
return NULL;
}
if (pthread_cond_init(&handle->cond, NULL) != 0)
{
pthread_mutex_destroy(&handle->mutex);
free(handle);
return NULL;
}
handle->autoReset = autoReset;
return handle;
}
void os_freeEvent(osEventHandle * handle)
{
assert(handle);
pthread_cond_destroy (&handle->cond );
pthread_mutex_destroy(&handle->mutex);
free(handle);
}
bool os_waitEvent(osEventHandle * handle, unsigned int timeout)
{
assert(handle);
if (pthread_mutex_lock(&handle->mutex) != 0)
{
DEBUG_ERROR("Failed to lock the mutex");
return false;
}
while(!handle->flag)
{
if (timeout == TIMEOUT_INFINITE)
{
if (pthread_cond_wait(&handle->cond, &handle->mutex) != 0)
{
DEBUG_ERROR("Wait to wait on the condition");
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;
if (pthread_mutex_unlock(&handle->mutex) != 0)
{
DEBUG_ERROR("Failed to unlock the mutex");
return false;
}
return true;
}
bool os_signalEvent(osEventHandle * handle)
{
assert(handle);
if (pthread_mutex_lock(&handle->mutex) != 0)
{
DEBUG_ERROR("Failed to lock the mutex");
return false;
}
handle->flag = true;
if (pthread_mutex_unlock(&handle->mutex) != 0)
{
DEBUG_ERROR("Failed to unlock the mutex");
return false;
}
if (pthread_cond_signal(&handle->cond) != 0)
{
DEBUG_ERROR("Failed to signal the condition");
return false;
}
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;
}