summaryrefslogtreecommitdiffstats
path: root/debian/transcode/transcode-1.1.7/import/x11source.c
blob: 0b1c6bbafd3dd5b2c8ef69a4612e950199bb32d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
 * x11source.c - X11/transcode bridge code, allowing screen capture.
 * (C) 2006-2010 - Francesco Romani <fromani -at- gmail -dot- com>
 *
 * This file is part of transcode, a video stream processing tool.
 *
 * transcode is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * transcode is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>

#include "transcode.h"

#include "magic.h"
#include "x11source.h"

#include "libtc/ratiocodes.h"
#include "libtc/tccodecs.h"
#include "libtc/tcframes.h"
#include "libtcvideo/tcvideo.h"

/*
 * TODO:
 * - internal docs
 * - internla refactoring to properly grab cursor
 */

#ifdef HAVE_X11

/*************************************************************************/
/* cursor grabbing support. */

#ifdef HAVE_X11_FIXES

#include <X11/extensions/Xfixes.h>


static void tc_x11source_acquire_cursor_fixes(TCX11Source *handle,
                                              uint8_t *data, int maxdata)
{
    XFixesCursorImage *cursor = XFixesGetCursorImage(handle->dpy);

    if (cursor == NULL) {
        /* this MUST be noisy! */
        tc_log_warn(__FILE__, "failed to get cursor image");
    } else {
        /* FIXME: this has to be rewritten and need significant
         * internal refactoring :( */
    }
}

#endif /* HAVE_X11_FIXES */

/* FIXME: explain why don't use funcpointers in here */
static void tc_x11source_acquire_cursor_plain(TCX11Source *handle,
                                              uint8_t *data, int maxdata)
{
    static int warn = 0;

    if (!warn) {
        tc_log_warn(__FILE__, "cursor grabbing not supported!");
        warn = 1;
    }
}

static void tc_x11source_init_cursor(TCX11Source *handle)
{
    /* sane default if we don't have any better */
    handle->acquire_cursor = tc_x11source_acquire_cursor_plain;
#ifdef HAVE_X11_FIXES
    handle->acquire_cursor = tc_x11source_acquire_cursor_fixes;
#endif
}

/*************************************************************************/

int tc_x11source_is_display_name(const char *name)
{
    if (name != NULL && strlen(name) != 0) {
        uint32_t disp, screen;
        int ret = sscanf(name, ":%u.%u", &disp, &screen);
        if (ret == 2) {
            /* looks like a display specifier */
            return TC_TRUE;
        }
    }
    return TC_FALSE;
}

int tc_x11source_probe(TCX11Source *handle, ProbeInfo *info)
{
    if (handle != NULL && info != NULL) {
        info->width = handle->width;
        info->height = handle->height;
        info->codec = handle->out_fmt;
        info->magic = TC_MAGIC_X11; /* enforce */
        info->asr = 1; /* force 1:1 ASR (XXX) */
        /* FPS/FRC MUST BE choosed by user; that's only a kind suggestion */
        info->fps = 10.0;
        tc_frc_code_from_value(&info->frc, info->fps);
        
        info->num_tracks = 0; /* no audio, here */
        return 0;
    }

    return 1;
}

/*************************************************************************/

static int tc_x11source_acquire_image_plain(TCX11Source *handle,
                                           uint8_t *data, int maxdata)
{
    int size = -1;

    /* but draw such areas if windows are opaque */
    /* FIXME: switch to XCreateImage? */
    handle->image = XGetImage(handle->dpy, handle->pix, 0, 0, 
                              handle->width, handle->height,
                              AllPlanes, ZPixmap);

    if (handle->image == NULL || handle->image->data == NULL) {
        tc_log_error(__FILE__, "cannot get X image");
    } else {
        size = (int)tc_video_frame_size(handle->image->width,
                                        handle->image->height,
                                        handle->out_fmt);
        if (size <= maxdata) {
            tcv_convert(handle->tcvhandle, handle->image->data, data,
                        handle->image->width, handle->image->height,
                        IMG_BGRA32, handle->conv_fmt);
        } else {
            size = 0;
        }
        XDestroyImage(handle->image);
    }
    return size;
}

static int tc_x11source_fini_plain(TCX11Source *handle)
{
    return 0;
}

static int tc_x11source_init_plain(TCX11Source *handle)
{
    handle->acquire_image = tc_x11source_acquire_image_plain;
    handle->fini = tc_x11source_fini_plain;
    return 0;
}


/*************************************************************************/

#ifdef HAVE_X11_SHM

static int tc_x11source_acquire_image_shm(TCX11Source *handle,
                                          uint8_t *data, int maxdata)
{
    int size = -1;
    Status ret;

    /* but draw such areas if windows are opaque */
    ret = XShmGetImage(handle->dpy, handle->pix, handle->image,
                       0, 0, AllPlanes);

    if (!ret || handle->image == NULL || handle->image->data == NULL) {
        tc_log_error(__FILE__, "cannot get X image (using SHM)");
    } else {
        size = (int)tc_video_frame_size(handle->image->width,
                                        handle->image->height,
                                        handle->out_fmt);
        if (size <= maxdata) {
            tcv_convert(handle->tcvhandle, handle->image->data, data,
                        handle->image->width, handle->image->height,
                        IMG_BGRA32, handle->conv_fmt);
        } else {
            size = 0;
        }
    }
    return size;
}

