Debian Codegraph @

src/utils.c

raw
1 /*
2 * Copyright (C) Pietro Pilolli 2010 <[email protected]>
3 *
4 * Curtain is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * Curtain is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <gtk/gtk.h>
19 #include <stdlib.h>
20
21 #include <cairo.h>
22
23
24 /* Ungrab pointer. */
25 void
26 ungrab_pointer (GdkDisplay *display)
27 {
28 GdkDevice *pointer = (GdkDevice *) NULL;
29 GdkDeviceManager *device_manager = (GdkDeviceManager *) NULL;
30
31 display = gdk_display_get_default ();
32 device_manager = gdk_display_get_device_manager (display);
33 pointer = gdk_device_manager_get_client_pointer (device_manager);
34
35 gdk_error_trap_push ();
36
37 gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
38 gdk_flush ();
39 if (gdk_error_trap_pop ())
40 {
41 /* this probably means the device table is outdated,
42 * e.g. this device doesn't exist anymore.
43 */
44 g_printerr ("Ungrab pointer device error\n");
45 }
46 }
47
48
49 /* Grab pointer. */
50 void
51 grab_pointer (GtkWidget *widget,
52 GdkEventMask eventmask)
53 {
54 GdkGrabStatus result;
55 GdkDisplay *display = (GdkDisplay *) NULL;
56 GdkDevice *pointer = (GdkDevice *) NULL;
57 GdkDeviceManager *device_manager = (GdkDeviceManager *) NULL;
58
59 display = gdk_display_get_default ();
60 ungrab_pointer (display);
61 device_manager = gdk_display_get_device_manager (display);
62 pointer = gdk_device_manager_get_client_pointer (device_manager);
63
64 gdk_error_trap_push ();
65
66 result = gdk_device_grab (pointer,
67 gtk_widget_get_window (widget),
68 GDK_OWNERSHIP_WINDOW,
69 TRUE,
70 eventmask,
71 NULL,
72 GDK_CURRENT_TIME);
73
74 gdk_flush ();
75 if (gdk_error_trap_pop ())
76 {
77 g_printerr ("Grab pointer error\n");
78 }
79
80 switch (result)
81 {
82 case GDK_GRAB_SUCCESS:
83 break;
84 case GDK_GRAB_ALREADY_GRABBED:
85 g_printerr ("Grab Pointer failed: AlreadyGrabbed\n");
86 break;
87 case GDK_GRAB_INVALID_TIME:
88 g_printerr ("Grab Pointer failed: GrabInvalidTime\n");
89 break;
90 case GDK_GRAB_NOT_VIEWABLE:
91 g_printerr ("Grab Pointer failed: GrabNotViewable\n");
92 break;
93 case GDK_GRAB_FROZEN:
94 g_printerr ("Grab Pointer failed: GrabFrozen\n");
95 break;
96 default:
97 g_printerr ("Grab Pointer failed: Unknown error\n");
98 }
99
100 }
101
102
103 /* Set the cairo surface color to the RGBA string. */
104 void
105 cairo_set_source_color_from_string (cairo_t *cr,
106 gchar *color)
107 {
108 if (cr)
109 {
110 gint r,g,b,a;
111 sscanf (color, "%02X%02X%02X%02X", &r, &g, &b, &a);
112 cairo_set_source_rgba (cr,
113 ((gdouble) r) / 256,
114 ((gdouble) g) / 256,
115 ((gdouble) b) / 256,
116 ((gdouble) a) / 256);
117 }
118 }
119
120
121 /* Scale the surface with the width and height requested */
122 cairo_surface_t *
123 scale_surface (cairo_surface_t *surface,
124 gdouble width,
125 gdouble height)
126 {
127 gdouble old_width = cairo_image_surface_get_width (surface);
128 gdouble old_height = cairo_image_surface_get_height (surface);
129
130 cairo_surface_t *new_surface = cairo_surface_create_similar (surface,
131 CAIRO_CONTENT_COLOR_ALPHA,
132 width,
133 height);
134
135 cairo_t *cr = cairo_create (new_surface);
136
137 /* Scale *before* setting the source surface (1) */
138 cairo_scale (cr, (width / old_width), (height / old_height));
139 cairo_set_source_surface (cr, surface, 0, 0);
140
141 /* To avoid getting the edge pixels blended with 0 alpha, which would
142 * occur with the default EXTEND_NONE. Use EXTEND_PAD for 1.2 or newer (2)
143 */
144 cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REFLECT);
145
146 /* Replace the destination with the source instead of overlaying */
147 cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
148
149 /* Do the actual drawing */
150 cairo_paint (cr);
151
152 cairo_destroy (cr);
153
154 return new_surface;
155 }
156
157