remoteosd.c 50.5 KB
Newer Older
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
/*****************************************************************************
 * remoteosd.c: remote osd over vnc filter module
 *****************************************************************************
 * Copyright (C) 2007-2008 Matthias Bauer
 * $Id$
 *
 * Authors: Matthias Bauer <matthias dot bauer #_at_# gmx dot ch>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implid 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

/*****************************************************************************
 * RemoteOSD uses the RFB-Protocol of VNC to display an On-Screen-Display
 * menu generated by a streaming server as overlay for the streamed video.
 *
 * The streaming server that implements this is the ffnetdev plugin for VDR.
 * VDR (VideoDiskRecorder) is an Linux based OpenSource harddisk recorder
 * software.
 * The VDR ffnetdev plugin emulates the hardware MPEG decoder and streams the
 * video over the network instead of hardware video outputs.
 * The OSD menu of VDR is offered with the RFB protocol to a VNC client.
 *
 * In fact this video-filter is a simple VNC client that could be also used to
 * connect to a real VNC host.
 * Only 8-bit color is supported at the moment.
 * Using password protected VNC hosts is supported but not recommended, because
 * you need to insert the used password in the plugin configuration page of
 * VLC configuration in plain text and it's saved in plain text.
 *****************************************************************************/

//#define VNC_DEBUG

/*****************************************************************************
 * Preamble
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_vout.h>

ivoire's avatar
ivoire committed
57
#include <vlc_filter.h>
58
#include "filter_common.h"
ivoire's avatar
ivoire committed
59 60 61
#include <vlc_image.h>
#include <vlc_osd.h>
#include <vlc_keys.h>
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

#include <vlc_network.h>
#include <gcrypt.h>              /* to encrypt password */
#include <vlc_gcrypt.h>

#include "remoteosd_rfbproto.h" /* type definitions of the RFB protocol for VNC */

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
#define READ_BUFFER_SIZE 1000000

#define RMTOSD_HOST_TEXT N_("VNC Host")
#define RMTOSD_HOST_LONGTEXT N_( \
    "VNC hostname or IP address." )

#define RMTOSD_PORT_TEXT N_("VNC Port")
#define RMTOSD_PORT_LONGTEXT N_( \
    "VNC portnumber." )

#define RMTOSD_PASSWORD_TEXT N_("VNC Password")
#define RMTOSD_PASSWORD_LONGTEXT N_( \
    "VNC password." )

#define RMTOSD_UPDATE_TEXT N_("VNC poll interval" )
#define RMTOSD_UPDATE_LONGTEXT N_( \
    "In this interval an update from VNC is requested, default every 300 ms. ")

#define RMTOSD_POLL_TEXT N_("VNC polling")
#define RMTOSD_POLL_LONGTEXT N_( \
    "Activate VNC polling. Do NOT activate for use as VDR ffnetdev client." )

#define RMTOSD_MOUSE_TEXT N_("Mouse events")
#define RMTOSD_MOUSE_LONGTEXT N_( \
    "Send mouse events to VNC host. Not needed for use as VDR ffnetdev client." )

#define RMTOSD_KEYS_TEXT N_("Key events")
#define RMTOSD_KEYS_LONGTEXT N_( \
    "Send key events to VNC host." )

#define RMTOSD_ALPHA_TEXT N_("Alpha transparency value (default 255)")
#define RMTOSD_ALPHA_LONGTEXT N_( \
    "The transparency of the OSD VNC can be changed by giving a value " \
    "between 0 and 255. A lower value specifies more transparency a higher " \
    "means less transparency. The default is being not transparent " \
    "(value 255) the minimum is fully transparent (value 0)." )

#define RMTOSD_CFG "rmtosd-"

#define RMTOSD_UPDATE_MIN     200
#define RMTOSD_UPDATE_DEFAULT 1000
#define RMTOSD_UPDATE_MAX     300

115 116
static int  CreateFilter ( vlc_object_t * );
static void DestroyFilter( vlc_object_t * );
117

118 119 120 121 122 123 124 125
vlc_module_begin ()
    set_description( N_("Remote-OSD over VNC") )
    set_capability( "sub filter", 100 )
    set_shortname( N_("Remote-OSD") )
    set_category( CAT_VIDEO )
    set_subcategory( SUBCAT_VIDEO_SUBPIC )
    add_shortcut( "rmtosd" )
    set_callbacks( CreateFilter, DestroyFilter )
126 127

    add_string( RMTOSD_CFG "host", "myvdr", NULL, RMTOSD_HOST_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
128
        RMTOSD_HOST_LONGTEXT, false )
129
    add_integer_with_range( RMTOSD_CFG "port", 20001, 1, 0xFFFF, NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
130
        RMTOSD_PORT_TEXT, RMTOSD_PORT_LONGTEXT, false )
131
    add_password( RMTOSD_CFG "password", "", NULL, RMTOSD_PASSWORD_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
132
        RMTOSD_PASSWORD_LONGTEXT, false )
133 134
    add_integer_with_range( RMTOSD_CFG "update", RMTOSD_UPDATE_DEFAULT,
        RMTOSD_UPDATE_MIN, RMTOSD_UPDATE_MAX, NULL, RMTOSD_UPDATE_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
135
        RMTOSD_UPDATE_LONGTEXT, true )
ivoire's avatar
ivoire committed
136
    add_bool( RMTOSD_CFG "vnc-polling", false, NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
137
              RMTOSD_POLL_TEXT , RMTOSD_POLL_LONGTEXT, false )
ivoire's avatar
ivoire committed
138
    add_bool( RMTOSD_CFG "mouse-events", false, NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
139
              RMTOSD_MOUSE_TEXT , RMTOSD_MOUSE_LONGTEXT, false )
ivoire's avatar
ivoire committed
140
    add_bool( RMTOSD_CFG "key-events", false, NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
141
              RMTOSD_KEYS_TEXT , RMTOSD_KEYS_LONGTEXT, false )
142
    add_integer_with_range( RMTOSD_CFG "alpha", 255, 0, 255, NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
143
        RMTOSD_ALPHA_TEXT, RMTOSD_ALPHA_LONGTEXT, true )
144

145
vlc_module_end ()
146

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
#define CHALLENGESIZE 16
#define MAX_VNC_SERVER_NAME_LENGTH 255

/* subfilter functions */
static subpicture_t *Filter( filter_t *, mtime_t );

static int MouseEvent ( vlc_object_t *p_this, char const *psz_var,
                        vlc_value_t oldval, vlc_value_t newval, void *p_data );

static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
                     vlc_value_t oldval, vlc_value_t newval, void *p_data );

static void stop_osdvnc ( filter_t *p_filter );

165
static void* vnc_worker_thread ( vlc_object_t *p_thread_obj );
166

167
static void* update_request_thread( vlc_object_t *p_thread_obj );
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

static bool open_vnc_connection ( filter_t *p_filter );

static bool handshaking ( filter_t *p_filter );

static bool process_server_message ( filter_t *p_filter,
                                     rfbServerToClientMsg *msg );

static inline void rgb_to_yuv( uint8_t *y, uint8_t *u, uint8_t *v,
                               int r, int g, int b );

