Switch minui over to C++.
Change-Id: I59e08a304ae514a3fdb6fab58721f11670bc1b01
diff --git a/minui/Android.mk b/minui/Android.mk
index 66dea74..52f0662 100644
--- a/minui/Android.mk
+++ b/minui/Android.mk
@@ -3,10 +3,10 @@
LOCAL_SRC_FILES := \
events.cpp \
- graphics.c \
- graphics_adf.c \
- graphics_fbdev.c \
- resources.c \
+ graphics.cpp \
+ graphics_adf.cpp \
+ graphics_fbdev.cpp \
+ resources.cpp \
LOCAL_WHOLE_STATIC_LIBRARIES += libadf
LOCAL_STATIC_LIBRARIES += libpng
diff --git a/minui/events.cpp b/minui/events.cpp
index daa10c6..2d47a58 100644
--- a/minui/events.cpp
+++ b/minui/events.cpp
@@ -39,10 +39,10 @@
};
static int g_epoll_fd;
-static struct epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS];
+static epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS];
static int npolledevents;
-static struct fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
+static fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS];
static unsigned ev_count = 0;
static unsigned ev_dev_count = 0;
@@ -62,7 +62,7 @@
DIR* dir = opendir("/dev/input");
if (dir != NULL) {
- struct dirent* de;
+ dirent* de;
while ((de = readdir(dir))) {
unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)];
@@ -83,7 +83,7 @@
continue;
}
- struct epoll_event ev;
+ epoll_event ev;
ev.events = EPOLLIN | EPOLLWAKEUP;
ev.data.ptr = &ev_fdinfo[ev_count];
if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
@@ -121,7 +121,7 @@
return -1;
}
- struct epoll_event ev;
+ epoll_event ev;
ev.events = EPOLLIN | EPOLLWAKEUP;
ev.data.ptr = (void *)&ev_fdinfo[ev_count];
int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev);
@@ -163,7 +163,7 @@
}
}
-int ev_get_input(int fd, uint32_t epevents, struct input_event* ev) {
+int ev_get_input(int fd, uint32_t epevents, input_event* ev) {
if (epevents & EPOLLIN) {
ssize_t r = read(fd, ev, sizeof(*ev));
if (r == sizeof(*ev)) {
diff --git a/minui/graphics.c b/minui/graphics.cpp
similarity index 96%
rename from minui/graphics.c
rename to minui/graphics.cpp
index 9d1e1b4..d7d6e8d 100644
--- a/minui/graphics.c
+++ b/minui/graphics.cpp
@@ -35,11 +35,11 @@
#include "minui.h"
#include "graphics.h"
-typedef struct {
+struct GRFont {
GRSurface* texture;
int cwidth;
int cheight;
-} GRFont;
+};
static GRFont* gr_font = NULL;
static minui_backend* gr_backend = NULL;
@@ -269,7 +269,7 @@
static void gr_init_font(void)
{
- gr_font = calloc(sizeof(*gr_font), 1);
+ gr_font = reinterpret_cast<GRFont*>(calloc(sizeof(*gr_font), 1));
int res = res_create_alpha_surface("font", &(gr_font->texture));
if (res == 0) {
@@ -282,14 +282,14 @@
printf("failed to read font: res=%d\n", res);
// fall back to the compiled-in font.
- gr_font->texture = malloc(sizeof(*gr_font->texture));
+ gr_font->texture = reinterpret_cast<GRSurface*>(malloc(sizeof(*gr_font->texture)));
gr_font->texture->width = font.width;
gr_font->texture->height = font.height;
gr_font->texture->row_bytes = font.width;
gr_font->texture->pixel_bytes = 1;
- unsigned char* bits = malloc(font.width * font.height);
- gr_font->texture->data = (void*) bits;
+ unsigned char* bits = reinterpret_cast<unsigned char*>(malloc(font.width * font.height));
+ gr_font->texture->data = reinterpret_cast<unsigned char*>(bits);
unsigned char data;
unsigned char* in = font.rundata;
diff --git a/minui/graphics.h b/minui/graphics.h
index 993e986..ed229a0 100644
--- a/minui/graphics.h
+++ b/minui/graphics.h
@@ -17,34 +17,26 @@
#ifndef _GRAPHICS_H_
#define _GRAPHICS_H_
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#include <stdbool.h>
#include "minui.h"
-typedef struct minui_backend {
+// TODO: lose the function pointers.
+struct minui_backend {
// Initializes the backend and returns a gr_surface to draw into.
- gr_surface (*init)(struct minui_backend*);
+ gr_surface (*init)(minui_backend*);
// Causes the current drawing surface (returned by the most recent
// call to flip() or init()) to be displayed, and returns a new
// drawing surface.
- gr_surface (*flip)(struct minui_backend*);
+ gr_surface (*flip)(minui_backend*);
// Blank (or unblank) the screen.
- void (*blank)(struct minui_backend*, bool);
+ void (*blank)(minui_backend*, bool);
// Device cleanup when drawing is done.
- void (*exit)(struct minui_backend*);
-} minui_backend;
+ void (*exit)(minui_backend*);
+};
minui_backend* open_fbdev();
minui_backend* open_adf();
-#ifdef __cplusplus
-}
-#endif
-
#endif
diff --git a/minui/graphics_adf.c b/minui/graphics_adf.cpp
similarity index 81%
rename from minui/graphics_adf.c
rename to minui/graphics_adf.cpp
index c023d4d..ea7c0ab 100644
--- a/minui/graphics_adf.c
+++ b/minui/graphics_adf.cpp
@@ -44,15 +44,13 @@
unsigned int current_surface;
unsigned int n_surfaces;
- struct adf_surface_pdata surfaces[2];
+ adf_surface_pdata surfaces[2];
};
-static gr_surface adf_flip(struct minui_backend *backend);
-static void adf_blank(struct minui_backend *backend, bool blank);
+static gr_surface adf_flip(minui_backend *backend);
+static void adf_blank(minui_backend *backend, bool blank);
-static int adf_surface_init(struct adf_pdata *pdata,
- struct drm_mode_modeinfo *mode, struct adf_surface_pdata *surf)
-{
+static int adf_surface_init(adf_pdata *pdata, drm_mode_modeinfo *mode, adf_surface_pdata *surf) {
memset(surf, 0, sizeof(*surf));
surf->fd = adf_interface_simple_buffer_alloc(pdata->intf_fd, mode->hdisplay,
@@ -65,8 +63,9 @@
surf->base.row_bytes = surf->pitch;
surf->base.pixel_bytes = (pdata->format == DRM_FORMAT_RGB565) ? 2 : 4;
- surf->base.data = mmap(NULL, surf->pitch * surf->base.height, PROT_WRITE,
- MAP_SHARED, surf->fd, surf->offset);
+ surf->base.data = reinterpret_cast<uint8_t*>(mmap(NULL,
+ surf->pitch * surf->base.height, PROT_WRITE,
+ MAP_SHARED, surf->fd, surf->offset));
if (surf->base.data == MAP_FAILED) {
close(surf->fd);
return -errno;
@@ -75,9 +74,9 @@
return 0;
}
-static int adf_interface_init(struct adf_pdata *pdata)
+static int adf_interface_init(adf_pdata *pdata)
{
- struct adf_interface_data intf_data;
+ adf_interface_data intf_data;
int ret = 0;
int err;
@@ -107,7 +106,7 @@
return ret;
}
-static int adf_device_init(struct adf_pdata *pdata, struct adf_device *dev)
+static int adf_device_init(adf_pdata *pdata, adf_device *dev)
{
adf_id_t intf_id;
int intf_fd;
@@ -137,7 +136,7 @@
static gr_surface adf_init(minui_backend *backend)
{
- struct adf_pdata *pdata = (struct adf_pdata *)backend;
+ adf_pdata *pdata = (adf_pdata *)backend;
adf_id_t *dev_ids = NULL;
ssize_t n_dev_ids, i;
gr_surface ret;
@@ -164,7 +163,7 @@
pdata->intf_fd = -1;
for (i = 0; i < n_dev_ids && pdata->intf_fd < 0; i++) {
- struct adf_device dev;
+ adf_device dev;
int err = adf_device_open(dev_ids[i], O_RDWR, &dev);
if (err < 0) {
@@ -194,10 +193,10 @@
return ret;
}
-static gr_surface adf_flip(struct minui_backend *backend)
+static gr_surface adf_flip(minui_backend *backend)
{
- struct adf_pdata *pdata = (struct adf_pdata *)backend;
- struct adf_surface_pdata *surf = &pdata->surfaces[pdata->current_surface];
+ adf_pdata *pdata = (adf_pdata *)backend;
+ adf_surface_pdata *surf = &pdata->surfaces[pdata->current_surface];
int fence_fd = adf_interface_simple_post(pdata->intf_fd, pdata->eng_id,
surf->base.width, surf->base.height, pdata->format, surf->fd,
@@ -209,22 +208,22 @@
return &pdata->surfaces[pdata->current_surface].base;
}
-static void adf_blank(struct minui_backend *backend, bool blank)
+static void adf_blank(minui_backend *backend, bool blank)
{
- struct adf_pdata *pdata = (struct adf_pdata *)backend;
+ adf_pdata *pdata = (adf_pdata *)backend;
adf_interface_blank(pdata->intf_fd,
blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON);
}
-static void adf_surface_destroy(struct adf_surface_pdata *surf)
+static void adf_surface_destroy(adf_surface_pdata *surf)
{
munmap(surf->base.data, surf->pitch * surf->base.height);
close(surf->fd);
}
-static void adf_exit(struct minui_backend *backend)
+static void adf_exit(minui_backend *backend)
{
- struct adf_pdata *pdata = (struct adf_pdata *)backend;
+ adf_pdata *pdata = (adf_pdata *)backend;
unsigned int i;
for (i = 0; i < pdata->n_surfaces; i++)
@@ -236,7 +235,7 @@
minui_backend *open_adf()
{
- struct adf_pdata *pdata = calloc(1, sizeof(*pdata));
+ adf_pdata* pdata = reinterpret_cast<adf_pdata*>(calloc(1, sizeof(*pdata)));
if (!pdata) {
perror("allocating adf backend failed");
return NULL;
diff --git a/minui/graphics_fbdev.c b/minui/graphics_fbdev.cpp
similarity index 95%
rename from minui/graphics_fbdev.c
rename to minui/graphics_fbdev.cpp
index 4a5b5b5..9dbdde8 100644
--- a/minui/graphics_fbdev.c
+++ b/minui/graphics_fbdev.cpp
@@ -43,7 +43,7 @@
static GRSurface* gr_draw = NULL;
static int displayed_buffer;
-static struct fb_var_screeninfo vi;
+static fb_var_screeninfo vi;
static int fb_fd = -1;
static minui_backend my_backend = {
@@ -80,17 +80,13 @@
}
static gr_surface fbdev_init(minui_backend* backend) {
- int fd;
- void *bits;
-
- struct fb_fix_screeninfo fi;
-
- fd = open("/dev/graphics/fb0", O_RDWR);
- if (fd < 0) {
+ int fd = open("/dev/graphics/fb0", O_RDWR);
+ if (fd == -1) {
perror("cannot open fb0");
return NULL;
}
+ fb_fix_screeninfo fi;
if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) {
perror("failed to get fb0 info");
close(fd);
@@ -124,7 +120,7 @@
vi.green.offset, vi.green.length,
vi.blue.offset, vi.blue.length);
- bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+ void* bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (bits == MAP_FAILED) {
perror("failed to mmap framebuffer");
close(fd);
@@ -137,7 +133,7 @@
gr_framebuffer[0].height = vi.yres;
gr_framebuffer[0].row_bytes = fi.line_length;
gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8;
- gr_framebuffer[0].data = bits;
+ gr_framebuffer[0].data = reinterpret_cast<uint8_t*>(bits);
memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes);
/* check if we can use double buffering */
diff --git a/minui/minui.h b/minui/minui.h
index 82abb8a..eca3a50 100644
--- a/minui/minui.h
+++ b/minui/minui.h
@@ -19,33 +19,30 @@
#include <sys/types.h>
-#include <stdbool.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
+#include <functional>
//
// Graphics.
//
-typedef struct {
+struct GRSurface {
int width;
int height;
int row_bytes;
int pixel_bytes;
unsigned char* data;
-} GRSurface;
+};
+// TODO: remove this.
typedef GRSurface* gr_surface;
-int gr_init(void);
-void gr_exit(void);
+int gr_init();
+void gr_exit();
-int gr_fb_width(void);
-int gr_fb_height(void);
+int gr_fb_width();
+int gr_fb_height();
-void gr_flip(void);
+void gr_flip();
void gr_fb_blank(bool blank);
void gr_clear(); // clear entire surface to current color
@@ -66,12 +63,14 @@
struct input_event;
+// TODO: move these over to std::function.
typedef int (*ev_callback)(int fd, uint32_t epevents, void* data);
typedef int (*ev_set_key_callback)(int code, int value, void* data);
int ev_init(ev_callback input_cb, void* data);
-void ev_exit(void);
+void ev_exit();
int ev_add_fd(int fd, ev_callback cb, void* data);
+void ev_iterate_available_keys(std::function<void(int)> f);
int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data);
// 'timeout' has the same semantics as poll(2).
@@ -80,9 +79,9 @@
// > 0 : block for 'timeout' milliseconds
int ev_wait(int timeout);
-int ev_get_input(int fd, uint32_t epevents, struct input_event *ev);
-void ev_dispatch(void);
-int ev_get_epollfd(void);
+int ev_get_input(int fd, uint32_t epevents, input_event* ev);
+void ev_dispatch();
+int ev_get_epollfd();
//
// Resources
@@ -124,15 +123,4 @@
// functions.
void res_free_surface(gr_surface surface);
-#ifdef __cplusplus
-}
-#endif
-
-#ifdef __cplusplus
-
-#include <functional>
-void ev_iterate_available_keys(std::function<void(int)> f);
-
-#endif
-
#endif
diff --git a/minui/resources.c b/minui/resources.cpp
similarity index 95%
rename from minui/resources.c
rename to minui/resources.cpp
index 886c325..fa413b6 100644
--- a/minui/resources.c
+++ b/minui/resources.cpp
@@ -37,7 +37,8 @@
#define SURFACE_DATA_ALIGNMENT 8
static gr_surface malloc_surface(size_t data_size) {
- unsigned char* temp = malloc(sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT);
+ size_t size = sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT;
+ unsigned char* temp = reinterpret_cast<unsigned char*>(malloc(size));
if (temp == NULL) return NULL;
gr_surface surface = (gr_surface) temp;
surface->data = temp + sizeof(GRSurface) +
@@ -50,6 +51,8 @@
char resPath[256];
unsigned char header[8];
int result = 0;
+ int color_type, bit_depth;
+ size_t bytesRead;
snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name);
resPath[sizeof(resPath)-1] = '\0';
@@ -59,7 +62,7 @@
goto exit;
}
- size_t bytesRead = fread(header, 1, sizeof(header), fp);
+ bytesRead = fread(header, 1, sizeof(header), fp);
if (bytesRead != sizeof(header)) {
result = -2;
goto exit;
@@ -91,7 +94,6 @@
png_set_sig_bytes(*png_ptr, sizeof(header));
png_read_info(*png_ptr, *info_ptr);
- int color_type, bit_depth;
png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
&color_type, NULL, NULL, NULL);
@@ -204,6 +206,8 @@
png_infop info_ptr = NULL;
png_uint_32 width, height;
png_byte channels;
+ unsigned char* p_row;
+ unsigned int y;
*pSurface = NULL;
@@ -220,8 +224,7 @@
png_set_bgr(png_ptr);
#endif
- unsigned char* p_row = malloc(width * 4);
- unsigned int y;
+ p_row = reinterpret_cast<unsigned char*>(malloc(width * 4));
for (y = 0; y < height; ++y) {
png_read_row(png_ptr, p_row, NULL);
transform_rgb_to_draw(p_row, surface->data + y * surface->row_bytes, channels, width);
@@ -244,6 +247,10 @@
png_uint_32 width, height;
png_byte channels;
int i;
+ png_textp text;
+ int num_text;
+ unsigned char* p_row;
+ unsigned int y;
*pSurface = NULL;
*frames = -1;
@@ -252,8 +259,6 @@
if (result < 0) return result;
*frames = 1;
- png_textp text;
- int num_text;
if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
for (i = 0; i < num_text; ++i) {
if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
@@ -270,7 +275,7 @@
goto exit;
}
- surface = malloc(*frames * sizeof(gr_surface));
+ surface = reinterpret_cast<gr_surface*>(malloc(*frames * sizeof(gr_surface)));
if (surface == NULL) {
result = -8;
goto exit;
@@ -287,8 +292,7 @@
png_set_bgr(png_ptr);
#endif
- unsigned char* p_row = malloc(width * 4);
- unsigned int y;
+ p_row = reinterpret_cast<unsigned char*>(malloc(width * 4));
for (y = 0; y < height; ++y) {
png_read_row(png_ptr, p_row, NULL);
int frame = y % *frames;
@@ -387,6 +391,8 @@
png_infop info_ptr = NULL;
png_uint_32 width, height;
png_byte channels;
+ unsigned char* row;
+ png_uint_32 y;
*pSurface = NULL;
@@ -407,8 +413,7 @@
goto exit;
}
- unsigned char* row = malloc(width);
- png_uint_32 y;
+ row = reinterpret_cast<unsigned char*>(malloc(width));
for (y = 0; y < height; ++y) {
png_read_row(png_ptr, row, NULL);
int w = (row[1] << 8) | row[0];