Fix the XShm 1.2 support in the x11 and xv video outputs.

- xcb closes the fd passed to xcb_shm_attach_fd(). Stop closing it
  again, which could close an unrelated fd.
- Unmap the shared memory when freeing it instead of leaking it.
- Remove the leftover shmctl(IPC_RMID) call on the fd in vo_x11.
- Unlink the shm file right after creating it.
- Size the file before mapping it and check for errors.
- Check for MIT-SHM 1.2 and fall back to SysV shared memory.
- Move the shared code to x11_common.c.
- configure: fix the --enable-xcb-shm option name, add help text and
  use CONFIG_XCB_SHM so the option works.


Index: Makefile
===================================================================
RCS file: /cvs/ports/x11/mplayer/Makefile,v
retrieving revision 1.346
diff -u -p -u -p -r1.346 Makefile
--- Makefile    19 Sep 2026 19:36:37 -0000      1.346
+++ Makefile    15 Nov 2026 00:13:54 -0000
@@ -2,7 +2,7 @@ COMMENT=        movie player supporting many fo
 
 V=             20251123
 FFMPEG_V=      9.0.2
-REVISION=      6
+REVISION=      7
 DISTNAME=      mplayer-${V}
 CATEGORIES=    x11 multimedia
 SITES=         https://comstyle.com/source/
Index: patches/patch-configure
===================================================================
RCS file: /cvs/ports/x11/mplayer/patches/patch-configure,v
retrieving revision 1.99
diff -u -p -u -p -r1.99 patch-configure
--- patches/patch-configure     11 Feb 2026 20:57:57 -0000      1.99
+++ patches/patch-configure     15 Nov 2026 00:13:54 -0000
@@ -1,4 +1,5 @@
-Add support for xcb-shm
+Use the shared memory helpers in x11_common.c: XShm 1.2 (fd passing
+via xcb-shm) when the X server supports it, SysV shared memory otherwise.
 
 Index: configure
 --- configure.orig
@@ -17,7 +18,15 @@ Index: configure
  # Use this before starting a check
  echocheck() {
    echo "============ Checking for $@ ============" >> "$TMPLOG"
-@@ -694,6 +701,7 @@ _mplayer=yes
+@@ -512,6 +519,7 @@ Video output:
+   --enable-x11             enable X11 video output [autodetect]
+   --enable-xshape          enable XShape support [autodetect]
+   --disable-xss            disable screensaver support via xss [autodetect]
++  --disable-xcb-shm        disable XShm fd passing via xcb-shm [autodetect]
+   --enable-fbdev           enable FBDev video output [autodetect]
+   --enable-mlib            enable mediaLib video output (Solaris) [disable]
+   --enable-3dfx            enable obsolete /dev/3dfx video output [disable]
+@@ -694,6 +702,7 @@ _mplayer=yes
  _x11=auto
  _xshape=auto
  _xss=auto
@@ -25,16 +34,16 @@ Index: configure
  _dga1=auto
  _dga2=auto
  _xv=auto
-@@ -1042,6 +1050,8 @@ for ac_option do
+@@ -1042,6 +1051,8 @@ for ac_option do
    --disable-xshape)     _xshape=no      ;;
    --enable-xss)         _xss=yes        ;;
    --disable-xss)        _xss=no         ;;
-+  --enable-xcb_shm)     _xcb_shm=yes    ;;
-+  --disable-xcb-shm)    _xcb_shm=no     ;;  
++  --enable-xcb-shm)     _xcb_shm=yes    ;;
++  --disable-xcb-shm)    _xcb_shm=no     ;;
    --enable-xv)          _xv=yes         ;;
    --disable-xv)         _xv=no          ;;
    --enable-vda)         _vda=yes        ;;
-@@ -1521,40 +1531,40 @@ echo configuration: $configuration > "$TMPLOG"
+@@ -1521,40 +1532,40 @@ echo configuration: $configuration > "$TMPLOG"
  echo >> "$TMPLOG"
  
  
@@ -108,7 +117,7 @@ Index: configure
  ffmpeg_filter_struct_name=AVFilter
  if grep -q FFFilter ffmpeg/configure; then
      ffmpeg_filter_struct_name=FFFilter
-@@ -2009,7 +2019,10 @@ fi
+@@ -2009,7 +2020,10 @@ fi
  if arm ; then
    cc_check && host_arch=aarch64 || host_arch=arm
  fi
