1/*
2 * Copyright © 2008 Bart Massey <bart@cs.pdx.edu>
3 * Copyright © 2008 Julien Danjou <julien@danjou.info>
4 *
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use, copy,
9 * modify, merge, publish, distribute, sublicense, and/or sell copies
10 * of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Except as contained in this notice, the names of the authors or
25 * their institutions shall not be used in advertising or otherwise to
26 * promote the sale, use or other dealings in this Software without
27 * prior written authorization from the authors.
28 */
29
30#include <stdlib.h>
31#include <stdio.h>
32#include <string.h>
33#include <assert.h>
34#include <xcb/xcb.h>
35#include "../aux/xcb_aux.h"
36#include "../event/xcb_event.h"
37#include "xcb_image.h"
38
39#include "test.xbm"
40
41static xcb_window_t make_window(xcb_connection_t *c,
42				xcb_screen_t *s,
43				uint32_t bg,
44				uint32_t fg,
45				uint32_t width,
46				uint32_t height) {
47    uint32_t mask = 0;
48    xcb_params_cw_t cwa;
49    xcb_window_t w;
50    xcb_void_cookie_t check_cookie;
51    xcb_generic_error_t *error;
52    xcb_visualtype_t *v = xcb_aux_find_visual_by_id(s, s->root_visual);
53    assert(v);
54    XCB_AUX_ADD_PARAM(&mask, &cwa, back_pixel, bg);
55    XCB_AUX_ADD_PARAM(&mask, &cwa, border_pixel, fg);
56    XCB_AUX_ADD_PARAM(&mask, &cwa, override_redirect, 1);
57    XCB_AUX_ADD_PARAM(&mask, &cwa, event_mask,
58		      XCB_EVENT_MASK_BUTTON_PRESS |
59		      XCB_EVENT_MASK_EXPOSURE);
60    w = xcb_generate_id(c);
61    check_cookie = xcb_aux_create_window_checked(c,
62      s->root_depth, w, s->root, 0, 0, width, height, 1,
63      XCB_WINDOW_CLASS_INPUT_OUTPUT, v->visual_id, mask, &cwa);
64    error = xcb_request_check(c, check_cookie);
65    assert(!error);
66    check_cookie = xcb_map_window_checked(c, w);
67    error = xcb_request_check(c, check_cookie);
68    assert(!error);
69    return w;
70}
71
72void process_events(xcb_connection_t *c,
73		    xcb_gcontext_t g,
74		    xcb_window_t w,
75		    xcb_pixmap_t p,
76		    uint32_t width,
77		    uint32_t height) {
78    xcb_generic_event_t *e;
79    xcb_void_cookie_t cookie;
80
81    while ((e = xcb_wait_for_event(c))) {
82	uint32_t r = XCB_EVENT_RESPONSE_TYPE(e);
83	xcb_generic_error_t *err;
84
85	fprintf(stderr, "event %d\n", r);
86	switch (r) {
87	case XCB_EXPOSE:
88	case XCB_MAP_NOTIFY:
89	    cookie = xcb_copy_area_checked(c, p, w, g,
90					   0, 0, 0, 0,
91					   width, height);
92	    assert(!xcb_request_check(c, cookie));
93	    break;
94	case XCB_BUTTON_PRESS:
95	    exit(0);
96	    break;
97	case 0:
98	    err = (xcb_generic_error_t *) e;
99	    printf("error: %d (sequence %d)\n",
100		   err->error_code, (unsigned int) err->full_sequence);
101	    exit(1);
102	default:
103	    break;
104	}
105	free(e);
106    }
107}
108
109#define INSET_X 31
110#define INSET_Y 32
111
112int main(int argc, char **argv) {
113    uint32_t width = test_width - 2 * INSET_X;
114    uint32_t height = test_height - 2 * INSET_Y;
115    int snum;
116    xcb_void_cookie_t check_cookie;
117    xcb_window_t w;
118    xcb_gcontext_t gc;
119    xcb_pixmap_t pix;
120    xcb_connection_t *c = xcb_connect(0, &snum);
121    xcb_screen_t *s = xcb_aux_get_screen(c, snum);
122    xcb_alloc_named_color_cookie_t bg_cookie =
123	xcb_alloc_named_color(c, s->default_colormap,
124			      strlen("white"), "white");
125    xcb_alloc_named_color_cookie_t fg_cookie =
126	xcb_alloc_named_color(c, s->default_colormap,
127			      strlen("black"), "black");
128    xcb_alloc_named_color_reply_t *bg_reply =
129	xcb_alloc_named_color_reply(c, bg_cookie, 0);
130    xcb_alloc_named_color_reply_t *fg_reply =
131	xcb_alloc_named_color_reply(c, fg_cookie, 0);
132    uint32_t fg, bg;
133    xcb_image_t *image, *native_image, *subimage;
134    uint32_t mask = 0;
135    xcb_params_gc_t gcv;
136
137    assert(bg_reply && fg_reply);
138    bg = bg_reply->pixel;
139    fg = fg_reply->pixel;
140    free(bg_reply);
141    free(fg_reply);
142    w = make_window(c, s, bg, fg, width, height);
143    gc = xcb_generate_id(c);
144    check_cookie = xcb_create_gc_checked(c, gc, w, 0, 0);
145    assert(!xcb_request_check(c, check_cookie));
146    image = xcb_image_create_from_bitmap_data((uint8_t *)test_bits,
147					      test_width, test_height);
148    native_image = xcb_image_native(c, image, 1);
149    assert(native_image);
150    if (native_image != image)
151	xcb_image_destroy(image);
152    subimage = xcb_image_subimage(native_image, INSET_X, INSET_Y,
153				  width, height,
154				  0, 0, 0);
155    assert(subimage);
156    xcb_image_destroy(native_image);
157    subimage->format = XCB_IMAGE_FORMAT_XY_BITMAP;
158    pix = xcb_generate_id(c);
159    xcb_create_pixmap(c, s->root_depth, pix, w,
160		      subimage->width, subimage->height);
161    gc = xcb_generate_id(c);
162    XCB_AUX_ADD_PARAM(&mask, &gcv, foreground, fg);
163    XCB_AUX_ADD_PARAM(&mask, &gcv, background, bg);
164    xcb_aux_create_gc(c, gc, pix, mask, &gcv);
165    xcb_image_put(c, pix, gc, subimage, 0, 0, 0);
166    process_events(c, gc, w, pix, width, height);
167    xcb_disconnect(c);
168    return 1;
169}
170