Get the current active layer stack

Is there a way to query for a list of all the currently active layers?

Let’s say I am on the initial layer ‘PRIMARY’ and I shift to ‘FUNCTION’, I would like to do something like:
Layer.getAllActive() and receive some sort of list containing [‘FUNCTION’,‘PRIMARY’,‘BASE’]

I don’t know if there is a BASE layer, but I believe I read somewhere that if no layer is active there is some kind of default layer that is not defined in the .ino file KEYMAPS.

Anyway, with or without that default layer, it would be super nice to get a list with the stack of layers ordered with the most recent first.

Without changing anything, you could use Layer.forEachActiveLayer(), which takes a function pointer, and executes that function by passing it the index in the active layer stack and the layer number of each active layer. It starts from the bottom of the stack, however, and might be ill-suited to your purpose.

Returning the active layer stack itself (i.e. a pointer to the array) is problematic, because there’s no way to safeguard it. Probably the best thing to do would be to add a function that returns the layer number when given an index value in the active layer stack, and another that returns the current number of active layers (or the current index of the top layer).

Do you mind elaborating on how you would like to use this information?

I’m building a rather intricate sketch for using Blender with only the left hand. This involves a crazy amount of layers (so far 27 and counting). The layers are activated from a Blender add-on that I am also writing which uses the kaleidoscope-focus plugin. All these layers are activated in a big messy mixture of moving to, shifting to and locking. Shifting and locking are triggered by key presses. Moving are mainly done in reaction to focus events.

To complement all this I’m also making an LED plugin. Mainly to visually help me recognize which layer I’m on should I not be able to deduce it from the context in Blender but also to give me a visual clue that the add-on and keyboard are working properly. Since some layers augment others I can’t just make the LED plugin act on the active layer. I might want to keep a color on a key below a transparent key. Therefore I would need some way to traverse the layers and put the colors on from bottom to top or something like that. I could end up building that stack myself, but I am NOT finding the low level nature of Arduino and Kaleidoscope programming easy so any built-in mechanism would save me a LOT of time!

From your description, I have the impression that the existing API might be sufficient for your needs. The Layer.lookupActiveLayer(key_addr) returns the layer number of the specified KeyAddr. You could, perhaps, use a different color for each layer in your LED plugin, and visually show which layer each key is getting its current value from. This won’t show you the whole active layer stack, but it might be sufficient for your purposes.

For the moment I think you are right. I don’t think I need the whole stack. And the ability to run through each key and checking where its behavior comes from is probably enough.

Thanks.

In case it helps:

for (auto key_addr : KeyAddr::all()) {
  uint8_t layer = Layer.lookupActiveLayer(key_addr);
  doStuff(key_addr, layer);
}
1 Like