static inline bool fill_rect( filter_sys_t* p_sys,
                              uint16_t i_x, uint16_t i_y,
                              uint16_t i_w, uint16_t i_h,
                              uint8_t i_color );
183 184 185 186 187
static inline bool copy_rect( filter_sys_t* p_sys,
                              uint16_t i_x, uint16_t i_y,
                              uint16_t i_w, uint16_t i_h,
                              uint16_t i_sx, uint16_t i_sy );

188 189 190 191 192 193 194 195

static inline bool raw_line(  filter_sys_t* p_sys,
                              uint16_t i_x, uint16_t i_y,
                              uint16_t i_w );

static void vnc_encrypt_bytes( unsigned char *bytes, char *passwd );


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
/*****************************************************************************
 * Sub filter code
 *****************************************************************************/

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
struct filter_sys_t
{
    bool          b_need_update;       /* VNC picture is updated, do update the OSD*/
    mtime_t       i_vnc_poll_interval; /* Update the OSD menu every n ms */

    uint8_t       i_alpha;             /* alpha transparency value */

    char          *psz_host;           /* VNC host */
    int           i_port;

    char          *psz_passwd;         /* VNC password */

    bool          b_vnc_poll;          /* Activate VNC polling ? */
    bool          b_vnc_mouse_events;  /* Send MouseEvents ? */
    bool          b_vnc_key_events;    /* Send KeyEvents ? */

    bool          b_connection_active; /* Handshaking finished ? */

    vlc_mutex_t   lock;                /* To lock for read/write on picture */

    picture_t     *p_pic;              /* The picture with OSD data from VNC */

    vout_thread_t *p_vout;             /* Pointer to video-out thread */

    int           i_socket;            /* Socket used for VNC */

    uint16_t      i_vnc_width;          /* The with of the VNC screen */
    uint16_t      i_vnc_height;         /* The height of the VNC screen */
	uint32_t      i_vnc_pixels;         /* The pixels of the VNC screen */

    bool    b_alpha_from_vnc;    /* Special ffnetdev alpha feature enabled ? */

    char          read_buffer[READ_BUFFER_SIZE];

    bool          b_continue;

    vlc_object_t* p_worker_thread;

    uint8_t       ar_color_table_yuv[256][4];
};

/*****************************************************************************
 * CreateFilter: Create the filter and open the definition file
 *****************************************************************************/
static int CreateFilter ( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;
    filter_sys_t *p_sys = NULL;

    msg_Dbg( p_filter, "Creating vnc osd filter..." );

254
    p_filter->p_sys = p_sys = calloc( 1, sizeof(*p_sys) );
255 256 257 258 259 260 261
    if( !p_filter->p_sys )
        return VLC_ENOMEM;

    /* Populating struct */
    vlc_mutex_init( &p_sys->lock );
    p_sys->b_continue = true;
    p_sys->i_socket = -1;
262
    p_sys->p_pic = NULL;
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

    p_sys->psz_host = var_CreateGetString( p_this, RMTOSD_CFG "host" );
    if( EMPTY_STR(p_sys->psz_host) )
    {
        msg_Err( p_filter, "unable to get vnc host" );
        goto error;
    }

    p_sys->psz_passwd = var_CreateGetString( p_this, RMTOSD_CFG "password" );
    if( !p_sys->psz_passwd )
    {
        msg_Err( p_filter, "unable to get vnc password" );
        goto error;
    }

    p_sys->i_port = var_CreateGetIntegerCommand( p_this, RMTOSD_CFG "port" );

    p_sys->i_alpha = var_CreateGetIntegerCommand( p_this, RMTOSD_CFG "alpha" );

Pavlov Konstantin's avatar
Pavlov Konstantin committed
282
    /* in milliseconds, 0 disables polling, should not be lower than 100 */
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 322
    p_sys->i_vnc_poll_interval  = var_CreateGetIntegerCommand( p_this,
                                                       RMTOSD_CFG "update" );
    if ( p_sys->i_vnc_poll_interval < 100)
    {
       p_sys->i_vnc_poll_interval = 100;
    }

    for ( int i = 0; i < 256; i++ )
    {
        p_sys->ar_color_table_yuv[i][0] = 255;
        p_sys->ar_color_table_yuv[i][1] = 255;
        p_sys->ar_color_table_yuv[i][2] = 255;
        p_sys->ar_color_table_yuv[i][3] = 255;
    }

    p_sys->b_vnc_poll = var_CreateGetBoolCommand( p_this,
                                            RMTOSD_CFG "vnc-polling" );
    p_sys->b_vnc_mouse_events = var_CreateGetBoolCommand( p_this,
                                            RMTOSD_CFG "mouse-events" );
    p_sys->b_vnc_key_events = var_CreateGetBoolCommand( p_this,
                                            RMTOSD_CFG "key-events" );

    /* Keep track of OSD Events */
    p_sys->b_need_update  = false;

    /* Attach subpicture filter callback */
    p_filter->pf_sub_filter = Filter;

    p_sys->p_vout = vlc_object_find( p_this, VLC_OBJECT_VOUT, FIND_PARENT );

    if( p_sys->p_vout )
    {
        var_AddCallback( p_sys->p_vout, "mouse-moved",
                         MouseEvent, p_this );
        var_AddCallback( p_sys->p_vout, "mouse-button-down",
                         MouseEvent, p_this );
        var_AddCallback( p_sys->p_vout->p_libvlc, "key-pressed",
                         KeyEvent, p_this );
    }

323
    es_format_Init( &p_filter->fmt_out, SPU_ES, VLC_CODEC_SPU );
324 325
    p_filter->fmt_out.i_priority = 0;

Rafaël Carré's avatar
Rafaël Carré committed
326 327
    vlc_gcrypt_init();

328
    /* create the vnc worker thread */
329 330
    p_sys->p_worker_thread = vlc_object_create( p_this,
                                                sizeof( vlc_object_t ) );
331 332
    vlc_object_attach( p_sys->p_worker_thread, p_this );
    if( vlc_thread_create( p_sys->p_worker_thread, "vnc worker thread",
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
333
                           vnc_worker_thread, VLC_THREAD_PRIORITY_LOW ) )
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
    {
        vlc_object_detach( p_sys->p_worker_thread );
        vlc_object_release( p_sys->p_worker_thread );
        msg_Err( p_filter, "cannot spawn vnc message reader thread" );
        goto error;
    }

    msg_Dbg( p_filter, "osdvnc filter started" );

    return VLC_SUCCESS;

error:
    msg_Err( p_filter, "osdvnc filter discarded" );

    stop_osdvnc( p_filter );

350
    vlc_mutex_destroy( &p_sys->lock );
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
    free( p_sys->psz_host );
    free( p_sys->psz_passwd );
    free( p_sys );

    return VLC_EGENERIC;
}

/*****************************************************************************
 * DestroyFilter: Make a clean exit of this plugin
 *****************************************************************************/
