moberg.c 7.64 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
#define _POSIX_C_SOURCE  200809L
#define _GNU_SOURCE               /* scandirat */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <basedir.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
Anders Blomdell's avatar
Anders Blomdell committed
13
14
#include <moberg.h>
#include <moberg_config.h>
Anders Blomdell's avatar
Anders Blomdell committed
15
#include <moberg_parser.h>
16

Anders Blomdell's avatar
Anders Blomdell committed
17
18
19
20
21
static struct deferred_action {
  struct deferred_action *next;
  int (*action)(void *param);
  void *param;
} *deferred_action = NULL;
22

Anders Blomdell's avatar
Anders Blomdell committed
23
struct moberg {
Anders Blomdell's avatar
Anders Blomdell committed
24
  struct moberg_config *config;
Anders Blomdell's avatar
Anders Blomdell committed
25
26
27
28
  struct channel_list {
    int capacity;
    struct moberg_channel **value;
  } analog_in, analog_out, digital_in, digital_out, encoder_in;
29
30
};

Anders Blomdell's avatar
Anders Blomdell committed
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
static int channel_list_set(struct channel_list *list,
                            int index,
                            struct moberg_channel *value)
{
  if (list->capacity <= index) {
    int capacity;
    for (capacity = 2 ; capacity <= index ; capacity *= 2);
    void *new = realloc(list->value, capacity * sizeof(**list->value));
    if (!new) {
      goto err;
    }
    void *p = new + list->capacity * sizeof(*list->value);
    memset(p, 0, (capacity - list->capacity) * sizeof(**list->value));
    list->value = new;
    list->capacity = capacity;
  }

  if (0 <= index && index < list->capacity) {
    list->value[index] = value;
    return 1;
  }
err:
  return 0;
}
                                   
static int channel_list_get(struct channel_list *list,
                            int index,
                            struct moberg_channel **value)
{
  if (0 <= index && index < list->capacity) {
    *value = list->value[index];
    return 1;
  }
  return 0;
}

static void channel_list_free(struct channel_list *list)
{
  for (int i = 0 ; i < list->capacity ; i++) {
    if (list->value[i]) {
      list->value[i]->down(list->value[i]);
    }
  }
  free(list->value);
}

Anders Blomdell's avatar
Anders Blomdell committed
77
78
79
80
static void parse_config_at(
  struct moberg *moberg,
  int dirfd,
  const char *pathname)
81
82
83
84
{
  if (dirfd >= 0) {
    int fd = openat(dirfd, pathname, O_RDONLY);
    if (fd >= 0) {
Anders Blomdell's avatar
Anders Blomdell committed
85
86
87
88
89
90
91
92
      struct stat statbuf;
      if (fstat(fd, &statbuf) == 0) {
        char *buf = malloc(statbuf.st_size + 1);
        if (buf) {
          if (read(fd, buf, statbuf.st_size) == statbuf.st_size) {
            buf[statbuf.st_size] = 0;
          }
          printf("Parsing... %s %d %d\n", pathname, dirfd, fd);
93
          struct moberg_config *config = moberg_parse(buf);
Anders Blomdell's avatar
Anders Blomdell committed
94
          printf("-> %p\n", config);
Anders Blomdell's avatar
Anders Blomdell committed
95
          if (config) {
Anders Blomdell's avatar
Anders Blomdell committed
96
97
98
99
100
101
            if (! moberg->config) {
              moberg->config = config;
            } else {
              moberg_config_join(moberg->config, config);
              moberg_config_free(config);
            }
Anders Blomdell's avatar
Anders Blomdell committed
102
103
104
105
          }
          free(buf);
        }
      }
106
107
108
109
110
      close(fd);
    }
  }
}

Anders Blomdell's avatar
Anders Blomdell committed
111
112
static int conf_filter(
  const struct dirent *entry)
113
114
115
116
117
118
119
120
121
{
  char *dot = strrchr(entry->d_name, '.');
  if (dot != NULL && strcmp(dot, ".conf") == 0) {
    return  1;
  } else {
    return 0;
  }
}

Anders Blomdell's avatar
Anders Blomdell committed
122
123
124
static void parse_config_dir_at(
  struct moberg *config,
  int dirfd)
125
126
127
128
129
130
{
  if (dirfd >= 0) {
    struct dirent **entry = NULL;
    int n = scandirat(dirfd, ".", &entry, conf_filter, alphasort);
    for (int i = 0 ; i < n ; i++) {
      parse_config_at(config, dirfd, entry[i]->d_name);
Anders Blomdell's avatar
Anders Blomdell committed
131
      free(entry[i]);
132
133
134
135
136
137
    }
    free(entry);
  }
  
}

