summaryrefslogtreecommitdiff
path: root/src/canberra-gtk.c
blob: ed6f029144860f676c79b1b2a1ee2f4e6f05d591 (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
/* $Id$ */

/***
  This file is part of libcanberra.

  Copyright 2008 Lennart Poettering

  libcanberra is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation, either version 2.1 of the
  License, or (at your option) any later version.

  libcanberra 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with libcanberra. If not, If not, see
  <http://www.gnu.org/licenses/>.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>

#include "canberra.h"
#include "canberra-gtk.h"
#include "common.h"
#include "malloc.h"

/**
 * SECTION:canberra-gtk
 * @short_description: Gtk+ libcanberra Bindings
 *
 * libcanberra-gtk provides a few functions that simplify libcanberra
 * usage from Gtk+ programs. It maintains a single application-global
 * ca_context object that is made accessible via
 * ca_gtk_context_get(). More importantly, it provides a few functions
 * to compile event sound property lists based on GtkWidget objects or
 * GdkEvent events.
 */

/**
 * ca_gtk_context_get:
 *
 * libcanberra-gtk maintains a single application-global ca_context
 * object. Use this function to access it. The
 * %CA_PROP_CANBERRA_XDG_THEME_NAME of this context property is dynamically bound to
 * the XSETTINGS setting for the XDG theme name.
 *
 * Returns: a pa_context object
 */

ca_context *ca_gtk_context_get(void) {
    static GStaticPrivate context_private = G_STATIC_PRIVATE_INIT;
    ca_context *c = NULL;
    const char *name;

    if ((c = g_static_private_get(&context_private)))
        return c;

    ca_assert_se(ca_context_create(&c) == CA_SUCCESS);

    if ((name = g_get_application_name()))
        ca_assert_se(ca_context_change_props(c, CA_PROP_APPLICATION_NAME, name, NULL) == 0);

    g_static_private_set(&context_private, c, (GDestroyNotify) ca_context_destroy);

    return c;
}

static GtkWindow* get_toplevel(GtkWidget *w) {
    if (!(w = gtk_widget_get_toplevel(w)))
        return NULL;

    if (!GTK_IS_WINDOW(w))
        return NULL;

    return GTK_WINDOW(w);
}

/**
 * ca_gtk_proplist_set_for_widget:
 * @p: The proplist to store these sound event properties in
 * @w: The Gtk widget to base these sound event properties on
 *
 * Fill in a ca_proplist object for a sound event that shall originate
 * from the specified Gtk Widget. This will fill in properties like
 * %CA_PROP_WINDOW_NAME or %CA_PROP_WINDOW_X11_DISPLAY for you.
 *
 * Returns: 0 on success, negative error code on error.
 */

int ca_gtk_proplist_set_for_widget(ca_proplist *p, GtkWidget *widget) {
    GtkWindow *w;
    int ret;
    const char *t, *role;
    GdkWindow *dw;
    GdkScreen *screen;

    ca_return_val_if_fail(p, CA_ERROR_INVALID);
    ca_return_val_if_fail(widget, CA_ERROR_INVALID);

    if (!(w = get_toplevel(widget)))
        return CA_ERROR_INVALID;

    if ((t = gtk_window_get_title(w)))
        if ((ret = ca_proplist_sets(p, CA_PROP_WINDOW_NAME, t)) < 0)
            return ret;

    if ((role = gtk_window_get_role(w))) {
        if (role && t) {
            char *id = ca_sprintf_malloc("%s#%s", t, role);

            if ((ret = ca_proplist_sets(p, CA_PROP_WINDOW_ID, id)) < 0) {
                ca_free(id);
                return ret;
            }

            ca_free(id);
        }
    } else if (t)
        if ((ret = ca_proplist_sets(p, CA_PROP_WINDOW_ID, t)) < 0)
            return ret;

    if ((t = gtk_window_get_icon_name(w)))
        if ((ret = ca_proplist_sets(p, CA_PROP_WINDOW_ICON_NAME, t)) < 0)
            return ret;

    if ((dw = GTK_WIDGET(w)->window))
        if ((ret = ca_proplist_setf(p, CA_PROP_WINDOW_X11_XID, "%lu", (unsigned long) GDK_WINDOW_XID(dw))) < 0)
            return ret;

    if ((screen = gtk_widget_get_screen(widget))) {

        if ((t = gdk_display_get_name(gdk_screen_get_display(screen))))
            if ((ret = ca_proplist_sets(p, CA_PROP_WINDOW_X11_DISPLAY, t)) < 0)
                return ret;

        if ((ret = ca_proplist_setf(p, CA_PROP_WINDOW_X11_SCREEN, "%i", gdk_screen_get_number(screen))) < 0)
            return ret;

        if (dw)
            if ((ret = ca_proplist_setf(p, CA_PROP_WINDOW_X11_MONITOR, "%i", gdk_screen_get_monitor_at_window(screen, dw))) < 0)
                return ret;
    }

    return CA_SUCCESS;
}

/**
 * ca_gtk_proplist_set_for_event:
 * @p: The proplist to store these sound event properties in
 * @e: The Gdk event to base these sound event properties on
 *
 * Fill in a ca_proplist object for a sound event that is being
 * triggered by the specified Gdk Event. This will fill in properties
 * like %CA_PROP_EVENT_MOUSE_X or %CA_PROP_EVENT_MOUSE_BUTTON for
 * you. This will internally also cal ca_gtk_proplist_set_for_widget()
 * on the widget this event belongs to.
 *
 * Returns: 0 on success, negative error code on error.
 */

int ca_gtk_proplist_set_for_event(ca_proplist *p, GdkEvent *e) {
    gdouble x, y;
    GdkWindow *gw;
    GtkWidget *w = NULL;
    int ret;

    ca_return_val_if_fail(p, CA_ERROR_INVALID);
    ca_return_val_if_fail(e, CA_ERROR_INVALID);

    if ((gw = e->any.window)) {
        gdk_window_get_user_data(gw, (gpointer*) &w);

        if (w)
            if ((ret = ca_gtk_proplist_set_for_widget(p, w)) < 0)
                return ret;
    }

    if (gdk_event_get_root_coords(e, &x, &y)) {

        if ((ret = ca_proplist_setf(p, CA_PROP_EVENT_MOUSE_X, "%0.0f", x)) < 0)
            return ret;

        if ((ret = ca_proplist_setf(p, CA_PROP_EVENT_MOUSE_Y, "%0.0f", y)) < 0)
            return ret;

        if (w)  {
            int width, height;

            width = gdk_screen_get_width(gtk_widget_get_screen(w));
            height = gdk_screen_get_height(gtk_widget_get_screen(w));

            if ((ret = ca_proplist_setf(p, CA_PROP_EVENT_MOUSE_HPOS, "%0.0f", x/width)) < 0)
                return ret;

            if ((ret = ca_proplist_setf(p, CA_PROP_EVENT_MOUSE_VPOS, "%0.0f", y/height)) < 0)
                return ret;
        }
    }

    if (e->type == GDK_BUTTON_PRESS ||
        e->type == GDK_2BUTTON_PRESS ||
        e->type == GDK_3BUTTON_PRESS ||
        e->type == GDK_BUTTON_RELEASE) {

        if ((ret = ca_proplist_setf(p, CA_PROP_EVENT_MOUSE_BUTTON, "%u", e->button.button)) < 0)
            return ret;
    }

    return CA_SUCCESS;
}

/**
 * ca_gtk_play_for_widget:
 * @w: The Gtk widget to base these sound event properties on
 * @id: The event id that can later be used to cancel this event sound
 * using ca_context_cancel(). This can be any integer and shall be
 * chosen be the client program. It is a good idea to pass 0 here if
 * cancelling the sound later is not needed. If the same id is passed
 * to multiple sounds they can be canceled with a single
 * ca_context_cancel() call.
 * @...: additional event properties as pairs of strings, terminated by NULL.
 *
 * Play a sound event for the specified widget. This will internally
 * call ca_gtk_proplist_set_for_widget() and then merge them with the
 * properties passed in via the NULL terminated argument
 * list. Finally, it will call ca_context_play_full() to actually play
 * the event sound.
 *
 * Returns: 0 on success, negative error code on error.
 */

int ca_gtk_play_for_widget(GtkWidget *w, uint32_t id, ...) {
    va_list ap;
    int ret;
    ca_proplist *p;

    ca_return_val_if_fail(w, CA_ERROR_INVALID);

    if ((ret = ca_proplist_create(&p)) < 0)
        return ret;

    if ((ret = ca_gtk_proplist_set_for_widget(p, w)) < 0)
        goto fail;

    va_start(ap, id);
    ret = ca_proplist_merge_ap(p, ap);
    va_end(ap);

    if (ret < 0)
        goto fail;

    ret = ca_context_play_full(ca_gtk_context_get(), id, p, NULL, NULL);

fail:

    ca_assert_se(ca_proplist_destroy(p) == 0);

    return ret;
}

/**
 * ca_gtk_play_for_event:
 * @e: The Gdk event to base these sound event properties on
 * @id: The event id that can later be used to cancel this event sound
 * using ca_context_cancel(). This can be any integer and shall be
 * chosen be the client program. It is a good idea to pass 0 here if
 * cancelling the sound later is not needed. If the same id is passed
 * to multiple sounds they can be canceled with a single
 * ca_context_cancel() call.
 * @...: additional event properties as pairs of strings, terminated by NULL.
 *
 * Play a sound event for the specified event. This will internally
 * call ca_gtk_proplist_set_for_event() and then merge them with the
 * properties passed in via the NULL terminated argument
 * list. Finally, it will call ca_context_play_full() to actually play
 * the event sound.
 *
 * Returns: 0 on success, negative error code on error.
 */

int ca_gtk_play_for_event(GdkEvent *e, uint32_t id, ...) {
    va_list ap;
    int ret;
    ca_proplist *p;

    ca_return_val_if_fail(e, CA_ERROR_INVALID);

    if ((ret = ca_proplist_create(&p)) < 0)
        return ret;

    if ((ret = ca_gtk_proplist_set_for_event(p, e)) < 0)
        goto fail;

    va_start(ap, id);
    ret = ca_proplist_merge_ap(p, ap);
    va_end(ap);

    if (ret < 0)
        goto fail;

    ret = ca_context_play_full(ca_gtk_context_get(), id, p, NULL, NULL);

fail:

    ca_assert_se(ca_proplist_destroy(p) == 0);

    return ret;
}

/**
 * ca_gtk_widget_disable_sounds:
 * @w: The Gtk widget to disable automatic event sounds for.
 * @enable: Boolean specifying whether sound events shall be enabled or disabled for this widget.
 *
 * By default sound events are automatically generated for all kinds
 * of input events. Use this function to disable this. This is
 * intended to be used for widgets which directly generate sound
 * events.
 */

void ca_gtk_widget_disable_sounds(GtkWidget *w, gboolean enable) {
    static GQuark disable_sound_quark = 0;

    /* This is the same quark used by libgnomeui! */
    if (!disable_sound_quark)
        disable_sound_quark = g_quark_from_static_string("gnome_disable_sound_events");

    g_object_set_qdata(G_OBJECT(w), disable_sound_quark, GINT_TO_POINTER(!!enable));
}