blob: f857c87255f75abc4f928a62c83484c0d7eff692 [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);
Bram Moolenaar821d7712019-08-30 16:30:00 +0200174 vim_free(scb);
Bram Moolenaar28e67e02019-08-15 23:05:49 +0200175 }
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200176 redraw_after_callback(TRUE);
177}
178
179 static void
180sound_play_common(typval_T *argvars, typval_T *rettv, int playfile)
181{
Yegappan Lakshmanan5bca9062021-07-24 21:33:26 +0200182 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
183 return;
184
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200185 if (context == NULL)
186 ca_context_create(&context);
187 if (context != NULL)
188 {
189 soundcb_T *soundcb = get_sound_callback(&argvars[1]);
190 int res = CA_ERROR_INVALID;
191
192 ++sound_id;
193 if (soundcb == NULL)
194 {
195 res = ca_context_play(context, sound_id,
196 playfile ? CA_PROP_MEDIA_FILENAME : CA_PROP_EVENT_ID,
197 tv_get_string(&argvars[0]),
198 CA_PROP_CANBERRA_CACHE_CONTROL, "volatile",
199 NULL);
200 }
201 else
202 {
203 static ca_proplist *proplist = NULL;
204
205 ca_proplist_create(&proplist);
206 if (proplist != NULL)
207 {
208 if (playfile)
209 ca_proplist_sets(proplist, CA_PROP_MEDIA_FILENAME,
210 (char *)tv_get_string(&argvars[0]));
211 else
212 ca_proplist_sets(proplist, CA_PROP_EVENT_ID,
213 (char *)tv_get_string(&argvars[0]));
214 ca_proplist_sets(proplist, CA_PROP_CANBERRA_CACHE_CONTROL,
215 "volatile");
216 res = ca_context_play_full(context, sound_id, proplist,
217 sound_callback, soundcb);
218 if (res != CA_SUCCESS)
219 delete_sound_callback(soundcb);
220
221 ca_proplist_destroy(proplist);
222 }
223 }
224 rettv->vval.v_number = res == CA_SUCCESS ? sound_id : 0;
225 }
226}
227
228 void
229f_sound_playevent(typval_T *argvars, typval_T *rettv)
230{
231 sound_play_common(argvars, rettv, FALSE);
232}
233
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200234/*
235 * implementation of sound_playfile({path} [, {callback}])
236 */
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200237 void
238f_sound_playfile(typval_T *argvars, typval_T *rettv)
239{
240 sound_play_common(argvars, rettv, TRUE);
241}
242
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200243/*
244 * implementation of sound_stop({id})
245 */
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200246 void
247f_sound_stop(typval_T *argvars, typval_T *rettv UNUSED)
248{
249 if (context != NULL)
250 ca_context_cancel(context, tv_get_number(&argvars[0]));
251}
252
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200253/*
254 * implementation of sound_clear()
255 */
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200256 void
Bram Moolenaar3ff5f0f2019-06-10 13:11:22 +0200257f_sound_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200258{
259 if (context != NULL)
260 {
261 ca_context_destroy(context);
262 context = NULL;
263 }
264}
265
Bram Moolenaar9b283522019-06-17 22:19:33 +0200266# if defined(EXITFREE) || defined(PROTO)
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200267 void
268sound_free(void)
269{
Bram Moolenaar821d7712019-08-30 16:30:00 +0200270 soundcb_queue_T *scb;
271
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200272 if (context != NULL)
273 ca_context_destroy(context);
Bram Moolenaar821d7712019-08-30 16:30:00 +0200274
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200275 while (first_callback != NULL)
276 delete_sound_callback(first_callback);
Bram Moolenaar821d7712019-08-30 16:30:00 +0200277
278 while (callback_queue != NULL)
279 {
280 scb = callback_queue;
281 callback_queue = scb->scb_next;
282 delete_sound_callback(scb->scb_callback);
283 vim_free(scb);
284 }
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200285}
Bram Moolenaar9b283522019-06-17 22:19:33 +0200286# endif
Bram Moolenaar427f5b62019-06-09 13:43:51 +0200287
Bram Moolenaar9b283522019-06-17 22:19:33 +0200288#elif defined(MSWIN)
289
290/*
291 * Sound implementation for MS-Windows.
292 */
293
294static HWND g_hWndSound = NULL;
295
296 static LRESULT CALLBACK
297sound_wndproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
298{
299 soundcb_T *p;
300
301 switch (message)
302 {
303 case MM_MCINOTIFY:
304 for (p = first_callback; p != NULL; p = p->snd_next)
305 if (p->snd_device_id == (MCIDEVICEID) lParam)
306 {
307 typval_T argv[3];
308 typval_T rettv;
Bram Moolenaar9b283522019-06-17 22:19:33 +0200309 char buf[32];
310
311 vim_snprintf(buf, sizeof(buf), "close sound%06ld",
312 p->snd_id);
313 mciSendString(buf, NULL, 0, 0);
314
315 argv[0].v_type = VAR_NUMBER;
316 argv[0].vval.v_number = p->snd_id;
317 argv[1].v_type = VAR_NUMBER;
318 argv[1].vval.v_number =
319 wParam == MCI_NOTIFY_SUCCESSFUL ? 0
320 : wParam == MCI_NOTIFY_ABORTED ? 1 : 2;
321 argv[2].v_type = VAR_UNKNOWN;
322
Bram Moolenaarb2129062019-08-03 18:31:11 +0200323 call_callback(&p->snd_callback, -1, &rettv, 2, argv);
Bram Moolenaar9b283522019-06-17 22:19:33 +0200324 clear_tv(&rettv);
325
326 delete_sound_callback(p);
327 redraw_after_callback(TRUE);
328
329 }
330 break;
331 }
332
333 return DefWindowProc(hwnd, message, wParam, lParam);
334}
335
336 static HWND
337sound_window()
338{
339 if (g_hWndSound == NULL)
340 {
341 LPCSTR clazz = "VimSound";
342 WNDCLASS wndclass = {
343 0, sound_wndproc, 0, 0, g_hinst, NULL, 0, 0, NULL, clazz };
344 RegisterClass(&wndclass);
345 g_hWndSound = CreateWindow(clazz, NULL, 0, 0, 0, 0, 0,
346 HWND_MESSAGE, NULL, g_hinst, NULL);
347 }
348
349 return g_hWndSound;
350}
351
352 void
353f_sound_playevent(typval_T *argvars, typval_T *rettv)
354{
355 WCHAR *wp;
356
Yegappan Lakshmanan5bca9062021-07-24 21:33:26 +0200357 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
358 return;
359
Bram Moolenaar9b283522019-06-17 22:19:33 +0200360 wp = enc_to_utf16(tv_get_string(&argvars[0]), NULL);
361 if (wp == NULL)
362 return;
363
Dominique Pelle2f9c2092021-06-07 20:28:45 +0200364 if (PlaySoundW(wp, NULL, SND_ASYNC | SND_ALIAS))
365 rettv->vval.v_number = ++sound_id;
Bram Moolenaar9b283522019-06-17 22:19:33 +0200366 free(wp);
Bram Moolenaar9b283522019-06-17 22:19:33 +0200367}
368
369 void
370f_sound_playfile(typval_T *argvars, typval_T *rettv)
371{
372 long newid = sound_id + 1;
373 size_t len;
374 char_u *p, *esc;
375 WCHAR *wp;
376 soundcb_T *soundcb;
377 char buf[32];
378 MCIERROR err;
379
Yegappan Lakshmanan5bca9062021-07-24 21:33:26 +0200380 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
381 return;
382
Bram Moolenaar9b283522019-06-17 22:19:33 +0200383 esc = vim_strsave_shellescape(tv_get_string(&argvars[0]), FALSE, FALSE);
384
385 len = STRLEN(esc) + 5 + 18 + 1;
386 p = alloc(len);
387 if (p == NULL)
388 {
389 free(esc);
390 return;
391 }
392 vim_snprintf((char *)p, len, "open %s alias sound%06ld", esc, newid);
393 free(esc);
394
395 wp = enc_to_utf16((char_u *)p, NULL);
396 free(p);
397 if (wp == NULL)
398 return;
399
400 err = mciSendStringW(wp, NULL, 0, sound_window());
401 free(wp);
402 if (err != 0)
403 return;
404
405 vim_snprintf(buf, sizeof(buf), "play sound%06ld notify", newid);
406 err = mciSendString(buf, NULL, 0, sound_window());
407 if (err != 0)
408 goto failure;
409
410 sound_id = newid;
411 rettv->vval.v_number = sound_id;
412
413 soundcb = get_sound_callback(&argvars[1]);
414 if (soundcb != NULL)
415 {
416 vim_snprintf(buf, sizeof(buf), "sound%06ld", newid);
417 soundcb->snd_id = newid;
418 soundcb->snd_device_id = mciGetDeviceID(buf);
419 }
420 return;
421
422failure:
423 vim_snprintf(buf, sizeof(buf), "close sound%06ld", newid);
424 mciSendString(buf, NULL, 0, NULL);
425}
426
427 void
428f_sound_stop(typval_T *argvars, typval_T *rettv UNUSED)
429{
430 long id = tv_get_number(&argvars[0]);
431 char buf[32];
432
433 vim_snprintf(buf, sizeof(buf), "stop sound%06ld", id);
434 mciSendString(buf, NULL, 0, NULL);
435}
436
437 void
438f_sound_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
439{
Bram Moolenaar1b33bee2019-09-07 14:50:49 +0200440 PlaySoundW(NULL, NULL, 0);
Bram Moolenaar9b283522019-06-17 22:19:33 +0200441 mciSendString("close all", NULL, 0, NULL);
442}
443
444# if defined(EXITFREE)
445 void
446sound_free(void)
447{
448 CloseWindow(g_hWndSound);
449
450 while (first_callback != NULL)
451 delete_sound_callback(first_callback);
452}
453# endif
454
455#endif // MSWIN
456
457#endif // FEAT_SOUND