mirvncserver.cpp 28.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * Copyright © 2014 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Alexandros Frantzis <alexandros.frantzis@canonical.com>
 */

19
20
#include <QtGui/QImage>

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
#include "mir_toolkit/mir_client_library.h"
#include "mir_toolkit/mir_screencast.h"
#include "mir/geometry/size.h"
#include "mir/geometry/rectangle.h"

#include "mir/raii.h"

#include <boost/program_options.hpp>

#include <getopt.h>
#include <csignal>

#include <EGL/egl.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>

#include <fstream>
#include <sstream>
#include <thread>
#include <iostream>
#include <stdexcept>
#include <future>
#include <vector>
#include <utility>
#include <chrono>

#include <unistd.h>
#include <rfb/rfb.h>
#include <rfb/keysym.h>

abmyii's avatar
abmyii committed
51
52
53
54
#include <stdio.h>
#include <libevdev/libevdev.h>
#include <libevdev/libevdev-uinput.h>

55
56
57
#include <fcntl.h>    /* For O_RDWR */
#include <unistd.h>   /* For open(), creat() */

abmyii's avatar
abmyii committed
58

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
namespace po = boost::program_options;

namespace
{

volatile sig_atomic_t running = 1;

/* On devices with android based openGL drivers, the vendor dispatcher table
 * may be optimized if USE_FAST_TLS_KEY is set, which hardcodes a TLS slot where
 * the vendor opengl function pointers live. Since glibc is not aware of this
 * use, collisions may happen.
 * Allocating a thread_local array helps avoid collisions by any thread_local usage
 * in async/future implementations.
 */
thread_local int dummy_tls[2];

abmyii's avatar
abmyii committed
75
76
struct libevdev *dev;
struct libevdev_uinput *uidev;
77

abmyii's avatar
abmyii committed
78
79
int maxx, maxy;
bool landscape = false;
abmyii's avatar
abmyii committed
80
bool mirror = false;
abmyii's avatar
abmyii committed
81
float scale_factor;
82
int orientation;
abmyii's avatar
abmyii committed
83

84
bool lmb;
85
86
bool rmb;
bool mmb;
87

abmyii's avatar
abmyii committed
88

89
90
91
92
93
void shutdown(int)
{
    running = 0;
}

abmyii's avatar
abmyii committed
94
void read_pixels(int bpp, GLenum format, mir::geometry::Size const& size, void *fb)
95
96
97
98
{
    auto width = size.width.as_uint32_t();
    auto height = size.height.as_uint32_t();

99
100
101
    // Read pixels into image (https://community.khronos.org/t/render-to-texture-under-qt/68430/3)
    QImage image(width, height, QImage::Format_RGBA8888);
    glReadPixels(0, 0, width, height, format, GL_UNSIGNED_BYTE, image.bits());
abmyii's avatar
abmyii committed
102

abmyii's avatar
abmyii committed
103
104
105
    if (mirror)
	image = image.mirrored(true, true);

abmyii's avatar
abmyii committed
106
107
108
109
110
111
112
113
    if (landscape) {
        //QPoint center = image.rect().center();
        QMatrix matrix;
        matrix.translate(0, 0);
        matrix.rotate(90);

        image = image.transformed(matrix);
    }
abmyii's avatar
abmyii committed
114
    std::memcpy(fb, (const char *)image.constBits(), image.byteCount());
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
#ifdef DEBUG
    unsigned int sum = 0;
    unsigned int *pui = (unsigned int *) buffer;
    for (int i = 0; i < width * height; i++) {
        sum += *(pui + i);
    }
    printf("read pixels sum 0x%x\n", sum);
#endif
}


uint32_t get_first_valid_output_id(MirDisplayConfiguration const& display_config)
{
    for (unsigned int i = 0; i < display_config.num_outputs; ++i)
    {
        MirDisplayOutput const& output = display_config.outputs[i];

        if (output.connected && output.used &&
            output.current_mode < output.num_modes)
        {
            return output.output_id;
        }
    }

    throw std::runtime_error("Couldn't find a valid output to screencast");
}

MirRectangle get_screen_region_from(MirDisplayConfiguration const& display_config, uint32_t output_id)
{
    if (output_id == mir_display_output_id_invalid)
        output_id = get_first_valid_output_id(display_config);

    for (unsigned int i = 0; i < display_config.num_outputs; ++i)
    {
        MirDisplayOutput const& output = display_config.outputs[i];

        if (output.output_id == output_id &&
            output.current_mode < output.num_modes)
        {
            MirDisplayMode const& mode = output.modes[output.current_mode];
            return MirRectangle{output.position_x, output.position_y,
                                mode.horizontal_resolution,
                                mode.vertical_resolution};
        }
    }

    throw std::runtime_error("Couldn't get screen region of specified output");
}

MirScreencastParameters get_screencast_params(MirConnection* connection,
                                              MirDisplayConfiguration const& display_config,
                                              std::vector<int> const& requested_size,
                                              std::vector<int> const& requested_region,
                                              uint32_t output_id)
{
    MirScreencastParameters params;
abmyii's avatar
abmyii committed
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
    if (requested_region.size() == 4)
    {
        /* TODO: the region should probably be validated
         * to make sure it overlaps any one of the active display areas
         */
        params.region.left = requested_region[0];
        params.region.top = requested_region[1];
        params.region.width = requested_region[2];
        params.region.height = requested_region[3];
    }
    else
    {
        params.region = get_screen_region_from(display_config, output_id);
    }

    if (requested_size.size() == 2)
    {
        params.width = requested_size[0];
        params.height = requested_size[1];
    }
    else
    {
        params.width = params.region.width;
        params.height = params.region.height;
    }

    unsigned int num_valid_formats = 0;
    mir_connection_get_available_surface_formats(connection, &params.pixel_format, 1, &num_valid_formats);

    if (num_valid_formats == 0)
        throw std::runtime_error("Couldn't find a valid pixel format for connection");

    return params;
}

double get_capture_rate_limit(MirDisplayConfiguration const& display_config, MirScreencastParameters const& params)
{
    mir::geometry::Rectangle screencast_area{
        {params.region.left, params.region.top},
        {params.region.width, params.region.height}};

    double highest_refresh_rate = 0.0;
    for (unsigned int i = 0; i < display_config.num_outputs; ++i)
    {
        MirDisplayOutput const& output = display_config.outputs[i];
        if (output.connected && output.used &&
            output.current_mode < output.num_modes)
        {
            MirDisplayMode const& mode = output.modes[output.current_mode];
            mir::geometry::Rectangle display_area{
                {output.position_x, output.position_y},
                {mode.horizontal_resolution, mode.vertical_resolution}};
            if (display_area.overlaps(screencast_area) && mode.refresh_rate > highest_refresh_rate)
                highest_refresh_rate = mode.refresh_rate;
        }
    }

    /* Either the display config has invalid refresh rate data
     * or the screencast region is outside of any display area, either way
     * let's default to sensible max capture limit
     */
    if (highest_refresh_rate == 0.0)
        highest_refresh_rate = 60.0;

    return highest_refresh_rate;
}

struct EGLSetup
{
    EGLSetup(MirConnection* connection, MirScreencast* screencast)
    {
        static EGLint const attribs[] = {
            EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
            EGL_RED_SIZE, 8,
            EGL_GREEN_SIZE, 8,
            EGL_BLUE_SIZE, 8,
            EGL_ALPHA_SIZE, 8,
            EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
            EGL_NONE};

        static EGLint const context_attribs[] = {
            EGL_CONTEXT_CLIENT_VERSION, 2,
            EGL_NONE };

        auto native_display =
            reinterpret_cast<EGLNativeDisplayType>(
                mir_connection_get_egl_native_display(connection));

abmyii's avatar
abmyii committed
260
261
	MirBufferStream *buffer_stream_;
	buffer_stream_ = mir_screencast_get_buffer_stream(screencast);
262
263
        auto native_window =
            reinterpret_cast<EGLNativeWindowType>(
abmyii's avatar
abmyii committed
264
265
                mir_buffer_stream_get_egl_native_window(buffer_stream_)
	    );
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321

        egl_display = eglGetDisplay(native_display);

        eglInitialize(egl_display, nullptr, nullptr);

        int n;
        eglChooseConfig(egl_display, attribs, &egl_config, 1, &n);

        egl_surface = eglCreateWindowSurface(egl_display, egl_config, native_window, NULL);
        if (egl_surface == EGL_NO_SURFACE)
            throw std::runtime_error("Failed to create EGL screencast surface");

        egl_context = eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, context_attribs);
        if (egl_context == EGL_NO_CONTEXT)
            throw std::runtime_error("Failed to create EGL context for screencast");

