blob: 69bbc5e6d91f48d7d916fbf141ab53ad5b218bdd [file] [log] [blame]
Bram Moolenaar427f5b62019-06-09 13:43:51 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * sound.c: functions related making noise
12 */
13
14#include "vim.h"
15
Bram Moolenaar9b283522019-06-17 22:19:33 +020016#if defined(FEAT_SOUND) || defined(PROTO)
Bram Moolenaar427f5b62019-06-09 13:43:51 +020017
18static long sound_id = 0;
Bram Moolenaar427f5b62019-06-09 13:43:51 +020019
20typedef struct soundcb_S soundcb_T;
21
22struct soundcb_S {
23 callback_T snd_callback;
Bram Moolenaar9b283522019-06-17 22:19:33 +020024#ifdef MSWIN
25 MCIDEVICEID snd_device_id;
26 long snd_id;
27#endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +020028 soundcb_T *snd_next;
29};
30
31static soundcb_T *first_callback = NULL;
32
Bram Moolenaar28e67e02019-08-15 23:05:49 +020033/*
34 * Return TRUE when a sound callback has been created, it may be invoked when
35 * the sound finishes playing. Also see has_sound_callback_in_queue().
36 */
37 int
38has_any_sound_callback(void)
39{
40 return first_callback != NULL;
41}
42
Bram Moolenaar427f5b62019-06-09 13:43:51 +020043 static soundcb_T *
44get_sound_callback(typval_T *arg)
45{
46 callback_T callback;
47 soundcb_T *soundcb;
48
49 if (arg->v_type == VAR_UNKNOWN)
50 return NULL;
51 callback = get_callback(arg);
52 if (callback.cb_name == NULL)
53 return NULL;
54
55 soundcb = ALLOC_ONE(soundcb_T);
56 if (soundcb == NULL)
57 free_callback(&callback);
58 else
59 {
60 soundcb->snd_next = first_callback;
61 first_callback = soundcb;
62 set_callback(&soundcb->snd_callback, &callback);
63 }
64 return soundcb;
65}
66
67/*
68 * Delete "soundcb" from the list of pending callbacks.
69 */
70 static void
71delete_sound_callback(soundcb_T *soundcb)
72{
73 soundcb_T *p;
74 soundcb_T *prev = NULL;
75
76 for (p = first_callback; p != NULL; prev = p, p = p->snd_next)
77 if (p == soundcb)
78 {
79 if (prev == NULL)
80 first_callback = p->snd_next;
81 else
82 prev->snd_next = p->snd_next;
83 free_callback(&p->snd_callback);
84 vim_free(p);
85 break;
86 }
87}
88
Bram Moolenaar9b283522019-06-17 22:19:33 +020089#if defined(HAVE_CANBERRA) || defined(PROTO)
90
91/*
92 * Sound implementation for Linux/Unix/Mac using libcanberra.
93 */
94# include <canberra.h>
95
96static ca_context *context = NULL;
97
Bram Moolenaar28e67e02019-08-15 23:05:49 +020098// Structure to store info about a sound callback to be invoked soon.
99typedef struct soundcb_queue_S soundcb_queue_T;
100
101struct soundcb_queue_S {
102 soundcb_queue_T *scb_next;
103 uint32_t scb_id; // ID of the sound
104 int scb_result; // CA_ value
105 soundcb_T *scb_callback; // function to call
106};
107
108// Queue of callbacks to invoke from the main loop.
109static soundcb_queue_T *callback_queue = NULL;
110
111/*
112 * Add a callback to the queue of callbacks to invoke later from the main loop.
113 * That is because the callback may be called from another thread and invoking
114 * another sound function may cause trouble.
115 */
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200116 static void
117sound_callback(
118 ca_context *c UNUSED,
119 uint32_t id,
120 int error_code,
121 void *userdata)
122{
Bram Moolenaar28e67e02019-08-15 23:05:49 +0200123 soundcb_T *soundcb = (soundcb_T *)userdata;
124 soundcb_queue_T *scb;
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200125
Bram Moolenaar28e67e02019-08-15 23:05:49 +0200126 scb = ALLOC_ONE(soundcb_queue_T);
127 if (scb == NULL)
128 return;
129 scb->scb_next = callback_queue;
130 callback_queue = scb;
131 scb->scb_id = id;
132 scb->scb_result = error_code == CA_SUCCESS ? 0
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200133 : error_code == CA_ERROR_CANCELED
134 || error_code == CA_ERROR_DESTROYED
135 ? 1 : 2;
Bram Moolenaar28e67e02019-08-15 23:05:49 +0200136 scb->scb_callback = soundcb;
137}
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200138
Bram Moolenaar28e67e02019-08-15 23:05:49 +0200139/*
140 * Return TRUE if there is a sound callback to be called.
141 */
142 int
143has_sound_callback_in_queue(void)
144{
145 return callback_queue != NULL;
146}
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200147
Bram Moolenaar28e67e02019-08-15 23:05:49 +0200148/*
149 * Invoke queued sound callbacks.
150 */
151 void
152invoke_sound_callback(void)
153{
154 soundcb_queue_T *scb;
155 typval_T argv[3];
156 typval_T rettv;
157
158
159 while (callback_queue != NULL)
160 {
161 scb = callback_queue;
162 callback_queue = scb->scb_next;
163
164 argv[0].v_type = VAR_NUMBER;
165 argv[0].vval.v_number = scb->scb_id;
166 argv[1].v_type = VAR_NUMBER;
167 argv[1].vval.v_number = scb->scb_result;
168 argv[2].v_type = VAR_UNKNOWN;
169
170 call_callback(&scb->scb_callback->snd_callback, -1, &rettv, 2, argv);
171 clear_tv(&rettv);
172
173 delete_sound_callback(scb->scb_callback);
174 }
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200175 redraw_after_callback(TRUE);
176}
177
178 static void
179sound_play_common(typval_T *argvars, typval_T *rettv, int playfile)
180{
181 if (context == NULL)
182 ca_context_create(&context);
183 if (context != NULL)
184 {
185 soundcb_T *soundcb = get_sound_callback(&argvars[1]);
186 int res = CA_ERROR_INVALID;
187
188 ++sound_id;
189 if (soundcb == NULL)
190 {
191 res = ca_context_play(context, sound_id,
192 playfile ? CA_PROP_MEDIA_FILENAME : CA_PROP_EVENT_ID,
193 tv_get_string(&argvars[0]),
194 CA_PROP_CANBERRA_CACHE_CONTROL, "volatile",
195 NULL);
196 }
197 else
198 {
199 static ca_proplist *proplist = NULL;
200
201 ca_proplist_create(&proplist);
202 if (proplist != NULL)
203 {
204 if (playfile)
205 ca_proplist_sets(proplist, CA_PROP_MEDIA_FILENAME,
206 (char *)tv_get_string(&argvars[0]));
207 else
208 ca_proplist_sets(proplist, CA_PROP_EVENT_ID,
209 (char *)tv_get_string(&argvars[0]));
210 ca_proplist_sets(proplist, CA_PROP_CANBERRA_CACHE_CONTROL,
211 "volatile");
212 res = ca_context_play_full(context, sound_id, proplist,
213 sound_callback, soundcb);
214 if (res != CA_SUCCESS)
215 delete_sound_callback(soundcb);
216
217 ca_proplist_destroy(proplist);
218 }
219 }
220 rettv->vval.v_number = res == CA_SUCCESS ? sound_id : 0;
221 }
222}
223
224 void
225f_sound_playevent(typval_T *argvars, typval_T *rettv)
226{
227 sound_play_common(argvars, rettv, FALSE);
228}
229
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200230/*
231 * implementation of sound_playfile({path} [, {callback}])
232 */
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200233 void
234f_sound_playfile(typval_T *argvars, typval_T *rettv)
235{
236 sound_play_common(argvars, rettv, TRUE);
237}
238
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200239/*
240 * implementation of sound_stop({id})
241 */
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200242 void
243f_sound_stop(typval_T *argvars, typval_T *rettv UNUSED)
244{
245 if (context != NULL)
246 ca_context_cancel(context, tv_get_number(&argvars[0]));
247}
248
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200249/*
250 * implementation of sound_clear()
251 */
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200252 void
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200253f_sound_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200254{
255 if (context != NULL)
256 {
257 ca_context_destroy(context);
258 context = NULL;
259 }
260}
261
Bram Moolenaar9b283522019-06-17 22:19:33 +0200262# if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200263 void
264sound_free(void)
265{
266 if (context != NULL)
267 ca_context_destroy(context);
268 while (first_callback != NULL)
269 delete_sound_callback(first_callback);
270}
Bram Moolenaar9b283522019-06-17 22:19:33 +0200271# endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200272
Bram Moolenaar9b283522019-06-17 22:19:33 +0200273#elif defined(MSWIN)
274
275/*
276 * Sound implementation for MS-Windows.
277 */
278
279static HWND g_hWndSound = NULL;
280
281 static LRESULT CALLBACK
282sound_wndproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
283{
284 soundcb_T *p;
285
286 switch (message)
287 {
288 case MM_MCINOTIFY:
289 for (p = first_callback; p != NULL; p = p->snd_next)
290 if (p->snd_device_id == (MCIDEVICEID) lParam)
291 {
292 typval_T argv[3];
293 typval_T rettv;
Bram Moolenaar9b283522019-06-17 22:19:33 +0200294 char buf[32];
295
296 vim_snprintf(buf, sizeof(buf), "close sound%06ld",
297 p->snd_id);
298 mciSendString(buf, NULL, 0, 0);
299
300 argv[0].v_type = VAR_NUMBER;
301 argv[0].vval.v_number = p->snd_id;
302 argv[1].v_type = VAR_NUMBER;
303 argv[1].vval.v_number =
304 wParam == MCI_NOTIFY_SUCCESSFUL ? 0
305 : wParam == MCI_NOTIFY_ABORTED ? 1 : 2;
306 argv[2].v_type = VAR_UNKNOWN;
307
Bram Moolenaarb2129062019-08-03 18:31:11 +0200308 call_callback(&p->snd_callback, -1, &rettv, 2, argv);
Bram Moolenaar9b283522019-06-17 22:19:33 +0200309 clear_tv(&rettv);
310
311 delete_sound_callback(p);
312 redraw_after_callback(TRUE);
313
314 }
315 break;
316 }
317
318 return DefWindowProc(hwnd, message, wParam, lParam);
319}
320
321 static HWND
322sound_window()
323{
324 if (g_hWndSound == NULL)
325 {
326 LPCSTR clazz = "VimSound";
327 WNDCLASS wndclass = {
328 0, sound_wndproc, 0, 0, g_hinst, NULL, 0, 0, NULL, clazz };
329 RegisterClass(&wndclass);
330 g_hWndSound = CreateWindow(clazz, NULL, 0, 0, 0, 0, 0,
331 HWND_MESSAGE, NULL, g_hinst, NULL);
332 }
333
334 return g_hWndSound;
335}
336
337 void
338f_sound_playevent(typval_T *argvars, typval_T *rettv)
339{
340 WCHAR *wp;
341
342 rettv->v_type = VAR_NUMBER;
343 rettv->vval.v_number = 0;
344
345 wp = enc_to_utf16(tv_get_string(&argvars[0]), NULL);
346 if (wp == NULL)
347 return;
348
349 PlaySoundW(wp, NULL, SND_ASYNC | SND_ALIAS);
350 free(wp);
351
352 rettv->vval.v_number = ++sound_id;
353}
354
355 void
356f_sound_playfile(typval_T *argvars, typval_T *rettv)
357{
358 long newid = sound_id + 1;
359 size_t len;
360 char_u *p, *esc;
361 WCHAR *wp;
362 soundcb_T *soundcb;
363 char buf[32];
364 MCIERROR err;
365
366 rettv->v_type = VAR_NUMBER;
367 rettv->vval.v_number = 0;
368
369 esc = vim_strsave_shellescape(tv_get_string(&argvars[0]), FALSE, FALSE);
370
371 len = STRLEN(esc) + 5 + 18 + 1;
372 p = alloc(len);
373 if (p == NULL)
374 {
375 free(esc);
376 return;
377 }
378 vim_snprintf((char *)p, len, "open %s alias sound%06ld", esc, newid);
379 free(esc);
380
381 wp = enc_to_utf16((char_u *)p, NULL);
382 free(p);
383 if (wp == NULL)
384 return;
385
386 err = mciSendStringW(wp, NULL, 0, sound_window());
387 free(wp);
388 if (err != 0)
389 return;
390
391 vim_snprintf(buf, sizeof(buf), "play sound%06ld notify", newid);
392 err = mciSendString(buf, NULL, 0, sound_window());
393 if (err != 0)
394 goto failure;
395
396 sound_id = newid;
397 rettv->vval.v_number = sound_id;
398
399 soundcb = get_sound_callback(&argvars[1]);
400 if (soundcb != NULL)
401 {
402 vim_snprintf(buf, sizeof(buf), "sound%06ld", newid);
403 soundcb->snd_id = newid;
404 soundcb->snd_device_id = mciGetDeviceID(buf);
405 }
406 return;
407
408failure:
409 vim_snprintf(buf, sizeof(buf), "close sound%06ld", newid);
410 mciSendString(buf, NULL, 0, NULL);
411}
412
413 void
414f_sound_stop(typval_T *argvars, typval_T *rettv UNUSED)
415{
416 long id = tv_get_number(&argvars[0]);
417 char buf[32];
418
419 vim_snprintf(buf, sizeof(buf), "stop sound%06ld", id);
420 mciSendString(buf, NULL, 0, NULL);
421}
422
423 void
424f_sound_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
425{
426 PlaySound(NULL, NULL, 0);
427 mciSendString("close all", NULL, 0, NULL);
428}
429
430# if defined(EXITFREE)
431 void
432sound_free(void)
433{
434 CloseWindow(g_hWndSound);
435
436 while (first_callback != NULL)
437 delete_sound_callback(first_callback);
438}
439# endif
440
441#endif // MSWIN
442
443#endif // FEAT_SOUND