summaryrefslogtreecommitdiff
path: root/extra/lightbar/windows.c
blob: 115074363c1f0f2e41906bfd66e9ff89fcae358b (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * Copyright 2014 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include <assert.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <xcb/xcb.h>

#include "simulation.h"

/*****************************************************************************/
/* Window drawing stuff */

/* Dimensions - may change */
static int win_w = 1024;
static int win_h = 32;

static xcb_connection_t *c;
static xcb_screen_t *screen;
static xcb_drawable_t win;
static xcb_gcontext_t foreground;
static xcb_colormap_t colormap_id;

static int fake_power;

void init_windows(void)
{
	uint32_t mask = 0;
	uint32_t values[2];

	/* Open the connection to the X server */
	c = xcb_connect(NULL, NULL);

	/* Get the first screen */
	screen = xcb_setup_roots_iterator(xcb_get_setup(c)).data;

	/* Get a colormap */
	colormap_id = xcb_generate_id(c);
	xcb_create_colormap(c, XCB_COLORMAP_ALLOC_NONE,
			    colormap_id, screen->root, screen->root_visual);

	/* Create foreground GC */
	foreground = xcb_generate_id(c);
	mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES;
	values[0] = screen->white_pixel;
	values[1] = 0;
	xcb_create_gc(c, foreground, screen->root, mask, values);

	/* Create the window */
	win = xcb_generate_id(c);
	mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
	values[0] = screen->black_pixel;
	values[1] = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS;
	xcb_create_window(c,				 /* Connection */
			  XCB_COPY_FROM_PARENT,		 /* depth */
			  win,				 /* window Id */
			  screen->root,			 /* parent window */
			  0, 0,				 /* x, y */
			  win_w, win_h,			 /* width, height */
			  10,				 /* border_width */
			  XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class */
			  screen->root_visual,		 /* visual */
			  mask, values);		 /* masks */

	/* Map the window on the screen */
	xcb_map_window(c, win);

	/* We flush the request */
	xcb_flush(c);
}

void cleanup(void)
{
	xcb_destroy_window(c, win);
	xcb_free_gc(c, foreground);
	xcb_free_colormap(c, colormap_id);
	xcb_disconnect(c);
}

/*****************************************************************************/
/* Draw the lightbar elements */

/* xcb likes 16-bit colors */
uint16_t leds[NUM_LEDS][3] = {
	{0xffff, 0x0000, 0x0000},
	{0x0000, 0xffff, 0x0000},
	{0x0000, 0x0000, 0xffff},
	{0xffff, 0xffff, 0x0000},
};
pthread_mutex_t leds_mutex = PTHREAD_MUTEX_INITIALIZER;

void change_gc_color(uint16_t red, uint16_t green, uint16_t blue)
{
	uint32_t mask = 0;
	uint32_t values[2];
	xcb_alloc_color_reply_t *reply;

	reply = xcb_alloc_color_reply(c,
				      xcb_alloc_color(c, colormap_id,
						      red, green, blue),
				      NULL);
	assert(reply);

	mask = XCB_GC_FOREGROUND;
	values[0] = reply->pixel;
	xcb_change_gc(c, foreground, mask, values);
	free(reply);
}

void update_window(void)
{
	xcb_segment_t segments[] = {
		{0, 0, win_w, win_h},
		{0, win_h, win_w, 0},
	};
	xcb_rectangle_t rect;
	int w = win_w / NUM_LEDS;
	int i;
	uint16_t copyleds[NUM_LEDS][3];

	if (fake_power) {
		pthread_mutex_lock(&leds_mutex);
		memcpy(copyleds, leds, sizeof(leds));
		pthread_mutex_unlock(&leds_mutex);

		for (i = 0; i < NUM_LEDS; i++) {
			rect.x = i * w;
			rect.y = 0;
			rect.width = w;
			rect.height = win_h;

			change_gc_color(copyleds[i][0],
					copyleds[i][1],
					copyleds[i][2]);

			xcb_poly_fill_rectangle(c, win, foreground, 1, &rect);
		}
	} else {
		rect.x = 0;
		rect.y = 0;
		rect.width = win_w;
		rect.height = win_h;

		change_gc_color(0, 0, 0);
		xcb_poly_fill_rectangle(c, win, foreground, 1, &rect);

		change_gc_color(0x8080, 0, 0);

		for (i = 0; i < NUM_LEDS; i++) {
			segments[0].x1 = i * w;
			segments[0].y1 = 0;
			segments[0].x2 = segments[0].x1 + w;
			segments[0].y2 = win_h;
			segments[1].x1 = segments[0].x1;
			segments[1].y1 = win_h;
			segments[1].x2 = segments[0].x2;
			segments[1].y2 = 0;
			xcb_poly_segment(c, win, foreground, 2, segments);
		}
	}

	xcb_flush(c);
}

void setrgb(int led, int red, int green, int blue)
{
	led %= NUM_LEDS;

	pthread_mutex_lock(&leds_mutex);
	leds[led][0] = red << 8 | red;
	leds[led][1] = green << 8 | green;
	leds[led][2] = blue << 8 | blue;
	pthread_mutex_unlock(&leds_mutex);

	update_window();
}

/*****************************************************************************/
/* lb_common stubs */



/* Brightness serves no purpose here. It's automatic on the Chromebook. */
static int brightness = 0xc0;
void lb_set_brightness(unsigned int newval)
{
	brightness = newval;
}
uint8_t lb_get_brightness(void)
{
	return brightness;
}

void lb_set_rgb(unsigned int led, int red, int green, int blue)
{
	int i;
	if (led >= NUM_LEDS)
		for (i = 0; i < NUM_LEDS; i++)
			setrgb(i, red, green, blue);
	else
		setrgb(led, red, green, blue);
}

int lb_get_rgb(unsigned int led, uint8_t *red, uint8_t *green, uint8_t *blue)
{
	led %= NUM_LEDS;
	pthread_mutex_lock(&leds_mutex);
	*red = leds[led][0];
	*green = leds[led][1];
	*blue = leds[led][2];
	pthread_mutex_unlock(&leds_mutex);
	return 0;
}

void lb_init(void)
{
	if (fake_power)
		lb_set_rgb(NUM_LEDS, 0, 0, 0);
};
void lb_off(void)
{
	fake_power = 0;
	update_window();
};
void lb_on(void)
{
	fake_power = 1;
	update_window();
};
void lb_hc_cmd_dump(struct ec_response_lightbar *out)
{
	printf("lightbar is %s\n", fake_power ? "on" : "off");
	memset(out, fake_power, sizeof(*out));
};
void lb_hc_cmd_reg(const struct ec_params_lightbar *in) { };

int lb_power(int enabled)
{
	return fake_power;
}


/*****************************************************************************/
/* Event handling stuff */

void *entry_windows(void *ptr)
{
	xcb_generic_event_t *e;
	xcb_expose_event_t *ev;
	xcb_button_press_event_t *bv;
	int chg = 1;

	while ((e = xcb_wait_for_event(c))) {

		switch (e->response_type & ~0x80) {
		case XCB_EXPOSE:
			ev = (xcb_expose_event_t *)e;
			if (win_w != ev->width || win_h != ev->height) {
				win_w = ev->width;
				win_h = ev->height;
			}
			update_window();
			break;
		case XCB_BUTTON_PRESS:
			bv = (xcb_button_press_event_t *)e;
			switch (bv->detail) {
			case 1:
				demo_battery_level(-1);
				break;
			case 3:
				demo_battery_level(+1);
				break;
			case 2:
				chg = !chg;
				demo_is_charging(chg);
				break;
			}
			break;
		}

		free(e);
	}

	cleanup();
	exit(0);
	return 0;
}