The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 20 | #ifndef TW_USE_MINUI_21 |
| 21 | |
Doug Zongker | 830b3e3 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 24 | #include <functional> |
Tianjie Xu | 0a59956 | 2017-03-28 20:11:15 +0000 | [diff] [blame] | 25 | #include <string> |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 26 | #include <vector> |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 27 | |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 28 | // |
| 29 | // Graphics. |
| 30 | // |
| 31 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 32 | struct GRSurface { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 33 | int width; |
| 34 | int height; |
| 35 | int row_bytes; |
| 36 | int pixel_bytes; |
| 37 | unsigned char* data; |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 38 | }; |
Doug Zongker | 39cf417 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 39 | |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 40 | struct GRFont { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 41 | GRSurface* texture; |
| 42 | int char_width; |
| 43 | int char_height; |
| 44 | }; |
| 45 | |
| 46 | enum GRRotation { |
| 47 | ROTATION_NONE = 0, |
| 48 | ROTATION_RIGHT = 1, |
| 49 | ROTATION_DOWN = 2, |
| 50 | ROTATION_LEFT = 3, |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 53 | int gr_init(); |
| 54 | void gr_exit(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 55 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 56 | int gr_fb_width(); |
| 57 | int gr_fb_height(); |
Doug Zongker | 39cf417 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 58 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 59 | void gr_flip(); |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 60 | void gr_fb_blank(bool blank); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 61 | |
Doug Zongker | 39cf417 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 62 | void gr_clear(); // clear entire surface to current color |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 63 | void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a); |
Doug Zongker | c560a67 | 2012-12-18 16:31:27 -0800 | [diff] [blame] | 64 | void gr_fill(int x1, int y1, int x2, int y2); |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 65 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 66 | void gr_texticon(int x, int y, GRSurface* icon); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 67 | #ifdef TW_NO_MINUI_CUSTOM_FONTS |
| 68 | void gr_text(int x, int y, const char *s, bool bold); |
| 69 | int gr_measure(const char *s); |
| 70 | void gr_font_size(int *x, int *y); |
| 71 | void gr_set_font(__attribute__ ((unused))const char* name); |
| 72 | #else |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 73 | |
| 74 | const GRFont* gr_sys_font(); |
Damien Bargiacchi | d00f5eb | 2016-09-09 07:14:08 -0700 | [diff] [blame] | 75 | int gr_init_font(const char* name, GRFont** dest); |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 76 | void gr_text(const GRFont* font, int x, int y, const char* s, bool bold); |
| 77 | int gr_measure(const GRFont* font, const char* s); |
| 78 | void gr_font_size(const GRFont* font, int* x, int* y); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 79 | #endif |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 80 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 81 | void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy); |
| 82 | unsigned int gr_get_width(GRSurface* surface); |
| 83 | unsigned int gr_get_height(GRSurface* surface); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 84 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 85 | // Set rotation, flips gr_fb_width/height if 90 degree rotation difference |
| 86 | void gr_rotate(GRRotation rotation); |
| 87 | |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 88 | // |
| 89 | // Input events. |
| 90 | // |
| 91 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 92 | struct input_event; |
| 93 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 94 | #ifdef TW_USE_MINUI_WITH_DATA |
| 95 | typedef int (*ev_callback)(int fd, uint32_t epevents, void* data); |
| 96 | typedef int (*ev_set_key_callback)(int code, int value, void* data); |
| 97 | |
| 98 | int ev_init(ev_callback input_cb, void* data); |
| 99 | int ev_add_fd(int fd, ev_callback cb, void* data); |
| 100 | int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data); |
| 101 | #else |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 102 | using ev_callback = std::function<int(int fd, uint32_t epevents)>; |
| 103 | using ev_set_key_callback = std::function<int(int code, int value)>; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 104 | |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 105 | #ifdef TW_USE_MINUI_WITH_OPTIONAL_TOUCH_EVENTS |
Tao Bao | af9f8b4 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 106 | int ev_init(ev_callback input_cb, bool allow_touch_inputs = false); |
Tao Bao | af9f8b4 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 107 | void ev_iterate_touch_inputs(const std::function<void(int)>& action); |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 108 | #else |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 109 | int ev_init(ev_callback input_cb); |
Ethan Yonker | ecbd3e8 | 2017-12-14 14:43:59 -0600 | [diff] [blame] | 110 | #endif |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 111 | int ev_add_fd(int fd, ev_callback cb); |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 112 | int ev_sync_key_state(const ev_set_key_callback& set_key_cb); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 113 | #endif |
| 114 | void ev_exit(); |
| 115 | void ev_iterate_available_keys(const std::function<void(int)>& f); |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 116 | |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 117 | // 'timeout' has the same semantics as poll(2). |
| 118 | // 0 : don't block |
| 119 | // < 0 : block forever |
| 120 | // > 0 : block for 'timeout' milliseconds |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 121 | int ev_wait(int timeout); |
| 122 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 123 | int ev_get_input(int fd, uint32_t epevents, input_event* ev); |
| 124 | void ev_dispatch(); |
| 125 | int ev_get_epollfd(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 126 | |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 127 | // |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 128 | // Resources |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 129 | // |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 130 | |
Tianjie Xu | 0a59956 | 2017-03-28 20:11:15 +0000 | [diff] [blame] | 131 | bool matches_locale(const std::string& prefix, const std::string& locale); |
Tianjie Xu | 2430e29 | 2016-04-19 15:02:41 -0700 | [diff] [blame] | 132 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 133 | // res_create_*_surface() functions return 0 if no error, else |
| 134 | // negative. |
| 135 | // |
| 136 | // A "display" surface is one that is intended to be drawn to the |
| 137 | // screen with gr_blit(). An "alpha" surface is a grayscale image |
| 138 | // interpreted as an alpha mask used to render text in the current |
| 139 | // color (with gr_text() or gr_texticon()). |
| 140 | // |
| 141 | // All these functions load PNG images from "/res/images/${name}.png". |
| 142 | |
| 143 | // Load a single display surface from a PNG image. |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 144 | int res_create_display_surface(const char* name, GRSurface** pSurface); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 145 | |
| 146 | // Load an array of display surfaces from a single PNG image. The PNG |
| 147 | // should have a 'Frames' text chunk whose value is the number of |
| 148 | // frames this image represents. The pixel data itself is interlaced |
| 149 | // by row. |
Tao Bao | b723f4f | 2015-12-11 15:18:51 -0800 | [diff] [blame] | 150 | int res_create_multi_display_surface(const char* name, int* frames, |
| 151 | int* fps, GRSurface*** pSurface); |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 152 | int res_create_multi_display_surface(const char* name, int* frames, |
| 153 | GRSurface*** pSurface); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 154 | |
| 155 | // Load a single alpha surface from a grayscale PNG image. |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 156 | int res_create_alpha_surface(const char* name, GRSurface** pSurface); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 157 | |
| 158 | // Load part of a grayscale PNG image that is the first match for the |
| 159 | // given locale. The image is expected to be a composite of multiple |
| 160 | // translations of the same text, with special added rows that encode |
| 161 | // the subimages' size and intended locale in the pixel data. See |
Tianjie Xu | 9a25977 | 2016-07-28 14:15:00 -0700 | [diff] [blame] | 162 | // bootable/recovery/tools/recovery_l10n for an app that will generate |
| 163 | // these specialized images from Android resources. |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 164 | int res_create_localized_alpha_surface(const char* name, const char* locale, |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 165 | GRSurface** pSurface); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 166 | |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 167 | // Return a list of locale strings embedded in |png_name|. Return a empty list in case of failure. |
| 168 | std::vector<std::string> get_locales_in_png(const std::string& png_name); |
| 169 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 170 | // Free a surface allocated by any of the res_create_*_surface() |
| 171 | // functions. |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 172 | void res_free_surface(GRSurface* surface); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 173 | |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 174 | #else //ifndef TW_USE_MINUI_21 |
| 175 | |
| 176 | // This the old minui21/minui.h for compatibility with building TWRP |
| 177 | // in pre 6.0 trees. |
| 178 | |
| 179 | #include <stdbool.h> |
| 180 | |
| 181 | #ifdef __cplusplus |
| 182 | extern "C" { |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 183 | #endif |
Ethan Yonker | 8373cfe | 2017-09-08 06:50:54 -0500 | [diff] [blame] | 184 | |
| 185 | typedef void* gr_surface; |
| 186 | typedef unsigned short gr_pixel; |
| 187 | |
| 188 | int gr_init(void); |
| 189 | void gr_exit(void); |
| 190 | |
| 191 | int gr_fb_width(void); |
| 192 | int gr_fb_height(void); |
| 193 | gr_pixel *gr_fb_data(void); |
| 194 | void gr_flip(void); |
| 195 | void gr_fb_blank(bool blank); |
| 196 | |
| 197 | void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a); |
| 198 | void gr_fill(int x1, int y1, int x2, int y2); |
| 199 | |
| 200 | // system/core/charger uses different gr_print signatures in diferent |
| 201 | // Android versions, either with or without int bold. |
| 202 | int gr_text(int x, int y, const char *s, ...); |
| 203 | int gr_text_impl(int x, int y, const char *s, int bold); |
| 204 | |
| 205 | void gr_texticon(int x, int y, gr_surface icon); |
| 206 | int gr_measure(const char *s); |
| 207 | void gr_font_size(int *x, int *y); |
| 208 | void gr_get_memory_surface(gr_surface); |
| 209 | |
| 210 | void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy); |
| 211 | unsigned int gr_get_width(gr_surface surface); |
| 212 | unsigned int gr_get_height(gr_surface surface); |
| 213 | |
| 214 | // input event structure, include <linux/input.h> for the definition. |
| 215 | // see http://www.mjmwired.net/kernel/Documentation/input/ for info. |
| 216 | struct input_event; |
| 217 | |
| 218 | typedef int (*ev_callback)(int fd, uint32_t epevents, void *data); |
| 219 | typedef int (*ev_set_key_callback)(int code, int value, void *data); |
| 220 | |
| 221 | int ev_init(ev_callback input_cb, void *data); |
| 222 | void ev_exit(void); |
| 223 | int ev_add_fd(int fd, ev_callback cb, void *data); |
| 224 | int ev_sync_key_state(ev_set_key_callback set_key_cb, void *data); |
| 225 | |
| 226 | /* timeout has the same semantics as for poll |
| 227 | * 0 : don't block |
| 228 | * < 0 : block forever |
| 229 | * > 0 : block for 'timeout' milliseconds |
| 230 | */ |
| 231 | int ev_wait(int timeout); |
| 232 | |
| 233 | int ev_get_input(int fd, uint32_t epevents, struct input_event *ev); |
| 234 | void ev_dispatch(void); |
| 235 | int ev_get_epollfd(void); |
| 236 | |
| 237 | // Resources |
| 238 | |
| 239 | // Returns 0 if no error, else negative. |
| 240 | int res_create_surface(const char* name, gr_surface* pSurface); |
| 241 | |
| 242 | // Load an array of display surfaces from a single PNG image. The PNG |
| 243 | // should have a 'Frames' text chunk whose value is the number of |
| 244 | // frames this image represents. The pixel data itself is interlaced |
| 245 | // by row. |
| 246 | int res_create_multi_display_surface(const char* name, |
| 247 | int* frames, gr_surface** pSurface); |
| 248 | |
| 249 | int res_create_localized_surface(const char* name, gr_surface* pSurface); |
| 250 | void res_free_surface(gr_surface surface); |
| 251 | static inline int res_create_display_surface(const char* name, gr_surface* pSurface) { |
| 252 | return res_create_surface(name, pSurface); |
| 253 | } |
| 254 | |
| 255 | // These are new graphics functions from 5.0 that were not available in |
| 256 | // 4.4 that are required by charger and healthd |
| 257 | void gr_clear(); |
| 258 | |
| 259 | |
| 260 | #ifdef __cplusplus |
| 261 | } |
| 262 | #endif |
| 263 | |
| 264 | #endif // ifndef TW_USE_MINUI_21 |
| 265 | #endif // ifndef _MINUI_H_ |