static void DestroyFilter( vlc_object_t *p_this )
{
    filter_t     *p_filter = (filter_t*)p_this;
    filter_sys_t *p_sys = p_filter->p_sys;

    msg_Dbg( p_filter, "DestroyFilter called." );

    stop_osdvnc( p_filter );

    if( p_sys->p_vout )
    {
        var_DelCallback( p_sys->p_vout, "mouse-moved",
                         MouseEvent, p_this );
        var_DelCallback( p_sys->p_vout, "mouse-button-down",
                         MouseEvent, p_this );
        var_DelCallback( p_sys->p_vout->p_libvlc, "key-pressed",
                         KeyEvent, p_this );

        vlc_object_release( p_sys->p_vout );
    }

    var_Destroy( p_this, RMTOSD_CFG "host" );
    var_Destroy( p_this, RMTOSD_CFG "port" );
    var_Destroy( p_this, RMTOSD_CFG "password" );
    var_Destroy( p_this, RMTOSD_CFG "update" );
    var_Destroy( p_this, RMTOSD_CFG "vnc-polling" );
    var_Destroy( p_this, RMTOSD_CFG "mouse-events" );
    var_Destroy( p_this, RMTOSD_CFG "key-events" );
    var_Destroy( p_this, RMTOSD_CFG "alpha" );

391
    vlc_mutex_destroy( &p_sys->lock );
392 393 394 395 396 397 398 399 400
    free( p_sys->psz_host );
    free( p_sys->psz_passwd );
    free( p_sys );
}

static void stop_osdvnc ( filter_t *p_filter )
{
    filter_sys_t *p_sys = p_filter->p_sys;

401 402
    /* It will unlock socket reading */
    vlc_object_kill( p_filter );
403

404 405
    /* */
    if( p_sys->p_worker_thread )
406 407
    {
        msg_Dbg( p_filter, "joining worker_thread" );
408
        vlc_object_kill( p_sys->p_worker_thread );
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 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 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
        vlc_thread_join( p_sys->p_worker_thread );
        vlc_object_detach( p_sys->p_worker_thread );
        vlc_object_release( p_sys->p_worker_thread );
        msg_Dbg( p_filter, "released worker_thread" );
    }

    msg_Dbg( p_filter, "osdvnc stopped" );
}

static bool read_exact( filter_t *p_filter,
                        int i_socket,
                        char* p_readbuf,
                        int i_bytes )
{
    return i_bytes == net_Read( p_filter, i_socket, NULL,
                                  (unsigned char*)p_readbuf,
                                  i_bytes, true );
}


static bool write_exact( filter_t *p_filter,
                         int i_socket,
                         char* p_writebuf,
                         int i_bytes )
{
    return i_bytes == net_Write( p_filter, i_socket, NULL,
                                  (unsigned char*)p_writebuf, i_bytes );
}

static bool open_vnc_connection ( filter_t *p_filter )
{
    filter_sys_t *p_sys = p_filter->p_sys;

    msg_Dbg( p_filter, "Open socket to vnc server on %s:%u.",
              p_sys->psz_host, p_sys->i_port );

    p_sys->i_socket = net_ConnectTCP( p_filter, p_sys->psz_host, p_sys->i_port );

    if( p_sys->i_socket < 0 )
    {
        msg_Err( p_filter, "Could not open socket" );
        return false;
    }

    msg_Dbg( p_filter, "socket is open." );

    return true;
}

