// SPDX-License-Identifier: GPL-3.0-or-later #pragma once #include #include #include #include #include static void qmlInput(QQuickWindow &window,const QJsonArray &events) { require(events.size()<=32,"Too many input events"); for(const auto &value:events){ require(value.isObject(),"Invalid input event");auto e=value.toObject();auto kind=e.value("kind").toString(); if(kind=="down" || kind=="up" || kind=="move" || kind=="wheel") { double x=e.value("x").toDouble(-1),y=e.value("y").toDouble(-1); require(std::isfinite(x)&&std::isfinite(y)&&x>=0&&x<640&&y>=0&&y<480,"Invalid input coordinates"); QPointF p(x,y); if(kind=="wheel"){ int delta=e.value("delta").toInt();require(std::abs(delta)<=1200,"Invalid wheel input"); QWheelEvent ev(p,p,QPoint(),QPoint(0,delta),Qt::NoButton,Qt::NoModifier,Qt::NoScrollPhase,false);QCoreApplication::sendEvent(&window,&ev); }else{ auto type=kind=="down"?QEvent::MouseButtonPress:kind=="up"?QEvent::MouseButtonRelease:QEvent::MouseMove; QMouseEvent ev(type,p,p,p,kind=="move"?Qt::NoButton:Qt::LeftButton,kind=="up"?Qt::NoButton:Qt::LeftButton,Qt::NoModifier); QCoreApplication::sendEvent(&window,&ev); } }else if(kind=="text"){ auto text=e.value("text").toString();require(text.size()<=1024&&!text.contains(QChar(0)),"Invalid text input"); QInputMethodEvent ev;ev.setCommitString(text); auto focus=window.activeFocusItem();if(focus)QCoreApplication::sendEvent(focus,&ev); }else if(kind=="key"){ const QMap keys{{"Enter",Qt::Key_Return},{"Tab",Qt::Key_Tab},{"Backspace",Qt::Key_Backspace},{"Delete",Qt::Key_Delete},{"ArrowLeft",Qt::Key_Left},{"ArrowRight",Qt::Key_Right},{"ArrowUp",Qt::Key_Up},{"ArrowDown",Qt::Key_Down},{"Home",Qt::Key_Home},{"End",Qt::Key_End},{"Escape",Qt::Key_Escape},{"SelectAll",Qt::Key_A}}; auto name=e.value("key").toString();require(keys.contains(name),"Unsupported QML key"); auto modifiers=name=="SelectAll"?Qt::ControlModifier:Qt::NoModifier; QKeyEvent down(QEvent::KeyPress,keys[name],modifiers),up(QEvent::KeyRelease,keys[name],modifiers); QCoreApplication::sendEvent(&window,&down);QCoreApplication::sendEvent(&window,&up); }else throw std::runtime_error("Unknown QML input event"); } }