qmk

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.z3bra.org/qmk.git
Log | Files | Refs | Submodules | LICENSE

sigma.c (6236B)


      1 /*
      2   Copyright 2018 Yann Hodique <yann.hodique@gmail.com> @sigma
      3 
      4   This program is free software: you can redistribute it and/or modify
      5   it under the terms of the GNU General Public License as published by
      6   the Free Software Foundation, either version 2 of the License, or
      7   (at your option) any later version.
      8 
      9   This program is distributed in the hope that it will be useful,
     10   but WITHOUT ANY WARRANTY; without even the implied warranty of
     11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12   GNU General Public License for more details.
     13 
     14   You should have received a copy of the GNU General Public License
     15   along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16 */
     17 #include "sigma.h"
     18 
     19 userspace_config_t runtime_userspace_config;
     20 userspace_config_t stored_userspace_config;
     21 
     22 __attribute__ ((weak))
     23 void matrix_init_keymap(void) {}
     24 
     25 __attribute__ ((weak))
     26 void startup_keymap(void) {}
     27 
     28 __attribute__ ((weak))
     29 void shutdown_keymap(void) {}
     30 
     31 __attribute__ ((weak))
     32 void suspend_power_down_keymap(void) {}
     33 
     34 __attribute__ ((weak))
     35 void suspend_wakeup_init_keymap(void) {}
     36 
     37 __attribute__ ((weak))
     38 void matrix_scan_keymap(void) {}
     39 
     40 __attribute__ ((weak))
     41 bool process_record_keymap(uint16_t keycode, keyrecord_t *record) {
     42   return true;
     43 }
     44 
     45 __attribute__ ((weak))
     46 bool process_record_secrets(uint16_t keycode, keyrecord_t *record) {
     47   return true;
     48 }
     49 
     50 
     51 __attribute__ ((weak))
     52 uint32_t layer_state_set_keymap (uint32_t state) {
     53   return state;
     54 }
     55 
     56 __attribute__ ((weak))
     57 uint32_t default_layer_state_set_keymap (uint32_t state) {
     58   return state;
     59 }
     60 
     61 __attribute__ ((weak))
     62 void led_set_keymap(uint8_t usb_led) {}
     63 
     64 void set_os(uint8_t os) {
     65   runtime_userspace_config.os_target = os;
     66 
     67   switch (os) {
     68   case _OS_MACOS:
     69     set_unicode_input_mode(UC_OSX);
     70     break;
     71   case _OS_LINUX:
     72     set_unicode_input_mode(UC_LNX);
     73     break;
     74   case _OS_WINDOWS:
     75     set_unicode_input_mode(UC_WIN);
     76     break;
     77   }
     78 }
     79 
     80 void matrix_init_user(void) {
     81   stored_userspace_config.raw = eeconfig_read_user();
     82   runtime_userspace_config.raw = stored_userspace_config.raw;
     83 
     84   set_os(runtime_userspace_config.os_target);
     85 }
     86 
     87 void store_userspace_config(void) {
     88   eeconfig_update_user(stored_userspace_config.raw);
     89 }
     90 
     91 LEADER_EXTERNS();
     92 
     93 void matrix_scan_user(void) {
     94   LEADER_DICTIONARY() {
     95     leading = false;
     96     leader_end();
     97 
     98     SEQ_TWO_KEYS(KC_F1, KC_L) {
     99       set_os(_OS_LINUX);
    100     }
    101     SEQ_TWO_KEYS(KC_F1, KC_M) {
    102       set_os(_OS_MACOS);
    103     }
    104     SEQ_TWO_KEYS(KC_F1, KC_W) {
    105       set_os(_OS_WINDOWS);
    106     }
    107     SEQ_TWO_KEYS(KC_F1, KC_S) {
    108       stored_userspace_config.raw = runtime_userspace_config.raw;
    109       store_userspace_config();
    110     }
    111   }
    112 }
    113 
    114 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
    115   uint8_t os_target = runtime_userspace_config.os_target;
    116   bool pressed = record->event.pressed;
    117 
    118   switch (keycode) {
    119   case KC_QWERTY:
    120     if (pressed) {
    121       set_single_persistent_default_layer(_QWERTY);
    122     }
    123     return false;
    124     break;
    125 
    126   case KC_MAKE:  // Compiles the firmware, and adds the flash command based on keyboard bootloader
    127     if (!pressed) {
    128       send_string_with_delay_P(PSTR("make " QMK_KEYBOARD ":" QMK_KEYMAP
    129 #if defined(__ARM__)
    130                    ":dfu-util"
    131 #elif defined(BOOTLOADER_DFU)
    132                    ":dfu"
    133 #elif defined(BOOTLOADER_HALFKAY)
    134                    ":teensy"
    135 #elif defined(BOOTLOADER_CATERINA)
    136                    ":avrdude"
    137 #endif // bootloader options
    138                    SS_TAP(X_ENTER)), 10);
    139     }
    140     return false;
    141     break;
    142 
    143   case EPRM: // Resets EEPROM
    144     if (pressed) {
    145       eeconfig_init();
    146       default_layer_set(1UL<<eeconfig_read_default_layer());
    147       layer_state_set(layer_state);
    148       set_os(_OS_MACOS);
    149       store_userspace_config();
    150     }
    151     return false;
    152     break;
    153   case VRSN: // Prints firmware version
    154     if (pressed) {
    155       send_string_with_delay_P(PSTR(QMK_KEYBOARD "/" QMK_KEYMAP " @ " QMK_VERSION ", Built on: " QMK_BUILDDATE), MACRO_TIMER);
    156     }
    157     return false;
    158     break;
    159 
    160   case KC_OS_CUT:
    161     switch (os_target) {
    162     case _OS_MACOS:
    163       if (pressed)
    164         SEND_STRING(SS_LGUI("x"));
    165       break;
    166     case _OS_LINUX:
    167       pressed ?
    168         register_code(KC_CUT)
    169         : unregister_code(KC_CUT);
    170       break;
    171     default:
    172       if (pressed)
    173         SEND_STRING(SS_LCTRL("x"));
    174       break;
    175     }
    176     break;
    177 
    178   case KC_OS_COPY:
    179     switch (os_target) {
    180     case _OS_MACOS:
    181       if (pressed)
    182         SEND_STRING(SS_LGUI("c"));
    183       break;
    184     case _OS_LINUX:
    185       pressed ?
    186         register_code(KC_COPY)
    187         : unregister_code(KC_COPY);
    188       break;
    189     default:
    190       if (pressed)
    191         SEND_STRING(SS_LCTRL("c"));
    192       break;
    193     }
    194     break;
    195 
    196   case KC_OS_PASTE:
    197     switch (os_target) {
    198     case _OS_MACOS:
    199       if (pressed)
    200         SEND_STRING(SS_LGUI("v"));
    201       break;
    202     case _OS_LINUX:
    203       pressed ?
    204         register_code(KC_PASTE)
    205         : unregister_code(KC_PASTE);
    206       break;
    207     default:
    208       if (pressed)
    209         SEND_STRING(SS_LCTRL("v"));
    210       break;
    211     }
    212     break;
    213 
    214   case KC_OS_UNDO:
    215     switch (os_target) {
    216     case _OS_MACOS:
    217       if (pressed)
    218         SEND_STRING(SS_LGUI("z"));
    219       break;
    220     case _OS_LINUX:
    221       pressed ?
    222         register_code(KC_UNDO)
    223         : unregister_code(KC_UNDO);
    224       break;
    225     default:
    226       if (pressed)
    227         SEND_STRING(SS_LCTRL("z"));
    228       break;
    229     }
    230     break;
    231 
    232   case KC_OS_REDO:
    233     switch (os_target) {
    234     case _OS_MACOS:
    235       if (pressed)
    236         SEND_STRING(SS_LGUI(SS_LSFT("z")));
    237       break;
    238     case _OS_LINUX:
    239       pressed ?
    240         register_code(KC_AGAIN)
    241         : unregister_code(KC_AGAIN);
    242       break;
    243     default:
    244       if (pressed)
    245         SEND_STRING(SS_LCTRL(SS_LSFT("z")));
    246       break;
    247     }
    248     break;
    249 
    250   case KC_OS_LOCK:
    251     switch (os_target) {
    252     case _OS_MACOS:
    253       if (pressed)
    254         SEND_STRING(SS_LGUI(SS_LCTRL("q")));
    255       break;
    256     case _OS_LINUX:
    257       pressed ?
    258         register_code(KC_PWR)
    259         : unregister_code(KC_PWR);
    260       break;
    261     default:
    262       if (pressed)
    263         SEND_STRING(SS_LGUI("l"));
    264       break;
    265     }
    266     break;
    267 
    268   case KC_SCRT:
    269     if (pressed) {
    270       SEND_STRING(SS_LGUI("`"));
    271     }
    272     break;
    273   }
    274 
    275   return process_record_keymap(keycode, record);
    276 }