PlayList.cpp 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126
  1. #include "Main.h"
  2. #include <assert.h>
  3. #include "MoreItems.h"
  4. #include "AdData.h"
  5. #include "../nu/AutoLock.h"
  6. #include "strutil.h"
  7. #include "../nu/AutoCharFn.h"
  8. #include "../nu/AutoChar.h"
  9. #include "../nu/AutoWide.h"
  10. #include "../nu/ns_wc.h"
  11. #include "WinampPlaylist.h"
  12. #include <algorithm>
  13. #include "api.h"
  14. #include "../WAT/WAT.h"
  15. using namespace Nullsoft::Utility;
  16. LockGuard playlistGuard(512);
  17. #define MALLOC_CHUNK 512
  18. static int _lastlen = -1;
  19. extern "C"
  20. {
  21. static wchar_t *_plstring_wcsdup( const wchar_t *str )
  22. {
  23. if ( !str )
  24. return 0;
  25. size_t len = wcslen( str );
  26. size_t *self = (size_t *)malloc( sizeof( size_t ) + ( len + 1 ) * sizeof( wchar_t ) );
  27. *self = 1;
  28. wchar_t *new_str = (wchar_t *)( ( (int8_t *)self ) + sizeof( size_t ) );
  29. memcpy( new_str, str, ( len + 1 ) * sizeof( wchar_t ) );
  30. return new_str;
  31. }
  32. static wchar_t *_plstring_malloc( size_t str_size )
  33. {
  34. size_t *self = (size_t *)malloc( sizeof( size_t ) + ( str_size ) );
  35. *self = 1;
  36. wchar_t *new_str = (wchar_t *)( ( (int8_t *)self ) + sizeof( size_t ) );
  37. return new_str;
  38. }
  39. static void _plstring_release( wchar_t *str )
  40. {
  41. if ( str )
  42. {
  43. size_t *self = (size_t *)( ( (int8_t *)str ) - sizeof( size_t ) );
  44. ( *self )--;
  45. if ( *self == 0 )
  46. {
  47. free( self );
  48. }
  49. }
  50. }
  51. static void _plstring_retain( wchar_t *str )
  52. {
  53. if ( str )
  54. {
  55. size_t *self = (size_t *)( ( (int8_t *)str ) - sizeof( size_t ) );
  56. ( *self )++;
  57. }
  58. }
  59. __declspec( dllexport ) wchar_t *( *plstring_wcsdup )( const wchar_t *str ) = _plstring_wcsdup;
  60. __declspec( dllexport ) wchar_t *( *plstring_malloc )( size_t str_size ) = _plstring_malloc;
  61. __declspec( dllexport ) void ( *plstring_release )( wchar_t *str ) = _plstring_release;
  62. __declspec( dllexport ) void ( *plstring_retain )( wchar_t *str ) = _plstring_retain;
  63. }
  64. static bool ndestring_tried_load = false;
  65. void plstring_init()
  66. {
  67. if ( !ndestring_tried_load )
  68. {
  69. wchar_t path[ MAX_PATH ] = { 0 };
  70. PathCombineW( path, SHAREDDIR, L"nde.dll" );
  71. HMODULE ndelib = LoadLibraryW( path );
  72. if ( ndelib )
  73. {
  74. FARPROC ndestring_wcsdup = GetProcAddress( ndelib, "ndestring_wcsdup" );
  75. FARPROC ndestring_malloc = GetProcAddress( ndelib, "ndestring_malloc" );
  76. FARPROC ndestring_release = GetProcAddress( ndelib, "ndestring_release" );
  77. FARPROC ndestring_retain = GetProcAddress( ndelib, "ndestring_retain" );
  78. if ( ndestring_wcsdup && ndestring_malloc && ndestring_release && ndestring_retain )
  79. {
  80. *(FARPROC *)&plstring_wcsdup = ndestring_wcsdup;
  81. *(FARPROC *)&plstring_malloc = ndestring_malloc;
  82. *(FARPROC *)&plstring_release = ndestring_release;
  83. *(FARPROC *)&plstring_retain = ndestring_retain;
  84. }
  85. }
  86. ndestring_tried_load = true;
  87. }
  88. }
  89. #define CHECK_STUFF()
  90. /* { \
  91. if (list_pos >= list_size && list_size>0) { MessageBox(NULL,"SHIT2!","a",0); } \
  92. if (list_size > malloced_size) MessageBox(NULL,"SHIT!","a",0); }
  93. */
  94. static void Playlist_notifyModified();
  95. struct pl_entry
  96. {
  97. pl_entry();
  98. void Free();
  99. void Create( int length, const wchar_t *filename, const wchar_t *title, const char *curtain, const wchar_t *ext, int is_nde_string = 0 );
  100. void CreateMore( int length, const wchar_t *filename, const wchar_t *title, const char *curtain/*, const char *browser*/ );
  101. void SetTitle( const wchar_t *title );
  102. void SetFile( const wchar_t *file );
  103. wchar_t *strFile = 0; // file name oct-17-2005-maksim
  104. wchar_t *strTitle = 0; // title oct-17-2005-maksim
  105. wchar_t *strExt = 0;
  106. size_t cbTitle = 0; // title length oct-17-2005-maksim
  107. int length = 0;
  108. int8_t selected:1;
  109. int8_t cached:1;
  110. int8_t tmp:1;
  111. int more = 0; // number of subitems in the playlist (or 0 for none) mar-16-2005-benski
  112. int curitems = 0; // number of hidden items in this entry
  113. int curindex = 0; // Current index of item we're playing (0 for 1st/none hidden item)
  114. int repeatCount = 0; // number of times to repeat
  115. int repeatCurrent = 0; // where we're at...
  116. unsigned long starttime = 0; // Start time in MS (0, begin of file)
  117. unsigned long endtime = 0; // End time in MS (0, end of file)
  118. moreitems *moreStuff = NULL; // added for subitems in a playlist mar-16-2005-benski
  119. ad_data *ads = NULL; // advertisement struct oct-17-2005-maksim
  120. };
  121. void pl_entry::Free()
  122. {
  123. more = 0;
  124. if ( moreStuff )
  125. {
  126. delete moreStuff;
  127. moreStuff = NULL;
  128. }
  129. if ( ads )
  130. {
  131. delete ads;
  132. ads = 0;
  133. }
  134. plstring_release( strFile );
  135. strFile = 0;
  136. plstring_release( strExt );
  137. strExt = 0;
  138. plstring_release( strTitle );
  139. strTitle = 0;
  140. cbTitle = 0;
  141. }
  142. pl_entry::pl_entry()
  143. {}
  144. void pl_entry::Create( int length, const wchar_t *filename, const wchar_t *title, const char *curtain, const wchar_t *ext, int is_nde_string )
  145. {
  146. assert( filename );
  147. assert( title );
  148. if ( is_nde_string & 1 )
  149. {
  150. strFile = plstring_wcsdup( filename );
  151. }
  152. else
  153. {
  154. while ( filename && *filename && IsCharSpaceW( *filename ) )
  155. filename = CharNextW( filename );
  156. strFile = plstring_wcsdup( filename );
  157. wchar_t *end = GetLastCharacterW( strFile );
  158. while ( end && IsCharSpaceW( *end ) )
  159. {
  160. *end = 0;
  161. end = CharPrevW( strFile, end );
  162. }
  163. }
  164. cbTitle = wcslen( title ) + 1;
  165. strTitle = _wcsdup( title );
  166. if ( ext && *ext )
  167. strExt = _wcsdup( ext );
  168. if ( ( curtain && curtain[ 0 ] ) /*|| (browser && browser[0])*/ )
  169. {
  170. ads = new ad_data;
  171. if ( curtain && curtain[ 0 ] != 0 )
  172. {
  173. ads->cbCurtain = (int)strlen( curtain ) + 1;
  174. ads->strCurtain = new char[ ads->cbCurtain ];
  175. StringCchCopyA( ads->strCurtain, ads->cbCurtain, curtain );
  176. }
  177. }
  178. else
  179. {
  180. ads = NULL;
  181. }
  182. this->length = length;
  183. selected = 0;
  184. cached = 1;
  185. more = 0;
  186. curindex = 0;
  187. curitems = 0;
  188. moreStuff = NULL;
  189. repeatCount = 0;
  190. repeatCurrent = 0;
  191. }
  192. void pl_entry::CreateMore( int length, const wchar_t *filename, const wchar_t *title, const char *curtain/*, const char *browser*/ )
  193. {
  194. Create( length, filename, title, curtain, L""/*, browser*/);
  195. more = 1;
  196. moreStuff = new moreitems;
  197. }
  198. void pl_entry::SetTitle( const wchar_t *title )
  199. {
  200. size_t newSize = wcslen( title ) + 1;
  201. if ( newSize > cbTitle )
  202. {
  203. delete[] strTitle;
  204. strTitle = new wchar_t[ newSize ];
  205. }
  206. cbTitle = newSize;
  207. StringCchCopyW( strTitle, cbTitle, title );
  208. }
  209. void pl_entry::SetFile( const wchar_t *file )
  210. {
  211. plstring_release( strFile );
  212. while ( file && *file && IsCharSpaceW( *file ) )
  213. file = CharNextW( file );
  214. strFile = plstring_wcsdup( file );
  215. wchar_t *end = GetLastCharacterW( strFile );
  216. while ( end && IsCharSpaceW( *end ) )
  217. {
  218. *end = 0;
  219. end = CharPrevW( strFile, end );
  220. }
  221. }
  222. static pl_entry *list = 0;
  223. static int list_size = 0;
  224. static int list_pos = 0;
  225. static int malloced_size = 0;
  226. static pl_entry *tlist = 0;
  227. static int t_len = 0;
  228. static int rnd_listmodified = 1;
  229. static int rnd_lastls = -1023;
  230. static int rnd_i = 0;
  231. static int *rnd_rtable;
  232. extern "C"
  233. {
  234. int PlayList_get_lastlen()
  235. {
  236. return _lastlen;
  237. }
  238. static void Playlist_rnd_deleteitem(int item);
  239. static void Playlist_rnd_additem();
  240. wchar_t *remove_urlcodesW(wchar_t *p)
  241. {
  242. assert(p);
  243. if (!config_fixtitles)
  244. return p;
  245. wchar_t buf[FILENAME_SIZE] = {0}, *i = buf, *p2 = p;
  246. StringCchCopyW(buf, FILENAME_SIZE, p);
  247. while (i && *i)
  248. {
  249. if (*i == L'_' && (config_fixtitles&2))
  250. {
  251. *p = L' ';
  252. i = CharNextW(i);
  253. }
  254. else if (!StrCmpNW(i, L"%20", 3) && (config_fixtitles&1))
  255. {
  256. *p = L' ';
  257. i += 3;
  258. }
  259. else
  260. {
  261. CopyCharW(p, i);
  262. i = CharNextW(i);
  263. }
  264. p = CharNextW(p);
  265. }
  266. *p = 0;
  267. return p2;
  268. }
  269. static void PlayList_allocmem(int newsize);
  270. void PlayList_resetcurrent( void )
  271. {
  272. AutoLock lock( playlistGuard );
  273. if ( list_pos < 0 || list_pos >= list_size )
  274. return;
  275. if ( list )
  276. list[ list_pos ].curindex = 0;
  277. }
  278. void PlayList_saveend( int start )
  279. {
  280. AutoLock lock( playlistGuard );
  281. if ( tlist )
  282. {
  283. // scan and remove subitems in a playlist mar-16-2005-benski
  284. for ( int i = 0; i < t_len; i++ )
  285. tlist[ i ].Free();
  286. delete[] tlist;
  287. tlist = 0;
  288. }
  289. if ( start < list_size )
  290. {
  291. t_len = ( list_size - start );
  292. assert( t_len > 0 );
  293. tlist = new pl_entry[ t_len ];
  294. memcpy( tlist, list + start, sizeof( pl_entry ) * t_len );
  295. list_size = start;
  296. }
  297. }
  298. void PlayList_restoreend( void )
  299. {
  300. AutoLock lock( playlistGuard );
  301. if ( tlist )
  302. {
  303. if ( t_len )
  304. {
  305. PlayList_allocmem( list_size + t_len );
  306. memcpy( list + list_size, tlist, t_len * sizeof( pl_entry ) );
  307. list_size += t_len;
  308. }
  309. delete[] tlist; // note: we *don't* want to clean up the moreStuff linked list here
  310. tlist = NULL;
  311. t_len = 0;
  312. }
  313. }
  314. int PlayList_getselect( int x )
  315. {
  316. AutoLock lock( playlistGuard );
  317. if ( x < 0 || x >= list_size )
  318. return 0;
  319. CHECK_STUFF();
  320. return list[ x ].selected;
  321. }
  322. int PlayList_getselect2( int x, wchar_t *filename )
  323. {
  324. AutoLock lock( playlistGuard );
  325. if ( x < 0 || x >= list_size )
  326. return 0;
  327. CHECK_STUFF();
  328. if ( filename )
  329. lstrcpynW( filename, list[ x ].strFile, FILENAME_SIZE );
  330. return list[ x ].selected;
  331. }
  332. int PlayList_GetNextSelected( int start )
  333. {
  334. AutoLock lock( playlistGuard );
  335. start++;
  336. if ( start < 0 || start >= list_size )
  337. return -1;
  338. while ( start < list_size )
  339. {
  340. if ( list[ start ].selected )
  341. return start;
  342. start++;
  343. }
  344. return -1;
  345. }
  346. int PlayList_GetSelectedCount()
  347. {
  348. AutoLock lock( playlistGuard );
  349. int num = 0;
  350. for ( int i = 0; i < list_size; i++ )
  351. {
  352. if ( list[ i ].selected )
  353. num++;
  354. }
  355. return num;
  356. }
  357. void PlayList_setselect( int x, int sel )
  358. {
  359. AutoLock lock( playlistGuard );
  360. if ( x < 0 || x >= list_size )
  361. return;
  362. CHECK_STUFF();
  363. list[ x ].selected = sel;
  364. }
  365. int PlayList_getcached( int x )
  366. {
  367. AutoLock lock( playlistGuard );
  368. if ( x < 0 || x >= list_size )
  369. return 0;
  370. CHECK_STUFF();
  371. return list[ x ].cached;
  372. }
  373. void PlayList_setcached( int x, int cached )
  374. {
  375. AutoLock lock( playlistGuard );
  376. if ( x < 0 || x >= list_size )
  377. return;
  378. CHECK_STUFF();
  379. list[ x ].cached = cached;
  380. }
  381. int PlayList_getsonglength( int x )
  382. {
  383. AutoLock lock( playlistGuard );
  384. if ( x < 0 || x >= list_size )
  385. return -1;
  386. CHECK_STUFF();
  387. return list[ x ].length;
  388. }
  389. int PlayList_gettotallength( void )
  390. {
  391. AutoLock lock( playlistGuard );
  392. int l = 0;
  393. CHECK_STUFF();
  394. for ( int x = 0; x < list_size; x++ )
  395. {
  396. if ( list[ x ].length >= 0 )
  397. l += list[ x ].length;
  398. else
  399. return -1;
  400. }
  401. return l;
  402. }
  403. int PlayList_getcurrentlength( void )
  404. {
  405. AutoLock lock( playlistGuard );
  406. return ( PlayList_getsonglength( list_pos ) );
  407. }
  408. static void PlayList_allocmem( int newsize )
  409. {
  410. assert( newsize );
  411. int os = malloced_size;
  412. if ( newsize > malloced_size )
  413. malloced_size = newsize + MALLOC_CHUNK;
  414. else if ( newsize < malloced_size - MALLOC_CHUNK )
  415. malloced_size = newsize;
  416. else return;
  417. if ( !malloced_size )
  418. {
  419. if ( list )
  420. {
  421. delete[] list;
  422. list = 0;
  423. }
  424. }
  425. else if ( list )
  426. {
  427. pl_entry *ol = list;
  428. assert( malloced_size > 0 );
  429. list = new pl_entry[ malloced_size ];
  430. assert( min( os, malloced_size ) > 0 );
  431. assert( min( os, malloced_size ) <= malloced_size );
  432. memcpy( list, ol, min( os, malloced_size ) * sizeof( pl_entry ) );
  433. delete[] ol;
  434. }
  435. else
  436. {
  437. assert( malloced_size > 0 );
  438. list = new pl_entry[ malloced_size ];
  439. memset( list, 0, sizeof( pl_entry ) * ( malloced_size ) );
  440. }
  441. }
  442. int PlayList_gethidden( int pos )
  443. {
  444. AutoLock lock( playlistGuard );
  445. if ( pos < 0 || pos >= list_size )
  446. return 0;
  447. if ( list )
  448. return list[ pos ].curitems;
  449. else
  450. return 0;
  451. }
  452. int PlayList_ishidden( int pos )
  453. {
  454. AutoLock lock( playlistGuard );
  455. if ( pos < 0 || pos >= list_size )
  456. return 0;
  457. if ( list )
  458. if ( list[ pos ].curitems )
  459. {
  460. if ( list[ pos ].curindex != list[ pos ].curitems )
  461. return 1;
  462. }
  463. return 0;
  464. }
  465. int PlayList_alldone( int pos )
  466. {
  467. AutoLock lock( playlistGuard );
  468. if ( pos < 0 || pos >= list_size )
  469. return 1;
  470. if ( list )
  471. {
  472. if ( list[ pos ].curitems )
  473. {
  474. if ( list[ pos ].curindex == 0 )
  475. return 1;
  476. }
  477. }
  478. return 0;
  479. }
  480. int PlayList_hasanycurtain( int pos )
  481. {
  482. AutoLock lock( playlistGuard );
  483. if ( pos < 0 || pos >= list_size )
  484. return 0;
  485. if ( list )
  486. {
  487. if ( list[ pos ].ads && list[ pos ].ads->strCurtain )
  488. return 1;
  489. }
  490. return 0;
  491. }
  492. const char *PlayList_getcurtain( int pos )
  493. {
  494. AutoLock lock( playlistGuard );
  495. if ( pos < 0 || pos >= list_size )
  496. return NULL;
  497. if ( list )
  498. {
  499. if ( list[ pos ].ads && list[ pos ].ads->strCurtain && list[ pos ].curindex == 0 )
  500. return list[ pos ].ads->strCurtain;
  501. if ( list[ pos ].curindex && list[ pos ].curitems )
  502. return list[ pos ].moreStuff->GetHiddenCurtain( list[ pos ].curindex );
  503. }
  504. return NULL;
  505. }
  506. const char *PlayList_getExtInf( int pos )
  507. {
  508. AutoLock lock( playlistGuard );
  509. if ( pos < 0 || pos >= list_size )
  510. return NULL;
  511. if ( list )
  512. {
  513. pl_entry l_entry = list[ pos ];
  514. if ( l_entry.strExt && *l_entry.strExt )
  515. {
  516. wa::strings::wa_string l_ext_inf( " ext=\"" );
  517. l_ext_inf.append( l_entry.strExt );
  518. l_ext_inf.append( "\"" );
  519. return _strdup( l_ext_inf.GetA().c_str() );
  520. }
  521. }
  522. return NULL;
  523. }
  524. /*
  525. const char *PlayList_getbrowser(int pos)
  526. {
  527. AutoLock lock (playlistGuard);
  528. if (pos < 0 || pos >= list_size) return NULL;
  529. if ( list )
  530. {
  531. if ( list[pos].ads && list[pos].ads->cbBrowser ) return list[pos].ads->strBrowser;
  532. }
  533. return NULL;
  534. }
  535. */
  536. int PlayList_current_hidden( void )
  537. {
  538. AutoLock lock( playlistGuard );
  539. if ( list_pos < 0 || list_pos >= list_size )
  540. return 0;
  541. if ( list && list[ list_pos ].curitems )
  542. {
  543. if ( list[ list_pos ].curitems && list[ list_pos ].curindex < list[ list_pos ].curitems )
  544. {
  545. list[ list_pos ].curindex++;
  546. return 1;
  547. }
  548. }
  549. return 0;
  550. }
  551. int PlayList_getitem( int position, wchar_t *filename, wchar_t *filetitle )
  552. {
  553. AutoLock lock( playlistGuard );
  554. if ( !list || position < 0 || position >= list_size )
  555. return 1;
  556. CHECK_STUFF();
  557. if ( filename )
  558. StringCchCopyW( filename, FILENAME_SIZE, list[ position ].strFile );
  559. if ( filetitle )
  560. StringCchPrintfW( filetitle, FILETITLE_SIZE, L"%d. %s", position + 1, list[ position ].strTitle );
  561. if ( list[ position ].length >= 0 )
  562. {
  563. wchar_t tmp[ 512 ] = { 0 };
  564. StringCchPrintfW( tmp, 512, L" - [%d:%02d]", list[ position ].length / 60, list[ position ].length % 60 );
  565. if ( filetitle )
  566. StringCchCatW( filetitle, FILETITLE_SIZE, tmp );
  567. }
  568. return 0;
  569. }
  570. /* this function call specifically has 256 characters max because of the data structure in ipc_pe.h (fileinfo2) */
  571. int PlayList_getitem3( int position, char *filetitle, char *filelength )
  572. {
  573. AutoLock lock( playlistGuard );
  574. if ( !list || position < 0 || position >= list_size )
  575. return 1;
  576. CHECK_STUFF();
  577. if ( filetitle )
  578. WideCharToMultiByteSZ( CP_ACP, 0, list[ position ].strTitle, -1, filetitle, 256, 0, 0 );
  579. if ( filelength && list[ position ].length >= 0 )
  580. StringCchPrintfA( filelength, 16, "%d:%02d", list[ position ].length / 60, list[ position ].length % 60 );
  581. return 0;
  582. }
  583. /* this function call specifically has 256 characters max because of the data structure in ipc_pe.h (fileinfo2) */
  584. int PlayList_getitem3W( int position, wchar_t *filetitle, wchar_t *filelength )
  585. {
  586. AutoLock lock( playlistGuard );
  587. if ( !list || position < 0 || position >= list_size )
  588. return 1;
  589. CHECK_STUFF();
  590. if ( filetitle )
  591. lstrcpynW( filetitle, list[ position ].strTitle, 256 );
  592. if ( filelength && list[ position ].length >= 0 )
  593. StringCchPrintfW( filelength, 16, L"%d:%02d", list[ position ].length / 60, list[ position ].length % 60 );
  594. return 0;
  595. }
  596. void PlayList_updateitem( int position )
  597. {
  598. AutoLock lock( playlistGuard );
  599. if ( !list || position < 0 || position >= list_size )
  600. return;
  601. _lastlen = -1;
  602. wchar_t *str = remove_urlcodesW( PlayList_gettitle( list[ position ].strFile, 1 ) );
  603. list[ position ].SetTitle( str );
  604. PlayList_setcached( position, 1 );
  605. if ( _lastlen != -1 )
  606. list[ position ].length = _lastlen;
  607. }
  608. int PlayList_getitem_pl( int position, wchar_t *filetitle )
  609. {
  610. AutoLock lock( playlistGuard );
  611. // static int i;
  612. CHECK_STUFF();
  613. if ( !list || position < 0 || position >= list_size )
  614. return 1;
  615. /*
  616. if (!PlayList_getcached(position) && config_riol==0 && !i)
  617. {
  618. i=1;
  619. _lastlen=-1;
  620. lstrcpy(list[position].filetitle,remove_urlcodes(PlayList_gettitle(list[position].filename,1)));
  621. PlayList_setcached(position,1);
  622. if (_lastlen != -1) list[position].length = _lastlen;
  623. i=0;
  624. }
  625. */
  626. if ( filetitle && config_shownumsinpl )
  627. {
  628. if ( list_size >= 10000 )
  629. StringCchPrintfW( filetitle, FILETITLE_SIZE, ( config_zeropadplnum ? L"%05u. %s" : L"%5u. %s" ), position + 1, list[ position ].strTitle );
  630. else if ( list_size >= 1000 )
  631. StringCchPrintfW( filetitle, FILETITLE_SIZE, ( config_zeropadplnum ? L"%04u. %s" : L"%4u. %s" ), position + 1, list[ position ].strTitle );
  632. else if ( list_size >= 100 )
  633. StringCchPrintfW( filetitle, FILETITLE_SIZE, ( config_zeropadplnum ? L"%03u. %s" : L"%3u. %s" ), position + 1, list[ position ].strTitle );
  634. else if ( list_size >= 10 )
  635. StringCchPrintfW( filetitle, FILETITLE_SIZE, ( config_zeropadplnum ? L"%02u. %s" : L"%2u. %s" ), position + 1, list[ position ].strTitle );
  636. else
  637. StringCchPrintfW( filetitle, FILETITLE_SIZE, L"%1u. %s", position + 1, list[ position ].strTitle );
  638. }
  639. else if ( filetitle )
  640. StringCchCopyW( filetitle, FILETITLE_SIZE, list[ position ].strTitle );
  641. return 0;
  642. }
  643. int PlayList_getitem2( int position, char *filename, char *filetitle )
  644. {
  645. AutoLock lock( playlistGuard );
  646. if ( !list || position < 0 || position >= list_size )
  647. return 1;
  648. CHECK_STUFF();
  649. if ( filename )
  650. StringCchCopyA( filename, FILENAME_SIZE, AutoCharFn( list[ position ].strFile ) );
  651. if ( filetitle )
  652. WideCharToMultiByteSZ( CP_ACP, 0, list[ position ].strTitle, -1, filetitle, FILETITLE_SIZE, 0, 0 );
  653. return 0;
  654. }
  655. int PlayList_getitem2W( int position, wchar_t *filename, wchar_t *filetitle )
  656. {
  657. AutoLock lock( playlistGuard );
  658. if ( !list || position < 0 || position >= list_size )
  659. return 1;
  660. CHECK_STUFF();
  661. if ( filename )
  662. {
  663. if ( list[ position ].strFile )
  664. StringCchCopyW( filename, FILENAME_SIZE, list[ position ].strFile );
  665. else
  666. return 1;
  667. }
  668. if ( filetitle )
  669. {
  670. if ( list[ position ].strTitle )
  671. StringCchCopyW( filetitle, FILETITLE_SIZE, list[ position ].strTitle );
  672. else
  673. return 1;
  674. }
  675. return 0;
  676. }
  677. int PlayList_getitem_jtfW( int position, wchar_t *str )
  678. {
  679. AutoLock lock( playlistGuard );
  680. if ( !list || position < 0 || position >= list_size )
  681. return 1;
  682. CHECK_STUFF();
  683. StringCchPrintfW( str, FILENAME_SIZE + FILETITLE_SIZE + 1, L"%s %s", list[ position ].strFile, list[ position ].strTitle );
  684. return 0;
  685. }
  686. int PlayList_getlength( void )
  687. {
  688. AutoLock lock( playlistGuard );
  689. return list_size;
  690. }
  691. int plmodified = 0, plcleared = 0, plneedsave = 0;
  692. int g_has_deleted_current;
  693. // returns:
  694. // 1, last item in list
  695. // 0 not
  696. // -1 no items in list
  697. int PlayList_deleteitem( int item )
  698. {
  699. AutoLock lock( playlistGuard );
  700. if ( !list ) return -1;
  701. CHECK_STUFF();
  702. if ( item < 0 || item >= list_size ) return -1;
  703. list[ item ].Free();
  704. list_size--;
  705. if ( list_size != item )
  706. memmove( list + item, list + item + 1, sizeof( list[ 0 ] ) * ( list_size - item ) );
  707. if ( item == list_pos )
  708. g_has_deleted_current = 1;
  709. if ( item <= list_pos )
  710. list_pos--;
  711. if ( list_pos < 0 ) list_pos = 0;
  712. PlayList_allocmem( list_size + 1 );
  713. Playlist_notifyModified();
  714. Playlist_rnd_deleteitem( item );
  715. return list ? 0 : 1;
  716. }
  717. void PlayList_delete( void )
  718. {
  719. AutoLock lock( playlistGuard );
  720. CHECK_STUFF();
  721. //if (GetPrivateProfileIntW(L"Winamp", L"rmudhack", 0, INI_FILE) == 666) return ;
  722. if ( list )
  723. {
  724. // code to cleanup subitems in a playlist entry mar-16-2005-benski
  725. for ( int i = 0; i < list_size; i++ )
  726. list[ i ].Free();
  727. // delete[] list;
  728. }
  729. // list=0;
  730. list_size = 0;
  731. list_pos = 0;
  732. plcleared = 1;
  733. // malloced_size=0;
  734. Playlist_notifyModified();
  735. }
  736. void PlayList_destroy(void)
  737. {
  738. AutoLock lock (playlistGuard);
  739. PlayList_delete();
  740. delete[] list;
  741. list = 0;
  742. }
  743. void PlayList_setlastlen(int x)
  744. {
  745. AutoLock lock (playlistGuard);
  746. CHECK_STUFF();
  747. if (x >= 0 && x < list_size) list[x].length = _lastlen;
  748. }
  749. static wchar_t *GetFileInfo(const wchar_t *filename, wchar_t *title, size_t titleCch)
  750. {
  751. In_Module *mod = in_setmod_noplay(filename, 0);
  752. _lastlen = -1;
  753. if (!mod)
  754. {
  755. StringCchCopyW(title, GETFILEINFO_TITLE_LENGTH, filename);
  756. return title;
  757. }
  758. InW_GetFileInfo(mod, (filename == FileName) ? 0 : filename, title, &_lastlen);
  759. if (!title[0])
  760. {
  761. StringCchCopyW(title, titleCch, filename);
  762. if (!PathIsURLW(filename))
  763. {
  764. PathStripPathW(title);
  765. PathRemoveExtensionW(title);
  766. }
  767. }
  768. if (_lastlen != -1) _lastlen /= 1000;
  769. return 0;
  770. }
  771. static wchar_t *GetExtendedTitle( const wchar_t *filename, wchar_t *title, size_t titleCch, bool active = false )
  772. {
  773. AutoCharFn narrowFile( filename );
  774. // make sure the title is inited otherwise it'll be filled/set with rubbish noticeable
  775. // on 5.3+ due to nothing internally using this api now and a retval = 1 done for it
  776. // basically for 3rd party/legacy handling now - 13 May 07
  777. char narrowTitle[ GETFILEINFO_TITLE_LENGTH ] = { 0 };
  778. waHookTitleStruct hts = { 0 };
  779. hts.filename = narrowFile;
  780. hts.length = -1;
  781. hts.title = narrowTitle;
  782. if ( PathIsURLW( filename ) )
  783. {
  784. wchar_t buf[ 32 ] = { 0 };
  785. extendedFileInfoStructW s = { filename, L"streammetadata", buf, sizeof( buf ) / sizeof( wchar_t ) };
  786. if ( 0 != SendMessageW( hMainWindow, WM_WA_IPC, (WPARAM)&s, IPC_GET_EXTENDED_FILE_INFOW_HOOKABLE ) && L'1' == buf[ 0 ] )
  787. {
  788. hts.force_useformatting = 1;
  789. }
  790. }
  791. /*
  792. wchar_t buf[32]=L"";
  793. if (PathIsURLW(filename) && in_get_extended_fileinfoW(filename, L"streammetadata", buf, sizeof(buf)/sizeof(wchar_t)) && buf[0]=='1')
  794. hts.force_useformatting = 1;
  795. */
  796. if ( config_useexttitles && SendMessageW( hMainWindow, WM_WA_IPC, (WPARAM)&hts, IPC_HOOK_TITLES ) )
  797. {
  798. MultiByteToWideCharSZ( CP_ACP, 0, narrowTitle, -1, title, (int)titleCch );
  799. if ( hts.length != -1 )
  800. {
  801. _lastlen = hts.length;
  802. return title;
  803. }
  804. else
  805. {
  806. In_Module *mod = in_setmod_noplay( filename, 0 );
  807. _lastlen = -1;
  808. if ( mod )
  809. {
  810. wchar_t title2[ GETFILEINFO_TITLE_LENGTH ] = { 0 };
  811. InW_GetFileInfo( mod, filename, title2, &_lastlen );
  812. }
  813. if ( _lastlen != -1 ) _lastlen /= 1000;
  814. }
  815. return title;
  816. }
  817. return 0;
  818. }
  819. static wchar_t *GetExtendedTitleW(const wchar_t *filename, wchar_t *title, size_t titleCch, bool active=false)
  820. {
  821. waHookTitleStructW hts = {0};
  822. hts.filename = filename;
  823. hts.length = -1;
  824. hts.title = title;
  825. if (PathIsURLW(filename))
  826. {
  827. wchar_t buf[32] = {0};
  828. extendedFileInfoStructW s = {filename, L"streammetadata", buf, sizeof(buf)/sizeof(wchar_t)};
  829. if (0 != SendMessageW(hMainWindow, WM_WA_IPC, (WPARAM)&s, IPC_GET_EXTENDED_FILE_INFOW_HOOKABLE) &&
  830. L'1' == buf[0])
  831. {
  832. hts.force_useformatting = 1;
  833. }
  834. }
  835. /*
  836. wchar_t buf[32]=L"";
  837. if (PathIsURLW(filename) && in_get_extended_fileinfoW(filename, L"streammetadata", buf, 32) && buf[0]=='1')
  838. hts.force_useformatting = 1;
  839. */
  840. if (config_useexttitles && SendMessageW(hMainWindow, WM_WA_IPC, (WPARAM)&hts, IPC_HOOK_TITLESW))
  841. {
  842. if (hts.length != -1)
  843. {
  844. _lastlen = hts.length;
  845. return title;
  846. }
  847. else
  848. {
  849. In_Module *mod = in_setmod_noplay(filename, 0);
  850. _lastlen = -1;
  851. if (mod)
  852. {
  853. wchar_t title2[GETFILEINFO_TITLE_LENGTH] = {0};
  854. InW_GetFileInfo(mod, filename, title2, &_lastlen);
  855. }
  856. if (_lastlen != -1) _lastlen /= 1000;
  857. }
  858. return title;
  859. }
  860. return 0;
  861. }
  862. wchar_t *PlayList_gettitle(const wchar_t *filename, int useID3)
  863. {
  864. /* OK, this function is going to get ugly */
  865. AutoLock lock (playlistGuard);
  866. static wchar_t title[GETFILEINFO_TITLE_LENGTH] = {0};
  867. CHECK_STUFF();
  868. if (useID3 || (filename[0] && !StrCmpIW(PathFindExtensionW(filename), L"aip")))
  869. {
  870. if (filename[0])
  871. {
  872. wchar_t *ret = GetExtendedTitle(filename, title, GETFILEINFO_TITLE_LENGTH);
  873. if (ret)
  874. return ret;
  875. else
  876. {
  877. ret = GetExtendedTitleW(filename, title, GETFILEINFO_TITLE_LENGTH);
  878. if (ret)
  879. return ret;
  880. else
  881. {
  882. ret = GetFileInfo(filename, title, GETFILEINFO_TITLE_LENGTH);
  883. if (ret)
  884. return ret;
  885. }
  886. }
  887. }
  888. else
  889. {
  890. wchar_t *ret = GetExtendedTitle(FileName, title, GETFILEINFO_TITLE_LENGTH, true);
  891. if (ret)
  892. return ret;
  893. else
  894. {
  895. ret = GetExtendedTitleW(FileName, title, GETFILEINFO_TITLE_LENGTH, true);
  896. if (ret)
  897. return ret;
  898. else
  899. {
  900. ret = GetFileInfo(FileName, title, GETFILEINFO_TITLE_LENGTH);
  901. if (ret)
  902. return ret;
  903. }
  904. }
  905. }
  906. }
  907. else
  908. {
  909. _lastlen = -1;
  910. if (filename[0] && PathIsURLW(filename))
  911. {
  912. if (title) StringCchCopyW(title, GETFILEINFO_TITLE_LENGTH, filename);
  913. }
  914. else if (title)
  915. {
  916. wchar_t buf[1024] = {0};
  917. StringCchCopyW(buf, 1024, filename);
  918. StringCchCopyW(title, GETFILEINFO_TITLE_LENGTH, PathFindFileNameW(buf));
  919. PathRemoveExtensionW(title);
  920. }
  921. }
  922. return title;
  923. }
  924. void PlayList_append_withinfo_curtain(const wchar_t *filename, const wchar_t *title, int length, char *curtain, const wchar_t *ext, int is_nde_string)
  925. {
  926. AutoLock lock (playlistGuard);
  927. bool cached = true;
  928. wchar_t fn[FILENAME_SIZE] = {0};
  929. CHECK_STUFF();
  930. if (!(is_nde_string & 1) && !PathIsURLW(filename))
  931. {
  932. if (wcsstr(fn, L"\\"))
  933. {
  934. int ret = GetLongPathNameW(filename, fn, FILENAME_SIZE);
  935. if (ret && ret < FILENAME_SIZE)
  936. filename=fn;
  937. }
  938. }
  939. if (!title)
  940. {
  941. title = remove_urlcodesW(PlayList_gettitle(filename, (config_riol == 1 ? 1 : 0)));
  942. // changed 5.64 - if we have a length but no title, use the length and build the title
  943. if (length < 0)
  944. {
  945. _lastlen = -1;
  946. // added post 5.65 to ensure read on loading will work
  947. // after some of the other changes made in 5.64 / 5.65
  948. if (config_riol == 1)
  949. {
  950. In_Module *mod = in_setmod_noplay(filename, 0);
  951. if (mod)
  952. {
  953. wchar_t title2[GETFILEINFO_TITLE_LENGTH] = {0};
  954. InW_GetFileInfo(mod, filename, title2, &_lastlen);
  955. if (_lastlen != -1) _lastlen /= 1000;
  956. }
  957. }
  958. }
  959. length = _lastlen;
  960. cached = config_riol == 1;
  961. }
  962. if (list_size && list[list_size - 1].more)
  963. {
  964. // We have the final item to add to the last index
  965. // Add it to the more items list
  966. // Reset more flag
  967. // and do not adjust the list_size;
  968. list[list_size - 1].moreStuff->AddHiddenItem(filename, title, length, list[list_size - 1].more, curtain);
  969. list[list_size - 1].curitems = list[list_size - 1].more;
  970. list[list_size - 1].curindex = 0;
  971. list[list_size - 1].more = 0;
  972. }
  973. else
  974. {
  975. PlayList_allocmem(list_size + 1);
  976. if (list_pos == -1)
  977. list_pos = 0;
  978. list[list_size].Create(length, filename, title, curtain, ext, is_nde_string);
  979. list[list_size].cached = cached ? 1 : 0;
  980. Playlist_rnd_additem();
  981. list_size++;
  982. }
  983. Playlist_notifyModified();
  984. }
  985. void PlayList_append_withinfo(const wchar_t *filename, const wchar_t *title, const wchar_t *ext, int length, int is_nde_string)
  986. {
  987. AutoLock lock (playlistGuard);
  988. PlayList_append_withinfo_curtain(filename, title, length, NULL, ext, is_nde_string);
  989. }
  990. void PlayList_append_withinfo_hidden(const wchar_t *filename, const wchar_t *title, int length, char *curtain/*, char *browser*/)
  991. {
  992. AutoLock lock (playlistGuard);
  993. bool cached = true;
  994. wchar_t fn[FILENAME_SIZE] = {0};
  995. CHECK_STUFF();
  996. if (!PathIsURLW(filename))
  997. {
  998. if (wcsstr(fn, L"\\"))
  999. {
  1000. int ret = GetLongPathNameW(filename, fn, FILENAME_SIZE);
  1001. if (ret && ret < FILENAME_SIZE)
  1002. filename=fn;
  1003. }
  1004. }
  1005. if (!title)
  1006. {
  1007. title = remove_urlcodesW(PlayList_gettitle(filename, (config_riol == 1 ? 1 : 0)));
  1008. // changed 5.64 - if we have a length but no title, use the length and build the title
  1009. if (length < 0)
  1010. {
  1011. _lastlen = -1;
  1012. // added post 5.65 to ensure read on loading will work
  1013. // after some of the other changes made in 5.64 / 5.65
  1014. if (config_riol == 1)
  1015. {
  1016. In_Module *mod = in_setmod_noplay(filename, 0);
  1017. if (mod)
  1018. {
  1019. wchar_t title2[GETFILEINFO_TITLE_LENGTH] = {0};
  1020. InW_GetFileInfo(mod, filename, title2, &_lastlen);
  1021. if (_lastlen != -1) _lastlen /= 1000;
  1022. }
  1023. }
  1024. }
  1025. length = _lastlen;
  1026. cached = config_riol == 1;
  1027. }
  1028. if (list_size && list[list_size - 1].more)
  1029. {
  1030. // add item with info to more list
  1031. // increment more flag so another item may be added to the hidden list
  1032. list[list_size - 1].moreStuff->AddHiddenItem(filename, title, length, list[list_size - 1].more, curtain);
  1033. list[list_size - 1].more++;
  1034. }
  1035. else
  1036. {
  1037. // Create a new entry, but set the more flag. so the next item gets added to
  1038. // more list
  1039. PlayList_allocmem(list_size + 1);
  1040. if (list_pos == -1)
  1041. list_pos = 0;
  1042. list[list_size].CreateMore(length, filename, title, curtain/*, browser*/);
  1043. list[list_size].cached = cached ? 1 : 0;
  1044. Playlist_rnd_additem();
  1045. list_size++;
  1046. }
  1047. Playlist_notifyModified();
  1048. }
  1049. static void _appendcd(const wchar_t *url)
  1050. {
  1051. AutoLock lock (playlistGuard);
  1052. wchar_t buf2[32] = {0};
  1053. in_get_extended_fileinfoW(url, L"ntracks", buf2, 32);
  1054. int n = _wtoi(buf2);
  1055. if (n > 0 && n < 256)
  1056. {
  1057. for (int x = 0; x < n; x ++)
  1058. {
  1059. wchar_t s[64] = {0};
  1060. StringCchPrintfW(s, 64, L"%s,%d",url, x + 1);
  1061. PlayList_append(s, 0);
  1062. }
  1063. }
  1064. }
  1065. void PlayList_check_unknown(const wchar_t *url, int is_nde_string)
  1066. {
  1067. if (!_wcsicmp(extensionW(url), L"")) // no extension, check for pls/m3u signatures
  1068. {
  1069. // TODO: move to its own function
  1070. FILE *fp = _wfopen(url,L"r");
  1071. char buf[21] = {0};
  1072. if (fp)
  1073. {
  1074. fread(buf, 1, 21, fp);
  1075. fclose(fp);
  1076. buf[20] = 0;
  1077. }
  1078. if (!_strnicmp(buf, "[Reference]", 11))
  1079. LoadPlaylistByExtension(url, L".asx", 1, 0);
  1080. else if (!_strnicmp(buf, "[playlist]", 10))
  1081. LoadPlaylistByExtension(url, L".pls", 1, 0);
  1082. else if (!memcmp(buf, L"[playlist]", 20))
  1083. LoadPlaylistByExtension(url, L".pls", 1, 0);
  1084. else if (!_strnicmp(buf, "#EXTM3U", 7))
  1085. LoadPlaylistByExtension(url, L".m3u", 1, 0);
  1086. else if (!_strnicmp(buf, "<ASX", 4))
  1087. LoadPlaylistByExtension(url, L".asx", 1, 0);
  1088. else if (!_strnicmp(buf, "<?wpl", 5))
  1089. LoadPlaylistByExtension(url, L".wpl", 1, 0);
  1090. else if (!_strnicmp(buf, "<?zpl", 5))
  1091. LoadPlaylistByExtension(url, L".zpl", 1, 0);
  1092. else PlayList_append(url, is_nde_string);
  1093. }
  1094. else
  1095. PlayList_append(url, is_nde_string);
  1096. }
  1097. void PlayList_appendthing(const wchar_t *url, int doMIMEcheck, int is_nde_string)
  1098. {
  1099. wchar_t temp2[MAX_PATH] = {0};
  1100. AutoLock lock (playlistGuard);
  1101. if (!_wcsicmp(extensionW(url), L"lnk"))
  1102. {
  1103. if (ResolveShortCut(hMainWindow, url, temp2))
  1104. {
  1105. url = temp2; //StringCchCopy(url, MAX_PATH, temp2);
  1106. is_nde_string &= ~1;
  1107. }
  1108. else
  1109. return ;
  1110. }
  1111. if (!_wcsnicmp(url, L"cda://", 6))
  1112. {
  1113. if ( wcslen(url) == 7)
  1114. _appendcd(url);
  1115. else
  1116. PlayList_append(url, is_nde_string);
  1117. }
  1118. else if (PathIsURLW(url))
  1119. {
  1120. if (LoadPlaylist(url, 1, doMIMEcheck) == -1) // if it's not a playlist file
  1121. PlayList_append(url, is_nde_string);
  1122. }
  1123. else if (!lstrcmpW(url + 1, L":\\"))
  1124. PlayList_adddir(url, 1);
  1125. else
  1126. {
  1127. WIN32_FIND_DATAW d = {0};
  1128. HANDLE h = FindFirstFileW(url, &d);
  1129. if (h != INVALID_HANDLE_VALUE)
  1130. {
  1131. FindClose(h);
  1132. if (d.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  1133. PlayList_adddir(url, 1);
  1134. else if (LoadPlaylist(url, 1, 0) == -1) // if it's not a playlist file
  1135. {
  1136. PlayList_check_unknown(url, is_nde_string); // no extension, check for pls/m3u signatures
  1137. }
  1138. }
  1139. else
  1140. PlayList_append(url, is_nde_string);
  1141. }
  1142. }
  1143. int PlayList_getrepeatcount(int pos)
  1144. {
  1145. AutoLock lock (playlistGuard);
  1146. if ( list_size )
  1147. {
  1148. return list[pos].repeatCount;
  1149. }
  1150. else return 0;
  1151. }
  1152. void PlayList_SetLastItem_RepeatCount(int count)
  1153. {
  1154. AutoLock lock (playlistGuard);
  1155. if ( list_size )
  1156. {
  1157. list[list_size -1].repeatCount = count;
  1158. }
  1159. }
  1160. void PlayList_SetLastItem_Range(unsigned long start, unsigned long end)
  1161. {
  1162. int index = 0;
  1163. if ( list[list_size - 1].curitems ) index = list[list_size - 1].curitems;
  1164. else if ( list[list_size - 1].more - 1 > 0 ) index = list[list_size - 1].more - 1;
  1165. if ( list_size && index )
  1166. {
  1167. list[list_size - 1].moreStuff->SetRange(index, start, end);
  1168. }
  1169. else if ( list_size )
  1170. {
  1171. list[list_size - 1].starttime = start;
  1172. list[list_size - 1].endtime = end;
  1173. }
  1174. Playlist_notifyModified();
  1175. }
  1176. unsigned long PlayList_GetItem_Start(int pos)
  1177. {
  1178. if (pos >= 0 && pos < list_size)
  1179. {
  1180. if ( !list[pos].curindex )
  1181. return list[pos].starttime;
  1182. else
  1183. {
  1184. return list[pos].moreStuff->GetStart(list[pos].curindex);
  1185. }
  1186. }
  1187. else
  1188. return 0;
  1189. }
  1190. unsigned long PlayList_GetItem_End(int pos)
  1191. {
  1192. if (pos >= 0 && pos < list_size)
  1193. {
  1194. if ( !list[pos].curindex )
  1195. return list[pos].endtime;
  1196. else
  1197. {
  1198. return list[pos].moreStuff->GetEnd(list[pos].curindex);
  1199. }
  1200. }
  1201. else
  1202. return 0;
  1203. }
  1204. void PlayList_terminate_lasthidden(void)
  1205. {
  1206. AutoLock lock (playlistGuard);
  1207. if ( list_size && list[list_size - 1].more )
  1208. {
  1209. // Reset more flag
  1210. // and do not adjust the list_size;
  1211. list[list_size - 1].curitems = list[list_size - 1].more - 1;
  1212. list[list_size - 1].curindex = 0;
  1213. list[list_size - 1].more = 0;
  1214. Playlist_notifyModified();
  1215. }
  1216. }
  1217. void PlayList_append(const wchar_t *filename, int is_nde_string)
  1218. {
  1219. AutoLock lock (playlistGuard);
  1220. PlayList_append_withinfo_curtain(filename, NULL, 0, NULL, L"", 0);
  1221. }
  1222. void PlayList_getcurrent_onstop(wchar_t *filename, wchar_t *filetitle)
  1223. {
  1224. AutoLock lock (playlistGuard);
  1225. CHECK_STUFF();
  1226. if (list_pos >= 0 && list_pos < list_size)
  1227. {
  1228. int position = list_pos;
  1229. if (!PlayList_getcached(position))
  1230. {
  1231. _lastlen = -1;
  1232. wchar_t *str = remove_urlcodesW(PlayList_gettitle(list[position].strFile, 1));
  1233. list[position].SetTitle(str);
  1234. PlayList_setcached(position, 1);
  1235. if (_lastlen != -1)
  1236. list[position].length = _lastlen;
  1237. }
  1238. StringCchCopyW(filename, FILENAME_SIZE, list[list_pos].strFile);
  1239. StringCchCopyW(filetitle, FILETITLE_SIZE, list[list_pos].strTitle);
  1240. //StringCchPrintfW(filetitle, FILETITLE_SIZE, L"%d. %s", list_pos + 1, list[list_pos].strTitle);
  1241. }
  1242. else *filename = *filetitle = 0;
  1243. }
  1244. void PlayList_GetCurrentTitle(wchar_t *filetitle, int cchLen)
  1245. {
  1246. AutoLock lock (playlistGuard);
  1247. CHECK_STUFF();
  1248. if (list_pos >= 0 && list_pos < list_size)
  1249. {
  1250. StringCchCopyW(filetitle, cchLen, list[list_pos].strTitle);
  1251. //StringCchPrintfW(filetitle, cchLen, L"%d. %s", list_pos + 1, list[list_pos].strTitle);
  1252. }
  1253. else *filetitle = 0;
  1254. }
  1255. void PlayList_getcurrent(wchar_t *filename, wchar_t *filetitle, wchar_t *filetitlenum)
  1256. {
  1257. AutoLock lock (playlistGuard);
  1258. CHECK_STUFF();
  1259. if (list_pos >= 0 && list_pos < list_size)
  1260. {
  1261. if ( !list[ list_pos ].curindex )
  1262. {
  1263. wa::strings::wa_string l_filename( L"" );
  1264. wa::strings::wa_string l_original_filename( list[ list_pos ].strFile );
  1265. if ( list[ list_pos ].strExt && *list[ list_pos ].strExt && l_original_filename.contains( "://" ) )
  1266. {
  1267. wa::strings::wa_string l_ext( "." );
  1268. l_ext.append( list[ list_pos ].strExt );
  1269. l_ext.toUpper();
  1270. wa::strings::wa_string l_strFile( list[ list_pos ].strFile );
  1271. l_filename.append( "http://client.winamp.com/fileproxy?destination=" );
  1272. l_filename.append( list[ list_pos ].strFile );
  1273. wa::strings::wa_string l_upper_case_filename( l_strFile.GetW() );
  1274. l_upper_case_filename.toUpper();
  1275. if ( !l_upper_case_filename.contains( l_ext.GetW() ) )
  1276. {
  1277. if ( !l_strFile.contains( "?" ) )
  1278. l_filename.append( "?" );
  1279. else
  1280. l_filename.append( "&" );
  1281. l_filename.append( "ext=" );
  1282. l_filename.append( l_ext );
  1283. }
  1284. StringCchCopyW( filename, FILENAME_SIZE, l_filename.GetW().c_str() );
  1285. }
  1286. else
  1287. StringCchCopyW( filename, FILENAME_SIZE, list[ list_pos ].strFile );
  1288. }
  1289. else
  1290. {
  1291. StringCchCopyW(filename, FILENAME_SIZE, list[list_pos].moreStuff->GetHiddenFilename(list[list_pos].curindex));
  1292. }
  1293. StringCchCopyW(filetitle, FILETITLE_SIZE, list[list_pos].strTitle);
  1294. StringCchPrintfW(FileTitleNum, FILETITLE_SIZE, L"%d. %s", list_pos + 1, list[list_pos].strTitle);
  1295. }
  1296. else *filename = *filetitle = 0;
  1297. }
  1298. void PlayList_setcurrent(const wchar_t *filename, wchar_t *filetitle)
  1299. {
  1300. AutoLock lock (playlistGuard);
  1301. PlayList_setitem(list_pos, filename, filetitle);
  1302. }
  1303. void PlayList_setitem(int x, const wchar_t *filename, wchar_t *filetitle)
  1304. {
  1305. AutoLock lock (playlistGuard);
  1306. CHECK_STUFF();
  1307. if (x >= 0 && x < list_size)
  1308. {
  1309. list[x].SetFile(filename);
  1310. // Dont allow update of the title if there is hidden elements
  1311. if ( !list[x].curitems )
  1312. {
  1313. wchar_t *str = remove_urlcodesW(filetitle);
  1314. list[x].SetTitle(str);
  1315. }
  1316. list[x].cached = 1;
  1317. if (x != list_pos) Playlist_notifyModified();
  1318. }
  1319. }
  1320. void PlayList_swap(int e1, int e2)
  1321. {
  1322. AutoLock lock (playlistGuard);
  1323. pl_entry p;
  1324. if (e1 < 0 || e2 < 0 || e1 >= list_size || e2 >= list_size || e1 == e2) return ;
  1325. p = list[e1];
  1326. list[e1] = list[e2];
  1327. list[e2] = p;
  1328. Playlist_notifyModified();
  1329. }
  1330. int PlayList_setposition(int pos)
  1331. {
  1332. AutoLock lock (playlistGuard);
  1333. if (list_pos >= 0 && list_pos < list_size)
  1334. list[list_pos].curindex = 0;
  1335. list_pos = 0;
  1336. if (pos >= 0 && pos < list_size)
  1337. list[pos].curindex = 0;
  1338. return (PlayList_advance(pos));
  1339. }
  1340. int PlayList_getPosition()
  1341. {
  1342. AutoLock lock (playlistGuard); // to let any pending actions finish
  1343. return list_pos;
  1344. }
  1345. int PlayList_getNextPosition()
  1346. {
  1347. AutoLock lock (playlistGuard); // to let any pending actions finish
  1348. // rnd
  1349. int pl_len = PlayList_getlength();
  1350. if (pl_len > 0)
  1351. {
  1352. // repeat track is enabled so will be current one
  1353. if (!config_pladv && config_repeat)
  1354. {
  1355. // manual playlist advance is on so should be the current track
  1356. // (may change but won't unless user does a prev / next action)
  1357. return list_pos;
  1358. }
  1359. else
  1360. {
  1361. // shuffle is off so can determine next entry
  1362. if (!config_shuffle)
  1363. {
  1364. // if at the end then loop to the start
  1365. // otherwise it will be the next entry
  1366. // butif repeat is off then we don't know
  1367. return ((list_pos + 1 >= pl_len) ? (!config_repeat ? -1 : 0) : list_pos + 1);
  1368. }
  1369. // shuffle is on so need to find out from table
  1370. else
  1371. {
  1372. // keep things in check when at end of table
  1373. if (!rnd_listmodified && rnd_i >= 0 && rnd_i + 1 != rnd_lastls)
  1374. {
  1375. return (rnd_rtable ? rnd_rtable[rnd_i + 1] : 0);
  1376. }
  1377. // otherwise if at the end and repeat on go to start
  1378. else
  1379. {
  1380. if(config_repeat)
  1381. {
  1382. return (rnd_rtable ? rnd_rtable[0] : 0);
  1383. }
  1384. }
  1385. }
  1386. }
  1387. }
  1388. // if nothing or only one item then default to the first entry
  1389. else if(!pl_len)
  1390. {
  1391. return (!config_repeat ? -1 : 0);
  1392. }
  1393. // not sure what is the next so fail nicely
  1394. return -1;
  1395. }
  1396. int PlayList_advance(int byval)
  1397. {
  1398. AutoLock lock (playlistGuard);
  1399. CHECK_STUFF();
  1400. if ( byval == HIDDEN_TRAP )
  1401. {
  1402. if ( list_size )
  1403. {
  1404. // Our hidden trap
  1405. if ( list[list_pos].curitems && list[list_pos].curindex < list[list_pos].curitems )
  1406. {
  1407. list[list_pos].curindex++;
  1408. return list_pos;
  1409. }
  1410. byval = 1;
  1411. list[list_pos].curindex = 0;
  1412. if ( list[list_pos].repeatCount && list[list_pos].repeatCurrent++ != list[list_pos].repeatCount )
  1413. {
  1414. return list_pos;
  1415. }
  1416. list[list_pos].repeatCurrent = 0;
  1417. }
  1418. }
  1419. if (!byval) return list_pos;
  1420. //if ( list_size && byval != 1 ) for ( x = 0; x < list_size; x++ ) list[x].curindex = 0;
  1421. //if ( list_size && byval == 1 ) list[list_pos].curindex = 0;
  1422. if (list_size > 1 && list_pos >= 0 && list_pos < list_size)
  1423. {
  1424. list[list_pos].curindex = 0;
  1425. list[list_pos].repeatCurrent = 0;
  1426. }
  1427. list_pos += byval;
  1428. if (list_pos < 0) list_pos = 0;
  1429. else if (list_pos >= list_size) list_pos = list_size - 1;
  1430. else return list_pos;
  1431. return -1;
  1432. }
  1433. void PlayList_addfromdlg(const wchar_t *fns)
  1434. {
  1435. AutoLock lock (playlistGuard);
  1436. WinampPlaylist playlist;
  1437. playlistManager->LoadFromDialog(fns, &playlist);
  1438. }
  1439. void PlayList_getcurrent_tupdate(wchar_t *FileName, wchar_t *FileTitle)
  1440. {
  1441. AutoLock lock (playlistGuard);
  1442. if (!g_has_deleted_current)
  1443. {
  1444. PlayList_refreshtitle(); // removed for evil vbr fix. ? ?
  1445. PlayList_getcurrent(FileName, FileTitle, FileTitleNum);
  1446. }
  1447. else
  1448. {
  1449. _lastlen = -1;
  1450. StringCchCopyW(FileTitle, FILETITLE_SIZE, remove_urlcodesW(PlayList_gettitle(L"", 1)));
  1451. }
  1452. }
  1453. void PlayList_refreshtitle(void)
  1454. {
  1455. AutoLock lock (playlistGuard);
  1456. CHECK_STUFF();
  1457. if (list_pos >= 0 && list_pos < list_size)
  1458. {
  1459. _lastlen = -1;
  1460. // TODO: benski> tag probably needs this, but MN radio can't have it: if ( !list[list_pos].curitems )
  1461. {
  1462. wchar_t *title = remove_urlcodesW(PlayList_gettitle(L"", 1));
  1463. list[list_pos].SetTitle(title);
  1464. }
  1465. PlayList_setcached(list_pos, 1);
  1466. if (_lastlen != -1) list[list_pos].length = _lastlen;
  1467. //JF100500 SendMessageW(hPLWindow,WM_USER+1,0,0);
  1468. }
  1469. }
  1470. const wchar_t *PlayList_GetCachedTitle(const wchar_t *filename)
  1471. {
  1472. AutoLock lock (playlistGuard);
  1473. CHECK_STUFF();
  1474. for (int i = 0;i < list_size;i++)
  1475. {
  1476. if (!lstrcmpiW(filename, list[i].strFile))
  1477. {
  1478. if (list[i].cached)
  1479. {
  1480. _lastlen = list[i].length;
  1481. return list[i].strTitle;
  1482. }
  1483. else
  1484. return 0;
  1485. }
  1486. }
  1487. return 0;
  1488. }
  1489. void PlayList_UpdateTitle(const wchar_t *filename)
  1490. {
  1491. AutoLock lock (playlistGuard);
  1492. CHECK_STUFF();
  1493. for (int i = 0;i < list_size;i++)
  1494. {
  1495. if (!lstrcmpiW(filename, list[i].strFile))
  1496. PlayList_updateitem(i);
  1497. }
  1498. Playlist_notifyModified();
  1499. }
  1500. void PlayList_updaterandpos(void)
  1501. {
  1502. AutoLock lock (playlistGuard);
  1503. //MessageBox(NULL,"updaterandpos","b",0);
  1504. rnd_listmodified = 1;
  1505. }
  1506. void Playlist_rnd_additem()
  1507. {
  1508. AutoLock lock (playlistGuard);
  1509. if (!rnd_listmodified && rnd_rtable && rnd_lastls > 0)
  1510. {
  1511. int x, newpos;
  1512. rnd_lastls++;
  1513. int *newtable = (int*)realloc(rnd_rtable, sizeof(int) * (rnd_lastls));
  1514. if (!newtable)
  1515. {
  1516. newtable = (int*)malloc(sizeof(int) * (rnd_lastls));
  1517. if (newtable)
  1518. {
  1519. memcpy(newtable, rnd_rtable, sizeof(int)*(rnd_lastls - 1));
  1520. free(rnd_rtable);
  1521. rnd_rtable = newtable;
  1522. }
  1523. else return ;
  1524. }
  1525. else rnd_rtable = newtable;
  1526. newpos = warand() % rnd_lastls;
  1527. for (x = rnd_lastls - 1; x > newpos; x --)
  1528. {
  1529. rnd_rtable[x] = rnd_rtable[x - 1];
  1530. if (rnd_rtable[x] >= list_size) rnd_rtable[x]++;
  1531. }
  1532. rnd_rtable[x] = list_size;
  1533. while (x > 0)
  1534. {
  1535. x--;
  1536. if (rnd_rtable[x] >= list_size) rnd_rtable[x]++;
  1537. }
  1538. }
  1539. else rnd_listmodified = 1;
  1540. }
  1541. void Playlist_rnd_deleteitem(int item)
  1542. {
  1543. AutoLock lock (playlistGuard);
  1544. if (!rnd_listmodified && rnd_rtable && rnd_lastls > 0)
  1545. {
  1546. for (int x = 0; x < rnd_lastls; x ++)
  1547. {
  1548. if (rnd_rtable[x] > item)
  1549. {
  1550. rnd_rtable[x]--;
  1551. }
  1552. else if (rnd_rtable[x] == item)
  1553. {
  1554. std::rotate(&rnd_rtable[x], &rnd_rtable[x + 1], &rnd_rtable[rnd_lastls]);
  1555. if (rnd_rtable[x] > item) rnd_rtable[x]--;
  1556. if (rnd_i >= x) rnd_i--;
  1557. if (rnd_i < 0) rnd_i = 0;
  1558. rnd_lastls--;
  1559. }
  1560. }
  1561. }
  1562. else rnd_listmodified = 1;
  1563. }
  1564. static int Rand(int n)
  1565. {
  1566. return warand() % n ;
  1567. }
  1568. int PlayList_randpos(int x)
  1569. {
  1570. AutoLock lock (playlistGuard);
  1571. int ls;
  1572. if (x == -666) // exiting winamp
  1573. {
  1574. rnd_i = 0;
  1575. free(rnd_rtable);
  1576. rnd_rtable = 0;
  1577. return 0;
  1578. }
  1579. if (ls = PlayList_getlength())
  1580. {
  1581. if (ls != rnd_lastls || rnd_listmodified)
  1582. {
  1583. int first_run = (rnd_lastls < 0);
  1584. //init table;
  1585. rnd_lastls = ls;
  1586. rnd_listmodified = 0;
  1587. if (!ls) return 0;
  1588. free(rnd_rtable);
  1589. rnd_rtable = (int *) malloc(ls * sizeof(int));
  1590. for (x = 0; x < ls; x ++)
  1591. {
  1592. rnd_rtable[x] = x;
  1593. }
  1594. x = 0;
  1595. if (first_run && list_pos >= 0 && list_pos < ls)
  1596. {
  1597. // at startup, go back to current song from last session:
  1598. rnd_rtable[list_pos] = 0;
  1599. rnd_rtable[0] = list_pos;
  1600. x = 1; // (don't re-randomize this entry)
  1601. }
  1602. std::random_shuffle(&rnd_rtable[x], &rnd_rtable[ls], Rand);
  1603. rnd_i = 0;
  1604. }
  1605. else rnd_i += x;
  1606. if (rnd_i >= ls)
  1607. {
  1608. if (config_repeat)
  1609. {
  1610. rnd_i = 0;
  1611. if (ls > 2)
  1612. {
  1613. // note: config_shuffle_morph_rate==0 means slow morph, and minimal changes to playlist each time through [window_size==2]
  1614. // config_shuffle_morph_rate==50 means fast morph, and guarantees that HALF of the other songs will play before any one song repeats [ideal]
  1615. int window_size = max(MulDiv(ls, config_shuffle_morph_rate, 100), 2);
  1616. for (x = 0; x < ls - 1; x ++)
  1617. {
  1618. std::random_shuffle(&rnd_rtable[x], &rnd_rtable[min(x + window_size, ls)], Rand);
  1619. }
  1620. }
  1621. }
  1622. else rnd_i = ls - 1;
  1623. PlayList_setposition(rnd_rtable[rnd_i]);
  1624. return 1;
  1625. }
  1626. if (rnd_i < 0)
  1627. {
  1628. rnd_i = 0;
  1629. PlayList_setposition(rnd_rtable[rnd_i]);
  1630. return 1;
  1631. }
  1632. PlayList_setposition(rnd_rtable[rnd_i]);
  1633. }
  1634. else rnd_lastls = 0;
  1635. return 0;
  1636. }
  1637. static bool PlayList_sortByFile(pl_entry &a, pl_entry &b) //const void *a, const void *b)
  1638. {
  1639. const wchar_t *file1 = PathFindFileNameW(a.strFile);
  1640. const wchar_t *file2 = PathFindFileNameW(b.strFile);
  1641. // int comp = CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE | /*NORM_IGNOREKANATYPE |*/ NORM_IGNOREWIDTH, file1, -1, file2 , -1);
  1642. //return comp == CSTR_LESS_THAN;
  1643. return FileCompareLogical(file1, file2) < 0;
  1644. }
  1645. static bool PlayList_sortByTitle(pl_entry &a, pl_entry &b)
  1646. {
  1647. // int comp = CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE/*|NORM_IGNOREKANATYPE*/|NORM_IGNOREWIDTH, a.strTitle, -1, b.strTitle, -1);
  1648. // return comp == CSTR_LESS_THAN;
  1649. return CompareStringLogical(a.strTitle, b.strTitle) < 0;
  1650. }
  1651. static bool PlayList_sortByDirectory(pl_entry &a, pl_entry &b) // by dir, then by title
  1652. {
  1653. const wchar_t *directory1 = a.strFile;
  1654. const wchar_t *directory2 = b.strFile;
  1655. const wchar_t *directoryEnd1 = scanstr_backcW(directory1, L"\\", 0);
  1656. const wchar_t *directoryEnd2 = scanstr_backcW(directory2 , L"\\", 0);
  1657. size_t dirLen1 = directoryEnd1 - directory1;
  1658. size_t dirLen2 = directoryEnd2 - directory2;
  1659. if (!dirLen1 && !dirLen2) // both in the current directory?
  1660. return PlayList_sortByFile(a, b); // not optimized, because the function does another scanstr_back, but easy for now :)
  1661. if (!dirLen1) // only the first dir is empty?
  1662. return true; // sort it first
  1663. if (!dirLen2) // only the second dir empty?
  1664. return false; // empty dirs go first
  1665. int comp = FileCompareLogicalN(directory1, dirLen1, directory2, dirLen2);
  1666. if (comp == 0)
  1667. return PlayList_sortByFile(a, b);
  1668. else
  1669. return comp < 0;
  1670. #if 0 // old way
  1671. int comp = CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE | /*NORM_IGNOREKANATYPE | */NORM_IGNOREWIDTH, directory1, dirLen1, directory2 , dirLen2);
  1672. if (comp == CSTR_EQUAL) // same dir
  1673. return PlayList_sortByFile(a, b); // do second sort
  1674. else // different dirs
  1675. return comp == CSTR_LESS_THAN;
  1676. #endif
  1677. }
  1678. void PlayList_mark()
  1679. {
  1680. for (int x = 0; x < list_size; x++)
  1681. list[x].tmp = 0;
  1682. list[list_pos].tmp = 1;
  1683. }
  1684. void PlayList_restore()
  1685. {
  1686. for (int x = 0; x < list_size; x ++)
  1687. {
  1688. if (list[x].tmp)
  1689. {
  1690. list_pos = x;
  1691. break;
  1692. }
  1693. }
  1694. }
  1695. void PlayList_sort(int bytitle, int start_p)
  1696. {
  1697. AutoLock lock (playlistGuard);
  1698. if (start_p >= list_size || list_size < 2 || !list) return ;
  1699. PlayList_mark();
  1700. if (bytitle == 0)
  1701. std::sort(&list[start_p], &list[list_size], PlayList_sortByFile);
  1702. else if (bytitle == 1)
  1703. std::sort(&list[start_p], &list[list_size], PlayList_sortByTitle);
  1704. else
  1705. std::sort(&list[start_p], &list[list_size], PlayList_sortByDirectory);
  1706. PlayList_restore();
  1707. Playlist_notifyModified();
  1708. }
  1709. void PlayList_reverse(void)
  1710. {
  1711. AutoLock lock (playlistGuard);
  1712. if (!list || list_size <= 1)
  1713. return ;
  1714. PlayList_mark();
  1715. std::reverse(list, &list[list_size]);
  1716. PlayList_restore();
  1717. Playlist_notifyModified();
  1718. }
  1719. void PlayList_randomize(void)
  1720. {
  1721. AutoLock lock (playlistGuard);
  1722. if (!list || list_size <= 1)
  1723. return ;
  1724. PlayList_mark();
  1725. std::random_shuffle(list, &list[list_size], Rand);
  1726. PlayList_restore();
  1727. Playlist_notifyModified();
  1728. }
  1729. void PlayList_adddir(const wchar_t *path, int recurse)
  1730. {
  1731. if (playlistManager)
  1732. {
  1733. AutoLock lock (playlistGuard);
  1734. WinampDirectoryLoad dir(recurse!=0, 0);
  1735. WinampPlaylist playlist(0);
  1736. playlistManager->LoadDirectory(path, &playlist, &dir);
  1737. }
  1738. }
  1739. void PlayList_makerelative(const wchar_t *listfile, wchar_t *filename, int useBase)
  1740. {
  1741. if (useBase)
  1742. MakeRelativePathName(filename, filename, M3UBASE);
  1743. else
  1744. {
  1745. wchar_t path[MAX_PATH] = {0};
  1746. StringCchCopyW(path, MAX_PATH, listfile);
  1747. PathRemoveFileSpecW(path);
  1748. PathRemoveBackslashW(path);
  1749. MakeRelativePathName(filename, filename, path);
  1750. }
  1751. }
  1752. }
  1753. static void Playlist_notifyModified()
  1754. {
  1755. plneedsave = plmodified = 1;
  1756. SendMessageW(hMainWindow, WM_WA_IPC, 0, IPC_PLAYLIST_MODIFIED);
  1757. }
  1758. int IsPlaylistExtension(const wchar_t *ext)
  1759. {
  1760. wchar_t temp[32] = {0};
  1761. StringCchPrintfW(temp, 32, L"hi.%s", ext);
  1762. if (playlistManager && playlistManager->CanLoad(temp)) // TODO: make a "valid extension" method
  1763. return 1;
  1764. else
  1765. return 0;
  1766. }
  1767. int IsPlaylistExtensionA(const char *ext)
  1768. {
  1769. return IsPlaylistExtension(AutoWide(ext));
  1770. }
  1771. void GetMIMEType(const char *url, char *mimeType, int mimeTypeCch);
  1772. int LoadPlaylistByExtension(const wchar_t *fn, const wchar_t *ext, int whattodo, int useBase)
  1773. {
  1774. if (!playlistManager || !playlistManager->CanLoad(ext))
  1775. return -1;
  1776. //if (useBase)
  1777. // WASABI_API_APP->path_setWorkingPath(M3UBASE);
  1778. //cut: SetCurrentDirectoryW(M3UBASE);
  1779. int i = 0;
  1780. if (PathIsURLW(fn))
  1781. {
  1782. int rval = 1;
  1783. if (!httpRetrieveFileW(hMainWindow, AutoChar(fn), TEMP_FILE, getStringW(IDS_RETRPL, NULL, 0)))
  1784. {
  1785. rval = LoadPlaylistByExtension(TEMP_FILE, ext, whattodo, useBase);
  1786. }
  1787. DeleteFileW(TEMP_FILE);
  1788. return rval;
  1789. }
  1790. if (PlayList_getlength())
  1791. {
  1792. if (whattodo < 1)
  1793. {
  1794. PlayList_delete();
  1795. i = 1;
  1796. }
  1797. }
  1798. WinampPlaylist playlist(useBase?M3UBASE:0, !useBase);
  1799. playlistManager->LoadAs(fn, ext, &playlist);
  1800. if (i)
  1801. {
  1802. StopPlaying(0);
  1803. if (config_shuffle) PlayList_randpos( -BIGINT);
  1804. PlayList_getcurrent(FileName, FileTitle, FileTitleNum);
  1805. if (whattodo == 0) StartPlaying();
  1806. }
  1807. return 0;
  1808. }
  1809. int LoadPlaylist(const wchar_t *filename, int whattodo, int doMIMEcheck)
  1810. {
  1811. wchar_t ext[65]=L".";
  1812. extension_exW(filename, ext+1, 64); // TODO: make api_playlistmanager use this function
  1813. int x = LoadPlaylistByExtension(filename, ext, whattodo, 0);
  1814. if (doMIMEcheck && x == -1 && PathIsURLW(filename))
  1815. {
  1816. /*
  1817. benski> disabled for now
  1818. GetMIMEType(AutoChar(filename), ext, 64);
  1819. if (*ext)
  1820. {
  1821. if (!_stricmp(ext, "video/x-ms-asf"))
  1822. return LoadPlaylistByExtension(filename, L".asx", whattodo, 0); // TODO: make "load playlist by MIME type" API
  1823. else if (!_stricmp(ext, "video/asx"))
  1824. return LoadPlaylistByExtension(filename, L".asx", whattodo, 0);
  1825. }
  1826. */
  1827. }
  1828. return x;
  1829. }
  1830. void PlayList_insert(int position, const wchar_t *filename)
  1831. {
  1832. AutoLock lock (playlistGuard);
  1833. PlayList_saveend(position);
  1834. PlayList_appendthing(filename, 0, 0);
  1835. PlayList_restoreend();
  1836. }