Skip to content

Commit

Permalink
Use preprocessor macros instead of enums for event flag constants
Browse files Browse the repository at this point in the history
Signed-off-by: Tormod Volden <[email protected]>
  • Loading branch information
tormodvolden committed May 12, 2024
1 parent bff2f08 commit 213ffc2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion libusb/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,7 @@ void API_EXPORTED libusb_close(libusb_device_handle *dev_handle)
* Clear the event pipe if there are no further pending events. */
usbi_mutex_lock(&ctx->event_data_lock);
if (!--ctx->device_close)
ctx->event_flags &= ~(unsigned int)USBI_EVENT_DEVICE_CLOSE;
ctx->event_flags &= ~USBI_EVENT_DEVICE_CLOSE;
if (!ctx->event_flags)
usbi_clear_event(&ctx->event);
usbi_mutex_unlock(&ctx->event_data_lock);
Expand Down
20 changes: 10 additions & 10 deletions libusb/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -2117,35 +2117,35 @@ static int handle_event_trigger(struct libusb_context *ctx)
usbi_mutex_lock(&ctx->event_data_lock);

/* check if someone modified the event sources */
if (ctx->event_flags & (unsigned int)USBI_EVENT_EVENT_SOURCES_MODIFIED)
if (ctx->event_flags & USBI_EVENT_EVENT_SOURCES_MODIFIED)
usbi_dbg(ctx, "someone updated the event sources");

if (ctx->event_flags & (unsigned int)USBI_EVENT_USER_INTERRUPT) {
if (ctx->event_flags & USBI_EVENT_USER_INTERRUPT) {
usbi_dbg(ctx, "someone purposefully interrupted");
ctx->event_flags &= ~(unsigned int)USBI_EVENT_USER_INTERRUPT;
ctx->event_flags &= ~USBI_EVENT_USER_INTERRUPT;
}

if (ctx->event_flags & (unsigned int)USBI_EVENT_HOTPLUG_CB_DEREGISTERED) {
if (ctx->event_flags & USBI_EVENT_HOTPLUG_CB_DEREGISTERED) {
usbi_dbg(ctx, "someone unregistered a hotplug cb");
ctx->event_flags &= ~(unsigned int)USBI_EVENT_HOTPLUG_CB_DEREGISTERED;
ctx->event_flags &= ~USBI_EVENT_HOTPLUG_CB_DEREGISTERED;
hotplug_event = 1;
}

/* check if someone is closing a device */
if (ctx->event_flags & (unsigned int)USBI_EVENT_DEVICE_CLOSE)
if (ctx->event_flags & USBI_EVENT_DEVICE_CLOSE)
usbi_dbg(ctx, "someone is closing a device");

/* check for any pending hotplug messages */
if (ctx->event_flags & USBI_EVENT_HOTPLUG_MSG_PENDING) {
usbi_dbg(ctx, "hotplug message received");
ctx->event_flags &= ~(unsigned int)USBI_EVENT_HOTPLUG_MSG_PENDING;
ctx->event_flags &= ~USBI_EVENT_HOTPLUG_MSG_PENDING;
hotplug_event = 1;
assert(!list_empty(&ctx->hotplug_msgs));
list_cut(&hotplug_msgs, &ctx->hotplug_msgs);
}

/* complete any pending transfers */
if (ctx->event_flags & (unsigned int)USBI_EVENT_TRANSFER_COMPLETED) {
if (ctx->event_flags & USBI_EVENT_TRANSFER_COMPLETED) {
struct usbi_transfer *itransfer, *tmp;
struct list_head completed_transfers;

Expand All @@ -2167,7 +2167,7 @@ static int handle_event_trigger(struct libusb_context *ctx)
/* an error occurred, put the remaining transfers back on the list */
list_splice_front(&completed_transfers, &ctx->completed_transfers);
} else if (list_empty(&ctx->completed_transfers)) {
ctx->event_flags &= ~(unsigned int)USBI_EVENT_TRANSFER_COMPLETED;
ctx->event_flags &= ~USBI_EVENT_TRANSFER_COMPLETED;
}
}

Expand Down Expand Up @@ -2232,7 +2232,7 @@ static int handle_events(struct libusb_context *ctx, struct timeval *tv)
}

/* reset the flag now that we have the updated list */
ctx->event_flags &= ~(unsigned int)USBI_EVENT_EVENT_SOURCES_MODIFIED;
ctx->event_flags &= ~USBI_EVENT_EVENT_SOURCES_MODIFIED;

/* if no further pending events, clear the event so that we do
* not immediately return from the wait function */
Expand Down
27 changes: 13 additions & 14 deletions libusb/libusbi.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,25 +471,24 @@ static inline struct libusb_context *usbi_get_context(struct libusb_context *ctx
return ctx;
}

enum usbi_event_flags {
/* The list of event sources has been modified */
USBI_EVENT_EVENT_SOURCES_MODIFIED = 1U << 0,
/* event flags of the ctx structure */
/* The list of event sources has been modified */
#define USBI_EVENT_EVENT_SOURCES_MODIFIED (1U << 0)

/* The user has interrupted the event handler */
USBI_EVENT_USER_INTERRUPT = 1U << 1,
/* The user has interrupted the event handler */
#define USBI_EVENT_USER_INTERRUPT (1U << 1)

/* A hotplug callback deregistration is pending */
USBI_EVENT_HOTPLUG_CB_DEREGISTERED = 1U << 2,
/* A hotplug callback deregistration is pending */
#define USBI_EVENT_HOTPLUG_CB_DEREGISTERED (1U << 2)

/* One or more hotplug messages are pending */
USBI_EVENT_HOTPLUG_MSG_PENDING = 1U << 3,
/* One or more hotplug messages are pending */
#define USBI_EVENT_HOTPLUG_MSG_PENDING (1U << 3)

/* One or more completed transfers are pending */
USBI_EVENT_TRANSFER_COMPLETED = 1U << 4,
/* One or more completed transfers are pending */
#define USBI_EVENT_TRANSFER_COMPLETED (1U << 4)

/* A device is in the process of being closed */
USBI_EVENT_DEVICE_CLOSE = 1U << 5,
};
/* A device is in the process of being closed */
#define USBI_EVENT_DEVICE_CLOSE (1U << 5)

/* Macros for managing event handling state */
static inline int usbi_handling_events(struct libusb_context *ctx)
Expand Down

0 comments on commit 213ffc2

Please sign in to comment.