static bool handshaking ( filter_t *p_filter )
{
    filter_sys_t *p_sys = p_filter->p_sys;

    msg_Dbg( p_filter, "Reading protocol version" );

    rfbProtocolVersionMsg pv;
    if ( !read_exact( p_filter, p_sys->i_socket, pv,
                      sz_rfbProtocolVersionMsg ) )
    {
        msg_Err( p_filter, "Could not read version message" );
        return false;
    }
    pv[sz_rfbProtocolVersionMsg] = '\0'; /* pv size is sz_rfbProtocolVersionMsg+1 */

    msg_Dbg( p_filter, "Server version is %s", pv );

    strncpy(pv, "RFB 003.003\n", sz_rfbProtocolVersionMsg);

    if( !write_exact(p_filter, p_sys->i_socket, pv,
                     sz_rfbProtocolVersionMsg) )
    {
        msg_Err( p_filter, "Could not write version message" );
        return false;
    }

    msg_Dbg( p_filter, "Reading authentication scheme" );
    uint32_t i_authScheme;
    if( !read_exact( p_filter, p_sys->i_socket, (char*)&i_authScheme, 4 ) )
    {
        msg_Err( p_filter, "Could not read authentication scheme" );
        return false;
    }
    i_authScheme = htonl(i_authScheme);

    msg_Dbg( p_filter, "Authentication scheme = %x", i_authScheme );
    if ( i_authScheme == rfbConnFailed )
    {
        msg_Err( p_filter, "Connection rejected by server" );
        return false;
    }
    if (i_authScheme == rfbVncAuth)
    {
        unsigned char challenge[CHALLENGESIZE];
        if ( !read_exact( p_filter, p_sys->i_socket,
                          (char*)challenge, CHALLENGESIZE ) )
        {
            msg_Err( p_filter, "Could not read password challenge" );
            return false;
        }

        vnc_encrypt_bytes( challenge, p_sys->psz_passwd );

        if( !write_exact(p_filter, p_sys->i_socket,
                         (char*)challenge, CHALLENGESIZE ) )
        {
            msg_Err( p_filter, "Could not write password" );
            return false;
        }
        uint32_t i_authResult;
        if( !read_exact( p_filter, p_sys->i_socket, (char*)&i_authResult, 4 ) )
        {
            msg_Err( p_filter, "Could not read authentication result" );
            return false;
        }
        i_authResult = htonl(i_authResult);
        if (i_authResult != rfbVncAuthOK)
        {
            msg_Err( p_filter, "VNC authentication failed" );
            return false;
        }
    }

    msg_Dbg( p_filter, "Writing client init message" );
    rfbClientInitMsg ci;
    ci.shared = 1;
    if( !write_exact( p_filter, p_sys->i_socket,
                      (char*)&ci, sz_rfbClientInitMsg ) )
    {
        msg_Err( p_filter, "Could not write client init message" );
        return false;
    }

    msg_Dbg( p_filter, "Reading server init message" );
    rfbServerInitMsg si;
    if( !read_exact( p_filter, p_sys->i_socket,
                     (char*)&si, sz_rfbServerInitMsg ) )
    {
        msg_Err( p_filter, "Could not read server init message" );
        return false;
    }
    si.framebufferWidth = htons(si.framebufferWidth);
    si.framebufferHeight = htons(si.framebufferHeight);
    si.format.redMax = htons(si.format.redMax);
    si.format.greenMax = htons(si.format.greenMax);
    si.format.blueMax = htons(si.format.blueMax);

    p_sys->i_vnc_width = si.framebufferWidth;
    p_sys->i_vnc_height = si.framebufferHeight;

    msg_Dbg( p_filter, "Servers preferred pixelformat: "
                        "%ux%u, R(%u),G(%u),B(%u), %u bit, depht=%u, %s",
                        si.framebufferWidth,
                        si.framebufferHeight,
                        si.format.redMax,
                        si.format.greenMax,
                        si.format.blueMax,
                        si.format.bitsPerPixel,
                        si.format.depth,
                        si.format.trueColour ? "TrueColor" : "Not-TrueColor");

    uint32_t i_nameLength = htonl(si.nameLength);
    if( i_nameLength > MAX_VNC_SERVER_NAME_LENGTH )
    {
        msg_Err( p_filter, "Server name too long" );
        return false;
    }
    char s_ServerName[MAX_VNC_SERVER_NAME_LENGTH+1];

    msg_Dbg( p_filter, "Reading server name with size = %u", i_nameLength );
    if( !read_exact( p_filter, p_sys->i_socket, s_ServerName, i_nameLength ) )
    {
        msg_Err( p_filter, "Could not read server name" );
        return false;
    }
    s_ServerName[i_nameLength] = '\0';

    if( strcmp( s_ServerName, "VDR-OSD") == 0 )
    {
        msg_Dbg( p_filter, "Server is a VDR" );
        p_sys->b_alpha_from_vnc = true;
    }
    else
    {
        msg_Dbg( p_filter, "Server is a normal VNC" );
        p_sys->b_alpha_from_vnc = false;
    }


    msg_Dbg( p_filter, "Server init message read properly" );
    msg_Dbg( p_filter, "Server name is %s", s_ServerName );

    msg_Dbg( p_filter, "Writing SetPixelFormat message" );

    rfbSetPixelFormatMsg sp;
    sp.type = rfbSetPixelFormat;
604
    sp.pad1 = sp.pad2 = 0;
605 606 607 608 609 610 611 612 613 614
    sp.format.bitsPerPixel = 8;
    sp.format.depth = 8 ;
    sp.format.bigEndian = 1;
    sp.format.trueColour = 0;
    sp.format.redMax = htons(31);
    sp.format.greenMax = htons(31);
    sp.format.blueMax = htons(31);
    sp.format.redShift = 10;
    sp.format.greenShift = 5;
    sp.format.blueShift = 0;
615
    sp.format.pad1 = sp.format.pad2 = 0;
616 617 618 619 620 621 622 623 624 625 626 627

    if( !write_exact( p_filter, p_sys->i_socket,
                      (char*)&sp, sz_rfbSetPixelFormatMsg) )
    {
        msg_Err( p_filter, "Could not write SetPixelFormat message" );
        return false;
    }

    msg_Dbg( p_filter, "Writing SetEncodings message" );

    rfbSetEncodingsMsg se;
    se.type = rfbSetEncodings;
628
    se.pad = 0;
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 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
    se.nEncodings = htons( p_sys->b_alpha_from_vnc ? 3 : 2 );

    if( !write_exact( p_filter, p_sys->i_socket,
                      (char*)&se, sz_rfbSetEncodingsMsg) )
    {
        msg_Err( p_filter, "Could not write SetEncodings message begin" );
        return false;
    }

    uint32_t i_encoding;

    msg_Dbg( p_filter, "Writing SetEncodings rfbEncodingCopyRect" );
    i_encoding = htonl(rfbEncodingCopyRect);
    if( !write_exact( p_filter, p_sys->i_socket, (char*)&i_encoding, 4) )
    {
        msg_Err( p_filter, "Could not write encoding type rfbEncodingCopyRect." );
        return false;
    }

    msg_Dbg( p_filter, "Writing SetEncodings rfbEncodingRRE" );
    i_encoding = htonl(rfbEncodingRRE);
    if( !write_exact(p_filter, p_sys->i_socket, (char*)&i_encoding, 4) )
    {
        msg_Err( p_filter, "Could not write encoding type rfbEncodingRRE." );
        return false;
    }

    if( p_sys->b_alpha_from_vnc )
    {
        msg_Dbg( p_filter, "Writing SetEncodings rfbEncSpecialUseAlpha" );
        i_encoding = 0x00F0FFFF; /* rfbEncSpecialUseAlpha is 0xFFFFF000
                                  * before we swap it */
        if( !write_exact(p_filter, p_sys->i_socket, (char*)&i_encoding, 4) )
        {
            msg_Err( p_filter, "Could not write encoding type rfbEncSpecialUseAlpha." );
            return false;
        }
    }
    return true;

}

671
static void* vnc_worker_thread( vlc_object_t *p_thread_obj )
672 673 674
{
    filter_t* p_filter = (filter_t*)(p_thread_obj->p_parent);
    filter_sys_t *p_sys = p_filter->p_sys;
675
    vlc_object_t *p_update_request_thread;
676
    int canc = vlc_savecancel ();
677 678 679

    msg_Dbg( p_filter, "VNC worker thread started" );

680
    if( !open_vnc_connection ( p_filter ) )
681 682
    {
        msg_Err( p_filter, "Could not connect to vnc host" );
683
        goto exit;
684 685
    }

686
    if( !handshaking ( p_filter ) )
687 688
    {
        msg_Err( p_filter, "Error occured while handshaking vnc host" );
689
        goto exit;
690 691 692 693 694 695 696
    }

    p_sys->b_connection_active = true; /* to enable sending key
                                            * and mouse events to host */

    /* Create an empty picture for VNC the data */
    vlc_mutex_lock( &p_sys->lock );
697
    p_sys->p_pic = picture_New( VLC_CODEC_YUVA,
698
                                p_sys->i_vnc_width, p_sys->i_vnc_height, VOUT_ASPECT_FACTOR );
699 700 701
    if( !p_sys->p_pic )
    {
        vlc_mutex_unlock( &p_sys->lock );
702
        goto exit;
703 704 705 706 707 708
    }
	p_sys->i_vnc_pixels = p_sys->i_vnc_width * p_sys->i_vnc_height;

    vlc_mutex_unlock( &p_sys->lock );

    /* create the update request thread */
709 710 711 712
    p_update_request_thread = vlc_object_create( p_filter,
                                                 sizeof( vlc_object_t ) );
    vlc_object_attach( p_update_request_thread, p_filter );
    if( vlc_thread_create( p_update_request_thread,
713
                           "vnc update request thread",
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
714
                           update_request_thread, VLC_THREAD_PRIORITY_LOW ) )
715
    {
716 717
        vlc_object_detach( p_update_request_thread );
        vlc_object_release( p_update_request_thread );
718
        msg_Err( p_filter, "cannot spawn vnc update request thread" );
719
        goto exit;
720 721 722
    }

    /* connection is initialized, now read and handle server messages */
723 724 725 726 727 728
    while( vlc_object_alive( p_thread_obj ) )
    {
        rfbServerToClientMsg msg;
        int i_msgSize;

        memset( &msg, 0, sizeof(msg) );
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
        if( !read_exact(p_filter, p_sys->i_socket, (char*)&msg, 1 ) )
        {
            msg_Err( p_filter, "Error while waiting for next server message");
            break;
        }
        switch (msg.type)
        {
        case rfbFramebufferUpdate:
            i_msgSize = sz_rfbFramebufferUpdateMsg;
            break;
        case rfbSetColourMapEntries:
            i_msgSize = sz_rfbSetColourMapEntriesMsg;
            break;
        case rfbBell:
            i_msgSize = sz_rfbBellMsg;
            break;
        case rfbServerCutText:
            i_msgSize = sz_rfbServerCutTextMsg;
            break;
        case rfbReSizeFrameBuffer:
            i_msgSize = sz_rfbReSizeFrameBufferMsg;
            break;
        default:
            i_msgSize = 0;
            msg_Err( p_filter, "Invalid message %u received", msg.type );
            break;
        }
757

758 759
        if( i_msgSize <= 0 )
            break;
760

761 762 763 764 765 766 767
        if( --i_msgSize > 0 )
        {
            if ( !read_exact( p_filter, p_sys->i_socket,
                              ((char*)&msg)+1, i_msgSize ) )
            {
                msg_Err( p_filter, "Error while reading message of type %u",
                         msg.type );
768
                break;
769 770 771
            }
        }
        process_server_message( p_filter, &msg);
772 773
    }

774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
    msg_Dbg( p_filter, "joining update_request_thread" );
    vlc_object_kill( p_update_request_thread );
    vlc_thread_join( p_update_request_thread );
    vlc_object_detach( p_update_request_thread );
    vlc_object_release( p_update_request_thread );
    msg_Dbg( p_filter, "released update_request_thread" );

exit:

    vlc_mutex_lock( &p_sys->lock );
    p_sys->b_connection_active = false;

    if (p_sys->i_socket >= 0)
        net_Close(p_sys->i_socket);

    if( p_sys->p_pic )
        picture_Release( p_sys->p_pic );

    /* It will hide the subtitle */
    p_sys->b_continue = false;
    p_sys->b_need_update = true;
    vlc_mutex_unlock( &p_sys->lock );
796

797
    msg_Dbg( p_filter, "VNC message reader thread ended" );
798
    vlc_restorecancel (canc);
799
    return NULL;
800 801
}

