99ea55df12e9c90b8466bedfca0ff1d48a6ea023
[profile/ivi/pulseaudio.git] / src / pulsecore / x11prop.c
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, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27
28 #include "x11prop.h"
29
30 #include <pulsecore/macro.h>
31
32 #include <xcb/xproto.h>
33 #include <xcb/xcb_atom.h>
34
35 #define PA_XCB_FORMAT 8
36
37 static xcb_screen_t *screen_of_display(xcb_connection_t *xcb, int screen) {
38     const xcb_setup_t *s;
39     xcb_screen_iterator_t iter;
40
41     if ((s = xcb_get_setup(xcb))) {
42         iter = xcb_setup_roots_iterator(s);
43         for (; iter.rem; --screen, xcb_screen_next(&iter))
44             if (0 == screen)
45                 return iter.data;
46     }
47     return NULL;
48 }
49
50 void pa_x11_set_prop(xcb_connection_t *xcb, int screen, const char *name, const char *data) {
51     xcb_screen_t *xs;
52     xcb_intern_atom_cookie_t cookie;
53     xcb_intern_atom_reply_t *reply;
54
55     pa_assert(xcb);
56     pa_assert(name);
57     pa_assert(data);
58
59     if ((xs = screen_of_display(xcb, screen))) {
60         cookie = xcb_intern_atom(xcb, 0, strlen(name), name);
61         reply = xcb_intern_atom_reply(xcb, cookie, NULL);
62
63         xcb_change_property(xcb, XCB_PROP_MODE_REPLACE, xs->root, reply->atom, XCB_ATOM_STRING, PA_XCB_FORMAT, (int) strlen(data), (const void*) data);
64     }
65 }
66
67 void pa_x11_del_prop(xcb_connection_t *xcb, int screen, const char *name) {
68     xcb_screen_t *xs;
69     xcb_intern_atom_cookie_t cookie;
70     xcb_intern_atom_reply_t *reply;
71
72     pa_assert(xcb);
73     pa_assert(name);
74
75     if ((xs = screen_of_display(xcb, screen))) {
76         cookie = xcb_intern_atom(xcb, 0, strlen(name), name);
77         reply = xcb_intern_atom_reply(xcb, cookie, NULL);
78
79         xcb_delete_property(xcb, xs->root, reply->atom);
80     }
81 }
82
83 char* pa_x11_get_prop(xcb_connection_t *xcb, int screen, const char *name, char *p, size_t l) {
84     char *ret = NULL;
85     int len;
86     xcb_get_property_cookie_t req;
87     xcb_get_property_reply_t* prop = NULL;
88     xcb_screen_t *xs;
89     xcb_intern_atom_cookie_t cookie;
90     xcb_intern_atom_reply_t *reply;
91
92     pa_assert(xcb);
93     pa_assert(name);
94     pa_assert(p);
95
96
97     xs = screen_of_display(xcb, screen);
98     /*
99      * Also try and get the settings from the first screen.
100      * This allows for e.g. a Media Center to run on screen 1 (e.g. HDMI) and have
101      * different defaults (e.g. prefer the HDMI sink) than the primary screen 0
102      * which uses the Internal Audio sink.
103      */
104     if (!xs && 0 != screen)
105         xs = screen_of_display(xcb, 0);
106
107     if (xs) {
108         cookie = xcb_intern_atom(xcb, 0, strlen(name), name);
109         reply = xcb_intern_atom_reply(xcb, cookie, NULL);
110
111         req = xcb_get_property(xcb, 0, xs->root, reply->atom, XCB_ATOM_STRING, 0, (uint32_t)(l-1));
112         prop = xcb_get_property_reply(xcb, req, NULL);
113
114         if (!prop)
115             goto finish;
116
117         if (PA_XCB_FORMAT != prop->format)
118             goto finish;
119
120         len = xcb_get_property_value_length(prop);
121         if (len < 1 || len >= (int)l)
122             goto finish;
123
124         memcpy(p, xcb_get_property_value(prop), len);
125         p[len] = 0;
126
127         ret = p;
128     }
129
130 finish:
131
132     if (prop)
133         free(prop);
134
135     return ret;
136 }