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 | |
Doug Zongker | 830b3e3 | 2014-03-11 13:22:04 -0700 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 22 | #include <functional> |
Tianjie Xu | 2078b22 | 2017-03-22 12:27:26 -0700 | [diff] [blame] | 23 | #include <string> |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 24 | #include <vector> |
Doug Zongker | 28ce47c | 2011-10-28 10:33:05 -0700 | [diff] [blame] | 25 | |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 26 | // |
| 27 | // Graphics. |
| 28 | // |
| 29 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 30 | struct GRSurface { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 31 | int width; |
| 32 | int height; |
| 33 | int row_bytes; |
| 34 | int pixel_bytes; |
| 35 | unsigned char* data; |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 36 | }; |
Doug Zongker | 39cf417 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 37 | |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 38 | struct GRFont { |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 39 | GRSurface* texture; |
| 40 | int char_width; |
| 41 | int char_height; |
| 42 | }; |
| 43 | |
| 44 | enum GRRotation { |
| 45 | ROTATION_NONE = 0, |
| 46 | ROTATION_RIGHT = 1, |
| 47 | ROTATION_DOWN = 2, |
| 48 | ROTATION_LEFT = 3, |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 51 | int gr_init(); |
| 52 | void gr_exit(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 53 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 54 | int gr_fb_width(); |
| 55 | int gr_fb_height(); |
Doug Zongker | 39cf417 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 56 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 57 | void gr_flip(); |
Dima Zavin | 4daf48a | 2011-08-30 11:59:20 -0700 | [diff] [blame] | 58 | void gr_fb_blank(bool blank); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 59 | |
Doug Zongker | 39cf417 | 2014-03-06 16:16:05 -0800 | [diff] [blame] | 60 | void gr_clear(); // clear entire surface to current color |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 61 | 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] | 62 | void gr_fill(int x1, int y1, int x2, int y2); |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 63 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 64 | void gr_texticon(int x, int y, GRSurface* icon); |
Damien Bargiacchi | 35fff61 | 2016-08-11 15:57:03 -0700 | [diff] [blame] | 65 | |
| 66 | const GRFont* gr_sys_font(); |
Damien Bargiacchi | d00f5eb | 2016-09-09 07:14:08 -0700 | [diff] [blame] | 67 | int gr_init_font(const char* name, GRFont** dest); |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 68 | void gr_text(const GRFont* font, int x, int y, const char* s, bool bold); |
| 69 | int gr_measure(const GRFont* font, const char* s); |
| 70 | void gr_font_size(const GRFont* font, int* x, int* y); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 71 | |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 72 | void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy); |
| 73 | unsigned int gr_get_width(GRSurface* surface); |
| 74 | unsigned int gr_get_height(GRSurface* surface); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 75 | |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 76 | // Set rotation, flips gr_fb_width/height if 90 degree rotation difference |
| 77 | void gr_rotate(GRRotation rotation); |
| 78 | |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 79 | // |
| 80 | // Input events. |
| 81 | // |
| 82 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 83 | struct input_event; |
| 84 | |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 85 | using ev_callback = std::function<int(int fd, uint32_t epevents)>; |
| 86 | using ev_set_key_callback = std::function<int(int code, int value)>; |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 87 | |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 88 | int ev_init(ev_callback input_cb, bool allow_touch_inputs = false); |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 89 | void ev_exit(); |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 90 | int ev_add_fd(int fd, ev_callback cb); |
Chih-Hung Hsieh | 23abfd3 | 2016-07-27 10:19:47 -0700 | [diff] [blame] | 91 | void ev_iterate_available_keys(const std::function<void(int)>& f); |
Tao Bao | 5f8dd99 | 2017-07-28 00:05:40 -0700 | [diff] [blame] | 92 | void ev_iterate_touch_inputs(const std::function<void(int)>& action); |
Tao Bao | 0b1118d | 2017-01-14 07:46:10 -0800 | [diff] [blame] | 93 | int ev_sync_key_state(const ev_set_key_callback& set_key_cb); |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 94 | |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 95 | // 'timeout' has the same semantics as poll(2). |
| 96 | // 0 : don't block |
| 97 | // < 0 : block forever |
| 98 | // > 0 : block for 'timeout' milliseconds |
Dima Zavin | bc29063 | 2011-08-30 11:59:45 -0700 | [diff] [blame] | 99 | int ev_wait(int timeout); |
| 100 | |
Elliott Hughes | 07cfb8f | 2015-04-10 13:12:05 -0700 | [diff] [blame] | 101 | int ev_get_input(int fd, uint32_t epevents, input_event* ev); |
| 102 | void ev_dispatch(); |
| 103 | int ev_get_epollfd(); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 104 | |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 105 | // |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 106 | // Resources |
Elliott Hughes | 0713819 | 2015-04-10 09:40:53 -0700 | [diff] [blame] | 107 | // |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 108 | |
Tianjie Xu | 2078b22 | 2017-03-22 12:27:26 -0700 | [diff] [blame] | 109 | bool matches_locale(const std::string& prefix, const std::string& locale); |
Tianjie Xu | 2430e29 | 2016-04-19 15:02:41 -0700 | [diff] [blame] | 110 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 111 | // res_create_*_surface() functions return 0 if no error, else |
| 112 | // negative. |
| 113 | // |
| 114 | // A "display" surface is one that is intended to be drawn to the |
| 115 | // screen with gr_blit(). An "alpha" surface is a grayscale image |
| 116 | // interpreted as an alpha mask used to render text in the current |
| 117 | // color (with gr_text() or gr_texticon()). |
| 118 | // |
| 119 | // All these functions load PNG images from "/res/images/${name}.png". |
| 120 | |
| 121 | // Load a single display surface from a PNG image. |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 122 | int res_create_display_surface(const char* name, GRSurface** pSurface); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 123 | |
| 124 | // Load an array of display surfaces from a single PNG image. The PNG |
| 125 | // should have a 'Frames' text chunk whose value is the number of |
| 126 | // frames this image represents. The pixel data itself is interlaced |
| 127 | // by row. |
Luke Song | 846012f | 2017-09-13 15:56:16 -0700 | [diff] [blame] | 128 | int res_create_multi_display_surface(const char* name, int* frames, int* fps, |
| 129 | GRSurface*** pSurface); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 130 | |
| 131 | // Load a single alpha surface from a grayscale PNG image. |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 132 | int res_create_alpha_surface(const char* name, GRSurface** pSurface); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 133 | |
| 134 | // Load part of a grayscale PNG image that is the first match for the |
| 135 | // given locale. The image is expected to be a composite of multiple |
| 136 | // translations of the same text, with special added rows that encode |
| 137 | // the subimages' size and intended locale in the pixel data. See |
Tianjie Xu | 9a25977 | 2016-07-28 14:15:00 -0700 | [diff] [blame] | 138 | // bootable/recovery/tools/recovery_l10n for an app that will generate |
| 139 | // these specialized images from Android resources. |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 140 | int res_create_localized_alpha_surface(const char* name, const char* locale, |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 141 | GRSurface** pSurface); |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 142 | |
Tianjie Xu | 29d5575 | 2017-09-20 17:53:46 -0700 | [diff] [blame] | 143 | // Return a list of locale strings embedded in |png_name|. Return a empty list in case of failure. |
| 144 | std::vector<std::string> get_locales_in_png(const std::string& png_name); |
| 145 | |
Doug Zongker | a418aa7 | 2014-03-17 12:10:02 -0700 | [diff] [blame] | 146 | // Free a surface allocated by any of the res_create_*_surface() |
| 147 | // functions. |
Elliott Hughes | 0a5cb0c | 2015-04-15 10:58:56 -0700 | [diff] [blame] | 148 | void res_free_surface(GRSurface* surface); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 149 | |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 150 | #endif |