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 | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 29 | : Conditional(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 | xml_attribute<>* attr; |
| 32 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 33 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 34 | mFont = NULL; |
| 35 | mIsStatic = 1; |
| 36 | mVarChanged = 0; |
| 37 | mFontHeight = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 38 | maxWidth = 0; |
| 39 | charSkip = 0; |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 40 | isHighlighted = false; |
| 41 | hasHighlightColor = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 43 | if (!node) |
| 44 | return; |
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 | // Initialize color to solid black |
| 47 | memset(&mColor, 0, sizeof(COLOR)); |
| 48 | mColor.alpha = 255; |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 49 | memset(&mHighlightColor, 0, sizeof(COLOR)); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 50 | mHighlightColor.alpha = 255; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 51 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 52 | attr = node->first_attribute("color"); |
| 53 | if (attr) |
| 54 | { |
| 55 | std::string color = attr->value(); |
| 56 | ConvertStrToColor(color, &mColor); |
| 57 | } |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 58 | attr = node->first_attribute("highlightcolor"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 59 | if (attr) |
| 60 | { |
| 61 | std::string color = attr->value(); |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 62 | ConvertStrToColor(color, &mHighlightColor); |
| 63 | hasHighlightColor = true; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 64 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 65 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 66 | // Load the font, and possibly override the color |
| 67 | child = node->first_node("font"); |
| 68 | if (child) |
| 69 | { |
| 70 | attr = child->first_attribute("resource"); |
| 71 | if (attr) |
| 72 | mFont = PageManager::FindResource(attr->value()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 73 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 74 | attr = child->first_attribute("color"); |
| 75 | if (attr) |
| 76 | { |
| 77 | std::string color = attr->value(); |
| 78 | ConvertStrToColor(color, &mColor); |
| 79 | } |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 80 | |
| 81 | attr = child->first_attribute("highlightcolor"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 82 | if (attr) |
| 83 | { |
| 84 | std::string color = attr->value(); |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 85 | ConvertStrToColor(color, &mHighlightColor); |
| 86 | hasHighlightColor = true; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 87 | } |
| 88 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 89 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 90 | // Load the placement |
| 91 | LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &mPlacement); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 92 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 93 | child = node->first_node("text"); |
| 94 | if (child) mText = child->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 95 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 96 | // Simple way to check for static state |
| 97 | mLastValue = parseText(); |
| 98 | if (mLastValue != mText) mIsStatic = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 99 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 100 | gr_getFontDetails(mFont ? mFont->GetResource() : NULL, (unsigned*) &mFontHeight, NULL); |
| 101 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | int GUIText::Render(void) |
| 105 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 106 | if (!isConditionTrue()) |
| 107 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 108 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 109 | void* fontResource = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 110 | string displayValue; |
| 111 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 112 | if (mFont) |
| 113 | fontResource = mFont->GetResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 114 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 115 | mLastValue = parseText(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 116 | displayValue = mLastValue; |
| 117 | |
| 118 | if (charSkip) |
| 119 | displayValue.erase(0, charSkip); |
| 120 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 121 | mVarChanged = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 122 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 123 | int x = mRenderX, y = mRenderY; |
| 124 | int width = gr_measureEx(displayValue.c_str(), fontResource); |
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 (mPlacement != TOP_LEFT && mPlacement != BOTTOM_LEFT) |
| 127 | { |
| 128 | if (mPlacement == CENTER || mPlacement == CENTER_X_ONLY) |
| 129 | x -= (width / 2); |
| 130 | else |
| 131 | x -= width; |
| 132 | } |
| 133 | if (mPlacement != TOP_LEFT && mPlacement != TOP_RIGHT) |
| 134 | { |
| 135 | if (mPlacement == CENTER) |
| 136 | y -= (mFontHeight / 2); |
| 137 | else if (mPlacement == BOTTOM_LEFT || mPlacement == BOTTOM_RIGHT) |
| 138 | y -= mFontHeight; |
| 139 | } |
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 (hasHighlightColor && isHighlighted) |
Dees_Troy | 4d12f96 | 2012-10-19 13:13:15 -0400 | [diff] [blame] | 142 | gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha); |
| 143 | else |
| 144 | gr_color(mColor.red, mColor.green, mColor.blue, mColor.alpha); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 145 | |
| 146 | if (maxWidth) |
| 147 | gr_textExW(x, y, displayValue.c_str(), fontResource, maxWidth + x); |
| 148 | else |
| 149 | gr_textEx(x, y, displayValue.c_str(), fontResource); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 150 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | int GUIText::Update(void) |
| 154 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 155 | if (!isConditionTrue()) |
| 156 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 157 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 158 | static int updateCounter = 3; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 159 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 160 | // This hack just makes sure we update at least once a minute for things like clock and battery |
| 161 | if (updateCounter) updateCounter--; |
| 162 | else |
| 163 | { |
| 164 | mVarChanged = 1; |
| 165 | updateCounter = 3; |
| 166 | } |
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 (mIsStatic || !mVarChanged) |
| 169 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 170 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 171 | std::string newValue = parseText(); |
| 172 | if (mLastValue == newValue) |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 173 | return 0; |
| 174 | else |
| 175 | mLastValue = newValue; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 176 | return 2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | int GUIText::GetCurrentBounds(int& w, int& h) |
| 180 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 181 | void* fontResource = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 182 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 183 | if (mFont) |
| 184 | fontResource = mFont->GetResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 185 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 186 | h = mFontHeight; |
Dees_Troy | a13d74f | 2013-03-24 08:54:55 -0500 | [diff] [blame] | 187 | mLastValue = parseText(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 188 | w = gr_measureEx(mLastValue.c_str(), fontResource); |
| 189 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | std::string GUIText::parseText(void) |
| 193 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 194 | static int counter = 0; |
| 195 | std::string str = mText; |
| 196 | size_t pos = 0; |
| 197 | size_t next = 0, end = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 198 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 199 | while (1) |
| 200 | { |
| 201 | next = str.find('%', pos); |
| 202 | if (next == std::string::npos) return str; |
| 203 | end = str.find('%', next + 1); |
| 204 | if (end == std::string::npos) return str; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 205 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 206 | // We have a block of data |
| 207 | std::string var = str.substr(next + 1, (end - next) - 1); |
| 208 | str.erase(next, (end - next) + 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 209 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 210 | if (next + 1 == end) |
| 211 | { |
| 212 | str.insert(next, 1, '%'); |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | std::string value; |
| 217 | if (DataManager::GetValue(var, value) == 0) |
| 218 | str.insert(next, value); |
| 219 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 220 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 221 | pos = next + 1; |
| 222 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | int GUIText::NotifyVarChange(std::string varName, std::string value) |
| 226 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 227 | mVarChanged = 1; |
| 228 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | int GUIText::SetMaxWidth(unsigned width) |
| 232 | { |
| 233 | maxWidth = width; |
| 234 | mVarChanged = 1; |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | int GUIText::SkipCharCount(unsigned skip) |
| 239 | { |
| 240 | charSkip = skip; |
| 241 | mVarChanged = 1; |
| 242 | return 0; |
| 243 | } |