@@ -119,7 +128,7 @@ Index: configure
  
  echo "Detected operating system: $system_name"
  echo "Detected host architecture: $host_arch"
-@@ -2563,7 +2576,7 @@ case "$host_arch" in
+@@ -2563,7 +2577,7 @@ case "$host_arch" in
      arch='sparc'
      iproc='sparc'
      if test "$host_arch" = "sparc64" ; then
@@ -128,7 +137,7 @@ Index: configure
        proc='ultrasparc'
        def_fast_64bit='#define HAVE_FAST_64BIT 1'
      elif sunos ; then
-@@ -2864,7 +2877,14 @@ EOF
+@@ -2864,7 +2878,14 @@ EOF
      arch='arc'
      iproc='arc'
      ;;
@@ -143,7 +152,7 @@ Index: configure
    *)
      echo "The architecture of your CPU ($host_arch) is not supported by this 
configure script"
      echo "It seems nobody has ported MPlayer to your OS or CPU type yet."
-@@ -2942,7 +2962,7 @@ os2 &&
+@@ -2942,7 +2963,7 @@ os2 &&
    { saved_filter_flags=$filter_flags; filter_flags=os2_aout_filter_flags; }
  cc_check -c || die "Symbol mangling check failed."
  os2 && filter_flags=$saved_filter_flags
@@ -152,7 +161,7 @@ Index: configure
  extern_prefix=${sym%%ff_extern*}
  def_extern_asm="#define EXTERN_ASM $extern_prefix"
  def_extern_prefix="#define EXTERN_PREFIX \"$extern_prefix\""
-@@ -3006,7 +3026,7 @@ else
+@@ -3006,7 +3027,7 @@ else
  fi
  
  CFLAGS="-D_ISOC99_SOURCE -I. -Iffmpeg $CFLAGS"
@@ -161,7 +170,7 @@ Index: configure
  
  # On glibc, add some more CPPFLAGS for enabling required functionality.
  cpp_condition_check features.h "defined __GLIBC__" &&
-@@ -3087,7 +3107,7 @@ elif test $relocatable = "yes" ; then
+@@ -3087,7 +3108,7 @@ elif test $relocatable = "yes" ; then
  fi
  echores $relocatable
  
