Use MIT-SHM 1.2 (fd passing) with POSIX shm instead of SysV shm.

xcb owns the fd passed to xcb_shm_attach_fd() and closes it after
sending it, so it must not be closed again later.  The mapping comes
from mmap(2), so release it with munmap(2) rather than shmdt(2).
Only use SHM if the server supports MIT-SHM 1.2 and the connection
is a unix domain socket.

Index: video/out/vo_x11.c
--- video/out/vo_x11.c.orig
+++ video/out/vo_x11.c
@@ -20,6 +20,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include <sys/types.h>
 
 #include <libswscale/swscale.h>
@@ -36,9 +37,11 @@
 #include "present_sync.h"
 #include "x11_common.h"
 
-#include <sys/ipc.h>
-#include <sys/shm.h>
+#include <sys/mman.h>
+#include <sys/socket.h>
 #include <X11/extensions/XShm.h>
+#include <X11/Xlib-xcb.h>
+#include <xcb/shm.h>
 
 #include "sub/osd.h"
 #include "sub/draw_bmp.h"
@@ -76,15 +79,90 @@ struct priv {
 
     int Shmem_Flag;
     XShmSegmentInfo Shminfo[2];
+    size_t shmlen[2];
     int Shm_Warned_Slow;
 };
 
 static bool resize(struct vo *vo);
 
+/*
+ * MIT-SHM 1.2 (fd passing) is needed, and fd passing only works over
+ * a unix domain socket.
+ */
+static bool shm_fd_usable(Display *dpy)
+{
+    struct sockaddr_storage ss;
+    socklen_t sslen = sizeof(ss);
+    int major, minor;
+    Bool pixmaps;
+
+    if (!XShmQueryVersion(dpy, &major, &minor, &pixmaps))
+        return false;
+    if (major < 1 || (major == 1 && minor < 2))
+        return false;
+    if (getsockname(ConnectionNumber(dpy), (struct sockaddr *)&ss,
+                    &sslen) == -1)
+        return false;
+    return ss.ss_family == AF_UNIX;
+}
+
+/*
+ * Create a POSIX shm segment of len bytes, map it and pass it to the
+ * X server.  xcb owns the fd once it has been handed to
+ * xcb_shm_attach_fd_checked() and closes it after sending it, so it
+ * must not be closed here after that point.
+ */
+static bool shm_fd_attach(Display *dpy, XShmSegmentInfo *si, size_t len)
+{
+    char name[] = "/tmp/mpv-x11-XXXXXXXXXX";
+    xcb_connection_t *c = XGetXCBConnection(dpy);
+    xcb_generic_error_t *err;
+    void *addr;
+    int fd;
+
+    *si = (XShmSegmentInfo){0};
+    si->shmid = -1;
+
+    fd = shm_mkstemp(name);
+    if (fd == -1)
+        return false;
+    shm_unlink(name);
+    if (ftruncate(fd, len) == -1) {
+        close(fd);
+        return false;
+    }
+    addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+    if (addr == MAP_FAILED) {
+        close(fd);
+        return false;
+    }
+
+    si->shmseg = xcb_generate_id(c);
+    si->readOnly = False;
+    err = xcb_request_check(c, xcb_shm_attach_fd_checked(c, si->shmseg,
+                                                         fd, si->readOnly));
+    if (err) {
+        free(err);
+        munmap(addr, len);
+        *si = (XShmSegmentInfo){0};
+        return false;
+    }
+    si->shmaddr = addr;
+    return true;
+}
+
+static void shm_fd_detach(Display *dpy, XShmSegmentInfo *si, size_t len)
+{
+    XShmDetach(dpy, si);
+    munmap(si->shmaddr, len);
+    *si = (XShmSegmentInfo){0};
+}
+
 static bool getMyXImage(struct priv *p, int foo)
 {
     struct vo *vo = p->vo;
-    if (vo->x11->display_is_local && XShmQueryExtension(vo->x11->display)) {
+    size_t len;
+    if (vo->x11->display_is_local && shm_fd_usable(vo->x11->display)) {
         p->Shmem_Flag = 1;
         vo->x11->ShmCompletionEvent = XShmGetEventBase(vo->x11->display)
                                     + ShmCompletion;
@@ -102,29 +180,18 @@ static bool getMyXImage(struct priv *p, int foo)
             MP_WARN(vo, "Shared memory error,disabling ( Ximage error )\n");
             goto shmemerror;
         }
-        p->Shminfo[foo].shmid = shmget(IPC_PRIVATE,
-                                       p->myximage[foo]->bytes_per_line *
-                                       p->myximage[foo]->height,
-                                       IPC_CREAT | 0777);
-        if (p->Shminfo[foo].shmid < 0) {
+        len = (size_t)p->myximage[foo]->bytes_per_line *
+              p->myximage[foo]->height;
+        if (!shm_fd_attach(vo->x11->display, &p->Shminfo[foo], len)) {
             XDestroyImage(p->myximage[foo]);
-            MP_WARN(vo, "Shared memory error,disabling ( seg id error )\n");
+            p->myximage[foo] = NULL;
+            MP_WARN(vo, "Shared memory error,disabling ( attach error )\n");
             goto shmemerror;
         }
-        p->Shminfo[foo].shmaddr = (char *) shmat(p->Shminfo[foo].shmid, 0, 0);
-
-        if (p->Shminfo[foo].shmaddr == ((char *) -1)) {
-            XDestroyImage(p->myximage[foo]);
-            MP_WARN(vo, "Shared memory error,disabling ( address error )\n");
-            goto shmemerror;
-        }
+        p->shmlen[foo] = len;
         p->myximage[foo]->data = p->Shminfo[foo].shmaddr;
-        p->Shminfo[foo].readOnly = False;
-        XShmAttach(vo->x11->display, &p->Shminfo[foo]);
 
         XSync(vo->x11->display, False);
-
-        shmctl(p->Shminfo[foo].shmid, IPC_RMID, 0);
     } else {
 shmemerror:
         p->Shmem_Flag = 0;
@@ -148,10 +215,9 @@ shmemerror:
 static void freeMyXImage(struct priv *p, int foo)
 {
     struct vo *vo = p->vo;
-    if (p->Shmem_Flag) {
-        XShmDetach(vo->x11->display, &p->Shminfo[foo]);
+    if (p->Shminfo[foo].shmaddr) {
         XDestroyImage(p->myximage[foo]);
-        shmdt(p->Shminfo[foo].shmaddr);
+        shm_fd_detach(vo->x11->display, &p->Shminfo[foo], p->shmlen[foo]);
     } else {
         if (p->myximage[foo]) {
             // XDestroyImage() would free the data too since XFree() just calls
