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.

531 lines
14 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: commands.c
  3. *
  4. * Executes the users commands.
  5. *
  6. *
  7. * Created: 18-11-93
  8. * Author: Stephen Estrop [StephenE]
  9. *
  10. * Copyright (c) 1993 Microsoft Corporation
  11. \**************************************************************************/
  12. #pragma warning( once : 4201 4214 )
  13. #define NOOLE
  14. #include <windows.h> /* required for all Windows applications */
  15. #include <windowsx.h>
  16. #include <string.h>
  17. #include <malloc.h>
  18. #include <stdarg.h>
  19. #include <stdio.h>
  20. #include "resource.h"
  21. #include "cdplayer.h"
  22. #include "ledwnd.h"
  23. #include "cdapi.h"
  24. #include "scan.h"
  25. #include "trklst.h"
  26. #include "database.h"
  27. #include "commands.h"
  28. #include "diskinfo.h"
  29. /******************************Public*Routine******************************\
  30. * CdPlayerEjectCmd
  31. *
  32. *
  33. *
  34. * History:
  35. * 18-11-93 - StephenE - Created
  36. *
  37. \**************************************************************************/
  38. void
  39. CdPlayerEjectCmd(
  40. void
  41. )
  42. {
  43. if (g_State & CD_PAUSED) {
  44. g_State &= (~(CD_PAUSED_AND_MOVED | CD_PAUSED));
  45. }
  46. if (g_State & CD_PLAYING) {
  47. g_State &= ~CD_PLAYING;
  48. g_State |= CD_STOPPED;
  49. }
  50. if (EjectTheCdromDisc(g_CurrCdrom)) {
  51. g_State = (CD_NO_CD | CD_STOPPED);
  52. SetPlayButtonsEnableState();
  53. TimeAdjustInitialize( g_CurrCdrom );
  54. }
  55. }
  56. /******************************Public*Routine******************************\
  57. * CdPlayerPlayCmd
  58. *
  59. *
  60. *
  61. * History:
  62. * 18-11-93 - StephenE - Created
  63. *
  64. \**************************************************************************/
  65. void
  66. CdPlayerPlayCmd(
  67. void
  68. )
  69. {
  70. /*
  71. ** If we can't lock all the cdrom devices
  72. ** don't do anything. The user will have press
  73. ** the play button again.
  74. */
  75. if ( LockALLTableOfContents() ) {
  76. if (g_State & CD_LOADED ) {
  77. if ((g_State & CD_STOPPED) && PlayCurrTrack( g_CurrCdrom )) {
  78. g_State &= ~CD_STOPPED;
  79. g_State |= CD_PLAYING;
  80. }
  81. else if ( g_State & CD_PAUSED ) {
  82. if ( g_State & CD_PAUSED_AND_MOVED ) {
  83. g_State &= ~CD_PAUSED_AND_MOVED;
  84. g_State |= CD_PLAYING;
  85. if ( SeekToCurrSecond( g_CurrCdrom ) ) {
  86. g_State &= ~CD_PAUSED;
  87. g_State |= CD_PLAYING;
  88. }
  89. else {
  90. g_State &= ~CD_PLAYING;
  91. }
  92. }
  93. else if ( ResumeTheCdromDrive( g_CurrCdrom ) ) {
  94. g_State &= ~CD_PAUSED;
  95. g_State |= CD_PLAYING;
  96. }
  97. UpdateDisplay( DISPLAY_UPD_LED );
  98. }
  99. }
  100. else {
  101. #if DBG
  102. dprintf( "Failing play because NO disc loaded" );
  103. #endif
  104. }
  105. SetPlayButtonsEnableState();
  106. SetFocus( g_hwndControls[INDEX(IDM_PLAYBAR_PAUSE)] );
  107. }
  108. }
  109. /******************************Public*Routine******************************\
  110. * CdPlayerPauseCmd
  111. *
  112. *
  113. *
  114. * History:
  115. * 18-11-93 - StephenE - Created
  116. *
  117. \**************************************************************************/
  118. void
  119. CdPlayerPauseCmd(
  120. void
  121. )
  122. {
  123. /*
  124. ** If we can't lock all the cdrom devices
  125. ** don't do anything. The user will have press
  126. ** the pause button again.
  127. */
  128. if ( LockALLTableOfContents() ) {
  129. if ( g_State & CD_PLAYING ) {
  130. if ( PauseTheCdromDrive( g_CurrCdrom ) ) {
  131. g_State &= ~CD_PLAYING;
  132. g_State |= CD_PAUSED;
  133. }
  134. }
  135. else if ( g_State & CD_PAUSED ) {
  136. if ( g_State & CD_PAUSED_AND_MOVED ) {
  137. g_State &= ~CD_PAUSED_AND_MOVED;
  138. g_State |= CD_PLAYING;
  139. if ( SeekToCurrSecond( g_CurrCdrom ) ) {
  140. g_State &= ~CD_PAUSED;
  141. g_State |= CD_PLAYING;
  142. }
  143. else {
  144. g_State &= ~CD_PLAYING;
  145. }
  146. }
  147. else if ( ResumeTheCdromDrive( g_CurrCdrom ) ) {
  148. g_State &= ~CD_PAUSED;
  149. g_State |= CD_PLAYING;
  150. }
  151. UpdateDisplay( DISPLAY_UPD_LED );
  152. }
  153. SetPlayButtonsEnableState();
  154. SetFocus( g_hwndControls[INDEX(IDM_PLAYBAR_PLAY)] );
  155. }
  156. }
  157. /******************************Public*Routine******************************\
  158. * CdPlayerStopCmd
  159. *
  160. *
  161. *
  162. * History:
  163. * 18-11-93 - StephenE - Created
  164. *
  165. \**************************************************************************/
  166. void
  167. CdPlayerStopCmd(
  168. void
  169. )
  170. {
  171. /*
  172. ** If we can't lock all the cdrom devices
  173. ** don't do anything. The user will have press
  174. ** the stop button again.
  175. */
  176. if ( LockALLTableOfContents() ) {
  177. BOOL bPlaying, bPaused;
  178. bPlaying = g_State & CD_PLAYING;
  179. bPaused = g_State & CD_PAUSED;
  180. if ( (bPlaying || bPaused) && StopTheCdromDrive(g_CurrCdrom) ) {
  181. g_State &= ~(bPlaying ? CD_PLAYING : CD_PAUSED);
  182. g_State |= CD_STOPPED;
  183. /*
  184. ** Stop the current play operation and seek to the first
  185. ** playable track.
  186. */
  187. CURRTRACK(g_CurrCdrom) = FindFirstTrack( g_CurrCdrom );
  188. TimeAdjustSkipToTrack( g_CurrCdrom, CURRTRACK(g_CurrCdrom) );
  189. UpdateDisplay( DISPLAY_UPD_LED | DISPLAY_UPD_TRACK_TIME |
  190. DISPLAY_UPD_TRACK_NAME );
  191. SetPlayButtonsEnableState();
  192. // Stop it again!!!
  193. // This is to prevent a strange bug with NEC 4x,6x players
  194. // Where a SEEK command in TimeAdjustSkipToTrack causes
  195. // The ejection mechanism to get locked.
  196. StopTheCdromDrive (g_CurrCdrom);
  197. /*
  198. ** The lines below are to fix a strange bug whereby the first
  199. ** time we set the focus after starting cdplayer and cdplayer is
  200. ** not the foreground window at time of the SetFocus cdplayer
  201. ** jumps to the foreground. Most annoying!
  202. */
  203. if (GetForegroundWindow() == g_hwndApp) {
  204. SetFocus( g_hwndControls[INDEX(IDM_PLAYBAR_PLAY)] );
  205. }
  206. }
  207. }
  208. }
  209. /******************************Public*Routine******************************\
  210. * CdPlayerPrevTrackCmd
  211. *
  212. *
  213. *
  214. * History:
  215. * 18-11-93 - StephenE - Created
  216. *
  217. \**************************************************************************/
  218. void
  219. CdPlayerPrevTrackCmd(
  220. void
  221. )
  222. {
  223. /*
  224. ** If we can't lock all the cdrom devices
  225. ** don't do anything. The user will have press
  226. ** the previous track button again.
  227. */
  228. if ( LockALLTableOfContents() ) {
  229. DWORD dwOldState;
  230. int i, j;
  231. PTRACK_PLAY tr;
  232. if ( (CDTIME(g_CurrCdrom).TrackCurSec == 0)
  233. && (CDTIME(g_CurrCdrom).TrackCurMin == 0) ) {
  234. dwOldState = g_State;
  235. i = g_CurrCdrom;
  236. tr = FindPrevTrack( g_CurrCdrom, g_fContinuous );
  237. if ( tr == NULL ) {
  238. /*
  239. ** If we were Paused or Playing fake a press on the
  240. ** "stop" button
  241. */
  242. if ( g_State & (CD_PLAYING | CD_PAUSED) ) {
  243. SendMessage( g_hwndControls[INDEX(IDM_PLAYBAR_STOP)],
  244. WM_LBUTTONDOWN, 0, 0L );
  245. SendMessage( g_hwndControls[INDEX(IDM_PLAYBAR_STOP)],
  246. WM_LBUTTONUP, 0, 0L );
  247. }
  248. }
  249. else {
  250. TimeAdjustSkipToTrack( g_CurrCdrom, tr );
  251. if ( (i != g_CurrCdrom) && (dwOldState & CD_PLAYING) ) {
  252. j = g_CurrCdrom;
  253. g_CurrCdrom = i;
  254. SwitchToCdrom( j, FALSE );
  255. SendMessage( g_hwndControls[INDEX(IDM_PLAYBAR_PLAY)],
  256. WM_LBUTTONDOWN, 0, 0L );
  257. SendMessage( g_hwndControls[INDEX(IDM_PLAYBAR_PLAY)],
  258. WM_LBUTTONUP, 0, 0L );
  259. }
  260. }
  261. }
  262. else {
  263. TimeAdjustSkipToTrack( g_CurrCdrom, CURRTRACK( g_CurrCdrom ) );
  264. }
  265. }
  266. }
  267. /******************************Public*Routine******************************\
  268. * CdPlayerNextTrackCmd
  269. *
  270. *
  271. *
  272. * History:
  273. * 18-11-93 - StephenE - Created
  274. *
  275. \**************************************************************************/
  276. void
  277. CdPlayerNextTrackCmd(
  278. void
  279. )
  280. {
  281. /*
  282. ** If we can't lock all the cdrom devices
  283. ** don't do anything. The user will have press
  284. ** the next track button again.
  285. */
  286. if ( LockALLTableOfContents() ) {
  287. DWORD dwOldState;
  288. PTRACK_PLAY tr;
  289. dwOldState = g_State;
  290. tr = FindNextTrack( g_fContinuous );
  291. if ( tr == NULL ) {
  292. /*
  293. ** If the CD is playing fake a press on the "stop" button
  294. ** otherwise call the stop function directly.
  295. */
  296. if ( g_State & (CD_PLAYING | CD_PAUSED) ) {
  297. SendMessage( g_hwndControls[INDEX(IDM_PLAYBAR_STOP)],
  298. WM_LBUTTONDOWN, 0, 0L );
  299. SendMessage( g_hwndControls[INDEX(IDM_PLAYBAR_STOP)],
  300. WM_LBUTTONUP, 0, 0L );
  301. }
  302. else {
  303. /*
  304. ** Seek to the first playable track.
  305. */
  306. CURRTRACK(g_CurrCdrom) = FindFirstTrack( g_CurrCdrom );
  307. if (CURRTRACK(g_CurrCdrom) != NULL ) {
  308. TimeAdjustSkipToTrack( g_CurrCdrom,
  309. CURRTRACK(g_CurrCdrom) );
  310. UpdateDisplay( DISPLAY_UPD_LED | DISPLAY_UPD_TRACK_TIME |
  311. DISPLAY_UPD_TRACK_NAME );
  312. SetPlayButtonsEnableState();
  313. SetFocus( g_hwndControls[INDEX(IDM_PLAYBAR_PLAY)] );
  314. }
  315. }
  316. }
  317. else {
  318. if ( g_LastCdrom != g_CurrCdrom) {
  319. SwitchToCdrom( g_CurrCdrom,FALSE);
  320. TimeAdjustSkipToTrack( g_CurrCdrom, tr );
  321. if ( dwOldState & CD_PLAYING ) {
  322. SendMessage( g_hwndControls[INDEX(IDM_PLAYBAR_PLAY)],
  323. WM_LBUTTONDOWN, 0, 0L );
  324. SendMessage( g_hwndControls[INDEX(IDM_PLAYBAR_PLAY)],
  325. WM_LBUTTONUP, 0, 0L );
  326. }
  327. }
  328. else {
  329. TimeAdjustSkipToTrack( g_CurrCdrom, tr );
  330. }
  331. }
  332. }
  333. }
  334. /******************************Public*Routine******************************\
  335. * CdPlayerSeekCmd
  336. *
  337. * How Seek works.
  338. *
  339. * When the user presses a seek button (forwards or backwards) this function
  340. * gets called. The cdplayer can be in three possible states:
  341. *
  342. * 1. Playing
  343. * 2. Paused
  344. * 3. Stopped
  345. *
  346. * In state 1 (playing) we pause the device and alter its global state flags
  347. * to show that it was playing (CD_WAS_PLAYING). This is so that we can
  348. * return to the playing state when the user stops seeking.
  349. *
  350. * We then start a timer. We always call the timer function, this means
  351. * that we will allways skip one second no matter how quickly the user
  352. * clicks the seek button. The timer just increments (or decrements) the
  353. * current play position according to the currrent play list. When the users
  354. * releases the seek button we stop the timer and resume playing if the
  355. * CD_WAS_PLAYING flag was set.
  356. *
  357. * The interesting case is when we try to seek beyond the end of the current
  358. * play list. We detect this by testing the state of CURRTRACK(g_CurrCdrom).
  359. * If NULL we have come to the end of the play list. If we were paused or
  360. * previously playing we fake a stop command by sending a WM_LBUTTONDOWN,
  361. * WM_LBUTTONUP message pair to the stop button. This causes the focus
  362. * to be moved from the currently pressed seek button, which means that
  363. * this function will get called BEFORE the CdPlayerStopCmd function. So
  364. * by the time CdPlayerStopCmd gets called the correct playing state of
  365. * the cdrom device would have been restored by the code below.
  366. *
  367. *
  368. * History:
  369. * 18-11-93 - StephenE - Created
  370. *
  371. \**************************************************************************/
  372. void
  373. CdPlayerSeekCmd(
  374. HWND hwnd,
  375. BOOL fStart,
  376. UINT uDirection
  377. )
  378. {
  379. UINT rc;
  380. BOOL frc;
  381. if ( fStart ) {
  382. if (g_State & CD_PLAYING) {
  383. g_State &= ~CD_PLAYING;
  384. g_State |= CD_WAS_PLAYING;
  385. PauseTheCdromDrive( g_CurrCdrom );
  386. }
  387. g_State |= CD_SEEKING;
  388. rc = SetTimer( hwnd, uDirection, SKIPBEAT_TIMER_RATE,
  389. SkipBeatTimerProc );
  390. ASSERT(rc != 0);
  391. SkipBeatTimerProc( hwnd, WM_TIMER, uDirection, 0L );
  392. }
  393. else {
  394. g_State &= ~CD_SEEKING;
  395. if (g_State & CD_WAS_PLAYING) {
  396. g_State &= ~CD_WAS_PLAYING;
  397. g_State |= CD_PLAYING;
  398. }
  399. if ( g_State & CD_PAUSED ) {
  400. g_State |= CD_PAUSED_AND_MOVED;
  401. }
  402. else {
  403. SeekToCurrSecond( g_CurrCdrom );
  404. }
  405. frc = KillTimer( hwnd, uDirection );
  406. ASSERT(frc != FALSE);
  407. }
  408. }
  409. /******************************Public*Routine******************************\
  410. * CdDiskInfoDlg
  411. *
  412. *
  413. *
  414. * History:
  415. * dd-mm-93 - StephenE - Created
  416. *
  417. \**************************************************************************/
  418. void
  419. CdDiskInfoDlg(
  420. void
  421. )
  422. {
  423. if (g_State & (CD_BEING_SCANNED | CD_IN_USE | CD_NO_CD | CD_DATA_CD_LOADED)) {
  424. MessageBeep (MB_ICONEXCLAMATION);
  425. } else {
  426. DialogBoxParam( g_hInst, MAKEINTRESOURCE(IDR_DISCINFODLG),
  427. g_hwndApp, DiskInfoDlgProc, (LPARAM)g_CurrCdrom );
  428. }
  429. }