// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include "RWindow.hpp" #include #include #include #include #include #include #include #include #include #include static gboolean viewUpdate(gpointer data) { RWindow *win = reinterpret_cast(data); win->updateDrawBuffer(); return true; } void RWindow::keyMapInit(void) { keyMap.insert(std::pair('w', static_cast(bsp::KeyCodes::JoystickUp))); keyMap.insert(std::pair('x', static_cast(bsp::KeyCodes::JoystickDown))); keyMap.insert(std::pair('a', static_cast(bsp::KeyCodes::JoystickLeft))); keyMap.insert(std::pair('s', static_cast(bsp::KeyCodes::JoystickEnter))); keyMap.insert(std::pair('d', static_cast(bsp::KeyCodes::JoystickRight))); keyMap.insert(std::pair('q', static_cast(bsp::KeyCodes::FnLeft))); keyMap.insert(std::pair('e', static_cast(bsp::KeyCodes::FnRight))); keyMap.insert(std::pair('0', static_cast(bsp::KeyCodes::NumericKey0))); keyMap.insert(std::pair('1', static_cast(bsp::KeyCodes::NumericKey1))); keyMap.insert(std::pair('2', static_cast(bsp::KeyCodes::NumericKey2))); keyMap.insert(std::pair('3', static_cast(bsp::KeyCodes::NumericKey3))); keyMap.insert(std::pair('4', static_cast(bsp::KeyCodes::NumericKey4))); keyMap.insert(std::pair('5', static_cast(bsp::KeyCodes::NumericKey5))); keyMap.insert(std::pair('6', static_cast(bsp::KeyCodes::NumericKey6))); keyMap.insert(std::pair('7', static_cast(bsp::KeyCodes::NumericKey7))); keyMap.insert(std::pair('8', static_cast(bsp::KeyCodes::NumericKey8))); keyMap.insert(std::pair('9', static_cast(bsp::KeyCodes::NumericKey9))); keyMap.insert(std::pair('z', static_cast(bsp::KeyCodes::NumericKeyAst))); keyMap.insert(std::pair('c', static_cast(bsp::KeyCodes::NumericKeyPnd))); keyMap.insert(std::pair('r', static_cast(bsp::KeyCodes::VolUp))); keyMap.insert(std::pair('f', static_cast(bsp::KeyCodes::VolDown))); keyMap.insert(std::pair('t', static_cast(bsp::KeyCodes::Torch))); keyMap.insert(std::pair('v', static_cast(bsp::KeyCodes::SSwitchDown))); keyMap.insert(std::pair('b', static_cast(bsp::KeyCodes::SSwitchMid))); keyMap.insert(std::pair('n', static_cast(bsp::KeyCodes::SSwitchUp))); keyMap.insert(std::pair('u', static_cast(bsp::KeyCodes::HeadsetOk))); keyMap.insert(std::pair('j', static_cast(bsp::KeyCodes::HeadsetVolUp))); keyMap.insert(std::pair('m', static_cast(bsp::KeyCodes::HeadsetVolDown))); batteryKeyMap.insert(std::pair('[', 1)); batteryKeyMap.insert(std::pair(']', 2)); batteryKeyMap.insert(std::pair('p', 3)); batteryKeyMap.insert(std::pair('l', 3)); batteryKeyMap.insert(std::pair(';', 4)); batteryKeyMap.insert(std::pair('\'', 5)); } void RWindow::updateDrawBuffer() { for (ssize_t i = 0; i < width * height; i++) for (size_t j = 0; j < 3; j++) { uint8_t c = shmMemPtr[i] * 17; // scales 0-15 eink colours indices to 0-255 RGB range rgbMemPtr[i * 3 + j] = c; } queue_draw(); } bool RWindow::onKeyPress(GdkEventKey *event) { if (event->type == GDK_KEY_PRESS) { uint32_t key_code = event->keyval; auto f = keys.find(key_code); if (f == keys.end()) { // slider keys are not generating relase if ((key_code != 'v') && (key_code != 'b') && (key_code != 'n')) { keys.insert(std::pair(event->keyval, event->time)); } std::map::iterator key_it = keyMap.find(key_code); if (key_it != keyMap.end()) { uint8_t message[2]; message[0] = static_cast(bsp::KeyEvents::Pressed); message[1] = key_it->second; write(fifoFd, message, 2); } std::map::iterator batt_it = batteryKeyMap.find(key_code); if (batt_it != batteryKeyMap.end()) { uint8_t message = key_code; write(fifoFdBatt, &message, 1); } } } return false; } bool RWindow::onKeyRelease(GdkEventKey *event) { if (event->type == GDK_KEY_RELEASE) { uint32_t key_code = event->keyval; std::map::iterator it = keys.find(key_code); if (it != keys.end()) { std::map::iterator key_it = keyMap.find(key_code); if (key_it != keyMap.end()) { uint8_t message[2]; message[0] = static_cast(bsp::KeyEvents::Released); message[1] = key_it->second; write(fifoFd, message, 2); } keys.erase(it); } } return false; } RWindow::RWindow(char *shmMemory, int fifoKbd, int fifoBatt, int w, int h) : shmMemPtr{shmMemory}, rgbMemPtr{nullptr}, width{w}, height{h}, fifoFd{fifoKbd}, fifoFdBatt{fifoBatt} { rgbMemPtr = new char[3 * w * h]; drawArea.setDrawData(rgbMemPtr); set_title("GUI RENDERER"); set_default_size(480, 600); add(drawArea); show_all_children(); g_timeout_add(50, viewUpdate, this); this->signal_key_press_event().connect(sigc::mem_fun(*this, &RWindow::onKeyPress), false); this->signal_key_release_event().connect(sigc::mem_fun(*this, &RWindow::onKeyRelease), false); keyMapInit(); } RWindow::~RWindow() { if (rgbMemPtr != nullptr) { delete[] rgbMemPtr; } }