802
static void* update_request_thread( vlc_object_t *p_thread_obj )
803 804 805
{
    filter_t* p_filter = (filter_t*)(p_thread_obj->p_parent);
    filter_sys_t *p_sys = p_filter->p_sys;
806
    int canc = vlc_savecancel ();
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822

    msg_Dbg( p_filter, "VNC update request thread started" );

    rfbFramebufferUpdateRequestMsg udr;
    udr.type = rfbFramebufferUpdateRequest;
    udr.incremental = 0;
    udr.x = 0;
    udr.y = 0;
    udr.w = htons(p_sys->i_vnc_width);
    udr.h = htons(p_sys->i_vnc_height);

    if( write_exact(p_filter, p_sys->i_socket, (char*)&udr,
           sz_rfbFramebufferUpdateRequestMsg) == false)
    {
        msg_Err( p_filter, "Could not write rfbFramebufferUpdateRequestMsg." );
        p_sys->b_continue = false;
823
        return NULL;
824 825 826 827 828
    }

    udr.incremental = 1;
    mtime_t i_poll_interval_microsec = p_sys->i_vnc_poll_interval * 1000;

829
    if( p_sys->b_vnc_poll)
830
    {
831
        while( vlc_object_alive( p_thread_obj ) )
832 833 834 835 836 837
        {
            msleep( i_poll_interval_microsec );
            if( write_exact(p_filter, p_sys->i_socket, (char*)&udr,
                   sz_rfbFramebufferUpdateRequestMsg) == false)
            {
                msg_Err( p_filter, "Could not write rfbFramebufferUpdateRequestMsg." );
838
                break;
839 840
            }
        }
841
        p_sys->b_continue = false;
842 843 844 845 846
    }
    else
    {
        msg_Dbg( p_filter, "VNC polling disabled." );
    }
847

848
    msg_Dbg( p_filter, "VNC update request thread ended" );
849
    vlc_restorecancel (canc);
850
    return NULL;
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
}

