| 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 | #if PLATFORM(X11) && defined(GDK_WINDOWING_X11) |
| 27 | #include <X11/Xatom.h> |
| 28 | #include <gdk/gdkx.h> |
| 29 | #endif |
| 30 | |
| 31 | GdkDevice *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 | UNUSED_PARAM(window); |
| 38 | return gdk_device_get_core_pointer(); |
| 39 | #endif // GTK_API_VERSION_2 |
| 40 | } |
| 41 | |
| 42 | #ifdef GTK_API_VERSION_2 |
| 43 | static cairo_format_t |
| 44 | gdk_cairo_format_for_content(cairo_content_t content) |
| 45 | { |
| 46 | switch (content) { |
| 47 | case CAIRO_CONTENT_COLOR: |
| 48 | return CAIRO_FORMAT_RGB24; |
| 49 | case CAIRO_CONTENT_ALPHA: |
| 50 | return CAIRO_FORMAT_A8; |
| 51 | case CAIRO_CONTENT_COLOR_ALPHA: |
| 52 | default: |
| 53 | return CAIRO_FORMAT_ARGB32; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | static cairo_surface_t* |
| 58 | gdk_cairo_surface_coerce_to_image(cairo_surface_t* surface, |
| 59 | cairo_content_t content, |
| 60 | int width, |
| 61 | int height) |
| 62 | { |
| 63 | cairo_surface_t * copy; |
| 64 | cairo_t * cr; |
| 65 | |
| 66 | if (cairo_surface_get_type(surface) == CAIRO_SURFACE_TYPE_IMAGE |
| 67 | && cairo_surface_get_content(surface) == content |
| 68 | && cairo_image_surface_get_width(surface) >= width |
| 69 | && cairo_image_surface_get_height(surface) >= height) |
| 70 | return cairo_surface_reference(surface); |
| 71 | |
| 72 | copy = cairo_image_surface_create(gdk_cairo_format_for_content(content), |
| 73 | width, |
| 74 | height); |
| 75 | |
| 76 | cr = cairo_create(copy); |
| 77 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 78 | cairo_set_source_surface(cr, surface, 0, 0); |
| 79 | cairo_paint(cr); |
| 80 | cairo_destroy(cr); |
| 81 | |
| 82 | return copy; |
| 83 | } |
| 84 | |
| 85 | static void |
| 86 | convert_alpha(guchar * destData, int destStride, |
| 87 | guchar * srcData, int srcStride, |
| 88 | int srcX, int srcY, int width, int height) |
| 89 | { |
| 90 | UNUSED_PARAM(srcX); |
| 91 | |
| 92 | int x, y; |
| 93 | |
| 94 | srcData += srcStride * srcY + srcY * 4; |
| 95 | |
| 96 | for (y = 0; y < height; y++) { |
| 97 | guint32 * src = (guint32 *) srcData; |
| 98 | |
| 99 | for (x = 0; x < width; x++) { |
| 100 | guint alpha = src[x] >> 24; |
| 101 | |
| 102 | if (!alpha) { |
| 103 | destData[x * 4 + 0] = 0; |
| 104 | destData[x * 4 + 1] = 0; |
| 105 | destData[x * 4 + 2] = 0; |
| 106 | } else { |
| 107 | destData[x * 4 + 0] = (((src[x] & 0xff0000) >> 16) * 255 + alpha / 2) / alpha; |
| 108 | destData[x * 4 + 1] = (((src[x] & 0x00ff00) >> 8) * 255 + alpha / 2) / alpha; |
| 109 | destData[x * 4 + 2] = (((src[x] & 0x0000ff) >> 0) * 255 + alpha / 2) / alpha; |
| 110 | } |
| 111 | destData[x * 4 + 3] = alpha; |
| 112 | } |
| 113 | |
| 114 | srcData += srcStride; |
| 115 | destData += destStride; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | static void |
| 120 | convert_no_alpha(guchar * destData, int destStride, guchar * srcData, |
| 121 | int srcStride, int srcX, int srcY, |
| 122 | int width, int height) |
| 123 | { |
| 124 | int x, y; |
| 125 | |
| 126 | srcData += srcStride * srcY + srcX * 4; |
| 127 | |
| 128 | for (y = 0; y < height; y++) { |
| 129 | guint32 * src = (guint32 *) srcData; |
| 130 | |
| 131 | for (x = 0; x < width; x++) { |
| 132 | destData[x * 3 + 0] = src[x] >> 16; |
| 133 | destData[x * 3 + 1] = src[x] >> 8; |
| 134 | destData[x * 3 + 2] = src[x]; |
| 135 | } |
| 136 | |
| 137 | srcData += srcStride; |
| 138 | destData += destStride; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * gdk_pixbuf_get_from_surface: |
| 144 | * @surface: surface to copy from |
| 145 | * @src_x: Source X coordinate within @surface |
| 146 | * @src_y: Source Y coordinate within @surface |
| 147 | * @width: Width in pixels of region to get |
| 148 | * @height: Height in pixels of region to get |
| 149 | * |
| 150 | * Transfers image data from a #cairo_surface_t and converts it to an RGB(A) |
| 151 | * representation inside a #GdkPixbuf. This allows you to efficiently read |
| 152 | * individual pixels from cairo surfaces. For #GdkWindows, use |
| 153 | * gdk_pixbuf_get_from_window() instead. |
| 154 | * |
| 155 | * This function will create an RGB pixbuf with 8 bits per channel. The pixbuf |
| 156 | * will contain an alpha channel if the @surface contains one. |
| 157 | * |
| 158 | * Return value: (transfer full): A newly-created pixbuf with a reference count |
| 159 | * of 1, or %NULL on error |
| 160 | **/ |
| 161 | GdkPixbuf* |
| 162 | gdk_pixbuf_get_from_surface(cairo_surface_t * surface, |
| 163 | int srcX, int srcY, |
| 164 | int width, int height) |
| 165 | { |
| 166 | cairo_content_t content; |
| 167 | GdkPixbuf * dest; |
| 168 | |
| 169 | /* General sanity checks */ |
| 170 | g_return_val_if_fail(surface, NULL); |
| 171 | g_return_val_if_fail(srcX >= 0 && srcY >= 0, NULL); |
| 172 | g_return_val_if_fail(width > 0 && height > 0, NULL); |
| 173 | |
| 174 | content = cairo_surface_get_content(surface) | CAIRO_CONTENT_COLOR; |
| 175 | dest = gdk_pixbuf_new(GDK_COLORSPACE_RGB, |
| 176 | !!(content & CAIRO_CONTENT_ALPHA), |
| 177 | 8, |
| 178 | width, height); |
| 179 | |
| 180 | surface = gdk_cairo_surface_coerce_to_image(surface, content, srcX + width, srcY + height); |
| 181 | cairo_surface_flush(surface); |
| 182 | if (cairo_surface_status(surface) || !dest) { |
| 183 | cairo_surface_destroy(surface); |
| 184 | return NULL; |
| 185 | } |
| 186 | |
| 187 | if (gdk_pixbuf_get_has_alpha(dest)) |
| 188 | convert_alpha(gdk_pixbuf_get_pixels(dest), |
| 189 | gdk_pixbuf_get_rowstride(dest), |
| 190 | cairo_image_surface_get_data(surface), |
| 191 | cairo_image_surface_get_stride(surface), |
| 192 | srcX, srcY, |
| 193 | width, height); |
| 194 | else |
| 195 | convert_no_alpha(gdk_pixbuf_get_pixels(dest), |
| 196 | gdk_pixbuf_get_rowstride(dest), |
| 197 | cairo_image_surface_get_data(surface), |
| 198 | cairo_image_surface_get_stride(surface), |
| 199 | srcX, srcY, |
| 200 | width, height); |
| 201 | |
| 202 | cairo_surface_destroy(surface); |
| 203 | return dest; |
| 204 | } |
| 205 | |
| 206 | #if PLATFORM(X11) && defined(GDK_WINDOWING_X11) |
| 207 | static int getScreenCurrentDesktop(GdkScreen *screen) |
| 208 | { |
| 209 | Display *display = GDK_DISPLAY_XDISPLAY(gdk_screen_get_display(screen)); |
| 210 | Window rootWindow = XRootWindow(display, GDK_SCREEN_XNUMBER(screen)); |
| 211 | Atom currentDesktop = XInternAtom(display, "_NET_CURRENT_DESKTOP" , True); |
| 212 | |
| 213 | Atom type; |
| 214 | int format; |
| 215 | unsigned long itemsCount, bytesAfter; |
| 216 | unsigned char *returnedData = NULL; |
| 217 | XGetWindowProperty(display, rootWindow, currentDesktop, 0, G_MAXLONG, False, XA_CARDINAL, |
| 218 | &type, &format, &itemsCount, &bytesAfter, &returnedData); |
| 219 | |
| 220 | int workspace = 0; |
| 221 | if (type == XA_CARDINAL && format == 32 && itemsCount > 0) |
| 222 | workspace = (int)returnedData[0]; |
| 223 | |
| 224 | if (returnedData) |
| 225 | XFree(returnedData); |
| 226 | |
| 227 | return workspace; |
| 228 | } |
| 229 | |
| 230 | static void getScreenWorkArea(GdkScreen *screen, GdkRectangle *area) |
| 231 | { |
| 232 | Display *display = GDK_DISPLAY_XDISPLAY(gdk_screen_get_display(screen)); |
| 233 | Atom workArea = XInternAtom(display, "_NET_WORKAREA" , True); |
| 234 | |
| 235 | /* Defaults in case of error. */ |
| 236 | area->x = 0; |
| 237 | area->y = 0; |
| 238 | area->width = gdk_screen_get_width(screen); |
| 239 | area->height = gdk_screen_get_height(screen); |
| 240 | |
| 241 | if (workArea == None) |
| 242 | return; |
| 243 | |
| 244 | Window rootWindow = XRootWindow(display, GDK_SCREEN_XNUMBER(screen)); |
| 245 | Atom type; |
| 246 | int format; |
| 247 | unsigned long itemsCount, bytesAfter; |
| 248 | unsigned char *returnedData = 0; |
| 249 | int result = XGetWindowProperty(display, rootWindow, workArea, 0, 4 * 32 /* Max length */, False, AnyPropertyType, |
| 250 | &type, &format, &itemsCount, &bytesAfter, &returnedData); |
| 251 | if (result != Success || type == None || !format || bytesAfter || itemsCount % 4) |
| 252 | return; |
| 253 | |
| 254 | int desktop = getScreenCurrentDesktop(screen); |
| 255 | long *workAreas = (long *)returnedData; |
| 256 | area->x = workAreas[desktop * 4]; |
| 257 | area->y = workAreas[desktop * 4 + 1]; |
| 258 | area->width = workAreas[desktop * 4 + 2]; |
| 259 | area->height = workAreas[desktop * 4 + 3]; |
| 260 | |
| 261 | XFree(returnedData); |
| 262 | } |
| 263 | #endif // PLATFORM(X11) && defined(GDK_WINDOWING_X11) |
| 264 | |
| 265 | void gdk_screen_get_monitor_workarea(GdkScreen *screen, int monitor, GdkRectangle *area) |
| 266 | { |
| 267 | gdk_screen_get_monitor_geometry(screen, monitor, area); |
| 268 | |
| 269 | #if PLATFORM(X11) && defined(GDK_WINDOWING_X11) |
| 270 | GdkRectangle workArea; |
| 271 | getScreenWorkArea(screen, &workArea); |
| 272 | gdk_rectangle_intersect(&workArea, area, area); |
| 273 | #endif |
| 274 | } |
| 275 | #endif // GTK_API_VERSION_2 |
| 276 | |