Merge pull request #364 from keyboardio/f/namespacing
Move most sources to a kaleidoscope/ subdir and namespacepull/367/head
commit
b40b2ea4a6
@ -0,0 +1,153 @@
|
|||||||
|
/* Kaleidoscope - Firmware for computer input devices
|
||||||
|
* Copyright (C) 2013-2018 Keyboard.io, Inc.
|
||||||
|
*
|
||||||
|
* 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, version 3.
|
||||||
|
*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
//end of add your includes here
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
void loop();
|
||||||
|
void setup();
|
||||||
|
#ifdef __cplusplus
|
||||||
|
} // extern "C"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//add your function definitions for the project KeyboardIO here
|
||||||
|
|
||||||
|
#define TS(X) //Serial.print(micros() );Serial.print("\t");Serial.println(X);
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#include KALEIDOSCOPE_HARDWARE_H
|
||||||
|
#include "kaleidoscope/key_events.h"
|
||||||
|
#include "kaleidoscope/hid.h"
|
||||||
|
#include "kaleidoscope/layers.h"
|
||||||
|
#include "kaleidoscope/macro_map.h"
|
||||||
|
#include "kaleidoscope_internal/event_dispatch.h"
|
||||||
|
#include "kaleidoscope/macro_helpers.h"
|
||||||
|
#include "kaleidoscope/plugin.h"
|
||||||
|
|
||||||
|
#define HOOK_MAX 64
|
||||||
|
|
||||||
|
extern HARDWARE_IMPLEMENTATION KeyboardHardware;
|
||||||
|
|
||||||
|
#ifndef VERSION
|
||||||
|
#define VERSION "locally-built"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/** Kaleidoscope API (major) version.
|
||||||
|
*
|
||||||
|
* The API is guaranteed to be backwards compatible for the entire duration of a
|
||||||
|
* major version. However, breaking changes may come, and result in a major
|
||||||
|
* version bump. To help migration, the `KALEIDOSCOPE_API_VERSION` macro can be
|
||||||
|
* used to check the major version provided by the Kaleidoscope we are compiling
|
||||||
|
* against. This can be used to error out with a helpful message, or change how
|
||||||
|
* the API is used - it is entirely up to the plugin or sketch author. The point
|
||||||
|
* of this macro is to let them easily check the version.
|
||||||
|
*/
|
||||||
|
#define KALEIDOSCOPE_API_VERSION 2
|
||||||
|
|
||||||
|
/** Required Kaleidoscope major version.
|
||||||
|
*
|
||||||
|
* For the sake of convenience, defining `KALEIDOSCOPE_REQUIRED_API_VERSION`
|
||||||
|
* before including `Kaleidoscope.h` itself will result in comparing its value
|
||||||
|
* to `KALEIDOSCOPE_API_VERSION`. If they differ, a helpful error message is
|
||||||
|
* printed.
|
||||||
|
*
|
||||||
|
* Done so that a new API version would result in a helpful error message,
|
||||||
|
* instead of cryptic compile errors.
|
||||||
|
*/
|
||||||
|
#if defined(KALEIDOSCOPE_REQUIRED_API_VERSION) && (KALEIDOSCOPE_REQUIRED_API_VERSION != KALEIDOSCOPE_API_VERSION)
|
||||||
|
#define xstr(a) str(a)
|
||||||
|
#define str(a) #a
|
||||||
|
static_assert(KALEIDOSCOPE_REQUIRED_API_VERSION == KALEIDOSCOPE_API_VERSION,
|
||||||
|
"Kaleidoscope API version mismatch! We have version " xstr(KALEIDOSCOPE_API_VERSION)
|
||||||
|
" available, but version " xstr(KALEIDOSCOPE_REQUIRED_API_VERSION) " is required.");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace kaleidoscope {
|
||||||
|
|
||||||
|
class Kaleidoscope_ {
|
||||||
|
public:
|
||||||
|
Kaleidoscope_(void);
|
||||||
|
|
||||||
|
void setup(void);
|
||||||
|
void loop(void);
|
||||||
|
|
||||||
|
/** Detaching from / attaching to the host.
|
||||||
|
*
|
||||||
|
* These two functions wrap the hardware plugin's similarly named functions.
|
||||||
|
* We wrap them, because we'd like plugins and user-code not having to use
|
||||||
|
* `KeyboardHardware` directly.
|
||||||
|
*
|
||||||
|
* The methods themselves implement detaching from / attaching to the host,
|
||||||
|
* without rebooting the device, and remaining powered in between.
|
||||||
|
*
|
||||||
|
* Intended to be used in cases where we want to change some settings between
|
||||||
|
* detach and attach.
|
||||||
|
*/
|
||||||
|
void detachFromHost() {
|
||||||
|
KeyboardHardware.detachFromHost();
|
||||||
|
}
|
||||||
|
void attachToHost() {
|
||||||
|
KeyboardHardware.attachToHost();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns the timer as it was at the start of the cycle.
|
||||||
|
* The goal of this method is two-fold:
|
||||||
|
* - To reduce the amount of calls to millis(), providing something cheaper.
|
||||||
|
* - To have a consistent timer value for the whole duration of a cycle.
|
||||||
|
*
|
||||||
|
* This cached value is updated at the start of each cycle as the name
|
||||||
|
* implies. It is recommended to use this in plugins over millis() unless
|
||||||
|
* there is good reason not to.
|
||||||
|
*/
|
||||||
|
static uint32_t millisAtCycleStart() {
|
||||||
|
return millis_at_cycle_start_;
|
||||||
|
}
|
||||||
|
|
||||||
|
EventHandlerResult onFocusEvent(const char *command) {
|
||||||
|
return kaleidoscope::Hooks::onFocusEvent(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static uint32_t millis_at_cycle_start_;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern kaleidoscope::Kaleidoscope_ Kaleidoscope;
|
||||||
|
|
||||||
|
} // namespace kaleidoscope
|
||||||
|
|
||||||
|
// For compatibility reasons we enable class Kaleidoscope_ also to be available
|
||||||
|
// in global namespace.
|
||||||
|
//
|
||||||
|
typedef kaleidoscope::Kaleidoscope_ Kaleidoscope_;
|
||||||
|
|
||||||
|
// For compatibility reasons we enable the global variable Kaleidoscope
|
||||||
|
// in global namespace.
|
||||||
|
//
|
||||||
|
using kaleidoscope::Kaleidoscope;
|
||||||
|
|
||||||
|
// Use this function macro to register plugins with Kaleidoscope's
|
||||||
|
// hooking system. The macro accepts a list of plugin instances that
|
||||||
|
// must have been instantiated at global scope.
|
||||||
|
//
|
||||||
|
#define KALEIDOSCOPE_INIT_PLUGINS(...) _KALEIDOSCOPE_INIT_PLUGINS(__VA_ARGS__)
|
@ -0,0 +1,68 @@
|
|||||||
|
/* Kaleidoscope - Firmware for computer input devices
|
||||||
|
* Copyright (C) 2013-2018 Keyboard.io, Inc.
|
||||||
|
*
|
||||||
|
* 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, version 3.
|
||||||
|
*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "kaleidoscope_internal/deprecations.h"
|
||||||
|
|
||||||
|
static const uint8_t LAYER_SHIFT_OFFSET = 42;
|
||||||
|
|
||||||
|
#define KEYMAP_0 0
|
||||||
|
#define KEYMAP_1 1
|
||||||
|
#define KEYMAP_2 2
|
||||||
|
#define KEYMAP_3 3
|
||||||
|
#define KEYMAP_4 4
|
||||||
|
#define KEYMAP_5 5
|
||||||
|
#define KEYMAP_6 6
|
||||||
|
#define KEYMAP_7 7
|
||||||
|
|
||||||
|
|
||||||
|
#define KEYMAP_PREVIOUS 33
|
||||||
|
#define KEYMAP_NEXT 34
|
||||||
|
|
||||||
|
|
||||||
|
#define Key_Keymap0 (Key) { KEYMAP_0, KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP }
|
||||||
|
#define Key_Keymap1 (Key) { KEYMAP_1, KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP }
|
||||||
|
#define Key_Keymap2 (Key) { KEYMAP_2, KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP }
|
||||||
|
#define Key_Keymap3 (Key) { KEYMAP_3, KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP }
|
||||||
|
#define Key_Keymap4 (Key) { KEYMAP_4, KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP }
|
||||||
|
#define Key_Keymap5 (Key) { KEYMAP_5, KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP }
|
||||||
|
#define Key_Keymap0_Momentary (Key){ KEYMAP_0 + LAYER_SHIFT_OFFSET, KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP }
|
||||||
|
#define Key_Keymap1_Momentary (Key){ KEYMAP_1 + LAYER_SHIFT_OFFSET, KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP }
|
||||||
|
#define Key_Keymap2_Momentary (Key){ KEYMAP_2 + LAYER_SHIFT_OFFSET, KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP }
|
||||||
|
#define Key_Keymap3_Momentary (Key){ KEYMAP_3 + LAYER_SHIFT_OFFSET, KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP }
|
||||||
|
#define Key_Keymap4_Momentary (Key){ KEYMAP_4 + LAYER_SHIFT_OFFSET, KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP }
|
||||||
|
#define Key_Keymap5_Momentary (Key){ KEYMAP_5 + LAYER_SHIFT_OFFSET, KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP }
|
||||||
|
|
||||||
|
#define Key_KeymapNext_Momentary (Key) { KEYMAP_NEXT + LAYER_SHIFT_OFFSET, KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP }
|
||||||
|
#define Key_KeymapPrevious_Momentary (Key) { KEYMAP_PREVIOUS + LAYER_SHIFT_OFFSET, KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP }
|
||||||
|
|
||||||
|
|
||||||
|
/** Lock/Unlock layer `n`.
|
||||||
|
*
|
||||||
|
* When locking a layer, it will remain active until unlocked explicitly. `n`
|
||||||
|
* can be a number, or an enum value declared previously.
|
||||||
|
*/
|
||||||
|
#define LockLayer(n) (Key){ n, KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP }
|
||||||
|
#define UnlockLayer(n) LockLayer(n)
|
||||||
|
|
||||||
|
/** Temporarily shift to layer `n`.
|
||||||
|
*
|
||||||
|
* Shifts to layer `n` for as long as the key is held. When the key is
|
||||||
|
* released, the layer shifts back too. `n` can be a number, or an enum
|
||||||
|
* value declared previously.
|
||||||
|
*/
|
||||||
|
#define ShiftToLayer(n) (Key){ n + LAYER_SHIFT_OFFSET, KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP }
|
@ -0,0 +1,115 @@
|
|||||||
|
/* Kaleidoscope - Firmware for computer input devices
|
||||||
|
* Copyright (C) 2013-2018 Keyboard.io, Inc.
|
||||||
|
*
|
||||||
|
* 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, version 3.
|
||||||
|
*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include "kaleidoscope/key_defs.h"
|
||||||
|
#include KALEIDOSCOPE_HARDWARE_H
|
||||||
|
|
||||||
|
// Macro for defining the keymap. This should be used in the sketch
|
||||||
|
// file (*.ino) to define the keymap[] array that holds the user's
|
||||||
|
// layers. It also computes the number of layers in that keymap.
|
||||||
|
#define KEYMAPS(layers...) \
|
||||||
|
const Key keymaps[][ROWS][COLS] PROGMEM = { layers }; \
|
||||||
|
uint8_t layer_count = sizeof(keymaps) / sizeof(*keymaps);
|
||||||
|
|
||||||
|
extern uint8_t layer_count;
|
||||||
|
|
||||||
|
namespace kaleidoscope {
|
||||||
|
class Layer_ {
|
||||||
|
public:
|
||||||
|
Layer_() {}
|
||||||
|
|
||||||
|
/* There are two lookup functions, because we have two caches, and different
|
||||||
|
* parts of the firmware will want to use either this or that (or perhaps
|
||||||
|
* both, in rare cases).
|
||||||
|
*
|
||||||
|
* First of all, we use caches because looking up a key through all the layers
|
||||||
|
* is costy, and the cost increases dramatically the more layers we have.
|
||||||
|
*
|
||||||
|
* Then, we have the `liveCompositeKeymap`, because to have layer behaviours
|
||||||
|
* we want, that is, if you hold a key on a layer, release the layer key but
|
||||||
|
* continue holding the other, we want for the layered keycode to continue
|
||||||
|
* repeating.
|
||||||
|
*
|
||||||
|
* At the same time, we want other keys to not be affected by the
|
||||||
|
* now-turned-off layer. So we update the keycode in the cache on-demand, when
|
||||||
|
* the key is pressed. (see the top of `handleKeyswitchEvent`).
|
||||||
|
*
|
||||||
|
* On the other hand, we also have plugins that scan the whole keymap, and do
|
||||||
|
* things based on that information, such as highlighting keys that changed
|
||||||
|
* between layers. These need to be able to look at a state of where the
|
||||||
|
* keymap *should* be, not necessarily where it is. The `liveCompositeKeymap`
|
||||||
|
* is not useful here. So we use `activeLayers` which we update whenever
|
||||||
|
* layers change (see `Layer.on` and `Layer.off`), and it updates the cache to
|
||||||
|
* show how the keymap should look, without the `liveCompositeKeymap`-induced
|
||||||
|
* behaviour.
|
||||||
|
*
|
||||||
|
* Thus, if we are curious about what a given key will do, use `lookup`. If we
|
||||||
|
* are curious what the active layer state describes the key as, use
|
||||||
|
* `lookupOnActiveLayer`.
|
||||||
|
*/
|
||||||
|
static Key lookup(byte row, byte col) {
|
||||||
|
return liveCompositeKeymap[row][col];
|
||||||
|
}
|
||||||
|
static Key lookupOnActiveLayer(byte row, byte col) {
|
||||||
|
uint8_t layer = activeLayers[row][col];
|
||||||
|
return (*getKey)(layer, row, col);
|
||||||
|
}
|
||||||
|
static uint8_t lookupActiveLayer(byte row, byte col) {
|
||||||
|
return activeLayers[row][col];
|
||||||
|
}
|
||||||
|
static void on(uint8_t layer);
|
||||||
|
static void off(uint8_t layer);
|
||||||
|
static void move(uint8_t layer);
|
||||||
|
|
||||||
|
static uint8_t top(void) {
|
||||||
|
return highestLayer;
|
||||||
|
}
|
||||||
|
static void next(void);
|
||||||
|
static void previous(void);
|
||||||
|
|
||||||
|
static boolean isOn(uint8_t layer);
|
||||||
|
|
||||||
|
static void defaultLayer(uint8_t layer);
|
||||||
|
static uint8_t defaultLayer(void);
|
||||||
|
|
||||||
|
static uint32_t getLayerState(void);
|
||||||
|
|
||||||
|
static Key eventHandler(Key mappedKey, byte row, byte col, uint8_t keyState);
|
||||||
|
|
||||||
|
static Key(*getKey)(uint8_t layer, byte row, byte col);
|
||||||
|
|
||||||
|
static Key getKeyFromPROGMEM(uint8_t layer, byte row, byte col);
|
||||||
|
|
||||||
|
static void updateLiveCompositeKeymap(byte row, byte col);
|
||||||
|
static void updateActiveLayers(void);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static void updateHighestLayer(void);
|
||||||
|
|
||||||
|
static uint8_t DefaultLayer;
|
||||||
|
static uint32_t LayerState;
|
||||||
|
static uint8_t highestLayer;
|
||||||
|
static Key liveCompositeKeymap[ROWS][COLS];
|
||||||
|
static uint8_t activeLayers[ROWS][COLS];
|
||||||
|
|
||||||
|
static void handleKeymapKeyswitchEvent(Key keymapEntry, uint8_t keyState);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
extern kaleidoscope::Layer_ Layer;
|
@ -0,0 +1,137 @@
|
|||||||
|
/* Kaleidoscope - Firmware for computer input devices
|
||||||
|
* Copyright (C) 2013-2018 Keyboard.io, Inc.
|
||||||
|
*
|
||||||
|
* 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, version 3.
|
||||||
|
*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
// Code generated by muli-line pre-processor macros is hard to read after
|
||||||
|
// pre-processing.
|
||||||
|
//
|
||||||
|
// When you want to check pre-processed code, e.g. for debugging or
|
||||||
|
// to understand what's going on, do the following:
|
||||||
|
//
|
||||||
|
// 1) Add the compiler command line definition
|
||||||
|
// -DKALEIDOSCOPE_ENABLE_MACRO_NEWLINE_SUBSTITUTION
|
||||||
|
// This prevents the __NL__ macro being defined below.
|
||||||
|
// 2) Generate the preprocessed code (it will contain a lot of __NL__ definitions).
|
||||||
|
// 3) Open the pre-processed code in your favorite editor.
|
||||||
|
// 3.1) Replace all __NL__ with newline characters.
|
||||||
|
// In vim the command would be ':%s/__NL__/\r/g'.
|
||||||
|
// 3.2) Autocorrect code-indenting to improve readability. This is necessary
|
||||||
|
// as pre-processor macros remove all whitespaces at the beginning of lines.
|
||||||
|
// With vim, the command gg=G helps (just type the characters one after
|
||||||
|
// the other).
|
||||||
|
// 4) Don't forget to remove the
|
||||||
|
// -DKALEIDOSCOPE_ENABLE_MACRO_NEWLINE_SUBSTITUTION
|
||||||
|
// from your compiler command line. Else the code won't compile.
|
||||||
|
|
||||||
|
#ifndef KALEIDOSCOPE_ENABLE_MACRO_NEWLINE_SUBSTITUTION
|
||||||
|
#define __NL__
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define __NN__
|
||||||
|
|
||||||
|
// Some auxiliary macros
|
||||||
|
//
|
||||||
|
#define __STRINGIZE(S) #S
|
||||||
|
#define STRINGIZE(S) __STRINGIZE(S)
|
||||||
|
|
||||||
|
// Allow for the creation of verbose messages in static_asserts
|
||||||
|
//
|
||||||
|
#define VERBOSE_STATIC_ASSERT_HEADER \
|
||||||
|
"\n" __NL__ \
|
||||||
|
"\n***************************************************************" __NL__ \
|
||||||
|
"\n******************** READ THIS CAREFULLY! *********************" __NL__ \
|
||||||
|
"\n***************************************************************" __NL__ \
|
||||||
|
"\n"
|
||||||
|
|
||||||
|
#define VERBOSE_STATIC_ASSERT_FOOTER \
|
||||||
|
"\n" __NL__ \
|
||||||
|
"\n***************************************************************" __NL__ \
|
||||||
|
"\n***************************************************************" __NL__ \
|
||||||
|
"\n***************************************************************" __NL__ \
|
||||||
|
"\n"
|
||||||
|
|
||||||
|
#define VERBOSE_FILE_INFO \
|
||||||
|
"\nFile: " __FILE__ __NL__
|
||||||
|
|
||||||
|
#define VERBOSE_LINE_INFO \
|
||||||
|
"\nLine: " STRINGIZE(__LINE__) __NL__
|
||||||
|
|
||||||
|
// The macro function RESTRICT_ARGS_COUNT can be used to generate more
|
||||||
|
// verbose error messages when users supply an insuitable number of arguments
|
||||||
|
// to a macro.
|
||||||
|
//
|
||||||
|
// For a macro it is used wherever one of the arguments A, B, C might
|
||||||
|
// be used, e.g.
|
||||||
|
//
|
||||||
|
#if 0 // This is just so that A_MACRO is not actually defined
|
||||||
|
#define A_MACRO(A, B, C, ...) \
|
||||||
|
(void)RESTRICT_ARGS_COUNT(0, 3, A_MACRO, ##__VA_ARGS__); \
|
||||||
|
int a = A; \
|
||||||
|
int b = B; \
|
||||||
|
int c = C;
|
||||||
|
#endif
|
||||||
|
//
|
||||||
|
// Note that RESTRICT_ARGS_COUNT can also be invoked wherever one of the macro
|
||||||
|
// arguments is used, e.g.
|
||||||
|
//
|
||||||
|
#if 0 // This is just so that B_MACRO is not actually defined
|
||||||
|
#define B_MACRO(A, B, C, ...)
|
||||||
|
int array[] = { A, B, RESTRICT_ARGS_COUNT(C, 3, B_MACRO, ##__VA_ARGS__) };
|
||||||
|
#endif
|
||||||
|
//
|
||||||
|
#define RESTRICT_ARGS_COUNT(B, \
|
||||||
|
NUM_EXPECTED_ARGS, \
|
||||||
|
ORIGINAL_MACRO, \
|
||||||
|
...) \
|
||||||
|
((struct { __NL__ \
|
||||||
|
/* Here we are in the body of a dummy lambda function. */ __NL__ \
|
||||||
|
/* []{} is, BTW, the shortest way to write a lambda. */ __NL__ \
|
||||||
|
/* It is only used to hold the static_assert that cannot be */ __NL__ \
|
||||||
|
/* defined directly in the keymap initializer list. By using the */ __NL__ \
|
||||||
|
/* comma operator ((A, B) always evaluates to b), we ensure */ __NL__ \
|
||||||
|
/* that not the lambda but B is what ASSERT_ARGS_COUNT */ __NL__ \
|
||||||
|
/* finally evaluates to. */ __NL__ \
|
||||||
|
/* Please not that passing B through this macro is a must */ __NL__ \
|
||||||
|
/* as we need it for the comma operator to work. */ __NL__ \
|
||||||
|
static_assert(sizeof(const char) == sizeof(#__VA_ARGS__ ), __NL__ \
|
||||||
|
/* sizeof(const char) == sizeof(#__VA_ARGS__ ) checks the quoted */ __NL__ \
|
||||||
|
/* list of additional arguments. If there are none, then the */ __NL__ \
|
||||||
|
/* length of #__VA_ARGS__ is a single char as it contains '\0'. */ __NL__ \
|
||||||
|
/* This check is not able to find the corner case of a single */ __NL__ \
|
||||||
|
/* superfluous comma at the end of the macro arguments as this */ __NL__ \
|
||||||
|
/* causes #__VA_ARGS__ being empty (only '\0'). */ __NL__ \
|
||||||
|
VERBOSE_STATIC_ASSERT_HEADER __NL__ \
|
||||||
|
__NL__ \
|
||||||
|
VERBOSE_FILE_INFO __NL__ \
|
||||||
|
VERBOSE_LINE_INFO __NL__ \
|
||||||
|
"\n" __NL__ \
|
||||||
|
"\nStrange arguments found in invocation of " #ORIGINAL_MACRO "." __NL__ \
|
||||||
|
"\n" __NL__ \
|
||||||
|
"\nPlease make sure to pass exactly " #NUM_EXPECTED_ARGS __NL__ \
|
||||||
|
" macro arguments to" __NL__ \
|
||||||
|
"\n" #ORIGINAL_MACRO ". Also make sure that there are no dangling" __NL__ \
|
||||||
|
"\ncommas at the end of the argument list." __NL__ \
|
||||||
|
"\n" __NL__ \
|
||||||
|
"\nThis is the superfluous part at the end of the macro" __NL__ \
|
||||||
|
"\narguments list: \'" #__VA_ARGS__ "\'" __NL__ \
|
||||||
|
__NL__ \
|
||||||
|
VERBOSE_STATIC_ASSERT_FOOTER __NL__ \
|
||||||
|
); __NL__ \
|
||||||
|
__NL__ \
|
||||||
|
}){}, /* End of dummy lambda, the comma operator's A operand. */ __NL__ \
|
||||||
|
B /* The overall ASSERT_ARGS_COUNT evaluates to B. */ __NL__ \
|
||||||
|
)
|
Loading…
Reference in new issue