1 /*
2  * x11source.h - X11/transcode bridge code, allowing screen capture.
3  * (C) 2006-2010 - Francesco Romani <fromani -at- gmail -dot- com>
4  *
5  * This file is part of transcode, a video stream processing tool.
6  *
7  * transcode is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * transcode is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef TC_X11SOURCE_H
22 #define TC_X11SOURCE_H
23 
24 #include "config.h"
25 
26 #include <stdint.h>
27 
28 #include "probe.h"
29 #include "framebuffer.h"
30 
31 typedef struct tcX11source_ TCX11Source;
32 
33 /*
34  * Quick summary:
35  *
36  * this code acts as a bridge to a running X11 server, allowing
37  * client code to query about picture attributes, from transcode
38  * point of view, so frame size, frame depth and so on, and allow
39  * to grab images when requested (ok, that's still needs some work
40  * to avoid async responses and other X11 quirks).
41  *
42  * PLEASE NOTE: Only *LOCAL* X11 connections (NOT NETWORKED)
43  * are supported.
44  *
45  * Quick TODO (approx. priorty sorted):
46  * - internal refactoring
47  * - support 15/16 bits color depth.
48  * - grab pointer too.
49  * - support useful extensions like Damage/etc. etc.
50  * - docs for sources
51  * - user docs
52  */
53 
54 #ifdef HAVE_X11
55 
56 #include <X11/Xlib.h>
57 #include <X11/Xutil.h>
58 
59 # ifdef HAVE_X11_SHM
60 # include <sys/ipc.h>
61 # include <sys/shm.h>
62 # include <X11/extensions/XShm.h>
63 # endif /* X11_SHM */
64 
65 #endif /* X11 */
66 
67 struct tcX11source_ {
68 #ifdef HAVE_X11
69     Display *dpy;
70     int screen;
71     Window root;
72     Pixmap pix;
73     GC gc;
74     XImage *image;
75 
76 # ifdef HAVE_X11_SHM
77     XVisualInfo vis_info;
78     XShmSegmentInfo shm_info;
79 # endif /* X11_SHM */
80 #endif /* X11 */
81 
82     int width;
83     int height;
84     int depth;
85 
86     int mode;
87     uint32_t out_fmt; /* TC internal identifier */
88     int conv_fmt; /* precomputed tcv_convert identifier */
89     TCVHandle tcvhandle;
90 
91     int (*acquire_image)(TCX11Source *handle, uint8_t *data, int maxdata);
92     void (*acquire_cursor)(TCX11Source *handle, uint8_t *data, int maxdata);
93     int (*fini)(TCX11Source *handle);
94 };
95 
96 typedef enum tcx11sourcemode_ TCX11SourceMode;
97 enum tcx11sourcemode_ {
98     TC_X11_MODE_PLAIN = 0,
99     TC_X11_MODE_SHM,
100 
101     TC_X11_MODE_BEST = 255, /* this MUST be the last one */
102 };
103 
104 
105 /*
106  * tc_x11source_is_display_name:
107  *     check if given name looks like an X11 display ID.
108  *
109  *     PLEASE NOTE: only LOCAL display are supported (^:[0-9]+\.[0-9]+$)
110  *
111  * Parameters:
112  *     name: ID to be verified
113  * Return Value:
114  *     TC_TRUE: given name looks like an X11 display ID (so it can
115  *              be used as argument for _open, see below).
116  *     TC_FALSE: otherwise.
117  */
118 int tc_x11source_is_display_name(const char *name);
119 
120 /*
121  * tc_x11source_probe:
122  *      fetch image parameters through given connection and
123  *      store them into given info structure.
124  *
125  * Parameters:
126  *      handle: connection handle to be used for probing.
127  *        info: pointer to a ProbeInfo strucutre which will
128  *              hold probed informations.
129  * Return Value:
130  *      -1: error on connection, reason will be tc_log_*()'d out.
131  *       0: succesfull
132  *       1: wrong (NULL) parameters.
133  */
134 int tc_x11source_probe(TCX11Source *handle, ProbeInfo *info);
135 
136 /*
137  * tc_x11source_open:
138  *      connext to given LOCAL X11 display, and prepare ofr later
139  *      probing and/or image acquisition.
140  *
141  * Parameters:
142  *       handle: connection handle to be used. (Allocation must
143  *               be handled by caller).
144  *      display: LOCAL X11 display identifier to connect on.
145  *         mode: select X extensions to use, if avalaible.
146  *       format: image (colorspace) format to be used in
147  *               tc_x11source_acquire. Currently only following
148  *               formats are supported:
149  *               TC_CODEC_RGB, TC_CODEC_YUV420P, TC_CODEC_YUV422P
150  * Return Value:
151  *      -1: error on connection, reason will be tc_log_*()'d out.
152  *       0: succesfull
153  *       1: wrong (NULL) parameters.
154  */
155 int tc_x11source_open(TCX11Source *handle, const char *display,
156                       int mode, uint32_t format);
157 
158 /*
159  * tc_x11source_close:
160  *      close an X11 connection represented by given handle, and
161  *      releases all acquired resources.
162  *
163  * Parameters:
164  *      handle: connection handle to be closed.
165  * Return Value:
166  *      -1: error on connection, reason will be tc_log_*()'d out.
167  *       0: succesfull
168  *       1: wrong (NULL) parameters.
169  */
170 int tc_x11source_close(TCX11Source *handle);
171 
172 /*
173  * tc_x11source_acquire:
174  *     grab a screenshot from given X11 source connection, convert
175  *     it in RGB24 format and store it in given buffer, if this buffer
176  *     is large enough to store the full picture.
177  *
178  * Parameters:
179  *      handle: connection handle to be used for picture acquisition.
180  *        data: picture buffer
181  *     maxdata: size of picture buffer
182  * Return Value:
183  *     -1: can't get image data from X11 connection
184  *      0: image buffer too small, so buffer was left untouched
185  *     >0: size of acquire dimage.
186  */
187 int tc_x11source_acquire(TCX11Source *handle, uint8_t *data, int maxdata);
188 
189 
190 #endif /* TC_X11SOURCE_H */
191