summaryrefslogtreecommitdiffstats
path: root/gui/console.cpp
blob: 6d53ed10119cb6280e478cce5b8a598d16293b48 (plain) (blame)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// console.cpp - GUIConsole object

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/reboot.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>

#include <string>

extern "C" {
#include "../common.h"
#include "../minuitwrp/minui.h"
#include "../recovery_ui.h"
}

#include "rapidxml.hpp"
#include "objects.hpp"


static std::vector<std::string> gConsole;

extern "C" void gui_print(const char *fmt, ...)
{
    char buf[512];          // We're going to limit a single request to 512 bytes

    va_list ap;
    va_start(ap, fmt);
    vsnprintf(buf, 512, fmt, ap);
    va_end(ap);

    char *start, *next;

	if (buf[0] == '\n' && strlen(buf) < 2) {
		// This prevents the double lines bug seen in the console during zip installs
		return;
	}

    for (start = next = buf; *next != '\0'; next++)
    {
        if (*next == '\n')
        {
            *next = '\0';
            next++;

            std::string line = start;
            gConsole.push_back(line);
            start = next;

			// Handle the normal \n\0 case
            if (*next == '\0')
				return;
        }
    }
    std::string line = start;
    gConsole.push_back(line);
    return;
}

extern "C" void gui_print_overwrite(const char *fmt, ...)
{
    char buf[512];          // We're going to limit a single request to 512 bytes

    va_list ap;
    va_start(ap, fmt);
    vsnprintf(buf, 512, fmt, ap);
    va_end(ap);

    // Pop the last line, and we can continue
    if (!gConsole.empty())   gConsole.pop_back();

    char *start, *next;
    for (start = next = buf; *next != '\0'; next++)
    {
        if (*next == '\n')
        {
            *next = '\0';
            next++;
            
            std::string line = start;
            gConsole.push_back(line);
            start = next;

            // Handle the normal \n\0 case
            if (*next == '\0')
				return;
        }
    }
    std::string line = start;
    gConsole.push_back(line);
    return;
}

GUIConsole::GUIConsole(xml_node<>* node)
{
    xml_attribute<>* attr;
    xml_node<>* child;

    mFont = NULL;
    mCurrentLine = -1;
    memset(&mForegroundColor, 255, sizeof(COLOR));
    memset(&mBackgroundColor, 0, sizeof(COLOR));
    mBackgroundColor.alpha = 255;
    memset(&mScrollColor, 0x08, sizeof(COLOR));
    mScrollColor.alpha = 255;
    mLastCount = 0;
    mSlideout = 0;
    mSlideoutState = hidden;

    mRenderX = 0; mRenderY = 0; mRenderW = gr_fb_width(); mRenderH = gr_fb_height();

    if (!node)
    {
        mSlideoutX = 0; mSlideoutY = 0; mSlideoutW = 0; mSlideoutH = 0;
        mConsoleX = 0;  mConsoleY = 0;  mConsoleW = gr_fb_width();  mConsoleH = gr_fb_height();
    }
    else
    {
        child = node->first_node("font");
        if (child)
        {
            attr = child->first_attribute("resource");
            if (attr)
                mFont = PageManager::FindResource(attr->value());
        }

        child = node->first_node("color");
        if (child)
        {
            attr = child->first_attribute("foreground");
            if (attr)
            {
                std::string color = attr->value();
                ConvertStrToColor(color, &mForegroundColor);
            }
            attr = child->first_attribute("background");
            if (attr)
            {
                std::string color = attr->value();
                ConvertStrToColor(color, &mBackgroundColor);
            }
            attr = child->first_attribute("scroll");
            if (attr)
            {
                std::string color = attr->value();
                ConvertStrToColor(color, &mScrollColor);
            }
        }

        // Load the placement
        LoadPlacement(node->first_node("placement"), &mConsoleX, &mConsoleY, &mConsoleW, &mConsoleH);

        child = node->first_node("slideout");
        if (child)
        {
            mSlideout = 1;
            LoadPlacement(child, &mSlideoutX, &mSlideoutY);

            attr = child->first_attribute("resource");
            if (attr)   mSlideoutImage = PageManager::FindResource(attr->value());

            if (mSlideoutImage && mSlideoutImage->GetResource())
            {
                mSlideoutW = gr_get_width(mSlideoutImage->GetResource());
                mSlideoutH = gr_get_height(mSlideoutImage->GetResource());
            }
        }
    }

    gr_getFontDetails(mFont, &mFontHeight, NULL);
    SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
    SetRenderPos(mConsoleX, mConsoleY);
    return;
}

int GUIConsole::RenderSlideout(void)
{
    if (!mSlideoutImage || !mSlideoutImage->GetResource())      return -1;

    gr_blit(mSlideoutImage->GetResource(), 0, 0, mSlideoutW, mSlideoutH, mSlideoutX, mSlideoutY);
    return 0;
}

