-
Notifications
You must be signed in to change notification settings - Fork 1
/
mis-sound.c
148 lines (124 loc) · 3.19 KB
/
mis-sound.c
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
#include "maemo-input-sounds.h"
int sound_init(struct private_data *priv) {
char *display_name;
int ret;
if (ca_context_create(&priv->canberra_ctx)) {
LOG_ERROR("failed to create canberra context");
exit(1);
}
display_name = XDisplayName(0);
ret = ca_context_change_props(priv->canberra_ctx,
"application.name",
"maemo-input-sounds",
"application.id",
"org.maemo.XInputSounds",
"window.x11.screen",
display_name,
"media.language",
"en_EN",
"canberra.cache-control",
"permanent", NULL);
if (ret)
LOG_VERBOSE1("failed to change canberra properties: %s",
ca_strerror(ret));
ret = ca_context_set_driver(priv->canberra_ctx, "pulse");
if (ret) {
LOG_ERROR1("failed to select canberra PulseAudio driver: %s",
ca_strerror(ret));
exit(1);
}
ret = ca_context_open(priv->canberra_ctx);
if (ret) {
LOG_ERROR1("failed to open canberra context: %s",
ca_strerror(ret));
}
if (priv->canberra_device_name) {
ret =
ca_context_change_device(priv->canberra_ctx,
priv->canberra_device_name);
if (ret) {
LOG_VERBOSE1("failed to reroute context to %s",
ca_strerror(ret));
} else {
LOG_VERBOSE1("sound rerouted to %s",
priv->canberra_device_name);
}
}
return ret;
}
int sound_exit(struct private_data *priv) {
int ret = 0;
if (priv->canberra_ctx) {
ret = ca_context_destroy(priv->canberra_ctx);
priv->canberra_ctx = NULL;
}
return ret;
}
int sound_play(struct private_data *priv, int event_code, signed int interval) {
int result;
char *volume;
const char *media_path;
const char *media_name;
int play_failed;
int vol;
char *s = alloca(sizeof(char) * 16);
if (!priv) {
LOG_ERROR("priv == NULL");
return 0;
}
if (priv->pa_ctx_state != PA_CONTEXT_READY)
return 0;
if (priv->sound_not_ready) {
sound_exit(priv);
sound_init(priv);
priv->sound_not_ready = 0;
}
if (priv->canberra_ctx) {
if (priv->device_state)
return 0;
if (event_code == ButtonPress) {
volume = priv->volume_pen_down;
media_path = "/usr/share/sounds/ui-pen_down.wav";
media_name = "x-maemo-touchscreen-pressed";
} else {
if (event_code != KeyPress)
return 1;
volume = priv->volume_key_press;
media_path = "/usr/share/sounds/ui-key_press.wav";
media_name = "x-maemo-key-pressed";
}
if (!volume)
volume = "-25";
if (event_code == KeyPress && interval <= 100) {
vol = strtol(volume, NULL, 10);
volume = s;
snprintf(s, 12, "%d", vol - 30);
#if 0
// XXX
if (!flag_record_maybe)
return 0;
#endif
}
LOG_VERBOSE1("vol %s, interval %d", volume, interval);
if (g_str_equal(volume, "-60"))
return 0;
play_failed = ca_context_play(priv->canberra_ctx,
0,
"media.filename",
media_path,
"media.name",
media_name,
"canberra.volume",
volume,
"module-stream-restore.id",
media_name, NULL);
if (play_failed)
LOG_VERBOSE1("failed to play sound %s (%s)", media_path,
ca_strerror(play_failed));
result = play_failed == 0;
} else {
LOG_ERROR("priv->canberra_ctx == NULL");
result = 0;
}
return result;
}