static int tc_x11source_fini_shm(TCX11Source *handle)
{
    Status ret = XShmDetach(handle->dpy, &handle->shm_info);
    if (!ret) { /* XXX */
        tc_log_error(__FILE__, "failed to attach SHM from Xserver");
        return -1;
    }
    XDestroyImage(handle->image);
    handle->image = NULL;

    XSync(handle->dpy, False); /* XXX */
    if (shmdt(handle->shm_info.shmaddr) != 0) {
        tc_log_error(__FILE__, "failed to destroy shared memory segment");
        return -1;
    }
    return 0;
}

static int tc_x11source_init_shm(TCX11Source *handle)
{
    Status ret;

    ret = XMatchVisualInfo(handle->dpy, handle->screen, handle->depth,
                           DirectColor, &handle->vis_info);
    if (!ret) {
        tc_log_error(__FILE__, "Can't match visual information");
        goto xshm_failed;
    }
    handle->image = XShmCreateImage(handle->dpy, handle->vis_info.visual,
                                    handle->depth, ZPixmap,
                                    NULL, &handle->shm_info,
                                    handle->width, handle->height);
    if (handle->image == NULL) {
        tc_log_error(__FILE__, "XShmCreateImage failed.");
        goto xshm_failed_image;
    }
    handle->shm_info.shmid = shmget(IPC_PRIVATE,
                                    handle->image->bytes_per_line * handle->image->height,
                                    IPC_CREAT | 0777);
    if (handle->shm_info.shmid < 0) {
        tc_log_error(__FILE__, "failed to create shared memory segment");
        goto xshm_failed_image;
    }
    handle->shm_info.shmaddr = shmat(handle->shm_info.shmid, NULL, 0);
    if (handle->shm_info.shmaddr == (void*)-1) {
        tc_log_error(__FILE__, "failed to attach shared memory segment");
        goto xshm_failed_image;
    }
    
    shmctl(handle->shm_info.shmid, IPC_RMID, 0); /* XXX */

    handle->image->data = handle->shm_info.shmaddr;
    handle->shm_info.readOnly = False;

    ret = XShmAttach(handle->dpy, &handle->shm_info);
    if (!ret) {
        tc_log_error(__FILE__, "failed to attach SHM to Xserver");
        goto xshm_failed_image;
    }

    XSync(handle->dpy, False);
    handle->mode = TC_X11_MODE_SHM;
    handle->acquire_image = tc_x11source_acquire_image_shm;
    handle->fini = tc_x11source_fini_shm;

    return 0;

xshm_failed_image:
    XDestroyImage(handle->image);
    handle->image = NULL;
xshm_failed:
    return -1;
}

/*************************************************************************/

#endif /* X11_SHM */

static int tc_x11source_map_format(TCX11Source *handle, uint32_t format)
{
    int ret = -1;

    if (handle != NULL) {
        ret = 0;
        switch (format) {
          case CODEC_RGB: /* compatibility */
          case TC_CODEC_RGB:
            handle->out_fmt = TC_CODEC_RGB;
            handle->conv_fmt = IMG_RGB24;
            if (verbose >= TC_DEBUG) {
                tc_log_info(__FILE__, "output colorspace: RGB24");
            }
            break;
          case CODEC_YUV: /* compatibility */
          case TC_CODEC_YUV420P:
            handle->out_fmt = TC_CODEC_YUV420P;
            handle->conv_fmt = IMG_YUV420P;
            if (verbose >= TC_DEBUG) {
                tc_log_info(__FILE__, "output colorspace: YUV420P");
            }
            break;
          case CODEC_YUV422: /* compatibility */
          case TC_CODEC_YUV422P:
            handle->out_fmt = TC_CODEC_YUV422P;
            handle->conv_fmt = IMG_YUV422P;
            if (verbose >= TC_DEBUG) {
                tc_log_info(__FILE__, "output colorspace: YUV4222");
            }
            break;
          default:
            tc_log_error(__FILE__, "unknown colorspace requested: 0x%x",
                         format);
            ret = -1;
        }
    }
    return ret;
}

int tc_x11source_acquire(TCX11Source *handle, uint8_t *data, int maxdata)
{
    int size = -1;

    if (handle == NULL || data == NULL || maxdata <= 0) {
        tc_log_error(__FILE__, "x11source_acquire: wrong (NULL) parameters");
        return size;
    }

    XLockDisplay(handle->dpy);
    /* OK, let's hack a bit our GraphicContext */
    XSetSubwindowMode(handle->dpy, handle->gc, IncludeInferiors);
    /* don't catch areas of windows covered by children windows */
    XCopyArea(handle->dpy, handle->root, handle->pix, handle->gc,
              0, 0, handle->width, handle->height, 0, 0);

    XSetSubwindowMode(handle->dpy, handle->gc, ClipByChildren);
    /* but draw such areas if windows are opaque */
    
    size = handle->acquire_image(handle, data, maxdata);
    if (size > 0) {
        handle->acquire_cursor(handle, data, maxdata); /* cannot fail */
    }
    XUnlockDisplay(handle->dpy);
    return size;
}

