Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
5.9 KiB

  1. /*
  2. sound.c
  3. Sound menu stuff
  4. Puts up a list of all the sounds in the [sounds]
  5. section of win.ini and plays the selected one
  6. */
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <windows.h>
  10. #include <commdlg.h>
  11. #include "PlaySnd.h"
  12. #define KEYBUFSIZE 2048
  13. LONG SoundDlgProc(HWND hDlg, UINT msg, DWORD wParam, LONG lParam);
  14. typedef struct SoundNameAlias {
  15. LPSTR pName;
  16. UINT alias;
  17. } SOUNDNAMEALIAS;
  18. static SOUNDNAMEALIAS sss[] = {"System"};
  19. static SOUNDNAMEALIAS SNA[] = {
  20. "SystemAsterisk", sndAlias('S', '*') ,
  21. "SystemQuestion", sndAlias('S', '?') ,
  22. "SystemHand", sndAlias('S', 'H') ,
  23. "SystemExit", sndAlias('S', 'E') ,
  24. "SystemStartup", sndAlias('S', 'S') ,
  25. "SystemWelcome", sndAlias('S', 'W') ,
  26. "SystemExclamation", sndAlias('S', '!') ,
  27. "SystemDefault", sndAlias('S', 'D') };
  28. #define NUMSYSTEMSOUNDS (sizeof(SNA)/sizeof(SOUNDNAMEALIAS))
  29. UINT TranslateNameToAlias(LPSTR name)
  30. {
  31. UINT n;
  32. for (n=0; n<NUMSYSTEMSOUNDS; ++n) {
  33. if (!lstrcmpi(name, SNA[n].pName)) {
  34. return(SNA[n].alias);
  35. }
  36. }
  37. return(0);
  38. }
  39. void Sounds(HWND hWnd)
  40. {
  41. WinEval(DialogBox(ghModule, MAKEINTRESOURCE(IDD_SOUNDDLG) // "SoundDlg"
  42. , hWnd, (DLGPROC)SoundDlgProc) != -1);
  43. }
  44. void PlaySelection(HWND hDlg)
  45. {
  46. DWORD dwSel;
  47. DWORD dwFlags = SND_ASYNC | bNoDefault | SND_NOSTOP;
  48. char name[40];
  49. UINT alias;
  50. LPSTR pName = name;
  51. BOOL fSync;
  52. BOOL fWait;
  53. dwSel = SendDlgItemMessage(hDlg, IDSND_LIST, LB_GETCURSEL, 0, 0);
  54. if (dwSel != LB_ERR) {
  55. SendDlgItemMessage(hDlg, IDSND_LIST,
  56. LB_GETTEXT,
  57. (WPARAM)dwSel,
  58. (LPARAM)name);
  59. dwFlags = SND_ALIAS;
  60. fSync = (BOOL)SendDlgItemMessage(hDlg, IDSND_SYNC, BM_GETCHECK, 0, 0);
  61. if (fSync) {
  62. // Turn off ASYNC
  63. dwFlags &= ~SND_ASYNC;
  64. } else {
  65. dwFlags |= SND_ASYNC;
  66. }
  67. fWait = (BOOL)SendDlgItemMessage(hDlg, IDSND_WAIT, BM_GETCHECK, 0, 0);
  68. if (!fWait) dwFlags |= SND_NOWAIT;
  69. if (1 == SendDlgItemMessage(hDlg, IDSND_IDPLAY, BM_GETCHECK, 0, 0)) {
  70. if (0 != (alias = TranslateNameToAlias(name))) {
  71. dwFlags |= SND_ALIAS_ID;
  72. pName = (LPSTR)alias;
  73. }
  74. }
  75. if (!PlaySound(pName, NULL, dwFlags | bNoDefault)) {
  76. Error("Failed to play alias: %s", name);
  77. }
  78. }
  79. }
  80. VOID SetSoundName(HWND hDlg)
  81. {
  82. DWORD dwSel;
  83. char name[40];
  84. char filename[MAX_PATH];
  85. UINT n;
  86. dwSel = SendDlgItemMessage(hDlg, IDSND_LIST, LB_GETCURSEL, 0, 0);
  87. if (dwSel != LB_ERR
  88. && SendDlgItemMessage(hDlg, IDSND_LIST,
  89. LB_GETTEXT,
  90. (WPARAM)dwSel,
  91. (LPARAM)name)
  92. && (n=GetProfileString("SOUNDS", name, NULL, filename, sizeof(filename)))
  93. ) {
  94. do {
  95. n--;
  96. if (filename[n] == ',') {
  97. filename[n]=0;
  98. }
  99. } while (filename[n]);
  100. GetFileTitle(filename, name, sizeof(name));
  101. SendDlgItemMessage(hDlg, IDSND_SOUNDNAME, WM_SETTEXT, 0, (LPARAM)name);
  102. } else {
  103. SendDlgItemMessage(hDlg, IDSND_SOUNDNAME, WM_SETTEXT, 0, (LPARAM)"<no selection>");
  104. }
  105. }
  106. LONG SoundDlgProc(HWND hDlg, UINT msg, DWORD wParam, LONG lParam)
  107. {
  108. LPSTR lpBuf, lpKey;
  109. DWORD dwChars;
  110. // dprintf4(("SoundDlgProc: %8.8XH, %8.8XH, %8.8XH", msg, wParam, lParam));
  111. switch (msg) {
  112. case WM_INITDIALOG:
  113. // fill the listbox with the keys in the [sounds]
  114. // section of win.ini
  115. // allocate a buffer to put the keys in
  116. WinEval(lpBuf = (LPSTR) GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, KEYBUFSIZE));
  117. // get the key list
  118. dwChars = GetProfileString("sounds", NULL, "", lpBuf, KEYBUFSIZE);
  119. dprintf4(("%lu chars read from [sounds] section", dwChars));
  120. if (dwChars > 0) {
  121. // add each entry to the list box
  122. lpKey = lpBuf;
  123. while (*lpKey) {
  124. // TEMPORARY SECTION
  125. if (lstrcmpi((LPCTSTR)lpKey, "enable")) // if not ENABLE
  126. // END OF TEMPORARY SECTION
  127. SendMessage(GetDlgItem(hDlg, IDSND_LIST),
  128. LB_ADDSTRING,
  129. 0,
  130. (LONG)lpKey);
  131. lpKey += strlen(lpKey) + 1;
  132. }
  133. } else {
  134. // show there aren't any
  135. SendMessage(GetDlgItem(hDlg, IDSND_LIST),
  136. LB_ADDSTRING,
  137. 0,
  138. (LONG)(LPSTR)"[none]");
  139. }
  140. SendDlgItemMessage(hDlg, IDSND_SOUNDNAME, WM_SETTEXT, 0, (LPARAM)"");
  141. if (bSync) {
  142. // Set the initial state of the Sync checkbox from global flag
  143. SendDlgItemMessage(hDlg, IDSND_SYNC, BM_SETCHECK, 1, 0);
  144. }
  145. if (!bNoWait) {
  146. // Set the initial state of the Wait checkbox from global flag
  147. SendDlgItemMessage(hDlg, IDSND_WAIT, BM_SETCHECK, 1, 0);
  148. }
  149. GlobalFree((HANDLE)lpBuf);
  150. // disable the play button till we get a selection
  151. EnableWindow(GetDlgItem(hDlg, IDSND_PLAY), FALSE);
  152. break;
  153. case WM_COMMAND:
  154. dprintf4(("WM_COMMAND: %08lXH, %08lX", wParam, lParam));
  155. switch (LOWORD(wParam)) {
  156. case IDOK:
  157. EndDialog(hDlg, TRUE);
  158. break;
  159. case IDSND_PLAY:
  160. // get the current selection and try to play it
  161. PlaySelection(hDlg);
  162. break;
  163. case IDSND_LIST:
  164. switch (HIWORD(wParam)){
  165. case LBN_SELCHANGE:
  166. // enable the play button
  167. EnableWindow(GetDlgItem(hDlg, IDSND_PLAY), TRUE);
  168. dprintf3(("Play button enabled"));
  169. // Set the current sound name
  170. SetSoundName(hDlg);
  171. break;
  172. case LBN_DBLCLK:
  173. PlaySelection(hDlg);
  174. break;
  175. default:
  176. break;
  177. }
  178. break;
  179. default:
  180. break;
  181. }
  182. break;
  183. default:
  184. return FALSE; // say we didn't handle it
  185. break;
  186. }
  187. return TRUE; // say we handled it
  188. }