]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/pictures.c
XCB: split picture (X11/XV) and events (all) header
[vlc] / modules / video_output / xcb / pictures.c
1 /**
2  * @file pictures.c
3  * @brief Pictures management code for XCB video output plugins
4  */
5 /*****************************************************************************
6  * Copyright © 2009-2013 Rémi Denis-Courmont
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <assert.h>
29
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_SHM_H
32 # include <sys/shm.h>
33 # include <sys/stat.h>
34 #endif
35
36 #include <xcb/xcb.h>
37 #include <xcb/shm.h>
38
39 #include <vlc_common.h>
40 #include <vlc_vout_display.h>
41
42 #include "pictures.h"
43 #include "events.h"
44
45 /** Check MIT-SHM shared memory support */
46 bool XCB_shm_Check (vlc_object_t *obj, xcb_connection_t *conn)
47 {
48 #ifdef HAVE_SYS_SHM_H
49     xcb_shm_query_version_cookie_t ck;
50     xcb_shm_query_version_reply_t *r;
51
52     ck = xcb_shm_query_version (conn);
53     r = xcb_shm_query_version_reply (conn, ck, NULL);
54     if (r != NULL)
55     {
56         free (r);
57         return true;
58     }
59     msg_Err (obj, "shared memory (MIT-SHM) not available");
60     msg_Warn (obj, "display will be slow");
61 #else
62     msg_Warn (obj, "shared memory (MIT-SHM) not implemented");
63     (void) conn;
64 #endif
65     return false;
66 }
67
68 /**
69  * Initialize a picture buffer as shared memory, according to the video output
70  * format. If a attach is true, the segment is attached to
71  * the X server (MIT-SHM extension).
72  */
73 int XCB_pictures_Alloc (vout_display_t *vd, picture_resource_t *res,
74                         size_t size, xcb_connection_t *conn, bool attach)
75 {
76     res->p_sys = malloc (sizeof(*res->p_sys));
77     if (!res->p_sys)
78         return VLC_EGENERIC;
79
80 #ifdef HAVE_SYS_SHM_H
81     /* Allocate shared memory segment */
82     int id = shmget (IPC_PRIVATE, size, IPC_CREAT | S_IRWXU);
83     if (id == -1)
84     {
85         msg_Err (vd, "shared memory allocation error: %m");
86         free (res->p_sys);
87         return VLC_EGENERIC;
88     }
89
90     /* Attach the segment to VLC */
91     void *shm = shmat (id, NULL, 0 /* read/write */);
92     if (-1 == (intptr_t)shm)
93     {
94         msg_Err (vd, "shared memory attachment error: %m");
95         shmctl (id, IPC_RMID, 0);
96         free (res->p_sys);
97         return VLC_EGENERIC;
98     }
99
100     xcb_shm_seg_t segment;
101     if (attach)
102     {
103         /* Attach the segment to X */
104         xcb_void_cookie_t ck;
105
106         segment = xcb_generate_id (conn);
107         ck = xcb_shm_attach_checked (conn, segment, id, 1);
108
109         switch (XCB_error_Check (vd, conn, "shared memory server-side error",
110                                  ck))
111         {
112             case 0:
113                 break;
114
115             case XCB_ACCESS:
116             {
117                 struct shmid_ds buf;
118                 /* Retry with promiscuous permissions */
119                 shmctl (id, IPC_STAT, &buf);
120                 buf.shm_perm.mode |= S_IRGRP|S_IROTH;
121                 shmctl (id, IPC_SET, &buf);
122                 ck = xcb_shm_attach_checked (conn, segment, id, 1);
123                 if (XCB_error_Check (vd, conn, "same error on retry", ck) == 0)
124                     break;
125                 /* fall through */
126             }
127
128             default:
129                 msg_Info (vd, "using buggy X11 server - SSH proxying?");
130                 segment = 0;
131         }
132     }
133     else
134         segment = 0;
135
136     shmctl (id, IPC_RMID, NULL);
137     res->p_sys->segment = segment;
138     res->p->p_pixels = shm;
139 #else
140     assert (!attach);
141     res->p_sys->segment = 0;
142
143     /* XXX: align on 32 bytes for VLC chroma filters */
144     res->p->p_pixels = malloc (size);
145     if (unlikely(res->p->p_pixels == NULL))
146     {
147         free (res->p_sys);
148         return VLC_EGENERIC;
149     }
150 #endif
151     return VLC_SUCCESS;
152 }
153
154 /**
155  * Release picture private data: detach the shared memory segment.
156  */
157 void XCB_pictures_Free (picture_resource_t *res, xcb_connection_t *conn)
158 {
159 #ifdef HAVE_SYS_SHM_H
160     xcb_shm_seg_t segment = res->p_sys->segment;
161
162     if (conn != NULL && segment != 0)
163         xcb_shm_detach (conn, segment);
164     shmdt (res->p->p_pixels);
165 #else
166     free (res->p->p_pixels);
167 #endif
168 }
169