From b7141a3e8b604734cf7a645250e01953c896d4a7 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Sun, 6 Dec 2015 12:57:15 -0800 Subject: [PATCH] Add check for joystick when initializing library Fix C++ inclusion --- libx52/src/libx52.h | 4 ++-- libx52/src/x52_core.c | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/libx52/src/libx52.h b/libx52/src/libx52.h index a5bf037..53dc779 100644 --- a/libx52/src/libx52.h +++ b/libx52/src/libx52.h @@ -15,7 +15,7 @@ #include #include -#ifndef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -313,7 +313,7 @@ int libx52_update(libx52_device *x52); */ int libx52_vendor_command(libx52_device *x52, uint16_t index, uint16_t value); -#ifndef __cplusplus +#ifdef __cplusplus } #endif diff --git a/libx52/src/x52_core.c b/libx52/src/x52_core.c index 0be544f..2b334fe 100644 --- a/libx52/src/x52_core.c +++ b/libx52/src/x52_core.c @@ -47,7 +47,7 @@ libx52_device* libx52_init(void) libx52_device *x52_dev; /* Allocate memory for the library's data structures */ - x52_dev = malloc(sizeof(libx52_device)); + x52_dev = calloc(1, sizeof(libx52_device)); if (!x52_dev) { errno = ENOMEM; return NULL; @@ -77,6 +77,11 @@ libx52_device* libx52_init(void) } libusb_free_device_list(list, 1); + /* Make sure we actually have an X52 device detected */ + if (!x52_dev->hdl) { + goto err_recovery; + } + return x52_dev; err_recovery: free(x52_dev);