4 #include <ucw/bitops.h>
5 #include <ucw/clists.h>
7 #include <ucw/mainloop.h>
8 #include <ucw/string.h>
17 #include <pulse/pulseaudio.h>
22 * Interface to Novation Nocturn
24 * Protocol reverse-engineered by De Wet van Niekerk <dewert@gmail.com>,
25 * see https://github.com/dewert/nocturn-linux-midi for inspiration.
28 static libusb_context *usb_ctx;
29 static libusb_device_handle *usb_dev;
31 static struct main_file **usb_fds;
33 static int usb_fd_ready(struct main_file *f UNUSED)
35 DBG("USB: Handling events (ready on fd %d)", f->fd);
36 struct timeval tv = { 0, 0 };
38 int err = libusb_handle_events_timeout_completed(usb_ctx, &tv, &comp);
40 msg(L_ERROR, "libusb_handle_events: error %d", err);
44 static void usb_added_fd(int fd, short events, void *user_data UNUSED)
46 if (fd >= (int) GARY_SIZE(usb_fds))
47 GARY_RESIZE(usb_fds, fd + 1);
49 struct main_file *f = usb_fds[fd];
52 f = xmalloc_zero(sizeof(*f));
55 else if (file_is_active(f))
57 DBG("USB: Releasing fd %d", fd);
61 DBG("USB: Adding fd %d with event mask %u", fd, events);
63 f->read_handler = (events & POLLIN) ? usb_fd_ready : NULL;
64 f->write_handler = (events & POLLOUT) ? usb_fd_ready : NULL;
68 static void usb_removed_fd(int fd, void *user_data UNUSED)
70 DBG("USB: Releasing fd %d", fd);
71 ASSERT(fd < (int) GARY_SIZE(usb_fds));
72 struct main_file *f = usb_fds[fd];
74 ASSERT(file_is_active(f));
78 static const char noct_init[4][9] = {
79 { 3, 0xb0, 0x00, 0x00 },
80 { 8, 0x28, 0x00, 0x2b, 0x4a, 0x2c, 0x00, 0x2e, 0x35 },
81 { 6, 0x2a, 0x02, 0x2c, 0x72, 0x2e, 0x30 },
85 static void noct_read_done(struct libusb_transfer *xfer)
87 byte *pkt = xfer->buffer;
88 int len = xfer->actual_length;
89 DBG("USB: Read done: status %d, length %d", xfer->status, len);
91 if (xfer->status != LIBUSB_TRANSFER_COMPLETED)
93 msg(L_ERROR, "USB read failed with status %d, not submitting again", xfer->status);
99 mem_to_hex(buf, pkt, len, ' ');
100 DBG("USB: Read <%s>", buf);
108 msg(L_ERROR, "Unknown USB packet: length %d not divisible by 3", len);
113 msg(L_ERROR, "Unknown USB packet: expected 0xb0 at position %d", i);
122 // Unknown packet sent during init
128 int delta = (arg < 0x40 ? arg : arg - 0x80);
129 DBG("Noct: Rotary %d = %d", r, delta);
136 DBG("Noct: Slider value = %d", arg);
141 // Unknown packet, maybe least significant bit of slider
146 int delta = (arg < 0x40 ? arg : arg - 0x80);
147 DBG("Noct: Center = %d", delta);
152 if (arg == 0x00 || arg == 0x7f)
155 DBG("Noct: Center touch = %d", state);
160 if (arg == 0x00 || arg == 0x7f)
163 DBG("Noct: Slider touch = %d", state);
168 if (arg == 0x00 || arg == 0x7f)
172 DBG("Noct: Rotary %d touch = %d", r, state);
177 if (arg == 0x00 || arg == 0x7f)
181 DBG("Noct: Button %d = %d", b, state);
186 msg(L_ERROR, "Unknown USB packet: unrecognized cmd=%02x arg=%02x", cmd, arg);
190 if ((err = libusb_submit_transfer(xfer)) < 0)
191 die("Cannot submit transfer: error %d", err);
194 static void noct_read_init(void)
196 DBG("Noct: Read init");
198 struct libusb_transfer *xfer = libusb_alloc_transfer(0);
199 libusb_fill_interrupt_transfer(xfer, usb_dev, 0x81, xmalloc(8), 8, noct_read_done, NULL, 0);
202 if ((err = libusb_submit_transfer(xfer)) < 0)
203 die("Cannot submit transfer: error %d", err);
206 static byte noct_button_state[16];
207 static byte noct_ring_mode[8]; // 0=from-min, 1=from-max, 2=from-mid-right, 3=from-mid-both, 4=single-on, 5=single-off
208 static byte noct_ring_val[9];
210 static uns noct_dirty_button;
211 static uns noct_dirty_ring_mode;
212 static uns noct_dirty_ring_val;
214 static struct libusb_transfer *noct_write_xfer;
215 static uns noct_write_pending;
216 static void noct_sched_write(void);
218 static void noct_write_done(struct libusb_transfer *xfer)
220 int len = xfer->actual_length;
221 DBG("USB: Write done: status %d, length %d", xfer->status, len);
223 if (xfer->status != LIBUSB_TRANSFER_COMPLETED)
225 msg(L_ERROR, "USB write failed with status %d", xfer->status);
229 noct_write_pending = 0;
233 static void noct_do_write(uns cmd, uns arg)
235 DBG("USB: Submitting write %02x %02x", cmd, arg);
236 ASSERT(!noct_write_pending);
237 noct_write_pending = 1;
239 struct libusb_transfer *xfer = noct_write_xfer;
240 byte *pkt = xfer->buffer;
246 if ((err = libusb_submit_transfer(xfer)) < 0)
247 die("Cannot submit transfer: error %d", err);
250 static void noct_sched_write(void)
252 if (noct_write_pending)
255 if (noct_dirty_button)
257 int i = bit_ffs(noct_dirty_button);
258 noct_dirty_button ^= 1U << i;
259 noct_do_write(0x70 + i, noct_button_state[i]);
261 else if (noct_dirty_ring_mode)
263 int i = bit_ffs(noct_dirty_ring_mode);
264 noct_dirty_ring_mode ^= 1U << i;
265 noct_do_write(0x48 + i, noct_ring_mode[i] << 4);
267 else if (noct_dirty_ring_val)
269 int i = bit_ffs(noct_dirty_ring_val);
270 noct_dirty_ring_val ^= 1U << i;
272 noct_do_write(0x50 + i, noct_ring_val[i]);
274 noct_do_write(0x40 + i, noct_ring_val[i]);
278 static void noct_write_init(void)
280 DBG("Noct: Write init");
282 noct_write_xfer = libusb_alloc_transfer(0);
283 libusb_fill_interrupt_transfer(noct_write_xfer, usb_dev, 0x02, xmalloc(8), 0, noct_write_done, NULL, 1000);
286 noct_button_state[2] = 1;
287 noct_ring_mode[0] = 4;
288 noct_ring_val[0] = 0x40;
291 noct_dirty_button = 0xffff;
292 noct_dirty_ring_mode = 0xff;
293 noct_dirty_ring_val = 0x1ff;
297 static void usb_init(void)
301 if ((err = libusb_init(&usb_ctx)) < 0)
302 die("libusb_init failed: error %d", err);
303 libusb_set_debug(usb_ctx, 3);
305 libusb_device **dev_list;
306 libusb_device *found_dev = NULL;
307 ssize_t len = libusb_get_device_list(usb_ctx, &dev_list);
308 for (ssize_t i=0; i < len; i++)
310 libusb_device *dev = dev_list[i];
311 struct libusb_device_descriptor desc;
312 if (libusb_get_device_descriptor(dev, &desc) >= 0 &&
313 desc.idVendor == 0x1235 &&
314 desc.idProduct == 0x000a)
316 msg(L_DEBUG, "Found device: bus %d, addr %d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
318 die("Multiple Nocturn devices found. Please fix me to handle it.");
319 found_dev = libusb_ref_device(dev);
322 libusb_free_device_list(dev_list, 1);
325 die("No Nocturn device found");
327 msg(L_DEBUG, "Initializing device");
329 if ((err = libusb_open(found_dev, &usb_dev)) < 0)
330 die("libusb_open failed: error %d", err);
332 // There exist configurations 1 (high brightness) and 2 (power-save)
333 if ((err = libusb_set_configuration(usb_dev, 1)) < 0)
334 die("libusb_set_configuration: error %d", err);
336 if ((err = libusb_claim_interface(usb_dev, 0)) < 0)
337 die("libusb_claim_interface: error %d", err);
339 for (int i=0; i<4; i++)
342 if ((err = libusb_interrupt_transfer(usb_dev, 0x02, (byte *) noct_init[i] + 1, noct_init[i][0], &done, 5000)) < 0)
343 die("Cannot send init packets: error %d", err);
344 if (done != noct_init[i][0])
345 die("Partial send of init packet: %d < %d", done, noct_init[i][0]);
349 byte xxx[] = { 0x7f, 0x01 };
351 libusb_interrupt_transfer(usb_dev, 0x02, xxx, 2, &done, 5000);
354 DBG("USB: Connecting libusb to mainloop");
356 if (!libusb_pollfds_handle_timeouts(usb_ctx))
357 die("Unsupported version of libusb, please fix me");
359 GARY_INIT_ZERO(usb_fds, 0);
360 libusb_set_pollfd_notifiers(usb_ctx, usb_added_fd, usb_removed_fd, NULL);
362 const struct libusb_pollfd **fds = libusb_get_pollfds(usb_ctx);
364 for (int i=0; fds[i]; i++)
365 usb_added_fd(fds[i]->fd, fds[i]->events, NULL);
373 * Interface to PulseAudio
378 static pa_context *pulse_ctx;
389 static enum pulse_state pulse_state;
390 #define PULSE_STATE(s) do { pulse_state = s; DBG("Pulse: " #s); } while (0)
392 // Tracking of currently running asynchronous operations
399 static clist pulse_op_list;
401 static struct pulse_op *pulse_op_new(void)
403 struct pulse_op *op = xmalloc_zero(sizeof(*op));
404 clist_add_tail(&pulse_op_list, &op->n);
408 static void pulse_op_done(struct pulse_op *op)
411 pa_operation_unref(op->o);
412 clist_remove(&op->n);
416 static void pulse_op_cancel_all(void)
419 while (op = (struct pulse_op *) clist_head(&pulse_op_list))
421 DBG("Pulse: Cancelling pending operation");
422 pa_operation_cancel(op->o);
427 #define PULSE_ASYNC_RUN(name, ...) do { struct pulse_op *_op = pulse_op_new(); _op->o = name(__VA_ARGS__, _op); } while (0)
428 #define PULSE_ASYNC_INIT_RUN(name, ...) do { struct pulse_op *_op = pulse_op_new(); _op->is_init = 1; _op->o = name(__VA_ARGS__, _op); } while (0)
430 static void pulse_sink_input_cb(pa_context *ctx UNUSED, const pa_sink_input_info *i, int eol, void *userdata)
432 struct pulse_op *op = userdata;
437 PULSE_STATE(PS_ONLINE);
442 DBG("Pulse: SINK INPUT #%u: %s client=%d sink=%d", i->index, i->name, i->client, i->sink);
445 static void pulse_sink_cb(pa_context *ctx, const pa_sink_info *i, int eol, void *userdata)
447 struct pulse_op *op = userdata;
453 PULSE_STATE(PS_GET_SINK_INPUTS);
454 PULSE_ASYNC_INIT_RUN(pa_context_get_sink_input_info_list, ctx, pulse_sink_input_cb);
460 DBG("Pulse: SINK #%u: %s (%s)", i->index, i->name, i->description);
463 static void pulse_client_cb(pa_context *ctx, const pa_client_info *i, int eol, void *userdata)
465 struct pulse_op *op = userdata;
471 PULSE_STATE(PS_GET_SINKS);
472 PULSE_ASYNC_INIT_RUN(pa_context_get_sink_info_list, ctx, pulse_sink_cb);
478 DBG("Pulse: CLIENT #%u: %s mod=%u drv=%s", i->index, i->name, i->owner_module, i->driver);
481 static void pulse_subscribe_done_cb(pa_context *ctx, int success, void *userdata)
483 pulse_op_done(userdata);
486 msg(L_ERROR, "pa_context_subscribe failed: success=%d", success);
488 PULSE_STATE(PS_GET_CLIENTS);
489 PULSE_ASYNC_INIT_RUN(pa_context_get_client_info_list, ctx, pulse_client_cb);
492 static void pulse_event_cb(pa_context *ctx, pa_subscription_event_type_t type, uint32_t idx, void *userdata UNUSED)
494 DBG("Pulse: SUBSCRIBE EVENT type=%08x idx=%u", type, idx);
496 uns object = type & PA_SUBSCRIPTION_EVENT_FACILITY_MASK;
497 uns action = type & PA_SUBSCRIPTION_EVENT_TYPE_MASK;
500 case PA_SUBSCRIPTION_EVENT_CLIENT:
501 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
502 PULSE_ASYNC_RUN(pa_context_get_client_info, ctx, idx, pulse_client_cb);
503 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
504 DBG("Pulse: REMOVE CLIENT #%u", idx);
506 case PA_SUBSCRIPTION_EVENT_SINK:
507 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
508 PULSE_ASYNC_RUN(pa_context_get_sink_info_by_index, ctx, idx, pulse_sink_cb);
509 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
510 DBG("Pulse: REMOVE SINK #%u", idx);
512 case PA_SUBSCRIPTION_EVENT_SINK_INPUT:
513 if (action == PA_SUBSCRIPTION_EVENT_NEW || action == PA_SUBSCRIPTION_EVENT_CHANGE)
514 PULSE_ASYNC_RUN(pa_context_get_sink_input_info, ctx, idx, pulse_sink_input_cb);
515 else if (action == PA_SUBSCRIPTION_EVENT_REMOVE)
516 DBG("Pulse: REMOVE SINK INPUT #%u", idx);
521 static void pulse_state_cb(pa_context *ctx, void *userdata UNUSED)
523 int state = pa_context_get_state(ctx);
524 DBG("Pulse: State callback, new state = %d", state);
525 if (state == PA_CONTEXT_READY)
527 if (pulse_state == PS_OFFLINE)
529 PULSE_STATE(PS_SUBSCRIBE);
530 pa_context_set_subscribe_callback(ctx, pulse_event_cb, NULL);
531 PULSE_ASYNC_INIT_RUN(pa_context_subscribe, ctx, PA_SUBSCRIPTION_MASK_ALL, pulse_subscribe_done_cb);
536 if (pulse_state != PS_OFFLINE)
538 PULSE_STATE(PS_OFFLINE);
539 pulse_op_cancel_all();
544 static void pulse_init(void)
547 clist_init(&pulse_op_list);
548 pulse_ctx = pa_context_new(&pmain_api, "ursaryd");
549 pa_context_set_state_callback(pulse_ctx, pulse_state_cb, NULL);
550 pa_context_connect(pulse_ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL);
553 int main(int argc UNUSED, char **argv)
558 // msg(L_INFO, "Initializing USB");
561 msg(L_INFO, "Initializing PulseAudio");
564 msg(L_INFO, "Entering main loop");