@@ -170,14 +179,20 @@ Index: configure
    # Checking assembler (_as) compatibility...
    # Added workaround for older as that reads from stdin by default - atmos
    as_version=$(echo '' | $_as -version 2>&1 | sed -n 's/^.*assembler 
\(version \)*\([0-9.]*\).*$/\2/p')
-@@ -5003,6 +5023,19 @@ else
+@@ -5003,6 +5024,25 @@ else
  fi
  echores "$_x11"
  
-+echocheck "xcb shm extension"
++echocheck "xcb-shm (XShm fd passing)"
++if test "$_x11" = no || test "$_shm" = no ; then
++  _xcb_shm=no
++fi
 +if test "$_xcb_shm" = auto ; then
 +  _xcb_shm=no
-+  statement_check "xcb/shm.h" 'xcb_shm_query_version(NULL)' -lX11-xcb 
-lxcb-shm -lxcb && _xcb_shm=yes
++  statement_check_broken X11/Xlib-xcb.h xcb/shm.h \
++    'xcb_shm_attach_fd_checked(XGetXCBConnection(NULL), 0, -1, 0)' \
++    -lX11-xcb -lxcb-shm -lxcb &&
++    statement_check sys/mman.h 'shm_mkstemp(0)' && _xcb_shm=yes
 +fi
 +if test "$_xcb_shm" = yes ; then
 +  def_xcb_shm='#define CONFIG_XCB_SHM 1'
@@ -190,7 +205,7 @@ Index: configure
  echocheck "Xss screensaver extensions"
  if test "$_xss" = auto ; then
    _xss=no
-@@ -6359,12 +6392,16 @@ fi #if irix
+@@ -6359,12 +6399,16 @@ fi #if irix
  echocheck "sndio audio"
  if test "$_sndio" = auto ; then
    _sndio=no
@@ -209,7 +224,7 @@ Index: configure
  else
    def_sndio='#undef CONFIG_SNDIO_AUDIO'
    noaomodules="sndio $noaomodules"
-@@ -6547,7 +6584,7 @@ echocheck "cdparanoia"
+@@ -6547,7 +6591,7 @@ echocheck "cdparanoia"
  if test "$_cdparanoia" = auto ; then
      _cdparanoia=no
      for inc_tmp in "" "-I/usr/include/cdda" "-I/usr/local/include/cdda" ; do
@@ -218,7 +233,7 @@ Index: configure
          _cdparanoia=yes && extra_cflags="$extra_cflags $inc_tmp" && break
      done
  fi
-@@ -8438,6 +8475,7 @@ extra_ldflags="$extra_ldflags $libm"
+@@ -8438,6 +8482,7 @@ extra_ldflags="$extra_ldflags $libm"
  # XML documentation tests
  echocheck "XML catalogs"
  for try_catalog in \
@@ -226,7 +241,7 @@ Index: configure
    /etc/sgml/catalog \
    /usr/share/xml/docbook/*/catalog.xml \
    /opt/local/share/xml/docbook-xml/*/catalog.xml \
-@@ -8465,6 +8503,7 @@ fi
+@@ -8465,6 +8510,7 @@ fi
  
  echocheck "XML chunked stylesheet"
  for try_chunk_xsl in \
@@ -234,7 +249,7 @@ Index: configure
    /usr/share/xml/docbook/*/html/chunk.xsl \
    /usr/share/sgml/docbook/stylesheet/xsl/nwalsh/html/chunk.xsl \
    /usr/share/sgml/docbook/yelp/docbook/html/chunk.xsl \
-@@ -8490,6 +8529,7 @@ fi
+@@ -8490,6 +8536,7 @@ fi
  
  echocheck "XML monolithic stylesheet"
  for try_docbook_xsl in \
@@ -242,7 +257,7 @@ Index: configure
    /usr/share/xml/docbook/*/html/docbook.xsl \
    /usr/share/sgml/docbook/stylesheet/xsl/nwalsh/html/docbook.xsl \
    /usr/share/sgml/docbook/yelp/docbook/html/docbook.xsl \
-@@ -8543,6 +8583,7 @@ EOF
+@@ -8543,6 +8590,7 @@ EOF
  echocheck "XML DTD"
  #FIXME: This should prefer higher version numbers, not the other way around ..
  for try_dtd in \
@@ -250,7 +265,7 @@ Index: configure
    /usr/share/xml/docbook/*/dtd/4*/docbookx.dtd \
    /usr/share/xml/docbook/*/docbookx.dtd \
    /usr/share/sgml/docbook/*/docbookx.dtd \
-@@ -9455,6 +9496,7 @@ $def_xf86keysym
+@@ -9455,6 +9503,7 @@ $def_xf86keysym
  $def_xinerama
  $def_xmga
  $def_xss
@@ -258,7 +273,7 @@ Index: configure
  $def_xv
  $def_xvr100
  $def_yuv4mpeg
-@@ -9754,9 +9796,6 @@ cmp -s "$TMPH" config.h || mv -f "$TMPH" config.h
+@@ -9754,9 +9803,6 @@ cmp -s "$TMPH" config.h || mv -f "$TMPH" config.h
  
  ############################################################################
  
@@ -268,7 +283,7 @@ Index: configure
  # Create avconfig.h for FFmpeg.
  cat > "$TMPH" << EOF
  /* Generated by mpconfigure */
-@@ -9849,8 +9888,6 @@ print_enabled_components libavformat/demuxer_list.c FF
+@@ -9849,8 +9895,6 @@ print_enabled_components libavformat/demuxer_list.c FF
  print_enabled_components libavformat/muxer_list.c FFOutputFormat muxer_list 
$libavmuxers
  print_enabled_components libavformat/protocol_list.c URLProtocol 
url_protocols $libavprotocols
  print_enabled_filters libavfilter/filter_list.c "$ffmpeg_filter_struct_name" 
filter_list $libavfilters
Index: patches/patch-libvo_vo_x11_c
===================================================================
RCS file: /cvs/ports/x11/mplayer/patches/patch-libvo_vo_x11_c,v
retrieving revision 1.2
diff -u -p -u -p -r1.2 patch-libvo_vo_x11_c
--- patches/patch-libvo_vo_x11_c        19 Feb 2025 05:43:14 -0000      1.2
+++ patches/patch-libvo_vo_x11_c        15 Nov 2026 00:13:54 -0000
@@ -1,56 +1,28 @@
-Implement XShm 1.2
+Use the shared memory helpers in x11_common.c: XShm 1.2 (fd passing
+via xcb-shm) when the X server supports it, SysV shared memory otherwise.
 
 Index: libvo/vo_x11.c
 --- libvo/vo_x11.c.orig
 +++ libvo/vo_x11.c
-@@ -38,7 +38,11 @@
+@@ -36,15 +36,14 @@
+ #include "x11_common.h"
+ 
  #ifdef HAVE_SHM
- #include <sys/ipc.h>
- #include <sys/shm.h>
-+#include <sys/mman.h>
+-#include <sys/ipc.h>
+-#include <sys/shm.h>
  #include <X11/extensions/XShm.h>
-+#include <X11/Xlib-xcb.h>
-+#include <xcb/shm.h>
-+#include <unistd.h>
  
  static int Shmem_Flag;
  
-@@ -78,7 +82,9 @@ static unsigned char *ImageDataOrig;
- static XImage *myximage = NULL;
- static int depth, bpp;
- static XWindowAttributes attribs;
--
-+#ifdef HAVE_SHM
-+static  char myshmname[128];
-+#endif
- static int int_pause;
- 
- static int Flip_Flag;
-@@ -123,9 +129,23 @@ static int dst_width;
- 
- static XVisualInfo vinfo;
+ //static int Quiet_Flag;  Here also what is this for. It's used but isn't 
initialized?
+ static XShmSegmentInfo Shminfo[1];
+-static int gXErrorFlag;
++static int Shm_Type;
++static size_t Shm_Size;
+ static int CompletionType = -1;
+ #endif
  
-+#ifdef HAVE_SHM
-+static Bool XShmAttachFd(Display *dpy, XShmSegmentInfo *shminfo)
-+{
-+    xcb_connection_t *xcb_conn = XGetXCBConnection(dpy);
-+        
-+    shminfo->shmseg = xcb_generate_id(xcb_conn);
-+    xcb_shm_attach_fd(xcb_conn, shminfo->shmseg,
-+                      shminfo->shmid, shminfo->readOnly);
-+    return 1;
-+}
-+#endif
-+
- static void getMyXImage(void)
- {
- #ifdef HAVE_SHM
-+    size_t len;
-+
-     if (mLocalDisplay && XShmQueryExtension(mDisplay))
-         Shmem_Flag = 1;
-     else
-@@ -148,33 +168,40 @@ static void getMyXImage(void)
+@@ -148,45 +147,17 @@ static void getMyXImage(void)
                     "Shared memory error,disabling ( Ximage error )\n");
              goto shmemerror;
          }
