summaryrefslogtreecommitdiff
path: root/app/mixer.c
blob: b95c0319db7a63ae1e87401dad34f8c250e0286f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 *  dolmetschctl
 *
 *
 *  Copyright (C) 2015 Christian Pointner <equinox@spreadspace.org>
 *
 *  This file is part of dolmetschctl.
 *
 *  dolmetschctl is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  dolmetschctl is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with dolmetschctl. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <error.h>
#include <poll.h>
#include <assert.h>
#include <sys/types.h>
#include <dirent.h>

#include "mixer.h"

static void free_lang_entry(void* ptr)
{
  lang_t* l = ptr;
  assert(l);
  free(l->name_);
  slist_clear(&(l->cmds_));
  free(l);
}

static void free_task_entry(void* ptr)
{
  task_t* t = ptr;
  assert(t);
  free(t->buf_);
  free(t);
}

static int mixer_read_config_file(mixer_t* x, int dirfd, const char* filename)
{
  assert(dirfd > 0);

  int fd = openat(dirfd, filename, O_RDONLY);
  if(fd < 0) {
    error(0, errno, "erorr open('%s')", filename);
    return -1;
  }
  FILE* conf = fdopen(fd, "r");
  if(conf == NULL) {
    error(0, errno, "erorr fdopen('%s')", filename);
    return -1;
  }

  lang_t* lang = malloc(sizeof(lang_t));
  assert(lang);
  assert((lang->name_ = strdup(filename)));
  slist_init(&(lang->cmds_), free);
  assert(slist_add(&(x->langs_), lang));

  char buf[256];
  while(fgets(buf, sizeof(buf), conf) != NULL) {
    midi_cmd_t* cmd = malloc(sizeof(midi_cmd_t));
    if(!cmd) {
      error(0, ENOMEM, "can't create midi command stucture");
      break;
    }
    if(sscanf(buf, "%02hhx %02hhx %02hhx", &((*cmd)[0]), &((*cmd)[1]), &((*cmd)[2])) != 3) {
      continue;
    }
    assert(slist_add(&(lang->cmds_), cmd));
  }

  fclose(conf);
  return 0;
}

static int mixer_read_config(mixer_t* x)
{
  char path[1024];
  int ret = snprintf(path, sizeof(path), "%s/dolmetschctl/%s", ETCDIR, x->name_);
  assert(ret < sizeof(path));

  DIR* d = opendir(path);
  if(d == NULL) {
    error(0, errno, "MIXER: cannot open config directory '%s'", path);
    return -1;
  }

  struct dirent* entry;
  while((entry = readdir(d))) {
    if(entry->d_type == DT_REG) {
      if((ret = mixer_read_config_file(x, dirfd(d), entry->d_name)))
        break;
    }
  }

  if(closedir(d) < 0) {
    error(0, errno, "MIXER: cannot closeconfig directory '%s'", path);
    return -1;
  }

  return ret;
}

int mixer_init(mixer_t* x, const char* name, const char* device)
{
  assert(x != NULL);

  x->name_ = name;
  x->output_ = NULL;
  slist_init(&(x->langs_), free_lang_entry);
  slist_init(&(x->tasks_), free_task_entry);
  int ret = snd_rawmidi_open(NULL, &(x->output_), device, SND_RAWMIDI_NONBLOCK);
  if(ret < 0) {
    error(0, 0, "MIXER: cannot open midi port '%s': %s", device, snd_strerror(ret));
    return ret;
  }

  return mixer_read_config(x);;
}

static void mixer_print_midi_cmds(slist_t cmds)
{
  if(!slist_length(&(cmds)))
    printf("<no commands>\n");
  else {
    slist_element_t* tmp;
    for(tmp = cmds.first_; tmp; tmp = tmp->next_) {
      midi_cmd_t* c = (midi_cmd_t*)(tmp->data_);
      printf("  0x%02X 0x%02X 0x%02X\n", (*c)[0], (*c)[1], (*c)[2]);
    }
  }
}

void mixer_print_langs(mixer_t* x)
{
  assert(x);

  printf("languages:\n");
  if(!slist_length(&(x->langs_)))
    printf(" <no languages>\n");
  else {
    slist_element_t* tmp;
    for(tmp = x->langs_.first_; tmp; tmp = tmp->next_) {
      lang_t* l = (lang_t*)(tmp->data_);
      printf(" %s:\n", l->name_);
      mixer_print_midi_cmds(l->cmds_);
    }
  }
}

