151 lines
2.8 KiB
C++
151 lines
2.8 KiB
C++
#include <stdio.h>
|
|
#include <cstdint>
|
|
#include <vector>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
|
|
// For reading FIFO
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
|
|
// For sleep
|
|
#include <thread>
|
|
|
|
// Local files
|
|
#include "ergodox.hpp"
|
|
#include "commands.h"
|
|
#include "config.h"
|
|
|
|
|
|
#include "spdlog/spdlog.h"
|
|
|
|
#ifndef DISABLE_SPECIAL_CHAR
|
|
#include "modules/special_chars.hpp"
|
|
#endif
|
|
|
|
#ifndef DISABLE_SPELL
|
|
#include "modules/spell.hpp"
|
|
#endif
|
|
|
|
#ifndef DISABLE_VISUALIZER
|
|
#include "modules/visualizer.hpp"
|
|
#endif
|
|
|
|
// TODO:
|
|
//
|
|
// MPD connection error handling
|
|
// Cleaner building
|
|
//
|
|
// FFT:
|
|
// stereo support (and maybe different bitrates?)
|
|
// Optimization: don't copy filename in buffer?
|
|
// understand consumption rate
|
|
// understand BIN2HZ
|
|
// understand values and sizes (DFT_TOTAL, DFT_NONZERO, etc)
|
|
// note that wave and spectrum have different sizes
|
|
// clear fft when not in use
|
|
//
|
|
//
|
|
// Keyboard interface:
|
|
// Fix segfault
|
|
// Clean up reconnect code
|
|
// Better log messages, compiled spdlog
|
|
//
|
|
// Get parameters from keyboard (width, height, etc)
|
|
//
|
|
// Later:
|
|
// beat detection
|
|
// waveform animation
|
|
// pcm from pulse
|
|
|
|
|
|
// HID interface wrapper
|
|
Ergodox Dox = Ergodox::init(
|
|
HID_VENDOR_ID,
|
|
HID_PRODUCT_ID,
|
|
HID_USAGE,
|
|
HID_USAGE_PAGE
|
|
);
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
spdlog::set_level(spdlog::level::info);
|
|
|
|
|
|
uint8_t last_layer_layout;
|
|
|
|
Dox.connect_loop();
|
|
|
|
#ifndef DISABLE_VISUALIZER
|
|
Visualizer::init();
|
|
#endif
|
|
|
|
while (1) {
|
|
|
|
if (Dox.is_connected()) {
|
|
#ifndef DISABLE_VISUALIZER
|
|
Visualizer::fn(Dox);
|
|
#endif
|
|
|
|
// Dox.write might detect that we've been disconnected,
|
|
// and Dox.read will fail if we are.
|
|
// This check prevents it from doing that, and instead jumps to reconnect.
|
|
if (!Dox.is_connected()) { continue; }
|
|
|
|
// Read a packet if there is a packet to read
|
|
if (Dox.read()) {
|
|
uint8_t cmd = Dox.read_buf[0];
|
|
|
|
switch (cmd) {
|
|
|
|
#ifndef DISABLE_SPELL
|
|
case CMD_SPELLCHECK_WORD: {
|
|
Spell::do_cmd(Dox);
|
|
break;
|
|
}
|
|
#endif
|
|
|
|
#ifndef DISABLE_SPECIAL_CHAR
|
|
case CMD_SPECIAL_CHAR: {
|
|
SpecialChars::do_cmd(Dox);
|
|
break;
|
|
}
|
|
#endif
|
|
}
|
|
}
|
|
|
|
|
|
// Switch layer if necessary.
|
|
if (
|
|
(last_layer_layout != Dox.get_layer_layout()) &&
|
|
(Dox.get_layer_layout() != LAYOUT_NULL)
|
|
) {
|
|
|
|
last_layer_layout = Dox.get_layer_layout();
|
|
|
|
switch(last_layer_layout) {
|
|
case (LAYOUT_EN):
|
|
std::system("awesome-client \"modules.ibus.set('en')\"");
|
|
break;
|
|
case (LAYOUT_RU):
|
|
std::system("awesome-client \"modules.ibus.set('ru')\"");
|
|
break;
|
|
}
|
|
}
|
|
|
|
} else {
|
|
Dox.connect_loop();
|
|
}
|
|
|
|
// Sleep for a bit so we don't consume
|
|
// 100% of a cpu.
|
|
std::this_thread::sleep_for(
|
|
std::chrono::milliseconds(LOOP_SLEEP_MS)
|
|
);
|
|
}
|
|
|
|
#ifndef DISABLE_VISUALIZER
|
|
Visualizer::cleanup();
|
|
#endif
|
|
return 0;
|
|
} |