int GUIConsole::RenderConsole(void)
{
    void* fontResource = NULL;
    if (mFont)  fontResource = mFont->GetResource();

    // We fill the background
    gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, 255);
    gr_fill(mConsoleX, mConsoleY, mConsoleW, mConsoleH);

    gr_color(mScrollColor.red, mScrollColor.green, mScrollColor.blue, mScrollColor.alpha);
    gr_fill(mConsoleX + (mConsoleW * 9 / 10), mConsoleY, (mConsoleW / 10), mConsoleH);

    // Render the lines
    gr_color(mForegroundColor.red, mForegroundColor.green, mForegroundColor.blue, mForegroundColor.alpha);

    // Don't try to continue to render without data
    mLastCount = gConsole.size();
    if (mLastCount == 0)        return (mSlideout ? RenderSlideout() : 0);

    // Find the start point
    int start;
    int curLine = mCurrentLine;    // Thread-safing (Another thread updates this value)
    if (curLine == -1)
    {
        start = mLastCount - mMaxRows;
    }
    else
    {
        if (curLine > (int) mLastCount)   curLine = (int) mLastCount;
        if ((int) mMaxRows > curLine)     curLine = (int) mMaxRows;
        start = curLine - mMaxRows;
    }

    unsigned int line;
    for (line = 0; line < mMaxRows; line++)
    {
        if ((start + (int) line) >= 0 && (start + (int) line) < (int) mLastCount)
        {
            gr_textExW(mConsoleX, mStartY + (line * mFontHeight), gConsole[start + line].c_str(), fontResource, mConsoleW + mConsoleX);
        }
    }
    return (mSlideout ? RenderSlideout() : 0);
}

int GUIConsole::Render(void)
{
    if (mSlideout && mSlideoutState == hidden)
    {
        return RenderSlideout();
    }
    return RenderConsole();
}

int GUIConsole::Update(void)
{
    if (mSlideout && mSlideoutState != visible)
    {
        if (mSlideoutState == hidden)
            return 0;

        if (mSlideoutState == request_hide)
            mSlideoutState = hidden;

        if (mSlideoutState == request_show)
            mSlideoutState = visible;

        // Any time we activate the slider, we reset the position
        mCurrentLine = -1;
        return 2;
    }

    if (mCurrentLine == -1 && mLastCount != gConsole.size())
    {
        // We can use Render, and return for just a flip
        Render();
        return 2;
    }
    else if (mLastTouchY >= 0)
    {
        // They're still touching, so re-render
        Render();
        return 2;
    }
    return 0;
}

int GUIConsole::SetRenderPos(int x, int y, int w, int h)
{
    // Adjust the stub position accordingly
    mSlideoutX += (x - mConsoleX);
    mSlideoutY += (y - mConsoleY);

    mConsoleX = x;
    mConsoleY = y;
    if (w || h)
    {
        mConsoleW = w;
        mConsoleH = h;
    }

    // Calculate the max rows
    mMaxRows = mConsoleH / mFontHeight;

    // Adjust so we always fit to bottom
    mStartY = mConsoleY + (mConsoleH % mFontHeight);
    return 0;
}

// IsInRegion - Checks if the request is handled by this object
//  Return 0 if this object handles the request, 1 if not
int GUIConsole::IsInRegion(int x, int y)
{
    if (mSlideout)
    {
        // Check if they tapped the slideout button
        if (x >= mSlideoutX && x <= mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH)
            return 1;

        // If we're only rendering the slideout, bail now
        if (mSlideoutState == hidden)
            return 0;
    }

    return (x < mConsoleX || x > mConsoleX + mConsoleW || y < mConsoleY || y > mConsoleY + mConsoleH) ? 0 : 1;
}

// NotifyTouch - Notify of a touch event
//  Return 0 on success, >0 to ignore remainder of touch, and <0 on error
int GUIConsole::NotifyTouch(TOUCH_STATE state, int x, int y)
{
    if (mSlideout && mSlideoutState == hidden)
    {
        if (state == TOUCH_START)
        {
            mSlideoutState = request_show;
            return 1;
        }
    }
    else if (mSlideout && mSlideoutState == visible)
    {
        // Are we sliding it back in?
        if (state == TOUCH_START && x > mSlideoutX && x < (mSlideoutX + mSlideoutW) && y > mSlideoutY && y < (mSlideoutY + mSlideoutH))
        {
            mSlideoutState = request_hide;
            return 1;
        }
    }

    // If we don't have enough lines to scroll, throw this away.
    if (mLastCount < mMaxRows)   return 1;

    // We are scrolling!!!
    switch (state)
    {
    case TOUCH_START:
        mLastTouchX = x;
        mLastTouchY = y;
        if ((x - mConsoleX) > ((9 * mConsoleW) / 10))
            mSlideMultiplier = 10;
        else
            mSlideMultiplier = 1;
        break;

    case TOUCH_DRAG:
        // This handles tapping
        if (x == mLastTouchX && y == mLastTouchY)   break;
        mLastTouchX = -1;

        if (y > mLastTouchY + 5)
        {
            mLastTouchY = y;
            if (mCurrentLine == -1)
                mCurrentLine = mLastCount - mMaxRows;
            else if (mCurrentLine > mSlideMultiplier)
                mCurrentLine -= mSlideMultiplier;
            else
                mCurrentLine = mMaxRows;

            if (mCurrentLine < (int) mMaxRows)
                mCurrentLine = mMaxRows;
        }
        else if (y < mLastTouchY - 5)
        {
            mLastTouchY = y;
            if (mCurrentLine >= 0)
            {
                mCurrentLine += mSlideMultiplier;
                if (mCurrentLine >= (int) mLastCount)
                    mCurrentLine = -1;
            }
        }
        break;

    case TOUCH_RELEASE:
        // On a tap, we jump to the tail
        if (mLastTouchX >= 0)
            mCurrentLine = -1;

        mLastTouchY = -1;
	case TOUCH_REPEAT:
	case TOUCH_HOLD:
        break;
    }
    return 0;
}