static bool process_server_message ( filter_t *p_filter,
                                     rfbServerToClientMsg *msg )
{
    filter_sys_t *p_sys = p_filter->p_sys;

    switch (msg->type)
    {
    case rfbFramebufferUpdate:
        {
            msg->fu.nRects = htons(msg->fu.nRects);
            rfbFramebufferUpdateRectHeader hdr;

            for (int i_rect = 0; i_rect < msg->fu.nRects; i_rect++)
            {
                if (!read_exact(p_filter, p_sys->i_socket, (char*)&hdr,
                    sz_rfbFramebufferUpdateRectHeader ) )
                {
                    msg_Err( p_filter, "Could not read FrameBufferUpdate header" );
                    return false;
                }
                hdr.r.x = htons(hdr.r.x);
                hdr.r.y = htons(hdr.r.y);
                hdr.r.w = htons(hdr.r.w);
                hdr.r.h = htons(hdr.r.h);
                hdr.encoding = htonl(hdr.encoding);

                switch (hdr.encoding)
                {
                case rfbEncodingRaw:
                    {
                        int i_line;
                        for (i_line = 0; i_line < hdr.r.h; i_line++)
                        {
                            if ( !read_exact( p_filter, p_sys->i_socket,
                                    p_sys->read_buffer, hdr.r.w ) )
                            {
                                msg_Err( p_filter,
                                 "Could not read FrameBufferUpdate line data" );
                               return false;
                            }
                            vlc_mutex_lock( &p_sys->lock );
                            if ( !raw_line( p_sys, hdr.r.x,
                                            hdr.r.y + i_line,
                                            hdr.r.w ) )
                            {
                                msg_Err( p_filter, "raw_line failed." );
                                vlc_mutex_unlock( &p_sys->lock );
                                return false;
                            }
                            vlc_mutex_unlock( &p_sys->lock );
                        }
                    }
                    break;

                case rfbEncodingCopyRect:
                    {
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
                        rfbCopyRect rect;

                        if ( !read_exact( p_filter, p_sys->i_socket,
                                          (char*)&rect,
                                          sz_rfbCopyRect ) )
                        {
                            msg_Err( p_filter, "Could not read rfbCopyRect" );
                            return false;
                        }
                        rect.srcX = htons( rect.srcX );
                        rect.srcY = htons( rect.srcY );

                        vlc_mutex_lock( &p_sys->lock );
                        if ( !copy_rect( p_sys,
                                         hdr.r.x,   hdr.r.y,
                                         hdr.r.w,   hdr.r.h,
                                         rect.srcX, rect.srcY ) )
                        {
                            msg_Err( p_filter, "copy_rect failed." );
                            vlc_mutex_unlock( &p_sys->lock );
                            return false;
                        }
                        vlc_mutex_unlock( &p_sys->lock );
932
                    }
933
                    break;
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136

                case rfbEncodingRRE:
                    {
                        rfbRREHeader rrehdr;
                        if ( !read_exact( p_filter, p_sys->i_socket,
                                          (char*)&rrehdr,
                                          sz_rfbRREHeader ) )
                        {
                            msg_Err( p_filter, "Could not read rfbRREHeader" );
                            return false;
                        }
                        uint8_t i_pixcolor;
                        if ( !read_exact(p_filter, p_sys->i_socket,
                                         (char*)&i_pixcolor, 1 ) )
                        {
                            msg_Err( p_filter, "Could not read RRE pixcolor" );
                            return false;
                        }

                        vlc_mutex_lock( &p_sys->lock );
                        if ( !fill_rect( p_sys,
                                        hdr.r.x, hdr.r.y,
                                        hdr.r.w, hdr.r.h,
                                        i_pixcolor) )
                        {
                            msg_Err( p_filter, "main fill_rect failed." );
                            vlc_mutex_unlock( &p_sys->lock );
                            return false;
                        }
                        vlc_mutex_unlock( &p_sys->lock );

                        rrehdr.nSubrects = htonl(rrehdr.nSubrects);

                        int i_datasize = rrehdr.nSubrects *
                                     ( sizeof(i_pixcolor) + sz_rfbRectangle ) ;
                        if ( i_datasize > READ_BUFFER_SIZE )
                        {
                            msg_Err( p_filter, "Buffer too small, "
                                     "need %u bytes", i_datasize );
                            return false;
                        }
                        if ( !read_exact( p_filter, p_sys->i_socket,
                                       p_sys->read_buffer, i_datasize ) )
                        {
                            msg_Err( p_filter,
                                     "Could not read RRE subrect data" );
                            return false;
                        }

                        uint32_t i_subrect;
                        rfbRectangle* p_subrect;
                        int i_offset = 0;
                        vlc_mutex_lock( &p_sys->lock );
                        for ( i_subrect = 0;
                              i_subrect < rrehdr.nSubrects; i_subrect++)
                        {
                            i_pixcolor = p_sys->read_buffer[i_offset];
                            i_offset += sizeof(i_pixcolor);
                            p_subrect =
                               (rfbRectangle*)(p_sys->read_buffer + i_offset);
                            i_offset += sz_rfbRectangle;

                            if (!fill_rect( p_sys,
                                            htons(p_subrect->x) + hdr.r.x,
                                            htons(p_subrect->y) + hdr.r.y,
                                            htons(p_subrect->w),
                                            htons(p_subrect->h),
                                            i_pixcolor) )
                            {
                                msg_Err( p_filter,
                                    "subrect %u fill_rect failed.", i_subrect );
                                vlc_mutex_unlock( &p_sys->lock );
                                return false;
                            }
                        }
                        vlc_mutex_unlock( &p_sys->lock );
                    }
                    break;

                }

            }
            vlc_mutex_lock( &p_sys->lock );
            p_sys->b_need_update = true;
            vlc_mutex_unlock( &p_sys->lock );
        }
        return true;

    case rfbSetColourMapEntries:
        {
            msg->scme.nColours = htons(msg->scme.nColours);
            msg->scme.firstColour = htons(msg->scme.firstColour);
            int i_datasize;
            if ( p_sys->b_alpha_from_vnc == true )
            {
                i_datasize = 2 * msg->scme.nColours * 4;
            }
            else
            {
                i_datasize = 2 * msg->scme.nColours * 3;
            }
            if ( i_datasize > READ_BUFFER_SIZE )
            {
                msg_Err( p_filter, "Buffer too small, need %u bytes",
                                   i_datasize );
                return false;
            }

            if ( !read_exact( p_filter, p_sys->i_socket,
                              p_sys->read_buffer, i_datasize ) )
            {
                msg_Err( p_filter, "Could not read color map data" );
                return false;
            }

            uint8_t i_red, i_green, i_blue, i_alpha, i_color_index;
            uint16_t i_offset = 0;
            i_alpha = 255;

            for (int i = 0; i < msg->scme.nColours; i++)
            {
                i_color_index = i+msg->scme.firstColour;
                if ( p_sys->b_alpha_from_vnc == true )
                {
                    i_alpha = p_sys->read_buffer[i_offset];
                    i_offset += 2;
                }
                i_red   = p_sys->read_buffer[i_offset];
                i_offset += 2;
                i_green = p_sys->read_buffer[i_offset];
                i_offset += 2;
                i_blue  = p_sys->read_buffer[i_offset];
                i_offset += 2;
                rgb_to_yuv( &p_sys->ar_color_table_yuv[i_color_index][0],
                            &p_sys->ar_color_table_yuv[i_color_index][1],
                            &p_sys->ar_color_table_yuv[i_color_index][2],
                            i_red,
                            i_green,
                            i_blue );
                p_sys->ar_color_table_yuv[i][3] = i_alpha;
            }
        }
        return true;

    case rfbBell:
        msg_Err( p_filter, "rfbBell received" );
        return true;

    case rfbServerCutText:
        msg->sct.length = htons(msg->sct.length);
        if ( msg->sct.length > READ_BUFFER_SIZE )
        {
            msg_Err( p_filter, "Buffer too small, need %u bytes", msg->sct.length );
            return false;
        }
        if ( !read_exact(p_filter, p_sys->i_socket,
                         p_sys->read_buffer, msg->sct.length ) )
        {
            msg_Err( p_filter, "Could not read Reading rfbServerCutText data" );
            return false;
        }
        return true;

    case rfbReSizeFrameBuffer:
        msg_Err( p_filter, "Reading rfbReSizeFrameBuffer not implemented" );
        return false;

    default:
        msg_Err( p_filter, "Invalid message %u received", msg->type );
        return false;
    }
    return false;
}

/****************************************************************************
 * Filter: the whole thing
 ****************************************************************************
 * This function outputs subpictures at regular time intervals.
 ****************************************************************************/
static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
{
    filter_sys_t *p_sys = p_filter->p_sys;
    subpicture_t *p_spu;
    subpicture_region_t *p_region;
    video_format_t fmt;
    picture_t *p_pic;

    if( !p_sys->b_need_update )
    {
        return NULL;
    }

    vlc_mutex_lock( &p_sys->lock );

    p_pic = p_sys->p_pic;

    if( !p_pic )
    {
        vlc_mutex_unlock( &p_sys->lock );
        return NULL;
    }

    /* Allocate the subpicture internal data. */
Laurent Aimar's avatar
Laurent Aimar committed
1137
    p_spu = filter_NewSubpicture( p_filter );
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
    if( !p_spu )
    {
        vlc_mutex_unlock( &p_sys->lock );
        return NULL;
    }

    p_spu->b_absolute = false;
    p_spu->i_start = date;
    p_spu->i_stop = 0;
    p_spu->b_ephemer = true;

1149 1150 1151
    if( !p_sys->b_continue )
        p_spu->i_stop = p_spu->i_start + 1;

1152 1153
    /* Create new SPU region */
    memset( &fmt, 0, sizeof(video_format_t) );
1154
    fmt.i_chroma = VLC_CODEC_YUVA;
1155 1156 1157 1158 1159
    fmt.i_aspect = VOUT_ASPECT_FACTOR;
    fmt.i_sar_num = fmt.i_sar_den = 1;
    fmt.i_width = fmt.i_visible_width = p_pic->p[Y_PLANE].i_visible_pitch;
    fmt.i_height = fmt.i_visible_height = p_pic->p[Y_PLANE].i_visible_lines;
    fmt.i_x_offset = fmt.i_y_offset = 0;
1160
    p_region = subpicture_region_New( &fmt );
1161 1162 1163 1164 1165 1166 1167 1168
    if( !p_region )
    {
        msg_Err( p_filter, "cannot allocate SPU region" );
        p_filter->pf_sub_buffer_del( p_filter, p_spu );
        vlc_mutex_unlock( &p_sys->lock );
        return NULL;
    }

1169
    /* FIXME the copy is probably not needed anymore */
1170
    picture_Copy( p_region->p_picture, p_pic );
1171 1172 1173 1174 1175 1176

    p_sys->b_need_update = false;

    vlc_mutex_unlock( &p_sys->lock );

    /* set to one of the 9 relative locations */
1177
    p_region->i_align = 0; /* Center */
1178 1179 1180
    p_spu->b_absolute = false;


1181 1182
    p_spu->i_original_picture_width = 0; /*Let vout core do the horizontal scaling */
    p_spu->i_original_picture_height = fmt.i_height;
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234

    p_spu->p_region = p_region;

    p_spu->i_alpha = ( p_sys->i_alpha );

    return p_spu;
}


