]> git.sesse.net Git - vlc/blobdiff - modules/access/screen/xcb.c
Add ranges to font sizes
[vlc] / modules / access / screen / xcb.c
index 53280a8a1f89b740cd96f8ab3f4cf051c39cf7fa..dd3ab99fa986aca79fe847c13f87aba26d70f5ac 100644 (file)
@@ -5,20 +5,20 @@
 /*****************************************************************************
  * Copyright © 2009 Rémi Denis-Courmont
  *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public License
- * as published by the Free Software Foundation; either version 2.1
- * of the License, or (at your option) any later version.
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
+ * 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.
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- ****************************************************************************/
+ * You should have received a copy of the GNU Lesser 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.
+ *****************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
 #include <stdarg.h>
 #include <assert.h>
 #include <xcb/xcb.h>
+#include <xcb/composite.h>
 #include <vlc_common.h>
 #include <vlc_demux.h>
 #include <vlc_plugin.h>
 
-#define CACHING_TEXT N_("Caching value in ms")
-#define CACHING_LONGTEXT N_( \
-    "Caching value for screen capture. " \
-    "This value should be set in milliseconds.")
-
 #define FPS_TEXT N_("Frame rate")
 #define FPS_LONGTEXT N_( \
     "How many times the screen content should be refreshed per second.")
 
 #define LEFT_TEXT N_("Region left column")
 #define LEFT_LONGTEXT N_( \
-    "Abscissa of the capture reion in pixels.")
+    "Abscissa of the capture region in pixels.")
 
 #define TOP_TEXT N_("Region top row")
 #define TOP_LONGTEXT N_( \
@@ -73,8 +69,6 @@ vlc_module_begin ()
     set_capability ("access_demux", 0)
     set_callbacks (Open, Close)
 
-    add_integer ("screen-caching", DEFAULT_PTS_DELAY * 1000 / CLOCK_FREQ,
-                 CACHING_TEXT, CACHING_LONGTEXT, true)
     add_float ("screen-fps", 2.0, FPS_TEXT, FPS_LONGTEXT, true)
     add_integer ("screen-left", 0, LEFT_TEXT, LEFT_LONGTEXT, true)
         change_integer_range (-32768, 32767)
@@ -110,6 +104,7 @@ struct demux_sys_t
     mtime_t           pts, interval;
     float             rate;
     xcb_window_t      window;
+    xcb_pixmap_t      pixmap;
     int16_t           x, y;
     uint16_t          w, h;
     uint16_t          cur_w, cur_h;
@@ -176,16 +171,39 @@ static int Open (vlc_object_t *obj)
             goto error;
         }
         p_sys->window = ul;
+
+        xcb_composite_query_version_reply_t *r =
+            xcb_composite_query_version_reply (conn,
+                xcb_composite_query_version (conn, 0, 4), NULL);
+        if (r == NULL || r->minor_version < 2)
+        {
+            msg_Err (obj, "X Composite extension not available");
+            free (r);
+            goto error;
+        }
+        msg_Dbg (obj, "using Composite extension v%"PRIu32".%"PRIu32,
+                 r->major_version, r->minor_version);
+        free (r);
+
+        xcb_composite_redirect_window (conn, p_sys->window,
+                                       XCB_COMPOSITE_REDIRECT_AUTOMATIC);
     }
     else
         goto error;
 
     /* Window properties */
-    p_sys->x = var_InheritInteger (obj, "screen-left");
-    p_sys->y = var_InheritInteger (obj, "screen-top");
+    p_sys->pixmap = xcb_generate_id (conn);
     p_sys->w = var_InheritInteger (obj, "screen-width");
     p_sys->h = var_InheritInteger (obj, "screen-height");
-    p_sys->follow_mouse = var_InheritBool (obj, "screen-follow-mouse");
+    if (p_sys->w != 0 || p_sys->h != 0)
+        p_sys->follow_mouse = var_InheritBool (obj, "screen-follow-mouse");
+    else /* Following mouse is meaningless if width&height are dynamic. */
+        p_sys->follow_mouse = false;
+    if (!p_sys->follow_mouse) /* X and Y are meaningless if following mouse */
+    {
+        p_sys->x = var_InheritInteger (obj, "screen-left");
+        p_sys->y = var_InheritInteger (obj, "screen-top");
+    }
 
     /* Initializes format */
     p_sys->rate = var_InheritFloat (obj, "screen-fps");