Anders Blomdell's avatar
Anders Blomdell committed
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
static int install_channel(struct moberg *moberg,
                           int index,
                           struct moberg_device* device,
                           struct moberg_channel *channel)
{
  fprintf(stderr, "CHAN %p %d %d\n", channel, channel->kind, index);
  if (channel) {
    struct moberg_channel *old = NULL;
    switch (channel->kind) {
      case chan_ANALOGIN:
        channel_list_get(&moberg->analog_in, index, &old);
        break;
      case chan_ANALOGOUT:
        channel_list_get(&moberg->analog_out, index, &old);
        break;
      case chan_DIGITALIN:
        channel_list_get(&moberg->digital_in, index, &old);
        break;
      case chan_DIGITALOUT:
        channel_list_get(&moberg->digital_out, index, &old);
        break;
      case chan_ENCODERIN:
        channel_list_get(&moberg->encoder_in, index, &old);
        break;
    }
    if (old) {
      old->down(old);
    }
    channel->up(channel);
    /* TODO: Clean up old channel */
    switch (channel->kind) {
      case chan_ANALOGIN:
        if (! channel_list_set(&moberg->analog_in, index, channel)) {
          goto err;
        }
        break;
      case chan_ANALOGOUT:
        if (! channel_list_set(&moberg->analog_out, index, channel)) {
          goto err;
        }
        break;
      case chan_DIGITALIN:
        if (! channel_list_set(&moberg->digital_in, index, channel)) {
          goto err;
        }
        break;
      case chan_DIGITALOUT:
        if (! channel_list_set(&moberg->digital_out, index, channel)) {
          goto err;
        }
        break;
      case chan_ENCODERIN:
        if (! channel_list_set(&moberg->encoder_in, index, channel)) {
          goto err;
        }
        break;
    }
  }
  return 1;
err:
  return 0;
}

201
202
static int install_config(struct moberg *moberg)
{
Anders Blomdell's avatar
Anders Blomdell committed
203
204
205
  struct moberg_channel_install install = {
    .context=moberg,
    .channel=install_channel
206
207
  };
  return moberg_config_install_channels(moberg->config, &install);
Anders Blomdell's avatar
Anders Blomdell committed
208
  /* TODO cleanup unused devices...*/
209
210
}

Anders Blomdell's avatar
Anders Blomdell committed
211
212
struct moberg *moberg_new(
  struct moberg_config *config)
213
{
Anders Blomdell's avatar
Anders Blomdell committed
214
  struct moberg *result = malloc(sizeof(*result));
Anders Blomdell's avatar
Anders Blomdell committed
215
216
  if (! result) {
    fprintf(stderr, "Failed to allocate moberg struct\n");
217
218
    goto err;
  }
Anders Blomdell's avatar
Anders Blomdell committed
219
220
221
222
223
224
225
226
227
228
  result->analog_in.capacity = 0;
  result->analog_in.value = NULL;
  result->analog_out.capacity = 0;
  result->analog_out.value = NULL;
  result->digital_in.capacity = 0;
  result->digital_in.value = NULL;
  result->digital_out.capacity = 0;
  result->digital_out.value = NULL;
  result->encoder_in.capacity = 0;
  result->encoder_in.value = NULL;
229
230
  if (config) {
    result->config = config;
Anders Blomdell's avatar
Anders Blomdell committed
231
232
  } else {
    result->config = NULL;
233
234
235
236
237
238
239
240
241
242
243
244
    
    /* Parse default configuration(s) */
    const char * const *config_paths = xdgSearchableConfigDirectories(NULL);
    const char * const *path;
    for (path = config_paths ; *path ; path++) {
      int dirfd1 = open(*path, O_DIRECTORY);
      if (dirfd >= 0) {
        parse_config_at(result, dirfd1, "moberg.conf");
        int dirfd2 = openat(dirfd1, "moberg.d", O_DIRECTORY);
        if (dirfd2 >= 0) { 
          parse_config_dir_at(result, dirfd2);
          close(dirfd2);
Anders Blomdell's avatar
Anders Blomdell committed
245
        }
246
        close(dirfd1);
247
      }
248
      free((char*)*path);
249
    }
250
251
252
253
    free((const char **)config_paths);
    
    /* Read environment default */
    /* Parse environment overrides */
254
  }
255
  install_config(result);
256
  
257
err:
258
259
  return result;
}
Anders Blomdell's avatar
Anders Blomdell committed
260
261
262

void moberg_free(struct moberg *moberg)
{
Anders Blomdell's avatar
Anders Blomdell committed
263
264
  if (moberg) {
    moberg_config_free(moberg->config);
Anders Blomdell's avatar
Anders Blomdell committed
265
266
267
268
269
    channel_list_free(&moberg->analog_in);
    channel_list_free(&moberg->analog_out);
    channel_list_free(&moberg->digital_in);
    channel_list_free(&moberg->digital_out);
    channel_list_free(&moberg->encoder_in);
Anders Blomdell's avatar
Anders Blomdell committed
270
271
    free(moberg);
  }
Anders Blomdell's avatar
Anders Blomdell committed
272
273
274
275
276
277
278
  while (deferred_action) {
    fprintf(stderr, "RUNNING deferred\n");
    struct deferred_action *deferred = deferred_action;
    deferred_action = deferred_action->next;
    deferred->action(deferred->param);
    free(deferred);
  }
Anders Blomdell's avatar
Anders Blomdell committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
}

enum moberg_status moberg_start(
  struct moberg *moberg,
  FILE *f)
{
  return moberg_OK;
}


enum moberg_status moberg_stop(
  struct moberg *moberg,
  FILE *f)
{
  return moberg_OK;
}

Anders Blomdell's avatar
Anders Blomdell committed
296
297
298
299
300
301
302
303
304
305
306
307
308
void moberg_deferred_action(
  int (*action)(void *param),
  void *param)
{
  struct deferred_action *deferred = malloc(sizeof(*deferred));
  if (deferred) {
    fprintf(stderr, "DEFERRED %p %p\n", action, param);
    deferred->next = deferred_action;
    deferred->action = action;
    deferred->param = param;
    deferred_action = deferred;
  }
}