1/***
2  This file is part of PulseAudio.
3
4  Copyright 2004-2006 Lennart Poettering
5
6  PulseAudio is free software; you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation; either version 2.1 of the License,
9  or (at your option) any later version.
10
11  PulseAudio is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15
16  You should have received a copy of the GNU Lesser General Public License
17  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18***/
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <string.h>
25
26#include "x11prop.h"
27
28#include <pulsecore/macro.h>
29
30#include <xcb/xproto.h>
31
32#define PA_XCB_FORMAT 8
33
34static xcb_screen_t *screen_of_display(xcb_connection_t *xcb, int screen) {
35    const xcb_setup_t *s;
36    xcb_screen_iterator_t iter;
37
38    if ((s = xcb_get_setup(xcb))) {
39        iter = xcb_setup_roots_iterator(s);
40        for (; iter.rem; --screen, xcb_screen_next(&iter))
41            if (0 == screen)
42                return iter.data;
43    }
44    return NULL;
45}
46
47void pa_x11_set_prop(xcb_connection_t *xcb, int screen, const char *name, const char *data) {
48    xcb_screen_t *xs;
49    xcb_intern_atom_reply_t *reply;
50
51    pa_assert(xcb);
52    pa_assert(name);
53    pa_assert(data);
54
55    if ((xs = screen_of_display(xcb, screen))) {
56        reply = xcb_intern_atom_reply(xcb,
57                                      xcb_intern_atom(xcb, 0, strlen(name), name),
58                                      NULL);
59
60        if (reply) {
61            xcb_change_property(xcb, XCB_PROP_MODE_REPLACE, xs->root, reply->atom,
62                                XCB_ATOM_STRING, PA_XCB_FORMAT,
63                                (int) strlen(data), (const void*) data);
64
65            free(reply);
66        }
67    }
68}
69
70void pa_x11_del_prop(xcb_connection_t *xcb, int screen, const char *name) {
71    xcb_screen_t *xs;
72    xcb_intern_atom_reply_t *reply;
73
74    pa_assert(xcb);
75    pa_assert(name);
76
77    if ((xs = screen_of_display(xcb, screen))) {
78        reply = xcb_intern_atom_reply(xcb,
79                                      xcb_intern_atom(xcb, 0, strlen(name), name),
80                                      NULL);
81
82        if (reply) {
83            xcb_delete_property(xcb, xs->root, reply->atom);
84            free(reply);
85        }
86    }
87}
88
89char* pa_x11_get_prop(xcb_connection_t *xcb, int screen, const char *name, char *p, size_t l) {
90    char *ret = NULL;
91    int len;
92    xcb_get_property_cookie_t req;
93    xcb_get_property_reply_t* prop = NULL;
94    xcb_screen_t *xs;
95    xcb_intern_atom_reply_t *reply;
96
97    pa_assert(xcb);
98    pa_assert(name);
99    pa_assert(p);
100
101    xs = screen_of_display(xcb, screen);
102    /*
103     * Also try and get the settings from the first screen.
104     * This allows for e.g. a Media Center to run on screen 1 (e.g. HDMI) and have
105     * different defaults (e.g. prefer the HDMI sink) than the primary screen 0
106     * which uses the Internal Audio sink.
107     */
108    if (!xs && 0 != screen)
109        xs = screen_of_display(xcb, 0);
110
111    if (xs) {
112        reply = xcb_intern_atom_reply(xcb,
113                                      xcb_intern_atom(xcb, 0, strlen(name), name),
114                                      NULL);
115
116        if (!reply)
117            goto finish;
118
119        req = xcb_get_property(xcb, 0, xs->root, reply->atom, XCB_ATOM_STRING, 0, (uint32_t)(l-1));
120        free(reply);
121        prop = xcb_get_property_reply(xcb, req, NULL);
122
123        if (!prop)
124            goto finish;
125
126        if (PA_XCB_FORMAT != prop->format)
127            goto finish;
128
129        len = xcb_get_property_value_length(prop);
130        if (len < 1 || len >= (int)l)
131            goto finish;
132
133        memcpy(p, xcb_get_property_value(prop), len);
134        p[len] = 0;
135
136        ret = p;
137    }
138
139finish:
140
141    if (prop)
142        free(prop);
143
144    return ret;
145}
146