@@ -194,7 +212,6 @@ static int Open (vlc_object_t *obj)
     p_sys->interval = (float)CLOCK_FREQ / p_sys->rate;
     if (!p_sys->interval)
         goto error;
-    var_Create (obj, "screen-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
 
     p_sys->cur_w = 0;
     p_sys->cur_h = 0;
@@ -257,13 +274,7 @@ static int Control (demux_t *demux, int query, va_list args)
         case DEMUX_GET_PTS_DELAY:
         {
             int64_t *v = va_arg (args, int64_t *);
-            *v = var_GetInteger (demux, "screen-caching") * UINT64_C(1000);
-            return VLC_SUCCESS;
-        }
-
-        {
-            bool *v = (bool*)va_arg( args, bool * );
-            *v = false;
+            *v = INT64_C(1000) * var_InheritInteger (demux, "live-caching");
             return VLC_SUCCESS;
         }
 
@@ -291,115 +302,132 @@ static int Control (demux_t *demux, int query, va_list args)
 static void Demux (void *data)
 {
     demux_t *demux = data;
-    demux_sys_t *p_sys = demux->p_sys;
-    xcb_connection_t *conn = p_sys->conn;
+    demux_sys_t *sys = demux->p_sys;
+    xcb_connection_t *conn = sys->conn;
+
+    /* Determine capture region */
+    xcb_get_geometry_cookie_t gc;
+    xcb_query_pointer_cookie_t qc;
 
-    /* Update capture region (if needed) */
+    gc = xcb_get_geometry (conn, sys->window);
+    if (sys->follow_mouse)
+        qc = xcb_query_pointer (conn, sys->window);
 
-    xcb_get_geometry_reply_t *geo =
-        xcb_get_geometry_reply (conn,
-            xcb_get_geometry (conn, p_sys->window), NULL);
+    xcb_get_geometry_reply_t *geo = xcb_get_geometry_reply (conn, gc, NULL);
     if (geo == NULL)
     {
-        msg_Err (demux, "bad X11 drawable 0x%08"PRIx32, p_sys->window);
+        msg_Err (demux, "bad X11 drawable 0x%08"PRIx32, sys->window);
+discard:
+        if (sys->follow_mouse)
+            xcb_discard_reply (conn, gc.sequence);
         return;
     }
 
-    xcb_window_t root = geo->root;
-    int16_t x = p_sys->x, y = p_sys->y;
-    xcb_translate_coordinates_cookie_t tc;
-    xcb_query_pointer_cookie_t qc;
+    int w = sys->w;
+    int h = sys->h;
+    int x, y;
 
-    if (p_sys->window != root)
-        tc = xcb_translate_coordinates (conn, p_sys->window, root,
-                                        x, y);
-    if (p_sys->follow_mouse)
-        qc = xcb_query_pointer (conn, p_sys->window);
-
-    uint16_t ow = geo->width - x;
-    uint16_t oh = geo->height - y;
-    uint16_t w = p_sys->w;
-    uint16_t h = p_sys->h;
-    if (w == 0 || w > ow)
-        w = ow;
-    if (h == 0 || h > oh)
-        h = oh;
-    uint8_t depth = geo->depth;
-    free (geo);
-
-    if (p_sys->window != root)
+    if (sys->follow_mouse)
     {
-        xcb_translate_coordinates_reply_t *coords =
-             xcb_translate_coordinates_reply (conn, tc, NULL);
-        if (coords != NULL)
+        xcb_query_pointer_reply_t *ptr =
+            xcb_query_pointer_reply (conn, qc, NULL);
+        if (ptr == NULL)
         {
-            x = coords->dst_x;
-            y = coords->dst_y;
-            free (coords);
+            free (geo);
+            return;
         }
+
+        if (w == 0 || w > geo->width)
+            w = geo->width;
+        x = ptr->win_x;
+        if (x < w / 2)
+            x = 0;
+        else if (x >= (int)geo->width - (w / 2))
+            x = geo->width - w;
+        else
+            x -= w / 2;
+
+        if (h == 0 || h > geo->height)
+            h = geo->height;
+        y = ptr->win_y;
+        if (y < h / 2)
+            y = 0;
+        else if (y >= (int)geo->height - (h / 2))
+            y = geo->height - h;
+        else
+            y -= h / 2;
+    }
+    else
+    {
+        int max;
+
+        x = sys->x;
+        max = (int)geo->width - x;
+        if (max <= 0)
+            goto discard;
+        if (w == 0 || w > max)
+            w = max;
+
+        y = sys->y;
+        max = (int)geo->height - y;
+        if (max <= 0)
+            goto discard;
+        if (h == 0 || h > max)
+            h = max;
     }
 
-    if (p_sys->follow_mouse)
+    /* Update elementary stream format (if needed) */
+    if (w != sys->cur_w || h != sys->cur_h)
     {
-        xcb_query_pointer_reply_t *ptr =
-            xcb_query_pointer_reply (conn, qc, NULL);
-        if (ptr != NULL)
+        if (sys->es != NULL)
+            es_out_Del (demux->out, sys->es);
+
+        /* Update composite pixmap */
+        if (sys->window != geo->root)
+        {
+            xcb_free_pixmap (conn, sys->pixmap); /* no-op first time */
+            xcb_composite_name_window_pixmap (conn, sys->window, sys->pixmap);
+            xcb_create_pixmap (conn, geo->depth, sys->pixmap,
+                               geo->root, geo->width, geo->height);
+        }
+
+        sys->es = InitES (demux, w, h, geo->depth);
+        if (sys->es != NULL)
         {
-            int16_t min_x = x + (w / 2);
-            int16_t min_y = y + (h / 2);
-            int16_t max_x = x + ow - ((w + 1) / 2);
-            int16_t max_y = y + oh - ((h + 1) / 2);
-
-            assert (max_x >= min_x); /* max_x - min_x = ow - w >= 0 */
-            if (ptr->root_x > max_x)
-                x += ow - w;
-            else if (ptr->root_x > min_x)
-                x += ptr->root_x - min_x;
-
-            assert (max_y >= min_y);
-            if (ptr->root_y > max_y)
-                y += oh - h;
-            else if (ptr->root_y > min_y)
-                y += ptr->root_y - min_y;
+            sys->cur_w = w;
+            sys->cur_h = h;
         }
     }
 
     /* Capture screen */
+    xcb_drawable_t drawable =
+        (sys->window != geo->root) ? sys->pixmap : sys->window;
+    free (geo);
+
     xcb_get_image_reply_t *img;
     img = xcb_get_image_reply (conn,
-        xcb_get_image (conn, XCB_IMAGE_FORMAT_Z_PIXMAP, root,
+        xcb_get_image (conn, XCB_IMAGE_FORMAT_Z_PIXMAP, drawable,
                        x, y, w, h, ~0), NULL);
     if (img == NULL)
         return;
 
-    block_t *block = block_heap_Alloc (img, xcb_get_image_data (img),
-                                       xcb_get_image_data_length (img));
+    size_t datalen = xcb_get_image_data_length (img);
+    block_t *block = block_heap_Alloc (img, sizeof (*img) + datalen);
     if (block == NULL)
         return;
-
-    /* Update elementary stream format (if needed) */
-    if (w != p_sys->cur_w || h != p_sys->cur_h)
-    {
-        if (p_sys->es != NULL)
-            es_out_Del (demux->out, p_sys->es);
-        p_sys->es = InitES (demux, w, h, depth);
-        if (p_sys->es != NULL)
-        {
-            p_sys->cur_w = w;
-            p_sys->cur_h = h;
-        }
-    }
+    block->p_buffer = xcb_get_image_data (img);
+    block->i_buffer = datalen;
 
     /* Send block - zero copy */
-    if (p_sys->es != NULL)
+    if (sys->es != NULL)
     {
-        if (p_sys->pts == VLC_TS_INVALID)
-            p_sys->pts = mdate ();
-        block->i_pts = block->i_dts = p_sys->pts;
+        if (sys->pts == VLC_TS_INVALID)
+            sys->pts = mdate ();
+        block->i_pts = block->i_dts = sys->pts;
 
-        es_out_Control (demux->out, ES_OUT_SET_PCR, p_sys->pts);
-        es_out_Send (demux->out, p_sys->es, block);
-        p_sys->pts += p_sys->interval;
+        es_out_Control (demux->out, ES_OUT_SET_PCR, sys->pts);
+        es_out_Send (demux->out, sys->es, block);
+        sys->pts += sys->interval;
     }
 }