static inline void rgb_to_yuv( uint8_t *y, uint8_t *u, uint8_t *v,
                               int r, int g, int b )
{
    *y = ( ( (  66 * r + 129 * g +  25 * b + 128 ) >> 8 ) + 16 );
    *u =   ( ( -38 * r -  74 * g + 112 * b + 128 ) >> 8 ) + 128 ;
    *v =   ( ( 112 * r -  94 * g -  18 * b + 128 ) >> 8 ) + 128 ;
}

static inline bool fill_rect( filter_sys_t* p_sys,
                              uint16_t i_x, uint16_t i_y,
                              uint16_t i_w, uint16_t i_h,
                              uint8_t i_color)
{
    plane_t *p_outY = p_sys->p_pic->p+Y_PLANE;
    plane_t *p_outU = p_sys->p_pic->p+U_PLANE;
    plane_t *p_outV = p_sys->p_pic->p+V_PLANE;
    plane_t *p_outA = p_sys->p_pic->p+A_PLANE;
    int i_pitch = p_outY->i_pitch;
    int i_lines = p_outY->i_lines;
    if ( i_x + i_w > i_pitch)
        return false;
    if ( i_y + i_h > i_lines)
        return false;
    int i_line_offset = i_y * i_pitch;
    uint8_t i_yuv_y = p_sys->ar_color_table_yuv[i_color][0];
    uint8_t i_yuv_u = p_sys->ar_color_table_yuv[i_color][1];
    uint8_t i_yuv_v = p_sys->ar_color_table_yuv[i_color][2];
    uint8_t i_alpha = p_sys->ar_color_table_yuv[i_color][3];
    for( int i_line = 0; i_line < i_h; i_line++ )
    {
        for( int i_column = 0; i_column < i_w; i_column++ )
        {
            int i_total_offset = i_line_offset + i_x + i_column;
            p_outY->p_pixels[ i_total_offset ] = i_yuv_y;
            p_outU->p_pixels[ i_total_offset ] = i_yuv_u;
            p_outV->p_pixels[ i_total_offset ] = i_yuv_v;
            p_outA->p_pixels[ i_total_offset ] = i_alpha;
        }
        i_line_offset += i_pitch;
    }
    return true;
}

1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
static inline bool copy_rect( filter_sys_t* p_sys,
                              uint16_t i_x, uint16_t i_y,
                              uint16_t i_w, uint16_t i_h,
                              uint16_t i_sx, uint16_t i_sy )
{
    plane_t *p_Y = p_sys->p_pic->p+Y_PLANE;
    plane_t *p_U = p_sys->p_pic->p+U_PLANE;
    plane_t *p_V = p_sys->p_pic->p+V_PLANE;
    plane_t *p_A = p_sys->p_pic->p+A_PLANE;

    int i_pitch = p_Y->i_pitch;
    int i_lines = p_Y->i_lines;

    fprintf( stderr, "copy_rect: (%d,%d)+(%d,%d) -> (%d,%d)\n", i_x, i_y, i_w, i_h, i_sx, i_sy );

    if( i_x + i_w > i_pitch || i_sx + i_w > i_pitch )
        return false;
    if( i_y + i_h > i_lines || i_sy + i_h > i_lines)
        return false;

    if( i_w <= 0 || i_h <= 0 )
        return true;

    uint8_t *pb_buffer = calloc( i_w * i_h, 4 );
    if( !pb_buffer )
        return false;

    for( int i_line = 0; i_line < i_h; i_line++ )
    {
        for( int i_column = 0; i_column < i_w; i_column++ )
        {
            const int i_src_offset = ( i_sy + i_line ) * i_pitch + i_sx + i_column;
            const int i_tmp_offset = (    0 + i_line ) *     i_w +    0 + i_column;

            pb_buffer[4*i_tmp_offset + 0] = p_Y->p_pixels[i_src_offset];
            pb_buffer[4*i_tmp_offset + 1] = p_U->p_pixels[i_src_offset];
            pb_buffer[4*i_tmp_offset + 2] = p_V->p_pixels[i_src_offset];
            pb_buffer[4*i_tmp_offset + 3] = p_A->p_pixels[i_src_offset];
        }
    }

    for( int i_line = 0; i_line < i_h; i_line++ )
    {
        for( int i_column = 0; i_column < i_w; i_column++ )
        {
            const int i_tmp_offset = (   0 + i_line ) *     i_w +   0 + i_column;
            const int i_dst_offset = ( i_y + i_line ) * i_pitch + i_x + i_column;

            p_Y->p_pixels[i_dst_offset] = pb_buffer[4*i_tmp_offset + 0];
            p_U->p_pixels[i_dst_offset] = pb_buffer[4*i_tmp_offset + 1];
            p_V->p_pixels[i_dst_offset] = pb_buffer[4*i_tmp_offset + 2];
            p_A->p_pixels[i_dst_offset] = pb_buffer[4*i_tmp_offset + 3];
        }
    }
    free( pb_buffer );
    return true;

}

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
static inline bool raw_line(  filter_sys_t* p_sys,
                              uint16_t i_x, uint16_t i_y,
                              uint16_t i_w )
{
    plane_t *p_outY = p_sys->p_pic->p+Y_PLANE;
    plane_t *p_outU = p_sys->p_pic->p+U_PLANE;
    plane_t *p_outV = p_sys->p_pic->p+V_PLANE;
    plane_t *p_outA = p_sys->p_pic->p+A_PLANE;
    int i_pitch = p_outY->i_pitch;
    int i_lines = p_outY->i_lines;
    if ( i_x + i_w > i_pitch)
        return false;
    if ( i_y > i_lines)
        return false;

    int i_line_offset = i_y * i_pitch + i_x;

    for( int i_column = 0; i_column < i_w; i_column++ )
    {
        int i_offset = i_line_offset + i_column;
        uint8_t i_color = p_sys->read_buffer[i_column];
        p_outY->p_pixels[ i_offset ] = p_sys->ar_color_table_yuv[i_color][0];
        p_outU->p_pixels[ i_offset ] = p_sys->ar_color_table_yuv[i_color][1];
        p_outV->p_pixels[ i_offset ] = p_sys->ar_color_table_yuv[i_color][2];
        p_outA->p_pixels[ i_offset ] = p_sys->ar_color_table_yuv[i_color][3];
    }

    return true;
}


