1/*
2 * Copyright © 2007 Bart Massey <bart@cs.pdx.edu>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * Except as contained in this notice, the names of the authors or their
22 * institutions shall not be used in advertising or otherwise to promote the
23 * sale, use or other dealings in this Software without prior written
24 * authorization from the authors.
25 */
26
27#include <stdlib.h>
28#include <stdio.h>
29
30#include <sys/ipc.h>
31#include <sys/shm.h>
32
33#include <xcb/xcb.h>
34#include <xcb/shm.h>
35
36#include "../aux/xcb_aux.h"
37#include "xcb_image.h"
38
39#define W_W 40
40#define W_H 40
41
42
43
44int
45main (int argc, char *argv[])
46{
47  xcb_connection_t   *c;
48  xcb_screen_t       *screen;
49  xcb_drawable_t      win;
50  xcb_drawable_t      rect;
51  xcb_rectangle_t     rect_coord = { 0, 0, W_W, W_H};
52  xcb_gcontext_t      bgcolor, fgcolor;
53  xcb_point_t         points[2];
54  uint32_t           mask;
55  uint32_t           valgc[2];
56  uint32_t           valwin[3];
57  int              depth;
58  int              screen_nbr;
59  xcb_generic_event_t *e;
60  uint8_t format;
61
62  /* Open the connexion to the X server and get the first screen */
63  c = xcb_connect (NULL, &screen_nbr);
64  screen = xcb_aux_get_screen (c, screen_nbr);
65  depth = xcb_aux_get_depth (c, screen);
66
67  /* Create a black graphic context for drawing in the foreground */
68  win = screen->root;
69
70  fgcolor = xcb_generate_id(c);
71  mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
72  valgc[0] = screen->black_pixel;
73  valgc[1] = 0; /* no graphics exposures */
74  xcb_create_gc(c, fgcolor, win, mask, valgc);
75
76  bgcolor = xcb_generate_id(c);
77  mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
78  valgc[0] = screen->white_pixel;
79  valgc[1] = 0; /* no graphics exposures */
80  xcb_create_gc(c, bgcolor, win, mask, valgc);
81
82  /* Shm test */
83  printf ("shm test begin\n");
84  xcb_image_t *img = 0;
85  xcb_shm_query_version_reply_t *rep;
86  xcb_shm_segment_info_t shminfo;
87
88  rep = xcb_shm_query_version_reply (c,
89				 xcb_shm_query_version (c),
90				 NULL);
91  if (!rep || !rep->shared_pixmaps ||
92      rep->major_version < 1 ||
93      (rep->major_version == 1 && rep->minor_version == 0))
94      {
95	  printf ("No or insufficient shm support...\n");
96	  exit (0);
97      }
98  format = rep->pixmap_format;
99  img = xcb_image_create_native (c, W_W, W_H, format, depth,
100				     0, ~0, 0);
101
102  if (!img)
103      {
104	  printf ("Can't create image...\n");
105	  exit (0);
106      }
107
108  printf ("Create image summary:\n");
109  printf (" * format..........: %d\n", img->format);
110  printf (" * byte order......: %d\n", img->byte_order);
111  printf (" * bitmap unit.....: %d\n", img->bpp);
112  printf (" * bitmap order....: %d\n", img->bit_order);
113  printf (" * bitmap pad......: %d\n", img->scanline_pad);
114
115  shminfo.shmid = shmget (IPC_PRIVATE, img->size, IPC_CREAT|0777);
116  shminfo.shmaddr = shmat(shminfo.shmid, 0, 0);
117  img->data = shminfo.shmaddr;
118
119  shminfo.shmseg = xcb_generate_id (c);
120  xcb_shm_attach(c, shminfo.shmseg, shminfo.shmid, 0);
121  shmctl(shminfo.shmid, IPC_RMID, 0);
122
123  /* Draw in the image */
124  printf ("put the pixel\n");
125  xcb_image_put_pixel (img, 20, 20, 65535);
126  printf ("fin put pixel\n");
127
128  /* Ask for our window's Id */
129  win = xcb_generate_id(c);
130
131  /* Create the window */
132  mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK | XCB_CW_DONT_PROPAGATE;
133  valwin[0] = screen->white_pixel;
134  valwin[1] = XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE | XCB_EVENT_MASK_EXPOSURE;
135  valwin[2] = XCB_EVENT_MASK_BUTTON_PRESS;
136  xcb_create_window (c,                        /* Connection          */
137 		   0,                        /* depth               */
138		   win,                      /* window Id           */
139		   screen->root,             /* parent window       */
140		   0, 0,                     /* x, y                */
141		   W_W, W_H,                 /* width, height       */
142		   10,                       /* border_width        */
143		   XCB_WINDOW_CLASS_INPUT_OUTPUT,/* class               */
144		   screen->root_visual,      /* visual              */
145		   mask, valwin);            /* masks, not used yet */
146
147  /* Map the window on the screen */
148  xcb_map_window (c, win);
149
150  /* Create a Pixmap that will fill the window */
151  rect = xcb_generate_id (c);
152  xcb_create_pixmap(c, depth, rect, win, W_W, W_H);
153  xcb_poly_fill_rectangle(c, rect, bgcolor, 1, &rect_coord);
154  points[0].x = 0;
155  points[0].y = 0;
156  points[1].x = 1;
157  points[1].y = 1;
158  xcb_poly_line(c, XCB_COORD_MODE_ORIGIN, rect, fgcolor, 2, points);
159
160  xcb_flush (c);
161
162  while ((e = xcb_wait_for_event(c)))
163    {
164      switch (e->response_type)
165	{
166	case XCB_EXPOSE:
167	  {
168	    xcb_copy_area(c, rect, win, bgcolor,
169			0, 0, 0, 0, W_W, W_H);
170	    printf ("put image\n");
171	    xcb_image_shm_put (c, win, fgcolor,
172			       img, shminfo,
173			       0, 0, 0, 0, W_W,W_H,
174			       0);
175	    xcb_flush (c);
176	    break;
177	  }
178	}
179      free (e);
180    }
181
182  return 1;
183}
184