Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 1 | /* 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 Moolenaar | 9b28352 | 2019-06-17 22:19:33 +0200 | [diff] [blame] | 16 | #if defined(FEAT_SOUND) || defined(PROTO) |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 17 | |
| 18 | static long sound_id = 0; |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 19 | |
| 20 | typedef struct soundcb_S soundcb_T; |
| 21 | |
| 22 | struct soundcb_S { |
| 23 | callback_T snd_callback; |
Bram Moolenaar | 9b28352 | 2019-06-17 22:19:33 +0200 | [diff] [blame] | 24 | #ifdef MSWIN |
| 25 | MCIDEVICEID snd_device_id; |
| 26 | long snd_id; |
| 27 | #endif |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 28 | soundcb_T *snd_next; |
| 29 | }; |
| 30 | |
| 31 | static soundcb_T *first_callback = NULL; |
| 32 | |
Bram Moolenaar | 28e67e0 | 2019-08-15 23:05:49 +0200 | [diff] [blame] | 33 | /* |
| 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 |
| 38 | has_any_sound_callback(void) |
| 39 | { |
| 40 | return first_callback != NULL; |
| 41 | } |
| 42 | |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 43 | static soundcb_T * |
| 44 | get_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 |
| 71 | delete_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 Moolenaar | 9b28352 | 2019-06-17 22:19:33 +0200 | [diff] [blame] | 89 | #if defined(HAVE_CANBERRA) || defined(PROTO) |
| 90 | |
| 91 | /* |
| 92 | * Sound implementation for Linux/Unix/Mac using libcanberra. |
| 93 | */ |
| 94 | # include <canberra.h> |
| 95 | |
| 96 | static ca_context *context = NULL; |
| 97 | |
Bram Moolenaar | 28e67e0 | 2019-08-15 23:05:49 +0200 | [diff] [blame] | 98 | // Structure to store info about a sound callback to be invoked soon. |
| 99 | typedef struct soundcb_queue_S soundcb_queue_T; |
| 100 | |
| 101 | struct 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. |
| 109 | static 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 Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 116 | static void |
| 117 | sound_callback( |
| 118 | ca_context *c UNUSED, |
| 119 | uint32_t id, |
| 120 | int error_code, |
| 121 | void *userdata) |
| 122 | { |
Bram Moolenaar | 28e67e0 | 2019-08-15 23:05:49 +0200 | [diff] [blame] | 123 | soundcb_T *soundcb = (soundcb_T *)userdata; |
| 124 | soundcb_queue_T *scb; |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 125 | |
Bram Moolenaar | 28e67e0 | 2019-08-15 23:05:49 +0200 | [diff] [blame] | 126 | 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 Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 133 | : error_code == CA_ERROR_CANCELED |
| 134 | || error_code == CA_ERROR_DESTROYED |
| 135 | ? 1 : 2; |
Bram Moolenaar | 28e67e0 | 2019-08-15 23:05:49 +0200 | [diff] [blame] | 136 | scb->scb_callback = soundcb; |
| 137 | } |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 138 | |
Bram Moolenaar | 28e67e0 | 2019-08-15 23:05:49 +0200 | [diff] [blame] | 139 | /* |
| 140 | * Return TRUE if there is a sound callback to be called. |
| 141 | */ |
| 142 | int |
| 143 | has_sound_callback_in_queue(void) |
| 144 | { |
| 145 | return callback_queue != NULL; |
| 146 | } |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 147 | |
Bram Moolenaar | 28e67e0 | 2019-08-15 23:05:49 +0200 | [diff] [blame] | 148 | /* |
| 149 | * Invoke queued sound callbacks. |
| 150 | */ |
| 151 | void |
| 152 | invoke_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 Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 175 | redraw_after_callback(TRUE); |
| 176 | } |
| 177 | |
| 178 | static void |
| 179 | sound_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 |
| 225 | f_sound_playevent(typval_T *argvars, typval_T *rettv) |
| 226 | { |
| 227 | sound_play_common(argvars, rettv, FALSE); |
| 228 | } |
| 229 | |
Bram Moolenaar | 3ff5f0f | 2019-06-10 13:11:22 +0200 | [diff] [blame] | 230 | /* |
| 231 | * implementation of sound_playfile({path} [, {callback}]) |
| 232 | */ |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 233 | void |
| 234 | f_sound_playfile(typval_T *argvars, typval_T *rettv) |
| 235 | { |
| 236 | sound_play_common(argvars, rettv, TRUE); |
| 237 | } |
| 238 | |
Bram Moolenaar | 3ff5f0f | 2019-06-10 13:11:22 +0200 | [diff] [blame] | 239 | /* |
| 240 | * implementation of sound_stop({id}) |
| 241 | */ |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 242 | void |
| 243 | f_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 Moolenaar | 3ff5f0f | 2019-06-10 13:11:22 +0200 | [diff] [blame] | 249 | /* |
| 250 | * implementation of sound_clear() |
| 251 | */ |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 252 | void |
Bram Moolenaar | 3ff5f0f | 2019-06-10 13:11:22 +0200 | [diff] [blame] | 253 | f_sound_clear(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 254 | { |
| 255 | if (context != NULL) |
| 256 | { |
| 257 | ca_context_destroy(context); |
| 258 | context = NULL; |
| 259 | } |
| 260 | } |
| 261 | |
Bram Moolenaar | 9b28352 | 2019-06-17 22:19:33 +0200 | [diff] [blame] | 262 | # if defined(EXITFREE) || defined(PROTO) |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 263 | void |
| 264 | sound_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 Moolenaar | 9b28352 | 2019-06-17 22:19:33 +0200 | [diff] [blame] | 271 | # endif |
Bram Moolenaar | 427f5b6 | 2019-06-09 13:43:51 +0200 | [diff] [blame] | 272 | |
Bram Moolenaar | 9b28352 | 2019-06-17 22:19:33 +0200 | [diff] [blame] | 273 | #elif defined(MSWIN) |
| 274 | |
| 275 | /* |
| 276 | * Sound implementation for MS-Windows. |
| 277 | */ |
| 278 | |
| 279 | static HWND g_hWndSound = NULL; |
| 280 | |
| 281 | static LRESULT CALLBACK |
| 282 | sound_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 Moolenaar | 9b28352 | 2019-06-17 22:19:33 +0200 | [diff] [blame] | 294 | 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 Moolenaar | b212906 | 2019-08-03 18:31:11 +0200 | [diff] [blame] | 308 | call_callback(&p->snd_callback, -1, &rettv, 2, argv); |
Bram Moolenaar | 9b28352 | 2019-06-17 22:19:33 +0200 | [diff] [blame] | 309 | 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 |
| 322 | sound_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 |
| 338 | f_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 |
| 356 | f_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 | |
| 408 | failure: |
| 409 | vim_snprintf(buf, sizeof(buf), "close sound%06ld", newid); |
| 410 | mciSendString(buf, NULL, 0, NULL); |
| 411 | } |
| 412 | |
| 413 | void |
| 414 | f_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 |
| 424 | f_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 |
| 432 | sound_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 |