Fix up according to Coding Style
[pulseaudio-mirror.git] / src / pulsecore / x11prop.c
blob8df327883523a94abce7fdb31c467c96d35cb2c3
1 /***
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.
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.
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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <string.h>
28 #include "x11prop.h"
30 #include <pulsecore/macro.h>
32 #include <xcb/xproto.h>
33 #include <xcb/xcb_atom.h>
35 #define PA_XCB_FORMAT 8
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;
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;
47 return NULL;
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_atom_t a;
54 pa_assert(xcb);
55 pa_assert(name);
56 pa_assert(data);
58 if ((xs = screen_of_display(xcb, screen))) {
59 a = xcb_atom_get(xcb, name);
60 xcb_change_property(xcb, XCB_PROP_MODE_REPLACE, xs->root, a, STRING, PA_XCB_FORMAT, (int) strlen(data), (const void*) data);
64 void pa_x11_del_prop(xcb_connection_t *xcb, int screen, const char *name) {
65 xcb_screen_t *xs;
66 xcb_atom_t a;
68 pa_assert(xcb);
69 pa_assert(name);
71 if ((xs = screen_of_display(xcb, screen))) {
72 a = xcb_atom_get(xcb, name);
73 xcb_delete_property(xcb, xs->root, a);
77 char* pa_x11_get_prop(xcb_connection_t *xcb, int screen, const char *name, char *p, size_t l) {
78 char *ret = NULL;
79 int len;
80 xcb_get_property_cookie_t req;
81 xcb_get_property_reply_t* prop = NULL;
82 xcb_screen_t *xs;
83 xcb_atom_t a;
85 pa_assert(xcb);
86 pa_assert(name);
87 pa_assert(p);
90 xs = screen_of_display(xcb, screen);
92 * Also try and get the settings from the first screen.
93 * This allows for e.g. a Media Center to run on screen 1 (e.g. HDMI) and have
94 * different defaults (e.g. prefer the HDMI sink) than the primary screen 0
95 * which uses the Internal Audio sink.
97 if (!xs && 0 != screen)
98 xs = screen_of_display(xcb, 0);
100 if (xs) {
101 a = xcb_atom_get(xcb, name);
103 req = xcb_get_property(xcb, 0, xs->root, a, STRING, 0, (uint32_t)(l-1));
104 prop = xcb_get_property_reply(xcb, req, NULL);
106 if (!prop)
107 goto finish;
109 if (PA_XCB_FORMAT != prop->format)
110 goto finish;
112 len = xcb_get_property_value_length(prop);
113 if (len < 1 || len >= (int)l)
114 goto finish;
116 memcpy(p, xcb_get_property_value(prop), len);
117 p[len] = 0;
119 ret = p;
122 finish:
124 if (prop)
125 free(prop);
127 return ret;