1
0

notifier.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. #include <lib/std.mi>
  2. #include "attribs.m"
  3. Function reset();
  4. Function createNotifier(boolean cancel);
  5. Function showNotifier(Int w);
  6. Function onNext();
  7. function cancelAnimation();
  8. Function Int fillNextTrackInfo(String corneroverride);
  9. Function Int fillCustomInfo(String customstring);
  10. Function prepareAlbumArtNotifier();
  11. Function checkPref(int bypassfs);
  12. Function String getArtist();
  13. Function onAlbumArt(Boolean success);
  14. Global Container notifier_container;
  15. Global Layout notifier_layout;
  16. Global Timer notifier_timer;
  17. Global String last_autotitle, cur_status;
  18. Global AlbumArtLayer cover;
  19. Global boolean handleAACalback = false;
  20. Global Boolean triggerBug; //BUGFIX remove this
  21. Global Timer fallbackTempFix; //BUGFIX remove this
  22. Global Boolean b_tohide = 0;
  23. Global int width = 0;
  24. Global int left = 0;
  25. #define WEBCOVER_SHOUTCAST "winamp.cover.shoutcast"
  26. #define FORCE_BUG_MODE
  27. // ------------------------------------------------------------------------------
  28. // init
  29. // ------------------------------------------------------------------------------
  30. System.onScriptLoaded() {
  31. initAttribs();
  32. notifier_timer = new Timer;
  33. fallbackTempFix = new Timer; //BUGFIX remove this
  34. fallbackTempFix.setDelay(3000); //BUGFIX remove this / Time that onAlbumArtLoaded have to execute before bug mode is ON
  35. }
  36. // ------------------------------------------------------------------------------
  37. // shutdown
  38. // ------------------------------------------------------------------------------
  39. System.onScriptUnloading() {
  40. delete notifier_timer;
  41. delete fallbackTempFix; //BUGFIX remove this
  42. }
  43. // ------------------------------------------------------------------------------
  44. // called by the system when the global hotkey for notification is pressed
  45. // ------------------------------------------------------------------------------
  46. System.onShowNotification() {
  47. //if (checkPref(1)) return; --mp: if we push the hotkey, we want to show the notifier, no matter what the pref settings are.
  48. createNotifier(false);
  49. if (getStatus() == STATUS_PLAYING) cur_status = "Playing";
  50. if (getStatus() == STATUS_PAUSED) cur_status = "Playback Paused";
  51. if (getStatus() == STATUS_STOPPED) cur_status = "Playback Stopped";
  52. prepareAlbumArtNotifier();
  53. complete; // prevents other scripts from getting the message
  54. return 1; // tells anybody else that might watch the returned value that, yes, we implemented that
  55. }
  56. // ------------------------------------------------------------------------------
  57. // called by the system when the title for the playing item changes, this could be the result of the player
  58. // going to the next track, or of an update in the track title
  59. // ------------------------------------------------------------------------------
  60. System.onTitleChange(String newtitle) {
  61. if (last_autotitle == newtitle) return;
  62. if (StrLeft(newtitle, 1) == "[") {
  63. if (StrLeft(newtitle, 7) == "[Buffer" ||
  64. StrLeft(newtitle, 4) == "[ICY") return;
  65. }
  66. last_autotitle = newtitle;
  67. onNext();
  68. fallbackTempFix.stop(); //BUGFIX remove later
  69. fallbackTempFix.start(); //BUGFIX remove later
  70. }
  71. // ------------------------------------------------------------------------------
  72. // called by the system when the user clicks the next button
  73. // ------------------------------------------------------------------------------
  74. onNext() {
  75. if (checkPref(0)) return;
  76. createNotifier(false);
  77. cur_status = "";
  78. prepareAlbumArtNotifier();
  79. }
  80. // ------------------------------------------------------------------------------
  81. // called by the system when the user clicks the play button
  82. // ------------------------------------------------------------------------------
  83. System.onPlay() {
  84. if (checkPref(0)) return;
  85. createNotifier(false);
  86. cur_status = "Playing";
  87. prepareAlbumArtNotifier();
  88. }
  89. // ------------------------------------------------------------------------------
  90. // called by the system when the user clicks the pause button
  91. // ------------------------------------------------------------------------------
  92. System.onPause() {
  93. if (checkPref(0)) return;
  94. DebugString("onPause",9);
  95. createNotifier(true);
  96. showNotifier(fillCustomInfo("Playback Paused"));
  97. }
  98. // ------------------------------------------------------------------------------
  99. // called by the system when the user clicks the pause button again
  100. // ------------------------------------------------------------------------------
  101. System.onResume() {
  102. if (checkPref(0)) return;
  103. DebugString("onResume",9);
  104. createNotifier(false);
  105. cur_status = "Resuming Playback";
  106. prepareAlbumArtNotifier();
  107. }
  108. // ------------------------------------------------------------------------------
  109. // called by the system when the user clicks the play button
  110. // ------------------------------------------------------------------------------
  111. System.onStop() {
  112. if (checkPref(0)) return;
  113. createNotifier(true);
  114. showNotifier(fillCustomInfo("End of Playback"));
  115. }
  116. // ------------------------------------------------------------------------------
  117. // checks if we should display anything
  118. // ------------------------------------------------------------------------------
  119. Int checkPref(int bypassfs) {
  120. if (!bypassfs && notifier_disablefullscreen_attrib.getData() == "1" && isVideoFullscreen()) return 1;
  121. if (notifier_never_attrib.getData() == "1") return 1;
  122. if (notifier_minimized_attrib.getData() == "1" && !isMinimized()) return 1;
  123. if (notifier_windowshade_attrib.getData() == "1") {
  124. if (isMinimized()) return 0;
  125. Container c = getContainer("main");
  126. if (!c) return 1;
  127. Layout l = c.getCurLayout();
  128. if (!l) return 1;
  129. if (l.getId() != "shade") return 1;
  130. }
  131. return 0;
  132. }
  133. // ------------------------------------------------------------------------------
  134. // fade in/out completed
  135. // ------------------------------------------------------------------------------
  136. notifier_layout.onTargetReached() {
  137. int a = notifier_layout.getAlpha();
  138. if (a == 255) {
  139. notifier_timer.setDelay(StringToInteger(notifier_holdtime_attrib.getData()));
  140. notifier_timer.start();
  141. }
  142. else if (a == 0) {
  143. reset();
  144. }
  145. }
  146. // ------------------------------------------------------------------------------
  147. // hold time elapsed
  148. // ------------------------------------------------------------------------------
  149. notifier_timer.onTimer() {
  150. stop();
  151. if (notifier_layout.isTransparencySafe()) {
  152. notifier_layout.setTargetA(0);
  153. notifier_layout.setTargetSpeed(StringToInteger(notifier_fadeouttime_attrib.getData()) / 1000);
  154. notifier_layout.gotoTarget();
  155. } else {
  156. reset();
  157. }
  158. }
  159. // ------------------------------------------------------------------------------
  160. // when notifier is clicked, bring back the app from minimized state if its minimized and focus it
  161. // ------------------------------------------------------------------------------
  162. notifier_layout.onLeftButtonDown(int x, int y) {
  163. cancelAnimation();
  164. restoreApplication();
  165. activateApplication();
  166. /*if (notifier_opennowplaying_attrib.getData() == "1")
  167. {
  168. String artist = getArtist();
  169. if (artist == "") return;
  170. System.navigateUrlBrowser("http://client.winamp.com/nowplaying/artist/?icid=notifiermodern&artistName=" + artist);
  171. }*/
  172. reset();
  173. }
  174. notifier_layout.onRightButtonUp(int x, int y) {
  175. cancelAnimation();
  176. reset();
  177. complete;
  178. return;
  179. }
  180. //TODO merge w/ code below
  181. String getArtist ()
  182. {
  183. String artist = getPlayItemMetaDataString("artist");
  184. if (artist == "") artist = getPlayItemMetaDataString("uvox/artist");
  185. if (artist == "") artist = getPlayItemMetaDataString("cbs/artist");
  186. if (artist == "") artist = getPlayItemMetaDataString("streamtitle");
  187. if (artist == "") artist = getPlayItemDisplayTitle();
  188. return artist;
  189. }
  190. // ------------------------------------------------------------------------------
  191. // close the notifier window, destroys the container automatically because it's dynamic
  192. // ------------------------------------------------------------------------------
  193. reset() {
  194. notifier_container.close();
  195. notifier_container = NULL;
  196. notifier_layout = NULL;
  197. handleAACalback = FALSE;
  198. }
  199. // ------------------------------------------------------------------------------
  200. createNotifier(boolean cancel) {
  201. if (notifier_container == NULL) {
  202. notifier_container = newDynamicContainer("notifier");
  203. if (!notifier_container) return; // reinstall duh!
  204. if (isDesktopAlphaAvailable())
  205. notifier_layout = notifier_container.getLayout("desktopalpha");
  206. else
  207. notifier_layout = notifier_container.getLayout("normal");
  208. if (!notifier_layout) return; // reinstall twice, man
  209. } else if (cancel) {
  210. cancelAnimation();
  211. }
  212. }
  213. cancelAnimation()
  214. {
  215. notifier_layout.cancelTarget();
  216. notifier_timer.stop();
  217. }
  218. // ------------------------------------------------------------------------------
  219. showNotifier(int w) {
  220. w = w + 32;
  221. Layout m = getContainer("main").getCurLayout();
  222. int x = left = getViewportWidthFromGuiObject(m) + getViewportLeftFromGuiObject(m) - w - 2;
  223. int y = getViewportHeightFromGuiObject(m) + getViewportTopFromGuiObject(m) - 80 - 2;
  224. // show if not there or if already shown then lets resize on the fly (bento style)
  225. if (!notifier_layout.isVisible()) {
  226. notifier_layout.resize(x, y, w, 80);
  227. }
  228. else {
  229. notifier_layout.resize(notifier_layout.getguiX(), y, notifier_layout.getGuiW(), 80);
  230. }
  231. if (notifier_layout.isTransparencySafe()) {
  232. notifier_layout.show();
  233. notifier_layout.setTargetA(255);
  234. notifier_layout.setTargetX(x);
  235. notifier_layout.setTargetY(y);
  236. notifier_layout.setTargetW(w);
  237. notifier_layout.setTargetH(80);
  238. notifier_layout.setTargetSpeed(StringToInteger(notifier_fadeintime_attrib.getData()) / 1000);
  239. notifier_layout.gotoTarget();
  240. } else {
  241. notifier_layout.setAlpha(255);
  242. notifier_layout.show();
  243. notifier_timer.setDelay(StringToInteger(notifier_holdtime_attrib.getData()));
  244. notifier_timer.start();
  245. }
  246. }
  247. // ------------------------------------------------------------------------------
  248. prepareAlbumArtNotifier()
  249. {
  250. if (!notifier_layout) return;
  251. Group g_albumart = notifier_layout.findObject("notifier.albumart");
  252. DebugString("prepareAlbumArtNotifier: handleAACalback="+integerToString(handleAACalback), 9);
  253. if (g_albumart)
  254. {
  255. cover = g_albumart.findObject("notifier.cover");
  256. DebugString("prepareAlbumArtNotifier: cover.isLoading="+integerToString(cover.isLoading()), 9);
  257. DebugString("prepareAlbumArtNotifier: cover.isInvalid="+integerToString(cover.isInvalid()), 9);
  258. handleAACalback = true;
  259. cover.refresh();
  260. }
  261. }
  262. cover.onAlbumArtLoaded(boolean success)
  263. {
  264. /*
  265. Created a seperate function for the code that was here because for some reason I couldn't force this
  266. event (from the fallbackTempFix.onTimer) with cover.onAlbumArtLoaded(success) after the Winamp bug appears.
  267. Weird, yes.
  268. */
  269. FORCE_BUG_MODE onAlbumArt(success);
  270. }
  271. // ------------------------------------------------------------------------------
  272. Int fillNextTrackInfo(String corneroverride) {
  273. Int maxv = 0;
  274. Int stream = 0;
  275. if (!notifier_layout) return 0;
  276. Group g_text = notifier_layout.findObject("notifier.text");
  277. Group g_albumart = notifier_layout.findObject("notifier.albumart");
  278. Text plentry = g_text.findObject("plentry");
  279. Text nexttrack = g_text.findObject("nexttrack");
  280. Text _title = g_text.findObject("title");
  281. Text album = g_text.findObject("album");
  282. Text artist = g_text.findObject("artist");
  283. Text endofplayback = notifier_layout.findObject("endofplayback");
  284. DebugString("got callback for " + getPlayItemString(), 0);
  285. // Get Stream Name - if no stream returns ""
  286. string s = getPlayItemMetaDataString("streamname");
  287. string stype = getPlayItemMetaDataString("streamtype"); //"streamtype" will return "2" for SHOUTcast and "5" for SHOUTcast 2
  288. if (stype == "2" || stype == "5") stream = 1;
  289. if (endofplayback) endofplayback.hide();
  290. if (plentry)
  291. {
  292. plentry.setText(integerToString(getPlaylistIndex()+1)+translate(" of ")+integerToString(getPlaylistLength()));
  293. plentry.show();
  294. }
  295. if (nexttrack) {
  296. if (corneroverride == "") {
  297. if (!stream) {
  298. if (!isVideo())
  299. {
  300. nexttrack.setText("New track");
  301. }
  302. else
  303. {
  304. nexttrack.setText("New video");
  305. }
  306. }
  307. else
  308. {
  309. nexttrack.setText("On air");
  310. }
  311. }
  312. else
  313. {
  314. nexttrack.setText(corneroverride);
  315. }
  316. nexttrack.show();
  317. }
  318. string set_artist = "";
  319. string set = "";
  320. if (_title) {
  321. String str;
  322. if (!stream)
  323. {
  324. _title.setXmlParam("ticker", "0");
  325. _title.setXmlParam("display", "");
  326. str = getPlayitemMetaDataString("title");
  327. if (str == "") str = getPlayitemDisplayTitle();
  328. String l = getPlayItemMetaDataString("length");
  329. if (l != "") {
  330. str += " (" + integerToTime(stringtointeger(l)) + ")";
  331. }
  332. _title.setText(str);
  333. }
  334. else
  335. {
  336. if (str = getPlayItemMetaDataString("streamtitle") != "")
  337. {
  338. int v = strsearch(str, " - "); // We divide the string by a " - " sublimiter - no luck for old / wrong tagged stations
  339. if (v > 0) {
  340. set_artist = strleft (str, v); // Store artist
  341. string str = strright (str, strlen(str) - 3 - v);
  342. _title.setText(str);
  343. }
  344. else
  345. {
  346. _title.setXmlParam("ticker", "1"); // These titles can be _very_ long
  347. _title.setText(str);
  348. }
  349. } else
  350. {
  351. _title.setXmlParam("ticker", "1");
  352. _title.setXmlParam("display", "songtitle");
  353. _title.setText("");
  354. }
  355. }
  356. _title.show();
  357. }
  358. if (artist) {
  359. if (!stream) {
  360. if (isVideo())
  361. {
  362. artist.setText("");
  363. }
  364. else
  365. {
  366. artist.setText(getPlayitemMetaDataString("artist"));
  367. }
  368. }
  369. else
  370. {
  371. // Perhaps we've stored the artist before?
  372. if (set_artist != "")
  373. {
  374. artist.setText(set_artist);
  375. }
  376. // Then display the station name
  377. else if (s != "")
  378. {
  379. artist.setText(s);
  380. }
  381. // So, we've had no luck - just display a static text :(
  382. else
  383. {
  384. if (isVideo())
  385. {
  386. artist.setText("Internet TV");
  387. }
  388. else
  389. {
  390. artist.setText("Internet Radio");
  391. }
  392. }
  393. }
  394. artist.show();
  395. }
  396. if (album) {
  397. String str;
  398. if (!stream && !isVideo()) {
  399. album.setXmlParam("display", "");
  400. str = getPlayitemMetaDataString("album");
  401. String l = getPlayitemMetaDataString("track");
  402. if (l != "" && l != "-1") str += " (" + translate("Track ") + l + ")";
  403. album.setText(str);
  404. }
  405. else
  406. {
  407. album.setXmlParam("display", "");
  408. // we have divided the songname - let's display the station name
  409. if (set_artist != "" && s != "")
  410. {
  411. album.setText(s);
  412. }
  413. // no luck either...
  414. else
  415. {
  416. album.setText("");
  417. album.setXmlParam("display", "songinfo_localise");
  418. }
  419. }
  420. album.show();
  421. }
  422. // Album Art Stuff
  423. Layer webcover;
  424. if (g_albumart)
  425. {
  426. cover = g_albumart.findObject("notifier.cover");
  427. webcover = g_albumart.findObject("notifier.webcover");
  428. }
  429. Boolean showAlbumArt = FALSE;
  430. if (cover != NULL && webcover != NULL && notifier_artworkinnotification_attrib.getData() == "1")
  431. {
  432. if (stream)
  433. {
  434. if(stype == "2" || stype == "5" && cover.isInvalid())
  435. {
  436. webcover.setXMLParam("image", WEBCOVER_SHOUTCAST);
  437. cover.hide();
  438. webcover.show();
  439. showAlbumArt = TRUE;
  440. }
  441. else if(stype == "5" && !cover.isInvalid())
  442. {
  443. webcover.hide();
  444. cover.show();
  445. showAlbumArt = TRUE;
  446. }
  447. }
  448. else
  449. {
  450. if (cover.isInvalid()) // Check if the album art obj shows a pic
  451. {
  452. showAlbumArt = FALSE;
  453. }
  454. else
  455. {
  456. webcover.hide();
  457. cover.show();
  458. showAlbumArt = TRUE;
  459. }
  460. }
  461. }
  462. if (showAlbumArt)
  463. {
  464. if (g_albumart) g_albumart.show();
  465. if (g_text) g_text.setXmlParam("x", "75");
  466. if (g_text) g_text.setXmlParam("w", "-95");
  467. }
  468. else
  469. {
  470. if (g_albumart) g_albumart.hide();
  471. if (g_text) g_text.setXmlParam("x", "15");
  472. if (g_text) g_text.setXmlParam("w", "-35");
  473. }
  474. if (g_text) g_text.show();
  475. maxv = artist.getAutoWidth();
  476. if (maxv < album.getAutoWidth()) maxv = album.getAutoWidth();
  477. if (maxv < _title.getAutoWidth()) maxv = _title.getAutoWidth();
  478. if (maxv < (nexttrack.getAutoWidth() + plentry.getAutoWidth() - 5) ) maxv = nexttrack.getAutoWidth() + plentry.getAutoWidth() - 5;
  479. if (maxv < 128) maxv = 128;
  480. Layout m = getContainer("main").getCurLayout();
  481. if (maxv > getViewportWidthFromGuiObject(m)/4) maxv = getViewportWidthFromGuiObject(m)/4;
  482. width = maxv;
  483. return maxv + ( showAlbumArt * 91 );
  484. }
  485. // ------------------------------------------------------------------------------
  486. Int fillCustomInfo(String customtext)
  487. {
  488. Group p = notifier_layout;
  489. Group g_text = p.findObject("notifier.text");
  490. Group g_albumart = p.findObject("notifier.albumart");
  491. Text plentry = p.findObject("plentry");
  492. Text nexttrack = p.findObject("nexttrack");
  493. Text _title = p.findObject("title");
  494. Text album = p.findObject("album");
  495. Text artist = p.findObject("artist");
  496. Text endofplayback = p.findObject("endofplayback");
  497. if (g_text) { g_text.hide(); }
  498. if (g_albumart) g_albumart.hide();
  499. if (plentry) { plentry.hide(); }
  500. if (nexttrack) nexttrack.hide();
  501. if (_title) { _title.hide(); }
  502. if (artist) { artist.hide(); }
  503. if (album) { album.hide(); }
  504. if (endofplayback != NULL /*&& s_endofplayback != NULL*/) {
  505. endofplayback.setText(translate(customtext)+" ");
  506. //s_endofplayback.setText(translate(customtext)+" ");
  507. int aw = endofplayback.getAutoWidth();
  508. endofplayback.show();
  509. //s_endofplayback.show();
  510. if (aw > 128)
  511. return aw;
  512. }
  513. return 128;
  514. }
  515. //BUGFIX remove this timer later
  516. fallbackTempFix.onTimer() //As soon as this timer runs, bug mode is ON ;)
  517. {
  518. if (checkPref(0)) return;
  519. if (!notifier_layout) onNext();
  520. if(!triggerBug)
  521. {
  522. triggerBug = true;
  523. onAlbumArt(cover.isInvalid()); //First time we see the bug
  524. fallbackTempFix.setDelay(30);
  525. DebugString("Hello Bug", 9);
  526. }
  527. else if(triggerBug && !cover.isLoading()) onAlbumArt(cover.isInvalid());
  528. }
  529. onAlbumArt(Boolean success){
  530. fallbackTempFix.stop(); //BUGFIX remove later
  531. DebugString("onAlbumArtLoaded: success="+integerToString(success), 9);
  532. DebugString("onAlbumArtLoaded: handleAACalback="+integerToString(handleAACalback), 9);
  533. DebugString("onAlbumArtLoaded: cover.isLoading="+integerToString(cover.isLoading()), 9);
  534. DebugString("onAlbumArtLoaded: cover.isInvalid="+integerToString(cover.isInvalid()), 9);
  535. if (!handleAACalback || !notifier_layout /*|| isLoading()*/)
  536. {
  537. return;
  538. }
  539. handleAACalback = cover.isLoading();
  540. cancelAnimation();
  541. showNotifier(fillNextTrackInfo(cur_status));
  542. }