Subversion Repositories Kolibri OS

Rev

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