Added basic support for the new event notification code.

This commit is contained in:
Jef Driesen 2009-02-20 12:11:11 +00:00
parent eb9efb47e1
commit 7c3f4e864f
4 changed files with 73 additions and 3 deletions

View File

@ -22,11 +22,15 @@
#ifndef DEVICE_PRIVATE_H
#define DEVICE_PRIVATE_H
#include <limits.h>
#include "device.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#include "device.h"
#define DEVICE_PROGRESS_INITIALIZER {0, UINT_MAX}
struct device_t;
struct device_backend_t;
@ -38,6 +42,10 @@ struct device_t {
// Progress callback data.
progress_callback_t progress;
void *userdata;
// Event notifications.
unsigned int event_mask;
device_event_callback_t event_callback;
void *event_userdata;
};
struct device_backend_t {
@ -78,6 +86,9 @@ progress_set_maximum (device_progress_state_t *progress, unsigned int value);
void
device_init (device_t *device, const device_backend_t *backend);
void
device_event_emit (device_t *device, device_event_t event, const void *data);
#ifdef __cplusplus
}
#endif /* __cplusplus */

View File

@ -31,6 +31,10 @@ device_init (device_t *device, const device_backend_t *backend)
device->backend = backend;
device->progress = NULL;
device->userdata = NULL;
device->event_mask = 0;
device->event_callback = NULL;
device->event_userdata = NULL;
}
@ -57,6 +61,20 @@ device_set_progress (device_t *device, progress_callback_t callback, void *userd
}
device_status_t
device_set_events (device_t *device, unsigned int events, device_event_callback_t callback, void *userdata)
{
if (device == NULL)
return DEVICE_STATUS_UNSUPPORTED;
device->event_mask = events;
device->event_callback = callback;
device->event_userdata = userdata;
return DEVICE_STATUS_SUCCESS;
}
device_status_t
device_handshake (device_t *device, unsigned char data[], unsigned int size)
{
@ -196,3 +214,34 @@ progress_set_maximum (device_progress_state_t *progress, unsigned int value)
progress->maximum = value;
}
void
device_event_emit (device_t *device, device_event_t event, const void *data)
{
device_progress_t *progress = (device_progress_t *) data;
// Check the event data for errors.
switch (event) {
case DEVICE_EVENT_WAITING:
assert (data == NULL);
break;
case DEVICE_EVENT_PROGRESS:
assert (progress != NULL);
assert (progress->maximum != 0);
assert (progress->maximum >= progress->current);
break;
default:
break;
}
// Check if there is a callback function registered.
if (device == NULL || device->event_callback == NULL)
return;
// Check the event mask.
if ((event & device->event_mask) == 0)
return;
device->event_callback (device, event, data, device->event_userdata);
}

View File

@ -57,12 +57,19 @@ typedef enum device_status_t {
} device_status_t;
typedef enum device_event_t {
DEVICE_EVENT_WAITING,
DEVICE_EVENT_PROGRESS
DEVICE_EVENT_WAITING = (1 << 0),
DEVICE_EVENT_PROGRESS = (1 << 1)
} device_event_t;
typedef struct device_t device_t;
typedef struct device_progress_t {
unsigned int current;
unsigned int maximum;
} device_progress_t;
typedef void (*device_event_callback_t) (device_t *device, device_event_t event, const void *data, void *userdata);
typedef int (*dive_callback_t) (const unsigned char *data, unsigned int size, void *userdata);
typedef void (*progress_callback_t) (device_event_t event, unsigned int current, unsigned int maximum, void *userdata);
@ -70,6 +77,8 @@ device_type_t device_get_type (device_t *device);
device_status_t device_set_progress (device_t *device, progress_callback_t callback, void *userdata);
device_status_t device_set_events (device_t *device, unsigned int events, device_event_callback_t callback, void *userdata);
device_status_t device_handshake (device_t *device, unsigned char data[], unsigned int size);
device_status_t device_version (device_t *device, unsigned char data[], unsigned int size);

View File

@ -24,6 +24,7 @@ device_get_type
device_handshake
device_read
device_set_progress
device_set_events
device_version
device_write