1/*
2 * Copyright (C) 2010 Collabora Ltd.
3 * Copyright (C) 2010 Igalia, S.L.
4 *
5 *  This library is free software; you can redistribute it and/or
6 *  modify it under the terms of the GNU Library General Public
7 *  License as published by the Free Software Foundation; either
8 *  version 2 of the License, or (at your option) any later version.
9 *
10 *  This library is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 *  Library General Public License for more details.
14 *
15 *  You should have received a copy of the GNU Library General Public License
16 *  along with this library; see the file COPYING.LIB.  If not, write to
17 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 *  Boston, MA 02110-1301, USA.
19 */
20
21#include "config.h"
22#include "GtkVersioning.h"
23
24#include <gtk/gtk.h>
25
26#ifdef GDK_WINDOWING_X11
27#include <X11/Xatom.h>
28#include <gdk/gdkx.h>
29#endif
30
31GdkDevice *getDefaultGDKPointerDevice(GdkWindow* window)
32{
33#ifndef GTK_API_VERSION_2
34    GdkDeviceManager *manager =  gdk_display_get_device_manager(gdk_window_get_display(window));
35    return gdk_device_manager_get_client_pointer(manager);
36#else
37    return gdk_device_get_core_pointer();
38#endif // GTK_API_VERSION_2
39}
40
41#ifdef GTK_API_VERSION_2
42static cairo_format_t
43gdk_cairo_format_for_content(cairo_content_t content)
44{
45    switch (content) {
46    case CAIRO_CONTENT_COLOR:
47        return CAIRO_FORMAT_RGB24;
48    case CAIRO_CONTENT_ALPHA:
49        return CAIRO_FORMAT_A8;
50    case CAIRO_CONTENT_COLOR_ALPHA:
51    default:
52        return CAIRO_FORMAT_ARGB32;
53    }
54}
55
56static cairo_surface_t*
57gdk_cairo_surface_coerce_to_image(cairo_surface_t* surface,
58                                  cairo_content_t content,
59                                  int width,
60                                  int height)
61{
62    cairo_surface_t * copy;
63    cairo_t * cr;
64
65    if (cairo_surface_get_type(surface) == CAIRO_SURFACE_TYPE_IMAGE
66        && cairo_surface_get_content(surface) == content
67        && cairo_image_surface_get_width(surface) >= width
68        && cairo_image_surface_get_height(surface) >= height)
69        return cairo_surface_reference(surface);
70
71    copy = cairo_image_surface_create(gdk_cairo_format_for_content(content),
72                                      width,
73                                      height);
74
75    cr = cairo_create(copy);
76    cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
77    cairo_set_source_surface(cr, surface, 0, 0);
78    cairo_paint(cr);
79    cairo_destroy(cr);
80
81    return copy;
82}
83
84static void
85convert_alpha(guchar * destData, int destStride,
86              guchar * srcData, int srcStride,
87              int srcX, int srcY, int width, int height)
88{
89    int x, y;
90
91    srcData += srcStride * srcY + srcY * 4;
92
93    for (y = 0; y < height; y++) {
94        guint32 * src = (guint32 *) srcData;
95
96        for (x = 0; x < width; x++) {
97            guint alpha = src[x] >> 24;
98
99            if (!alpha) {
100                destData[x * 4 + 0] = 0;
101                destData[x * 4 + 1] = 0;
102                destData[x * 4 + 2] = 0;
103            } else {
104                destData[x * 4 + 0] = (((src[x] & 0xff0000) >> 16) * 255 + alpha / 2) / alpha;
105                destData[x * 4 + 1] = (((src[x] & 0x00ff00) >>  8) * 255 + alpha / 2) / alpha;
106                destData[x * 4 + 2] = (((src[x] & 0x0000ff) >>  0) * 255 + alpha / 2) / alpha;
107            }
108            destData[x * 4 + 3] = alpha;
109        }
110
111        srcData += srcStride;
112        destData += destStride;
113    }
114}
115
116static void
117convert_no_alpha(guchar * destData, int destStride, guchar * srcData,
118                 int srcStride, int srcX, int srcY,
119                 int width, int height)
120{
121    int x, y;
122
123    srcData += srcStride * srcY + srcX * 4;
124
125    for (y = 0; y < height; y++) {
126        guint32 * src = (guint32 *) srcData;
127
128        for (x = 0; x < width; x++) {
129            destData[x * 3 + 0] = src[x] >> 16;
130            destData[x * 3 + 1] = src[x] >>  8;
131            destData[x * 3 + 2] = src[x];
132        }
133
134        srcData += srcStride;
135        destData += destStride;
136    }
137}
138
139/**
140 * gdk_pixbuf_get_from_surface:
141 * @surface: surface to copy from
142 * @src_x: Source X coordinate within @surface
143 * @src_y: Source Y coordinate within @surface
144 * @width: Width in pixels of region to get
145 * @height: Height in pixels of region to get
146 *
147 * Transfers image data from a #cairo_surface_t and converts it to an RGB(A)
148 * representation inside a #GdkPixbuf. This allows you to efficiently read
149 * individual pixels from cairo surfaces. For #GdkWindows, use
150 * gdk_pixbuf_get_from_window() instead.
151 *
152 * This function will create an RGB pixbuf with 8 bits per channel. The pixbuf
153 * will contain an alpha channel if the @surface contains one.
154 *
155 * Return value: (transfer full): A newly-created pixbuf with a reference count
156 * of 1, or %NULL on error
157 **/
158GdkPixbuf*
159gdk_pixbuf_get_from_surface(cairo_surface_t * surface,
160                            int srcX, int srcY,
161                            int width, int height)
162{
163    cairo_content_t content;
164    GdkPixbuf * dest;
165
166    /* General sanity checks */
167    g_return_val_if_fail(surface, NULL);
168    g_return_val_if_fail(srcX >= 0 && srcY >= 0, NULL);
169    g_return_val_if_fail(width > 0 && height > 0, NULL);
170
171    content = cairo_surface_get_content(surface) | CAIRO_CONTENT_COLOR;
172    dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
173                          !!(content & CAIRO_CONTENT_ALPHA),
174                          8,
175                          width, height);
176
177    surface = gdk_cairo_surface_coerce_to_image(surface, content, srcX + width, srcY + height);
178    cairo_surface_flush(surface);
179    if (cairo_surface_status(surface) || !dest) {
180        cairo_surface_destroy(surface);
181        return NULL;
182    }
183
184    if (gdk_pixbuf_get_has_alpha(dest))
185        convert_alpha(gdk_pixbuf_get_pixels(dest),
186                       gdk_pixbuf_get_rowstride(dest),
187                       cairo_image_surface_get_data(surface),
188                       cairo_image_surface_get_stride(surface),
189                       srcX, srcY,
190                       width, height);
191    else
192        convert_no_alpha(gdk_pixbuf_get_pixels(dest),
193                          gdk_pixbuf_get_rowstride(dest),
194                          cairo_image_surface_get_data(surface),
195                          cairo_image_surface_get_stride(surface),
196                          srcX, srcY,
197                          width, height);
198
199    cairo_surface_destroy(surface);
200    return dest;
201}
202
203#ifdef GDK_WINDOWING_X11
204static int getScreenCurrentDesktop(GdkScreen *screen)
205{
206    Display *display = GDK_DISPLAY_XDISPLAY(gdk_screen_get_display(screen));
207    Window rootWindow = XRootWindow(display, GDK_SCREEN_XNUMBER(screen));
208    Atom currentDesktop = XInternAtom(display, "_NET_CURRENT_DESKTOP", True);
209
210    Atom type;
211    int format;
212    unsigned long itemsCount, bytesAfter;
213    unsigned char *returnedData = NULL;
214    XGetWindowProperty(display, rootWindow, currentDesktop, 0, G_MAXLONG, False, XA_CARDINAL,
215                       &type, &format, &itemsCount, &bytesAfter, &returnedData);
216
217    int workspace = 0;
218    if (type == XA_CARDINAL && format == 32 && itemsCount > 0)
219        workspace = (int)returnedData[0];
220
221    if (returnedData)
222        XFree(returnedData);
223
224    return workspace;
225}
226
227static void getScreenWorkArea(GdkScreen *screen, GdkRectangle *area)
228{
229    Display *display = GDK_DISPLAY_XDISPLAY(gdk_screen_get_display(screen));
230    Atom workArea = XInternAtom(display, "_NET_WORKAREA", True);
231
232    /* Defaults in case of error. */
233    area->x = 0;
234    area->y = 0;
235    area->width = gdk_screen_get_width(screen);
236    area->height = gdk_screen_get_height(screen);
237
238    if (workArea == None)
239        return;
240
241    Window rootWindow = XRootWindow(display, GDK_SCREEN_XNUMBER(screen));
242    Atom type;
243    int format;
244    unsigned long itemsCount, bytesAfter;
245    unsigned char *returnedData = 0;
246    int result = XGetWindowProperty(display, rootWindow, workArea, 0, 4 * 32 /* Max length */, False, AnyPropertyType,
247                                    &type, &format, &itemsCount, &bytesAfter, &returnedData);
248    if (result != Success || type == None || !format || bytesAfter || itemsCount % 4)
249        return;
250
251    int desktop = getScreenCurrentDesktop(screen);
252    long *workAreas = (long *)returnedData;
253    area->x = workAreas[desktop * 4];
254    area->y = workAreas[desktop * 4 + 1];
255    area->width = workAreas[desktop * 4 + 2];
256    area->height = workAreas[desktop * 4 + 3];
257
258    XFree(returnedData);
259}
260#endif // GDK_WINDOWING_X11
261
262void gdk_screen_get_monitor_workarea(GdkScreen *screen, int monitor, GdkRectangle *area)
263{
264    gdk_screen_get_monitor_geometry(screen, monitor, area);
265
266#ifdef GDK_WINDOWING_X11
267    GdkRectangle workArea;
268    getScreenWorkArea(screen, &workArea);
269    gdk_rectangle_intersect(&workArea, area, area);
270#endif
271}
272#endif // GTK_API_VERSION_2
273