Subversion Repositories Kolibri OS

Rev

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

  1. #define MEMSIZE 4096 * 50
  2.  
  3. //===================================================//
  4. //                                                   //
  5. //                       LIB                         //
  6. //                                                   //
  7. //===================================================//
  8.  
  9. #include "../lib/file_system.h"
  10. #include "../lib/list_box.h"
  11. #include "../lib/gui.h"
  12. #include "../lib/random.h"
  13. #include "../lib/kfont.h"
  14. #include "../lib/collection.h"
  15.  
  16. #include "../lib/obj/libio.h"
  17. #include "../lib/obj/libimg.h"
  18. #include "../lib/obj/libini.h"
  19. #include "../lib/obj/proc_lib.h"
  20. #include "../lib/obj/box_lib.h"
  21.  
  22. #include "../lib/patterns/libimg_load_skin.h"
  23. #include "../lib/patterns/simple_open_dialog.h"
  24.  
  25. //===================================================//
  26. //                                                   //
  27. //                       DATA                        //
  28. //                                                   //
  29. //===================================================//
  30.  
  31. //simple open dialog data
  32. char default_dir[] = "/rd/1";
  33. od_filter filter2 = { 15, "MP3\0WAV\0XM\0\0" };
  34.  
  35. #define ABOUT_MESSAGE "Pixie Player v2.62
  36.  
  37. A tiny music folder player.
  38. Supports MP3, WAV, XM audio file formats.
  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
  47.  
  48. kolibri-n.org & aspero.pro"
  49.  
  50. scroll_bar scroll1 = { 5,200,398,44,0,2,115,15,0,0xeeeeee,0xBBBbbb,0xeeeeee,
  51.         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)
  147.                                         && (list.ProcessMouse(mouse.x, mouse.y)) DrawPlayList();
  148.                                 if (mouse.down) && (mouse.key&MOUSE_RIGHT) CreateThread(#EventShowAbout,#menu_stak+4092);
  149.                         }
  150.                         if(mouse.key&MOUSE_LEFT) && (mouse.x<14)
  151.                                 && (window_mode == WINDOW_MODE_SMALL) EventDragWindow();
  152.                         break;
  153.                 case evButton:
  154.                         switch(GetButtonID()) {
  155.                                 case BUTTON_WINDOW_CLOSE: EventExitApp(); break;
  156.                                 case BUTTON_WINDOW_MINIMIZE: MinimizeWindow(); break;
  157.                                 case BUTTON_WINDOW_REDUCE: EventChangeWindowMode(); break;
  158.                                 case BUTTON_PLAYBACK_PREV: EventPlaybackPrevious();     break;
  159.                                 case BUTTON_PLAYBACK_NEXT: EventPlaybackNext(); break;
  160.                                 case BUTTON_PLAYBACK_PLAY_PAUSE: EventPlayAndPause(); break;
  161.                                 case BUTTON_REPEAT: EventRepeatClick(); break;
  162.                                 case BUTTON_SHUFFLE: EventshuffleClick(); break;
  163.                                 case BUTTON_OPEN_DIALOG: EventFileDialogOpen(); break;
  164.                                 case BUTTON_SHOW_VOLUME: RunProgram("/sys/@VOLUME", NULL); break;
  165.                         }
  166.                         break;   
  167.                 case evKey:
  168.                         GetKeys();
  169.                         if (key_modifier&KEY_LCTRL) || (key_modifier&KEY_RCTRL) {
  170.                                 if (key_scancode==SCAN_CODE_LEFT) EventPlaybackPrevious();
  171.                                 if (key_scancode==SCAN_CODE_RIGHT) EventPlaybackNext();
  172.                                 break;
  173.                         }              
  174.                         if (key_scancode==SCAN_CODE_KEY_O) EventFileDialogOpen();
  175.                         if (key_scancode==SCAN_CODE_KEY_M) RunProgram("/sys/@VOLUME", "m");
  176.                         if (key_scancode==SCAN_CODE_RIGHT) RunProgram("/sys/@VOLUME", "+");
  177.                         if (key_scancode==SCAN_CODE_LEFT)  RunProgram("/sys/@VOLUME", "-");
  178.                         if (key_scancode==SCAN_CODE_ENTER) EventStartPlayingSelectedItem();
  179.                         if (key_scancode==SCAN_CODE_KEY_P)||(key_scancode==SCAN_CODE_SPACE) EventPlayAndPause();
  180.                         if (list.ProcessKey(key_scancode)) DrawPlayList();
  181.                         break;
  182.                 case evReDraw:
  183.                         if (window_mode == WINDOW_MODE_NORMAL)
  184.                                 DefineDragableWindow(win_x_normal, win_y_normal, skin.w - 1, skin.h + list.h-1);
  185.                         if (window_mode == WINDOW_MODE_SMALL)
  186.                                 DefineDragableWindow(win_x_small, win_y_small, WIN_W_SMALL, WIN_H_SMALL);
  187.                         draw_window();
  188.                         if (param[0]) {
  189.                                 EventOpenFolder(#param);
  190.                                 param[0] = NULL;
  191.                         }
  192.                         break;
  193.                 default:
  194.                         EventCheckSongFinished();
  195.           }
  196.   }
  197. }
  198.  
  199.  
  200. void DrawPlayList()
  201. {
  202.         int i;
  203.         int yyy;
  204.         char temp_filename[4096];
  205.         dword text_color, bg_color;
  206.         for (i=0; i<list.visible; i++;)
  207.         {
  208.                 strcpy(#temp_filename, files_mas[i + list.first] * 304 + buf + 72);
  209.                 temp_filename[strrchr(#temp_filename, '.')-1] = '\0';
  210.                 //if (strlen(#temp_filename)>47) strcpy(#temp_filename+44, "...");
  211.                
  212.                 yyy = i*list.item_h+list.y;
  213.                
  214.                 //this is selected file
  215.                 if (list.cur_y - list.first == i)
  216.                 {
  217.                         if (i>=list.count) continue;
  218.                         bg_color = theme.color_list_active_bg;
  219.                         text_color = theme.color_list_text;
  220.                 }
  221.                 //this is not selected file
  222.                 else
  223.                 {
  224.                         if (i>=list.count) continue;
  225.                         bg_color = theme.color_list_bg;
  226.                         text_color = theme.color_list_text;
  227.                 }
  228.                 //this is cur_y playing file
  229.                 if (i + list.first == current_playing_file_n)
  230.                 && (playback_mode == PLAYBACK_MODE_PLAYING)
  231.                 {
  232.                         text_color = theme.color_list_active_text;
  233.                 }
  234.                 DrawBar(list.x, yyy, list.w, list.item_h, bg_color);
  235.                 kfont.WriteIntoWindow(6, yyy+list.text_y, bg_color,
  236.                         text_color, list.font_type, #temp_filename);
  237.         }
  238.         DrawBar(list.x,list.visible * list.item_h + list.y, list.w,
  239.                 -list.visible * list.item_h + list.h, theme.color_list_bg);
  240.         DrawScroller();
  241. }
  242.  
  243.  
  244. void draw_window() {
  245.         GetProcessInfo(#Form, SelfInfo);
  246.         DrawTopPanel();
  247.         IF (Form.status_window>=2) return;
  248.         if (window_mode == WINDOW_MODE_NORMAL)
  249.         {
  250.                 DrawPlayList();
  251.                 DrawRectangle(0, skin.h-1, skin.w-1, list.h+1, theme.color_list_border);
  252.         }
  253. }
  254.  
  255. dword GetSongTitle()
  256. {
  257.         char cur_y_playing_title[245];
  258.         strcpy(#cur_y_playing_title, #current_filename);
  259.         cur_y_playing_title[strrchr(#cur_y_playing_title, '.')-1] = '\0';
  260.         //if (strlen(#cur_y_playing_title) > 36) strcpy(#cur_y_playing_title + 34, "...");
  261.         return #cur_y_playing_title;
  262. }
  263.  
  264.  
  265. void DrawTopPanel()
  266. {
  267.        
  268.         int button_y;
  269.         //Mode depended
  270.         if (window_mode == WINDOW_MODE_NORMAL)
  271.         {
  272.                 button_y = 46;
  273.                 img_draw stdcall(skin.image, 0, 0, skin.w, skin.h, 0, 0);
  274.                 if (playback_mode != PLAYBACK_MODE_STOPED)
  275.                         img_draw stdcall(skin.image, 46, button_y, 41, 21, skin.w+1, WIN_H_SMALL+1);
  276.                 if (repeat)
  277.                         img_draw stdcall(skin.image, Form.width-102, button_y+2, 17,17,skin.w+43, WIN_H_SMALL+1);
  278.                 if (shuffle)
  279.                         img_draw stdcall(skin.image, Form.width-83, button_y+2, 17,17, skin.w+62, WIN_H_SMALL+1);
  280.  
  281.                 if (!work_folder) DrawPixieTitle("Pixie");
  282.                 else DrawPixieTitle(#work_folder + strrchr(#work_folder, '/'));
  283.                 kfont.WriteIntoWindow(8, 24, theme.color_top_panel_bg,
  284.                         theme.color_top_panel_song_name, list.font_type, GetSongTitle());
  285.                 //Playing control buttons
  286.                 DefineHiddenButton(7, button_y, 38, 20, BUTTON_PLAYBACK_PREV);
  287.                 DefineHiddenButton(47, button_y, 38, 20, BUTTON_PLAYBACK_PLAY_PAUSE);
  288.                 DefineHiddenButton(87, button_y, 38, 20, BUTTON_PLAYBACK_NEXT);
  289.                 //Window control buttons
  290.                 DefineHiddenButton(Form.width - 27, 1, 26, 15, BUTTON_WINDOW_CLOSE);
  291.                 DefineHiddenButton(Form.width - 55, 1, 26, 15, BUTTON_WINDOW_MINIMIZE);
  292.                 DefineHiddenButton(Form.width - 83, 1, 26, 15, BUTTON_WINDOW_REDUCE);
  293.                 //Other buttons
  294.                 button_y += 3;
  295.                 DefineHiddenButton(Form.width - 101,button_y, 17, 16, BUTTON_REPEAT);
  296.                 DefineHiddenButton(Form.width - 82, button_y, 17, 16, BUTTON_SHUFFLE);
  297.                 DefineHiddenButton(Form.width - 54, button_y, 23, 23, BUTTON_OPEN_DIALOG);
  298.                 DefineHiddenButton(Form.width - 27, button_y, 23, 23, BUTTON_SHOW_VOLUME);
  299.         }
  300.         else if (window_mode == WINDOW_MODE_SMALL)
  301.         {
  302.                 button_y = 7;
  303.                 img_draw stdcall(skin.image, 0, 0, WIN_W_SMALL, WIN_H_SMALL, skin.w-1, 0);
  304.                 DefineHiddenButton(0, 0, WIN_W_SMALL, WIN_H_SMALL, 99 + BT_NOFRAME);
  305.                 //Playing control buttons
  306.                 DefineHiddenButton(20, button_y, 24, 16, BUTTON_PLAYBACK_PREV);
  307.                 DefineHiddenButton(46, button_y, 24, 16, BUTTON_PLAYBACK_PLAY_PAUSE);
  308.                 DefineHiddenButton(72, button_y, 24, 16, BUTTON_PLAYBACK_NEXT);
  309.                 //Window control buttons
  310.                 DefineHiddenButton(Form.width - 20, 1, 19, 13, BUTTON_WINDOW_CLOSE);
  311.                 DefineHiddenButton(Form.width - 20, 16, 19, 13, BUTTON_WINDOW_REDUCE);
  312.         }
  313. }
  314.  
  315.  
  316. void DrawScroller()
  317. {
  318.         scroll1.max_area = list.count;
  319.         scroll1.cur_area = list.visible;
  320.         scroll1.position = list.first;
  321.         scroll1.all_redraw = 0;
  322.         scroll1.start_x = skin.w - scroll1.size_x-1;
  323.         scroll1.start_y = list.y-1;
  324.         scroll1.size_y = list.h+2;
  325.         if (list.count > list.visible) scrollbar_v_draw(#scroll1);
  326. }
  327.  
  328. void DrawPixieTitle(dword _title)
  329. {
  330.         kfont.WriteIntoWindow(8, 5, theme.color_top_panel_bg,
  331.                 theme.color_top_panel_folder_name, list.font_type, _title);
  332. }
  333.  
  334. //===================================================//
  335. //                                                   //
  336. //                     EVENTS                        //
  337. //                                                   //
  338. //===================================================//
  339.  
  340.  
  341. void EventOpenFolder(dword _open_path)
  342. {
  343.         if (ESBYTE[_open_path])
  344.         {
  345.                 strcpy(#work_folder, _open_path);
  346.                 work_folder[strrchr(#work_folder, '/')-1]='\0';
  347.                 OpenDirectory(#work_folder);
  348.                 SetOpenedFileFirst(_open_path);
  349.         }
  350.         list.SetSizes(1, skin.h, skin.w-1, 22*15, 22);
  351.         if (list.count <= list.visible)
  352.         {
  353.                 list.h = list.count * list.item_h;
  354.                 list.visible = list.count;
  355.                 list.w -= 1;
  356.         }
  357.         else
  358.         {
  359.                 list.w -= scroll1.size_x;
  360.         }
  361.         MoveSize(OLD, OLD, OLD, skin.h + list.h);
  362.         list.KeyHome();
  363.         current_playing_file_n=0;
  364.         EventStopPlayingMp3();
  365.         EventStartPlayingMp3();
  366. }
  367.  
  368.  
  369. void EventStopPlayingMp3()
  370. {
  371.         if (player_run_id) player_run_id = KillProcess(player_run_id);
  372.         if (notify_run_id) notify_run_id = KillProcess(notify_run_id);
  373.         playback_mode = PLAYBACK_MODE_STOPED;
  374.         DrawTopPanel();
  375.         DrawPlayList();
  376. }
  377.  
  378.  
  379. void EventStartPlayingMp3()
  380. {
  381.         word i;
  382.         char item_path[4096];
  383.         char notify_message[512];
  384.         EventStopPlayingMp3();
  385.         if (current_playing_file_n >= list.count) {
  386.                 current_playing_file_n = list.count-1;
  387.                 return;
  388.         }
  389.         if (current_playing_file_n < 0) {
  390.                 current_playing_file_n = 0;
  391.                 return;
  392.         }
  393.         playback_mode = PLAYBACK_MODE_PLAYING;
  394.         strlcpy(#current_filename, Getcur_yItemName(), sizeof(current_filename));
  395.         sprintf(#item_path,"-h %s/%s",#work_folder,#current_filename);
  396.         DrawPlayList();
  397.         DrawTopPanel();
  398.         player_run_id = RunProgram("/sys/media/ac97snd", #item_path);  
  399.         sprintf(#notify_message,"'Now playing:\n%s' -St",#current_filename);
  400.         if (!repeat)
  401.         {
  402.                 for (i=2; i<strlen(#notify_message)-6; i++)
  403.                 {
  404.                         //replace ' char to avoid @notify misunderstood
  405.                         if (notify_message[i]=='\'') notify_message[i]=96;
  406.                 }
  407.                 notify_run_id = notify(#notify_message);
  408.         }
  409. }
  410.  
  411.  
  412. void EventPlayAndPause()
  413. {
  414.         if (playback_mode == PLAYBACK_MODE_PLAYING)
  415.         {
  416.                 playback_mode = PLAYBACK_MODE_STOPED;
  417.                 EventStopPlayingMp3();
  418.         }
  419.         else
  420.         {
  421.                 playback_mode = PLAYBACK_MODE_PLAYING;
  422.                 EventStartPlayingMp3();
  423.         }
  424. }
  425.  
  426.  
  427. void EventChangeWindowMode()
  428. {
  429.         if (window_mode == WINDOW_MODE_NORMAL)
  430.         {
  431.                 window_mode = WINDOW_MODE_SMALL;
  432.                 win_x_normal = Form.left;
  433.                 win_y_normal = Form.top;
  434.                 MoveSize(OLD, OLD, WIN_W_SMALL-1, WIN_H_SMALL-1);
  435.                 MoveSize(OLD, win_y_small, OLD, OLD);
  436.                 MoveSize(win_x_small, OLD, OLD, OLD);
  437.         }
  438.         else
  439.         {
  440.                 window_mode = WINDOW_MODE_NORMAL;
  441.                 win_x_small = Form.left;
  442.                 win_y_small = Form.top;
  443.                 MoveSize(win_x_normal, win_y_normal, skin.w -1 ,skin.h + list.h);
  444.         }
  445. }
  446.  
  447. void EventExitApp()
  448. {
  449.         EventStopPlayingMp3();
  450.         SaveIniConfig();
  451.         ExitProcess();
  452. }
  453.  
  454. void EventPlaybackPrevious()
  455. {
  456.         if (shuffle) current_playing_file_n = random(list.count);
  457.         else current_playing_file_n--;
  458.         EventStartPlayingMp3();
  459. }
  460.  
  461. void EventPlaybackNext()
  462. {
  463.         if (shuffle) current_playing_file_n = random(list.count);
  464.         else current_playing_file_n++;
  465.         EventStartPlayingMp3();
  466. }
  467.  
  468. void EventStartPlayingSelectedItem()
  469. {
  470.         current_playing_file_n=list.cur_y;
  471.         EventStartPlayingMp3();
  472. }
  473.  
  474. void EventFileDialogOpen()
  475. {
  476.         OpenDialog_start stdcall (#o_dialog);
  477.         if (o_dialog.status==1) EventOpenFolder(#openfile_path);
  478. }
  479.  
  480. void EventCheckSongFinished()
  481. {
  482.         if (playback_mode == PLAYBACK_MODE_PLAYING)
  483.         && (!GetProcessSlot(player_run_id)) {
  484.                 if (repeat) EventStartPlayingMp3();
  485.                 else EventPlaybackNext();
  486.         }
  487. }
  488.  
  489. void EventRepeatClick()
  490. {
  491.         repeat ^= 1;
  492.         DrawTopPanel();
  493. }
  494.  
  495. void EventshuffleClick()
  496. {
  497.         shuffle ^= 1;
  498.         DrawTopPanel();
  499. }
  500.  
  501. void EventShowAbout()
  502. {
  503.         proc_info pop_up;
  504.         loop() switch(WaitEvent())
  505.         {
  506.                 case evButton:
  507.                         ExitProcess();
  508.                         break;
  509.                 case evKey:
  510.                         GetKeys();
  511.                         if (key_scancode == SCAN_CODE_ESC) ExitProcess();
  512.                         break;
  513.                 case evReDraw:
  514.                         DefineDragableWindow(150, 200, 363, 300);
  515.                         GetProcessInfo(#pop_up, SelfInfo);
  516.                         DrawBar(0, 0, pop_up.width, pop_up.height, theme.color_top_panel_bg);
  517.                         DrawRectangle(0, 0, pop_up.width, pop_up.height, theme.color_list_border);
  518.                         WriteTextLines(10, 10, 0x90, theme.color_top_panel_song_name, ABOUT_MESSAGE, 19);
  519.  
  520.                         DefineHiddenButton(pop_up.width - 27, 1, 26, 15, BUTTON_WINDOW_CLOSE);
  521.                         img_draw stdcall(skin.image, pop_up.width-28, 0, 28, 18, skin.w - 29, 0);
  522.                         DrawCaptButton(pop_up.width-10-80, pop_up.height - 30, 80, 20, 11,
  523.                           theme.color_list_active_bg, theme.color_top_panel_song_name, "Clear");
  524.         }
  525. }
  526.  
  527.  
  528. stop:
  529.  
  530. char menu_stak[4096];