/*****************************************************************************
 * x11.c: Screen capture module.
 *****************************************************************************
 * Copyright (C) 2004 the VideoLAN team
 * $Id: c4f9d83eef8c97f4d265914f81bb6af566b5fa59 $
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
 *
 * 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 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

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

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

#include <vlc_common.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include "screen.h"

int screen_InitCapture( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    Display *p_display;
    XWindowAttributes win_info;
    int i_chroma;

    /* Open the display */
    p_display = XOpenDisplay( NULL );
    if( !p_display )
    {
        msg_Err( p_demux, "cannot open display" );
        return VLC_EGENERIC;
    }
    p_sys->p_data = (void *)p_display;

    /* Get the parameters of the root window */
    if( !XGetWindowAttributes( p_display,
                               DefaultRootWindow( p_display ),
                               &win_info ) )
    {
        msg_Err( p_demux, "can't get root window attributes" );
        XCloseDisplay( p_display );
        return VLC_EGENERIC;
    }

    switch( win_info.depth )
    {
    case 8: /* FIXME: set the palette */
        i_chroma = VLC_FOURCC('R','G','B','2'); break;
    case 15:
        i_chroma = VLC_FOURCC('R','V','1','5'); break;
    case 16:
        i_chroma = VLC_FOURCC('R','V','1','6'); break;
    case 24:
    case 32:
        i_chroma = VLC_FOURCC('R','V','3','2');
        win_info.depth = 32;
        break;
    default:
        msg_Err( p_demux, "unknown screen depth %i", win_info.depth );
        XCloseDisplay( p_display );
        return VLC_EGENERIC;
    }

    es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
    p_sys->fmt.video.i_visible_width =
    p_sys->fmt.video.i_width  = win_info.width;
    p_sys->fmt.video.i_visible_height =
    p_sys->fmt.video.i_height = win_info.height;
    p_sys->fmt.video.i_bits_per_pixel = win_info.depth;
    p_sys->fmt.video.i_chroma = i_chroma;

#if 0
    win_info.visual->red_mask;
    win_info.visual->green_mask;
    win_info.visual->blue_mask;
    win_info.visual->bits_per_rgb;
#endif

    return VLC_SUCCESS;
}

int screen_CloseCapture( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    Display *p_display = (Display *)p_sys->p_data;

    XCloseDisplay( p_display );
    if( p_sys->p_blend )
    {
        module_Unneed( p_sys->p_blend, p_sys->p_blend->p_module );
        vlc_object_detach( p_sys->p_blend );
        vlc_object_release( p_sys->p_blend );
    }
    return VLC_SUCCESS;
}

block_t *screen_Capture( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    Display *p_display = (Display *)p_sys->p_data;
    block_t *p_block;
    XImage *image;
    int i_size;
    int root_x = 0, root_y = 0;

    if( p_sys->b_follow_mouse || p_sys->p_mouse )
    {
        Window root = DefaultRootWindow( p_display ), child;
        int win_x, win_y;
        unsigned int mask;
        if( XQueryPointer( p_display, root,
            &root, &child, &root_x, &root_y, &win_x, &win_y,
            &mask ) )
        {
            if( p_sys->b_follow_mouse )
            {
                root_x -= p_sys->i_width/2;
                if( root_x < 0 ) root_x = 0;
                p_sys->i_left = __MIN( (unsigned int)root_x,
                                       p_sys->i_screen_width - p_sys->i_width );
                root_y -= p_sys->i_height/2;
                if( root_y < 0 ) root_y = 0;
                p_sys->i_top = __MIN( (unsigned int)root_y,
                                      p_sys->i_screen_height - p_sys->i_height );
            }
        }
        else
            msg_Dbg( p_demux, "XQueryPointer() failed" );

    }

    image = XGetImage( p_display, DefaultRootWindow( p_display ),
                       p_sys->i_left, p_sys->i_top, p_sys->fmt.video.i_width,
                       p_sys->fmt.video.i_height, AllPlanes, ZPixmap );

    if( !image )
    {
        msg_Warn( p_demux, "cannot get image" );
        return 0;
    }

    i_size = image->bytes_per_line * image->height;

    if( !( p_block = block_New( p_demux, i_size ) ) )
    {
        msg_Warn( p_demux, "cannot get block" );
        XDestroyImage( image );
        return 0;
    }

    if( !p_sys->p_mouse )
        vlc_memcpy( p_block->p_buffer, image->data, i_size );
    else
    {
        if( !p_sys->dst.i_planes )
            vout_InitPicture( p_demux, &p_sys->dst,
                              p_sys->fmt.video.i_chroma,
                              p_sys->fmt.video.i_width,
                              p_sys->fmt.video.i_height,
                              p_sys->fmt.video.i_aspect );
        if( !p_sys->p_blend )
        {
            p_sys->p_blend = vlc_object_create( p_demux, sizeof(filter_t) );
            if( !p_sys->p_blend )
                msg_Err( p_demux, "Could not allocate memory for blending module" );
            else
            {
                es_format_Init( &p_sys->p_blend->fmt_in, VIDEO_ES,
                                VLC_FOURCC('R','G','B','A') );
                p_sys->p_blend->fmt_in.video = p_sys->p_mouse->format;
                p_sys->p_blend->fmt_out = p_sys->fmt;
                p_sys->p_blend->p_module =
                    module_Need( p_sys->p_blend, "video blending", 0, 0 );
                if( !p_sys->p_blend->p_module )
                {
                    msg_Err( p_demux, "Could not load video blending module" );
                    vlc_object_detach( p_sys->p_blend );
                    vlc_object_release( p_sys->p_blend );
                    p_sys->p_blend = NULL;
                }
            }
        }
        if( p_sys->p_blend )
        {
            vlc_memcpy( p_block->p_buffer, image->data, i_size );
            p_sys->dst.p->p_pixels = p_block->p_buffer;
            p_sys->p_blend->pf_video_blend( p_sys->p_blend,
                                            &p_sys->dst,
                                            p_sys->p_mouse,
                                            root_x,
                                            root_y,
                                            255 );
        }
        else
        {
            picture_Release( p_sys->p_mouse );
            p_sys->p_mouse = NULL;
            vlc_memcpy( p_block->p_buffer, image->data, i_size );
        }
    }

    XDestroyImage( image );

    return p_block;
}

