1/*
2 * Copyright © 2008 Bart Massey <bart@cs.pdx.edu>
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Except as contained in this notice, the names of the authors or
24 * their institutions shall not be used in advertising or otherwise to
25 * promote the sale, use or other dealings in this Software without
26 * prior written authorization from the authors.
27 */
28
29/* gcc -g -O2 -Wall `pkg-config --cflags --libs xcb` -o test xcb_image.o test_xcb_image.c */
30
31#include <stdlib.h>
32#include <stdio.h>
33
34#include <xcb/xcb.h>
35
36#include "../aux/xcb_aux.h"
37#include "xcb_image.h"
38
39#define W_W 64
40#define W_H 64
41
42void
43reflect_window (xcb_connection_t *c,
44		xcb_drawable_t win,
45		xcb_drawable_t new_win,
46		xcb_gcontext_t gc,
47		uint16_t      width,
48		uint16_t      height)
49{
50  xcb_image_t *image;
51  uint32_t    pixel1;
52  uint32_t    pixel2;
53  int32_t     left_x;
54  int32_t     right_x;
55  int32_t     y;
56  int         format;
57
58  format = XCB_IMAGE_FORMAT_Z_PIXMAP;
59
60  printf ("get_image %d %d\n", width, height);
61  image = xcb_image_get (c, win,
62			 0, 0, width, height,
63			 ~0,
64			 format);
65
66  printf ("Create image summary:\n");
67  printf (" * format................: %d\n", image->format);
68  printf (" * byte order............: %d\n", image->byte_order);
69  printf (" * bitmap order..........: %d\n", image->bit_order);
70  printf (" * bitmap pad............: %d\n", image->scanline_pad);
71  printf (" * depth.................: %d\n", image->depth);
72  printf (" * bytes/line............: %d\n", image->stride);
73  printf (" * bits/pixel (or unit)..: %d\n", image->bpp);
74
75  printf ("bpl %d %d\n", image->stride, image->height);
76
77  printf("calculating reflection -- this may take awhile...\n");
78
79  for (left_x = 0 ; left_x < width/2 ; left_x++)
80    {
81      for (y = 0 ; y < height ; y++)
82	{
83	  pixel1 = xcb_image_get_pixel (image, left_x, y);
84	  right_x = width - left_x-1;
85	  if (left_x != right_x)
86	    {
87	      pixel2 = xcb_image_get_pixel (image, right_x, y);
88	      xcb_image_put_pixel (image, left_x, y, pixel2);
89	    }
90	  xcb_image_put_pixel (image, right_x, y, pixel1);
91	}
92    }
93
94  printf("putting image\n");
95
96  xcb_image_put (c, new_win, gc, image, 0, 0, 0);
97  image = xcb_image_get (c, new_win,
98			 0, 0, width, height,
99			 ~0,
100			 format);
101
102  printf ("done\n");
103}
104
105int
106main (int argc, char *argv[])
107{
108  xcb_connection_t   *c;
109  xcb_screen_t       *screen;
110  xcb_drawable_t      win;
111  xcb_drawable_t      new_win;
112  xcb_drawable_t      rect;
113  xcb_rectangle_t     rect_coord = { 0, 0, W_W, W_H};
114  xcb_gcontext_t      bgcolor, fgcolor;
115  xcb_point_t         points[2];
116  uint32_t           mask;
117  uint32_t           valgc[2];
118  uint32_t           valwin[3];
119  int              depth;
120  int              screen_nbr;
121  xcb_generic_event_t *e;
122
123  /* Open the connexion to the X server and get the first screen */
124  c = xcb_connect (NULL, &screen_nbr);
125  screen = xcb_aux_get_screen (c, screen_nbr);
126  depth = xcb_aux_get_depth (c, screen);
127
128  /* Create a black graphic context for drawing in the foreground */
129  win = screen->root;
130
131  fgcolor = xcb_generate_id(c);
132  mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
133  valgc[0] = screen->black_pixel;
134  valgc[1] = 0; /* no graphics exposures */
135  xcb_create_gc(c, fgcolor, win, mask, valgc);
136
137  bgcolor = xcb_generate_id(c);
138  mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
139  valgc[0] = screen->white_pixel;
140  valgc[1] = 0; /* no graphics exposures */
141  xcb_create_gc(c, bgcolor, win, mask, valgc);
142
143  /* Ask for our window's Id */
144  win = xcb_generate_id(c);
145
146  /* Create the window */
147  mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK | XCB_CW_DONT_PROPAGATE;
148  valwin[0] = screen->white_pixel;
149  valwin[1] = XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_EXPOSURE;
150  valwin[2] = XCB_EVENT_MASK_BUTTON_PRESS;
151  xcb_create_window (c,                        /* Connection          */
152 		   0,                        /* depth               */
153		   win,                      /* window Id           */
154		   screen->root,             /* parent window       */
155		   0, 0,                     /* x, y                */
156		   W_W, W_H,                 /* width, height       */
157		   10,                       /* border_width        */
158		   XCB_WINDOW_CLASS_INPUT_OUTPUT,/* class               */
159		   screen->root_visual,      /* visual              */
160		   mask, valwin);                 /* masks, not used yet */
161
162  /* Map the window on the screen */
163  xcb_map_window (c, win);
164
165  /* Create a Pixmap that will fill the window */
166  rect = xcb_generate_id (c);
167  xcb_create_pixmap(c, depth, rect, win, W_W, W_H);
168  xcb_poly_fill_rectangle(c, rect, bgcolor, 1, &rect_coord);
169  points[0].x = 0;
170  points[0].y = 0;
171  points[1].x = W_W;
172  points[1].y = W_H;
173  xcb_poly_line(c, XCB_COORD_MODE_ORIGIN, rect, fgcolor, 2, points);
174  points[0].x = W_W / 4;
175  points[0].y = 0;
176  points[1].x = W_W / 2;
177  points[1].y = W_H / 2;
178  xcb_poly_line(c, XCB_COORD_MODE_ORIGIN, rect, fgcolor, 2, points);
179
180  /* Ask for our window's Id */
181  new_win = xcb_generate_id(c);
182
183  /* Create the window */
184  mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK | XCB_CW_DONT_PROPAGATE;
185  valwin[0] = screen->white_pixel;
186  valwin[1] = XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_EXPOSURE;
187  valwin[2] = XCB_EVENT_MASK_BUTTON_PRESS;
188  xcb_create_window (c,                        /* Connection          */
189 		   0,                        /* depth               */
190		   new_win,                  /* window Id           */
191		   screen->root,             /* parent window       */
192		   0, 0,                     /* x, y                */
193		   W_W, W_H,                 /* width, height       */
194		   10,                       /* border_width        */
195		   XCB_WINDOW_CLASS_INPUT_OUTPUT,/* class               */
196		   screen->root_visual,      /* visual              */
197		   mask, valwin);                 /* masks, not used yet */
198
199
200
201  /* Map the window on the screen */
202  xcb_map_window (c, new_win);
203
204
205  xcb_flush (c);
206
207  while ((e = xcb_wait_for_event(c)))
208    {
209      switch (e->response_type)
210	{
211	case XCB_EXPOSE:
212	  {
213	    xcb_copy_area(c, rect, win, bgcolor,
214			0, 0, 0, 0, W_W, W_H);
215	    reflect_window (c, win, new_win,
216			    fgcolor,
217			    W_W, W_H);
218	    xcb_flush (c);
219	    break;
220	  }
221	}
222      free (e);
223    }
224
225  return 1;
226}
227