Hyprland/src/managers/input/Tablets.cpp

242 lines
9.7 KiB
C++
Raw Normal View History

2022-06-09 12:46:55 +02:00
#include "InputManager.hpp"
#include "../../Compositor.hpp"
void CInputManager::newTabletTool(wlr_input_device* pDevice) {
const auto PNEWTABLET = &m_lTablets.emplace_back();
2022-06-30 21:26:00 +02:00
try {
PNEWTABLET->name = std::string(pDevice->name);
} catch (std::exception& e) {
Debug::log(ERR, "Tablet had no name???"); // logic error
}
2022-07-11 23:09:35 +02:00
PNEWTABLET->wlrTablet = wlr_tablet_from_input_device(pDevice);
2022-06-09 12:46:55 +02:00
PNEWTABLET->wlrDevice = pDevice;
PNEWTABLET->wlrTabletV2 = wlr_tablet_create(g_pCompositor->m_sWLRTabletManager, g_pCompositor->m_sSeat.seat, pDevice);
PNEWTABLET->wlrTablet->data = PNEWTABLET;
2022-06-09 14:40:56 +02:00
Debug::log(LOG, "Attaching tablet to cursor!");
2022-06-09 12:46:55 +02:00
wlr_cursor_attach_input_device(g_pCompositor->m_sWLRCursor, pDevice);
PNEWTABLET->hyprListener_Destroy.initCallback(&pDevice->events.destroy, [](void* owner, void* data) {
const auto PTAB = (STablet*)owner;
g_pInputManager->m_lTablets.remove(*PTAB);
Debug::log(LOG, "Removed a tablet");
}, PNEWTABLET, "Tablet");
2022-07-11 23:09:35 +02:00
PNEWTABLET->hyprListener_Axis.initCallback(&wlr_tablet_from_input_device(pDevice)->events.axis, [](void* owner, void* data) {
2022-06-09 12:46:55 +02:00
const auto EVENT = (wlr_tablet_tool_axis_event*)data;
const auto PTAB = (STablet*)owner;
switch (EVENT->tool->type) {
case WLR_TABLET_TOOL_TYPE_MOUSE:
wlr_cursor_move(g_pCompositor->m_sWLRCursor, PTAB->wlrDevice, EVENT->dx, EVENT->dy);
g_pInputManager->refocus();
break;
default:
double x = (EVENT->updated_axes & WLR_TABLET_TOOL_AXIS_X) ? EVENT->x : NAN;
double y = (EVENT->updated_axes & WLR_TABLET_TOOL_AXIS_Y) ? EVENT->y : NAN;
wlr_cursor_warp_absolute(g_pCompositor->m_sWLRCursor, PTAB->wlrDevice, x, y);
g_pInputManager->refocus();
break;
}
2022-06-09 19:38:39 +02:00
const auto PTOOL = g_pInputManager->ensureTabletToolPresent(EVENT->tool);
2022-06-09 12:46:55 +02:00
// TODO: this might be wrong
2022-06-09 17:16:01 +02:00
if (PTOOL->active) {
2022-06-09 12:46:55 +02:00
g_pInputManager->refocus();
2022-06-09 18:09:09 +02:00
g_pInputManager->focusTablet(PTAB, EVENT->tool, true);
2022-06-09 17:16:01 +02:00
}
2022-06-09 19:19:42 +02:00
if (EVENT->updated_axes & WLR_TABLET_TOOL_AXIS_PRESSURE)
2022-06-09 12:46:55 +02:00
wlr_tablet_v2_tablet_tool_notify_pressure(PTOOL->wlrTabletToolV2, EVENT->pressure);
2022-06-09 19:19:42 +02:00
if (EVENT->updated_axes & WLR_TABLET_TOOL_AXIS_DISTANCE)
2022-06-09 12:46:55 +02:00
wlr_tablet_v2_tablet_tool_notify_distance(PTOOL->wlrTabletToolV2, EVENT->distance);
2022-06-09 19:19:42 +02:00
if (EVENT->updated_axes & WLR_TABLET_TOOL_AXIS_ROTATION)
2022-06-09 12:46:55 +02:00
wlr_tablet_v2_tablet_tool_notify_rotation(PTOOL->wlrTabletToolV2, EVENT->rotation);
2022-06-09 19:19:42 +02:00
if (EVENT->updated_axes & WLR_TABLET_TOOL_AXIS_SLIDER)
2022-06-09 12:46:55 +02:00
wlr_tablet_v2_tablet_tool_notify_slider(PTOOL->wlrTabletToolV2, EVENT->slider);
2022-06-09 19:19:42 +02:00
if (EVENT->updated_axes & WLR_TABLET_TOOL_AXIS_WHEEL)
2022-06-09 12:46:55 +02:00
wlr_tablet_v2_tablet_tool_notify_wheel(PTOOL->wlrTabletToolV2, EVENT->wheel_delta, 0);
if (EVENT->updated_axes & WLR_TABLET_TOOL_AXIS_TILT_X)
PTOOL->tiltX = EVENT->tilt_x;
if (EVENT->updated_axes & WLR_TABLET_TOOL_AXIS_TILT_Y)
PTOOL->tiltY = EVENT->tilt_y;
2022-06-09 19:19:42 +02:00
if (EVENT->updated_axes & (WLR_TABLET_TOOL_AXIS_TILT_X | WLR_TABLET_TOOL_AXIS_TILT_Y))
2022-06-09 12:46:55 +02:00
wlr_tablet_v2_tablet_tool_notify_tilt(PTOOL->wlrTabletToolV2, PTOOL->tiltX, PTOOL->tiltY);
2022-06-09 19:19:42 +02:00
2022-06-09 12:46:55 +02:00
}, PNEWTABLET, "Tablet");
2022-07-11 23:09:35 +02:00
PNEWTABLET->hyprListener_Tip.initCallback(&wlr_tablet_from_input_device(pDevice)->events.tip, [](void* owner, void* data) {
2022-06-09 12:46:55 +02:00
const auto EVENT = (wlr_tablet_tool_tip_event*)data;
const auto PTAB = (STablet*)owner;
2022-06-09 19:38:39 +02:00
const auto PTOOL = g_pInputManager->ensureTabletToolPresent(EVENT->tool);
2022-06-09 12:46:55 +02:00
// TODO: this might be wrong
2022-06-09 17:40:05 +02:00
if (EVENT->state == WLR_TABLET_TOOL_TIP_DOWN) {
g_pInputManager->refocus();
2022-06-09 18:09:09 +02:00
g_pInputManager->focusTablet(PTAB, EVENT->tool);
2022-06-09 12:46:55 +02:00
wlr_send_tablet_v2_tablet_tool_down(PTOOL->wlrTabletToolV2);
2022-06-09 17:40:05 +02:00
}
else {
2022-06-09 12:46:55 +02:00
wlr_send_tablet_v2_tablet_tool_up(PTOOL->wlrTabletToolV2);
2022-06-09 17:40:05 +02:00
}
2022-06-09 12:46:55 +02:00
}, PNEWTABLET, "Tablet");
2022-07-11 23:09:35 +02:00
PNEWTABLET->hyprListener_Button.initCallback(&wlr_tablet_from_input_device(pDevice)->events.button, [](void* owner, void* data) {
2022-06-09 12:46:55 +02:00
const auto EVENT = (wlr_tablet_tool_button_event*)data;
2022-06-09 19:38:39 +02:00
const auto PTOOL = g_pInputManager->ensureTabletToolPresent(EVENT->tool);
2022-06-09 12:46:55 +02:00
wlr_tablet_v2_tablet_tool_notify_button(PTOOL->wlrTabletToolV2, (zwp_tablet_pad_v2_button_state)EVENT->button, (zwp_tablet_pad_v2_button_state)EVENT->state);
}, PNEWTABLET, "Tablet");
2022-07-11 23:09:35 +02:00
PNEWTABLET->hyprListener_Proximity.initCallback(&wlr_tablet_from_input_device(pDevice)->events.proximity, [](void* owner, void* data) {
2022-06-09 12:46:55 +02:00
const auto EVENT = (wlr_tablet_tool_proximity_event*)data;
const auto PTAB = (STablet*)owner;
2022-06-09 19:38:39 +02:00
const auto PTOOL = g_pInputManager->ensureTabletToolPresent(EVENT->tool);
2022-06-09 12:46:55 +02:00
if (EVENT->state == WLR_TABLET_TOOL_PROXIMITY_OUT) {
PTOOL->active = false;
2022-06-09 21:19:31 +02:00
if (PTOOL->pSurface) {
wlr_tablet_v2_tablet_tool_notify_proximity_out(PTOOL->wlrTabletToolV2);
PTOOL->pSurface = nullptr;
}
2022-06-09 12:46:55 +02:00
} else {
PTOOL->active = true;
g_pInputManager->refocus();
2022-06-09 18:09:09 +02:00
g_pInputManager->focusTablet(PTAB, EVENT->tool);
2022-06-09 12:46:55 +02:00
}
}, PNEWTABLET, "Tablet");
}
2022-06-09 19:38:39 +02:00
STabletTool* CInputManager::ensureTabletToolPresent(wlr_tablet_tool* pTool) {
2022-06-09 12:46:55 +02:00
if (pTool->data == nullptr) {
const auto PTOOL = &m_lTabletTools.emplace_back();
2022-06-09 14:40:56 +02:00
Debug::log(LOG, "Creating tablet tool v2 for %x", pTool);
2022-06-09 12:46:55 +02:00
PTOOL->wlrTabletTool = pTool;
pTool->data = PTOOL;
PTOOL->wlrTabletToolV2 = wlr_tablet_tool_create(g_pCompositor->m_sWLRTabletManager, g_pCompositor->m_sSeat.seat, pTool);
PTOOL->hyprListener_TabletToolDestroy.initCallback(&pTool->events.destroy, [](void* owner, void* data) {
const auto PTOOL = (STabletTool*)owner;
PTOOL->wlrTabletTool->data = nullptr;
g_pInputManager->m_lTabletTools.remove(*PTOOL);
}, PTOOL, "Tablet Tool V1");
//TODO: set cursor request
}
return (STabletTool*)pTool->data;
}
void CInputManager::newTabletPad(wlr_input_device* pDevice) {
const auto PNEWPAD = &m_lTabletPads.emplace_back();
2022-06-30 21:26:00 +02:00
try {
PNEWPAD->name = std::string(pDevice->name);
} catch (std::exception& e) {
Debug::log(ERR, "Pad had no name???"); // logic error
}
2022-06-09 12:46:55 +02:00
PNEWPAD->wlrTabletPadV2 = wlr_tablet_pad_create(g_pCompositor->m_sWLRTabletManager, g_pCompositor->m_sSeat.seat, pDevice);
2022-07-11 23:09:35 +02:00
PNEWPAD->hyprListener_Button.initCallback(&wlr_tablet_pad_from_input_device(pDevice)->events.button, [](void* owner, void* data) {
2022-06-09 12:46:55 +02:00
const auto EVENT = (wlr_tablet_pad_button_event*)data;
const auto PPAD = (STabletPad*)owner;
wlr_tablet_v2_tablet_pad_notify_mode(PPAD->wlrTabletPadV2, EVENT->group, EVENT->mode, EVENT->time_msec);
wlr_tablet_v2_tablet_pad_notify_button(PPAD->wlrTabletPadV2, EVENT->button, EVENT->time_msec, (zwp_tablet_pad_v2_button_state)EVENT->state);
}, PNEWPAD, "Tablet Pad");
2022-07-11 23:09:35 +02:00
PNEWPAD->hyprListener_Strip.initCallback(&wlr_tablet_pad_from_input_device(pDevice)->events.strip, [](void* owner, void* data) {
2022-06-09 12:46:55 +02:00
const auto EVENT = (wlr_tablet_pad_strip_event*)data;
const auto PPAD = (STabletPad*)owner;
wlr_tablet_v2_tablet_pad_notify_strip(PPAD->wlrTabletPadV2, EVENT->strip, EVENT->position, EVENT->source == WLR_TABLET_PAD_STRIP_SOURCE_FINGER, EVENT->time_msec);
}, PNEWPAD, "Tablet Pad");
2022-07-11 23:09:35 +02:00
PNEWPAD->hyprListener_Ring.initCallback(&wlr_tablet_pad_from_input_device(pDevice)->events.strip, [](void* owner, void* data) {
2022-06-09 12:46:55 +02:00
const auto EVENT = (wlr_tablet_pad_ring_event*)data;
const auto PPAD = (STabletPad*)owner;
wlr_tablet_v2_tablet_pad_notify_ring(PPAD->wlrTabletPadV2, EVENT->ring, EVENT->position, EVENT->source == WLR_TABLET_PAD_RING_SOURCE_FINGER, EVENT->time_msec);
}, PNEWPAD, "Tablet Pad");
2022-07-11 23:09:35 +02:00
PNEWPAD->hyprListener_Attach.initCallback(&wlr_tablet_pad_from_input_device(pDevice)->events.strip, [](void* owner, void* data) {
2022-06-09 12:46:55 +02:00
const auto TABLET = (wlr_tablet_tool*)data;
const auto PPAD = (STabletPad*)owner;
PPAD->pTabletParent = (STablet*)TABLET->data;
if (!PPAD->pTabletParent)
Debug::log(ERR, "tabletpad got attached to a nullptr tablet!! this might be bad.");
}, PNEWPAD, "Tablet Pad");
PNEWPAD->hyprListener_Destroy.initCallback(&pDevice->events.destroy, [](void* owner, void* data) {
const auto PPAD = (STabletPad*)owner;
g_pInputManager->m_lTabletPads.remove(*PPAD);
Debug::log(LOG, "Removed a tablet pad");
}, PNEWPAD, "Tablet Pad");
2022-06-09 18:09:09 +02:00
}
void CInputManager::focusTablet(STablet* pTab, wlr_tablet_tool* pTool, bool motion) {
2022-06-09 19:38:39 +02:00
const auto PTOOL = g_pInputManager->ensureTabletToolPresent(pTool);
2022-06-09 18:09:09 +02:00
if (const auto PWINDOW = g_pCompositor->m_pLastWindow; g_pCompositor->windowValidMapped(PWINDOW)) {
const auto CURSORPOS = g_pInputManager->getMouseCoordsInternal();
const auto LOCAL = CURSORPOS - PWINDOW->m_vRealPosition.goalv();
2022-06-09 19:47:05 +02:00
if (PTOOL->pSurface != g_pCompositor->m_pLastFocus)
2022-06-09 18:09:09 +02:00
wlr_tablet_v2_tablet_tool_notify_proximity_out(PTOOL->wlrTabletToolV2);
2022-06-09 19:47:05 +02:00
if (g_pCompositor->m_pLastFocus) {
PTOOL->pSurface = g_pCompositor->m_pLastFocus;
2022-06-09 18:09:09 +02:00
wlr_tablet_v2_tablet_tool_notify_proximity_in(PTOOL->wlrTabletToolV2, pTab->wlrTabletV2, g_pCompositor->m_pLastFocus);
2022-06-09 19:47:05 +02:00
}
2022-06-09 18:09:09 +02:00
if (motion)
wlr_tablet_v2_tablet_tool_notify_motion(PTOOL->wlrTabletToolV2, LOCAL.x, LOCAL.y);
2022-06-09 19:38:39 +02:00
} else {
2022-06-09 19:47:05 +02:00
if (PTOOL->pSurface)
wlr_tablet_v2_tablet_tool_notify_proximity_out(PTOOL->wlrTabletToolV2);
2022-06-09 18:09:09 +02:00
}
2022-06-24 22:28:54 +02:00
}