QMKhost/src/main.cpp

151 lines
2.8 KiB
C++
Raw Normal View History

2022-06-24 21:33:12 -07:00
#include <stdio.h>
#include <cstdint>
#include <vector>
2022-06-26 12:39:02 -07:00
#include <stdexcept>
#include <string>
2022-11-20 12:05:44 -08:00
2022-06-24 21:33:12 -07:00
// For reading FIFO
#include <fcntl.h>
#include <unistd.h>
2022-11-20 12:05:44 -08:00
2022-07-08 19:07:46 -07:00
// For sleep
#include <thread>
2022-11-20 12:05:44 -08:00
2022-06-24 21:33:12 -07:00
// Local files
2022-06-26 12:39:02 -07:00
#include "ergodox.hpp"
2022-07-08 10:41:19 -07:00
#include "commands.h"
2022-07-08 16:56:32 -07:00
#include "config.h"
2022-06-25 19:12:01 -07:00
2022-11-20 12:05:44 -08:00
2022-07-08 19:36:05 -07:00
#include "spdlog/spdlog.h"
2022-11-20 12:05:44 -08:00
#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
2022-06-24 21:33:12 -07:00
// TODO:
//
2022-07-11 09:40:48 -07:00
// MPD connection error handling
// Cleaner building
//
2022-07-08 16:56:32 -07:00
// 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:
2022-07-09 20:36:45 -07:00
// Fix segfault
2022-07-08 16:56:32 -07:00
// Clean up reconnect code
2022-07-08 19:36:05 -07:00
// Better log messages, compiled spdlog
2022-07-08 16:56:32 -07:00
//
// Get parameters from keyboard (width, height, etc)
//
// Later:
// beat detection
// waveform animation
2022-07-08 17:00:20 -07:00
// pcm from pulse
2022-07-08 10:41:19 -07:00
2022-11-20 12:05:44 -08:00
// HID interface wrapper
Ergodox Dox = Ergodox::init(
HID_VENDOR_ID,
HID_PRODUCT_ID,
HID_USAGE,
HID_USAGE_PAGE
);
2022-06-24 21:33:12 -07:00
2022-07-20 21:31:14 -07:00
2022-11-20 12:05:44 -08:00
int main(int argc, char *argv[]) {
2022-07-08 19:36:05 -07:00
spdlog::set_level(spdlog::level::info);
2022-06-26 12:49:50 -07:00
2022-08-03 18:06:10 -07:00
uint8_t last_layer_layout;
2022-07-08 16:56:32 -07:00
Dox.connect_loop();
2022-07-08 10:41:19 -07:00
2022-11-20 12:05:44 -08:00
#ifndef DISABLE_VISUALIZER
Visualizer::init();
#endif
2022-07-08 19:07:46 -07:00
2022-07-08 16:33:42 -07:00
while (1) {
2022-07-21 18:07:24 -07:00
2022-07-08 16:33:42 -07:00
if (Dox.is_connected()) {
2022-11-20 12:05:44 -08:00
#ifndef DISABLE_VISUALIZER
Visualizer::fn(Dox);
#endif
2022-06-25 19:12:01 -07:00
2022-07-08 16:33:42 -07:00
// 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; }
2022-07-08 10:41:19 -07:00
2022-07-08 16:33:42 -07:00
// Read a packet if there is a packet to read
if (Dox.read()) {
uint8_t cmd = Dox.read_buf[0];
2022-08-10 09:07:04 -07:00
switch (cmd) {
2022-11-20 12:05:44 -08:00
#ifndef DISABLE_SPELL
case CMD_SPELLCHECK_WORD: {
Spell::do_cmd(Dox);
2022-07-08 16:33:42 -07:00
break;
2022-08-10 09:07:04 -07:00
}
2022-11-20 12:05:44 -08:00
#endif
2022-08-10 09:07:04 -07:00
2022-11-20 12:05:44 -08:00
#ifndef DISABLE_SPECIAL_CHAR
2022-08-10 09:07:04 -07:00
case CMD_SPECIAL_CHAR: {
2022-11-20 12:05:44 -08:00
SpecialChars::do_cmd(Dox);
2022-08-10 09:07:04 -07:00
break;
}
2022-11-20 12:05:44 -08:00
#endif
2022-07-08 16:33:42 -07:00
}
}
2022-07-20 21:26:06 -07:00
2022-08-10 09:07:04 -07:00
// Switch layer if necessary.
2022-08-03 18:06:10 -07:00
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;
}
}
2022-07-08 16:33:42 -07:00
} else {
2022-07-08 16:56:32 -07:00
Dox.connect_loop();
2022-07-08 10:41:19 -07:00
}
2022-07-20 21:26:06 -07:00
// Sleep for a bit so we don't consume
// 100% of a cpu.
std::this_thread::sleep_for(
std::chrono::milliseconds(LOOP_SLEEP_MS)
);
2022-06-24 21:33:12 -07:00
}
2022-06-26 12:49:50 -07:00
2022-11-20 12:05:44 -08:00
#ifndef DISABLE_VISUALIZER
Visualizer::cleanup();
#endif
2022-06-25 19:12:01 -07:00
return 0;
2022-06-24 21:33:12 -07:00
}