summaryrefslogtreecommitdiff
path: root/recovery/recovery_ui.cpp
blob: 78625def6beb9b4f54c2d90f9121cb71b369ea84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <fcntl.h>
#include <linux/fb.h>
#include <linux/input.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <linux/ioctl.h>

#include "common.h"
#include "device.h"
#include "ui.h"
#include "screen_ui.h"

/* CEA-861 specifies a maximum of 65 modes in an EDID */
#define CEA_MODEDB_SIZE 65
static const char* HEADERS[] = { "Use hardware button to move cursor; long-press to select item.",
                                 "",
                                 NULL };

// these strings are never actually displayed
static const char* ITEMS[] =  {"reboot system now",
                               "apply update from ADB",
                               "wipe data/factory reset",
                               "wipe cache partition",
                               NULL };

#define kFBDevice "/dev/graphics/fb0"

struct led_rgb_vals {
        uint8_t rgb[3];
};

// Return the current time as a double (including fractions of a second).
static double now() {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec + tv.tv_usec / 1000000.0;
}

class FuguUI : public ScreenRecoveryUI {
public:
    FuguUI() :
        text_visible(false),
        text_ever_visible(false),
        background_mode(NONE),
        up_keys(0),
        next_key_pos(0),
        pending_select(false),
        long_press(false) {
        pthread_mutex_init(&long_mu, NULL);
        memset(last_keys, 0, kKeyBufferSize * sizeof(int));
    }

    void SetBackground(Icon icon) {
        ScreenRecoveryUI::SetBackground(icon);

        background_mode = icon;
    }

    void SetColor(UIElement e) {
        switch (e) {
            case HEADER:
                gr_color(247, 0, 6, 255);
                break;
            case MENU:
                gr_color(0, 106, 157, 255);
                break;
            case MENU_SEL_BG:
                pthread_mutex_lock(&long_mu);
                if (pending_select) {
                    gr_color(0, 156, 100, 255);
                } else {
                    gr_color(0, 106, 157, 255);
                }
                pthread_mutex_unlock(&long_mu);
                break;
            case MENU_SEL_FG:
                gr_color(255, 255, 255, 255);
                break;
            case LOG:
                gr_color(249, 194, 0, 255);
                break;
            case TEXT_FILL:
                gr_color(0, 0, 0, 160);
                break;
            default:
                gr_color(255, 255, 255, 255);
                break;
        }
    }

    void ShowText(bool visible) {
        ScreenRecoveryUI::ShowText(visible);

        text_ever_visible = text_ever_visible || visible;
        text_visible = visible;
    }

    bool IsTextVisible() {
        return text_visible;
    }

    bool WasTextEverVisible() {
        return text_ever_visible;
    }

    void Print(const char* fmt, ...) {
        char buf[256];
        va_list ap;
        va_start(ap, fmt);
        vsnprintf(buf, 256, fmt, ap);
        va_end(ap);
        ScreenRecoveryUI::Print("%s", buf);
    }

    void StartMenu(const char* const * headers, const char* const * items,
                   int initial_selection) {
        ScreenRecoveryUI::StartMenu(headers, items, initial_selection);

        menu_items = 0;
        for (const char* const * p = items; *p; ++p) {
            ++menu_items;
        }
    }

    int SelectMenu(int sel) {
        if (sel < 0) {
            sel += menu_items;
        }
        sel %= menu_items;
        ScreenRecoveryUI::SelectMenu(sel);
        return sel;
    }

    void NextCheckKeyIsLong(bool is_long_press) {
        long_press = is_long_press;
    }

    void KeyLongPress(int key) {
        pthread_mutex_lock(&long_mu);
        pending_select = true;
        pthread_mutex_unlock(&long_mu);

        Redraw();
    }

    KeyAction CheckKey(int key) {
        pthread_mutex_lock(&long_mu);
        pending_select = false;
        pthread_mutex_unlock(&long_mu);

        if (key == KEY_F1) {
            return MOUNT_SYSTEM;
        }

        if (long_press) {
            if (text_visible) {
                EnqueueKey(KEY_ENTER);
                return IGNORE;
            } else {
                return TOGGLE;
            }
        } else {
            return text_visible ? ENQUEUE : IGNORE;
        }
    }

private:
    static const int kKeyBufferSize = 100;

    int text_visible;
    int text_ever_visible;

    Icon background_mode;

    int up_keys;
    int next_key_pos;
    int last_keys[kKeyBufferSize];

    int menu_items;

    pthread_mutex_t long_mu;
    bool pending_select;

    bool long_press;
};

class FuguDevice : public Device {
  public:
    FuguDevice() :
        ui(new FuguUI) {
    }

    RecoveryUI* GetUI() { return ui; }

    int HandleMenuKey(int key, int visible) {
        static int running = 0;

        if (visible) {
            switch (key) {
                case KEY_ENTER:
                    return kInvokeItem;
                    break;

                case KEY_UP:
                    return kHighlightUp;
                    break;

                case KEY_DOWN:
                case KEY_CONNECT:   // the Fugu hardware button
                    return kHighlightDown;
                    break;
            }
        }

        return kNoAction;
    }

    BuiltinAction InvokeMenuItem(int menu_position) {
        switch (menu_position) {
          case 0: return REBOOT;
          case 1: return APPLY_ADB_SIDELOAD;
          case 2: return WIPE_DATA;
          case 3: return WIPE_CACHE;
          default: return NO_ACTION;
        }
    }

    const char* const* GetMenuHeaders() { return HEADERS; }
    const char* const* GetMenuItems() { return ITEMS; }

  private:
    RecoveryUI* ui;
};

Device* make_device() {
    return new FuguDevice();
}