Subversion Repositories Kolibri OS

Rev

Rev 7183 | Rev 7189 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. //TODO
  2. //repeat track
  3. //edit list manually
  4.  
  5. #define MEMSIZE 4096 * 50
  6.  
  7. //===================================================//
  8. //                                                   //
  9. //                       LIB                         //
  10. //                                                   //
  11. //===================================================//
  12.  
  13. #include "../lib/file_system.h"
  14. #include "../lib/list_box.h"
  15. #include "../lib/gui.h"
  16. #include "../lib/random.h"
  17. #include "../lib/kfont.h"
  18. #include "../lib/collection.h"
  19.  
  20. #include "../lib/obj/libio.h"
  21. #include "../lib/obj/libimg.h"
  22. #include "../lib/obj/libini.h"
  23. #include "../lib/obj/proc_lib.h"
  24. #include "../lib/obj/box_lib.h"
  25.  
  26. #include "../lib/patterns/libimg_load_skin.h"
  27. #include "../lib/patterns/simple_open_dialog.h"
  28.  
  29. //===================================================//
  30. //                                                   //
  31. //                       DATA                        //
  32. //                                                   //
  33. //===================================================//
  34.  
  35. //simple open dialog data
  36. char default_dir[] = "/rd/1";
  37. od_filter filter2 = { 8, "MP3\0\0" };
  38.  
  39. #define ABOUT_MESSAGE "'Pixies Player v2.61
  40. A tiny music folder player.
  41. Supports MP3, WAV, XM audio file formats.
  42.  
  43. Controls:
  44. Open file: O key
  45. Play/Stop: Space or P key
  46. Start playing selected file: Enter
  47. Goto next/previous track: Ctrl + Left/Right
  48. Change sound volume: Left/Right key
  49. Mute: M key' -td"
  50.  
  51. scroll_bar scroll1 = { 5,200,398,44,0,2,115,15,0,0xeeeeee,0xBBBbbb,0xeeeeee,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1};
  52.  
  53. proc_info Form;
  54. llist list;
  55.  
  56. char pixie_ini_path[4096];
  57. char work_folder[4096];
  58. char current_filename[256];
  59.  
  60. enum {
  61.         BUTTON_WINDOW_CLOSE = 1,
  62.         BUTTON_WINDOW_MINIMIZE,
  63.         BUTTON_WINDOW_REDUCE,
  64.         BUTTON_PLAYBACK_PLAY_PAUSE = 10,
  65.         BUTTON_PLAYBACK_PREV,
  66.         BUTTON_PLAYBACK_NEXT,
  67.         BUTTON_REPEAT,
  68.         BUTTON_SHUFFLE,
  69.         BUTTON_OPEN_DIALOG,
  70.         BUTTON_SHOW_VOLUME
  71. };
  72.  
  73. int player_run_id;
  74. int notify_run_id;
  75.  
  76. bool repeat;
  77. bool shuffle;
  78.  
  79. int current_playing_file_n=0;
  80.  
  81. word win_x_normal, win_y_normal;
  82. word win_x_small, win_y_small;
  83.  
  84. byte window_mode;
  85. enum {
  86.         WINDOW_MODE_NORMAL,
  87.         WINDOW_MODE_SMALL
  88. };
  89.  
  90. byte playback_mode;
  91. enum {
  92.         PLAYBACK_MODE_STOPED,
  93.         PLAYBACK_MODE_PLAYING
  94. };
  95.  
  96. collection music_col;
  97.  
  98. //===================================================//
  99. //                                                   //
  100. //                       CODE                        //
  101. //                                                   //
  102. //===================================================//
  103.  
  104. #include "get_files_list.h"
  105. #include "settings.h"
  106.  
  107. void LoadLibraries()
  108. {
  109.         load_dll(boxlib, #box_lib_init,0);
  110.         load_dll(libio, #libio_init,1);
  111.         load_dll(libimg, #libimg_init,1);
  112.         load_dll(libini, #lib_init,1);
  113.         load_dll(Proc_lib, #OpenDialog_init,0);
  114.         OpenDialog_init stdcall (#o_dialog);
  115. }
  116.  
  117. void main()
  118. {
  119.         int tempstr;
  120.         tempstr = abspath("pixie.ini");
  121.         strcpy(#pixie_ini_path, tempstr);
  122.         list.SetFont(8, 16, 13);
  123.         if (!param) notify("'Pixie Player\nPress O key to open MP3 file' -St");
  124.         LoadLibraries();
  125.         LoadIniConfig();
  126.         kfont.init(DEFAULT_FONT);      
  127.         SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER);
  128.         loop()
  129.         {
  130.           WaitEventTimeout(10);
  131.           switch(EAX & 0xFF) {
  132.                 case evMouse:
  133.                         if (!CheckActiveProcess(Form.ID)) break;
  134.                         mouse.get();
  135.                         scrollbar_v_mouse (#scroll1);
  136.                         if (list.first != scroll1.position)
  137.                         {
  138.                                 list.first = scroll1.position;
  139.                                 DrawPlayList();
  140.                                 break;
  141.                         }
  142.                         if (list.MouseOver(mouse.x, mouse.y))
  143.                         {
  144.                                 if (mouse.vert) && (list.MouseScroll(mouse.vert)) DrawPlayList();
  145.                                 if (mouse.dblclick) EventStartPlayingSelectedItem();
  146.                                 if (mouse.down) && (mouse.key&MOUSE_LEFT) && (list.ProcessMouse(mouse.x, mouse.y)) DrawPlayList();
  147.                                 if (mouse.down) && (mouse.key&MOUSE_RIGHT) notify(ABOUT_MESSAGE);
  148.                         }
  149.                         if(mouse.key&MOUSE_LEFT) && (mouse.y<skin.h) && (window_mode == WINDOW_MODE_SMALL) EventDragWindow();
  150.                         break;
  151.                 case evButton:
  152.                         switch(GetButtonID()) {
  153.                                 case BUTTON_WINDOW_CLOSE: EventExitApp(); break;
  154.                                 case BUTTON_WINDOW_MINIMIZE: MinimizeWindow(); break;
  155.                                 case BUTTON_WINDOW_REDUCE: EventChangeWindowMode(); break;
  156.                                 case BUTTON_PLAYBACK_PREV: EventPlaybackPrevious();     break;
  157.                                 case BUTTON_PLAYBACK_NEXT: EventPlaybackNext(); break;
  158.                                 case BUTTON_PLAYBACK_PLAY_PAUSE: EventPlayAndPause(); break;
  159.                                 case BUTTON_REPEAT: EventRepeatClick(); break;
  160.                                 case BUTTON_SHUFFLE: EventshuffleClick(); break;
  161.                                 case BUTTON_OPEN_DIALOG: EventFileDialogOpen(); break;
  162.                                 case BUTTON_SHOW_VOLUME: RunProgram("/sys/@VOLUME", NULL); break;
  163.                         }
  164.                         break;   
  165.                 case evKey:
  166.                         GetKeys();
  167.                         if (key_modifier&KEY_LCTRL) || (key_modifier&KEY_RCTRL) {
  168.                                 if (key_scancode==SCAN_CODE_LEFT) EventPlaybackPrevious();
  169.                                 if (key_scancode==SCAN_CODE_RIGHT) EventPlaybackNext();
  170.                                 break;
  171.                         }              
  172.                         if (key_scancode==SCAN_CODE_KEY_O) EventFileDialogOpen();
  173.                         if (key_scancode==SCAN_CODE_KEY_M) RunProgram("/sys/@VOLUME", "m");
  174.                         if (key_scancode==SCAN_CODE_RIGHT) RunProgram("/sys/@VOLUME", "+");
  175.                         if (key_scancode==SCAN_CODE_LEFT)  RunProgram("/sys/@VOLUME", "-");
  176.                         if (key_scancode==SCAN_CODE_ENTER) EventStartPlayingSelectedItem();
  177.                         if (key_scancode==SCAN_CODE_KEY_P) || (key_scancode==SCAN_CODE_SPACE) EventPlayAndPause();
  178.                         if (list.ProcessKey(key_scancode)) DrawPlayList();
  179.                         break;
  180.                 case evReDraw:
  181.                         if (window_mode == WINDOW_MODE_NORMAL) DefineDragableWindow(win_x_normal, win_y_normal, skin.w - 1, skin.h + list.h-1);
  182.                         if (window_mode == WINDOW_MODE_SMALL) DefineDragableWindow(win_x_small, win_y_small, WIN_W_SMALL, WIN_H_SMALL);
  183.                         draw_window();
  184.                         if (param[0]) {
  185.                                 EventOpenFolder(#param);
  186.                                 param[0] = NULL;
  187.                         }
  188.                         break;
  189.                 default:
  190.                         EventCheckSongFinished();
  191.           }
  192.   }
  193. }
  194.  
  195.  
  196. void DrawPlayList()
  197. {
  198.         int i;
  199.         int yyy;
  200.         char temp_filename[4096];
  201.         dword text_color, bg_color;
  202.         for (i=0; i<list.visible; i++;)
  203.         {
  204.                 strcpy(#temp_filename, files_mas[i + list.first] * 304 + buf + 72);
  205.                 temp_filename[strrchr(#temp_filename, '.')-1] = '\0';
  206.                 //if (strlen(#temp_filename)>47) strcpy(#temp_filename+44, "...");
  207.                
  208.                 yyy = i*list.item_h+list.y;
  209.                
  210.                 //this is selected file
  211.                 if (list.cur_y - list.first == i)
  212.                 {
  213.                         if (i>=list.count) continue;
  214.                         bg_color = theme.color_list_active_bg;
  215.                         text_color = theme.color_list_text;
  216.                 }
  217.                 //this is not selected file
  218.                 else
  219.                 {
  220.                         if (i>=list.count) continue;
  221.                         bg_color = theme.color_list_bg;
  222.                         text_color = theme.color_list_text;
  223.                 }
  224.                 //this is cur_y playing file
  225.                 if (i + list.first == current_playing_file_n) && (playback_mode == PLAYBACK_MODE_PLAYING)
  226.                 {
  227.                         text_color = theme.color_list_active_text;
  228.                 }
  229.                 DrawBar(list.x, yyy, list.w, list.item_h, bg_color);
  230.                 kfont.WriteIntoWindow(6, yyy+list.text_y, bg_color, text_color, list.font_type, #temp_filename);
  231.         }
  232.         DrawBar(list.x,list.visible * list.item_h + list.y, list.w, -list.visible * list.item_h + list.h, theme.color_list_bg);
  233.         DrawScroller();
  234. }
  235.  
  236.  
  237. void draw_window() {
  238.         GetProcessInfo(#Form, SelfInfo);
  239.         DrawTopPanel();
  240.         IF (Form.status_window>=2) return;
  241.         if (window_mode == WINDOW_MODE_NORMAL)
  242.         {
  243.                 DrawPlayList();
  244.                 DrawRectangle(0, skin.h-1, skin.w-1, list.h+1, theme.color_list_border);
  245.         }
  246. }
  247.  
  248. dword GetSongTitle()
  249. {
  250.         char cur_y_playing_title[245];
  251.         strcpy(#cur_y_playing_title, #current_filename);
  252.         //cur_y_playing_title[strlen(#cur_y_playing_title)-4] = '\0';
  253.         //if (strlen(#cur_y_playing_title) > 36) strcpy(#cur_y_playing_title + 34, "...");
  254.         return #cur_y_playing_title;
  255. }
  256.  
  257.  
  258. void DrawTopPanel()
  259. {
  260.        
  261.         int button_y;
  262.         //Mode depended
  263.         if (window_mode == WINDOW_MODE_NORMAL)
  264.         {
  265.                 button_y = 46;
  266.                 img_draw stdcall(skin.image, 0, 0, skin.w, skin.h, 0, 0);
  267.                 if (playback_mode != PLAYBACK_MODE_STOPED) img_draw stdcall(skin.image, 46, button_y, 41, 21, skin.w+1, WIN_H_SMALL+1);
  268.                 if (repeat) img_draw stdcall(skin.image, Form.width - 101-1, button_y+2, 17, 17, skin.w+43, WIN_H_SMALL+1);
  269.                 if (shuffle) img_draw stdcall(skin.image, Form.width - 82-1, button_y+2, 17, 17, skin.w+62, WIN_H_SMALL+1);
  270.  
  271.                 if /*(!list.count) && */ (!work_folder) DrawPixieTitle("Pixie");
  272.                 else DrawPixieTitle(#work_folder + strrchr(#work_folder, '/'));
  273.                 kfont.WriteIntoWindow(8, 24, theme.color_top_panel_bg, theme.color_top_panel_song_name, list.font_type, GetSongTitle());
  274.                 //Playing control buttons
  275.                 DefineHiddenButton(7, button_y, 38, 20, BUTTON_PLAYBACK_PREV);
  276.                 DefineHiddenButton(47, button_y, 38, 20, BUTTON_PLAYBACK_PLAY_PAUSE);
  277.                 DefineHiddenButton(87, button_y, 38, 20, BUTTON_PLAYBACK_NEXT);
  278.                 //Window control buttons
  279.                 DefineHiddenButton(Form.width - 27, 1, 26, 15, BUTTON_WINDOW_CLOSE);
  280.                 DefineHiddenButton(Form.width - 55, 1, 26, 15, BUTTON_WINDOW_MINIMIZE);
  281.                 DefineHiddenButton(Form.width - 83, 1, 26, 15, BUTTON_WINDOW_REDUCE);
  282.                 //Other buttons
  283.                 button_y += 3;
  284.                 DefineHiddenButton(Form.width - 101,button_y, 17, 16, BUTTON_REPEAT);
  285.                 DefineHiddenButton(Form.width - 82, button_y, 17, 16, BUTTON_SHUFFLE);
  286.                 DefineHiddenButton(Form.width - 54, button_y, 23, 23, BUTTON_OPEN_DIALOG);
  287.                 DefineHiddenButton(Form.width - 27, button_y, 23, 23, BUTTON_SHOW_VOLUME);
  288.         }
  289.         else if (window_mode == WINDOW_MODE_SMALL)
  290.         {
  291.                 button_y = 7;
  292.                 img_draw stdcall(skin.image, 0, 0, WIN_W_SMALL, WIN_H_SMALL, skin.w-1, 0);
  293.                 DefineHiddenButton(0, 0, WIN_W_SMALL, WIN_H_SMALL, 99 + BT_NOFRAME);
  294.                 //Playing control buttons
  295.                 DefineHiddenButton(8, button_y, 24, 16, BUTTON_PLAYBACK_PREV);
  296.                 DefineHiddenButton(34, button_y, 24, 16, BUTTON_PLAYBACK_PLAY_PAUSE);
  297.                 DefineHiddenButton(60, button_y, 24, 16, BUTTON_PLAYBACK_NEXT);
  298.                 //Window control buttons
  299.                 DefineHiddenButton(Form.width - 20, 1, 19, 13, BUTTON_WINDOW_CLOSE);
  300.                 DefineHiddenButton(Form.width - 20, 16, 19, 13, BUTTON_WINDOW_REDUCE);
  301.         }
  302. }
  303.  
  304.  
  305. void DrawScroller()
  306. {
  307.         scroll1.max_area = list.count;
  308.         scroll1.cur_area = list.visible;
  309.         scroll1.position = list.first;
  310.         scroll1.all_redraw = 0;
  311.         scroll1.start_x = skin.w - scroll1.size_x-1;
  312.         scroll1.start_y = list.y-1;
  313.         scroll1.size_y = list.h+2;
  314.         if (list.count > list.visible) scrollbar_v_draw(#scroll1);
  315. }
  316.  
  317. void DrawPixieTitle(dword _title)
  318. {
  319.         kfont.WriteIntoWindow(8, 5, theme.color_top_panel_bg, theme.color_top_panel_folder_name, list.font_type, _title);
  320. }
  321.  
  322. //===================================================//
  323. //                                                   //
  324. //                     EVENTS                        //
  325. //                                                   //
  326. //===================================================//
  327.  
  328.  
  329. void EventOpenFolder(dword _open_path)
  330. {
  331.         if (ESBYTE[_open_path])
  332.         {
  333.                 strcpy(#work_folder, _open_path);
  334.                 work_folder[strrchr(#work_folder, '/')-1]='\0';
  335.                 OpenDirectory(#work_folder);
  336.                 SetOpenedFileFirst(_open_path);
  337.         }
  338.         list.SetSizes(1, skin.h, skin.w-1, 22*15, 22);
  339.         if (list.count <= list.visible)
  340.         {
  341.                 list.h = list.count * list.item_h;
  342.                 list.visible = list.count;
  343.                 list.w -= 1;
  344.         }
  345.         else
  346.         {
  347.                 list.w -= scroll1.size_x;
  348.         }
  349.         MoveSize(OLD, OLD, OLD, skin.h + list.h);
  350.         list.KeyHome();
  351.         current_playing_file_n=0;
  352.         EventStopPlayingMp3();
  353.         EventStartPlayingMp3();
  354. }
  355.  
  356.  
  357. void EventStopPlayingMp3()
  358. {
  359.         if (player_run_id) player_run_id = KillProcess(player_run_id);
  360.         if (notify_run_id) notify_run_id = KillProcess(notify_run_id);
  361.         playback_mode = PLAYBACK_MODE_STOPED;
  362.         DrawTopPanel();
  363.         DrawPlayList();
  364. }
  365.  
  366.  
  367. void EventStartPlayingMp3()
  368. {
  369.         word i;
  370.         char item_path[4096];
  371.         char notify_message[512];
  372.         EventStopPlayingMp3();
  373.         if (current_playing_file_n >= list.count) {
  374.                 current_playing_file_n = list.count-1;
  375.                 return;
  376.         }
  377.         if (current_playing_file_n < 0) {
  378.                 current_playing_file_n = 0;
  379.                 return;
  380.         }
  381.         playback_mode = PLAYBACK_MODE_PLAYING;
  382.         strlcpy(#current_filename, Getcur_yItemName(), sizeof(current_filename));
  383.         sprintf(#item_path,"-h %s/%s",#work_folder,#current_filename);
  384.         DrawPlayList();
  385.         DrawTopPanel();
  386.         player_run_id = RunProgram("/sys/media/ac97snd", #item_path);  
  387.         sprintf(#notify_message,"'Now playing:\n%s' -St",#current_filename);
  388.         if (!repeat)
  389.         {
  390.                 for (i=2; i<strlen(#notify_message)-6; i++)
  391.                         if (notify_message[i]=='\'') notify_message[i]=96; //replace ' char to avoid @notify misunderstood
  392.                 notify_run_id = notify(#notify_message);
  393.         }
  394. }
  395.  
  396.  
  397. void EventPlayAndPause()
  398. {
  399.         if (playback_mode == PLAYBACK_MODE_PLAYING)
  400.         {
  401.                 playback_mode = PLAYBACK_MODE_STOPED;
  402.                 EventStopPlayingMp3();
  403.         }
  404.         else
  405.         {
  406.                 playback_mode = PLAYBACK_MODE_PLAYING;
  407.                 EventStartPlayingMp3();
  408.         }
  409. }
  410.  
  411.  
  412. void EventChangeWindowMode()
  413. {
  414.         if (window_mode == WINDOW_MODE_NORMAL)
  415.         {
  416.                 window_mode = WINDOW_MODE_SMALL;
  417.                 win_x_normal = Form.left;
  418.                 win_y_normal = Form.top;
  419.                 MoveSize(OLD, OLD, WIN_W_SMALL-1, WIN_H_SMALL-1);
  420.                 MoveSize(OLD, win_y_small, OLD, OLD);
  421.                 MoveSize(win_x_small, OLD, OLD, OLD);
  422.         }
  423.         else
  424.         {
  425.                 window_mode = WINDOW_MODE_NORMAL;
  426.                 win_x_small = Form.left;
  427.                 win_y_small = Form.top;
  428.                 MoveSize(win_x_normal, win_y_normal, skin.w -1 ,skin.h + list.h);
  429.         }
  430. }
  431.  
  432. void EventExitApp()
  433. {
  434.         EventStopPlayingMp3();
  435.         SaveIniConfig();
  436.         ExitProcess();
  437. }
  438.  
  439. void EventPlaybackPrevious()
  440. {
  441.         if (shuffle) current_playing_file_n = random(list.count);
  442.         else current_playing_file_n--;
  443.         EventStartPlayingMp3();
  444. }
  445.  
  446. void EventPlaybackNext()
  447. {
  448.         if (shuffle) current_playing_file_n = random(list.count);
  449.         else current_playing_file_n++;
  450.         EventStartPlayingMp3();
  451. }
  452.  
  453. void EventStartPlayingSelectedItem()
  454. {
  455.         current_playing_file_n=list.cur_y;
  456.         EventStartPlayingMp3();
  457. }
  458.  
  459. void EventFileDialogOpen()
  460. {
  461.         OpenDialog_start stdcall (#o_dialog);
  462.         if (o_dialog.status==1) EventOpenFolder(#openfile_path);
  463. }
  464.  
  465. void EventCheckSongFinished()
  466. {
  467.         if (playback_mode == PLAYBACK_MODE_PLAYING) && (!GetProcessSlot(player_run_id)) {
  468.                 if (repeat) EventStartPlayingMp3();
  469.                 else EventPlaybackNext();
  470.         }
  471. }
  472.  
  473. void EventRepeatClick()
  474. {
  475.         repeat ^= 1;
  476.         DrawTopPanel();
  477. }
  478.  
  479. void EventshuffleClick()
  480. {
  481.         shuffle ^= 1;
  482.         DrawTopPanel();
  483. }
  484.  
  485. stop:
  486.  
  487. char menu_stak[4096];