Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. /*
  3.  * Author: JohnXenox aka Aleksandr Igorevich.
  4.  *
  5.  * Programme name: Beat
  6.  * Description: A simple metronome.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <kolibrisys.h>
  12.  
  13. char header[] = "Beat 2020.05.17";
  14.  
  15. unsigned int skin_height = 0;
  16.  
  17. unsigned int key = 0;
  18. unsigned int btn = 0;
  19.  
  20. short ctrl_keys_state = 0;
  21.  
  22. int thread_stack = 0x1000;
  23. int stack_size = 0x100;
  24.  
  25.  
  26. char startButtonBit = 0;
  27.  
  28. char tempoSelector = 0;
  29.  
  30. short tempo = 100; // Beats Per Minute.
  31.  
  32. char meter = 4;  // 4/4
  33.  
  34. char accentBeatFlags[12] = {0};
  35.  
  36.  
  37. char counter = 1;
  38.  
  39. counterIndicatorFlag = 0;
  40.  
  41.  
  42. //Event mask bits for function 40.
  43. enum EVENT_MASKS
  44. {
  45.      EVM_REDRAW =        0x00000001,
  46.      EVM_KEY =           0x00000002,
  47.      EVM_BUTTON =        0x00000004,
  48.      EVM_EXIT =          0x00000008,
  49.      EVM_BACKGROUND =    0x00000016,
  50.      EVM_MOUSE =         0x00000032,
  51.      EVM_IPC =           0x00000064,
  52.      EVM_STACK =         0x00000128,
  53.      EVM_DEBUG =         0x00000256,
  54.      EVM_STACK2 =        0x00000512,
  55.      EVM_MOUSE_FILTER =  0x80000000,
  56.      EVM_CURSOR_FILTER = 0x40000000
  57. };
  58.  
  59.  
  60.  
  61. enum EVENTS
  62. {
  63.      EVENT_REDRAW = 1,   /* Window and window elements should be redrawn */
  64.      EVENT_KEY = 2,      /* A key on the keyboard was pressed */
  65.      EVENT_BUTTON = 3,   /* A button was clicked with the mouse */
  66.      EVENT_MOUSE = 6     /* Mouse activity (movement, button press) was detected */
  67. };
  68.  
  69.  
  70. struct system_colors
  71. {
  72.     int frame;             // color of frame.
  73.     int grab;              // color of header.
  74.     int grab_button;       // color of button on header bar.
  75.     int grab_button_text;  // color of text on button on header bar.
  76.     int grab_text;         // color of text on header.
  77.     int work;              // color of working area.
  78.     int work_button;       // color of button in working area.
  79.     int work_button_text;  // color of text on button in working area.
  80.     int work_text;         // color of text in working area.
  81.     int work_graph;        // color of graphics in working area.
  82. };
  83.  
  84.  
  85. struct system_colors sc;
  86.  
  87. #include "Beat_lang.h"
  88. #include "Beat_lib.h"
  89.  
  90.  
  91. #define  KEY_ARROW_UP     0xB2
  92. #define  KEY_ARROW_DOWN   0xB1
  93. #define  KEY_ARROW_LEFT   0xB0
  94. #define  KEY_ARROW_RIGHT  0xB3
  95.  
  96. #define  KEYS_CTRL_ARROW_UP     0x52
  97. #define  KEYS_CTRL_ARROW_DOWN   0x51
  98. #define  KEYS_CTRL_ARROW_LEFT   0x50
  99. #define  KEYS_CTRL_ARROW_RIGHT  0x53
  100.  
  101. #define  KEY_SPACE  0x20
  102.  
  103. #define  KEY_ESCAPE  0x1B
  104.  
  105. #define  KEY_SLASH  0x2F
  106.  
  107.  
  108.  
  109.  
  110. char redraw_flag = 0;
  111.  
  112.  
  113. #define SMPL_NAME1 "Beep1.raw"
  114. #define SMPL_NAME2 "Beep2.raw"
  115. #define PRG_NAME "PlayNote"
  116.  
  117. char _path_to_a_sample1[4096] = {0};
  118. char _path_to_a_sample2[4096] = {0};
  119. char _path_to_a_playnote[4096] = {0};
  120.  
  121. #define SEARCH_PATHES_NUMBER 3
  122.  
  123. unsigned char* search_pathes_to_a_playnote[] = {
  124. _path_to_a_playnote,
  125. "/sys/"PRG_NAME,
  126. "/sys/Media/"PRG_NAME,
  127. };
  128.  
  129. char *path_to_a_playnote = 0;
  130.  
  131.  
  132.  
  133. int main(int argc, char** argv)
  134. {
  135.     setCurrentPathToARawFile(_path_to_a_sample1, argv[0], SMPL_NAME1);
  136.     setCurrentPathToARawFile(_path_to_a_sample2, argv[0], SMPL_NAME2);
  137.     setCurrentPathToARawFile(_path_to_a_playnote, argv[0], PRG_NAME);
  138.  
  139.     // searches for a PlayNote programme.
  140.     for(char i = 0; (i < SEARCH_PATHES_NUMBER); i++)
  141.     {
  142.         if(startApp("/sys/loool.raw", 0, search_pathes_to_a_playnote[i]) > 0)
  143.         {
  144.             path_to_a_playnote = search_pathes_to_a_playnote[i];
  145.         }
  146.     }
  147.  
  148.     if(path_to_a_playnote == 0)
  149.     {
  150.         #if defined (lang_en)
  151.             startApp("\"Can't find a PlayNote programme!\" -W", 0, "/sys/@notify");
  152.         #elif defined (lang_ru)
  153.             startApp("\"¥ ¬®£ã ­ ©â¨ ¯à®£à ¬¬ã PlayNote!\" -W", 0, "/sys/@notify");
  154.         #endif
  155.  
  156.         return 1;
  157.     }
  158.  
  159.     _ksys_set_wanted_events(EVM_REDRAW | EVM_KEY | EVM_BUTTON | EVM_MOUSE_FILTER);
  160.  
  161.  
  162.  
  163.  
  164.  
  165.     drawWindow();
  166.  
  167.     for (;;)
  168.     {
  169.  
  170.         if(startButtonBit == 1)
  171.         {
  172.  
  173.             if(redraw_flag == 0)
  174.             {
  175.  
  176.                 //if(counter == 0) counter++;
  177.  
  178.                 if(counter < meter)
  179.                     counter++;
  180.                 else if(counter == meter)
  181.                     counter = 1;
  182.  
  183.  
  184.  
  185.                 if(accentBeatFlags[counter - 1] == 0)
  186.                 {
  187.                     // play a beep sound.
  188.                     startApp(_path_to_a_sample2, 0, path_to_a_playnote);
  189.                 }
  190.                 else
  191.                 {
  192.                     // play a beep sound.
  193.                     startApp(_path_to_a_sample1, 0, path_to_a_playnote);
  194.                 }
  195.  
  196.  
  197.  
  198.  
  199.                 showCounterIndicator();
  200.  
  201.  
  202. //              makeDelay(7);
  203.  
  204.             }
  205.             else
  206.             {
  207.                 redraw_flag = 0;
  208.             }
  209.  
  210.         }
  211.  
  212.         switch(_ksys_wait_for_event(6000 / (tempo)))
  213.         {
  214.             case EVENT_REDRAW:
  215.  
  216.                    redraw_flag = 1;
  217.  
  218.                    drawWindow();
  219.                    break;
  220.  
  221.             case EVENT_KEY:
  222.                    ctrl_keys_state = getControlKeysOnAKeyboard();
  223.  
  224.                    // key pressed, read it and ignore
  225.                    key = _ksys_get_key();
  226.  
  227.                    key = ((key >> 8) & 0x000000FF);
  228.  
  229.  
  230.                    //printfOnADebugBoard("ctrl_keys_state: %d\n", ctrl_keys_state);
  231.  
  232.                    // makes exit.
  233.                    if(key == KEY_ESCAPE) return 0;
  234.  
  235.                    // starts beats.
  236.                    if(key == KEY_SPACE)
  237.                    {
  238.                        if(startButtonBit == 0)
  239.                            startButtonBit = 1;
  240.                        else
  241.                            startButtonBit = 0;
  242.  
  243.                        showStartButton();
  244.                    }
  245.  
  246.  
  247.                    // decreases tempo.
  248.                    if(key == KEY_ARROW_LEFT)
  249.                    {
  250.                        if(tempo != 1)
  251.                        {
  252.                            showTempoBar2(--tempo);
  253.                            setTempoSelectorByTempo(&tempo, &tempoSelector);
  254.  
  255.                        }
  256.                    }
  257.  
  258.                    // increases tempo.
  259.                    if(key == KEY_ARROW_RIGHT)
  260.                    {
  261.                        if(tempo != 320)
  262.                        {
  263.                            showTempoBar2(++tempo);
  264.                            setTempoSelectorByTempo(&tempo, &tempoSelector);
  265.                        }
  266.                    }
  267.  
  268.  
  269.                    // decreases a tempo selector.
  270.                    if(key == KEYS_CTRL_ARROW_LEFT)
  271.                    {
  272.                        if ((ctrl_keys_state == 4) || (ctrl_keys_state == 8))
  273.                        {
  274.                            if(tempoSelector != 0)
  275.                            {
  276.                                showTempoBar1(--tempoSelector);
  277.                                setTempoByTempoSelector(&tempo, tempoSelector);
  278.                                showTempoBar2(tempo);
  279.                            }
  280.                        }
  281.                    }
  282.  
  283.  
  284.                    // increases a tempo selector.
  285.                    if(key == KEYS_CTRL_ARROW_RIGHT)
  286.                    {
  287.                        if ((ctrl_keys_state == 4) || (ctrl_keys_state == 8))
  288.                        {
  289.                            if(tempoSelector != 9)
  290.                            {
  291.                                showTempoBar1(++tempoSelector);
  292.                                setTempoByTempoSelector(&tempo, tempoSelector);
  293.                                showTempoBar2(tempo);
  294.                            }
  295.                        }
  296.                    }
  297.  
  298.  
  299.                    if(key == 0x2C)
  300.                    {
  301.                        if(meter > 1)
  302.                        {
  303.                             showMeterBar(--meter);
  304.                             showMeterIndicator();
  305.                        }
  306.  
  307.                    }
  308.  
  309.                    if(key == 0x2E)
  310.                    {
  311.                        if(meter < 12)
  312.                        {
  313.                             showMeterBar(++meter);
  314.                             showMeterIndicator();
  315.                        }
  316.                    }
  317.  
  318.  
  319.                    for(unsigned char i = 0; i < 9; i++)
  320.                    {
  321.                        if(key == (0x31 + i))
  322.                        {
  323.                                if(accentBeatFlags[i] == 0)
  324.                                {
  325.                                    accentBeatFlags[i] = 1;
  326.                                }
  327.                                else if (accentBeatFlags[i] != 0)
  328.                                {
  329.                                    accentBeatFlags[i] = 0;
  330.                                }
  331.  
  332.  
  333.                            showMeterIndicator();
  334.                        }
  335.                    }
  336.  
  337.                    if(key == 0x30)
  338.                    {
  339.                        if(accentBeatFlags[9] == 0)
  340.                            accentBeatFlags[9] = 1;
  341.                        else if (accentBeatFlags[9] != 0)
  342.                            accentBeatFlags[9] = 0;
  343.  
  344.                        showMeterIndicator();
  345.                    }
  346.  
  347.                    if(key == 0x2D)
  348.                    {
  349.                        if(accentBeatFlags[10] == 0)
  350.                            accentBeatFlags[10] = 1;
  351.                        else if (accentBeatFlags[10] != 0)
  352.                            accentBeatFlags[10] = 0;
  353.  
  354.                        showMeterIndicator();
  355.                    }
  356.  
  357.                    if(key == 0x3D)
  358.                    {
  359.                        if(accentBeatFlags[11] == 0)
  360.                            accentBeatFlags[11] = 1;
  361.                        else if (accentBeatFlags[11] != 0)
  362.                            accentBeatFlags[11] = 0;
  363.  
  364.                        showMeterIndicator();
  365.                    }
  366.  
  367.  
  368.  
  369.  
  370.                    // invertation of colours.
  371.                    if(key == KEY_SLASH)
  372.                    {
  373.                        if(counterIndicatorFlag != 0)
  374.                            counterIndicatorFlag = 0;
  375.                        else if (counterIndicatorFlag == 0)
  376.                            counterIndicatorFlag = 1;
  377.  
  378.                        showCounterIndicator();
  379.                    }
  380.  
  381.  
  382.                    break;
  383.  
  384.             case EVENT_BUTTON:
  385.                    // button pressed; we have only one button, close
  386.                    btn = _ksys_get_button_id();
  387.  
  388.                    if(btn == 1) return 0;
  389.  
  390.                    if(btn == 7)
  391.                    {
  392.  
  393.                        if(startButtonBit == 0)
  394.                            startButtonBit = 1;
  395.                        else
  396.                            startButtonBit = 0;
  397.  
  398.                        showStartButton();
  399.                   }
  400.  
  401.  
  402.                    // decreases a tempo selector.
  403.                    if(btn == 10)
  404.                    {
  405.                        if(tempoSelector != 0)
  406.                        {
  407.                            showTempoBar1(--tempoSelector);
  408.                            setTempoByTempoSelector(&tempo, tempoSelector);
  409.                            showTempoBar2(tempo);
  410.                        }
  411.                    }
  412.  
  413.  
  414.                    // increases a tempo selector.
  415.                    if(btn == 11)
  416.                    {
  417.                        if(tempoSelector != 9)
  418.                        {
  419.                            showTempoBar1(++tempoSelector);
  420.                            setTempoByTempoSelector(&tempo, tempoSelector);
  421.                            showTempoBar2(tempo);
  422.                        }
  423.                    }
  424.  
  425.  
  426.                    // decreases tempo.
  427.                    if(btn == 12)
  428.                    {
  429.                        if(tempo != 1)
  430.                        {
  431.                            showTempoBar2(--tempo);
  432.                            setTempoSelectorByTempo(&tempo, &tempoSelector);
  433.  
  434.                        }
  435.                    }
  436.  
  437.  
  438.                    // increases tempo.
  439.                    if(btn == 13)
  440.                    {
  441.                        if(tempo < 320)
  442.                        {
  443.                            showTempoBar2(++tempo);
  444.                            setTempoSelectorByTempo(&tempo, &tempoSelector);
  445.                        }
  446.                    }
  447.  
  448.  
  449.                    if(btn == 14)
  450.                    {
  451.                        if(meter > 1)
  452.                        {
  453.                             showMeterBar(--meter);
  454.  
  455.                             // clreans unused flags.
  456.                             for(unsigned char i = meter; i < 11; i++)
  457.                             {
  458.                                 accentBeatFlags[i] = 0;
  459.                             }
  460.  
  461.                             //if(meter == 1) accentBeatFlags[0] = 0;
  462.  
  463.  
  464.                             showMeterIndicator();
  465.                        }
  466.  
  467.                    }
  468.  
  469.                    if(btn == 15)
  470.                    {
  471.                        if(meter < 12)
  472.                        {
  473.                             showMeterBar(++meter);
  474.  
  475.                             // clreans unused flags.
  476.                             for(unsigned char i = meter; i < 11; i++)
  477.                             {
  478.                                 accentBeatFlags[i] = 0;
  479.                             }
  480.  
  481.                             showMeterIndicator();
  482.                        }
  483.  
  484.                    }
  485.  
  486.  
  487.                    for(unsigned char i = 0; i < 12; i++)
  488.                    {
  489.                        if(btn == (100 + i))
  490.                        {
  491.                            //if(meter > 1)
  492.                            //{
  493.                                if(accentBeatFlags[i] == 0)
  494.                                {
  495.                                    accentBeatFlags[i] = 1;
  496.                                }
  497.                                else if (accentBeatFlags[i] != 0)
  498.                                {
  499.                                    accentBeatFlags[i] = 0;
  500.                                }
  501.                            //}
  502.                            //else if(meter == 1) accentBeatFlags[i] = 0;
  503.  
  504.                            showMeterIndicator();
  505.                        }
  506.                    }
  507.  
  508.  
  509.                    if(btn == 200)
  510.                    {
  511.                        if(counterIndicatorFlag != 0)
  512.                            counterIndicatorFlag = 0;
  513.                        else if (counterIndicatorFlag == 0)
  514.                            counterIndicatorFlag = 1;
  515.  
  516.                        showCounterIndicator();
  517.                    }
  518.  
  519.  
  520.  
  521.                    break;
  522.  
  523.         }
  524.     }
  525. }
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545.