        if (eglMakeCurrent(egl_display, egl_surface,
                           egl_surface, egl_context) != EGL_TRUE)
        {
            throw std::runtime_error("Failed to make screencast surface current");
        }

        uint32_t a_pixel;
        glReadPixels(0, 0, 1, 1, GL_BGRA_EXT, GL_UNSIGNED_BYTE, &a_pixel);
        if (glGetError() == GL_NO_ERROR)
            read_pixel_format = GL_BGRA_EXT;
        else
            read_pixel_format = GL_RGBA;
    }

    ~EGLSetup()
    {
        eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
        eglDestroySurface(egl_display, egl_surface);
        eglDestroyContext(egl_display, egl_context);
        eglTerminate(egl_display);
    }

    void swap_buffers() const
    {
        if (eglSwapBuffers(egl_display, egl_surface) != EGL_TRUE)
            throw std::runtime_error("Failed to swap screencast surface buffers");
    }

    GLenum pixel_read_format() const
    {
        return read_pixel_format;
    }

    EGLDisplay egl_display;
    EGLContext egl_context;
    EGLSurface egl_surface;
    EGLConfig egl_config;
    GLenum read_pixel_format;
};

abmyii's avatar
abmyii committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340

// https://github.com/hanzelpeter/dispmanx_vnc/blob/master/main.c
static int keysym2scancode(rfbKeySym key)
{
	int scancode = 0;

	int code = (int) key;
	if (code>='0' && code<='9') {
		scancode = (code & 0xF) - 1;
		if (scancode<0) scancode += 10;
		scancode += KEY_1;
	} else if (code>=0xFF50 && code<=0xFF58) {
		static const uint16_t map[] =
		{  KEY_HOME, KEY_LEFT, KEY_UP, KEY_RIGHT, KEY_DOWN,
		KEY_PAGEUP, KEY_PAGEDOWN, KEY_END, 0 };
		scancode = map[code & 0xF];
	} else if (code>=0xFFE1 && code<=0xFFEE) {
		static const uint16_t map[] =
		{  KEY_LEFTSHIFT, KEY_LEFTSHIFT,
abmyii's avatar
abmyii committed
341
342
		KEY_RIGHTSHIFT, KEY_LEFTCTRL,
		KEY_RIGHTCTRL, KEY_LEFTSHIFT,
abmyii's avatar
abmyii committed
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
		0,0,
		KEY_LEFTALT, KEY_RIGHTALT,
		0, 0, 0, 0 };
		scancode = map[code & 0xF];
	} else if ((code>='A' && code<='Z') || (code>='a' && code<='z')) {
		static const uint16_t map[] = {
			KEY_A, KEY_B, KEY_C, KEY_D, KEY_E,
			KEY_F, KEY_G, KEY_H, KEY_I, KEY_J,
			KEY_K, KEY_L, KEY_M, KEY_N, KEY_O,
			KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T,
			KEY_U, KEY_V, KEY_W, KEY_X, KEY_Y, KEY_Z };
		scancode = map[(code & 0x5F) - 'A'];
	} else {
		switch (code) {
		case XK_space:    scancode = KEY_SPACE;       break;

		case XK_exclam: scancode = KEY_1; break;
		case XK_at:     scancode         = KEY_2; break;
		case XK_numbersign:    scancode      = KEY_3; break;
		case XK_dollar:    scancode  = KEY_4; break;
		case XK_percent:    scancode = KEY_5; break;
		case XK_asciicircum:    scancode     = KEY_6; break;
		case XK_ampersand:    scancode       = KEY_7; break;
		case XK_asterisk:    scancode        = KEY_8; break;
		case XK_parenleft:    scancode       = KEY_9; break;
		case XK_parenright:    scancode      = KEY_0; break;
		case XK_minus:    scancode   = KEY_MINUS; break;
		case XK_underscore:    scancode      = KEY_MINUS; break;
		case XK_equal:    scancode   = KEY_EQUAL; break;
		case XK_plus:    scancode    = KEY_EQUAL; break;
		case XK_BackSpace:    scancode       = KEY_BACKSPACE; break;
		case XK_Tab:    scancode             = KEY_TAB; break;

		case XK_braceleft:    scancode        = KEY_LEFTBRACE;     break;
		case XK_braceright:    scancode       = KEY_RIGHTBRACE;     break;
		case XK_bracketleft:    scancode      = KEY_LEFTBRACE;     break;
		case XK_bracketright:    scancode     = KEY_RIGHTBRACE;     break;
		case XK_Return:    scancode  = KEY_ENTER;     break;

		case XK_semicolon:    scancode        = KEY_SEMICOLON;     break;
		case XK_colon:    scancode    = KEY_SEMICOLON;     break;
		case XK_apostrophe:    scancode       = KEY_APOSTROPHE;     break;
		case XK_quotedbl:    scancode         = KEY_APOSTROPHE;     break;
		case XK_grave:    scancode    = KEY_GRAVE;     break;
		case XK_asciitilde:    scancode       = KEY_GRAVE;     break;
		case XK_backslash:    scancode        = KEY_BACKSLASH;     break;
		case XK_bar:    scancode              = KEY_BACKSLASH;     break;

		case XK_comma:    scancode    = KEY_COMMA;      break;
		case XK_less:    scancode     = KEY_COMMA;      break;
		case XK_period:    scancode   = KEY_DOT;      break;
		case XK_greater:    scancode  = KEY_DOT;      break;
		case XK_slash:    scancode    = KEY_SLASH;      break;
		case XK_question:    scancode         = KEY_SLASH;      break;
		case XK_Caps_Lock:    scancode        = KEY_CAPSLOCK;      break;

		case XK_F1:    scancode               = KEY_F1; break;
		case XK_F2:    scancode               = KEY_F2; break;
		case XK_F3:    scancode               = KEY_F3; break;
		case XK_F4:    scancode               = KEY_F4; break;
		case XK_F5:    scancode               = KEY_F5; break;
		case XK_F6:    scancode               = KEY_F6; break;
		case XK_F7:    scancode               = KEY_F7; break;
		case XK_F8:    scancode               = KEY_F8; break;
		case XK_F9:    scancode               = KEY_F9; break;
		case XK_F10:    scancode              = KEY_F10; break;
		case XK_Num_Lock:    scancode         = KEY_NUMLOCK; break;
		case XK_Scroll_Lock:    scancode      = KEY_SCROLLLOCK; break;

		case XK_Page_Down:    scancode        = KEY_PAGEDOWN; break;
		case XK_Insert:    scancode   = KEY_INSERT; break;
		case XK_Delete:    scancode   = KEY_DELETE; break;
		case XK_Page_Up:    scancode  = KEY_PAGEUP; break;
		case XK_Escape:    scancode   = KEY_ESC; break;

		case XK_KP_Divide:   scancode = KEY_KPSLASH; break;
		case XK_KP_Multiply: scancode = KEY_KPASTERISK; break;
		case XK_KP_Add:      scancode = KEY_KPPLUS; break;
		case XK_KP_Subtract: scancode = KEY_KPMINUS; break;
		case XK_KP_Enter:    scancode = KEY_KPENTER; break;

		case XK_KP_Decimal:
		case XK_KP_Delete:
			scancode = KEY_KPDOT; break;

		case XK_KP_0:
		case XK_KP_Insert:
			scancode = KEY_KP0; break;

		case XK_KP_1:
		case XK_KP_End:
			scancode = KEY_KP1; break;

		case XK_KP_2:
		case XK_KP_Down:
			scancode = KEY_KP2; break;

		case XK_KP_3:
		case XK_KP_Page_Down:
			scancode = KEY_KP3; break;

		case XK_KP_4:
		case XK_KP_Left:
			scancode = KEY_KP4; break;

		case XK_KP_5:
			scancode = KEY_KP5; break;

		case XK_KP_6:
		case XK_KP_Right:
			scancode = KEY_KP6; break;

		case XK_KP_7:
		case XK_KP_Home:
			scancode = KEY_KP7; break;

		case XK_KP_8:
		case XK_KP_Up:
			scancode = KEY_KP8; break;

		case XK_KP_9:
		case XK_KP_Page_Up:
			scancode = KEY_KP9; break;

		}
	}

	return scancode;
}