/*****************************************************************************
 * MouseEvent: callback for mouse events
 *****************************************************************************/
static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    VLC_UNUSED(oldval); VLC_UNUSED(newval); VLC_UNUSED(psz_var);

    filter_t *p_filter = (filter_t *)p_data;
    filter_sys_t *p_sys = p_filter->p_sys;

    if( !p_sys->b_vnc_mouse_events )
        return VLC_SUCCESS;

    vout_thread_t *p_vout = (vout_thread_t*)p_sys->p_vout;
    int i_x, i_y;
    int i_v;


    i_v = var_GetInteger( p_sys->p_vout, "mouse-button-down" );
    i_y = var_GetInteger( p_sys->p_vout, "mouse-y" );
    i_x = var_GetInteger( p_sys->p_vout, "mouse-x" );

1348 1349 1350 1351 1352 1353 1354 1355
    vlc_mutex_lock( &p_sys->lock );

    const int v_h = p_vout->fmt_in.i_visible_height;
    const int v_w = p_sys->i_vnc_width * v_h / p_sys->i_vnc_height;
    const int v_x = (p_vout->fmt_in.i_visible_width-v_w)/2;

    i_x -= v_x;

1356 1357
    if( i_y < 0 || i_x < 0 || i_y >= v_h || i_x >= v_w )
    {
1358 1359 1360
        vlc_mutex_unlock( &p_sys->lock );
        msg_Dbg( p_this, "invalid mouse event? x=%d y=%d btn=%x", i_x, i_y, i_v );
        return VLC_SUCCESS;
1361
    }
1362 1363 1364 1365 1366 1367
    if( !p_sys->b_connection_active )
    {
        vlc_mutex_unlock( &p_sys->lock );
        return VLC_SUCCESS;
    }

1368 1369 1370 1371 1372 1373 1374 1375
#ifdef VNC_DEBUG
    msg_Dbg( p_this, "mouse event x=%d y=%d btn=%x", i_x, i_y, i_v );
#endif

    /* */
    i_x = i_x * p_sys->i_vnc_width / v_w;
    i_y = i_y * p_sys->i_vnc_height / v_h;

1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
    /* buttonMask bits 0-7 are buttons 1-8, 0=up, 1=down */
    rfbPointerEventMsg ev;
    ev.type = rfbPointerEvent;
    ev.buttonMask = i_v;
    ev.x = htons(i_x);
    ev.y = htons(i_y);

    write_exact( p_filter, p_sys->i_socket,
                 (char*)&ev, sz_rfbPointerEventMsg);

1386 1387
    vlc_mutex_unlock( &p_sys->lock );

1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
    return VLC_SUCCESS;
}

/*****************************************************************************
 * KeyEvent: callback for keyboard events
 *****************************************************************************/
static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
                     vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    VLC_UNUSED(psz_var); VLC_UNUSED(oldval);

    filter_t *p_filter = (filter_t *)p_data;
    filter_sys_t *p_sys = p_filter->p_sys;

    if( !p_sys->b_vnc_key_events )
        return VLC_SUCCESS;

    msg_Dbg( p_this, "key pressed (%d) ", newval.i_int );

    if ( !newval.i_int )
    {
        msg_Err( p_this, "Received invalid key event %d", newval.i_int );
        return VLC_EGENERIC;
    }

1413 1414 1415 1416 1417 1418 1419
    vlc_mutex_lock( &p_sys->lock );
    if( !p_sys->b_connection_active )
    {
        vlc_mutex_unlock( &p_sys->lock );
        return VLC_SUCCESS;
    }

1420 1421 1422
    uint32_t i_key32 = newval.i_int;
    i_key32 = htonl(i_key32);
    rfbKeyEventMsg ev;
1423

1424 1425
    ev.type = rfbKeyEvent;
    ev.down = 1;
1426
    ev.pad = 0;
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477

    /* first key-down for modifier-keys */
    if (newval.i_int & KEY_MODIFIER_CTRL)
    {
        ev.key = 0xffe3;
        write_exact( p_filter, p_sys->i_socket,
                     (char*)&ev, sz_rfbKeyEventMsg);
    }
    if (newval.i_int & KEY_MODIFIER_SHIFT)
    {
        ev.key = 0xffe1;
        write_exact( p_filter, p_sys->i_socket,
                     (char*)&ev, sz_rfbKeyEventMsg);
    }
    if (newval.i_int & KEY_MODIFIER_ALT)
    {
        ev.key = 0xffe9;
        write_exact( p_filter, p_sys->i_socket,
                     (char*)&ev, sz_rfbKeyEventMsg);
    }

    /* then key-down for the pressed key */
    ev.key = i_key32;
    write_exact( p_filter, p_sys->i_socket,
                 (char*)&ev, sz_rfbKeyEventMsg);

    ev.down = 0;

    /* then key-up for the pressed key */
    write_exact( p_filter, p_sys->i_socket,
                 (char*)&ev, sz_rfbKeyEventMsg);

    /* last key-down for modifier-keys */
    if (newval.i_int & KEY_MODIFIER_CTRL)
    {
        ev.key = 0xffe3;
        write_exact( p_filter, p_sys->i_socket,
                     (char*)&ev, sz_rfbKeyEventMsg);
    }
    if (newval.i_int & KEY_MODIFIER_SHIFT)
    {
        ev.key = 0xffe1;
        write_exact( p_filter, p_sys->i_socket,
                     (char*)&ev, sz_rfbKeyEventMsg);
    }
    if (newval.i_int & KEY_MODIFIER_ALT)
    {
       ev.key = 0xffe9;
       write_exact( p_filter, p_sys->i_socket,
                    (char*)&ev, sz_rfbKeyEventMsg);
    }
1478
    vlc_mutex_unlock( &p_sys->lock );
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509

    return VLC_SUCCESS;
}

static void vnc_encrypt_bytes( unsigned char *bytes, char *passwd )
{
    unsigned char key[8];
    unsigned int i;

    for (i = 0; i < 8; i++)
        key[i] = i < strlen( passwd ) ? passwd[i] : '\0';

    gcry_cipher_hd_t ctx;
    gcry_cipher_open( &ctx, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB,0);

    /* reverse bits of the key */
    for( i = 0 ; i < 8 ; i ++ )
        key[i] =
            (key[i] >> 7) +
            (((key[i] >> 6) & 0x01 ) << 1 ) +
            (((key[i] >> 5) & 0x01 ) << 2 ) +
            (((key[i] >> 4) & 0x01 ) << 3 ) +
            (((key[i] >> 3) & 0x01 ) << 4 ) +
            (((key[i] >> 2) & 0x01 ) << 5 ) +
            (((key[i] >> 1) & 0x01 ) << 6 ) +
            ((key[i] & 0x01) << 7 );

    gcry_cipher_setkey( ctx, key, 8 );
    gcry_cipher_encrypt( ctx, bytes, CHALLENGESIZE, bytes, CHALLENGESIZE );
    gcry_cipher_close( ctx );
}
1510