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