Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // text.cpp - GUIText object |
| 2 | |
| 3 | #include <stdarg.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <sys/reboot.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <sys/time.h> |
| 11 | #include <sys/mman.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <sys/ioctl.h> |
| 14 | #include <time.h> |
| 15 | #include <unistd.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include <string> |
| 19 | |
| 20 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 21 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 22 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | #include "rapidxml.hpp" |
| 26 | #include "objects.hpp" |
| 27 | |
| 28 | GUIText::GUIText(xml_node<>* node) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 29 | : GUIObject(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 30 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 31 | mFont = NULL; |
| 32 | mIsStatic = 1; |
| 33 | mVarChanged = 0; |
| 34 | mFontHeight = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 35 | maxWidth = 0; |
| 36 | charSkip = 0; |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 37 | isHighlighted = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 38 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 39 | if (!node) |
| 40 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 41 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 42 | // Load colors |
| 43 | mColor = LoadAttrColor(node, "color", COLOR(0,0,0,255)); |
| 44 | mHighlightColor = LoadAttrColor(node, "highlightcolor", mColor); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 45 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 46 | // Load the font, and possibly override the color |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 47 | mFont = LoadAttrFont(FindNode(node, "font"), "resource"); |
| 48 | mColor = LoadAttrColor(FindNode(node, "font"), "color", mColor); |
| 49 | mHighlightColor = LoadAttrColor(FindNode(node, "font"), "highlightcolor", mColor); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 50 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 51 | // Load the placement |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 52 | LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &mPlacement); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 53 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 54 | xml_node<>* child = FindNode(node, "text"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 55 | if (child) mText = child->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 56 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 57 | // Simple way to check for static state |
| 58 | mLastValue = parseText(); |
| 59 | if (mLastValue != mText) mIsStatic = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 60 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 61 | mFontHeight = mFont->GetHeight(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | int GUIText::Render(void) |
| 65 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 66 | if (!isConditionTrue()) |
| 67 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 68 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 69 | void* fontResource = NULL; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 70 | if (mFont) |
| 71 | fontResource = mFont->GetResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 72 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 73 | mLastValue = parseText(); |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 74 | string displayValue = mLastValue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 75 | |
| 76 | if (charSkip) |
| 77 | displayValue.erase(0, charSkip); |
| 78 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 79 | mVarChanged = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 80 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 81 | int x = mRenderX, y = mRenderY; |
| 82 | int width = gr_measureEx(displayValue.c_str(), fontResource); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 83 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 84 | if (mPlacement != TOP_LEFT && mPlacement != BOTTOM_LEFT) |
| 85 | { |
| 86 | if (mPlacement == CENTER || mPlacement == CENTER_X_ONLY) |
| 87 | x -= (width / 2); |
| 88 | else |
| 89 | x -= width; |
| 90 | } |
| 91 | if (mPlacement != TOP_LEFT && mPlacement != TOP_RIGHT) |
| 92 | { |
| 93 | if (mPlacement == CENTER) |
| 94 | y -= (mFontHeight / 2); |
| 95 | else if (mPlacement == BOTTOM_LEFT || mPlacement == BOTTOM_RIGHT) |
| 96 | y -= mFontHeight; |
| 97 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 98 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 99 | if (isHighlighted) |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 100 | gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha); |
| 101 | else |
| 102 | gr_color(mColor.red, mColor.green, mColor.blue, mColor.alpha); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 103 | |
| 104 | if (maxWidth) |
| 105 | gr_textExW(x, y, displayValue.c_str(), fontResource, maxWidth + x); |
| 106 | else |
| 107 | gr_textEx(x, y, displayValue.c_str(), fontResource); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 108 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | int GUIText::Update(void) |
| 112 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 113 | if (!isConditionTrue()) |
| 114 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 115 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 116 | static int updateCounter = 3; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 117 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 118 | // This hack just makes sure we update at least once a minute for things like clock and battery |
| 119 | if (updateCounter) updateCounter--; |
| 120 | else |
| 121 | { |
| 122 | mVarChanged = 1; |
| 123 | updateCounter = 3; |
| 124 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 125 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 126 | if (mIsStatic || !mVarChanged) |
| 127 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 128 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 129 | std::string newValue = parseText(); |
| 130 | if (mLastValue == newValue) |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 131 | return 0; |
| 132 | else |
| 133 | mLastValue = newValue; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 134 | return 2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | int GUIText::GetCurrentBounds(int& w, int& h) |
| 138 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 139 | void* fontResource = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 140 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 141 | if (mFont) |
| 142 | fontResource = mFont->GetResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 143 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 144 | h = mFontHeight; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 145 | mLastValue = parseText(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 146 | w = gr_measureEx(mLastValue.c_str(), fontResource); |
| 147 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | std::string GUIText::parseText(void) |
| 151 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 152 | static int counter = 0; |
| 153 | std::string str = mText; |
| 154 | size_t pos = 0; |
| 155 | size_t next = 0, end = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 156 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 157 | while (1) |
| 158 | { |
| 159 | next = str.find('%', pos); |
| 160 | if (next == std::string::npos) return str; |
| 161 | end = str.find('%', next + 1); |
| 162 | if (end == std::string::npos) return str; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 163 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 164 | // We have a block of data |
| 165 | std::string var = str.substr(next + 1, (end - next) - 1); |
| 166 | str.erase(next, (end - next) + 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 167 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 168 | if (next + 1 == end) |
| 169 | { |
| 170 | str.insert(next, 1, '%'); |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | std::string value; |
| 175 | if (DataManager::GetValue(var, value) == 0) |
| 176 | str.insert(next, value); |
| 177 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 178 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 179 | pos = next + 1; |
| 180 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 183 | int GUIText::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 184 | { |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 185 | GUIObject::NotifyVarChange(varName, value); |
| 186 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 187 | mVarChanged = 1; |
| 188 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | int GUIText::SetMaxWidth(unsigned width) |
| 192 | { |
| 193 | maxWidth = width; |
| 194 | mVarChanged = 1; |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | int GUIText::SkipCharCount(unsigned skip) |
| 199 | { |
| 200 | charSkip = skip; |
| 201 | mVarChanged = 1; |
| 202 | return 0; |
| 203 | } |