summaryrefslogtreecommitdiff
path: root/rgba-test.c
blob: 8bbac93e545dc99efb32d40611cf406ce72f4130 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * Copyright © 2011 Benjamin Franzke
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>

#include <wayland-client.h>

struct display {
	struct wl_display *display;
	struct {
		struct wl_compositor *compositor;
		struct wl_shm *shm;
	} interface;
	uint32_t mask;
};

struct window {
	struct display *display;
	struct {
		int width, height;
	} geometry;
	struct {
		struct wl_buffer *buffer;
		struct wl_surface *surface;
		uint8_t *data;
		int length;
		int stride;
	} shm_surface;
};

static void
redraw(struct wl_surface *surface, void *data, uint32_t time)
{
	struct window *window = data;
	uint32_t *pixel;
	int x,y;
	int a;

	for (y = 0; y < window->geometry.height; ++y) {
		pixel = (uint32_t *) (window->shm_surface.data +
				y * window->shm_surface.stride);
		a = 255 - y;
		for (x = 0; x < window->geometry.width; ++x, ++pixel) {
			if (x < 85)
				*pixel = (a << 24) | ((x + 170) << 16);
			else if (x < 170)
				*pixel = (a << 24) | ((x + 85) << 8);
			else
				*pixel = (a << 24) | (x);
		}
	}

	wl_buffer_damage(window->shm_surface.buffer, 0, 0,
			 window->geometry.width, window->geometry.height);
	wl_surface_damage(window->shm_surface.surface, 0, 0,
			  window->geometry.width, window->geometry.height);

	/* wl_display_frame_callback(window->display->display, redraw, window); */
}


static void
create_surface(struct window *window)
{
	struct display *display = window->display;
	struct wl_visual *visual;
	int fd;
	char filename[] = "/tmp/wayland-shm-XXXXXX";

	window->shm_surface.surface =
		wl_compositor_create_surface(display->interface.compositor);

#if 0
	window->shm_surface.stride = window->geometry.width * 4;
#else
	window->shm_surface.stride = window->geometry.width * 4 + 4;
#endif
	window->shm_surface.length =
		window->shm_surface.stride * window->geometry.height;

	fd = mkstemp(filename);
	if (fd < 0) {
		fprintf(stderr, "open %s failed: %m\n", filename);
		exit(1);
	}

	if (ftruncate(fd, window->shm_surface.length) < 0) {
		fprintf(stderr, "ftruncate failed: %m");
		close(fd);
		exit(1);
	}

	window->shm_surface.data =
		mmap(NULL, window->shm_surface.length,
		     PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	unlink(filename);

	if (window->shm_surface.data == MAP_FAILED) {
		fprintf(stderr, "mmap failed: %m\n");
		exit(1);
	}

	visual = wl_display_get_argb_visual(display->display);
	window->shm_surface.buffer =
		wl_shm_create_buffer(display->interface.shm, fd,
				     window->geometry.width,
				     window->geometry.height,
				     window->shm_surface.stride, visual);
	close(fd);

	redraw(window->shm_surface.surface, window, 0);

	wl_surface_attach(window->shm_surface.surface,
			  window->shm_surface.buffer, 0, 0);
	wl_surface_map_toplevel(window->shm_surface.surface);
}

static void
display_handle_global(struct wl_display *display, uint32_t id,
		      const char *interface, uint32_t version, void *data)
{
	struct display *d = data;

	if (strcmp(interface, "wl_compositor") == 0) {
		d->interface.compositor = wl_compositor_create(display, id, 1);
	} else if (strcmp(interface, "wl_shm") == 0) {
		d->interface.shm = wl_shm_create(display, id, 1);
	}
}

static int
event_mask_update(uint32_t mask, void *data)
{
	struct display *d = data;

	d->mask = mask;

	return 0;
}

int
main(int argc, char **argv)
{
	struct display display = { 0 };
	struct window  window  = { 0 };

	memset(&display, 0, sizeof display);
	memset(&window,  0, sizeof window);

	window.display = &display;
	window.geometry.width  = 256;
	window.geometry.height = 256;

	display.display = wl_display_connect(NULL);
	assert(display.display);

	wl_display_add_global_listener(display.display,
				    display_handle_global, &display);
	/* process connection events */
	wl_display_iterate(display.display, WL_DISPLAY_READABLE);

	create_surface(&window);

	wl_display_get_fd(display.display, event_mask_update, &display);
	while (true)
		wl_display_iterate(display.display, display.mask);
	
	return 0;
}