@@ -58,58 +30,54 @@ Index: libvo/vo_x11.c
 -                                  myximage->bytes_per_line *
 -                                  myximage->height, IPC_CREAT | SHM_R | 
SHM_W);
 -        if (Shminfo[0].shmid < 0)
-+      memcpy(myshmname, "/tmp/mplayer-x11-XXXXXXXXXX", sizeof(myshmname));
-+      Shminfo[0].shmid = shm_mkstemp(myshmname);
-+      if (Shminfo[0].shmid < 0)
++        Shm_Size = (size_t)myximage->bytes_per_line * myximage->height;
++        Shm_Type = vo_x11_shm_attach(mDisplay, &Shminfo[0], Shm_Size);
++        if (Shm_Type == VO_X11_SHM_NONE)
          {
              XDestroyImage(myximage);
-             mp_msg(MSGT_VO, MSGL_V, "%s\n", strerror(errno));
-             //perror( strerror( errno ) );
+-            mp_msg(MSGT_VO, MSGL_V, "%s\n", strerror(errno));
+-            //perror( strerror( errno ) );
              mp_msg(MSGT_VO, MSGL_WARN,
 -                   "Shared memory error,disabling ( seg id error )\n");
-+                   "Shared memory error,disabling ( shm_open error )\n");
-             goto shmemerror;
-         }
+-            goto shmemerror;
+-        }
 -        Shminfo[0].shmaddr = (char *) shmat(Shminfo[0].shmid, 0, 0);
 -
 -        if (Shminfo[0].shmaddr == ((char *) -1))
-+      len = myximage->bytes_per_line * myximage->height;
-+      
-+        Shminfo[0].shmaddr = mmap(NULL, len, PROT_READ | PROT_WRITE,
-+            MAP_SHARED|__MAP_NOFAULT, Shminfo[0].shmid, 0);
-+      
-+        if (Shminfo[0].shmaddr == MAP_FAILED)
-         {
-             XDestroyImage(myximage);
+-        {
+-            XDestroyImage(myximage);
 -            if (Shminfo[0].shmaddr != ((char *) -1))
 -                shmdt(Shminfo[0].shmaddr);
-             mp_msg(MSGT_VO, MSGL_WARN,
+-            mp_msg(MSGT_VO, MSGL_WARN,
 -                   "Shared memory error,disabling ( address error )\n");
-+                   "Shared memory error,disabling ( mmap error )\n");
++                   "Shared memory error,disabling ( segment error )\n");
              goto shmemerror;
          }