474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// Rotation logic
int mod(int a, int b)
{ return (a%b+b)%b; }

void rotate(int direction, rfbClientPtr cl) {
    orientation += direction;
    orientation = mod(orientation, 4);  // 4 orientations
    bool landscape_cp = landscape;
    switch (orientation) {
        case 0:
	    mirror = false;
	    landscape = false;
	    break;
	case 1:
	    mirror = false;
	    landscape = true;
	    break;
	case 2:
	    mirror = true;
	    landscape = false;
	    break;
	case 3:
	    mirror = true;
	    landscape = true;
	    break;
    }

    if (landscape != landscape_cp) { // Landscape mode toggled
        std::swap(maxx, maxy);
    }
}


507
508
509
// vnc key event function, escape to quit
static void dokey(rfbBool down,rfbKeySym key,rfbClientPtr cl)
{
abmyii's avatar
abmyii committed
510
511
512
513
514
  /* if(down && key==XK_Escape) { */
  /*     /1* close down server, disconnecting clients *1/ */
  /*     rfbShutdownServer(cl->screen,TRUE); */
  /*     running = 0; */
  /* } */
515
516
517
518
  // printf("keysym: %04X\n", key);

  if (down && key == 0xFF52)  // Ctrl+Super+Up
      rotate(-1, cl);
519
520
  else if (down && key == 0xFF54)  // Ctrl+Super+Down
      rotate(1, cl);
521
522
523
  else
      libevdev_uinput_write_event(uidev, EV_KEY, keysym2scancode(key), down);
      libevdev_uinput_write_event(uidev, EV_SYN, SYN_REPORT, 0);
524
525
}

