File size: 4,439 Bytes
2e7de11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Task image policy: Friction animation tools remain available; its expression
// code editor and ECMAScript execution are disabled. No agent text is logged.
#include <QCoreApplication>
#include <QEvent>
#include <QPointer>
#include <QTimer>
#include <QRegularExpression>
#include <cmath>
#include <QAction>
#include <QMenu>
#include <QDialog>
#include <QJSEngine>
#include <QJSValue>
#include <QWidget>
#include <dlfcn.h>
#include <cstdio>
#include <cstdlib>

static bool codeWindow(const QWidget *widget) {
    for (auto current = widget; current; current = current->parentWidget()) {
        const QString title = current->windowTitle();
        const QString type = current->metaObject()->className();
        if (title.startsWith("Expression", Qt::CaseInsensitive)
                || type.contains("ExpressionEditor") || type.contains("JSEditor"))
            return true;
    }
    return false;
}

static void blocked() {
    std::fputs("GUI-only task: expression scripting is disabled.\n", stderr);
}

class CodeEditorFilter : public QObject {
public:
    using QObject::QObject;
    bool eventFilter(QObject *object, QEvent *event) override {
        if (event->type() == QEvent::Show) {
            if (auto menu = qobject_cast<QMenu *>(object)) {
                for (auto action : menu->actions()) {
                    if (action->text().contains("Expression", Qt::CaseInsensitive)) {
                        action->setEnabled(false);
                        action->setToolTip("Expression scripting is disabled; use keyframes.");
                    }
                }
            }
        }
        auto widget = qobject_cast<QWidget *>(object);
        if (!widget || !codeWindow(widget)) return false;
        switch (event->type()) {
        case QEvent::Show: {
            widget->setEnabled(false);
            auto window = widget->window();
            window->setAttribute(Qt::WA_DontShowOnScreen, true);
            QPointer<QWidget> safeWindow(window);
            QTimer::singleShot(0, this, [safeWindow] {
                if (safeWindow) safeWindow->close();
            });
            blocked();
            return true;
        }
        case QEvent::KeyPress:
        case QEvent::KeyRelease:
        case QEvent::InputMethod:
        case QEvent::MouseButtonPress:
        case QEvent::MouseButtonRelease:
        case QEvent::Shortcut:
        case QEvent::ShortcutOverride:
            return true;
        default:
            return false;
        }
    }
};

static void installFilter() {
    static QPointer<CodeEditorFilter> filter;
    auto app = QCoreApplication::instance();
    if (app && !filter) {
        filter = new CodeEditorFilter(app);
        app->installEventFilter(filter);
    }
}

int QCoreApplication::exec() {
    installFilter();
    using Function = int (*)();
    static auto original = reinterpret_cast<Function>(dlsym(RTLD_NEXT, "_ZN16QCoreApplication4execEv"));
    if (!original) std::abort();
    return original();
}

int QDialog::exec() {
    if (codeWindow(this)) {
        setEnabled(false);
        blocked();
        return QDialog::Rejected;
    }
    using Function = int (*)(QDialog *);
    static auto original = reinterpret_cast<Function>(dlsym(RTLD_NEXT, "_ZN7QDialog4execEv"));
    if (!original) std::abort();
    return original(this);
}

QJSValue QJSEngine::evaluate(const QString &program, const QString &, int) {
    installFilter();
    // Friction's ordinary numeric sliders also call evaluate(text()). Parse
    // literal numbers directly, without executing ECMAScript, so normal GUI
    // coordinate/keyframe entry remains available.
    static const QRegularExpression number(
        QStringLiteral(R"(^[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?$)"));
    const QString text = program.trimmed();
    if (number.match(text).hasMatch()) {
        bool ok = false;
        const double value = text.toDouble(&ok);
        if (ok && std::isfinite(value)) return QJSValue(value);
    }
    blocked();
    return QJSValue(QJSValue::UndefinedValue);
}

QJSValue QJSValue::call(const QJSValueList &) {
    blocked();
    return QJSValue(QJSValue::UndefinedValue);
}

QJSValue QJSValue::callWithInstance(const QJSValue &, const QJSValueList &) {
    blocked();
    return QJSValue(QJSValue::UndefinedValue);
}

QJSValue QJSValue::callAsConstructor(const QJSValueList &) {
    blocked();
    return QJSValue(QJSValue::UndefinedValue);
}