]> git.sesse.net Git - vlc/blob - modules/video_output/xcb/pictures.c
xcb: clean up non-static function names
[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 "xcb_vlc.h"
43
44 /** Check MIT-SHM shared memory support */
45 bool XCB_shm_Check (vlc_object_t *obj, xcb_connection_t *conn)
46 {
47 #ifdef HAVE_SYS_SHM_H
48     xcb_shm_query_version_cookie_t ck;
49     xcb_shm_query_version_reply_t *r;
50
51     ck = xcb_shm_query_version (conn);
52     r = xcb_shm_query_version_reply (conn, ck, NULL);
53     if (r != NULL)
54     {
55         free (r);
56         return true;
57     }
58     msg_Err (obj, "shared memory (MIT-SHM) not available");
59     msg_Warn (obj, "display will be slow");
60 #else
61     msg_Warn (obj, "shared memory (MIT-SHM) not implemented");
62     (void) conn;
63 #endif
64     return false;
65 }
66
67 /**
68  * Initialize a picture buffer as shared memory, according to the video output
69  * format. If a attach is true, the segment is attached to
70  * the X server (MIT-SHM extension).
71  */
72 int XCB_pictures_Alloc (vout_display_t *vd, picture_resource_t *res,
73                         size_t size, xcb_connection_t *conn, bool attach)
74 {
75     res->p_sys = malloc (sizeof(*res->p_sys));
76     if (!res->p_sys)
77         return VLC_EGENERIC;
78
79 #ifdef HAVE_SYS_SHM_H
80     /* Allocate shared memory segment */
81     int id = shmget (IPC_PRIVATE, size, IPC_CREAT | S_IRWXU);
82     if (id == -1)
83     {
84         msg_Err (vd, "shared memory allocation error: %m");
85         free (res->p_sys);
86         return VLC_EGENERIC;
87     }
88
89     /* Attach the segment to VLC */
90     void *shm = shmat (id, NULL, 0 /* read/write */);
91     if (-1 == (intptr_t)shm)
92     {
93         msg_Err (vd, "shared memory attachment error: %m");
94         shmctl (id, IPC_RMID, 0);
95         free (res->p_sys);
96         return VLC_EGENERIC;
97     }
98
99     xcb_shm_seg_t segment;
100     if (attach)
101     {
102         /* Attach the segment to X */
103         xcb_void_cookie_t ck;
104
105         segment = xcb_generate_id (conn);
106         ck = xcb_shm_attach_checked (conn, segment, id, 1);
107
108         switch (XCB_error_Check (vd, conn, "shared memory server-side error",
109                                  ck))
110         {
111             case 0:
112                 break;
113
114             case XCB_ACCESS:
115             {
116                 struct shmid_ds buf;
117                 /* Retry with promiscuous permissions */
118                 shmctl (id, IPC_STAT, &buf);
119                 buf.shm_perm.mode |= S_IRGRP|S_IROTH;
120                 shmctl (id, IPC_SET, &buf);
121                 ck = xcb_shm_attach_checked (conn, segment, id, 1);
122                 if (XCB_error_Check (vd, conn, "same error on retry", ck) == 0)
123                     break;
124                 /* fall through */
125             }
126
127             default:
128                 msg_Info (vd, "using buggy X11 server - SSH proxying?");
129                 segment = 0;
130         }
131     }
132     else
133         segment = 0;
134
135     shmctl (id, IPC_RMID, NULL);
136     res->p_sys->segment = segment;
137     res->p->p_pixels = shm;
138 #else
139     assert (!attach);
140     res->p_sys->segment = 0;
141
142     /* XXX: align on 32 bytes for VLC chroma filters */
143     res->p->p_pixels = malloc (size);
144     if (unlikely(res->p->p_pixels == NULL))
145     {
146         free (res->p_sys);
147         return VLC_EGENERIC;
148     }
149 #endif
150     return VLC_SUCCESS;
151 }
152
153 /**
154  * Release picture private data: detach the shared memory segment.
155  */
156 void XCB_pictures_Free (picture_resource_t *res, xcb_connection_t *conn)
157 {
158 #ifdef HAVE_SYS_SHM_H
159     xcb_shm_seg_t segment = res->p_sys->segment;
160
161     if (conn != NULL && segment != 0)
162         xcb_shm_detach (conn, segment);
163     shmdt (res->p->p_pixels);
164 #else
165     free (res->p->p_pixels);
166 #endif
167 }
168