abmyii's avatar
abmyii committed
526
527
528

static void doptr(int buttonMask, int x, int y, rfbClientPtr cl)
{
abmyii's avatar
abmyii committed
529
  int left_down = buttonMask & 0x1;
530
531
532
  int right_down = buttonMask & 0x4;
  int middle_down = buttonMask & 0x2;

abmyii's avatar
abmyii committed
533
534
535
536
537
  if (mirror) {
    y = maxy - y;
    x = maxx - x;
  }

abmyii's avatar
abmyii committed
538
  if (landscape) {
539
    x = maxx - x;
abmyii's avatar
abmyii committed
540
541
    std::swap(x, y);
  }
542

abmyii's avatar
abmyii committed
543
  if (left_down) {
544
545
546
    libevdev_uinput_write_event(uidev, EV_KEY, BTN_LEFT, 1);
    libevdev_uinput_write_event(uidev, EV_SYN, SYN_REPORT, 0);

547
548
549
    libevdev_uinput_write_event(uidev, EV_ABS, ABS_MT_TRACKING_ID, 10);
    libevdev_uinput_write_event(uidev, EV_KEY, BTN_TOUCH, 1);
    libevdev_uinput_write_event(uidev, EV_KEY, BTN_TOOL_FINGER, 1);
abmyii's avatar
abmyii committed
550
551
    libevdev_uinput_write_event(uidev, EV_ABS, ABS_MT_POSITION_X, x*scale_factor);
    libevdev_uinput_write_event(uidev, EV_ABS, ABS_MT_POSITION_Y, y*scale_factor);
552
    libevdev_uinput_write_event(uidev, EV_SYN, SYN_REPORT, 0);
abmyii's avatar
abmyii committed
553
554
555
    lmb = true;
  }
  else if (lmb) {
556
557
558
    libevdev_uinput_write_event(uidev, EV_KEY, BTN_LEFT, 0);
    libevdev_uinput_write_event(uidev, EV_SYN, SYN_REPORT, 0);

559
560
561
562
    libevdev_uinput_write_event(uidev, EV_ABS, ABS_MT_TRACKING_ID, -1);
    libevdev_uinput_write_event(uidev, EV_KEY, BTN_TOUCH, 0);
    libevdev_uinput_write_event(uidev, EV_KEY, BTN_TOOL_FINGER, 0);
    libevdev_uinput_write_event(uidev, EV_SYN, SYN_REPORT, 0);
abmyii's avatar
abmyii committed
563
564
    lmb = false;
  }
565
566
567
568
569
570
571
572
573
574
575
576

  if (right_down || rmb) {
    libevdev_uinput_write_event(uidev, EV_KEY, BTN_RIGHT, right_down >> 2);
    libevdev_uinput_write_event(uidev, EV_SYN, SYN_REPORT, 0);
    rmb ^= true;
  }

  if (middle_down || mmb) {
    libevdev_uinput_write_event(uidev, EV_KEY, BTN_MIDDLE, middle_down >> 1);
    libevdev_uinput_write_event(uidev, EV_SYN, SYN_REPORT, 0);
    mmb ^= true;
  }
abmyii's avatar
abmyii committed
577
578
579
}


