Hyprland/src/managers/input/InputManager.cpp

1155 lines
48 KiB
C++
Raw Normal View History

2022-03-17 15:53:45 +01:00
#include "InputManager.hpp"
2022-06-09 12:46:55 +02:00
#include "../../Compositor.hpp"
2022-03-17 15:53:45 +01:00
2022-03-24 15:57:46 +01:00
void CInputManager::onMouseMoved(wlr_pointer_motion_event* e) {
2022-09-03 19:35:17 +02:00
static auto *const PSENS = &g_pConfigManager->getConfigValuePtr("general:sensitivity")->floatValue;
static auto *const PNOACCEL = &g_pConfigManager->getConfigValuePtr("input:force_no_accel")->intValue;
static auto* const PSENSTORAW = &g_pConfigManager->getConfigValuePtr("general:apply_sens_to_raw")->intValue;
2022-03-17 15:53:45 +01:00
2022-09-03 19:35:17 +02:00
const auto DELTA = *PNOACCEL == 1 ? Vector2D(e->unaccel_dx, e->unaccel_dy) : Vector2D(e->delta_x, e->delta_y);
2022-06-06 12:08:33 +02:00
2022-09-03 19:35:17 +02:00
if (*PSENSTORAW == 1)
wlr_relative_pointer_manager_v1_send_relative_motion(g_pCompositor->m_sWLRRelPointerMgr, g_pCompositor->m_sSeat.seat, (uint64_t)e->time_msec * 1000, DELTA.x * *PSENS, DELTA.y * *PSENS, e->unaccel_dx * *PSENS, e->unaccel_dy * *PSENS);
2022-04-18 13:13:41 +02:00
else
2022-06-06 12:08:33 +02:00
wlr_relative_pointer_manager_v1_send_relative_motion(g_pCompositor->m_sWLRRelPointerMgr, g_pCompositor->m_sSeat.seat, (uint64_t)e->time_msec * 1000, DELTA.x, DELTA.y, e->unaccel_dx, e->unaccel_dy);
2022-09-03 19:35:17 +02:00
wlr_cursor_move(g_pCompositor->m_sWLRCursor, &e->pointer->base, DELTA.x * *PSENS, DELTA.y * *PSENS);
2022-03-18 20:42:49 +01:00
2022-03-18 23:52:36 +01:00
mouseMoveUnified(e->time_msec);
2022-06-24 23:27:02 +02:00
m_tmrLastCursorMovement.reset();
2022-03-17 16:56:33 +01:00
}
2022-03-24 15:57:46 +01:00
void CInputManager::onMouseWarp(wlr_pointer_motion_absolute_event* e) {
wlr_cursor_warp_absolute(g_pCompositor->m_sWLRCursor, &e->pointer->base, e->x, e->y);
2022-03-18 20:42:49 +01:00
2022-03-18 23:52:36 +01:00
mouseMoveUnified(e->time_msec);
2022-06-24 23:27:02 +02:00
m_tmrLastCursorMovement.reset();
2022-03-18 23:52:36 +01:00
}
2022-04-13 20:19:40 +02:00
void CInputManager::mouseMoveUnified(uint32_t time, bool refocus) {
2022-07-07 21:47:59 +02:00
static auto *const PFOLLOWMOUSE = &g_pConfigManager->getConfigValuePtr("input:follow_mouse")->intValue;
2022-08-06 22:26:32 +02:00
static auto *const PMOUSEDPMS = &g_pConfigManager->getConfigValuePtr("misc:mouse_move_enables_dpms")->intValue;
static auto *const PFOLLOWONDND = &g_pConfigManager->getConfigValuePtr("misc:always_follow_on_dnd")->intValue;
2022-08-10 17:46:01 +02:00
static auto *const PHOGFOCUS = &g_pConfigManager->getConfigValuePtr("misc:layers_hog_keyboard_focus")->intValue;
2022-08-22 18:22:26 +02:00
static auto *const PFLOATBEHAVIOR = &g_pConfigManager->getConfigValuePtr("input:float_switch_override_focus")->intValue;
2022-07-07 21:47:59 +02:00
m_pFoundSurfaceToFocus = nullptr;
m_pFoundLSToFocus = nullptr;
m_pFoundWindowToFocus = nullptr;
if (!g_pCompositor->m_bReadyToProcess || g_pCompositor->m_bIsShuttingDown)
return;
2022-05-12 12:13:02 +02:00
if (!g_pCompositor->m_sSeat.mouse) {
Debug::log(ERR, "BUG THIS: Mouse move on mouse nullptr!");
return;
}
2022-07-11 20:23:16 +02:00
if (g_pCompositor->m_sSeat.mouse->virt)
return; // don't refocus on virt
2022-07-31 15:46:42 +02:00
if (!g_pCompositor->m_bDPMSStateON && *PMOUSEDPMS) {
// enable dpms
g_pKeybindManager->dpms("on");
}
Vector2D mouseCoords = getMouseCoordsInternal();
2022-07-01 17:59:11 +02:00
const auto MOUSECOORDSFLOORED = mouseCoords.floor();
if (MOUSECOORDSFLOORED == m_vLastCursorPosFloored && !refocus)
2022-07-01 17:59:11 +02:00
return;
m_vLastCursorPosFloored = MOUSECOORDSFLOORED;
const auto PMONITOR = g_pCompositor->getMonitorFromCursor();
bool didConstraintOnCursor = false;
// constraints
// All constraints TODO: multiple mice?
if (g_pCompositor->m_sSeat.mouse->currentConstraint) {
// XWayland windows sometimes issue constraints weirdly.
// TODO: We probably should search their parent. wlr_xwayland_surface->parent
const auto CONSTRAINTWINDOW = g_pCompositor->getConstraintWindow(g_pCompositor->m_sSeat.mouse);
if (!CONSTRAINTWINDOW) {
2022-08-09 20:36:21 +02:00
unconstrainMouse();
} else {
// Native Wayland apps know how 2 constrain themselves.
// XWayland, we just have to accept them. Might cause issues, but thats XWayland for ya.
const auto CONSTRAINTPOS = CONSTRAINTWINDOW->m_bIsX11 ? Vector2D(CONSTRAINTWINDOW->m_uSurface.xwayland->x, CONSTRAINTWINDOW->m_uSurface.xwayland->y) : CONSTRAINTWINDOW->m_vRealPosition.vec();
const auto CONSTRAINTSIZE = CONSTRAINTWINDOW->m_bIsX11 ? Vector2D(CONSTRAINTWINDOW->m_uSurface.xwayland->width, CONSTRAINTWINDOW->m_uSurface.xwayland->height) : CONSTRAINTWINDOW->m_vRealSize.vec();
if (!VECINRECT(mouseCoords, CONSTRAINTPOS.x, CONSTRAINTPOS.y, CONSTRAINTPOS.x + CONSTRAINTSIZE.x - 1.0, CONSTRAINTPOS.y + CONSTRAINTSIZE.y - 1.0)) {
if (g_pCompositor->m_sSeat.mouse->constraintActive) {
Vector2D newConstrainedCoords = mouseCoords;
if (mouseCoords.x < CONSTRAINTPOS.x)
newConstrainedCoords.x = CONSTRAINTPOS.x;
else if (mouseCoords.x >= CONSTRAINTPOS.x + CONSTRAINTSIZE.x)
newConstrainedCoords.x = CONSTRAINTPOS.x + CONSTRAINTSIZE.x - 1.0;
if (mouseCoords.y < CONSTRAINTPOS.y)
newConstrainedCoords.y = CONSTRAINTPOS.y;
else if (mouseCoords.y >= CONSTRAINTPOS.y + CONSTRAINTSIZE.y)
newConstrainedCoords.y = CONSTRAINTPOS.y + CONSTRAINTSIZE.y - 1.0;
wlr_cursor_warp_closest(g_pCompositor->m_sWLRCursor, g_pCompositor->m_sSeat.mouse->mouse, newConstrainedCoords.x, newConstrainedCoords.y);
mouseCoords = newConstrainedCoords;
didConstraintOnCursor = true;
}
} else {
if ((!CONSTRAINTWINDOW->m_bIsX11 && PMONITOR && CONSTRAINTWINDOW->m_iWorkspaceID == PMONITOR->activeWorkspace) || (CONSTRAINTWINDOW->m_bIsX11)) {
g_pCompositor->m_sSeat.mouse->constraintActive = true;
didConstraintOnCursor = true;
}
}
}
}
2022-03-31 17:25:23 +02:00
// update stuff
updateDragIcon();
2022-04-13 20:19:40 +02:00
g_pLayoutManager->getCurrentLayout()->onMouseMove(getMouseCoordsInternal());
2022-03-31 17:25:23 +02:00
// focus
2022-03-20 14:36:55 +01:00
wlr_surface* foundSurface = nullptr;
2022-03-31 17:25:23 +02:00
2022-06-21 22:29:11 +02:00
if (didConstraintOnCursor)
return; // don't process when cursor constrained
2022-03-31 17:25:23 +02:00
if (PMONITOR && PMONITOR != g_pCompositor->m_pLastMonitor) {
g_pCompositor->m_pLastMonitor = PMONITOR;
2022-03-31 17:25:23 +02:00
// set active workspace and deactivate all other in wlr
2022-05-25 10:25:36 +02:00
const auto ACTIVEWORKSPACE = g_pCompositor->getWorkspaceByID(PMONITOR->activeWorkspace);
g_pCompositor->deactivateAllWLRWorkspaces(ACTIVEWORKSPACE->m_pWlrHandle);
ACTIVEWORKSPACE->setActive(true);
// event
g_pEventManager->postEvent(SHyprIPCEvent{"focusedmon", PMONITOR->szName + "," + ACTIVEWORKSPACE->m_szName});
}
2022-03-22 21:59:14 +01:00
Vector2D surfaceCoords;
Vector2D surfacePos = Vector2D(-1337, -1337);
2022-04-02 18:57:09 +02:00
CWindow* pFoundWindow = nullptr;
SLayerSurface* pFoundLayerSurface = nullptr;
2022-03-20 14:36:55 +01:00
// overlay is above fullscreen
if (!foundSurface)
foundSurface = g_pCompositor->vectorToLayerSurface(mouseCoords, &PMONITOR->m_aLayerSurfaceLists[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY], &surfaceCoords, &pFoundLayerSurface);
2022-09-19 11:23:13 +02:00
if (!foundSurface)
foundSurface = g_pCompositor->vectorToLayerSurface(mouseCoords, &PMONITOR->m_aLayerSurfaceLists[ZWLR_LAYER_SHELL_V1_LAYER_TOP], &surfaceCoords, &pFoundLayerSurface);
// then, we check if the workspace doesnt have a fullscreen window
2022-03-21 19:18:33 +01:00
const auto PWORKSPACE = g_pCompositor->getWorkspaceByID(PMONITOR->activeWorkspace);
2022-05-31 14:01:00 +02:00
if (PWORKSPACE->m_bHasFullscreenWindow && !foundSurface && PWORKSPACE->m_efFullscreenMode == FULLSCREEN_FULL) {
pFoundWindow = g_pCompositor->getFullscreenWindowOnWorkspace(PWORKSPACE->m_iID);
if (!pFoundWindow) {
// what the fuck, somehow happens occasionally??
PWORKSPACE->m_bHasFullscreenWindow = false;
return;
}
// only check floating because tiled cant be over fullscreen
2022-06-30 15:44:26 +02:00
for (auto w = g_pCompositor->m_vWindows.rbegin(); w != g_pCompositor->m_vWindows.rend(); w++) {
wlr_box box = {(*w)->m_vRealPosition.vec().x, (*w)->m_vRealPosition.vec().y, (*w)->m_vRealSize.vec().x, (*w)->m_vRealSize.vec().y};
if ((((*w)->m_bIsFloating && (*w)->m_bIsMapped && ((*w)->m_bCreatedOverFullscreen || (*w)->m_bPinned)) || ((*w)->m_iWorkspaceID == SPECIAL_WORKSPACE_ID && PMONITOR->specialWorkspaceOpen)) && wlr_box_contains_point(&box, mouseCoords.x, mouseCoords.y) && g_pCompositor->isWorkspaceVisible((*w)->m_iWorkspaceID) && !(*w)->isHidden()) {
2022-06-30 15:44:26 +02:00
pFoundWindow = (*w).get();
break;
2022-04-02 18:57:09 +02:00
}
}
if (!pFoundWindow->m_bIsX11) {
foundSurface = g_pCompositor->vectorWindowToSurface(mouseCoords, pFoundWindow, surfaceCoords);
surfacePos = Vector2D(-1337, -1337);
} else {
foundSurface = g_pXWaylandManager->getWindowSurface(pFoundWindow);
surfacePos = pFoundWindow->m_vRealPosition.vec();
}
2022-03-21 19:18:33 +01:00
}
2022-03-20 14:36:55 +01:00
// then windows
2022-04-24 11:41:52 +02:00
if (!foundSurface) {
if (PWORKSPACE->m_bHasFullscreenWindow && PWORKSPACE->m_efFullscreenMode == FULLSCREEN_MAXIMIZED) {
if (PMONITOR->specialWorkspaceOpen) {
pFoundWindow = g_pCompositor->vectorToWindowIdeal(mouseCoords);
if (pFoundWindow && pFoundWindow->m_iWorkspaceID != SPECIAL_WORKSPACE_ID) {
pFoundWindow = g_pCompositor->getFullscreenWindowOnWorkspace(PWORKSPACE->m_iID);
}
} else {
2022-07-13 18:33:36 +02:00
pFoundWindow = g_pCompositor->vectorToWindowIdeal(mouseCoords);
if (!(pFoundWindow && pFoundWindow->m_bIsFloating && pFoundWindow->m_bCreatedOverFullscreen))
pFoundWindow = g_pCompositor->getFullscreenWindowOnWorkspace(PWORKSPACE->m_iID);
}
2022-08-19 17:25:07 +02:00
} else {
2022-06-29 14:44:24 +02:00
pFoundWindow = g_pCompositor->vectorToWindowIdeal(mouseCoords);
2022-08-19 23:22:53 +02:00
// TODO: this causes crashes, sometimes. ???
// if (refocus && !pFoundWindow) {
// pFoundWindow = g_pCompositor->getFirstWindowOnWorkspace(PMONITOR->activeWorkspace);
// }
2022-08-19 17:25:07 +02:00
}
2022-04-24 11:41:52 +02:00
if (pFoundWindow) {
if (!pFoundWindow->m_bIsX11) {
foundSurface = g_pCompositor->vectorWindowToSurface(mouseCoords, pFoundWindow, surfaceCoords);
} else {
foundSurface = g_pXWaylandManager->getWindowSurface(pFoundWindow);
surfacePos = pFoundWindow->m_vRealPosition.vec();
}
2022-04-02 13:02:16 +02:00
}
2022-03-20 14:36:55 +01:00
}
2022-04-02 18:57:09 +02:00
2022-03-20 14:36:55 +01:00
// then surfaces below
if (!foundSurface)
foundSurface = g_pCompositor->vectorToLayerSurface(mouseCoords, &PMONITOR->m_aLayerSurfaceLists[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM], &surfaceCoords, &pFoundLayerSurface);
2022-03-20 14:36:55 +01:00
if (!foundSurface)
foundSurface = g_pCompositor->vectorToLayerSurface(mouseCoords, &PMONITOR->m_aLayerSurfaceLists[ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND], &surfaceCoords, &pFoundLayerSurface);
2022-03-20 14:36:55 +01:00
2022-08-28 22:45:05 +02:00
g_pCompositor->scheduleFrameForMonitor(g_pCompositor->m_pLastMonitor);
2022-03-20 14:36:55 +01:00
if (!foundSurface) {
2022-08-28 22:45:05 +02:00
if (!m_bEmptyFocusCursorSet) {
// TODO: maybe wrap?
if (m_ecbClickBehavior == CLICKMODE_KILL)
wlr_xcursor_manager_set_cursor_image(g_pCompositor->m_sWLRXCursorMgr, "crosshair", g_pCompositor->m_sWLRCursor);
else
wlr_xcursor_manager_set_cursor_image(g_pCompositor->m_sWLRXCursorMgr, "left_ptr", g_pCompositor->m_sWLRCursor);
m_bEmptyFocusCursorSet = true;
}
2022-03-18 23:52:36 +01:00
2022-03-22 18:29:13 +01:00
wlr_seat_pointer_clear_focus(g_pCompositor->m_sSeat.seat);
2022-03-18 23:52:36 +01:00
2022-07-07 21:47:59 +02:00
if (refocus) { // if we are forcing a refocus, and we don't find a surface, clear the kb focus too!
g_pCompositor->focusWindow(nullptr);
2022-07-07 21:47:59 +02:00
}
2022-03-18 23:52:36 +01:00
return;
}
2022-08-28 22:45:05 +02:00
m_bEmptyFocusCursorSet = false;
2022-03-18 23:52:36 +01:00
if (time)
2022-03-22 18:29:13 +01:00
wlr_idle_notify_activity(g_pCompositor->m_sWLRIdle, g_pCompositor->m_sSeat.seat);
2022-03-18 23:52:36 +01:00
Vector2D surfaceLocal = surfacePos == Vector2D(-1337, -1337) ? surfaceCoords : mouseCoords - surfacePos;
2022-03-18 23:52:36 +01:00
2022-06-22 15:45:56 +02:00
if (pFoundWindow && !pFoundWindow->m_bIsX11 && surfacePos != Vector2D(-1337, -1337)) {
// calc for oversized windows... fucking bullshit.
wlr_box geom;
wlr_xdg_surface_get_geometry(pFoundWindow->m_uSurface.xdg, &geom);
surfaceLocal = mouseCoords - surfacePos + Vector2D(geom.x, geom.y);
}
2022-08-10 17:46:01 +02:00
bool allowKeyboardRefocus = true;
if (*PHOGFOCUS && !refocus && g_pCompositor->m_pLastFocus) {
const auto PLS = g_pCompositor->getLayerSurfaceFromSurface(g_pCompositor->m_pLastFocus);
if (PLS && PLS->layerSurface->current.keyboard_interactive) {
allowKeyboardRefocus = false;
}
}
// set the values for use
if (refocus) {
m_pFoundLSToFocus = pFoundLayerSurface;
m_pFoundWindowToFocus = pFoundWindow;
m_pFoundSurfaceToFocus = foundSurface;
}
2022-04-13 20:19:40 +02:00
if (pFoundWindow) {
2022-06-12 09:31:56 +02:00
if (*PFOLLOWMOUSE != 1 && !refocus) {
2022-10-15 15:13:16 +02:00
if (pFoundWindow != g_pCompositor->m_pLastWindow && g_pCompositor->m_pLastWindow && ((pFoundWindow->m_bIsFloating && *PFLOATBEHAVIOR == 2) || (g_pCompositor->m_pLastWindow->m_bIsFloating != pFoundWindow->m_bIsFloating && *PFLOATBEHAVIOR != 0))) {
2022-04-13 20:19:40 +02:00
// enter if change floating style
2022-08-10 17:46:01 +02:00
if (*PFOLLOWMOUSE != 3 && allowKeyboardRefocus)
2022-08-01 18:42:11 +02:00
g_pCompositor->focusWindow(pFoundWindow, foundSurface);
2022-04-13 20:19:40 +02:00
wlr_seat_pointer_notify_enter(g_pCompositor->m_sSeat.seat, foundSurface, surfaceLocal.x, surfaceLocal.y);
2022-06-21 22:29:11 +02:00
} else if (*PFOLLOWMOUSE == 2) {
wlr_seat_pointer_notify_enter(g_pCompositor->m_sSeat.seat, foundSurface, surfaceLocal.x, surfaceLocal.y);
}
if (pFoundWindow == g_pCompositor->m_pLastWindow) {
if (foundSurface != g_pCompositor->m_pLastFocus || m_bLastFocusOnLS) {
// ^^^ changed the subsurface ^^^ came back from a LS
wlr_seat_pointer_notify_enter(g_pCompositor->m_sSeat.seat, foundSurface, surfaceLocal.x, surfaceLocal.y);
}
}
2022-08-06 22:26:32 +02:00
if (*PFOLLOWONDND && m_sDrag.dragIcon) {
wlr_seat_pointer_notify_enter(g_pCompositor->m_sSeat.seat, foundSurface, surfaceLocal.x, surfaceLocal.y);
}
if (*PFOLLOWMOUSE != 0 || pFoundWindow == g_pCompositor->m_pLastWindow)
wlr_seat_pointer_notify_motion(g_pCompositor->m_sSeat.seat, time, surfaceLocal.x, surfaceLocal.y);
2022-09-25 20:07:48 +02:00
m_bLastFocusOnLS = false;
2022-06-21 22:29:11 +02:00
return; // don't enter any new surfaces
2022-04-13 20:19:40 +02:00
} else {
2022-08-22 18:16:32 +02:00
if ((*PFOLLOWMOUSE != 3 && allowKeyboardRefocus) || refocus)
2022-08-01 18:42:11 +02:00
g_pCompositor->focusWindow(pFoundWindow, foundSurface);
2022-04-13 20:19:40 +02:00
}
m_bLastFocusOnLS = false;
2022-08-10 17:46:01 +02:00
} else {
2022-08-19 17:29:16 +02:00
if (pFoundLayerSurface && pFoundLayerSurface->layerSurface->current.keyboard_interactive && *PFOLLOWMOUSE != 3 && allowKeyboardRefocus) {
2022-08-10 17:46:01 +02:00
g_pCompositor->focusSurface(foundSurface);
2022-08-19 17:29:16 +02:00
}
if (pFoundLayerSurface)
m_bLastFocusOnLS = true;
2022-08-10 17:46:01 +02:00
}
2022-03-22 21:59:14 +01:00
2022-04-05 18:29:58 +02:00
wlr_seat_pointer_notify_enter(g_pCompositor->m_sSeat.seat, foundSurface, surfaceLocal.x, surfaceLocal.y);
wlr_seat_pointer_notify_motion(g_pCompositor->m_sSeat.seat, time, surfaceLocal.x, surfaceLocal.y);
2022-03-18 20:42:49 +01:00
}
2022-03-24 15:57:46 +01:00
void CInputManager::onMouseButton(wlr_pointer_button_event* e) {
2022-03-22 18:29:13 +01:00
wlr_idle_notify_activity(g_pCompositor->m_sWLRIdle, g_pCompositor->m_sSeat.seat);
2022-03-18 20:42:49 +01:00
2022-06-24 23:27:02 +02:00
m_tmrLastCursorMovement.reset();
2022-06-27 13:42:20 +02:00
switch (m_ecbClickBehavior) {
case CLICKMODE_DEFAULT:
processMouseDownNormal(e);
break;
case CLICKMODE_KILL:
processMouseDownKill(e);
break;
default:
break;
}
}
void CInputManager::processMouseRequest(wlr_seat_pointer_request_set_cursor_event* e) {
if (!g_pHyprRenderer->shouldRenderCursor())
return;
if (!e->surface) {
g_pHyprRenderer->m_bWindowRequestedCursorHide = true;
} else {
g_pHyprRenderer->m_bWindowRequestedCursorHide = false;
}
if (m_ecbClickBehavior == CLICKMODE_KILL) {
wlr_xcursor_manager_set_cursor_image(g_pCompositor->m_sWLRXCursorMgr, "crosshair", g_pCompositor->m_sWLRCursor);
return;
}
if (e->seat_client == g_pCompositor->m_sSeat.seat->pointer_state.focused_client)
wlr_cursor_set_surface(g_pCompositor->m_sWLRCursor, e->surface, e->hotspot_x, e->hotspot_y);
}
eClickBehaviorMode CInputManager::getClickMode() {
return m_ecbClickBehavior;
}
void CInputManager::setClickMode(eClickBehaviorMode mode) {
switch (mode) {
case CLICKMODE_DEFAULT:
Debug::log(LOG, "SetClickMode: DEFAULT");
m_ecbClickBehavior = CLICKMODE_DEFAULT;
wlr_xcursor_manager_set_cursor_image(g_pCompositor->m_sWLRXCursorMgr, "left_ptr", g_pCompositor->m_sWLRCursor);
break;
case CLICKMODE_KILL:
Debug::log(LOG, "SetClickMode: KILL");
m_ecbClickBehavior = CLICKMODE_KILL;
// remove constraints
2022-08-09 20:36:21 +02:00
g_pInputManager->unconstrainMouse();
2022-06-27 13:42:20 +02:00
refocus();
// set cursor
wlr_xcursor_manager_set_cursor_image(g_pCompositor->m_sWLRXCursorMgr, "crosshair", g_pCompositor->m_sWLRCursor);
break;
default:
break;
2022-09-25 20:07:48 +02:00
}
2022-06-27 13:42:20 +02:00
}
void CInputManager::processMouseDownNormal(wlr_pointer_button_event* e) {
2022-03-22 18:29:13 +01:00
const auto PKEYBOARD = wlr_seat_get_keyboard(g_pCompositor->m_sSeat.seat);
2022-03-20 18:23:16 +01:00
2022-06-27 13:42:20 +02:00
if (!PKEYBOARD) { // ???
Debug::log(ERR, "No active keyboard in processMouseDownNormal??");
2022-06-23 21:46:36 +02:00
return;
}
2022-07-26 14:50:21 +02:00
// notify the keybind manager
static auto *const PPASSMOUSE = &g_pConfigManager->getConfigValuePtr("binds:pass_mouse_when_bound")->intValue;
const auto PASS = g_pKeybindManager->onMouseEvent(e);
if (!PASS && !*PPASSMOUSE)
return;
2022-03-18 20:42:49 +01:00
switch (e->state) {
case WLR_BUTTON_PRESSED:
if (!g_pCompositor->m_sSeat.mouse->currentConstraint)
refocus();
2022-04-02 13:02:16 +02:00
2022-04-04 16:28:43 +02:00
// if clicked on a floating window make it top
if (g_pCompositor->m_pLastWindow && g_pCompositor->m_pLastWindow->m_bIsFloating)
2022-04-04 16:28:43 +02:00
g_pCompositor->moveWindowToTop(g_pCompositor->m_pLastWindow);
2022-03-18 20:42:49 +01:00
break;
case WLR_BUTTON_RELEASED:
break;
}
// notify app if we didnt handle it
2022-03-22 21:59:14 +01:00
if (g_pCompositor->doesSeatAcceptInput(g_pCompositor->m_pLastFocus)) {
2022-03-22 18:29:13 +01:00
wlr_seat_pointer_notify_button(g_pCompositor->m_sSeat.seat, e->time_msec, e->button, e->state);
2022-03-22 21:59:14 +01:00
}
2022-06-27 13:42:20 +02:00
}
void CInputManager::processMouseDownKill(wlr_pointer_button_event* e) {
switch (e->state) {
case WLR_BUTTON_PRESSED: {
const auto PWINDOW = g_pCompositor->m_pLastWindow;
if (!PWINDOW) {
2022-06-27 13:42:20 +02:00
Debug::log(ERR, "Cannot kill invalid window!");
break;
}
// kill the mf
kill(PWINDOW->getPID(), SIGKILL);
break;
}
case WLR_BUTTON_RELEASED:
break;
default:
break;
}
// reset click behavior mode
m_ecbClickBehavior = CLICKMODE_DEFAULT;
2022-03-17 16:56:33 +01:00
}
void CInputManager::onMouseWheel(wlr_pointer_axis_event* e) {
2022-10-06 21:20:10 +02:00
static auto *const PSCROLLFACTOR = &g_pConfigManager->getConfigValuePtr("input:touchpad:scroll_factor")->floatValue;
auto factor = (*PSCROLLFACTOR <= 0.f || e->source != WLR_AXIS_SOURCE_FINGER ? 1.f : *PSCROLLFACTOR);
2022-07-21 18:18:03 +02:00
bool passEvent = g_pKeybindManager->onAxisEvent(e);
2022-07-21 18:18:03 +02:00
wlr_idle_notify_activity(g_pCompositor->m_sWLRIdle, g_pCompositor->m_sSeat.seat);
2022-07-21 18:18:03 +02:00
if (passEvent) {
2022-10-06 21:20:10 +02:00
wlr_seat_pointer_notify_axis(g_pCompositor->m_sSeat.seat, e->time_msec, e->orientation, factor * e->delta,
std::round(factor * e->delta_discrete), e->source);
2022-07-15 20:54:05 +02:00
}
}
2022-03-17 19:03:15 +01:00
Vector2D CInputManager::getMouseCoordsInternal() {
2022-03-19 20:30:21 +01:00
return Vector2D(g_pCompositor->m_sWLRCursor->x, g_pCompositor->m_sWLRCursor->y);
2022-03-17 20:55:04 +01:00
}
void CInputManager::newKeyboard(wlr_input_device* keyboard) {
2022-06-28 15:40:14 +02:00
const auto PNEWKEYBOARD = &m_lKeyboards.emplace_back();
2022-03-17 20:55:04 +01:00
PNEWKEYBOARD->keyboard = keyboard;
2022-06-30 21:26:00 +02:00
try {
PNEWKEYBOARD->name = std::string(keyboard->name);
} catch (std::exception& e) {
Debug::log(ERR, "Keyboard had no name???"); // logic error
}
2022-03-17 20:55:04 +01:00
2022-07-11 23:09:35 +02:00
PNEWKEYBOARD->hyprListener_keyboardMod.initCallback(&wlr_keyboard_from_input_device(keyboard)->events.modifiers, &Events::listener_keyboardMod, PNEWKEYBOARD, "Keyboard");
PNEWKEYBOARD->hyprListener_keyboardKey.initCallback(&wlr_keyboard_from_input_device(keyboard)->events.key, &Events::listener_keyboardKey, PNEWKEYBOARD, "Keyboard");
2022-03-28 22:31:39 +02:00
PNEWKEYBOARD->hyprListener_keyboardDestroy.initCallback(&keyboard->events.destroy, &Events::listener_keyboardDestroy, PNEWKEYBOARD, "Keyboard");
2022-03-17 20:55:04 +01:00
2022-08-18 17:17:33 +02:00
PNEWKEYBOARD->hyprListener_keyboardKeymap.initCallback(&wlr_keyboard_from_input_device(keyboard)->events.keymap, [&](void* owner, void* data) {
const auto PKEYBOARD = (SKeyboard*)owner;
g_pEventManager->postEvent(SHyprIPCEvent{"activelayout", PKEYBOARD->name + "," +getActiveLayoutForKeyboard(PKEYBOARD)}, true); // force as this should ALWAYS be sent
2022-09-25 20:07:48 +02:00
2022-08-18 17:17:33 +02:00
}, PNEWKEYBOARD, "Keyboard");
disableAllKeyboards(false);
2022-09-25 20:07:48 +02:00
m_pActiveKeyboard = PNEWKEYBOARD;
2022-08-18 17:17:33 +02:00
PNEWKEYBOARD->active = true;
2022-06-25 11:50:09 +02:00
applyConfigToKeyboard(PNEWKEYBOARD);
2022-06-03 19:15:39 +02:00
2022-07-11 23:09:35 +02:00
wlr_seat_set_keyboard(g_pCompositor->m_sSeat.seat, wlr_keyboard_from_input_device(keyboard));
2022-03-19 11:27:19 +01:00
Debug::log(LOG, "New keyboard created, pointers Hypr: %x and WLR: %x", PNEWKEYBOARD, keyboard);
2022-03-24 21:05:34 +01:00
}
2022-08-05 16:21:08 +02:00
void CInputManager::newVirtualKeyboard(wlr_input_device* keyboard) {
const auto PNEWKEYBOARD = &m_lKeyboards.emplace_back();
PNEWKEYBOARD->keyboard = keyboard;
PNEWKEYBOARD->isVirtual = true;
try {
PNEWKEYBOARD->name = std::string(keyboard->name);
} catch (std::exception& e) {
Debug::log(ERR, "Keyboard had no name???"); // logic error
}
PNEWKEYBOARD->hyprListener_keyboardMod.initCallback(&wlr_keyboard_from_input_device(keyboard)->events.modifiers, &Events::listener_keyboardMod, PNEWKEYBOARD, "Keyboard");
PNEWKEYBOARD->hyprListener_keyboardKey.initCallback(&wlr_keyboard_from_input_device(keyboard)->events.key, &Events::listener_keyboardKey, PNEWKEYBOARD, "Keyboard");
PNEWKEYBOARD->hyprListener_keyboardDestroy.initCallback(&keyboard->events.destroy, &Events::listener_keyboardDestroy, PNEWKEYBOARD, "Keyboard");
PNEWKEYBOARD->hyprListener_keyboardKeymap.initCallback(&wlr_keyboard_from_input_device(keyboard)->events.keymap, [&](void* owner, void* data) {
const auto PKEYBOARD = (SKeyboard*)owner;
g_pEventManager->postEvent(SHyprIPCEvent{"activelayout", PKEYBOARD->name + "," +getActiveLayoutForKeyboard(PKEYBOARD)}, true); // force as this should ALWAYS be sent
2022-09-25 20:07:48 +02:00
}, PNEWKEYBOARD, "Keyboard");
disableAllKeyboards(true);
2022-08-05 16:21:08 +02:00
m_pActiveKeyboard = PNEWKEYBOARD;
2022-08-18 17:17:33 +02:00
PNEWKEYBOARD->active = true;
2022-08-05 16:21:08 +02:00
applyConfigToKeyboard(PNEWKEYBOARD);
wlr_seat_set_keyboard(g_pCompositor->m_sSeat.seat, wlr_keyboard_from_input_device(keyboard));
Debug::log(LOG, "New virtual keyboard created, pointers Hypr: %x and WLR: %x", PNEWKEYBOARD, keyboard);
}
2022-03-24 21:05:34 +01:00
void CInputManager::setKeyboardLayout() {
for (auto& k : m_lKeyboards)
applyConfigToKeyboard(&k);
g_pKeybindManager->updateXKBTranslationState();
}
void CInputManager::applyConfigToKeyboard(SKeyboard* pKeyboard) {
2022-07-28 21:38:30 +02:00
auto devname = pKeyboard->name;
transform(devname.begin(), devname.end(), devname.begin(), ::tolower);
const auto HASCONFIG = g_pConfigManager->deviceConfigExists(devname);
Debug::log(LOG, "ApplyConfigToKeyboard for \"%s\", hasconfig: %i", pKeyboard->name.c_str(), (int)HASCONFIG);
2022-03-24 21:05:34 +01:00
ASSERT(pKeyboard);
2022-03-24 21:05:34 +01:00
2022-07-11 23:09:35 +02:00
if (!wlr_keyboard_from_input_device(pKeyboard->keyboard))
2022-07-03 22:54:47 +02:00
return;
2022-07-28 21:38:30 +02:00
const auto REPEATRATE = HASCONFIG ? g_pConfigManager->getDeviceInt(devname, "repeat_rate") : g_pConfigManager->getInt("input:repeat_rate");
const auto REPEATDELAY = HASCONFIG ? g_pConfigManager->getDeviceInt(devname, "repeat_delay") : g_pConfigManager->getInt("input:repeat_delay");
2022-03-24 21:05:34 +01:00
2022-07-28 21:38:30 +02:00
const auto NUMLOCKON = HASCONFIG ? g_pConfigManager->getDeviceInt(devname, "numlock_by_default") : g_pConfigManager->getInt("input:numlock_by_default");
2022-06-30 21:26:00 +02:00
2022-08-19 20:01:51 +02:00
const auto FILEPATH = HASCONFIG ? g_pConfigManager->getDeviceString(devname, "kb_file") : g_pConfigManager->getString("input:kb_file");
2022-07-28 21:38:30 +02:00
const auto RULES = HASCONFIG ? g_pConfigManager->getDeviceString(devname, "kb_rules") : g_pConfigManager->getString("input:kb_rules");
const auto MODEL = HASCONFIG ? g_pConfigManager->getDeviceString(devname, "kb_model") : g_pConfigManager->getString("input:kb_model");
const auto LAYOUT = HASCONFIG ? g_pConfigManager->getDeviceString(devname, "kb_layout") : g_pConfigManager->getString("input:kb_layout");
const auto VARIANT = HASCONFIG ? g_pConfigManager->getDeviceString(devname, "kb_variant") : g_pConfigManager->getString("input:kb_variant");
const auto OPTIONS = HASCONFIG ? g_pConfigManager->getDeviceString(devname, "kb_options") : g_pConfigManager->getString("input:kb_options");
2022-03-24 21:05:34 +01:00
2022-06-28 15:40:14 +02:00
try {
2022-08-19 20:01:51 +02:00
if (NUMLOCKON == pKeyboard->numlockOn && REPEATDELAY == pKeyboard->repeatDelay && REPEATRATE == pKeyboard->repeatRate && RULES != "" && RULES == pKeyboard->currentRules.rules && MODEL == pKeyboard->currentRules.model && LAYOUT == pKeyboard->currentRules.layout && VARIANT == pKeyboard->currentRules.variant && OPTIONS == pKeyboard->currentRules.options && FILEPATH == pKeyboard->xkbFilePath) {
2022-06-28 15:40:14 +02:00
Debug::log(LOG, "Not applying config to keyboard, it did not change.");
return;
}
} catch (std::exception& e) {
// can be libc errors for null std::string
// we can ignore those and just apply
2022-06-25 11:50:09 +02:00
}
2022-07-11 23:09:35 +02:00
wlr_keyboard_set_repeat_info(wlr_keyboard_from_input_device(pKeyboard->keyboard), std::max(0, REPEATRATE), std::max(0, REPEATDELAY));
2022-06-30 21:26:00 +02:00
pKeyboard->repeatDelay = REPEATDELAY;
pKeyboard->repeatRate = REPEATRATE;
pKeyboard->numlockOn = NUMLOCKON;
2022-08-19 20:01:51 +02:00
pKeyboard->xkbFilePath = FILEPATH.c_str();
2022-06-30 21:26:00 +02:00
2022-03-24 21:05:34 +01:00
xkb_rule_names rules = {
.rules = RULES.c_str(),
.model = MODEL.c_str(),
.layout = LAYOUT.c_str(),
.variant = VARIANT.c_str(),
.options = OPTIONS.c_str()};
2022-03-24 21:05:34 +01:00
pKeyboard->currentRules.rules = RULES;
pKeyboard->currentRules.model = MODEL;
pKeyboard->currentRules.variant = VARIANT;
pKeyboard->currentRules.options = OPTIONS;
pKeyboard->currentRules.layout = LAYOUT;
2022-06-25 11:50:09 +02:00
2022-03-24 21:05:34 +01:00
const auto CONTEXT = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
if (!CONTEXT) {
Debug::log(ERR, "applyConfigToKeyboard: CONTEXT null??");
2022-03-24 21:05:34 +01:00
return;
}
2022-06-25 11:50:09 +02:00
Debug::log(LOG, "Attempting to create a keymap for layout %s with variant %s (rules: %s, model: %s, options: %s)", rules.layout, rules.variant, rules.rules, rules.model, rules.options);
2022-08-19 20:01:51 +02:00
xkb_keymap * KEYMAP = NULL;
if (!FILEPATH.empty()) {
2022-08-19 22:18:09 +02:00
auto path = absolutePath(FILEPATH, g_pConfigManager->configCurrentPath);
2022-08-19 20:01:51 +02:00
if (!std::filesystem::exists(path)) {
Debug::log(ERR, "input:kb_file= file doesnt exist");
} else {
KEYMAP = xkb_keymap_new_from_file(CONTEXT, fopen(path.c_str(), "r"), XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
}
2022-09-25 20:07:48 +02:00
}
2022-08-19 20:01:51 +02:00
if (!KEYMAP) {
KEYMAP = xkb_keymap_new_from_names(CONTEXT, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS);
}
2022-06-02 20:31:47 +02:00
2022-03-24 21:05:34 +01:00
if (!KEYMAP) {
g_pConfigManager->addParseError("Invalid keyboard layout passed. ( rules: " + RULES + ", model: " + MODEL + ", variant: " + VARIANT + ", options: " + OPTIONS + ", layout: " + LAYOUT + " )");
2022-03-24 21:05:34 +01:00
Debug::log(ERR, "Keyboard layout %s with variant %s (rules: %s, model: %s, options: %s) couldn't have been loaded.", rules.layout, rules.variant, rules.rules, rules.model, rules.options);
2022-06-25 11:50:09 +02:00
memset(&rules, 0, sizeof(rules));
pKeyboard->currentRules.rules = "";
pKeyboard->currentRules.model = "";
pKeyboard->currentRules.variant = "";
pKeyboard->currentRules.options = "";
pKeyboard->currentRules.layout = "";
2022-06-25 11:50:09 +02:00
KEYMAP = xkb_keymap_new_from_names(CONTEXT, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS);
}
2022-06-02 19:47:11 +02:00
2022-07-11 23:09:35 +02:00
wlr_keyboard_set_keymap(wlr_keyboard_from_input_device(pKeyboard->keyboard), KEYMAP);
2022-06-02 19:47:11 +02:00
wlr_keyboard_modifiers wlrMods = {0};
2022-06-02 19:47:11 +02:00
2022-06-30 21:26:00 +02:00
if (NUMLOCKON == 1) {
// lock numlock
const auto IDX = xkb_map_mod_get_index(KEYMAP, XKB_MOD_NAME_NUM);
if (IDX != XKB_MOD_INVALID)
wlrMods.locked |= (uint32_t)1 << IDX;
}
if (wlrMods.locked != 0) {
2022-07-11 23:09:35 +02:00
wlr_keyboard_notify_modifiers(wlr_keyboard_from_input_device(pKeyboard->keyboard), 0, 0, wlrMods.locked, 0);
2022-06-02 19:47:11 +02:00
}
2022-03-24 21:05:34 +01:00
xkb_keymap_unref(KEYMAP);
xkb_context_unref(CONTEXT);
g_pEventManager->postEvent(SHyprIPCEvent{"activelayout", pKeyboard->name + "," +getActiveLayoutForKeyboard(pKeyboard)}, true); // force as this should ALWAYS be sent
2022-08-18 17:17:33 +02:00
Debug::log(LOG, "Set the keyboard layout to %s and variant to %s for keyboard \"%s\"", rules.layout, rules.variant, pKeyboard->keyboard->name);
2022-09-25 20:07:48 +02:00
}
2022-03-17 20:55:04 +01:00
2022-07-11 20:23:16 +02:00
void CInputManager::newMouse(wlr_input_device* mouse, bool virt) {
m_lMice.emplace_back();
const auto PMOUSE = &m_lMice.back();
PMOUSE->mouse = mouse;
2022-07-11 20:23:16 +02:00
PMOUSE->virt = virt;
2022-06-30 21:26:00 +02:00
try {
PMOUSE->name = std::string(mouse->name);
} catch(std::exception& e) {
Debug::log(ERR, "Mouse had no name???"); // logic error
}
2022-03-18 22:53:27 +01:00
if (wlr_input_device_is_libinput(mouse)) {
const auto LIBINPUTDEV = (libinput_device*)wlr_libinput_get_device_handle(mouse);
Debug::log(LOG, "New mouse has libinput sens %.2f (%.2f) with accel profile %i (%i)", libinput_device_config_accel_get_speed(LIBINPUTDEV), libinput_device_config_accel_get_default_speed(LIBINPUTDEV), libinput_device_config_accel_get_profile(LIBINPUTDEV), libinput_device_config_accel_get_default_profile(LIBINPUTDEV));
2022-03-18 22:53:27 +01:00
}
2022-10-05 22:21:22 +02:00
setPointerConfigs();
2022-05-12 12:13:02 +02:00
PMOUSE->hyprListener_destroyMouse.initCallback(&mouse->events.destroy, &Events::listener_destroyMouse, PMOUSE, "Mouse");
2022-03-18 22:53:27 +01:00
wlr_cursor_attach_input_device(g_pCompositor->m_sWLRCursor, mouse);
2022-03-19 11:27:19 +01:00
2022-10-27 13:58:10 +02:00
PMOUSE->connected = true;
g_pCompositor->m_sSeat.mouse = PMOUSE;
2022-06-24 23:27:02 +02:00
m_tmrLastCursorMovement.reset();
2022-03-19 11:27:19 +01:00
Debug::log(LOG, "New mouse created, pointer WLR: %x", mouse);
2022-03-18 22:53:27 +01:00
}
2022-10-05 22:21:22 +02:00
void CInputManager::setPointerConfigs() {
for (auto& m : m_lMice) {
2022-10-05 22:21:22 +02:00
const auto PPOINTER = &m;
2022-10-05 22:21:22 +02:00
auto devname = PPOINTER->name;
2022-07-28 21:38:30 +02:00
transform(devname.begin(), devname.end(), devname.begin(), ::tolower);
const auto HASCONFIG = g_pConfigManager->deviceConfigExists(devname);
2022-10-27 13:58:10 +02:00
if (HASCONFIG) {
const auto ENABLED = g_pConfigManager->getDeviceInt(devname, "enabled");
if (ENABLED && !m.connected) {
wlr_cursor_attach_input_device(g_pCompositor->m_sWLRCursor, m.mouse);
m.connected = true;
} else if (!ENABLED && m.connected) {
wlr_cursor_detach_input_device(g_pCompositor->m_sWLRCursor, m.mouse);
m.connected = false;
}
}
if (wlr_input_device_is_libinput(m.mouse)) {
const auto LIBINPUTDEV = (libinput_device*)wlr_libinput_get_device_handle(m.mouse);
2022-07-28 21:38:30 +02:00
if ((HASCONFIG ? g_pConfigManager->getDeviceInt(devname, "clickfinger_behavior") : g_pConfigManager->getInt("input:touchpad:clickfinger_behavior")) == 0) // toggle software buttons or clickfinger
libinput_device_config_click_set_method(LIBINPUTDEV, LIBINPUT_CONFIG_CLICK_METHOD_BUTTON_AREAS);
else
libinput_device_config_click_set_method(LIBINPUTDEV, LIBINPUT_CONFIG_CLICK_METHOD_CLICKFINGER);
if ((HASCONFIG ? g_pConfigManager->getDeviceInt(devname, "left_handed") : g_pConfigManager->getInt("input:left_handed")) == 0)
2022-10-04 22:46:41 +02:00
libinput_device_config_left_handed_set(LIBINPUTDEV, 0);
else
libinput_device_config_left_handed_set(LIBINPUTDEV, 1);
if (libinput_device_config_middle_emulation_is_available(LIBINPUTDEV)) { // middleclick on r+l mouse button pressed
2022-07-28 21:38:30 +02:00
if ((HASCONFIG ? g_pConfigManager->getDeviceInt(devname, "middle_button_emulation") : g_pConfigManager->getInt("input:touchpad:middle_button_emulation")) == 1)
libinput_device_config_middle_emulation_set_enabled(LIBINPUTDEV, LIBINPUT_CONFIG_MIDDLE_EMULATION_ENABLED);
else
libinput_device_config_middle_emulation_set_enabled(LIBINPUTDEV, LIBINPUT_CONFIG_MIDDLE_EMULATION_DISABLED);
}
2022-10-05 22:21:22 +02:00
const auto SCROLLMETHOD = HASCONFIG ? g_pConfigManager->getDeviceString(devname, "scroll_method") : g_pConfigManager->getString("input:scroll_method");
if (SCROLLMETHOD == "" || SCROLLMETHOD == STRVAL_EMPTY) {
libinput_device_config_scroll_set_method(LIBINPUTDEV, libinput_device_config_scroll_get_default_method(LIBINPUTDEV));
} else if (SCROLLMETHOD == "no_scroll") {
libinput_device_config_scroll_set_method(LIBINPUTDEV, LIBINPUT_CONFIG_SCROLL_NO_SCROLL);
} else if (SCROLLMETHOD == "2fg") {
libinput_device_config_scroll_set_method(LIBINPUTDEV, LIBINPUT_CONFIG_SCROLL_2FG);
} else if (SCROLLMETHOD == "edge") {
libinput_device_config_scroll_set_method(LIBINPUTDEV, LIBINPUT_CONFIG_SCROLL_EDGE);
} else if (SCROLLMETHOD == "on_button_down") {
libinput_device_config_scroll_set_method(LIBINPUTDEV, LIBINPUT_CONFIG_SCROLL_ON_BUTTON_DOWN);
} else {
Debug::log(WARN, "Scroll method unknown");
}
2022-07-28 21:38:30 +02:00
if ((HASCONFIG ? g_pConfigManager->getDeviceInt(devname, "drag_lock") : g_pConfigManager->getInt("input:touchpad:drag_lock")) == 0)
libinput_device_config_tap_set_drag_lock_enabled(LIBINPUTDEV, LIBINPUT_CONFIG_DRAG_LOCK_DISABLED);
else
libinput_device_config_tap_set_drag_lock_enabled(LIBINPUTDEV, LIBINPUT_CONFIG_DRAG_LOCK_ENABLED);
if (libinput_device_config_tap_get_finger_count(LIBINPUTDEV)) // this is for tapping (like on a laptop)
2022-07-28 21:38:30 +02:00
if ((HASCONFIG ? g_pConfigManager->getDeviceInt(devname, "tap-to-click") : g_pConfigManager->getInt("input:touchpad:tap-to-click")) == 1)
libinput_device_config_tap_set_enabled(LIBINPUTDEV, LIBINPUT_CONFIG_TAP_ENABLED);
if (libinput_device_config_scroll_has_natural_scroll(LIBINPUTDEV)) {
double w = 0, h = 0;
if (libinput_device_has_capability(LIBINPUTDEV, LIBINPUT_DEVICE_CAP_POINTER) && libinput_device_get_size(LIBINPUTDEV, &w, &h) == 0) // pointer with size is a touchpad
2022-07-28 21:38:30 +02:00
libinput_device_config_scroll_set_natural_scroll_enabled(LIBINPUTDEV, (HASCONFIG ? g_pConfigManager->getDeviceInt(devname, "natural_scroll") : g_pConfigManager->getInt("input:touchpad:natural_scroll")));
else
2022-07-28 21:38:30 +02:00
libinput_device_config_scroll_set_natural_scroll_enabled(LIBINPUTDEV, (HASCONFIG ? g_pConfigManager->getDeviceInt(devname, "natural_scroll") : g_pConfigManager->getInt("input:natural_scroll")));
}
if (libinput_device_config_dwt_is_available(LIBINPUTDEV)) {
2022-07-28 21:38:30 +02:00
const auto DWT = static_cast<enum libinput_config_dwt_state>((HASCONFIG ? g_pConfigManager->getDeviceInt(devname, "disable_while_typing") : g_pConfigManager->getInt("input:touchpad:disable_while_typing")) != 0);
libinput_device_config_dwt_set_enabled(LIBINPUTDEV, DWT);
}
2022-07-28 21:38:30 +02:00
const auto LIBINPUTSENS = std::clamp((HASCONFIG ? g_pConfigManager->getDeviceFloat(devname, "sensitivity") : g_pConfigManager->getFloat("input:sensitivity")), -1.f, 1.f);
libinput_device_config_accel_set_speed(LIBINPUTDEV, LIBINPUTSENS);
2022-10-05 22:21:22 +02:00
const auto ACCELPROFILE = HASCONFIG ? g_pConfigManager->getDeviceString(devname, "accel_profile") : g_pConfigManager->getString("input:accel_profile");
if (ACCELPROFILE == "" || ACCELPROFILE == STRVAL_EMPTY) {
libinput_device_config_accel_set_profile(LIBINPUTDEV, libinput_device_config_accel_get_default_profile(LIBINPUTDEV));
} else if (ACCELPROFILE == "adaptive") {
libinput_device_config_accel_set_profile(LIBINPUTDEV, LIBINPUT_CONFIG_ACCEL_PROFILE_ADAPTIVE);
} else if (ACCELPROFILE == "flat") {
libinput_device_config_accel_set_profile(LIBINPUTDEV, LIBINPUT_CONFIG_ACCEL_PROFILE_FLAT);
} else {
Debug::log(WARN, "Unknown acceleration profile, falling back to default");
}
Debug::log(LOG, "Applied config to mouse %s, sens %.2f", m.name.c_str(), LIBINPUTSENS);
}
}
}
2022-03-18 23:25:26 +01:00
void CInputManager::destroyKeyboard(SKeyboard* pKeyboard) {
2022-03-28 22:31:39 +02:00
pKeyboard->hyprListener_keyboardDestroy.removeCallback();
pKeyboard->hyprListener_keyboardMod.removeCallback();
pKeyboard->hyprListener_keyboardKey.removeCallback();
2022-03-18 23:25:26 +01:00
if (pKeyboard->active) {
m_lKeyboards.remove(*pKeyboard);
if (m_lKeyboards.size() > 0) {
m_pActiveKeyboard = &m_lKeyboards.back();
m_pActiveKeyboard->active = true;
} else {
m_pActiveKeyboard = nullptr;
}
}
2022-03-18 23:25:26 +01:00
m_lKeyboards.remove(*pKeyboard);
}
void CInputManager::destroyMouse(wlr_input_device* mouse) {
for (auto& m : m_lMice) {
if (m.mouse == mouse) {
m_lMice.remove(m);
2022-05-12 12:13:02 +02:00
break;
}
}
g_pCompositor->m_sSeat.mouse = m_lMice.size() > 0 ? &m_lMice.front() : nullptr;
2022-05-12 12:13:02 +02:00
if (g_pCompositor->m_sSeat.mouse)
2022-08-09 20:36:21 +02:00
unconstrainMouse();
2022-03-18 23:25:26 +01:00
}
2022-03-24 15:57:46 +01:00
void CInputManager::onKeyboardKey(wlr_keyboard_key_event* e, SKeyboard* pKeyboard) {
2022-07-20 22:45:06 +02:00
bool passEvent = g_pKeybindManager->onKeyEvent(e, pKeyboard);
2022-03-18 23:06:45 +01:00
2022-03-22 18:29:13 +01:00
wlr_idle_notify_activity(g_pCompositor->m_sWLRIdle, g_pCompositor->m_sSeat.seat);
2022-03-17 20:55:04 +01:00
2022-07-20 22:45:06 +02:00
if (passEvent) {
2022-08-05 16:21:08 +02:00
const auto PIMEGRAB = m_sIMERelay.getIMEKeyboardGrab(pKeyboard);
if (PIMEGRAB && PIMEGRAB->pWlrKbGrab && PIMEGRAB->pWlrKbGrab->input_method) {
wlr_input_method_keyboard_grab_v2_set_keyboard(PIMEGRAB->pWlrKbGrab, wlr_keyboard_from_input_device(pKeyboard->keyboard));
wlr_input_method_keyboard_grab_v2_send_key(PIMEGRAB->pWlrKbGrab, e->time_msec, e->keycode, e->state);
} else {
wlr_seat_set_keyboard(g_pCompositor->m_sSeat.seat, wlr_keyboard_from_input_device(pKeyboard->keyboard));
wlr_seat_keyboard_notify_key(g_pCompositor->m_sSeat.seat, e->time_msec, e->keycode, e->state);
}
2022-03-19 22:03:40 +01:00
}
2022-03-18 23:06:45 +01:00
}
2022-03-17 20:55:04 +01:00
2022-03-18 23:06:45 +01:00
void CInputManager::onKeyboardMod(void* data, SKeyboard* pKeyboard) {
2022-08-05 16:21:08 +02:00
const auto PIMEGRAB = m_sIMERelay.getIMEKeyboardGrab(pKeyboard);
2022-09-04 18:46:28 +02:00
const auto ALLMODS = accumulateModsFromAllKBs();
auto MODS = wlr_keyboard_from_input_device(pKeyboard->keyboard)->modifiers;
MODS.depressed = ALLMODS;
2022-08-05 16:21:08 +02:00
if (PIMEGRAB && PIMEGRAB->pWlrKbGrab && PIMEGRAB->pWlrKbGrab->input_method) {
wlr_input_method_keyboard_grab_v2_set_keyboard(PIMEGRAB->pWlrKbGrab, wlr_keyboard_from_input_device(pKeyboard->keyboard));
2022-09-04 18:46:28 +02:00
wlr_input_method_keyboard_grab_v2_send_modifiers(PIMEGRAB->pWlrKbGrab, &MODS);
2022-08-05 16:21:08 +02:00
} else {
wlr_seat_set_keyboard(g_pCompositor->m_sSeat.seat, wlr_keyboard_from_input_device(pKeyboard->keyboard));
2022-09-04 18:46:28 +02:00
wlr_seat_keyboard_notify_modifiers(g_pCompositor->m_sSeat.seat, &MODS);
2022-08-05 16:21:08 +02:00
}
2022-08-18 17:17:33 +02:00
const auto PWLRKB = wlr_keyboard_from_input_device(pKeyboard->keyboard);
if (PWLRKB->modifiers.group != pKeyboard->activeLayout) {
pKeyboard->activeLayout = PWLRKB->modifiers.group;
g_pEventManager->postEvent(SHyprIPCEvent{"activelayout", pKeyboard->name + "," + getActiveLayoutForKeyboard(pKeyboard)}, true); // force as this should ALWAYS be sent
2022-08-18 17:17:33 +02:00
}
2022-03-21 19:28:43 +01:00
}
void CInputManager::refocus() {
2022-04-13 20:19:40 +02:00
mouseMoveUnified(0, true);
2022-03-31 17:25:23 +02:00
}
void CInputManager::updateDragIcon() {
2022-08-06 22:26:32 +02:00
if (!m_sDrag.dragIcon)
2022-03-31 17:25:23 +02:00
return;
2022-08-06 22:26:32 +02:00
switch (m_sDrag.dragIcon->drag->grab_type) {
2022-03-31 17:25:23 +02:00
case WLR_DRAG_GRAB_KEYBOARD:
break;
2022-06-29 14:15:08 +02:00
case WLR_DRAG_GRAB_KEYBOARD_POINTER: {
2022-08-06 22:26:32 +02:00
wlr_box box = {m_sDrag.pos.x - 2, m_sDrag.pos.y - 2, m_sDrag.dragIcon->surface->current.width + 4, m_sDrag.dragIcon->surface->current.height + 4};
2022-06-29 14:15:08 +02:00
g_pHyprRenderer->damageBox(&box);
2022-08-06 22:26:32 +02:00
m_sDrag.pos = getMouseCoordsInternal();
2022-03-31 17:25:23 +02:00
break;
2022-06-29 14:15:08 +02:00
}
2022-03-31 17:25:23 +02:00
default:
break;
}
}
void CInputManager::recheckConstraint(SMouse* pMouse) {
if (!pMouse->currentConstraint)
return;
const auto PREGION = &pMouse->currentConstraint->region;
if (pMouse->currentConstraint->type == WLR_POINTER_CONSTRAINT_V1_CONFINED) {
pixman_region32_copy(&pMouse->confinedTo, PREGION);
} else {
pixman_region32_clear(&pMouse->confinedTo);
}
2022-04-25 13:40:46 +02:00
const auto PWINDOW = g_pCompositor->getConstraintWindow(g_pCompositor->m_sSeat.mouse);
const auto PWINDOWNAME = PWINDOW ? PWINDOW->m_szTitle : "";
Debug::log(LOG, "Constraint rechecked: %i, %i to %i, %i for %x (window name: %s)", PREGION->extents.x1, PREGION->extents.y1, PREGION->extents.x2, PREGION->extents.y2, pMouse->currentConstraint->surface, PWINDOWNAME.c_str());
}
void CInputManager::constrainMouse(SMouse* pMouse, wlr_pointer_constraint_v1* constraint) {
if (pMouse->currentConstraint == constraint)
return;
const auto PWINDOW = g_pCompositor->getWindowFromSurface(constraint->surface);
const auto MOUSECOORDS = getMouseCoordsInternal();
pMouse->hyprListener_commitConstraint.removeCallback();
if (pMouse->currentConstraint) {
if (!constraint) {
// warpe to hint
if (constraint->current.committed & WLR_POINTER_CONSTRAINT_V1_STATE_CURSOR_HINT) {
if (PWINDOW) {
2022-04-25 13:40:46 +02:00
if (PWINDOW->m_bIsX11) {
wlr_cursor_warp(g_pCompositor->m_sWLRCursor, nullptr,
constraint->current.cursor_hint.x + PWINDOW->m_uSurface.xwayland->x, PWINDOW->m_uSurface.xwayland->y + PWINDOW->m_vRealPosition.vec().y);
wlr_seat_pointer_warp(constraint->seat, constraint->current.cursor_hint.x, constraint->current.cursor_hint.y);
} else {
wlr_cursor_warp(g_pCompositor->m_sWLRCursor, nullptr,
constraint->current.cursor_hint.x + PWINDOW->m_vRealPosition.vec().x, constraint->current.cursor_hint.y + PWINDOW->m_vRealPosition.vec().y);
2022-04-25 13:40:46 +02:00
wlr_seat_pointer_warp(constraint->seat, constraint->current.cursor_hint.x, constraint->current.cursor_hint.y);
}
}
}
}
2022-09-25 20:07:48 +02:00
wlr_pointer_constraint_v1_send_deactivated(pMouse->currentConstraint);
}
pMouse->currentConstraint = constraint;
pMouse->constraintActive = true;
if (pixman_region32_not_empty(&constraint->current.region)) {
pixman_region32_intersect(&constraint->region, &constraint->surface->input_region, &constraint->current.region);
} else {
pixman_region32_copy(&constraint->region, &constraint->surface->input_region);
}
// warp to the constraint
recheckConstraint(pMouse);
wlr_pointer_constraint_v1_send_activated(pMouse->currentConstraint);
pMouse->hyprListener_commitConstraint.initCallback(&pMouse->currentConstraint->surface->events.commit, &Events::listener_commitConstraint, pMouse, "Mouse constraint commit");
Debug::log(LOG, "Constrained mouse to %x", pMouse->currentConstraint);
}
2022-08-09 20:36:21 +02:00
void CInputManager::unconstrainMouse() {
if (!g_pCompositor->m_sSeat.mouse->currentConstraint)
return;
const auto CONSTRAINTWINDOW = g_pCompositor->getConstraintWindow(g_pCompositor->m_sSeat.mouse);
if (CONSTRAINTWINDOW) {
g_pXWaylandManager->activateSurface(g_pXWaylandManager->getWindowSurface(CONSTRAINTWINDOW), false);
2022-08-09 20:36:21 +02:00
}
wlr_pointer_constraint_v1_send_deactivated(g_pCompositor->m_sSeat.mouse->currentConstraint);
g_pCompositor->m_sSeat.mouse->constraintActive = false;
// TODO: its better to somehow detect the workspace...
g_pCompositor->m_sSeat.mouse->currentConstraint = nullptr;
g_pCompositor->m_sSeat.mouse->hyprListener_commitConstraint.removeCallback();
}
void Events::listener_commitConstraint(void* owner, void* data) {
//g_pInputManager->recheckConstraint((SMouse*)owner);
}
2022-06-09 12:46:55 +02:00
void CInputManager::updateCapabilities(wlr_input_device* pDev) {
// TODO: this is dumb
switch (pDev->type) {
case WLR_INPUT_DEVICE_KEYBOARD:
m_uiCapabilities |= WL_SEAT_CAPABILITY_KEYBOARD;
break;
case WLR_INPUT_DEVICE_POINTER:
m_uiCapabilities |= WL_SEAT_CAPABILITY_POINTER;
break;
case WLR_INPUT_DEVICE_TOUCH:
m_uiCapabilities |= WL_SEAT_CAPABILITY_TOUCH;
break;
default:
break;
}
wlr_seat_set_capabilities(g_pCompositor->m_sSeat.seat, m_uiCapabilities);
}
uint32_t CInputManager::accumulateModsFromAllKBs() {
uint32_t finalMask = 0;
for (auto& kb : m_lKeyboards) {
2022-09-05 11:19:40 +02:00
if (kb.isVirtual)
continue;
2022-07-11 23:09:35 +02:00
finalMask |= wlr_keyboard_get_modifiers(wlr_keyboard_from_input_device(kb.keyboard));
}
return finalMask;
}
2022-08-16 16:10:20 +02:00
std::string CInputManager::getActiveLayoutForKeyboard(SKeyboard* pKeyboard) {
const auto WLRKB = wlr_keyboard_from_input_device(pKeyboard->keyboard);
const auto KEYMAP = WLRKB->keymap;
const auto STATE = WLRKB->xkb_state;
const auto LAYOUTSNUM = xkb_keymap_num_layouts(KEYMAP);
2022-08-16 16:15:43 +02:00
for (uint32_t i = 0; i < LAYOUTSNUM; ++i) {
2022-08-16 16:10:20 +02:00
if (xkb_state_layout_index_is_active(STATE, i, XKB_STATE_LAYOUT_EFFECTIVE)) {
const auto LAYOUTNAME = xkb_keymap_layout_get_name(KEYMAP, i);
2022-09-25 20:07:48 +02:00
if (LAYOUTNAME)
2022-08-16 16:10:20 +02:00
return std::string(LAYOUTNAME);
return "error";
}
}
return "none";
}
void CInputManager::disableAllKeyboards(bool virt) {
for (auto& k : m_lKeyboards) {
if (k.isVirtual != virt)
continue;
k.active = false;
}
2022-08-19 20:01:51 +02:00
}
void CInputManager::newTouchDevice(wlr_input_device* pDevice) {
const auto PNEWDEV = &m_lTouchDevices.emplace_back();
PNEWDEV->pWlrDevice = pDevice;
try {
PNEWDEV->name = std::string(pDevice->name);
} catch(std::exception& e) {
Debug::log(ERR, "Touch Device had no name???"); // logic error
}
setTouchDeviceConfigs();
wlr_cursor_attach_input_device(g_pCompositor->m_sWLRCursor, pDevice);
Debug::log(LOG, "New touch device added at %x", PNEWDEV);
PNEWDEV->hyprListener_destroy.initCallback(&pDevice->events.destroy, [&](void* owner, void* data) {
destroyTouchDevice((STouchDevice*)data);
}, PNEWDEV, "TouchDevice");
}
void CInputManager::setTouchDeviceConfigs() {
// The third row is always 0 0 1 and is not expected by `libinput_device_config_calibration_set_matrix`
2022-10-10 12:29:03 +02:00
static const float MATRICES[8][6] = {
{ // normal
1, 0, 0,
0, 1, 0
},
{ // rotation 90°
0, -1, 1,
1, 0, 0
},
{ // rotation 180°
-1, 0, 1,
0, -1, 1
},
{ // rotation 270°
0, 1, 0,
-1, 0, 1
},
{ // flipped
-1, 0, 1,
0, 1, 0
},
{ // flipped + rotation 90°
0, 1, 0,
1, 0, 0
},
{ // flipped + rotation 180°
1, 0, 0,
0, -1, 1
},
{ // flipped + rotation 270°
0, -1, 1,
-1, 0, 1
}
};
for (auto& m : m_lTouchDevices) {
const auto PTOUCHDEV = &m;
auto devname = PTOUCHDEV->name;
transform(devname.begin(), devname.end(), devname.begin(), ::tolower);
const auto HASCONFIG = g_pConfigManager->deviceConfigExists(devname);
if (wlr_input_device_is_libinput(m.pWlrDevice)) {
const auto LIBINPUTDEV = (libinput_device*)wlr_libinput_get_device_handle(m.pWlrDevice);
const int ROTATION = std::clamp(HASCONFIG ? g_pConfigManager->getDeviceInt(devname, "touch_transform") : g_pConfigManager->getInt("input:touchdevice:transform"), 0, 7);
libinput_device_config_calibration_set_matrix(LIBINPUTDEV, MATRICES[ROTATION]);
2022-10-14 13:38:44 +02:00
const auto OUTPUT = HASCONFIG ? g_pConfigManager->getDeviceString(devname, "touch_output") : g_pConfigManager->getString("input:touchdevice:output");
if (!OUTPUT.empty() && OUTPUT != STRVAL_EMPTY)
PTOUCHDEV->boundOutput = OUTPUT;
else
PTOUCHDEV->boundOutput = "";
}
}
}
void CInputManager::destroyTouchDevice(STouchDevice* pDevice) {
Debug::log(LOG, "Touch device at %x removed", pDevice);
m_lTouchDevices.remove(*pDevice);
}
2022-10-04 21:07:21 +02:00
void CInputManager::newSwitch(wlr_input_device* pDevice) {
const auto PNEWDEV = &m_lSwitches.emplace_back();
PNEWDEV->pWlrDevice = pDevice;
Debug::log(LOG, "New switch with name \"%s\" added", pDevice->name);
PNEWDEV->hyprListener_destroy.initCallback(&pDevice->events.destroy, [&](void* owner, void* data) {
destroySwitch((SSwitchDevice*)owner);
}, PNEWDEV, "SwitchDevice");
const auto PSWITCH = wlr_switch_from_input_device(pDevice);
PNEWDEV->hyprListener_toggle.initCallback(&PSWITCH->events.toggle, [&](void* owner, void* data) {
const auto PDEVICE = (SSwitchDevice*)owner;
const auto NAME = std::string(PDEVICE->pWlrDevice->name);
Debug::log(LOG, "Switch %s fired, triggering binds.", NAME.c_str());
g_pKeybindManager->onSwitchEvent(NAME);
}, PNEWDEV, "SwitchDevice");
}
void CInputManager::destroySwitch(SSwitchDevice* pDevice) {
m_lSwitches.remove(*pDevice);
}