Skip to content

Commit

Permalink
Refactored coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
jgeboski committed Dec 20, 2014
1 parent b7843a9 commit a9f8e7a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 89 deletions.
156 changes: 75 additions & 81 deletions libg19/libg19.c
Expand Up @@ -27,27 +27,27 @@
#include "libg19.h"
#include "hdata.h"

static libusb_context * usb_ctx = NULL;
static libusb_device_handle * dhandle = NULL;
static libusb_context *usb_ctx = NULL;
static libusb_device_handle *dhandle = NULL;

static ssize_t devc;
static libusb_device ** dlist;
static ssize_t devc;
static libusb_device **dlist;

static uint8_t quit;
static uint8_t quit;
static pthread_t usb_et;

static struct libusb_transfer * gkeys_transfer = NULL;
static struct libusb_transfer * gkeysc_transfer = NULL;
static struct libusb_transfer * lkeys_transfer = NULL;
static struct libusb_transfer *gkeys_transfer = NULL;
static struct libusb_transfer *gkeysc_transfer = NULL;
static struct libusb_transfer *lkeys_transfer = NULL;

static G19GKeysFunc gkeys_func = NULL;
static G19LKeysFunc lkeys_func = NULL;

static void usb_event_thread(void * data)
static void usb_event_thread(void *data)
{
struct timeval tv;

while(!quit) {
while (!quit) {
tv.tv_sec = 1;
tv.tv_usec = 0;

Expand All @@ -59,38 +59,41 @@ static void usb_event_thread(void * data)

static int g19_device_proc()
{
struct libusb_device_descriptor devd;
struct libusb_config_descriptor * cfgd;
struct libusb_device_descriptor devd;
struct libusb_config_descriptor *cfgd;

const struct libusb_interface * intf;
const struct libusb_interface_descriptor * intfd;
const struct libusb_interface *intf;
const struct libusb_interface_descriptor *intfd;

int m, c, i, d;
int m;
int c;
int i;
int d;
int fail;

/* This needs cleaning up; all these nested statements are ugly */

for(m = 0, fail = 1; m < devc; m++) {
if(libusb_get_device_descriptor(dlist[m], &devd))
for (m = 0, fail = 1; m < devc; m++) {
if (libusb_get_device_descriptor(dlist[m], &devd))
continue;

if((devd.idVendor != G19_VENDOR_ID) || (devd.idProduct != G19_PRODUCT_ID))
if ((devd.idVendor != G19_VENDOR_ID) || (devd.idProduct != G19_PRODUCT_ID))
continue;

if(libusb_open(dlist[m], &dhandle))
if (libusb_open(dlist[m], &dhandle))
continue;

for(c = 0; c < devd.bNumConfigurations; c++) {
if(libusb_get_config_descriptor(dlist[m], c, &cfgd))
for (c = 0; c < devd.bNumConfigurations; c++) {
if (libusb_get_config_descriptor(dlist[m], c, &cfgd))
continue;

for(i = 0; i < cfgd->bNumInterfaces; i++) {
for (i = 0; i < cfgd->bNumInterfaces; i++) {
intf = &cfgd->interface[i];

for(d = 0; d < intf->num_altsetting; d++) {
for (d = 0; d < intf->num_altsetting; d++) {
intfd = &intf->altsetting[d];

if(libusb_kernel_driver_active(dhandle, intfd->bInterfaceNumber))
if (libusb_kernel_driver_active(dhandle, intfd->bInterfaceNumber))
libusb_detach_kernel_driver(
dhandle, intfd->bInterfaceNumber);

Expand All @@ -100,15 +103,15 @@ static int g19_device_proc()
fail = libusb_claim_interface(
dhandle, intfd->bInterfaceNumber);

if(fail)
if (fail)
fail = 0;
}
}

libusb_free_config_descriptor(cfgd);
}

if(fail != 0)
if (fail != 0)
return LIBUSB_SUCCESS;

libusb_close(dhandle);
Expand All @@ -130,28 +133,27 @@ int g19_init(int level)
{
int res;

if(usb_ctx != NULL)
if (usb_ctx != NULL)
return LIBUSB_ERROR_BUSY;

res = libusb_init(&usb_ctx);
libusb_set_debug(usb_ctx, level);

if(res != 0)
if (res != 0)
return res;

devc = libusb_get_device_list(usb_ctx, &dlist);
if(devc < 1)
if (devc < 1)
return LIBUSB_ERROR_NO_DEVICE;

res = g19_device_proc();
if(res != 0) {
if (res != 0) {
g19_deinit();
return res;
}

quit = 0;
pthread_create(&usb_et, NULL, (void *) usb_event_thread, NULL);

return 0;
}

Expand All @@ -160,33 +162,33 @@ int g19_init(int level)
**/
void g19_deinit(void)
{
if(dhandle != NULL) {
if (dhandle != NULL) {
libusb_release_interface(dhandle, 0);
libusb_reset_device(dhandle);
libusb_close(dhandle);
}

quit = 1;

if(gkeysc_transfer != NULL)
if (gkeysc_transfer != NULL)
libusb_free_transfer(gkeysc_transfer);

if(gkeys_transfer != NULL)
if (gkeys_transfer != NULL)
libusb_free_transfer(gkeys_transfer);

if(lkeys_transfer != NULL)
if (lkeys_transfer != NULL)
libusb_free_transfer(lkeys_transfer);

if(dlist != NULL)
if (dlist != NULL)
libusb_free_device_list(dlist, 1);

if(usb_ctx != NULL)
if (usb_ctx != NULL)
libusb_exit(usb_ctx);

pthread_join(usb_et, NULL);
}

static void g19_gkey_cb(struct libusb_transfer * transfer)
static void g19_gkey_cb(struct libusb_transfer *transfer)
{
uint32_t keys;

Expand All @@ -201,7 +203,7 @@ static void g19_gkey_cb(struct libusb_transfer * transfer)
libusb_submit_transfer(gkeys_transfer);
}

static void g19_lkey_cb(struct libusb_transfer * transfer)
static void g19_lkey_cb(struct libusb_transfer *transfer)
{
uint16_t keys;

Expand All @@ -222,26 +224,23 @@ void g19_set_gkeys_cb(G19GKeysFunc func)
uint8_t data[4];
uint8_t cdata[7];

if(dhandle == NULL)
if (dhandle == NULL)
return;

if(gkeys_transfer != NULL)
if (gkeys_transfer != NULL)
libusb_free_transfer(gkeys_transfer);

if(gkeysc_transfer != NULL)
if (gkeysc_transfer != NULL)
libusb_free_transfer(gkeysc_transfer);

gkeys_func = func;

gkeys_func = func;
gkeys_transfer = libusb_alloc_transfer(0);
gkeysc_transfer = libusb_alloc_transfer(0);

libusb_fill_interrupt_transfer(gkeys_transfer, dhandle,
0x83, data, 4, g19_gkey_cb, NULL, 0);

libusb_fill_interrupt_transfer(gkeysc_transfer, dhandle,
0x83, cdata, 7, NULL, NULL, 7);

libusb_fill_interrupt_transfer(gkeys_transfer, dhandle, 0x83, data, 4,
g19_gkey_cb, NULL, 0);
libusb_fill_interrupt_transfer(gkeysc_transfer, dhandle, 0x83, cdata, 7,
NULL, NULL, 7);
libusb_submit_transfer(gkeys_transfer);
}

Expand All @@ -255,19 +254,17 @@ void g19_set_lkeys_cb(G19LKeysFunc func)
{
uint8_t data[2];

if(dhandle == NULL)
if (dhandle == NULL)
return;

if(lkeys_transfer != NULL)
if (lkeys_transfer != NULL)
libusb_free_transfer(lkeys_transfer);

lkeys_func = func;

lkeys_func = func;
lkeys_transfer = libusb_alloc_transfer(0);

libusb_fill_interrupt_transfer(lkeys_transfer, dhandle,
0x81, data, 2, g19_lkey_cb, NULL, 0);

libusb_fill_interrupt_transfer(lkeys_transfer, dhandle, 0x81, data, 2,
g19_lkey_cb, NULL, 0);
libusb_submit_transfer(lkeys_transfer);
}

Expand All @@ -278,14 +275,15 @@ void g19_set_lkeys_cb(G19LKeysFunc func)
* @size size in bytes of @data
* @type the G19UpdateType to be used
**/
void g19_update_lcd(uint8_t * data, size_t size, G19UpdateType type)
void g19_update_lcd(uint8_t *data, size_t size, G19UpdateType type)
{
struct libusb_transfer * transfer;
uint8_t * bits;
uint16_t color;
int i, d;
struct libusb_transfer *transfer;
uint8_t *bits;
uint16_t color;
int i;
int d;

if((dhandle == NULL) || (size < 1))
if ((dhandle == NULL) || (size < 1))
return;

transfer = libusb_alloc_transfer(0);
Expand All @@ -296,7 +294,7 @@ void g19_update_lcd(uint8_t * data, size_t size, G19UpdateType type)
memset(bits, 0x00, G19_BMP_SIZE);
memcpy(bits, hdata, HDATA_SIZE);

if(!(type & G19_UPDATE_TYPE_RAW)) {
if (!(type & G19_UPDATE_TYPE_RAW)) {
i = HDATA_SIZE;
d = 0;

Expand All @@ -311,15 +309,14 @@ void g19_update_lcd(uint8_t * data, size_t size, G19UpdateType type)
memcpy(bits + i, &color, 2);
}
} else {
if(size > G19_BMP_DSIZE)
if (size > G19_BMP_DSIZE)
size = G19_BMP_DSIZE;

memcpy(bits + HDATA_SIZE, data, size);
}

libusb_fill_bulk_transfer(transfer, dhandle, 0x02,
bits, G19_BMP_SIZE, NULL, NULL, 0);

libusb_fill_bulk_transfer(transfer, dhandle, 0x02, bits, G19_BMP_SIZE,
NULL, NULL, 0);
libusb_submit_transfer(transfer);
}

Expand All @@ -333,10 +330,10 @@ void g19_update_lcd(uint8_t * data, size_t size, G19UpdateType type)
**/
int g19_set_brightness(uint8_t level)
{
struct libusb_transfer * transfer;
struct libusb_transfer *transfer;
uint8_t data[9];

if(dhandle == NULL)
if (dhandle == NULL)
return LIBUSB_ERROR_NO_DEVICE;

transfer = libusb_alloc_transfer(0);
Expand All @@ -346,7 +343,6 @@ int g19_set_brightness(uint8_t level)

libusb_fill_control_setup(data, 0x41, 10, 0, 0, 1);
libusb_fill_control_transfer(transfer, dhandle, data, NULL, NULL, 0);

return libusb_submit_transfer(transfer);
}

Expand All @@ -362,10 +358,10 @@ int g19_set_brightness(uint8_t level)
**/
int g19_set_backlight(uint8_t r, uint8_t g, uint8_t b)
{
struct libusb_transfer * transfer;
struct libusb_transfer *transfer;
uint8_t data[12];

if(dhandle == NULL)
if (dhandle == NULL)
return LIBUSB_ERROR_NO_DEVICE;

transfer = libusb_alloc_transfer(0);
Expand All @@ -378,7 +374,6 @@ int g19_set_backlight(uint8_t r, uint8_t g, uint8_t b)

libusb_fill_control_setup(data, 0x21, 9, 0x307, 1, 4);
libusb_fill_control_transfer(transfer, dhandle, data, NULL, NULL, 0);

return libusb_submit_transfer(transfer);
}

Expand All @@ -396,10 +391,10 @@ int g19_set_backlight(uint8_t r, uint8_t g, uint8_t b)
**/
int g19_set_mkey_led(uint32_t keys)
{
struct libusb_transfer * transfer;
struct libusb_transfer *transfer;
uint8_t data[10];

if(dhandle == NULL)
if (dhandle == NULL)
return LIBUSB_ERROR_NO_DEVICE;

transfer = libusb_alloc_transfer(0);
Expand All @@ -408,20 +403,19 @@ int g19_set_mkey_led(uint32_t keys)
data[8] = 0x10;
data[9] = 0x00;

if(keys & G19_KEY_M1)
if (keys & G19_KEY_M1)
data[9] |= 0x10 << 3;

if(keys & G19_KEY_M2)
if (keys & G19_KEY_M2)
data[9] |= 0x10 << 2;

if(keys & G19_KEY_M3)
if (keys & G19_KEY_M3)
data[9] |= 0x10 << 1;

if(keys & G19_KEY_MR)
if (keys & G19_KEY_MR)
data[9] |= 0x10 << 0;

libusb_fill_control_setup(data, 0x21, 9, 0x305, 1, 2);
libusb_fill_control_transfer(transfer, dhandle, data, NULL, NULL, 0);

return libusb_submit_transfer(transfer);
}

0 comments on commit a9f8e7a

Please sign in to comment.