Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Kristian Høgsberg |
| 3 | * Copyright © 2009 Chris Wilson |
| 4 | * |
| 5 | * Permission to use, copy, modify, distribute, and sell this software and its |
| 6 | * documentation for any purpose is hereby granted without fee, provided that |
| 7 | * the above copyright notice appear in all copies and that both that copyright |
| 8 | * notice and this permission notice appear in supporting documentation, and |
| 9 | * that the name of the copyright holders not be used in advertising or |
| 10 | * publicity pertaining to distribution of the software without specific, |
| 11 | * written prior permission. The copyright holders make no representations |
| 12 | * about the suitability of this software for any purpose. It is provided "as |
| 13 | * is" without express or implied warranty. |
| 14 | * |
| 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
| 16 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
| 17 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
| 18 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
| 19 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 20 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 21 | * OF THIS SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <stdint.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <unistd.h> |
| 30 | #include <math.h> |
| 31 | #include <time.h> |
| 32 | #include <cairo.h> |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 33 | #include <glib.h> |
| 34 | #include <gdk-pixbuf/gdk-pixbuf.h> |
| 35 | |
| 36 | #include "wayland-client.h" |
| 37 | #include "wayland-glib.h" |
| 38 | |
| 39 | #include "window.h" |
| 40 | |
| 41 | static const char gem_device[] = "/dev/dri/card0"; |
| 42 | static const char socket_name[] = "\0wayland"; |
| 43 | |
| 44 | struct image { |
| 45 | struct window *window; |
| 46 | struct display *display; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 47 | uint32_t key; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 48 | gchar *filename; |
| 49 | }; |
| 50 | |
| 51 | static void |
| 52 | set_source_pixbuf(cairo_t *cr, |
| 53 | const GdkPixbuf *pixbuf, |
| 54 | double src_x, |
| 55 | double src_y, |
| 56 | double src_width, |
| 57 | double src_height) |
| 58 | { |
| 59 | gint width = gdk_pixbuf_get_width (pixbuf); |
| 60 | gint height = gdk_pixbuf_get_height (pixbuf); |
| 61 | guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf); |
| 62 | int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf); |
| 63 | int n_channels = gdk_pixbuf_get_n_channels (pixbuf); |
| 64 | int cairo_stride; |
| 65 | guchar *cairo_pixels; |
| 66 | cairo_format_t format; |
| 67 | cairo_surface_t *surface; |
| 68 | int j; |
| 69 | |
| 70 | if (n_channels == 3) |
| 71 | format = CAIRO_FORMAT_RGB24; |
| 72 | else |
| 73 | format = CAIRO_FORMAT_ARGB32; |
| 74 | |
| 75 | surface = cairo_image_surface_create(format, width, height); |
| 76 | if (cairo_surface_status(surface)) { |
| 77 | cairo_set_source_surface(cr, surface, 0, 0); |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | cairo_stride = cairo_image_surface_get_stride(surface); |
| 82 | cairo_pixels = cairo_image_surface_get_data(surface); |
| 83 | |
| 84 | for (j = height; j; j--) { |
| 85 | guchar *p = gdk_pixels; |
| 86 | guchar *q = cairo_pixels; |
| 87 | |
| 88 | if (n_channels == 3) { |
| 89 | guchar *end = p + 3 * width; |
| 90 | |
| 91 | while (p < end) { |
| 92 | #if G_BYTE_ORDER == G_LITTLE_ENDIAN |
| 93 | q[0] = p[2]; |
| 94 | q[1] = p[1]; |
| 95 | q[2] = p[0]; |
| 96 | #else |
| 97 | q[1] = p[0]; |
| 98 | q[2] = p[1]; |
| 99 | q[3] = p[2]; |
| 100 | #endif |
| 101 | p += 3; |
| 102 | q += 4; |
| 103 | } |
| 104 | } else { |
| 105 | guchar *end = p + 4 * width; |
| 106 | guint t1,t2,t3; |
| 107 | |
| 108 | #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END |
| 109 | |
| 110 | while (p < end) { |
| 111 | #if G_BYTE_ORDER == G_LITTLE_ENDIAN |
| 112 | MULT(q[0], p[2], p[3], t1); |
| 113 | MULT(q[1], p[1], p[3], t2); |
| 114 | MULT(q[2], p[0], p[3], t3); |
| 115 | q[3] = p[3]; |
| 116 | #else |
| 117 | q[0] = p[3]; |
| 118 | MULT(q[1], p[0], p[3], t1); |
| 119 | MULT(q[2], p[1], p[3], t2); |
| 120 | MULT(q[3], p[2], p[3], t3); |
| 121 | #endif |
| 122 | |
| 123 | p += 4; |
| 124 | q += 4; |
| 125 | } |
| 126 | #undef MULT |
| 127 | } |
| 128 | |
| 129 | gdk_pixels += gdk_rowstride; |
| 130 | cairo_pixels += cairo_stride; |
| 131 | } |
| 132 | cairo_surface_mark_dirty(surface); |
| 133 | |
| 134 | cairo_set_source_surface(cr, surface, |
| 135 | src_x + .5 * (src_width - width), |
| 136 | src_y + .5 * (src_height - height)); |
| 137 | cairo_surface_destroy(surface); |
| 138 | } |
| 139 | |
| 140 | static void |
| 141 | image_draw(struct image *image) |
| 142 | { |
| 143 | struct rectangle rectangle; |
| 144 | GdkPixbuf *pb; |
| 145 | cairo_t *cr; |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 146 | cairo_surface_t *wsurface, *surface; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 147 | |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 148 | window_draw(image->window); |
| 149 | |
| 150 | window_get_child_rectangle(image->window, &rectangle); |
| 151 | |
| 152 | pb = gdk_pixbuf_new_from_file_at_size(image->filename, |
| 153 | rectangle.width, |
| 154 | rectangle.height, |
| 155 | NULL); |
| 156 | if (pb == NULL) |
| 157 | return; |
| 158 | |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 159 | wsurface = window_get_surface(image->window); |
| 160 | surface = cairo_surface_create_similar(wsurface, |
| 161 | CAIRO_CONTENT_COLOR_ALPHA, |
| 162 | rectangle.width, |
| 163 | rectangle.height); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 164 | |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 165 | cr = cairo_create(surface); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 166 | cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE); |
| 167 | cairo_set_source_rgba(cr, 0, 0, 0, 1); |
| 168 | cairo_paint(cr); |
| 169 | set_source_pixbuf(cr, pb, |
| 170 | 0, 0, |
| 171 | rectangle.width, rectangle.height); |
| 172 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
| 173 | cairo_paint(cr); |
| 174 | cairo_destroy(cr); |
| 175 | |
| 176 | g_object_unref(pb); |
| 177 | |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 178 | window_copy_surface(image->window, &rectangle, surface); |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 179 | window_commit(image->window, image->key); |
Kristian Høgsberg | 0953162 | 2010-06-14 23:22:15 -0400 | [diff] [blame] | 180 | cairo_surface_destroy(surface); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 181 | } |
| 182 | |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 183 | static void |
| 184 | redraw_handler(struct window *window, void *data) |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 185 | { |
| 186 | struct image *image = data; |
| 187 | |
| 188 | image_draw(image); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static void |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 192 | keyboard_focus_handler(struct window *window, |
Kristian Høgsberg | 43788b1 | 2010-07-28 23:50:12 -0400 | [diff] [blame^] | 193 | struct input *device, void *data) |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 194 | { |
| 195 | struct image *image = data; |
| 196 | |
Kristian Høgsberg | 80d746f | 2010-06-14 23:52:50 -0400 | [diff] [blame] | 197 | window_schedule_redraw(image->window); |
Kristian Høgsberg | a341fa0 | 2010-01-24 18:10:15 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 200 | static struct image * |
| 201 | image_create(struct display *display, uint32_t key, const char *filename) |
| 202 | { |
| 203 | struct image *image; |
| 204 | gchar *basename; |
| 205 | gchar *title; |
| 206 | |
| 207 | image = malloc(sizeof *image); |
| 208 | if (image == NULL) |
| 209 | return image; |
| 210 | memset(image, 0, sizeof *image); |
| 211 | |
| 212 | basename = g_path_get_basename(filename); |
| 213 | title = g_strdup_printf("Wayland Image - %s", basename); |
| 214 | g_free(basename); |
| 215 | |
| 216 | image->filename = g_strdup(filename); |
| 217 | |
| 218 | image->window = window_create(display, title, 100 * key, 100 * key, 500, 400); |
| 219 | image->display = display; |
| 220 | |
| 221 | /* FIXME: Window uses key 1 for moves, need some kind of |
| 222 | * allocation scheme here. Or maybe just a real toolkit. */ |
| 223 | image->key = key + 100; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 224 | |
Kristian Høgsberg | c8c3734 | 2010-06-25 11:19:22 -0400 | [diff] [blame] | 225 | window_set_user_data(image->window, image); |
| 226 | window_set_redraw_handler(image->window, redraw_handler); |
Kristian Høgsberg | 43788b1 | 2010-07-28 23:50:12 -0400 | [diff] [blame^] | 227 | window_set_keyboard_focus_handler(image->window, |
| 228 | keyboard_focus_handler); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 229 | |
| 230 | image_draw(image); |
| 231 | |
| 232 | return image; |
| 233 | } |
| 234 | |
| 235 | static const GOptionEntry option_entries[] = { |
| 236 | { NULL } |
| 237 | }; |
| 238 | |
| 239 | int |
| 240 | main(int argc, char *argv[]) |
| 241 | { |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 242 | struct display *d; |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 243 | int i; |
| 244 | |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 245 | d = display_create(&argc, &argv, option_entries); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 246 | |
| 247 | for (i = 1; i < argc; i++) { |
| 248 | struct image *image; |
| 249 | |
| 250 | image = image_create (d, i, argv[i]); |
| 251 | } |
| 252 | |
Kristian Høgsberg | 7824d81 | 2010-06-08 14:59:44 -0400 | [diff] [blame] | 253 | display_run(d); |
Chris Wilson | 0de19eb | 2009-02-21 15:22:06 -0500 | [diff] [blame] | 254 | |
| 255 | return 0; |
| 256 | } |