From 9831ddd3a82cfd077a1ba6c81878269060620a53 Mon Sep 17 00:00:00 2001 From: Serge Vakulenko Date: Tue, 6 Oct 2015 13:15:39 -0700 Subject: [PATCH] Add gpanel library. --- src/libgpanel/Makefile | 18 ++++++ src/libgpanel/char.c | 39 ++++++++++++ src/libgpanel/circle.c | 36 +++++++++++ src/libgpanel/clear.c | 30 ++++++++++ src/libgpanel/fill.c | 37 ++++++++++++ src/libgpanel/image.c | 37 ++++++++++++ src/libgpanel/line.c | 37 ++++++++++++ src/libgpanel/open.c | 50 ++++++++++++++++ src/libgpanel/pixel.c | 35 +++++++++++ src/libgpanel/rect.c | 37 ++++++++++++ src/libgpanel/text.c | 39 ++++++++++++ src/libgpanel/text_width.c | 52 ++++++++++++++++ sys/include/gpanel.h | 119 +++++++++++++++++++++++++++++++++++++ 13 files changed, 566 insertions(+) create mode 100644 src/libgpanel/Makefile create mode 100644 src/libgpanel/char.c create mode 100644 src/libgpanel/circle.c create mode 100644 src/libgpanel/clear.c create mode 100644 src/libgpanel/fill.c create mode 100644 src/libgpanel/image.c create mode 100644 src/libgpanel/line.c create mode 100644 src/libgpanel/open.c create mode 100644 src/libgpanel/pixel.c create mode 100644 src/libgpanel/rect.c create mode 100644 src/libgpanel/text.c create mode 100644 src/libgpanel/text_width.c create mode 100644 sys/include/gpanel.h diff --git a/src/libgpanel/Makefile b/src/libgpanel/Makefile new file mode 100644 index 0000000..6d55f9c --- /dev/null +++ b/src/libgpanel/Makefile @@ -0,0 +1,18 @@ +TOPSRC = $(shell cd ../..; pwd) +include $(TOPSRC)/target.mk + +CFLAGS += -O -Wall -Werror + +OBJS = open.o clear.o pixel.o line.o rect.o fill.o circle.o \ + image.o char.o text.o text_width.o + +all: ../libgpanel.a + +../libgpanel.a: ${OBJS} + @$(AR) cru $@ ${OBJS} + $(RANLIB) $@ + +install: all + +clean: + rm -f *.o a.out core test errs ../libgpanel*.a diff --git a/src/libgpanel/char.c b/src/libgpanel/char.c new file mode 100644 index 0000000..4e03c09 --- /dev/null +++ b/src/libgpanel/char.c @@ -0,0 +1,39 @@ +/* + * Draw a single character glyph. + * + * Copyright (C) 2015 Serge Vakulenko, + * + * Permission to use, copy, modify, and distribute this software + * and its documentation for any purpose and without fee is hereby + * granted, provided that the above copyright notice appear in all + * copies and that both that the copyright notice and this + * permission notice and warranty disclaimer appear in supporting + * documentation, and that the name of the author not be used in + * advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. + * + * The author disclaim all warranties with regard to this + * software, including all implied warranties of merchantability + * and fitness. In no event shall the author 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 +#include + +void gpanel_char(const struct gpanel_font_t *font, int color, int background, + int x, int y, int sym) +{ + struct gpanel_char_t param; + + param.font = font; + param.color = color; + param.background = background; + param.x = x; + param.y = y; + param.sym = sym; + ioctl(_gpanel_fd, GPANEL_CHAR, ¶m); +} diff --git a/src/libgpanel/circle.c b/src/libgpanel/circle.c new file mode 100644 index 0000000..5b0bbcc --- /dev/null +++ b/src/libgpanel/circle.c @@ -0,0 +1,36 @@ +/* + * Draw a circle (no fill). + * + * Copyright (C) 2015 Serge Vakulenko, + * + * Permission to use, copy, modify, and distribute this software + * and its documentation for any purpose and without fee is hereby + * granted, provided that the above copyright notice appear in all + * copies and that both that the copyright notice and this + * permission notice and warranty disclaimer appear in supporting + * documentation, and that the name of the author not be used in + * advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. + * + * The author disclaim all warranties with regard to this + * software, including all implied warranties of merchantability + * and fitness. In no event shall the author 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 +#include + +void gpanel_circle(int color, int x, int y, int radius) +{ + struct gpanel_circle_t param; + + param.color = color; + param.x = x; + param.y = x; + param.radius = radius; + ioctl(_gpanel_fd, GPANEL_CIRCLE, ¶m); +} diff --git a/src/libgpanel/clear.c b/src/libgpanel/clear.c new file mode 100644 index 0000000..0f00f1a --- /dev/null +++ b/src/libgpanel/clear.c @@ -0,0 +1,30 @@ +/* + * Clear the screen. + * + * Copyright (C) 2015 Serge Vakulenko, + * + * Permission to use, copy, modify, and distribute this software + * and its documentation for any purpose and without fee is hereby + * granted, provided that the above copyright notice appear in all + * copies and that both that the copyright notice and this + * permission notice and warranty disclaimer appear in supporting + * documentation, and that the name of the author not be used in + * advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. + * + * The author disclaim all warranties with regard to this + * software, including all implied warranties of merchantability + * and fitness. In no event shall the author 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 +#include + +void gpanel_clear(int color) +{ + ioctl(_gpanel_fd, GPANEL_CLEAR, color); +} diff --git a/src/libgpanel/fill.c b/src/libgpanel/fill.c new file mode 100644 index 0000000..b503af3 --- /dev/null +++ b/src/libgpanel/fill.c @@ -0,0 +1,37 @@ +/* + * Fill a rectangle. + * + * Copyright (C) 2015 Serge Vakulenko, + * + * Permission to use, copy, modify, and distribute this software + * and its documentation for any purpose and without fee is hereby + * granted, provided that the above copyright notice appear in all + * copies and that both that the copyright notice and this + * permission notice and warranty disclaimer appear in supporting + * documentation, and that the name of the author not be used in + * advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. + * + * The author disclaim all warranties with regard to this + * software, including all implied warranties of merchantability + * and fitness. In no event shall the author 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 +#include + +void gpanel_fill(int color, int x0, int y0, int x1, int y1) +{ + struct gpanel_rect_t param; + + param.color = color; + param.x0 = x0; + param.y0 = y0; + param.x1 = x1; + param.y1 = y1; + ioctl(_gpanel_fd, GPANEL_FILL, ¶m); +} diff --git a/src/libgpanel/image.c b/src/libgpanel/image.c new file mode 100644 index 0000000..a32a980 --- /dev/null +++ b/src/libgpanel/image.c @@ -0,0 +1,37 @@ +/* + * Draw a rectangular image. + * + * Copyright (C) 2015 Serge Vakulenko, + * + * Permission to use, copy, modify, and distribute this software + * and its documentation for any purpose and without fee is hereby + * granted, provided that the above copyright notice appear in all + * copies and that both that the copyright notice and this + * permission notice and warranty disclaimer appear in supporting + * documentation, and that the name of the author not be used in + * advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. + * + * The author disclaim all warranties with regard to this + * software, including all implied warranties of merchantability + * and fitness. In no event shall the author 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 +#include + +void gpanel_image(int x, int y, int width, int height, const unsigned short *data) +{ + struct gpanel_image_t param; + + param.x = x; + param.y = x; + param.width = width; + param.height = height; + param.image = data; + ioctl(_gpanel_fd, GPANEL_IMAGE, ¶m); +} diff --git a/src/libgpanel/line.c b/src/libgpanel/line.c new file mode 100644 index 0000000..5a56892 --- /dev/null +++ b/src/libgpanel/line.c @@ -0,0 +1,37 @@ +/* + * Draw a line. + * + * Copyright (C) 2015 Serge Vakulenko, + * + * Permission to use, copy, modify, and distribute this software + * and its documentation for any purpose and without fee is hereby + * granted, provided that the above copyright notice appear in all + * copies and that both that the copyright notice and this + * permission notice and warranty disclaimer appear in supporting + * documentation, and that the name of the author not be used in + * advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. + * + * The author disclaim all warranties with regard to this + * software, including all implied warranties of merchantability + * and fitness. In no event shall the author 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 +#include + +void gpanel_line(int color, int x0, int y0, int x1, int y1) +{ + struct gpanel_line_t param; + + param.color = color; + param.x0 = x0; + param.y0 = y0; + param.x1 = x1; + param.y1 = y1; + ioctl(_gpanel_fd, GPANEL_LINE, ¶m); +} diff --git a/src/libgpanel/open.c b/src/libgpanel/open.c new file mode 100644 index 0000000..37fa62f --- /dev/null +++ b/src/libgpanel/open.c @@ -0,0 +1,50 @@ +/* + * Device open and close routines. + * + * Copyright (C) 2015 Serge Vakulenko, + * + * Permission to use, copy, modify, and distribute this software + * and its documentation for any purpose and without fee is hereby + * granted, provided that the above copyright notice appear in all + * copies and that both that the copyright notice and this + * permission notice and warranty disclaimer appear in supporting + * documentation, and that the name of the author not be used in + * advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. + * + * The author disclaim all warranties with regard to this + * software, including all implied warranties of merchantability + * and fitness. In no event shall the author 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 +#include +#include + +int _gpanel_fd = -1; + +/* + * Open a graphics panel device. + */ +int gpanel_open(const char *devname) +{ + _gpanel_fd = open(devname, O_RDWR); + if (_gpanel_fd < 0) + return -1; + return 0; +} + +/* + * Close gpanel device. + */ +void gpanel_close(void) +{ + if (_gpanel_fd >= 0) { + close(_gpanel_fd); + _gpanel_fd = -1; + } +} diff --git a/src/libgpanel/pixel.c b/src/libgpanel/pixel.c new file mode 100644 index 0000000..0be1f39 --- /dev/null +++ b/src/libgpanel/pixel.c @@ -0,0 +1,35 @@ +/* + * Draw a single pixel. + * + * Copyright (C) 2015 Serge Vakulenko, + * + * Permission to use, copy, modify, and distribute this software + * and its documentation for any purpose and without fee is hereby + * granted, provided that the above copyright notice appear in all + * copies and that both that the copyright notice and this + * permission notice and warranty disclaimer appear in supporting + * documentation, and that the name of the author not be used in + * advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. + * + * The author disclaim all warranties with regard to this + * software, including all implied warranties of merchantability + * and fitness. In no event shall the author 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 +#include + +void gpanel_pixel(int color, int x, int y) +{ + struct gpanel_pixel_t param; + + param.color = color; + param.x = x; + param.y = x; + ioctl(_gpanel_fd, GPANEL_PIXEL, ¶m); +} diff --git a/src/libgpanel/rect.c b/src/libgpanel/rect.c new file mode 100644 index 0000000..831d153 --- /dev/null +++ b/src/libgpanel/rect.c @@ -0,0 +1,37 @@ +/* + * Draw a rectangle frame (no fill). + * + * Copyright (C) 2015 Serge Vakulenko, + * + * Permission to use, copy, modify, and distribute this software + * and its documentation for any purpose and without fee is hereby + * granted, provided that the above copyright notice appear in all + * copies and that both that the copyright notice and this + * permission notice and warranty disclaimer appear in supporting + * documentation, and that the name of the author not be used in + * advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. + * + * The author disclaim all warranties with regard to this + * software, including all implied warranties of merchantability + * and fitness. In no event shall the author 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 +#include + +void gpanel_rect(int color, int x0, int y0, int x1, int y1) +{ + struct gpanel_rect_t param; + + param.color = color; + param.x0 = x0; + param.y0 = y0; + param.x1 = x1; + param.y1 = y1; + ioctl(_gpanel_fd, GPANEL_RECT, ¶m); +} diff --git a/src/libgpanel/text.c b/src/libgpanel/text.c new file mode 100644 index 0000000..58fe631 --- /dev/null +++ b/src/libgpanel/text.c @@ -0,0 +1,39 @@ +/* + * Draw a string. + * + * Copyright (C) 2015 Serge Vakulenko, + * + * Permission to use, copy, modify, and distribute this software + * and its documentation for any purpose and without fee is hereby + * granted, provided that the above copyright notice appear in all + * copies and that both that the copyright notice and this + * permission notice and warranty disclaimer appear in supporting + * documentation, and that the name of the author not be used in + * advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. + * + * The author disclaim all warranties with regard to this + * software, including all implied warranties of merchantability + * and fitness. In no event shall the author 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 +#include + +void gpanel_text(const struct gpanel_font_t *font, int color, int background, + int x, int y, const char *text) +{ + struct gpanel_text_t param; + + param.font = font; + param.color = color; + param.background = background; + param.x = x; + param.y = y; + param.text = text; + ioctl(_gpanel_fd, GPANEL_TEXT, ¶m); +} diff --git a/src/libgpanel/text_width.c b/src/libgpanel/text_width.c new file mode 100644 index 0000000..d6237bb --- /dev/null +++ b/src/libgpanel/text_width.c @@ -0,0 +1,52 @@ +/* + * Compute a string width in pixels. + * + * Copyright (C) 2015 Serge Vakulenko, + * + * Permission to use, copy, modify, and distribute this software + * and its documentation for any purpose and without fee is hereby + * granted, provided that the above copyright notice appear in all + * copies and that both that the copyright notice and this + * permission notice and warranty disclaimer appear in supporting + * documentation, and that the name of the author not be used in + * advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. + * + * The author disclaim all warranties with regard to this + * software, including all implied warranties of merchantability + * and fitness. In no event shall the author 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 +#include +#include + +/* + * Calculate a width of text output. + * Handle both fixed and proportional fonts. + * TODO: UTF8 decoding. + */ +int gpanel_text_width(const struct gpanel_font_t *font, const char *text, int nchars) +{ + int width, c; + + if (! nchars) + nchars = strlen (text); + if (! font->width) { + /* Fixed-width font. */ + return nchars * font->maxwidth; + } + + width = 0; + while (--nchars >= 0) { + c = (unsigned char) *text++; + if (c < font->firstchar || c >= font->firstchar + font->size) + c = font->defaultchar; + width += font->width[c - font->firstchar]; + } + return width; +} diff --git a/sys/include/gpanel.h b/sys/include/gpanel.h new file mode 100644 index 0000000..d6a6fb2 --- /dev/null +++ b/sys/include/gpanel.h @@ -0,0 +1,119 @@ +/* + * Generic interface to a graphics panel device (TFT LCD, OLED and others). + * + * Copyright (C) 2015 Serge Vakulenko, + * + * Permission to use, copy, modify, and distribute this software + * and its documentation for any purpose and without fee is hereby + * granted, provided that the above copyright notice appear in all + * copies and that both that the copyright notice and this + * permission notice and warranty disclaimer appear in supporting + * documentation, and that the name of the author not be used in + * advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. + * + * The author disclaim all warranties with regard to this + * software, including all implied warranties of merchantability + * and fitness. In no event shall the author 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. + */ +#ifndef _GPANEL_H +#define _GPANEL_H + +/* + * Proportional/fixed font structure. + */ +struct gpanel_font_t { + const char * name; /* font name */ + int maxwidth; /* max width in pixels */ + unsigned int height; /* height in pixels */ + int ascent; /* ascent (baseline) height */ + int firstchar; /* first character in bitmap */ + int size; /* font size in characters */ + const unsigned short *bits; /* 16-bit right-padded bitmap data */ + const unsigned short *offset; /* offsets into bitmap data */ + const unsigned char *width; /* character widths or 0 if fixed */ + int defaultchar; /* default char (not glyph index) */ + long bits_size; /* # words of bits */ +}; + +struct gpanel_pixel_t { + int color; /* pixel color */ + int x, y; /* pixel position */ +}; + +struct gpanel_line_t { + int color; /* line color */ + int x0, y0; /* start point */ + int x1, y1; /* end point */ +}; + +struct gpanel_rect_t { + int color; /* border or fill color */ + int x0, y0; /* start point */ + int x1, y1; /* end point */ +}; + +struct gpanel_circle_t { + int color; /* border color */ + int x, y; /* center point */ + int radius; /* circle radius */ +}; + +struct gpanel_image_t { + int x, y; /* start point */ + int width, height; /* image size radius */ + const unsigned short *image; /* array of pixels */ +}; + +struct gpanel_char_t { + const struct gpanel_font_t *font; /* font data */ + int color; /* text color */ + int background; /* background color or -1 for transparent */ + int x, y; /* position */ + int sym; /* unicode symbol index */ +}; + +struct gpanel_text_t { + const struct gpanel_font_t *font; /* font data */ + int color; /* text color */ + int background; /* background color or -1 for transparent */ + int x, y; /* position */ + const char *text; /* UTF-8 text */ +}; + +#define GPANEL_CLEAR _IOW('g', 1, int) +#define GPANEL_PIXEL _IOW('g', 2, struct gpanel_pixel_t) +#define GPANEL_LINE _IOW('g', 3, struct gpanel_line_t) +#define GPANEL_RECT _IOW('g', 4, struct gpanel_rect_t) +#define GPANEL_FILL _IOW('g', 5, struct gpanel_rect_t) +#define GPANEL_CIRCLE _IOW('g', 6, struct gpanel_circle_t) +#define GPANEL_IMAGE _IOW('g', 7, struct gpanel_image_t) +#define GPANEL_CHAR _IOW('g', 8, struct gpanel_char_t) +#define GPANEL_TEXT _IOW('g', 9, struct gpanel_text_t) + +#ifndef KERNEL +/* + * User-level library. + */ +int gpanel_open(const char *devname); +void gpanel_close(void); +void gpanel_clear(int color); +void gpanel_pixel(int color, int x, int y); +void gpanel_line(int color, int x0, int y0, int x1, int y1); +void gpanel_rect(int color, int x0, int y0, int x1, int y1); +void gpanel_fill(int color, int x0, int y0, int x1, int y1); +void gpanel_circle(int color, int x, int y, int radius); +void gpanel_image(int x, int y, int width, int height, const unsigned short *data); +void gpanel_char(const struct gpanel_font_t *font, int color, int background, int x, int y, int sym); +void gpanel_text(const struct gpanel_font_t *font, int color, int background, int x, int y, const char *text); +int gpanel_text_width(const struct gpanel_font_t *font, const char *text, int nchars); + +extern int _gpanel_fd; +#endif + +#endif /* _GPANEL_H */