aboutsummaryrefslogtreecommitdiffstats
path: root/quantum/process_keycode/process_combo.h
blob: e21ee19609f609efa5f3b713e5b99fd9e92f7193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* Copyright 2016 Jack Humbert
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef PROCESS_COMBO_H
#define PROCESS_COMBO_H

#include "progmem.h"
#include "quantum.h"
#include <stdint.h>

#ifdef EXTRA_EXTRA_LONG_COMBOS
#    define MAX_COMBO_LENGTH 32
#elif EXTRA_LONG_COMBOS
#    define MAX_COMBO_LENGTH 16
#else
#    define MAX_COMBO_LENGTH 8
#endif

typedef struct {
    const uint16_t *keys;
    uint16_t        keycode;
#ifdef EXTRA_EXTRA_LONG_COMBOS
    uint32_t state;
#elif EXTRA_LONG_COMBOS
    uint16_t state;
#else
    uint8_t state;
#endif
} combo_t;

#define COMBO(ck, ca) \
    { .keys = &(ck)[0], .keycode = (ca) }
#define COMBO_ACTION(ck) \
    { .keys = &(ck)[0] }

#define COMBO_END 0
#ifndef COMBO_COUNT
#    define COMBO_COUNT 0
#endif
#ifndef COMBO_TERM
#    define COMBO_TERM TAPPING_TERM
#endif

bool process_combo(uint16_t keycode, keyrecord_t *record);
void matrix_scan_combo(void);
void process_combo_event(uint8_t combo_index, bool pressed);

void combo_enable(void);
void combo_disable(void);
void combo_toggle(void);
bool is_combo_enabled(void);