580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
void do_screencast(EGLSetup const& egl_setup,
                   mir::geometry::Size const& size,
                   int32_t number_of_captures,
                   double capture_fps,
                   std::ostream& out_stream)
{
    static int const rgba_pixel_size{4};
    auto const frame_size_bytes = rgba_pixel_size *
                                  size.width.as_uint32_t() *
                                  size.height.as_uint32_t();

    std::vector<char> frame_data(frame_size_bytes, 0);
    auto format = egl_setup.pixel_read_format();

    auto capture_period = std::chrono::duration<double>(1.0/capture_fps);

    // vnc server
597
    int bpp = 4;
abmyii's avatar
abmyii committed
598
599
600
    maxx = size.width.as_uint32_t();
    maxy = size.height.as_uint32_t();

601
602
603
604
605
606
    rfbScreenInfoPtr rfbScreen = rfbGetScreen(NULL,NULL,maxx,maxy,8,3,bpp);
    rfbScreen->desktopName = "MIR Screen";
    // rfbScreen->frameBuffer = (char*)malloc(maxx*maxy*bpp);
    rfbScreen->frameBuffer = frame_data.data();
    rfbScreen->alwaysShared = TRUE;
    rfbScreen->kbdAddEvent = dokey;
abmyii's avatar
abmyii committed
607
    rfbScreen->ptrAddEvent = doptr;
608
609
610
611
    rfbInitServer(rfbScreen);
    rfbRunEventLoop(rfbScreen,-1,TRUE);
    fprintf(stderr, "Running vnc background loop...\n");

abmyii's avatar
abmyii committed
612
613
    std::vector<double> fps_arr;

614
615
616
617
    while (running)
    {
        auto time_point = std::chrono::steady_clock::now() + capture_period;

618
619
620
621
622
	auto start = std::chrono::high_resolution_clock::now();
        read_pixels(bpp, format, size, frame_data.data());
	auto finish = std::chrono::high_resolution_clock::now();
        std::chrono::duration<double> elapsed = finish - start;
	std::cout << "Elapsed time: " << elapsed.count() << " s, " << 1/elapsed.count() << " fps\n";
abmyii's avatar
abmyii committed
623
624
625
626
	fps_arr.push_back(1/elapsed.count());

	float average = accumulate(fps_arr.begin(), fps_arr.end(), 0.0)/fps_arr.size();
	std::cout << "Average FPS: " << average << std::endl;
627

628
629
630
631
632
	if (maxx/maxy != rfbScreen->width/rfbScreen->height) {
	    printf("Ratios don't match\n");
	    printf("RFB: %dx%d\n",  rfbScreen->width, rfbScreen->height);
	    printf("Screen: %dx%d\n",  size.width, size.height);
	    std::swap(rfbScreen->width, rfbScreen->height);
633
	    rfbScreen->paddedWidthInBytes = rfbScreen->width * bpp;
634
635
636
637
638
639
640
641
642
643
644
645
	    printf("RFB: %dx%d\n\n",  rfbScreen->width, rfbScreen->height);
	    {
		rfbClientIteratorPtr iterator;
		rfbClientPtr cl;
		iterator = rfbGetClientIterator(rfbScreen);
		while ((cl = rfbClientIteratorNext(iterator)) != NULL)
			cl->newFBSizePending = 1;
	    }
	    rfbMarkRectAsModified(rfbScreen, 0, 0, rfbScreen->width, rfbScreen->height);
	}


646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
        auto write_out_future = std::async(
                std::launch::async,
                [rfbScreen, maxx, maxy] {
                    if (running)
                        rfbMarkRectAsModified(rfbScreen, 0, 0, maxx, maxy);
                });

        egl_setup.swap_buffers();
        write_out_future.wait();

        std::this_thread::sleep_until(time_point);
    }

    // TODO: SEGFAULT with this on, need checking...
    // rfbScreenCleanup(rfbScreen);
}

}

