Add gpanel library.

This commit is contained in:
Serge Vakulenko
2015-10-06 13:15:39 -07:00
parent 8f220cdf97
commit 9831ddd3a8
13 changed files with 566 additions and 0 deletions

18
src/libgpanel/Makefile Normal file
View File

@@ -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

39
src/libgpanel/char.c Normal file
View File

@@ -0,0 +1,39 @@
/*
* Draw a single character glyph.
*
* Copyright (C) 2015 Serge Vakulenko, <serge@vak.ru>
*
* 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 <sys/ioctl.h>
#include <sys/gpanel.h>
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, &param);
}

36
src/libgpanel/circle.c Normal file
View File

@@ -0,0 +1,36 @@
/*
* Draw a circle (no fill).
*
* Copyright (C) 2015 Serge Vakulenko, <serge@vak.ru>
*
* 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 <sys/ioctl.h>
#include <sys/gpanel.h>
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, &param);
}

30
src/libgpanel/clear.c Normal file
View File

@@ -0,0 +1,30 @@
/*
* Clear the screen.
*
* Copyright (C) 2015 Serge Vakulenko, <serge@vak.ru>
*
* 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 <sys/ioctl.h>
#include <sys/gpanel.h>
void gpanel_clear(int color)
{
ioctl(_gpanel_fd, GPANEL_CLEAR, color);
}

37
src/libgpanel/fill.c Normal file
View File

@@ -0,0 +1,37 @@
/*
* Fill a rectangle.
*
* Copyright (C) 2015 Serge Vakulenko, <serge@vak.ru>
*
* 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 <sys/ioctl.h>
#include <sys/gpanel.h>
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, &param);
}

37
src/libgpanel/image.c Normal file
View File

@@ -0,0 +1,37 @@
/*
* Draw a rectangular image.
*
* Copyright (C) 2015 Serge Vakulenko, <serge@vak.ru>
*
* 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 <sys/ioctl.h>
#include <sys/gpanel.h>
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, &param);
}

37
src/libgpanel/line.c Normal file
View File

@@ -0,0 +1,37 @@
/*
* Draw a line.
*
* Copyright (C) 2015 Serge Vakulenko, <serge@vak.ru>
*
* 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 <sys/ioctl.h>
#include <sys/gpanel.h>
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, &param);
}

50
src/libgpanel/open.c Normal file
View File

@@ -0,0 +1,50 @@
/*
* Device open and close routines.
*
* Copyright (C) 2015 Serge Vakulenko, <serge@vak.ru>
*
* 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 <unistd.h>
#include <fcntl.h>
#include <sys/gpanel.h>
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;
}
}

35
src/libgpanel/pixel.c Normal file
View File

@@ -0,0 +1,35 @@
/*
* Draw a single pixel.
*
* Copyright (C) 2015 Serge Vakulenko, <serge@vak.ru>
*
* 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 <sys/ioctl.h>
#include <sys/gpanel.h>
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, &param);
}

37
src/libgpanel/rect.c Normal file
View File

@@ -0,0 +1,37 @@
/*
* Draw a rectangle frame (no fill).
*
* Copyright (C) 2015 Serge Vakulenko, <serge@vak.ru>
*
* 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 <sys/ioctl.h>
#include <sys/gpanel.h>
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, &param);
}

39
src/libgpanel/text.c Normal file
View File

@@ -0,0 +1,39 @@
/*
* Draw a string.
*
* Copyright (C) 2015 Serge Vakulenko, <serge@vak.ru>
*
* 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 <sys/ioctl.h>
#include <sys/gpanel.h>
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, &param);
}

View File

@@ -0,0 +1,52 @@
/*
* Compute a string width in pixels.
*
* Copyright (C) 2015 Serge Vakulenko, <serge@vak.ru>
*
* 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 <string.h>
#include <sys/ioctl.h>
#include <sys/gpanel.h>
/*
* 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;
}

119
sys/include/gpanel.h Normal file
View File

@@ -0,0 +1,119 @@
/*
* Generic interface to a graphics panel device (TFT LCD, OLED and others).
*
* Copyright (C) 2015 Serge Vakulenko, <serge@vak.ru>
*
* 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 */