aboutsummaryrefslogtreecommitdiffstats
path: root/simple_x.c
blob: 98ddbbe849b8debb37218987a9c48f88b5da7d0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * SimplX - Proposed name if not taken already.
 *
 * Copyright (C) 2014  Tomasz Kramkowski <tk@the-tk.com>
 *
 * This program is free software. It is licensed under version 3 of the
 * GNU General Public License.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
 */
#include <stdlib.h>
#include <xcb/xcb.h>
#include <xcb/xcb_image.h>

/*
 * TODO: Figure out the alignment of these things fix the arrangement.
 *       Find a good name for this.
 */
typedef struct sx_gc {
	/*struct {*/
	xcb_connection_t *connection;
	xcb_screen_t *screen;
	xcb_drawable_t window;
	xcb_pixmap_t pixmap;
	xcb_image_t *image;
	xcb_gcontext_t gc;
	/*} xcb;*/
} SX;

SX *sx_new_gc(uint16_t width, uint16_t height)
{
	SX *sx = malloc(sizeof(SX));

	sx->connection = xcb_connect(NULL, NULL);

	sx->screen = xcb_setup_roots_iterator(xcb_get_setup(sx->connection)).data;

	uint32_t mask = XCB_CW_BACK_PIXEL; /* Needs events */
	uint32_t values[] = {sx->screen->black_pixel};

	sx->window = xcb_generate_id(sx->connection);
	xcb_create_window(sx->connection,
			24,
			sx->window,
			sx->screen->root,
			0, 0,
			width, height,
			1,
			XCB_WINDOW_CLASS_INPUT_OUTPUT,
			sx->screen->root_visual,
			mask, values);

	sx->pixmap = xcb_generate_id(sx->connection);
	xcb_create_pixmap(sx->connection,
			24,
			sx->pixmap,
			sx->window,
			width, height);

	uint8_t *img = malloc(width * height * 4);
	xcb_image_t *image = xcb_image_create(width, height,
			XCB_IMAGE_FORMAT_Z_PIXMAP,
			8, 24, 32,
			0,
			XCB_IMAGE_ORDER_MSB_FIRST,
			XCB_IMAGE_ORDER_LSB_FIRST,
			img,
			width * height * 4,
			img);

	mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND;
	values[0] = sx->screen->black_pixel;
	values[1] = 0xFFFFFF;

	sx->gc = xcb_generate_id(sx->connection);
	xcb_create_gc(sx->connection,
			sx->gc,
			sx->pixmap,
			mask, values);

	return sx;
}