summaryrefslogtreecommitdiffstats
path: root/src/canberra-boot.c
blob: b6993469ff579bb76a39f6b49506f8eb7fe5303f (plain)
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
/*-*- Mode: C; c-basic-offset: 8 -*-*/

/***
  This file is part of libcanberra.

  Copyright 2008 Lennart Poettering

  libcanberra is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation, either version 2.1 of the
  License, or (at your option) any later version.

  libcanberra 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with libcanberra. If not, see
  <http://www.gnu.org/licenses/>.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <sys/types.h>
#include <sys/eventfd.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include <canberra.h>
#include <libudev.h>

#include "macro.h"

static char *find_device(void) {
        struct udev *udev = NULL;
        struct udev_enumerate *udev_enum = NULL;
        struct udev_list_entry *i, *first;
        int internal_device = -1, pci_device = -1, other_device = -1;
        char *s = NULL;

        if (!(udev = udev_new())) {
                fprintf(stderr, "Failed to allocate udev context.\n");
                return NULL;
        }

        if (!(udev_enum =  udev_enumerate_new(udev))) {
                fprintf(stderr, "Failed to allocate enumeration object.\n");
                goto finish;
        }

        if (udev_enumerate_add_match_subsystem(udev_enum, "sound") < 0) {
                fprintf(stderr, "Failed to install subsystem match.\n");
                goto finish;
        }

        if (udev_enumerate_scan_devices(udev_enum) < 0) {
                fprintf(stderr, "Failed to enumerate devices.\n");
                goto finish;
        }

        first = udev_enumerate_get_list_entry(udev_enum);
        udev_list_entry_foreach(i, first) {
                const char *sysfs, *p;
                long l;
                char d[64];
                char *e = NULL;
                struct udev_device *dev;
                const char *ff, *class, *bus;

                sysfs = udev_list_entry_get_name(i);

                if (!(p = strrchr(sysfs, '/')))
                        continue;

                p++;

                if (strncmp(p, "card", 4) != 0)
                        continue;

                errno = 0;
                l = strtol(p + 4, &e, 10);
                if (!e || *e != 0 || errno != 0)
                        continue;

                /* Check whether this sound card has a playback device
                 * #0 (i.e. something that is not HDMI, SPDIF or
                 * something other weird.) */
                snprintf(d, sizeof(d), "/sys/class/sound/card%i/pcmC%iD0p", (int) l, (int) l);
                if (access(d, F_OK) < 0)
                        continue;

                if (!(dev = udev_device_new_from_syspath(udev, sysfs)))
                        continue;

                class = udev_device_get_property_value(dev, "SOUND_CLASS");
                ff = udev_device_get_property_value(dev, "SOUND_FORM_FACTOR");
                bus = udev_device_get_property_value(dev, "ID_BUS");

                /* Ignore modems and other non-audio sound device */
                if (class && !ca_streq(class, "sound")) {
                        udev_device_unref(dev);
                        continue;
                }

                /* Prefer "internal" devices */
                if (internal_device < 0 && ff && ca_streq(ff, "internal"))
                        internal_device = (int) l;

                /* If no "internal" device is available, prefer PCI devices */
                if (pci_device < 0 && bus && ca_streq(bus, "pci"))
                        pci_device = (int) l;

                /* If neither "internal" nor PCI devices are
                 * available, pick whatever we can find */
                if (other_device < 0)
                        other_device = (int) l;

                udev_device_unref(dev);
        }

        if (internal_device >= 0)
                asprintf(&s, "front:%i", internal_device);
        else if (pci_device >= 0)
                asprintf(&s, "front:%i", pci_device);
        else if (other_device >= 0)
                asprintf(&s, "front:%i", other_device);

finish:
        if (udev_enum)
                udev_enumerate_unref(udev_enum);

        if (udev)
                udev_unref(udev);

        return s;
}

static void finish_cb(ca_context *c, uint32_t id, int error_code, void *userdata) {
        uint64_t u = 1;

        for (;;) {
                if (write(CA_PTR_TO_INT(userdata), &u, sizeof(u)) > 0)
                        break;

                if (errno != EINTR) {
                        fprintf(stderr, "write() failed: %s\n", strerror(errno));
                        exit(EXIT_FAILURE);
                }
        }
}

int main(int argc, char *argv[]) {
        ca_context *c = NULL;
        ca_proplist *p = NULL;
        int ret = EXIT_FAILURE, r;
        int fd = -1;
        char *device = NULL;

        if (argc > 2) {
                fprintf(stderr, "This program expects no more than one parameter.\n");
                goto finish;
        }

        if ((fd = eventfd(0, EFD_CLOEXEC)) < 0) {
                fprintf(stderr, "Failed to create event file descriptor: %s\n", strerror(errno));
                goto finish;
        }

        if ((r = ca_context_create(&c)) < 0) {
                fprintf(stderr, "Failed to create context: %s\n", ca_strerror(r));
                goto finish;
        }

        if ((r = ca_context_set_driver(c, "alsa")) < 0) {
                fprintf(stderr, "Failed to set driver: %s\n", ca_strerror(r));
                goto finish;
        }

        if (!(device = find_device())) {
                ret = EXIT_SUCCESS;
                goto finish;
        }

        if ((r = ca_context_change_device(c, device)) < 0) {
                fprintf(stderr, "Failed to set device: %s\n", ca_strerror(r));
                goto finish;
        }

        if ((r = ca_proplist_create(&p)) < 0) {
                fprintf(stderr, "Failed to create property list: %s\n", ca_strerror(r));
                goto finish;
        }

        if ((r = ca_proplist_sets(p, CA_PROP_EVENT_ID, argc >= 2 ? argv[1] : "system-bootup")) < 0 ||
            (r = ca_proplist_sets(p, CA_PROP_CANBERRA_CACHE_CONTROL, "never")) < 0) {
                fprintf(stderr, "Failed to set event id: %s\n", strerror(r));
                goto finish;
        }

        if ((r = ca_context_play_full(c, 0, p, finish_cb, CA_INT_TO_PTR(fd))) < 0) {
                fprintf(stderr, "Failed to play event sound: %s\n", ca_strerror(r));
                goto finish;
        }

        for (;;) {
                uint64_t u;

                if (read(fd, &u, sizeof(u)) < 0) {
                        if (errno == EINTR)
                                break;

                        fprintf(stderr, "read() failed: %s\n", strerror(errno));

                } else
                        break;
        }

        ret = EXIT_SUCCESS;

finish:
        if (c)
                ca_context_destroy(c);

        if (p)
                ca_proplist_destroy(p);

        if (fd >= 0)
                close(fd);

        free(device);

        return ret;
}