static u_int8_t* mixer_create_task_buf(slist_t cmds, int len)
{
  u_int8_t* buf = malloc(len);
  slist_element_t* tmp;
  int i;
  for(tmp = cmds.first_, i=0; tmp; tmp = tmp->next_, i+=sizeof(midi_cmd_t)) {
    midi_cmd_t* c = (midi_cmd_t*)(tmp->data_);
    memcpy(&(buf[i]), (*c), sizeof(midi_cmd_t));
  }
  return buf;
}

int mixer_switch_lang(mixer_t* x, const char* lang, void (*done_cb)(void*), void* done_data)
{
  slist_element_t* tmp;
  for(tmp = x->langs_.first_; tmp; tmp = tmp->next_) {
    lang_t* l = (lang_t*)(tmp->data_);
    if(!strcmp(lang, l->name_)) {
      int len = slist_length(&(l->cmds_));
      if(len > 0) {
        task_t* task = malloc(sizeof(task_t));
        assert(task);
        task->len_ = sizeof(midi_cmd_t) * len;
        assert((task->buf_ = mixer_create_task_buf(l->cmds_, task->len_)));
        task->write_idx_ = 0;
        task->done_cb_ = done_cb;
        task->done_data_ = done_data;
        assert(slist_add(&(x->tasks_), task));
      }
      return 0;
    }
  }
  error(0, 0, "requested language '%s' not found ... ignoring request", lang);
  return 0;
}

void mixer_print_tasks(mixer_t* x)
{
  assert(x);

  printf("tasks:\n");
  if(!slist_length(&(x->tasks_)))
    printf(" <no tasks>\n");
  else {
    slist_element_t* tmp;
    for(tmp = x->tasks_.first_; tmp; tmp = tmp->next_) {
      task_t* t = (task_t*)(tmp->data_);
      int i;
      printf(" buf(%d) = ", t->len_);
      for(i = 0; i < t->len_; ++i) {
        printf("0x%02X%s", t->buf_[i], (i && !((i+1) % 3)) ? ", " : " ");
      }
      printf("\n write_idx = %d\n done = %016lX ( %016lX )\n\n", t->write_idx_, (int64_t)(t->done_cb_), (int64_t)(t->done_data_));
    }
  }
}

int mixer_get_poll_fd_count(mixer_t* x)
{
	assert(x);

  return snd_rawmidi_poll_descriptors_count(x->output_);
}

int mixer_get_poll_fds(mixer_t* x, struct pollfd *pfds, int npfds)
{
	assert(x && pfds && npfds);

  int ret = snd_rawmidi_poll_descriptors(x->output_, pfds, npfds);
  if(!slist_length(&(x->tasks_))) {
    int i;
    for(i = 0; i < ret; ++i)
      pfds[i].events = 0;
  }
  return ret;
}

int mixer_handle_revents(mixer_t* x, struct pollfd *pfds, int npfds)
{
	assert(x && pfds && npfds);

  int err;
  unsigned short revents;
  if((err = snd_rawmidi_poll_descriptors_revents(x->output_, pfds, npfds, &revents)) < 0) {
    error(0, 0, "MIXER: cannot get poll events: %s", snd_strerror(errno));
    return -1;
  }
  if(pfds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
    error(0, 0, "MIXER: got POLLERR, POLLHUP or POLLNVAL");
    return -1;
  }
  if(!(revents & POLLOUT))
    return 0;

  assert(x->tasks_.first_);
  task_t* task = (task_t*)(x->tasks_.first_->data_);
  assert(task);
  assert(task->buf_);

  int ret = snd_rawmidi_write(x->output_, &(task->buf_[task->write_idx_]), task->len_ - task->write_idx_);
  if(ret == -EAGAIN)
    return 0;
  if(ret < 0) {
    error(0, 0, "MIXER: cannot write to midi port: %s", snd_strerror(ret));
    return -1;
  }
  task->write_idx_ += ret;
  if(task->write_idx_ >= task->len_) {
    if((err = snd_rawmidi_drain(x->output_)) < 0) {
      error(0, 0, "MIXER: cannot drain output: %s", snd_strerror(err));
      return -1;
    }
    if(task->done_cb_)
      task->done_cb_(task->done_data_);

    slist_remove(&(x->tasks_), task);
  }

  return 0;
}