#endif
/* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/* Copyright 2016 Jack Humbert
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "process_music.h"

#ifdef AUDIO_ENABLE
#    include "process_audio.h"
#endif
#if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
#    include "process_midi.h"
#endif

#if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))

bool    music_activated     = false;
bool    midi_activated      = false;
uint8_t music_starting_note = 0x0C;
int     music_offset        = 7;
uint8_t music_mode          = MUSIC_MODE_MAJOR;

// music sequencer
static bool    music_sequence_recording = false;
static bool    music_sequence_recorded  = false;
static bool    music_sequence_playing   = false;
static uint8_t music_sequence[16]       = {0};
static uint8_t music_sequence_count     = 0;
static uint8_t music_sequence_position  = 0;

static uint16_t music_sequence_timer    = 0;
static uint16_t music_sequence_interval = 100;

#    ifdef AUDIO_ENABLE
#        ifndef MUSIC_ON_SONG
#            define MUSIC_ON_SONG SONG(MUSIC_ON_SOUND)
#        endif
#        ifndef MUSIC_OFF_SONG
#            define MUSIC_OFF_SONG SONG(MUSIC_OFF_SOUND)
#        endif
#        ifndef MIDI_ON_SONG
#            define MIDI_ON_SONG SONG(MUSIC_ON_SOUND)
#        endif
#        ifndef MIDI_OFF_SONG
#            define MIDI_OFF_SONG SONG(MUSIC_OFF_SOUND)
#        endif
#        ifndef CHROMATIC_SONG
#            define CHROMATIC_SONG SONG(CHROMATIC_SOUND)
#        endif
#        ifndef GUITAR_SONG
#            define GUITAR_SONG SONG(GUITAR_SOUND)
#        endif
#        ifndef VIOLIN_SONG
#            define VIOLIN_SONG SONG(VIOLIN_SOUND)
#        endif
#        ifndef MAJOR_SONG
#            define MAJOR_SONG SONG(MAJOR_SOUND)
#        endif
float music_mode_songs[NUMBER_OF_MODES][5][2] = {CHROMATIC_SONG, GUITAR_SONG, VIOLIN_SONG, MAJOR_SONG};
float music_on_song[][2]                      = MUSIC_ON_SONG;
float music_off_song[][2]                     = MUSIC_OFF_SONG;
float midi_on_song[][2]                       = MIDI_ON_SONG;
float midi_off_song[][2]                      = MIDI_OFF_SONG;
#    endif

static void music_noteon(uint8_t note) {
#    ifdef AUDIO_ENABLE
    if (music_activated) process_audio_noteon(note);
#    endif
#    if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
    if (midi_activated) process_midi_basic_noteon(note);
#    endif
}

static void music_noteoff(uint8_t note) {
#    ifdef AUDIO_ENABLE
    if (music_activated) process_audio_noteoff(note);
#    endif
#    if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
    if (midi_activated) process_midi_basic_noteoff(note);
#    endif
}

void music_all_notes_off(void) {
#    ifdef AUDIO_ENABLE
    if (music_activated) process_audio_all_notes_off();
#    endif
#    if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
    if (midi_activated) process_midi_all_notes_off();
#    endif
}

bool process_music(uint16_t keycode, keyrecord_t *record) {
    if (keycode == MU_ON && record->event.pressed) {
        music_on();
        return false;
    }

    if (keycode == MU_OFF && record->event.pressed) {
        music_off();
        return false;
    }

    if (keycode == MU_TOG && record->event.pressed) {
        if (music_activated) {
            music_off();
        } else {
            music_on();
        }
        return false;
    }

    if (keycode == MI_ON && record->event.pressed) {
        midi_on();
        return false;
    }

    if (keycode == MI_OFF && record->event.pressed) {
        midi_off();
        return false;
    }

    if (keycode == MI_TOG && record->event.pressed) {
        if (midi_activated) {
            midi_off();
        } else {
            midi_on();
        }
        return false;
    }

    if (keycode == MU_MOD && record->event.pressed) {
        music_mode_cycle();
        return false;
    }

    if (music_activated || midi_activated) {
        if (record->event.pressed) {
            if (keycode == KC_LCTL) {  // Start recording
                music_all_notes_off();
                music_sequence_recording = true;
                music_sequence_recorded  = false;
                music_sequence_playing   = false;
                music_sequence_count     = 0;
                return false;
            }

            if (keycode == KC_LALT) {  // Stop recording/playing
                music_all_notes_off();
                if (music_sequence_recording) {  // was recording
                    music_sequence_recorded = true;
                }
                music_sequence_recording = false;
                music_sequence_playing   = false;
                return false;
            }

            if (keycode == KC_LGUI && music_sequence_recorded) {  // Start playing
                music_all_notes_off();
                music_sequence_recording = false;
                music_sequence_playing   = true;
                music_sequence_position  = 0;
                music_sequence_timer     = 0;
                return false;
            }

            if (keycode == KC_UP) {
                music_sequence_interval -= 10;
                return false;
            }

            if (keycode == KC_DOWN) {
                music_sequence_interval += 10;
                return false;
            }
        }

        uint8_t note = 36;
#    ifdef MUSIC_MAP
        if (music_mode == MUSIC_MODE_CHROMATIC) {
            note = music_starting_note + music_offset + 36 + music_map[record->event.key.row][record->event.key.col];
        } else {
            uint8_t position = music_map[record->event.key.row][record->event.key.col];
            note             = music_starting_note + music_offset + 36 + SCALE[position % 12] + (position / 12) * 12;
        }
#    else
        if (music_mode == MUSIC_MODE_CHROMATIC)
            note = (music_starting_note + record->event.key.col + music_offset - 3) + 12 * (MATRIX_ROWS - record->event.key.row);
        else if (music_mode == MUSIC_MODE_GUITAR)
            note = (music_starting_note + record->event.key.col + music_offset + 32) + 5 * (MATRIX_ROWS - record->event.key.row);
        else if (music_mode == MUSIC_MODE_VIOLIN)
            note = (music_starting_note + record->event.key.col + music_offset + 32) + 7 * (MATRIX_ROWS - record->event.key.row);
        else if (music_mode == MUSIC_MODE_MAJOR)
            note = (music_starting_note + SCALE[record->event.key.col + music_offset] - 3) + 12 * (MATRIX_ROWS - record->event.key.row);
        else
            note = music_starting_note;
#    endif

        if (record->event.pressed) {
            music_noteon(note);
            if (music_sequence_recording) {
                music_sequence[music_sequence_count] = note;
                music_sequence_count++;
            }
        } else {
            music_noteoff(note);
        }

        if (music_mask(keycode)) return false;
    }

    return true;
}

bool music_mask(uint16_t keycode) {
#    ifdef MUSIC_MASK
    return MUSIC_MASK;
#    else
    return music_mask_kb(keycode);
#    endif
}

__attribute__((weak)) bool music_mask_kb(uint16_t keycode) { return music_mask_user(keycode); }

__attribute__((weak)) bool music_mask_user(uint16_t keycode) { return keycode < 0xFF; }

bool is_music_on(void) { return (music_activated != 0); }

void music_toggle(void) {
    if (!music_activated) {
        music_on();
    } else {
        music_off();
    }
}

void music_on(void) {
    music_activated = 1;
#    ifdef AUDIO_ENABLE
    PLAY_SONG(music_on_song);
#    endif
    music_on_user();
}

void music_off(void) {
    music_all_notes_off();
    music_activated = 0;
#    ifdef AUDIO_ENABLE
    PLAY_SONG(music_off_song);
#    endif
}

bool is_midi_on(void) { return (midi_activated != 0); }

void midi_toggle(void) {
    if (!midi_activated) {
        midi_on();
    } else {
        midi_off();
    }
}

void midi_on(void) {
    midi_activated = 1;
#    ifdef AUDIO_ENABLE
    PLAY_SONG(midi_on_song);
#    endif
    midi_on_user();
}

void midi_off(void) {
#    if defined(MIDI_ENABLE) && defined(MIDI_BASIC)
    process_midi_all_notes_off();
#    endif
    midi_activated = 0;
#    ifdef AUDIO_ENABLE
    PLAY_SONG(midi_off_song);
#    endif
}

void music_mode_cycle(void) {
    music_all_notes_off();
    music_mode = (music_mode + 1) % NUMBER_OF_MODES;
#    ifdef AUDIO_ENABLE
    PLAY_SONG(music_mode_songs[music_mode]);
#    endif
}

void matrix_scan_music(void) {
    if (music_sequence_playing) {
        if ((music_sequence_timer == 0) || (timer_elapsed(music_sequence_timer) > music_sequence_interval)) {
            music_sequence_timer = timer_read();
            uint8_t prev_note    = music_sequence[(music_sequence_position - 1 < 0) ? (music_sequence_position - 1 + music_sequence_count) : (music_sequence_position - 1)];
            uint8_t next_note    = music_sequence[music_sequence_position];
            music_noteoff(prev_note);
            music_noteon(next_note);
            music_sequence_position = (music_sequence_position + 1) % music_sequence_count;
        }
    }
}

__attribute__((weak)) void music_on_user() {}

__attribute__((weak)) void midi_on_user() {}

__attribute__((weak)) void music_scale_user() {}

#endif  // defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))