Skip to content

Commit df239a4

Browse files
author
volker
committed
correct brightness handling, reduced round trips
git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@973 3ae390bd-cb1e-0410-b409-cd5a39f66f1f
1 parent ba89cd6 commit df239a4

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

drv_X11.c

+38-16
Original file line numberDiff line numberDiff line change
@@ -91,25 +91,44 @@ static Window w, rw;
9191
static Visual *vi;
9292
static GC gc;
9393
static Colormap cm;
94-
static XColor xc;
9594
static Pixmap pm;
9695

9796

9897
/****************************************/
9998
/*** hardware dependant functions ***/
10099
/****************************************/
101100

102-
static void drv_X11_color(RGBA c, int brightness)
101+
static XColor drv_X11_color(RGBA c, int brightness)
103102
{
103+
static XColor col[64];
104+
static unsigned char alloced[64] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
107+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
108+
};
109+
XColor xc;
110+
int key;
111+
104112
xc.red = brightness * c.R;
105113
xc.green = brightness * c.G;
106114
xc.blue = brightness * c.B;
107-
xc.flags = DoRed | DoGreen | DoBlue;
108-
if (XAllocColor(dp, cm, &xc) == False) {
109-
error("%s: XAllocColor(%02x%02x%02x) failed!", Name, c.R, c.G, c.B);
115+
/* 16bits per color, compressed to 6 bits (2 each color) */
116+
key = (xc.red & 0xc000) >> 10 | (xc.green & 0xc000) >> 12 | (xc.blue & 0xc000) >> 14;
117+
/* todo: support more than 64 colors: check if allocated color is exactly the requested */
118+
if (alloced[key]) {
119+
xc = col[key];
120+
} else {
121+
xc.flags = DoRed | DoGreen | DoBlue;
122+
if (XAllocColor(dp, cm, &xc) == False) {
123+
error("%s: XAllocColor(%02x%02x%02x) failed!", Name, c.R, c.G, c.B);
124+
}
125+
col[key] = xc;
126+
alloced[key] = 1;
110127
}
128+
111129
XSetForeground(dp, gc, xc.pixel);
112130

131+
return (xc);
113132
}
114133

115134

@@ -135,6 +154,7 @@ static void drv_X11_blit(const int row, const int col, const int height, const i
135154
static int drv_X11_brightness(int brightness)
136155
{
137156
static int Brightness = 255;
157+
int i;
138158

139159
/* -1 is used to query the current brightness */
140160
if (brightness == -1)
@@ -156,11 +176,14 @@ static int drv_X11_brightness(int brightness)
156176
BL_COL.B = BP_COL.B * dim;
157177

158178
/* set new background */
159-
drv_X11_color(BR_COL, brightness);
160-
XSetWindowBackground(dp, w, xc.pixel);
179+
XSetWindowBackground(dp, w, drv_X11_color(BR_COL, brightness).pixel);
161180

162181
/* redraw every LCD pixel */
163-
drv_X11_blit(0, 0, DROWS, DCOLS);
182+
XClearWindow(dp, w);
183+
for (i = 0; i < DROWS * DCOLS; i++) {
184+
drv_X11_FB[i] = NO_COL;
185+
}
186+
drv_X11_blit(0, 0, LROWS, LCOLS);
164187

165188
/* remember new brightness */
166189
Brightness = brightness;
@@ -319,8 +342,8 @@ static void drv_X11_timer( __attribute__ ((unused))
319342
static int btn = 0;
320343

321344
if (XCheckWindowEvent(dp, w, ExposureMask | ButtonPressMask | ButtonReleaseMask, &ev) == 0
322-
/* there is no ClientMessageMask, so this will be checked separately */
323-
&& XCheckTypedWindowEvent(dp, w, ClientMessage, &ev) == 0)
345+
/* there is no ClientMessageMask, so this will be checked separately */
346+
&& XCheckTypedWindowEvent(dp, w, ClientMessage, &ev) == 0)
324347
return;
325348

326349
switch (ev.type) {
@@ -375,10 +398,10 @@ static void drv_X11_timer( __attribute__ ((unused))
375398
case ClientMessage:
376399
if ((Atom) (ev.xclient.data.l[0]) == wmDeleteMessage) {
377400
info("%s: Window closed by WindowManager, quit.", Name);
378-
if (raise(SIGTERM) != 0) {
379-
error("%s: Error raising SIGTERM: exit!", Name);
380-
exit(1);
381-
}
401+
if (raise(SIGTERM) != 0) {
402+
error("%s: Error raising SIGTERM: exit!", Name);
403+
exit(1);
404+
}
382405
} else {
383406
debug("%s: Got XClient message 0x%lx %lx %lx %lx %lx", Name, ev.xclient.data.l[0],
384407
ev.xclient.data.l[1], ev.xclient.data.l[2], ev.xclient.data.l[3], ev.xclient.data.l[4]);
@@ -511,8 +534,7 @@ static int drv_X11_start(const char *section)
511534
wmDeleteMessage = XInternAtom(dp, "WM_DELETE_WINDOW", False);
512535
XSetWMProtocols(dp, w, &wmDeleteMessage, 1);
513536

514-
drv_X11_color(BR_COL, 255);
515-
XSetWindowBackground(dp, w, xc.pixel);
537+
XSetWindowBackground(dp, w, drv_X11_color(BR_COL, 255).pixel);
516538
XClearWindow(dp, w);
517539

518540
/* set brightness (after first background painting) */

0 commit comments

Comments
 (0)