int tc_x11source_close(TCX11Source *handle)
{
    if (handle != NULL) {
        if (handle->dpy != NULL) {
            int ret = handle->fini(handle);
            if (ret != 0) {
                return ret;
            }

            tcv_free(handle->tcvhandle);

            XFreePixmap(handle->dpy, handle->pix); /* XXX */
            XFreeGC(handle->dpy, handle->gc); /* XXX */

            ret = XCloseDisplay(handle->dpy);
            if (ret == 0) {
                handle->dpy = NULL;
            } else {
                tc_log_error(__FILE__, "XCloseDisplay() failed: %i", ret);
                return -1;
            }
        }
    }
    return 0;
}

int tc_x11source_open(TCX11Source *handle, const char *display,
                      int mode, uint32_t format)
{
    XWindowAttributes winfo;
    Status ret;
    int err;

    if (handle == NULL) {
        return 1;
    }
    err = tc_x11source_map_format(handle, format);
    if (err != 0) {
        return err;
    }

    handle->mode = mode;
    handle->dpy = XOpenDisplay(display);
    if (handle->dpy == NULL) {
        tc_log_error(__FILE__, "failed to open display %s",
                     (display != NULL) ?display :"default");
        goto open_failed;
    }

    handle->screen = DefaultScreen(handle->dpy);
    handle->root = RootWindow(handle->dpy, handle->screen);
    /* Get the parameters of the root winfow */
    ret = XGetWindowAttributes(handle->dpy, handle->root, &winfo);
    if (!ret) {
        tc_log_error(__FILE__, "failed to get root window attributes");
        goto link_failed;
    }

    handle->width = winfo.width;
    handle->height = winfo.height;
    handle->depth = winfo.depth;

    if (handle->depth != 24) { /* XXX */
        tc_log_error(__FILE__, "Non-truecolor display depth"
                               " not supported. Yet.");
        goto link_failed;
    }

    if (verbose >= TC_STATS) {
        tc_log_info(__FILE__, "display properties: %ix%i@%i", 
                    handle->width, handle->height, handle->depth);
    }

    handle->pix = XCreatePixmap(handle->dpy, handle->root,
                                handle->width, handle->height,
                                handle->depth); /* XXX */
    if (!handle->pix) {
        tc_log_error(__FILE__, "Can't allocate Pixmap");
        goto pix_failed;
    }
 
    handle->gc = XCreateGC(handle->dpy, handle->root, 0, 0);
    /* FIXME: what about failures? */

    handle->tcvhandle = tcv_init();
    if (!handle->tcvhandle)
        goto tcv_failed;

    tc_x11source_init_cursor(handle); /* cannot fail */

#ifdef HAVE_X11_SHM
    if (XShmQueryExtension(handle->dpy) != 0
      && (mode & TC_X11_MODE_SHM)) {
        if (tc_x11source_init_shm(handle) < 0)
            goto init_failed;
    } else
#endif /* X11_SHM */
    if (tc_x11source_init_plain(handle) < 0)
        goto init_failed;
    return 0;

  init_failed:
    tcv_free(handle->tcvhandle);
  tcv_failed:
    XFreeGC(handle->dpy, handle->gc);
    XFreePixmap(handle->dpy, handle->pix);
  pix_failed:
  link_failed:
    XCloseDisplay(handle->dpy);
  open_failed:
    return -1;
}


#else /* HAVE_X11 */


int tc_x11source_open(TCX11Source *handle, const char *display,
                      int mode, uint32_t format)
{
    tc_log_error(__FILE__, "X11 support unavalaible");
    return -1;
}

int tc_x11source_close(TCX11Source *handle)
{
    tc_log_error(__FILE__, "X11 support unavalaible");
    return 0;
}

int tc_x11source_probe(TCX11Source *handle, ProbeInfo *info)
{
    tc_log_error(__FILE__, "X11 support unavalaible");
    return -1;
}

int tc_x11source_acquire(TCX11Source *handle, uint8_t *data, int maxdata)
{
    tc_log_error(__FILE__, "X11 support unavalaible");
    return -1;
}

int tc_x11source_is_display_name(const char *name)
{
    return TC_FALSE;
}

#endif /* HAVE_X11 */

/*************************************************************************/

/*
 * Local variables:
 *   c-file-style: "stroustrup"
 *   c-file-offsets: ((case-label . *) (statement-case-intro . *))
 *   indent-tabs-mode: nil
 * End:
 *
 * vim: expandtab shiftwidth=4:
 */