-+      if (ftruncate(Shminfo[0].shmid, len) == -1)
-+      {
-+            XDestroyImage(myximage);
-+            mp_msg(MSGT_VO, MSGL_WARN,
-+                   "Shared memory error,disabling ( fruncate error )\n");
-+            goto shmemerror;
-+      }
          myximage->data = Shminfo[0].shmaddr;
          ImageData = (unsigned char *) myximage->data;
-         Shminfo[0].readOnly = False;
+-        Shminfo[0].readOnly = False;
 -        XShmAttach(mDisplay, &Shminfo[0]);
-+        XShmAttachFd(mDisplay, &Shminfo[0]);
- 
-         XSync(mDisplay, False);
+-
+-        XSync(mDisplay, False);
+-
+-        if (gXErrorFlag)
+-        {
+-            XDestroyImage(myximage);
+-            shmdt(Shminfo[0].shmaddr);
+-            mp_msg(MSGT_VO, MSGL_WARN, "Shared memory error,disabling.\n");
+-            gXErrorFlag = 0;
+-            goto shmemerror;
+-        } else
+-            shmctl(Shminfo[0].shmid, IPC_RMID, 0);
  
-@@ -218,9 +245,10 @@ static void freeMyXImage(void)
+         {
+             static int firstTime = 1;
+@@ -218,9 +189,8 @@ static void freeMyXImage(void)
  #ifdef HAVE_SHM
      if (Shmem_Flag)
      {
-+      close(Shminfo[0].shmid);
-+      shm_unlink(myshmname);
-         XShmDetach(mDisplay, &Shminfo[0]);
+-        XShmDetach(mDisplay, &Shminfo[0]);
++        vo_x11_shm_detach(mDisplay, &Shminfo[0], Shm_Size, Shm_Type);
          XDestroyImage(myximage);
 -        shmdt(Shminfo[0].shmaddr);
      } else
Index: patches/patch-libvo_vo_xv_c
===================================================================
RCS file: /cvs/ports/x11/mplayer/patches/patch-libvo_vo_xv_c,v
retrieving revision 1.2
diff -u -p -u -p -r1.2 patch-libvo_vo_xv_c
--- patches/patch-libvo_vo_xv_c 19 Feb 2025 05:43:14 -0000      1.2
+++ patches/patch-libvo_vo_xv_c 15 Nov 2026 00:13:54 -0000
@@ -1,78 +1,118 @@
-Implement XShm 1.2
+Use the shared memory helpers in x11_common.c: XShm 1.2 (fd passing
+via xcb-shm) when the X server supports it, SysV shared memory otherwise.
+Track the segment type per buffer so a failed buffer falls back to
+non-shared XvImage on its own.
 
 Index: libvo/vo_xv.c
 --- libvo/vo_xv.c.orig
 +++ libvo/vo_xv.c
-@@ -74,7 +74,11 @@ const LIBVO_EXTERN(xv)
+@@ -72,12 +72,10 @@ static const vo_info_t info = {
+ 
+ const LIBVO_EXTERN(xv)
  #ifdef HAVE_SHM
- #include <sys/ipc.h>
- #include <sys/shm.h>
-+#include <sys/mman.h>
+-#include <sys/ipc.h>
+-#include <sys/shm.h>
  #include <X11/extensions/XShm.h>
-+#include <X11/Xlib-xcb.h>
-+#include <xcb/shm.h>
-+#include <unistd.h>
  
  static XShmSegmentInfo Shminfo[NUM_BUFFERS];
- static int Shmem_Flag;
-@@ -97,6 +101,9 @@ static int num_buffers = 1;     // default
- static int visible_buf = -1;    // -1 means: no buffer was drawn yet
- static XvImage *xvimage[NUM_BUFFERS];
- 
-+#ifdef HAVE_SHM
-+static  char myshmname[128];
-+#endif
- 
- static uint32_t image_width;
- static uint32_t image_height;
-@@ -110,6 +117,18 @@ static uint32_t max_width = 0, max_height = 0; // zero
- 
- static vo_draw_alpha_func draw_alpha_func;
- 
-+#ifdef HAVE_SHM
-+static Bool XShmAttachFd(Display *dpy, XShmSegmentInfo *shminfo)
-+{
-+    xcb_connection_t *xcb_conn = XGetXCBConnection(dpy);
-+        
-+    shminfo->shmseg = xcb_generate_id(xcb_conn);
-+    xcb_shm_attach_fd(xcb_conn, shminfo->shmseg,
-+                      shminfo->shmid, shminfo->readOnly);
-+    return 1;
-+}
-+#endif
-+
- static void fixup_osd_position(int *x0, int *y0, int *w, int *h)
- {
-     *x0 += image_width * (vo_panscan_x >> 1) / (vo_dwidth + vo_panscan_x);
-@@ -278,15 +297,18 @@ static void allocate_xvimage(int foo)
+-static int Shmem_Flag;
++static int Shm_Type[NUM_BUFFERS];
+ #endif
+ 
+ // Note: depends on the inclusion of X11/extensions/XShm.h
+@@ -263,31 +261,29 @@ static void allocate_xvimage(int foo)
+      * mit-shm this will bomb... trzing to fix ::atmos
+      */
+ #ifdef HAVE_SHM
++    Shm_Type[foo] = VO_X11_SHM_NONE;
+     if (mLocalDisplay && XShmQueryExtension(mDisplay))
+-        Shmem_Flag = 1;
+-    else
+-    {
+-        Shmem_Flag = 0;
+-        mp_msg(MSGT_VO, MSGL_INFO,
+-               MSGTR_LIBVO_XV_SharedMemoryNotSupported);
+-    }
+-    if (Shmem_Flag)
+     {
+         xvimage[foo] =
+             (XvImage *) XvShmCreateImage(mDisplay, xv_port, xv_format,
                                           NULL, image_width, image_height,
                                           &Shminfo[foo]);
- 
+-
 -        Shminfo[foo].shmid =
 -            shmget(IPC_PRIVATE, xvimage[foo]->data_size, IPC_CREAT | SHM_R | 
SHM_W);
 -        Shminfo[foo].shmaddr = (char *) shmat(Shminfo[foo].shmid, 0, 0);
-+      memcpy(myshmname, "/tmp/mplayer-xv-XXXXXXXXXX", sizeof(myshmname));
-+      Shminfo[foo].shmid = shm_mkstemp(myshmname);
-+        Shminfo[foo].shmaddr = mmap(NULL, xvimage[foo]->data_size,
-+          PROT_READ | PROT_WRITE, MAP_SHARED|__MAP_NOFAULT,
-+          Shminfo[foo].shmid, 0);
-+      ftruncate(Shminfo[foo].shmid, xvimage[foo]->data_size);
-         Shminfo[foo].readOnly = False;
- 
-         xvimage[foo]->data = Shminfo[foo].shmaddr;
+-        Shminfo[foo].readOnly = False;
+-
+-        xvimage[foo]->data = Shminfo[foo].shmaddr;
 -        XShmAttach(mDisplay, &Shminfo[foo]);
-+        XShmAttachFd(mDisplay, &Shminfo[foo]);
-         XSync(mDisplay, False);
+-        XSync(mDisplay, False);
 -        shmctl(Shminfo[foo].shmid, IPC_RMID, 0);
-+      shm_unlink(myshmname);
++        if (xvimage[foo])
++        {
++            Shm_Type[foo] = vo_x11_shm_attach(mDisplay, &Shminfo[foo],
++                                              xvimage[foo]->data_size);
++            if (Shm_Type[foo] == VO_X11_SHM_NONE)
++            {
++                XFree(xvimage[foo]);
++                mp_msg(MSGT_VO, MSGL_WARN,
++                       "[xv] Shared memory error, disabling.\n");
++            } else
++                xvimage[foo]->data = Shminfo[foo].shmaddr;
++        }
      } else
++        mp_msg(MSGT_VO, MSGL_INFO,
++               MSGTR_LIBVO_XV_SharedMemoryNotSupported);
++    if (Shm_Type[foo] == VO_X11_SHM_NONE)
  #endif
      {
-@@ -306,6 +328,7 @@ static void deallocate_xvimage(int foo)
-     if (Shmem_Flag)
+         xvimage[foo] =
+@@ -303,10 +299,11 @@ static void allocate_xvimage(int foo)
+ static void deallocate_xvimage(int foo)
+ {
+ #ifdef HAVE_SHM
+-    if (Shmem_Flag)
++    if (Shm_Type[foo] != VO_X11_SHM_NONE)
      {
-         XShmDetach(mDisplay, &Shminfo[foo]);
-+      close(Shminfo[foo].shmid);
-         shmdt(Shminfo[foo].shmaddr);
+-        XShmDetach(mDisplay, &Shminfo[foo]);
+-        shmdt(Shminfo[foo].shmaddr);
++        vo_x11_shm_detach(mDisplay, &Shminfo[foo], xvimage[foo]->data_size,
++                          Shm_Type[foo]);
++        Shm_Type[foo] = VO_X11_SHM_NONE;
      } else
  #endif
+     {
+@@ -318,10 +315,12 @@ static void deallocate_xvimage(int foo)
+     return;
+ }
+ 
+-static inline void put_xvimage( XvImage * xvi )
++static inline void put_xvimage(int buf)
+ {
++    XvImage *xvi = xvimage[buf];
++
+ #ifdef HAVE_SHM
+-    if (Shmem_Flag)
++    if (Shm_Type[buf] != VO_X11_SHM_NONE)
+     {
+         XvShmPutImage(mDisplay, xv_port, vo_window, vo_gc,
+                       xvi,
+@@ -353,7 +352,7 @@ static void check_events(void)
+         if ( visible_buf != -1 )
+         {
+           /* redraw the last visible buffer */
+-          put_xvimage( xvimage[visible_buf] );
++          put_xvimage(visible_buf);
+         }
+     }
+ }
+@@ -367,7 +366,7 @@ static void draw_osd(void)
+ 
+ static void flip_page(void)
+ {
+-    put_xvimage( xvimage[current_buf] );
++    put_xvimage(current_buf);
+ 
+     /* remember the currently visible buffer */
+     visible_buf = current_buf;
Index: patches/patch-libvo_x11_common_c
===================================================================
RCS file: patches/patch-libvo_x11_common_c
diff -N patches/patch-libvo_x11_common_c
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ patches/patch-libvo_x11_common_c    15 Nov 2026 00:13:54 -0000
@@ -0,0 +1,145 @@
+Shared memory helpers for vo_x11 and vo_xv.
+
+XShm 1.2 passes a file descriptor to the X server instead of using a
+SysV segment, so the memory stays owned by the client's uid and does
+not need to be mode 777. The fd is handed over to xcb, which closes it
+once the request is sent. SysV shared memory is used when the server
+or the build does not support fd passing.
+
+Index: libvo/x11_common.c
+--- libvo/x11_common.c.orig
++++ libvo/x11_common.c
+@@ -72,6 +72,17 @@
+ #include "subopt-helper.h"
+ #endif
+ 
++#ifdef HAVE_SHM
++#include <errno.h>
++#include <sys/ipc.h>
++#include <sys/shm.h>
++#ifdef CONFIG_XCB_SHM
++#include <sys/mman.h>
++#include <X11/Xlib-xcb.h>
++#include <xcb/shm.h>
++#endif
++#endif
++
+ #include "input/input.h"
+ #include "input/mouse.h"
+ 
+@@ -2456,3 +2467,115 @@ void xv_setup_colorkeyhandling( char con
+ }
+ 
+ #endif
++
++#ifdef HAVE_SHM
++#ifdef CONFIG_XCB_SHM
++/*
++ * MIT-SHM 1.2 passes a file descriptor to the X server instead of a
++ * SysV segment, so the memory stays owned by and private to the client.
++ */
++static int shm_fd_supported(Display *dpy)
++{
++    int major, minor;
++    Bool pixmaps;
++
++    if (!XShmQueryVersion(dpy, &major, &minor, &pixmaps))
++        return 0;
++    return major > 1 || (major == 1 && minor >= 2);
++}
++
++static int shm_fd_attach(Display *dpy, XShmSegmentInfo *shminfo, size_t size)
++{
++    char name[] = "/mplayer-shm-XXXXXXXXXX";
++    xcb_connection_t *conn = XGetXCBConnection(dpy);
++    xcb_void_cookie_t cookie;
++    xcb_generic_error_t *error;
++    xcb_shm_seg_t seg;
++    void *addr;
++    int fd;
++
++    fd = shm_mkstemp(name);
++    if (fd == -1) {
++        mp_msg(MSGT_VO, MSGL_V, "[x11] shm_mkstemp: %s\n", strerror(errno));
++        return 0;
++    }
++    shm_unlink(name);
++
++    if (ftruncate(fd, size) == -1) {
++        mp_msg(MSGT_VO, MSGL_V, "[x11] ftruncate: %s\n", strerror(errno));
++        close(fd);
++        return 0;
++    }
++    addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
++    if (addr == MAP_FAILED) {
++        mp_msg(MSGT_VO, MSGL_V, "[x11] mmap: %s\n", strerror(errno));
++        close(fd);
++        return 0;
++    }
++    seg = xcb_generate_id(conn);
++    if (seg == (xcb_shm_seg_t)-1) {
++        munmap(addr, size);
++        close(fd);
++        return 0;
++    }
++
++    /* xcb takes ownership of fd and closes it once the request is sent */
++    cookie = xcb_shm_attach_fd_checked(conn, seg, fd, 0);
++    error = xcb_request_check(conn, cookie);
++    if (error) {
++        mp_msg(MSGT_VO, MSGL_V, "[x11] ShmAttachFd failed: error %d\n",
++               error->error_code);
++        free(error);
++        munmap(addr, size);
++        return 0;
++    }
++
++    shminfo->shmseg = seg;
++    shminfo->shmid = -1;
++    shminfo->shmaddr = addr;
++    shminfo->readOnly = False;
++    return 1;
++}
++#endif
++
++int vo_x11_shm_attach(Display *dpy, XShmSegmentInfo *shminfo, size_t size)
++{
++#ifdef CONFIG_XCB_SHM
++    if (shm_fd_supported(dpy) && shm_fd_attach(dpy, shminfo, size))
++        return VO_X11_SHM_FD;
++#endif
++
++    shminfo->shmid = shmget(IPC_PRIVATE, size, IPC_CREAT | SHM_R | SHM_W);
++    if (shminfo->shmid < 0) {
++        mp_msg(MSGT_VO, MSGL_V, "[x11] shmget: %s\n", strerror(errno));
++        return VO_X11_SHM_NONE;
++    }
++    shminfo->shmaddr = shmat(shminfo->shmid, NULL, 0);
++    if (shminfo->shmaddr == (char *)-1) {
++        mp_msg(MSGT_VO, MSGL_V, "[x11] shmat: %s\n", strerror(errno));
++        shmctl(shminfo->shmid, IPC_RMID, NULL);
++        return VO_X11_SHM_NONE;
++    }
++    shminfo->readOnly = False;
++    XShmAttach(dpy, shminfo);
++    XSync(dpy, False);
++    /* the segment is destroyed once both sides have detached */
++    shmctl(shminfo->shmid, IPC_RMID, NULL);
++    return VO_X11_SHM_SYSV;
++}
++
++void vo_x11_shm_detach(Display *dpy, XShmSegmentInfo *shminfo, size_t size,
++                       int type)
++{
++    if (type == VO_X11_SHM_NONE)
++        return;
++    XShmDetach(dpy, shminfo);
++#ifdef CONFIG_XCB_SHM
++    if (type == VO_X11_SHM_FD) {
++        munmap(shminfo->shmaddr, size);
++        return;
++    }
++#endif
++    shmdt(shminfo->shmaddr);
++}
++#endif
Index: patches/patch-libvo_x11_common_h
===================================================================
RCS file: patches/patch-libvo_x11_common_h
diff -N patches/patch-libvo_x11_common_h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ patches/patch-libvo_x11_common_h    15 Nov 2026 00:13:54 -0000
@@ -0,0 +1,23 @@
+Shared memory helpers for vo_x11 and vo_xv.
+
+Index: libvo/x11_common.h
+--- libvo/x11_common.h.orig
++++ libvo/x11_common.h
+@@ -147,4 +147,17 @@ void update_xinerama_info(void);
+ 
+ int vo_find_depth_from_visuals(Display *dpy, int screen, Visual 
**visual_return);
+ 
++#ifdef HAVE_SHM
++#include <stddef.h>
++#include <X11/extensions/XShm.h>
++
++#define VO_X11_SHM_NONE 0
++#define VO_X11_SHM_SYSV 1
++#define VO_X11_SHM_FD   2
++
++int vo_x11_shm_attach(Display *dpy, XShmSegmentInfo *shminfo, size_t size);
++void vo_x11_shm_detach(Display *dpy, XShmSegmentInfo *shminfo, size_t size,
++                       int type);
++#endif
++
+ #endif /* MPLAYER_X11_COMMON_H */

Reply via email to