blob: ef4abe2522e042d59f510fbf0062fbe84cde4794 [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _MINUI_H_
18#define _MINUI_H_
19
Doug Zongker830b3e32014-03-11 13:22:04 -070020#include <sys/types.h>
21
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070022#include <functional>
Tianjie Xu2078b222017-03-22 12:27:26 -070023#include <string>
Tianjie Xu29d55752017-09-20 17:53:46 -070024#include <vector>
Doug Zongker28ce47c2011-10-28 10:33:05 -070025
Elliott Hughes07138192015-04-10 09:40:53 -070026//
27// Graphics.
28//
29
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070030struct GRSurface {
Luke Song846012f2017-09-13 15:56:16 -070031 int width;
32 int height;
33 int row_bytes;
34 int pixel_bytes;
35 unsigned char* data;
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070036};
Doug Zongker39cf4172014-03-06 16:16:05 -080037
Damien Bargiacchi35fff612016-08-11 15:57:03 -070038struct GRFont {
Luke Song846012f2017-09-13 15:56:16 -070039 GRSurface* texture;
40 int char_width;
41 int char_height;
42};
43
44enum GRRotation {
45 ROTATION_NONE = 0,
46 ROTATION_RIGHT = 1,
47 ROTATION_DOWN = 2,
48 ROTATION_LEFT = 3,
Damien Bargiacchi35fff612016-08-11 15:57:03 -070049};
50
Tao Bao9f426332018-06-13 10:39:44 -070051// Initializes the graphics backend and loads font file. Returns 0 on success, or -1 on error. Note
52// that the font initialization failure would be non-fatal, as caller may not need to draw any text
53// at all. Caller can check the font initialization result via gr_sys_font() as needed.
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070054int gr_init();
Tao Bao9f426332018-06-13 10:39:44 -070055
56// Frees the allocated resources. The function is idempotent, and safe to be called if gr_init()
57// didn't finish successfully.
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070058void gr_exit();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080059
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070060int gr_fb_width();
61int gr_fb_height();
Doug Zongker39cf4172014-03-06 16:16:05 -080062
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070063void gr_flip();
Dima Zavin4daf48a2011-08-30 11:59:20 -070064void gr_fb_blank(bool blank);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080065
Tao Bao9f426332018-06-13 10:39:44 -070066// Clears entire surface to current color.
67void gr_clear();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080068void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
Doug Zongkerc560a672012-12-18 16:31:27 -080069void gr_fill(int x1, int y1, int x2, int y2);
Damien Bargiacchi35fff612016-08-11 15:57:03 -070070
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070071void gr_texticon(int x, int y, GRSurface* icon);
Damien Bargiacchi35fff612016-08-11 15:57:03 -070072
73const GRFont* gr_sys_font();
Damien Bargiacchid00f5eb2016-09-09 07:14:08 -070074int gr_init_font(const char* name, GRFont** dest);
Luke Song846012f2017-09-13 15:56:16 -070075void gr_text(const GRFont* font, int x, int y, const char* s, bool bold);
Tao Bao9f426332018-06-13 10:39:44 -070076// Returns -1 if font is nullptr.
Luke Song846012f2017-09-13 15:56:16 -070077int gr_measure(const GRFont* font, const char* s);
Tao Bao9f426332018-06-13 10:39:44 -070078// Returns -1 if font is nullptr.
Tianjie Xu842f2a32018-05-31 18:16:28 -070079int gr_font_size(const GRFont* font, int* x, int* y);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080080
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -070081void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy);
Tao Bao9f426332018-06-13 10:39:44 -070082unsigned int gr_get_width(const GRSurface* surface);
83unsigned int gr_get_height(const GRSurface* surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080084
Tao Bao9f426332018-06-13 10:39:44 -070085// Sets rotation, flips gr_fb_width/height if 90 degree rotation difference
Luke Song846012f2017-09-13 15:56:16 -070086void gr_rotate(GRRotation rotation);
87
Elliott Hughes07138192015-04-10 09:40:53 -070088//
89// Input events.
90//
91
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080092struct input_event;
93
Tao Bao0b1118d2017-01-14 07:46:10 -080094using ev_callback = std::function<int(int fd, uint32_t epevents)>;
95using ev_set_key_callback = std::function<int(int code, int value)>;
Dima Zavinbc290632011-08-30 11:59:45 -070096
Tao Bao5f8dd992017-07-28 00:05:40 -070097int ev_init(ev_callback input_cb, bool allow_touch_inputs = false);
Elliott Hughes07cfb8f2015-04-10 13:12:05 -070098void ev_exit();
Tao Bao0b1118d2017-01-14 07:46:10 -080099int ev_add_fd(int fd, ev_callback cb);
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -0700100void ev_iterate_available_keys(const std::function<void(int)>& f);
Tao Bao5f8dd992017-07-28 00:05:40 -0700101void ev_iterate_touch_inputs(const std::function<void(int)>& action);
Tao Bao0b1118d2017-01-14 07:46:10 -0800102int ev_sync_key_state(const ev_set_key_callback& set_key_cb);
Dima Zavinbc290632011-08-30 11:59:45 -0700103
Elliott Hughes07138192015-04-10 09:40:53 -0700104// 'timeout' has the same semantics as poll(2).
105// 0 : don't block
106// < 0 : block forever
107// > 0 : block for 'timeout' milliseconds
Dima Zavinbc290632011-08-30 11:59:45 -0700108int ev_wait(int timeout);
109
Elliott Hughes07cfb8f2015-04-10 13:12:05 -0700110int ev_get_input(int fd, uint32_t epevents, input_event* ev);
111void ev_dispatch();
112int ev_get_epollfd();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800113
Elliott Hughes07138192015-04-10 09:40:53 -0700114//
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800115// Resources
Elliott Hughes07138192015-04-10 09:40:53 -0700116//
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800117
Tianjie Xu2078b222017-03-22 12:27:26 -0700118bool matches_locale(const std::string& prefix, const std::string& locale);
Tianjie Xu2430e292016-04-19 15:02:41 -0700119
Doug Zongkera418aa72014-03-17 12:10:02 -0700120// res_create_*_surface() functions return 0 if no error, else
121// negative.
122//
123// A "display" surface is one that is intended to be drawn to the
124// screen with gr_blit(). An "alpha" surface is a grayscale image
125// interpreted as an alpha mask used to render text in the current
126// color (with gr_text() or gr_texticon()).
127//
128// All these functions load PNG images from "/res/images/${name}.png".
129
130// Load a single display surface from a PNG image.
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700131int res_create_display_surface(const char* name, GRSurface** pSurface);
Doug Zongkera418aa72014-03-17 12:10:02 -0700132
133// Load an array of display surfaces from a single PNG image. The PNG
134// should have a 'Frames' text chunk whose value is the number of
135// frames this image represents. The pixel data itself is interlaced
136// by row.
Luke Song846012f2017-09-13 15:56:16 -0700137int res_create_multi_display_surface(const char* name, int* frames, int* fps,
138 GRSurface*** pSurface);
Doug Zongkera418aa72014-03-17 12:10:02 -0700139
140// Load a single alpha surface from a grayscale PNG image.
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700141int res_create_alpha_surface(const char* name, GRSurface** pSurface);
Doug Zongkera418aa72014-03-17 12:10:02 -0700142
143// Load part of a grayscale PNG image that is the first match for the
144// given locale. The image is expected to be a composite of multiple
145// translations of the same text, with special added rows that encode
146// the subimages' size and intended locale in the pixel data. See
Tianjie Xu9a259772016-07-28 14:15:00 -0700147// bootable/recovery/tools/recovery_l10n for an app that will generate
148// these specialized images from Android resources.
Doug Zongkera418aa72014-03-17 12:10:02 -0700149int res_create_localized_alpha_surface(const char* name, const char* locale,
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700150 GRSurface** pSurface);
Doug Zongkera418aa72014-03-17 12:10:02 -0700151
Tianjie Xu29d55752017-09-20 17:53:46 -0700152// Return a list of locale strings embedded in |png_name|. Return a empty list in case of failure.
153std::vector<std::string> get_locales_in_png(const std::string& png_name);
154
Doug Zongkera418aa72014-03-17 12:10:02 -0700155// Free a surface allocated by any of the res_create_*_surface()
156// functions.
Elliott Hughes0a5cb0c2015-04-15 10:58:56 -0700157void res_free_surface(GRSurface* surface);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800158
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800159#endif