Subversion Repositories Kolibri OS

Rev

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