Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ProjectorClient.cpp - github.com/neutrinolabs/ulalaca-xrdp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f6dcf18b65b088dae44c5d69af64c8fca98ef912 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//
// Created by Gyuhwan Park on 2022/05/06.
//

#if defined(HAVE_CONFIG_H)
#include <config_ac.h>
#endif

extern "C" {
#include "arch.h"
#include "parse.h"
#include "os_calls.h"
#include "defines.h"
#include "guid.h"
#include "xrdp_client_info.h"
}

#include "ProjectorClient.hpp"
#include "KeycodeMap.hpp"

ProjectorClient::ProjectorClient(
    ProjectionTarget &target,
    const std::string &socketPath
):
    _target(target),
    _ipcConnection(socketPath),
    _isTerminated(false)
{

}

FD ProjectorClient::descriptor() {
    return _ipcConnection.descriptor();
}

void ProjectorClient::start() {
    _ipcConnection.connect();
    _projectorThread = std::thread(&ProjectorClient::mainLoop, this);
}

void ProjectorClient::stop() {
    _isTerminated = true;
    
    if (_projectorThread.joinable()) {
        _projectorThread.join();
    }
    
    _ipcConnection.disconnect();
}

void ProjectorClient::handleEvent(XrdpEvent &event) {
    if (_isTerminated) {
        return;
    }

    if (!_ipcConnection.isGood()) {
        _target.ipcDisconnected();
        this->stop();
        return;
    }

    if (event.isKeyEvent()) {
        auto keycode = event.param3;
        auto cgKeycode = rdpKeycodeToCGKeycode(keycode);
        auto eventType = event.type == XrdpEvent::KEY_DOWN ?
            KEYBOARD_EVENT_TYPE_KEYDOWN :
            KEYBOARD_EVENT_TYPE_KEYUP;
        
        if (cgKeycode == -1) {
            return;
        }
        
        _ipcConnection.writeMessage(TYPE_EVENT_KEYBOARD, ULIPCKeyboardEvent {
            eventType, (uint32_t) cgKeycode, 0
        });
    } else if (event.type == XrdpEvent::KEY_SYNCHRONIZE_LOCK) {
        auto lockStatus = event.param1;
    }
    
    if (event.isMouseEvent()) {
        switch (event.type) {
            case XrdpEvent::MOUSE_MOVE: {
                uint16_t posX = event.param1;
                uint16_t posY = event.param2;
                
                _ipcConnection.writeMessage(TYPE_EVENT_MOUSE_MOVE, ULIPCMouseMoveEvent {
                    posX, posY,
                    0
                });
                return;
            }
            
            case XrdpEvent::MOUSE_BUTTON_LEFT_DOWN: {
                _ipcConnection.writeMessage(TYPE_EVENT_MOUSE_BUTTON, ULIPCMouseButtonEvent {
                    MOUSE_EVENT_TYPE_DOWN,
                    MOUSE_EVENT_BUTTON_LEFT,
                    0
                });
                return;
            }
            case XrdpEvent::MOUSE_BUTTON_LEFT_UP: {
                _ipcConnection.writeMessage(TYPE_EVENT_MOUSE_BUTTON, ULIPCMouseButtonEvent {
                    MOUSE_EVENT_TYPE_UP,
                    MOUSE_EVENT_BUTTON_LEFT,
                    0
                });
                return;
            }
    
    
            case XrdpEvent::MOUSE_BUTTON_RIGHT_DOWN: {
                _ipcConnection.writeMessage(TYPE_EVENT_MOUSE_BUTTON, ULIPCMouseButtonEvent {
                    MOUSE_EVENT_TYPE_DOWN,
                    MOUSE_EVENT_BUTTON_RIGHT,
                    0
                });
                return;
            }
            case XrdpEvent::MOUSE_BUTTON_RIGHT_UP: {
                _ipcConnection.writeMessage(TYPE_EVENT_MOUSE_BUTTON, ULIPCMouseButtonEvent {
                    MOUSE_EVENT_TYPE_UP,
                    MOUSE_EVENT_BUTTON_RIGHT,
                    0
                });
                return;
            }
    
            case XrdpEvent::MOUSE_WHEEL_UP: {
                _ipcConnection.writeMessage(TYPE_EVENT_MOUSE_WHEEL, ULIPCMouseWheelEvent {
                    0, -56, 0
                });
                return;
            }
            case XrdpEvent::MOUSE_WHEEL_DOWN: {
                _ipcConnection.writeMessage(TYPE_EVENT_MOUSE_WHEEL, ULIPCMouseWheelEvent {
                    0, 56, 0
                });
                return;
            }
            
            default:
                break;
        }
    
    }
    
    
    if (event.type == XrdpEvent::INVALIDATE_REQUEST) {
        uint16_t x1 = HIWORD(event.param1);
        uint16_t y1 = LOWORD(event.param1);
        uint16_t x2 = HIWORD(event.param2);
        uint16_t y2 = LOWORD(event.param2);
        
        // TODO: redraw(rect)?
    }
    
    if (event.type == XrdpEvent::CHANNEL_EVENT) {
        uint16_t channelId = LOWORD(event.param1);
        uint16_t flags = HIWORD(event.param1);
        auto size = (int) event.param2;
        auto data = (char *) event.param3;
        auto total_size = (int) event.param4;
    }
}

void ProjectorClient::setViewport(ULIPCRect rect) {
    _ipcConnection.writeMessage(TYPE_PROJECTION_SET_VIEWPORT, ULIPCProjectionSetViewport {
        0, (uint16_t) rect.width, (uint16_t) rect.height, 0
    });
}

void ProjectorClient::setOutputSuppression(bool isOutputSuppressed) {
    if (isOutputSuppressed) {
        _ipcConnection.writeMessage(TYPE_PROJECTION_STOP, ULIPCProjectionStop {
            0
        });
    } else {
        _ipcConnection.writeMessage(TYPE_PROJECTION_START, ULIPCProjectionStart {
            0
        });
    }
}

void ProjectorClient::mainLoop() {
    while (!_isTerminated && _ipcConnection.isGood()) {
        auto header = _ipcConnection.nextHeader();
        
        switch (header->messageType) {
            case TYPE_SCREEN_UPDATE_NOTIFY: {
                auto notification = _ipcConnection.read<ULIPCScreenUpdateNotify>(header->length);
    
                LOG(LOG_LEVEL_DEBUG, "mainLoop(): adding dirty rect");
                _target.addDirtyRect(notification->rect);
                continue;
            }
            case TYPE_SCREEN_UPDATE_COMMIT: {
                auto commit = _ipcConnection.read<ULIPCScreenUpdateCommit>(header->length);
                auto bitmap = _ipcConnection.read<uint8_t>(commit->bitmapLength);
    
                LOG(LOG_LEVEL_DEBUG, "mainLoop(): commiting update");
                _target.commitUpdate(
                    bitmap.get(),
                    commit->bitmapLength,
                    commit->screenRect.width,
                    commit->screenRect.height
                );
                continue;
            }
            default: {
                // ignore
                _ipcConnection.read<uint8_t>(header->length);
            }
        }
    }
}