int main(int argc, char* argv[])
try
{
    uint32_t output_id = mir_display_output_id_invalid;
    int number_of_captures = -1;
    std::string output_filename;
    std::string socket_filename;
    std::vector<int> screen_region;
    std::vector<int> requested_size;
    bool use_std_out = false;
    bool query_params_only = false;
    int capture_interval = 1;

678
    // Fake input device (https://stackoverflow.com/q/23092855)
abmyii's avatar
abmyii committed
679
    dev = libevdev_new();
680
681
682
683
684
685
686
687
688
689
690
691
692
693
    libevdev_set_name(dev, "mirvncserver input device");
    libevdev_set_id_vendor(dev, 0x1);
    libevdev_set_id_product(dev, 0x1);
    libevdev_set_id_version(dev, 0x1);
    libevdev_set_id_bustype(dev, BUS_USB);

    // (Absolute) pointer input (http://who-t.blogspot.com/2013/09/libevdev-accessing-and-modifying-devices.html)
    struct input_absinfo abs;
    abs.minimum = abs.resolution = abs.fuzz = abs.flat = 0;

    libevdev_enable_event_type(dev, EV_ABS);

    abs.maximum = 9;
    libevdev_enable_event_code(dev, EV_ABS, ABS_MT_SLOT, &abs);
abmyii's avatar
abmyii committed
694

695
696
697
698
    abs.maximum = 65535;
    libevdev_enable_event_code(dev, EV_ABS, ABS_MT_TRACKING_ID, &abs);

    abs.maximum = 1080;
699
    libevdev_enable_event_code(dev, EV_ABS, ABS_X, &abs);
700
701
702
    libevdev_enable_event_code(dev, EV_ABS, ABS_MT_POSITION_X, &abs);

    abs.maximum = 1920;
703
    libevdev_enable_event_code(dev, EV_ABS, ABS_Y, &abs);
704
705
706
    libevdev_enable_event_code(dev, EV_ABS, ABS_MT_POSITION_Y, &abs);

    // Keyboard input
abmyii's avatar
abmyii committed
707
    libevdev_enable_event_type(dev, EV_KEY);
708
709
710
    libevdev_enable_event_code(dev, EV_KEY, BTN_LEFT, NULL);
    libevdev_enable_event_code(dev, EV_KEY, BTN_RIGHT, NULL);
    libevdev_enable_event_code(dev, EV_KEY, BTN_MIDDLE, NULL);
711
712
    libevdev_enable_event_code(dev, EV_KEY, BTN_TOUCH, NULL);
    libevdev_enable_event_code(dev, EV_KEY, BTN_TOOL_FINGER, NULL);
abmyii's avatar
abmyii committed
713
714
715
    for (int i = 0; i < KEY_MAX; i++)
      libevdev_enable_event_code(dev, EV_KEY, i, NULL);

abmyii's avatar
abmyii committed
716
    libevdev_uinput_create_from_device(dev,
abmyii's avatar
abmyii committed
717
718
719
        LIBEVDEV_UINPUT_OPEN_MANAGED,
        &uidev);

720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
    //avoid unused warning/error
    dummy_tls[0] = 0;

    po::options_description desc("Usage");
    desc.add_options()
        ("help,h", "displays this message")
        ("number-of-frames,n",
            po::value<int>(&number_of_captures), "number of frames to capture")
        ("display-id,d",
            po::value<uint32_t>(&output_id), "id of the display to capture")
        ("mir-socket-file,m",
            po::value<std::string>(&socket_filename), "mir server socket filename")
        ("file,f",
            po::value<std::string>(&output_filename), "output filename (default is /tmp/mir_screencast_<w>x<h>.<rgba|bgra>")
        ("size,s",
            po::value<std::vector<int>>(&requested_size)->multitoken(),
            "screencast size [width height]")
        ("screen-region,r",
            po::value<std::vector<int>>(&screen_region)->multitoken(),
            "screen region to capture [left top width height]")
        ("stdout", po::value<bool>(&use_std_out)->zero_tokens(), "use stdout for output (--file is ignored)")
        ("query",
            po::value<bool>(&query_params_only)->zero_tokens(),
            "only queries the colorspace and output size used but does not start screencast")
        ("cap-interval",
            po::value<int>(&capture_interval),
            "adjusts the capture rate to <arg> display refresh intervals\n"
            "1 -> capture at display rate\n2 -> capture at half the display rate, etc..");

    po::variables_map vm;
    try
    {
        po::store(po::parse_command_line(argc, argv, desc), vm);
        po::notify(vm);

        if (vm.count("size") && requested_size.size() != 2)
            throw po::error("invalid number of parameters specified for size");

        if (vm.count("screen-region") && screen_region.size() != 4)
            throw po::error("invalid number of parameters specified for screen-region");

        if (vm.count("display-id") && vm.count("screen-region"))
            throw po::error("cannot set both display-id and screen-region");

        if (vm.count("cap-interval") && capture_interval < 1)
            throw po::error("invalid capture interval");
    }
    catch(po::error& e)
    {
        std::cerr << e.what() << std::endl << std::endl;
        std::cerr << desc << std::endl;
        return EXIT_FAILURE;
    }

    if (vm.count("help"))
    {
        std::cout << desc << std::endl;
        return EXIT_SUCCESS;
    }

    signal(SIGINT, shutdown);
    signal(SIGTERM, shutdown);

    char const* socket_name = vm.count("mir-socket-file") ? socket_filename.c_str() : nullptr;
    auto const connection = mir::raii::deleter_for(
        mir_connect_sync(socket_name, "mirscreencast"),
        [](MirConnection* c) { if (c) mir_connection_release(c); });

    if (connection == nullptr || !mir_connection_is_valid(connection.get()))
    {
        std::string msg("Failed to connect to server.");
        if (connection)
        {
            msg += " Error was :";
            msg += mir_connection_get_error_message(connection.get());
        }
        throw std::runtime_error(std::string(msg));
    }

    auto const display_config = mir::raii::deleter_for(
        mir_connection_create_display_config(connection.get()),
        &mir_display_config_destroy);

    if (display_config == nullptr)
        throw std::runtime_error("Failed to get display configuration\n");

    MirScreencastParameters params =
        get_screencast_params(connection.get(), *display_config, requested_size, screen_region, output_id);

abmyii's avatar
abmyii committed
809
810
811
812
    // Calculate scale factor (NOTE: assumes scale ratio is equivalent on both axes)
    scale_factor = (float)params.region.width / (float)params.width;
    printf("%d %d %f\n", params.width, params.region.width, scale_factor);

813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
    double capture_rate_limit = get_capture_rate_limit(*display_config, params);
    double capture_fps = capture_rate_limit/capture_interval;

    auto const screencast = mir::raii::deleter_for(
        mir_connection_create_screencast_sync(connection.get(), &params),
        [](MirScreencast* s) { if (s) mir_screencast_release_sync(s); });

    if (screencast == nullptr)
        throw std::runtime_error("Failed to create screencast");

    EGLSetup egl_setup{connection.get(), screencast.get()};
    mir::geometry::Size screencast_size {params.width, params.height};

    if (output_filename.empty() && !use_std_out)
    {
        std::stringstream ss;
        ss << "/tmp/mir_screencast_" ;
        ss << screencast_size.width << "x" << screencast_size.height;
        ss << "_" << capture_fps << "Hz";
        ss << (egl_setup.pixel_read_format() == GL_BGRA_EXT ? ".bgra" : ".rgba");
        output_filename = ss.str();
    }

    if (query_params_only)
    {
       std::cout << "Colorspace: " <<
           (egl_setup.pixel_read_format() == GL_BGRA_EXT ? "BGRA" : "RGBA") << std::endl;
       std::cout << "Output size: " <<
           screencast_size.width << "x" << screencast_size.height << std::endl;
       std::cout << "Capture rate (Hz): " << capture_fps << std::endl;
       std::cout << "Output to: " <<
           (use_std_out ? "standard out" : output_filename) << std::endl;
       return EXIT_SUCCESS;
    }

    if (use_std_out)
    {
        do_screencast(egl_setup, screencast_size, number_of_captures, capture_fps, std::cout);
    }
    else
    {
        std::ofstream file_stream(output_filename);
        do_screencast(egl_setup, screencast_size, number_of_captures, capture_fps, file_stream);
    }

858
    // Clear libevdev devices
abmyii's avatar
abmyii committed
859
    libevdev_uinput_destroy(uidev);
860
    libevdev_free(dev);
abmyii's avatar
abmyii committed
861

862
863
864
865
866
867
868
    return EXIT_SUCCESS;
}
catch(std::exception const& e)
{
    std::cerr << e.what() << std::endl;
    return EXIT_FAILURE;
}