moberg_config.c 2.61 KB
Newer Older
Anders Blomdell's avatar
Anders Blomdell committed
1
2
3
4
5
6
7
8
9
#include <stdlib.h>
#include <string.h>
#include <moberg_config.h>

struct moberg_config
{
  struct device_entry {
    struct device_entry *next;
    struct moberg_device *device;
Anders Blomdell's avatar
Anders Blomdell committed
10
  } *device_head, **device_tail;
Anders Blomdell's avatar
Anders Blomdell committed
11
12
13
14
15
16
};

struct moberg_config *moberg_config_new()
{
  struct moberg_config *result = malloc(sizeof *result);

17
  if (result) {
Anders Blomdell's avatar
Anders Blomdell committed
18
19
    result->device_head = NULL;
    result->device_tail = &result->device_head;
20
  }
Anders Blomdell's avatar
Anders Blomdell committed
21
22
23
24
25
26
  
  return result;
}

void moberg_config_free(struct moberg_config *config)
{
Anders Blomdell's avatar
Anders Blomdell committed
27
  struct device_entry *entry = config->device_head;
Anders Blomdell's avatar
Anders Blomdell committed
28
29
30
31
32
33
34
35
36
37
38
39
  while (entry) {
    struct device_entry *tmp = entry;
    entry = entry->next;
    moberg_device_free(tmp->device);
    free(tmp);
  }
  free(config);
}

int moberg_config_join(struct moberg_config *dest,
                       struct moberg_config *src)
{
Anders Blomdell's avatar
Anders Blomdell committed
40
41
42
43
44
45
46
47
48
49
50
  if (src && dest) {
    while (src->device_head) {
      struct device_entry *d = src->device_head;
      src->device_head = d->next;
      if (! src->device_head) {
        src->device_tail = &src->device_head;
      }
        
      *dest->device_tail = d;
      dest->device_tail = &d->next;
    }
Anders Blomdell's avatar
Anders Blomdell committed
51
52
53
54
55
56
57
58
59
60
    return 1;
  }
  return 0;
}

int moberg_config_add_device(struct moberg_config *config,
                             struct moberg_device *device)
{
  struct device_entry *entry = malloc(sizeof(*entry));

Anders Blomdell's avatar
Anders Blomdell committed
61
62
  if (! entry) { goto err; }
  entry->next = NULL;
Anders Blomdell's avatar
Anders Blomdell committed
63
  entry->device = device;
Anders Blomdell's avatar
Anders Blomdell committed
64
65
  *config->device_tail = entry;
  config->device_tail = &entry->next;
Anders Blomdell's avatar
Anders Blomdell committed
66
67

  return 1;
Anders Blomdell's avatar
Anders Blomdell committed
68
69
err:
  return 0;
Anders Blomdell's avatar
Anders Blomdell committed
70
71
}

72
int moberg_config_install_channels(struct moberg_config *config,
Anders Blomdell's avatar
Anders Blomdell committed
73
                                   struct moberg_channel_install *install)
74
{
Anders Blomdell's avatar
Anders Blomdell committed
75
76
77
78
  int result = 1;
  for (struct device_entry *d = config->device_head ; d ; d = d->next) {
    result &= moberg_device_install_channels(d->device, install);
  }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  struct device_entry *device = config->device_head;
  struct device_entry **previous = &config->device_head;
  while (device) {
    struct device_entry *next = device->next;
    if (moberg_device_in_use(device->device)) {
      previous = &device->next;
    } else {
      fprintf(stderr, "FREE\n");
      moberg_device_free(device->device);
      free(device);
      *previous = next;
    }
    device = next;
  }

Anders Blomdell's avatar
Anders Blomdell committed
94
  return result;
95
96
}

97
98
99
100
101
102
103
104
105
int moberg_config_start(struct moberg_config *config,
                        FILE *f)
{
  for (struct device_entry *d = config->device_head ; d ; d = d->next) {
    moberg_device_start(d->device, f);
  }
  return 1;
}

106

107
108
109
110
111
112
113
114
int moberg_config_stop(struct moberg_config *config,
                       FILE *f)
{
  for (struct device_entry *d = config->device_head ; d ; d = d->next) {
    moberg_device_stop(d->device, f);
  }
  return 1;
}