#include <alsa/asoundlib.h>
#include <log.h>
#include <stdlib.h>

#include "types.h"

#include "config.h"

void midi_open(MidiDevice *device, const char *name) {
  strlcpy(device->name, name, STR_LEN);
  device->input = NULL;
  device->output = NULL;

  snd_rawmidi_open(&device->input, &device->output, name, SND_RAWMIDI_NONBLOCK);

  device->error = device->input == NULL || device->output == NULL;

  log_info("(%s) MIDI open", name);
}

void midi_write(const MidiDevice *device, unsigned char code,
                unsigned char value) {
  if (device->error) {
    return;
  }

  unsigned char buffer[3];

  buffer[0] = 0xB0;
  buffer[1] = code;
  buffer[2] = value;

  snd_rawmidi_write(device->output, buffer, 3);
}

bool midi_background_listen(const MidiDevice *device,
                            const SharedContext *context,
                            void (*event_callback)(unsigned char code,
                                                   unsigned char value)) {
  pid_t pid;
  int bytes_read;
  unsigned char buffer[3];

  pid = fork();
  if (pid < 0) {
    log_error("Could not create subprocess");
    return false;
  }
  if (pid == 0) {
    return true;
  }
  log_info("(%s) background acquisition started (pid: %d)", device->name, pid);

  while (!context->stop) {
    bytes_read = snd_rawmidi_read(device->input, buffer, 3);
    if (bytes_read == 3) {
      event_callback(buffer[1], buffer[2]);
    }
  }

  log_info("(%s) background acquisition stopped by main thread (pid: %d)",
           device->name, pid);
  exit(EXIT_SUCCESS);
}
