@@ -8,7 +8,8 @@ man_MANS = aplay.1 arecord.1
noinst_HEADERS = \
formats.h \
container.h \
- aligner.h
+ aligner.h \
+ waiter.h
aplay_SOURCES = \
formats.h \
@@ -21,7 +22,9 @@ aplay_SOURCES = \
aligner.h \
aligner.c \
aligner-single.c \
- aligner-multiple.c
+ aligner-multiple.c \
+ waiter.h \
+ waiter.c
EXTRA_DIST = aplay.1 arecord.1
EXTRA_CLEAN = arecord
new file mode 100644
@@ -0,0 +1,60 @@
+/*
+ * waiter.c - I/O event waiter.
+ *
+ * Copyright (c) 2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
+ *
+ * Licensed under the terms of the GNU General Public License, version 2.
+ */
+
+#include "waiter.h"
+
+int waiter_context_init(struct waiter_context *waiter, enum waiter_type type)
+{
+ struct {
+ enum waiter_type type;
+ const struct waiter_data *waiter;
+ } entries[] = {
+ {WAITER_TYPE_COUNT, NULL},
+ };
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(entries); ++i) {
+ if (entries[i].type == type)
+ break;
+ }
+ if (i == ARRAY_SIZE(entries))
+ return -EINVAL;
+
+ waiter->private_data = malloc(entries[i].waiter->private_size);
+ if (waiter->private_data == NULL)
+ return -ENOMEM;
+ memset(waiter->private_data, 0, entries[i].waiter->private_size);
+
+ waiter->type = type;
+ waiter->ops = &entries[i].waiter->ops;
+
+ return 0;
+}
+
+int waiter_context_prepare(struct waiter_context *waiter, int *fds,
+ unsigned int fd_count)
+{
+ return waiter->ops->prepare(waiter, fds, fd_count);
+}
+
+int waiter_context_wait_event(struct waiter_context *waiter)
+{
+ return waiter->ops->wait_event(waiter);
+}
+
+void waiter_context_release(struct waiter_context *waiter)
+{
+ waiter->ops->release(waiter);
+}
+
+void waiter_context_destroy(struct waiter_context *waiter)
+{
+ if (waiter->private_data)
+ free(waiter->private_data);
+ waiter->private_data = NULL;
+}
new file mode 100644
@@ -0,0 +1,54 @@
+/*
+ * waiter.h - a header for I/O event waiter.
+ *
+ * Copyright (c) 2017 Takashi Sakamoto <o-takashi@sakamocchi.jp>
+ *
+ * Licensed under the terms of the GNU General Public License, version 2.
+ */
+
+#ifndef __ALSA_UTILS_APLAY_WAITER__H_
+#define __ALSA_UTILS_APLAY_WAITER__H_
+
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
+#endif
+
+enum waiter_type {
+ WAITER_TYPE_COUNT,
+};
+
+struct waiter_ops;
+
+struct waiter_context {
+ enum waiter_type type;
+ const struct waiter_ops *ops;
+ void *private_data;
+};
+
+int waiter_context_init(struct waiter_context *waiter, enum waiter_type type);
+int waiter_context_prepare(struct waiter_context *waiter, int *fds,
+ unsigned int fd_count);
+int waiter_context_wait_event(struct waiter_context *waiter);
+void waiter_context_release(struct waiter_context *waiter);
+void waiter_context_destroy(struct waiter_context *waiter);
+
+/* For internal use in 'waiter' module. */
+
+struct waiter_ops {
+ int (*prepare)(struct waiter_context *waiter, int *fds,
+ unsigned int fd_count);
+ int (*wait_event)(struct waiter_context *waiter);
+ void (*release)(struct waiter_context *waiter);
+};
+
+struct waiter_data {
+ struct waiter_ops ops;
+ unsigned int private_size;
+};
+
+#endif