view_cdrom.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. #include <shlwapi.h>
  2. #include "main.h"
  3. #include <windowsx.h>
  4. #include "resource.h"
  5. #include "../nu/DialogSkinner.h"
  6. #include "../nu/ChildSizer.h"
  7. #include "../winamp/wa_ipc.h"
  8. #include "../Winamp/strutil.h"
  9. #include "../nu/AutoChar.h"
  10. #include "../nu/AutoWide.h"
  11. #include "../nu/listview.h"
  12. #include <strsafe.h>
  13. #ifndef LVS_EX_DOUBLEBUFFER
  14. #define LVS_EX_DOUBLEBUFFER 0x00010000
  15. #endif
  16. static INT_PTR WINAPI DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  17. #define TIMER_NOTIFYINFO_ID 1985
  18. #define TIMER_NOTIFYINFO_DELAY 200
  19. HWND CreateCDViewWindow(HWND hwndParent, DM_NOTIFY_PARAM *phdr)
  20. {
  21. return WASABI_API_CREATEDIALOGPARAMW(IDD_VIEW_CDROM, hwndParent, DlgProc, (LPARAM)phdr);
  22. }
  23. void TAG_FMT(void *f, void *ff, void *p, char *out, int out_len)
  24. {
  25. waFormatTitle fmt;
  26. fmt.out = out;
  27. fmt.out_len = out_len;
  28. fmt.p = p;
  29. fmt.spec = 0;
  30. *(void **)&fmt.TAGFUNC = f;
  31. *(void **)&fmt.TAGFREEFUNC = ff;
  32. *out = 0;
  33. SendMessage(plugin.hwndWinampParent, WM_WA_IPC, (WPARAM)&fmt, IPC_FORMAT_TITLE);
  34. }
  35. static wchar_t m_artist[128], m_album[128], m_genre[128], m_year[32];
  36. static int m_start_immediate_extract = 0;
  37. static int m_atracks, m_dtracks;
  38. void CopyComment(wchar_t *&dest, wchar_t *comments)
  39. {
  40. if (comments)
  41. {
  42. int numCarriageReturns = 0;
  43. wchar_t *commentCursor = comments;
  44. while (commentCursor && *commentCursor)
  45. {
  46. if (*commentCursor == '\r')
  47. {
  48. commentCursor++;
  49. if (*commentCursor == '\n')
  50. commentCursor++;
  51. }
  52. if (*commentCursor == '\n')
  53. numCarriageReturns++;
  54. commentCursor = CharNext(commentCursor);
  55. }
  56. size_t size = commentCursor - comments;
  57. dest = (wchar_t *)calloc((size + numCarriageReturns + 1), sizeof(wchar_t));
  58. wchar_t *destCursor = dest;
  59. commentCursor = comments;
  60. while (commentCursor && *commentCursor)
  61. {
  62. if (*commentCursor == '\r')
  63. {
  64. *destCursor++ = *commentCursor++;
  65. if (*commentCursor == '\n')
  66. *destCursor++ = *commentCursor++;
  67. }
  68. if (*commentCursor == '\n')
  69. *destCursor++ = '\r';
  70. wchar_t *next = CharNextW(commentCursor);
  71. while (commentCursor != next)
  72. {
  73. *destCursor++ = *commentCursor++;
  74. }
  75. }
  76. *destCursor = 0;
  77. }
  78. else
  79. dest = 0;
  80. }
  81. static void extractFiles(HWND hwndDlg, CHAR cLetter, int all)
  82. {
  83. HWND hwndList;
  84. hwndList = GetDlgItem(hwndDlg, IDC_LIST2);
  85. char cMode;
  86. wchar_t title[32] = {0}, buf[512] = {0};
  87. INT msgTextId, l, i, cnt = 0;
  88. cdrip_params *p;
  89. wchar_t info[65536] = {0};
  90. LVITEMW lvitem = {0};
  91. cMode = DriveManager_GetDriveMode(cLetter);
  92. switch (cMode)
  93. {
  94. case DM_MODE_BURNING: msgTextId = IDS_ERROR_CD_RIP_IN_PROGRESS; break;
  95. case DM_MODE_RIPPING: msgTextId = IDS_ERROR_CD_BURN_IN_PROGRESS; break;
  96. default: msgTextId = 0; break;
  97. }
  98. if (msgTextId)
  99. {
  100. MessageBox(hwndDlg, WASABI_API_LNGSTRINGW(msgTextId), WASABI_API_LNGSTRINGW_BUF(IDS_CD_RIP,title,32), 0);
  101. return ;
  102. }
  103. if (m_dtracks && !m_atracks)
  104. {
  105. MessageBox(hwndDlg, WASABI_API_LNGSTRINGW(IDS_ERROR_CANNOT_EXTRACT_DATA_CDS),
  106. WASABI_API_LNGSTRINGW_BUF(IDS_CD_RIP,title,32), 0);
  107. return ;
  108. }
  109. l = (hwndList) ? ListView_GetItemCount(hwndList) : 0;
  110. if (l)
  111. {
  112. p = (cdrip_params *)calloc(1, sizeof(cdrip_params));
  113. if (!p) return;
  114. p->ntracks = l;
  115. p->tracks = (wchar_t **)calloc(sizeof(wchar_t*), p->ntracks);
  116. p->trackArtists = (wchar_t **)calloc(sizeof(wchar_t*), p->ntracks);
  117. p->composers = (wchar_t **)calloc(sizeof(wchar_t*), p->ntracks);
  118. p->gracenoteFileIDs = (wchar_t **)calloc(sizeof(wchar_t*), p->ntracks);
  119. p->gracenoteExtData = (wchar_t **)calloc(sizeof(wchar_t*), p->ntracks);
  120. p->conductors = (wchar_t **)calloc(sizeof(wchar_t*), p->ntracks);
  121. p->lengths = (int *)calloc(sizeof(int), p->ntracks);
  122. }
  123. else p = NULL;
  124. lvitem.mask = LVIF_TEXT;
  125. lvitem.iSubItem = 3;
  126. lvitem.cchTextMax = sizeof(buf)/sizeof(char);
  127. for (i = 0;i < l;i++)
  128. {
  129. if (all || (LVIS_SELECTED & ListView_GetItemState(hwndList, i, LVIS_SELECTED)))
  130. {
  131. wchar_t cdFilename[MAX_PATH] = {0};
  132. StringCchPrintfW(cdFilename, MAX_PATH, L"cda://%c,%d.cda", cLetter, i + 1);
  133. //check if track is Data track
  134. {
  135. wchar_t buf2[512] = L"";
  136. getFileInfoW(cdFilename, L"tracktype", buf2, sizeof(buf2)/sizeof(*buf2));
  137. if (lstrcmpiW(buf2, L"audio")) continue; //skip it
  138. }
  139. lvitem.iItem = i;
  140. lvitem.pszText = buf;
  141. SendMessageW(hwndList, LVM_GETITEMTEXTW, i, (LPARAM)&lvitem);
  142. int len = _wtoi(lvitem.pszText) * 60 + _wtoi(wcsstr(lvitem.pszText, L":") + 1); //such hackish :)
  143. p->total_length_bytes += len * 44100 * 4;
  144. getFileInfoW(cdFilename, L"title", info, sizeof(info)/sizeof(wchar_t));
  145. p->tracks[i] = _wcsdup(info);
  146. getFileInfoW(cdFilename, L"artist", info, sizeof(info)/sizeof(wchar_t));
  147. p->trackArtists[i] = _wcsdup(info);
  148. getFileInfoW(cdFilename, L"composer", info, sizeof(info)/sizeof(wchar_t));
  149. p->composers[i] = _wcsdup(info);
  150. getFileInfoW(cdFilename, L"conductor", info, sizeof(info)/sizeof(wchar_t));
  151. p->conductors[i] = _wcsdup(info);
  152. getFileInfoW(cdFilename, L"GracenoteFileID", info, sizeof(info)/sizeof(wchar_t));
  153. p->gracenoteFileIDs[i] = _wcsdup(info);
  154. getFileInfoW(cdFilename, L"GracenoteExtData", info, sizeof(info)/sizeof(wchar_t));
  155. p->gracenoteExtData[i] = _wcsdup(info);
  156. p->lengths[i] = len;
  157. cnt++;
  158. }
  159. }
  160. if (!cnt)
  161. {
  162. if (p)
  163. {
  164. for (int i = 0 ; i < l; i ++) free(p->tracks[i]);
  165. free(p->tracks);
  166. free(p->trackArtists);
  167. free(p->composers);
  168. free(p->gracenoteFileIDs);
  169. free(p->gracenoteExtData);
  170. free(p->conductors);
  171. free(p->lengths);
  172. free(p);
  173. }
  174. MessageBox(hwndDlg, WASABI_API_LNGSTRINGW(IDS_NO_TRACKS_TO_RIP),
  175. WASABI_API_LNGSTRINGW_BUF(IDS_CD_RIP,title,32), MB_OK);
  176. return ;
  177. }
  178. p->filenames = (wchar_t **)calloc(sizeof(wchar_t *), p->ntracks); // allocate for cdrip to use :)
  179. p->tempFilenames = (wchar_t **)calloc(sizeof(wchar_t *), p->ntracks); // allocate for cdrip to use :)
  180. p->artist = _wcsdup(m_artist);
  181. p->album = _wcsdup(m_album);
  182. p->genre = _wcsdup(m_genre);
  183. p->year = _wcsdup(m_year);
  184. wchar_t name[] = L"cda://X.cda";
  185. name[6] = cLetter;
  186. info[0] = 0;
  187. getFileInfoW(name, L"publisher", info, sizeof(info)/sizeof(wchar_t));
  188. p->publisher = _wcsdup(info);
  189. info[0] = 0;
  190. getFileInfoW(name, L"comment", info, sizeof(info)/sizeof(wchar_t));
  191. CopyComment(p->comment, info);
  192. info[0] = 0;
  193. getFileInfoW(name, L"disc", info, sizeof(info)/sizeof(wchar_t));
  194. p->disc = _wcsdup(info);
  195. p->drive_letter = cLetter;
  196. cdrip_extractFiles(p); // will free p when done with it
  197. }
  198. static void playFiles(HWND hwndDlg, CHAR cLetter, int enqueue, int all)
  199. {
  200. HWND hwndList;
  201. hwndList = GetDlgItem(hwndDlg, IDC_LIST2);
  202. CHAR cMode;
  203. INT msgTextId;
  204. cMode = DriveManager_GetDriveMode(cLetter);
  205. switch (cMode)
  206. {
  207. case DM_MODE_BURNING: msgTextId = IDS_ERROR_CD_RIP_IN_PROGRESS; break;
  208. case DM_MODE_RIPPING: msgTextId = IDS_ERROR_CD_BURN_IN_PROGRESS; break;
  209. default: msgTextId = 0; break;
  210. }
  211. if (msgTextId)
  212. {
  213. wchar_t title[64] = {0};
  214. MessageBox(hwndDlg, WASABI_API_LNGSTRINGW(msgTextId),
  215. WASABI_API_LNGSTRINGW_BUF(IDS_CD_PLAYBACK_ERROR, title, 64), 0);
  216. return ;
  217. }
  218. int cnt = 0;
  219. int l = (hwndList) ? ListView_GetItemCount(hwndList) : 0;
  220. if (enqueue && all == 1024) all = 0;
  221. int firstsel = -1;
  222. char buf[64] = {0}, titlebuf[2048] = {0};
  223. enqueueFileWithMetaStruct s;
  224. LVITEMA lvitem = {0};
  225. lvitem.cchTextMax = sizeof(buf)/sizeof(char);
  226. for (int i = 0;i < l;i++)
  227. {
  228. if (all == 1024 && firstsel < 0 && (LVIS_SELECTED & ListView_GetItemState(hwndList, i, LVIS_SELECTED))) firstsel = i;
  229. if (all || (LVIS_SELECTED & ListView_GetItemState(hwndList, i, LVIS_SELECTED)))
  230. {
  231. lvitem.iItem = i;
  232. lvitem.mask = LVIF_PARAM;
  233. lvitem.iSubItem = 0;
  234. SendMessageW(hwndList, LVM_GETITEMA, 0, (LPARAM)&lvitem);
  235. int a = (INT)(INT_PTR)lvitem.lParam;
  236. if (a > 0)
  237. {
  238. if (!cnt)
  239. {
  240. if (!enqueue) SendMessageW(plugin.hwndWinampParent, WM_WA_IPC, 0, IPC_DELETE);
  241. cnt++;
  242. }
  243. lvitem.mask = LVIF_TEXT;
  244. lvitem.iSubItem = 3;
  245. lvitem.pszText = buf;
  246. SendMessageW(hwndList, LVM_GETITEMTEXTA, i, (LPARAM)&lvitem);
  247. s.length = atoi(lvitem.pszText) * 60 + atoi(strstr(lvitem.pszText, ":") + 1); //such hackish :)
  248. StringCchPrintfA(buf, 64, "cda://%c,%d.cda", cLetter, i + 1);
  249. TAG_FMT(0, 0, buf, titlebuf, sizeof(titlebuf)/sizeof(*titlebuf));
  250. s.filename = buf;
  251. s.title = titlebuf;
  252. s.ext = NULL;
  253. SendMessage(plugin.hwndWinampParent, WM_WA_IPC, (WPARAM)&s, IPC_PLAYFILE);
  254. }
  255. }
  256. }
  257. if (cnt && !enqueue)
  258. {
  259. if (firstsel >= 0)
  260. {
  261. SendMessage(plugin.hwndWinampParent, WM_WA_IPC, firstsel, IPC_SETPLAYLISTPOS);
  262. SendMessage(plugin.hwndWinampParent, WM_COMMAND, 40047, 0); // stop button, literally
  263. SendMessage(plugin.hwndWinampParent, WM_COMMAND, 40045, 0); // play button, literally
  264. }
  265. else SendMessage(plugin.hwndWinampParent, WM_WA_IPC, 0, IPC_STARTPLAY);
  266. }
  267. }
  268. void saveCDToItemRecordList(CHAR cLetter, itemRecordList *obj, char *albumname)
  269. {
  270. char fname[64] = {0}, buf2[64] = {0};
  271. StringCchPrintfA(fname, 64, "cda://%c.cda", cLetter);
  272. getFileInfo(fname, "<begin>", buf2, sizeof(buf2)/sizeof(char));
  273. getFileInfo(fname, "ntracks", buf2, sizeof(buf2)/sizeof(char));
  274. int ntracks = atoi(buf2);
  275. if (ntracks > 0 && ntracks < 256)
  276. {
  277. obj->Items = 0; obj->Alloc = 0; obj->Size = 0;
  278. allocRecordList(obj, ntracks, 0);
  279. int x;
  280. for (x = 0; x < ntracks; x ++)
  281. {
  282. StringCchPrintfA(fname, 64, "cda://%c,%d.cda", cLetter, x + 1);
  283. getFileInfo(fname, "tracktype", buf2, sizeof(buf2)/sizeof(char));
  284. if (!lstrcmpiA(buf2, "audio"))
  285. {
  286. int len = -1;
  287. char titlebuf[FILETITLE_SIZE] = {0};
  288. mediaLibrary.GetFileInfo(fname, titlebuf, FILETITLE_SIZE, &len);
  289. itemRecord *pRec = &obj->Items[obj->Size];
  290. ZeroMemory(pRec, sizeof(itemRecord));
  291. if (titlebuf) pRec->title = _strdup(titlebuf);
  292. pRec->length = len;
  293. if (fname) pRec->filename = _strdup(fname);
  294. obj->Size++;
  295. }
  296. }
  297. }
  298. getFileInfo(fname, "<end>", buf2, sizeof(buf2)/sizeof(char));
  299. }
  300. static ChildWndResizeItem cdromwnd_rlist[] =
  301. {
  302. {IDC_LIST2, 0x0011},
  303. {IDC_CDINFO, 0x0000},
  304. {IDC_CDINFO2, 0x0000},
  305. {IDC_BUTTON_PLAY, 0x0101},
  306. {IDC_BUTTON_ENQUEUE, 0x0101},
  307. {IDC_BUTTON_EXTRACT, 0x0101},
  308. {IDC_BUTTON_EJECT, 0x0101},
  309. {IDC_BTN_SHOWINFO, 0x1111},
  310. };
  311. typedef struct _VIEWCOLUMN
  312. {
  313. INT stringId;
  314. LPTSTR pszConfig;
  315. INT defaultWidth;
  316. } VIEWCOLUMN;
  317. static VIEWCOLUMN viewColumns[] =
  318. {
  319. { IDS_TRACK_NUMBER, TEXT("col_track"), 60 },
  320. { IDS_ARTIST, TEXT("col_artist"), 150 },
  321. { IDS_TITLE, TEXT("col_title"), 200 },
  322. { IDS_LENGTH, TEXT("col_len"), 80 },
  323. };
  324. static char m_cdrom;
  325. typedef struct _APCPARAM
  326. {
  327. HWND hwndDlg;
  328. CHAR cLetter;
  329. INT_PTR user;
  330. }
  331. APCPARAM;
  332. static void CALLBACK APC_GetCracenoteInfo(ULONG_PTR param);
  333. static void GetGracenoteInfo(HWND hwndDlg, CHAR cLetter, HANDLE hThread = NULL)
  334. {
  335. HWND hwndList;
  336. int l, x;
  337. LVITEMW lvitem = {0};
  338. wchar_t buf[32] = {0}, titlebuf[256] = {0};
  339. if (hThread)
  340. {
  341. APCPARAM *pParam = (APCPARAM*)calloc(1, sizeof(APCPARAM));
  342. if (pParam)
  343. {
  344. pParam->cLetter = cLetter;
  345. pParam->hwndDlg = hwndDlg;
  346. if (!QueueUserAPC(APC_GetCracenoteInfo, hThread, (ULONG_PTR)pParam)) free(pParam);
  347. }
  348. return;
  349. }
  350. hwndList = GetDlgItem(hwndDlg, IDC_LIST2);
  351. if (!hwndList) return;
  352. l = (INT)SendMessageW(hwndList, LVM_GETITEMCOUNT, 0,0);
  353. // first, let's try to get artist/album info
  354. {
  355. StringCchPrintfW(buf, 32, L"cda://%c.cda", cLetter);
  356. wchar_t artistbuf[256] = {0}, albumbuf[256] = {0}, yearbuf[256] = {0}, genrebuf[256] = {0};
  357. getFileInfoW(buf, L"albumartist", artistbuf, sizeof(artistbuf) / sizeof(artistbuf[0]));
  358. getFileInfoW(buf, L"album", albumbuf, sizeof(albumbuf) / sizeof(albumbuf[0]));
  359. getFileInfoW(buf, L"genre", genrebuf, sizeof(genrebuf) / sizeof(genrebuf[0]));
  360. getFileInfoW(buf, L"year", yearbuf, sizeof(yearbuf) / sizeof(yearbuf[0]));
  361. wchar_t newbuf[1024] = {0};
  362. lstrcpynW(m_artist, artistbuf, sizeof(m_artist)/sizeof(*m_artist));
  363. lstrcpynW(m_album, albumbuf, sizeof(m_album)/sizeof(*m_album));
  364. lstrcpynW(m_year, yearbuf, sizeof(m_year)/sizeof(*m_year));
  365. lstrcpynW(m_genre, genrebuf, sizeof(m_genre)/sizeof(*m_genre));
  366. StringCchPrintfW(newbuf, 1024, WASABI_API_LNGSTRINGW(IDS_ML_VIEW_ARTIST_ALBUM), artistbuf, albumbuf);
  367. SetDlgItemText(hwndDlg, IDC_CDINFO, newbuf);
  368. StringCchPrintfW(newbuf, 1024, WASABI_API_LNGSTRINGW(IDS_ML_VIEW_YEAR_GENRE), yearbuf, genrebuf);
  369. SetDlgItemText(hwndDlg, IDC_CDINFO2, newbuf);
  370. }
  371. for (x = 0; x < l; x ++)
  372. {
  373. lvitem.iItem = x;
  374. lvitem.mask = LVIF_PARAM;
  375. lvitem.iSubItem = 0;
  376. SendMessageW(hwndList, LVM_GETITEMW, 0, (LPARAM)&lvitem);
  377. int wt = (INT)(INT_PTR)lvitem.lParam;
  378. if (wt > 0)
  379. {
  380. StringCchPrintfW(buf, 32, L"cda://%c,%d.cda", cLetter, x + 1);
  381. lvitem.mask = LVIF_TEXT;
  382. titlebuf[0] = 0;
  383. getFileInfoW(buf, L"title", titlebuf, sizeof(titlebuf) / sizeof(titlebuf[0]));
  384. if (titlebuf[0])
  385. {
  386. lvitem.iSubItem = 2;
  387. lvitem.pszText = titlebuf;
  388. SendMessageW(hwndList, LVM_SETITEMW, 0, (LPARAM)&lvitem);
  389. }
  390. titlebuf[0] = 0;
  391. getFileInfoW(buf, L"artist", titlebuf, sizeof(titlebuf) / sizeof(titlebuf[0]));
  392. if (titlebuf[0])
  393. {
  394. lvitem.iSubItem = 1;
  395. lvitem.pszText = titlebuf;
  396. SendMessageW(hwndList, LVM_SETITEMW, 0, (LPARAM)&lvitem);
  397. }
  398. }
  399. }
  400. if (l && hwndList)
  401. UpdateWindow(hwndList);
  402. if ( Plugin_IsExtractScheduled( cLetter ) )
  403. extractFiles( hwndDlg, cLetter, 1 );
  404. }
  405. static void UpdateCDView(HWND hwndDlg, DM_NOTIFY_PARAM *phdr)
  406. {
  407. HWND hwndList;
  408. m_cdrom = phdr->cLetter;
  409. m_atracks = m_dtracks = 0;
  410. hwndList = GetDlgItem(hwndDlg, IDC_LIST2);
  411. if (!hwndList) return;
  412. ListView_DeleteAllItems(hwndList);
  413. SetDlgItemText(hwndDlg, IDC_CDINFO2, L"");
  414. SendMessageW(hwndList, WM_SETREDRAW, FALSE, 0L);
  415. if (DMOP_MCIINFO == phdr->opCode)
  416. {
  417. DM_MCI_PARAM *pmci;
  418. LVITEMW lvitem = {0};
  419. wchar_t buffer[512] = {0};
  420. INT strid, param;
  421. pmci = (DM_MCI_PARAM*)phdr;
  422. for (int i = 0; i < pmci->nTracks; i++)
  423. {
  424. INT time = (0x7FFFFFFF & pmci->pTracks[i])/1000;
  425. if (0x80000000 & pmci->pTracks[i])
  426. {
  427. param = i + 1; strid = IDS_AUDIO_TRACK; m_atracks++;
  428. }
  429. else
  430. {
  431. param = -1; strid = IDS_DATA_TRACK; m_dtracks++;
  432. }
  433. StringCchPrintfW(buffer, sizeof(buffer)/sizeof(wchar_t), L"%d", i + 1);
  434. lvitem.mask = LVIF_TEXT | LVIF_PARAM;
  435. lvitem.iItem = i;
  436. lvitem.iSubItem = 0;
  437. lvitem.pszText = buffer;
  438. lvitem.lParam = param;
  439. INT index = (INT)SendMessageW(hwndList, LVM_INSERTITEMW, 0, (LPARAM)&lvitem);
  440. if (-1 != index)
  441. {
  442. lvitem.iItem = index;
  443. lvitem.mask = LVIF_TEXT;
  444. lvitem.iSubItem = 2;
  445. lvitem.pszText = WASABI_API_LNGSTRINGW(strid);
  446. SendMessageW(hwndList, LVM_SETITEMW, 0, (LPARAM)&lvitem);
  447. if (time < 0) StringCchCopyW(buffer, sizeof(buffer)/sizeof(wchar_t), L"???");
  448. else StringCchPrintfW(buffer, sizeof(buffer)/sizeof(wchar_t), L"%d:%02d", time / 60, time % 60);
  449. lvitem.iSubItem = 3;
  450. lvitem.pszText = buffer;
  451. SendMessageW(hwndList, LVM_SETITEMW, 0, (LPARAM)&lvitem);
  452. }
  453. }
  454. SetDlgItemText(hwndDlg, IDC_CDINFO, WASABI_API_LNGSTRINGW((m_atracks) ? IDS_CD_AUDIO : ((m_dtracks) ? IDS_DATA_CD : IDS_NO_CD)));
  455. if (m_atracks) GetGracenoteInfo(hwndDlg, phdr->cLetter, GetCurrentThread());
  456. }
  457. else SetDlgItemText(hwndDlg, IDC_CDINFO, WASABI_API_LNGSTRINGW(IDS_NO_CD));
  458. SendMessageW(hwndList, WM_SETREDRAW, TRUE, 0L);
  459. UpdateWindow(hwndList);
  460. }
  461. static LRESULT editCDInfo(HWND hwndDlg, CHAR cLetter, int trackNum)
  462. {
  463. wchar_t name[MAX_PATH] = {0};
  464. if (trackNum)
  465. StringCchPrintfW(name, MAX_PATH, L"cda://%c,%d", cLetter, trackNum);
  466. else
  467. StringCchPrintfW(name, MAX_PATH, L"cda://%c.cda", cLetter);
  468. infoBoxParamW p;
  469. p.filename = name;
  470. p.parent = hwndDlg;
  471. return SendMessageW(plugin.hwndWinampParent, WM_WA_IPC, (WPARAM)&p, IPC_INFOBOXW);
  472. }
  473. static void NotifyInfoWindow(HWND hwnd, CHAR cLetter, INT nTrack, BOOL bForceRefresh)
  474. {
  475. HWND hwndParent;
  476. hwndParent = GetParent(hwnd);
  477. if (hwndParent)
  478. {
  479. wchar_t szFileName[MAX_PATH], *p;
  480. if (nTrack && S_OK == StringCchPrintfW(szFileName, sizeof(szFileName)/sizeof(wchar_t), L"cda://%c,%d.cda", cLetter, nTrack)) p = szFileName;
  481. else p = L"";
  482. SendMessageW(hwndParent, WM_SHOWFILEINFO, (WPARAM)((bForceRefresh) ? WISF_FORCE : WISF_NORMAL), (LPARAM)p);
  483. }
  484. }
  485. static BOOL Window_OnInitDialog(HWND hwndDlg, HWND hwndFocus, LPARAM lParam)
  486. {
  487. HWND hwndList;
  488. if (!lParam)
  489. {
  490. DestroyWindow(hwndDlg);
  491. return 0;
  492. }
  493. SendMessageW(GetParent(hwndDlg), WM_COMMAND, MAKEWPARAM(IDC_BTN_SHOWINFO, BN_EX_GETTEXT), (LPARAM)GetDlgItem(hwndDlg, IDC_BTN_SHOWINFO));
  494. childSizer.Init(hwndDlg, cdromwnd_rlist, sizeof(cdromwnd_rlist) / sizeof(cdromwnd_rlist[0]));
  495. hwndList = GetDlgItem(hwndDlg, IDC_LIST2);
  496. if (hwndList)
  497. {
  498. MLSKINWINDOW sw;
  499. LVCOLUMNW column;
  500. sw.hwndToSkin = hwndList;
  501. sw.skinType = SKINNEDWND_TYPE_LISTVIEW;
  502. sw.style = SWLVS_FULLROWSELECT | SWLVS_DOUBLEBUFFER | SWLVS_ALTERNATEITEMS | SWS_USESKINFONT | SWS_USESKINCOLORS | SWS_USESKINCURSORS;
  503. MLSkinWindow(plugin.hwndLibraryParent, &sw);
  504. column.mask = LVCF_WIDTH | LVCF_TEXT;
  505. for (int i = 0; i < sizeof(viewColumns)/sizeof(VIEWCOLUMN); i++)
  506. {
  507. column.cx = g_view_metaconf->ReadInt(viewColumns[i].pszConfig, viewColumns[i].defaultWidth);
  508. column.pszText = WASABI_API_LNGSTRINGW(viewColumns[i].stringId);
  509. SendMessageW(hwndList, LVM_INSERTCOLUMNW, (WPARAM)0xEFFF, (LPARAM)&column);
  510. }
  511. }
  512. SetDlgItemText(hwndDlg, IDC_CDINFO, L"");
  513. SetDlgItemText(hwndDlg, IDC_CDINFO2, L"");
  514. NotifyInfoWindow(hwndDlg, ((DM_NOTIFY_PARAM*)lParam)->cLetter, NULL, TRUE); // ignore cache
  515. UpdateCDView(hwndDlg, (DM_NOTIFY_PARAM*)lParam);
  516. return FALSE;
  517. }
  518. static void Window_OnDestroy(HWND hwndDlg)
  519. {
  520. HWND hwndList = GetDlgItem(hwndDlg, IDC_LIST2);
  521. if (hwndList)
  522. {
  523. for (int i = 0; i < sizeof(viewColumns)/sizeof(VIEWCOLUMN); i++)
  524. {
  525. g_view_metaconf->WriteInt(viewColumns[i].pszConfig,
  526. (INT)SendMessageW(hwndList, LVM_GETCOLUMNWIDTH, i, 0L));
  527. }
  528. }
  529. if (m_cdrom) NotifyInfoWindow(hwndDlg, m_cdrom, NULL, FALSE);
  530. }
  531. static void Window_OnSize(HWND hwndDlg, UINT nType, INT cx, INT cy)
  532. {
  533. if (nType != SIZE_MINIMIZED)
  534. {
  535. childSizer.Resize(hwndDlg, cdromwnd_rlist, sizeof(cdromwnd_rlist) / sizeof(cdromwnd_rlist[0]));
  536. InvalidateRect(hwndDlg, NULL, TRUE);
  537. }
  538. }
  539. static void CALLBACK Window_TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
  540. {
  541. HWND hwndList;
  542. switch (idEvent)
  543. {
  544. case TIMER_NOTIFYINFO_ID:
  545. KillTimer(hwnd, TIMER_NOTIFYINFO_ID);
  546. hwndList = GetDlgItem(hwnd, IDC_LIST2);
  547. NotifyInfoWindow(hwnd, m_cdrom,
  548. (hwndList) ? (INT)SendMessage(hwndList, LVM_GETNEXTITEM, (WPARAM)-1, (LPARAM)LVNI_FOCUSED) + 1: 0,
  549. FALSE);
  550. break;
  551. }
  552. }
  553. static void Window_OnCommand(HWND hwndDlg, INT eventId, INT ctrlId, HWND hwndCtrl)
  554. {
  555. switch (ctrlId)
  556. {
  557. case IDC_BUTTON_ENQUEUE:
  558. case IDC_BUTTON_PLAY:
  559. {
  560. HWND hwndList = GetDlgItem(hwndDlg, IDC_LIST2);
  561. if (hwndList)
  562. {
  563. int selcnt = (INT)SendMessageW(hwndList, LVM_GETSELECTEDCOUNT, 0, 0L);
  564. playFiles(hwndDlg, m_cdrom, (IDC_BUTTON_ENQUEUE == ctrlId), (selcnt) ? ((selcnt == 1) ? 1024 : 0) : 1);
  565. }
  566. }
  567. break;
  568. case IDC_BUTTON_EXTRACT:
  569. {
  570. RECT r;
  571. GetWindowRect(hwndCtrl, &r);
  572. int x = Menu_TrackPopup(plugin.hwndLibraryParent, GetSubMenu(g_context_menus, 0),
  573. TPM_RIGHTBUTTON | TPM_LEFTBUTTON | TPM_BOTTOMALIGN |
  574. TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RETURNCMD,
  575. r.left, r.top, hwndDlg, NULL);
  576. switch (x)
  577. {
  578. case ID_EXTRACTMENU_EXTRACTSELECTEDTRACKS: extractFiles(hwndDlg, m_cdrom, 0); break;
  579. case ID_EXTRACTMENU_EXTRACTALLTRACKS: extractFiles(hwndDlg, m_cdrom, 1); break;
  580. case ID_EXTRACTMENU_CONFIGURE: Plugin_ShowRippingPreferences(); break;
  581. }
  582. UpdateWindow(hwndDlg);
  583. Sleep(100);
  584. MSG msg;
  585. while (PeekMessageW(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE)); //eat return
  586. }
  587. break;
  588. case IDC_BUTTON_EJECT:
  589. {
  590. wchar_t result[32] = {0};
  591. wchar_t name[] = L"cda://X.cda";
  592. name[6] = m_cdrom;
  593. getFileInfoW(name, L"<eject>", result, sizeof(result)/sizeof(wchar_t));
  594. }
  595. break;
  596. case IDC_BTN_SHOWINFO:
  597. switch (eventId)
  598. {
  599. case BN_CLICKED:
  600. SendMessageW(GetParent(hwndDlg), WM_COMMAND, MAKEWPARAM(ctrlId, eventId), (LPARAM)hwndCtrl);
  601. break;
  602. }
  603. break;
  604. }
  605. }
  606. static void ListView_OnItemChanged(HWND hwndDlg, NMLISTVIEW *pnmv)
  607. {
  608. if (LVIF_STATE & pnmv->uChanged)
  609. {
  610. if ((LVIS_FOCUSED & pnmv->uOldState) != (LVIS_FOCUSED & pnmv->uNewState))
  611. {
  612. KillTimer(hwndDlg, TIMER_NOTIFYINFO_ID);
  613. SetTimer(hwndDlg, TIMER_NOTIFYINFO_ID, TIMER_NOTIFYINFO_DELAY, Window_TimerProc);
  614. }
  615. }
  616. }
  617. static INT_PTR Window_OnNotify(HWND hwndDlg, INT ctrlId, LPNMHDR phdr)
  618. {
  619. switch (phdr->idFrom)
  620. {
  621. case IDC_LIST2:
  622. switch (phdr->code)
  623. {
  624. case LVN_ITEMCHANGED: ListView_OnItemChanged(hwndDlg, (NMLISTVIEW*)phdr); break;
  625. case LVN_KEYDOWN:
  626. {
  627. LPNMLVKEYDOWN pnkd = (LPNMLVKEYDOWN)phdr;
  628. switch (pnkd->wVKey)
  629. {
  630. case '3':
  631. if (GetAsyncKeyState(VK_MENU)&0x8000)
  632. {
  633. W_ListView view(GetDlgItem(hwndDlg, IDC_LIST2));
  634. if (view.GetSelectedCount() == 0 || view.GetSelectedCount() == view.GetCount())
  635. editCDInfo(hwndDlg, m_cdrom, 0);
  636. int sel =-1;
  637. while ((sel = view.GetNextSelected(sel)) != -1)
  638. {
  639. if (editCDInfo(hwndDlg, m_cdrom, sel+1) == 1)
  640. break;
  641. }
  642. PostMessageW(hwndDlg, WM_NEXTDLGCTL, (WPARAM)phdr->hwndFrom, TRUE);
  643. }
  644. break;
  645. case 'A':
  646. if (GetAsyncKeyState(VK_CONTROL)) ListView_SetItemState(phdr->hwndFrom, -1, LVIS_SELECTED, LVIS_SELECTED);
  647. break;
  648. }
  649. }
  650. break;
  651. case NM_DBLCLK:
  652. playFiles(hwndDlg, m_cdrom, (!!g_config->ReadInt(L"enqueuedef", 0)) ^(!!(GetAsyncKeyState(VK_SHIFT)&0x8000)), 1024);
  653. break;
  654. case LVN_BEGINDRAG:
  655. SetCapture(hwndDlg);
  656. break;
  657. case NM_RETURN:
  658. SendMessageW(hwndDlg, WM_COMMAND, ((!!(GetAsyncKeyState(VK_SHIFT)&0x8000)) ^(!!g_config->ReadInt(L"enqueuedef", 0)))
  659. ? IDC_BUTTON_ENQUEUE : IDC_BUTTON_PLAY, 0);
  660. break;
  661. }
  662. break;
  663. }
  664. return 0;
  665. }
  666. static void Window_OnMouseMove(HWND hwndDlg, INT vKey, POINTS pts)
  667. {
  668. mlDropItemStruct m = {0};
  669. if (GetCapture() != hwndDlg) return;
  670. POINTSTOPOINT(m.p, pts);
  671. MapWindowPoints(hwndDlg, HWND_DESKTOP, &m.p, 1);
  672. m.type = ML_TYPE_CDTRACKS;
  673. SENDMLIPC(plugin.hwndLibraryParent, ML_IPC_HANDLEDRAG, (WPARAM)&m);
  674. }
  675. static void Window_OnLButtonUp(HWND hwndDlg, INT vKey, POINTS pts)
  676. {
  677. mlDropItemStruct m = {0};
  678. if (GetCapture() != hwndDlg) return;
  679. ReleaseCapture();
  680. m.type = ML_TYPE_CDTRACKS;
  681. m.flags = ML_HANDLEDRAG_FLAG_NOCURSOR;
  682. POINTSTOPOINT(m.p, pts);
  683. MapWindowPoints(hwndDlg, HWND_DESKTOP, &m.p, 1);
  684. SENDMLIPC(plugin.hwndLibraryParent, ML_IPC_HANDLEDRAG, (WPARAM)&m);
  685. if (m.result > 0)
  686. {
  687. HWND hwndList;
  688. LVITEMW lvitem = {0};
  689. int i, l, len;
  690. itemRecordList myObj = {0};
  691. char trackname[] = "cda://X,%d.cda";
  692. char name[32] = {0}, total[512] = {0};
  693. hwndList = GetDlgItem(hwndDlg, IDC_LIST2);
  694. l = (hwndList) ? (INT)SendMessageW(hwndList, LVM_GETITEMCOUNT, 0,0) : 0;
  695. if (l > 256) l = 256;
  696. allocRecordList(&myObj, l, 0);
  697. lvitem.mask = LVIF_PARAM;
  698. lvitem.iSubItem = 0;
  699. trackname[6] = m_cdrom;
  700. for (i = 0; i < l; i++)
  701. {
  702. lvitem.iItem = i;
  703. SendMessageW(hwndList, LVM_GETITEMW, 0, (LPARAM)&lvitem);
  704. int p = (INT)(INT_PTR)lvitem.lParam;
  705. if ((LVIS_SELECTED & SendMessageW(hwndList, LVM_GETITEMSTATE, i, LVIS_SELECTED)) && p > 0)
  706. {
  707. StringCchPrintfA(name, sizeof(name)/sizeof(char), trackname, p);
  708. total[0] = 0;
  709. mediaLibrary.GetFileInfo(name, total, sizeof(total)/sizeof(char), &len);
  710. memset(myObj.Items + myObj.Size, 0, sizeof(itemRecord));
  711. myObj.Items[myObj.Size].filename = _strdup(name);
  712. myObj.Items[myObj.Size].length = len;
  713. myObj.Items[myObj.Size++].title = _strdup(total);
  714. }
  715. }
  716. if (myObj.Size)
  717. {
  718. m.flags = 0;
  719. m.result = 0;
  720. m.data = (void*)&myObj;
  721. SENDMLIPC(plugin.hwndLibraryParent, ML_IPC_HANDLEDROP, (WPARAM)&m);
  722. }
  723. freeRecordList(&myObj);
  724. }
  725. }
  726. static void Window_OnQueryInfo(HWND hwnd)
  727. {
  728. KillTimer(hwnd, TIMER_NOTIFYINFO_ID);
  729. NotifyInfoWindow(hwnd, m_cdrom, NULL, TRUE);
  730. SetTimer(hwnd, TIMER_NOTIFYINFO_ID, TIMER_NOTIFYINFO_DELAY, Window_TimerProc);
  731. }
  732. static void Window_OnFileTagUpdated(HWND hwnd, CHAR cLetter, LPCWSTR pszFileName)
  733. {
  734. INT len, lcid;
  735. len = (pszFileName) ? lstrlenW(pszFileName) : 0;
  736. if (len < 7) return;
  737. lcid = MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT);
  738. if (CSTR_EQUAL == CompareStringW(lcid, 0, L"cda://", 6, pszFileName, 6) && pszFileName[6] == cLetter)
  739. {
  740. GetGracenoteInfo(hwnd, cLetter, GetCurrentThread());
  741. }
  742. }
  743. static void Window_OnContextMenu(HWND hwndDlg, HWND hwndFrom, int x, int y)
  744. {
  745. POINT pt = {x,y};
  746. W_ListView view(GetDlgItem(hwndDlg, IDC_LIST2));
  747. if(view.GetCount() == 0) return;
  748. if (x == -1 || y == -1) // x and y are -1 if the user invoked a shift-f10 popup menu
  749. {
  750. RECT itemRect = {0};
  751. int selected = view.GetNextSelected();
  752. if (selected != -1) // if something is selected we'll drop the menu from there
  753. {
  754. view.GetItemRect(selected, &itemRect);
  755. ClientToScreen(view.getwnd(), (POINT *)&itemRect);
  756. }
  757. else // otherwise we'll drop it from the top-left corner of the listview, adjusting for the header location
  758. {
  759. GetWindowRect(view.getwnd(), &itemRect);
  760. HWND hHeader = (HWND)SNDMSG(hwndFrom, LVM_GETHEADER, 0, 0L);
  761. RECT headerRect;
  762. if ((WS_VISIBLE & GetWindowLongPtr(hHeader, GWL_STYLE)) && GetWindowRect(hHeader, &headerRect))
  763. {
  764. itemRect.top += (headerRect.bottom - headerRect.top);
  765. }
  766. }
  767. x = itemRect.left;
  768. y = itemRect.top;
  769. }
  770. HWND hHeader = (HWND)SNDMSG(hwndFrom, LVM_GETHEADER, 0, 0L);
  771. RECT headerRect;
  772. if (0 == (WS_VISIBLE & GetWindowLongPtr(hHeader, GWL_STYLE)) || FALSE == GetWindowRect(hHeader, &headerRect))
  773. {
  774. SetRectEmpty(&headerRect);
  775. }
  776. if (FALSE != PtInRect(&headerRect, pt))
  777. {
  778. return;
  779. }
  780. int r = Menu_TrackPopup(plugin.hwndLibraryParent, GetSubMenu(g_context_menus, 1),
  781. TPM_RETURNCMD | TPM_RIGHTBUTTON | TPM_LEFTBUTTON | TPM_NONOTIFY,
  782. x, y, hwndDlg, NULL);
  783. switch (r)
  784. {
  785. case ID_PE_ID3:
  786. {
  787. if (view.GetSelectedCount() == 0 || view.GetSelectedCount() == view.GetCount())
  788. editCDInfo(hwndDlg, m_cdrom, 0);
  789. int sel =-1;
  790. while ((sel = view.GetNextSelected(sel)) != -1)
  791. {
  792. if (editCDInfo(hwndDlg, m_cdrom, sel+1) == 1)
  793. break;
  794. }
  795. }
  796. PostMessageW(hwndDlg, WM_NEXTDLGCTL, (WPARAM)hwndFrom, TRUE);
  797. break;
  798. case ID_CDROMMENU_PLAYSELECTEDITEMS: playFiles(hwndDlg, m_cdrom, 0, 0); break;
  799. case ID_CDROMMENU_ENQUEUESELECTEDITEMS: playFiles(hwndDlg, m_cdrom, 1, 0); break;
  800. case ID_CDROMMENU_SELECTALL: ListView_SetItemState(hwndFrom, -1, LVIS_SELECTED, LVIS_SELECTED); break;
  801. case ID_CDROMMENU_PLAYALL: playFiles(hwndDlg, m_cdrom, 0, 1); break;
  802. case ID_CDROMMENU_ENQUEUEALL: playFiles(hwndDlg, m_cdrom, 1, 1); break;
  803. case ID_CDROMMENU_EXTRACT_EXTRACTSELECTEDITEMS: extractFiles(hwndDlg, m_cdrom, 0); break;
  804. case ID_CDROMMENU_EXTRACT_EXTRACTALL: extractFiles(hwndDlg, m_cdrom, 1); break;
  805. case ID_CDROMMENU_EXTRACT_CONFIGURE: Plugin_ShowRippingPreferences(); break;
  806. }
  807. UpdateWindow(hwndDlg);
  808. Sleep(100);
  809. MSG msg;
  810. while (PeekMessage(&msg, NULL, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE)); //eat return
  811. }
  812. static INT_PTR WINAPI DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  813. {
  814. INT_PTR a;
  815. a = dialogSkinner.Handle(hwndDlg, uMsg, wParam, lParam);
  816. if (a) return a;
  817. switch (uMsg)
  818. {
  819. case WM_INITDIALOG: return Window_OnInitDialog(hwndDlg, (HWND)wParam, lParam);
  820. case WM_DESTROY: Window_OnDestroy(hwndDlg); break;
  821. case WM_SIZE: Window_OnSize(hwndDlg, (UINT)wParam, GET_X_LPARAM(lParam), GET_Y_LPARAM(wParam)); break;
  822. case WM_COMMAND: Window_OnCommand(hwndDlg, HIWORD(wParam), LOWORD(wParam), (HWND)lParam); break;
  823. case WM_NOTIFY: return Window_OnNotify(hwndDlg, (INT)wParam, (LPNMHDR) lParam);
  824. case WM_MOUSEMOVE: Window_OnMouseMove(hwndDlg, (INT)wParam, MAKEPOINTS(lParam)); break;
  825. case WM_LBUTTONUP: Window_OnLButtonUp(hwndDlg, (INT)wParam, MAKEPOINTS(lParam)); break;
  826. case WM_CONTEXTMENU: Window_OnContextMenu(hwndDlg, (HWND)wParam, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)); return 1;
  827. case WM_ERASEBKGND: return 1;
  828. case WM_TAGUPDATED: Window_OnFileTagUpdated(hwndDlg, m_cdrom, (LPCWSTR)lParam); break;
  829. case WM_QUERYFILEINFO: Window_OnQueryInfo(hwndDlg); break;
  830. case WM_PAINT:
  831. {
  832. int tab[] = { IDC_LIST2 | DCW_SUNKENBORDER};
  833. dialogSkinner.Draw(hwndDlg, tab, 1);
  834. }
  835. return 0;
  836. case WM_EXTRACTDISC:
  837. if ((CHAR)wParam == m_cdrom) extractFiles(hwndDlg, m_cdrom, TRUE);
  838. SetWindowLongPtrW(hwndDlg, DWLP_MSGRESULT, ((CHAR)wParam == m_cdrom));
  839. return TRUE;
  840. }
  841. return 0;
  842. }
  843. static void CALLBACK APC_GetCracenoteInfo(ULONG_PTR param)
  844. {
  845. GetGracenoteInfo(((APCPARAM*)param)->hwndDlg, ((APCPARAM*)param)->cLetter, NULL);
  846. free((APCPARAM*)param);
  847. }