| 1 |
// Copyright © 2016 Canonical Ltd. |
| 2 |
// Author: Loïc Molinari <[email protected]> |
| 3 |
// |
| 4 |
// This file is part of Lomiri UI Toolkit. |
| 5 |
// |
| 6 |
// Lomiri UI Toolkit is free software: you can redistribute it and/or modify it |
| 7 |
// under the terms of the GNU Lesser General Public License as published by the |
| 8 |
// Free Software Foundation; version 3. |
| 9 |
// |
| 10 |
// Lomiri UI Toolkit is distributed in the hope that it will be useful, but |
| 11 |
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 13 |
// for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU Lesser General Public License |
| 16 |
// along with Lomiri UI Toolkit. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
#include "overlay_p.h" |
| 19 |
|
| 20 |
#include <unistd.h> |
| 21 |
#include <fcntl.h> |
| 22 |
|
| 23 |
#include <QtCore/QSysInfo> |
| 24 |
#include <QtGui/QGuiApplication> |
| 25 |
|
| 26 |
#include "lomirimetricsglobal_p.h" |
| 27 |
|
| 28 |
static const QPointF position = QPointF(5.0f, 5.0f); |
| 29 |
static const float opacity = 0.85f; |
| 30 |
|
| 31 |
// Keep in sync with corresponding enum! |
| 32 |
static const struct { |
| 33 |
const char* const name; |
| 34 |
quint16 size; |
| 35 |
} keywordInfo[]= { |
| 36 |
{ "qtVersion", sizeof("qtVersion") - 1 }, |
| 37 |
{ "qtPlatform", sizeof("qtPlatform") - 1 }, |
| 38 |
{ "glVersion", sizeof("glVersion") - 1 }, |
| 39 |
{ "cpuModel", sizeof("cpuModel") - 1 }, |
| 40 |
{ "gpuModel", sizeof("gpuModel") - 1 } |
| 41 |
}; |
| 42 |
enum { |
| 43 |
QtVersion = 0, QtPlatform, GlVersion, CpuModel, GpuModel, KeywordCount |
| 44 |
}; |
| 45 |
Q_STATIC_ASSERT(ARRAY_SIZE(keywordInfo) == KeywordCount); |
| 46 |
|
| 47 |
// Keep in sync with corresponding enum! |
| 48 |
static const struct { |
| 49 |
const char* const name; |
| 50 |
quint16 size; |
| 51 |
quint16 defaultWidth; |
| 52 |
UMEvent::Type type; |
| 53 |
} metricInfo[] = { |
| 54 |
{ "cpuUsage", sizeof("cpuUsage") - 1, 3, UMEvent::Process }, |
| 55 |
{ "threadCount", sizeof("threadCount") - 1, 3, UMEvent::Process }, |
| 56 |
{ "vszMemory", sizeof("vszMemory") - 1, 8, UMEvent::Process }, |
| 57 |
{ "rssMemory", sizeof("rssMemory") - 1, 8, UMEvent::Process }, |
| 58 |
{ "windowId", sizeof("windowId") - 1, 2, UMEvent::Window }, |
| 59 |
{ "windowSize", sizeof("windowSize") - 1, 9, UMEvent::Window }, |
| 60 |
{ "frameNumber", sizeof("frameNumber") - 1, 7, UMEvent::Frame }, |
| 61 |
{ "deltaTime", sizeof("deltaTime") - 1, 7, UMEvent::Frame }, |
| 62 |
{ "syncTime", sizeof("syncTime") - 1, 7, UMEvent::Frame }, |
| 63 |
{ "renderTime", sizeof("renderTime") - 1, 7, UMEvent::Frame }, |
| 64 |
{ "gpuTime", sizeof("gpuTime") - 1, 7, UMEvent::Frame }, |
| 65 |
{ "totalTime", sizeof("totalTime") - 1, 7, UMEvent::Frame } |
| 66 |
}; |
| 67 |
enum { |
| 68 |
CpuUsage = 0, ThreadCount, VszMemory, RssMemory, WindowId, WindowSize, FrameNumber, DeltaTime, |
| 69 |
SyncTime, RenderTime, GpuTime, TotalTime, MetricCount |
| 70 |
}; |
| 71 |
Q_STATIC_ASSERT(ARRAY_SIZE(metricInfo) == MetricCount); |
| 72 |
|
| 73 |
const int maxMetricWidth = 32; |
| 74 |
const int maxKeywordStringSize = 128; |
| 75 |
const int bufferSize = 128; |
| 76 |
Q_STATIC_ASSERT( |
| 77 |
bufferSize >= maxMetricWidth |
| 78 |
&& bufferSize >= maxKeywordStringSize); |
| 79 |
const int bufferAlignment = 64; |
| 80 |
|
| 81 |
const int maxParsedTextSize = 1024; // Including '\0'. |
| 82 |
|
| 83 |
static char cpuModelString[maxKeywordStringSize] = { 0 }; |
| 84 |
static int cpuModelStringSize = 0; |
| 85 |
|
| 86 |
Overlay::Overlay(const char* text, int windowId) |
| 87 |
: m_parsedText(new char [maxParsedTextSize]) |
| 88 |
#if !defined QT_NO_DEBUG |
| 89 |
, m_context(nullptr) |
| 90 |
#endif |
| 91 |
, m_text(QString::fromLatin1(text)) |
| 92 |
, m_metricsSize{} |
| 93 |
, m_frameSize(0, 0) |
| 94 |
, m_windowId(windowId) |
| 95 |
, m_flags(DirtyText | DirtyProcessEvent) |
| 96 |
{ |
| 97 |
DASSERT(text); |
| 98 |
|
| 99 |
m_buffer = alignedAlloc(bufferAlignment, bufferSize); |
| 100 |
memset(&m_processEvent, 0, sizeof(m_processEvent)); |
| 101 |
m_processEvent.type = UMEvent::Process; |
| 102 |
} |
| 103 |
|
| 104 |
Overlay::~Overlay() |
| 105 |
{ |
| 106 |
DASSERT(!(m_flags & Initialized)); |
| 107 |
|
| 108 |
free(m_buffer); |
| 109 |
delete [] m_parsedText; |
| 110 |
} |
| 111 |
|
| 112 |
bool Overlay::initialize() |
| 113 |
{ |
| 114 |
DASSERT(!(m_flags & Initialized)); |
| 115 |
DASSERT(QOpenGLContext::currentContext()); |
| 116 |
|
| 117 |
#if !defined QT_NO_DEBUG |
| 118 |
m_context = QOpenGLContext::currentContext(); |
| 119 |
#endif |
| 120 |
|
| 121 |
const bool initialized = m_bitmapText.initialize(); |
| 122 |
if (initialized) { |
| 123 |
m_bitmapText.bindProgram(); |
| 124 |
m_bitmapText.setOpacity(opacity); |
| 125 |
m_flags |= Initialized; |
| 126 |
return true; |
| 127 |
} else { |
| 128 |
return false; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
void Overlay::finalize() |
| 133 |
{ |
| 134 |
DASSERT(m_flags & Initialized); |
| 135 |
DASSERT(m_context == QOpenGLContext::currentContext()); |
| 136 |
|
| 137 |
m_bitmapText.finalize(); |
| 138 |
m_flags &= ~Initialized; |
| 139 |
|
| 140 |
#if !defined QT_NO_DEBUG |
| 141 |
m_context = nullptr; |
| 142 |
#endif |
| 143 |
} |
| 144 |
|
| 145 |
void Overlay::setProcessEvent(const UMEvent& processEvent) |
| 146 |
{ |
| 147 |
DASSERT(processEvent.type == UMEvent::Process); |
| 148 |
|
| 149 |
memcpy(&m_processEvent, &processEvent, sizeof(m_processEvent)); |
| 150 |
m_flags |= DirtyProcessEvent; |
| 151 |
} |
| 152 |
|
| 153 |
void Overlay::render(const UMEvent& frameEvent, const QSize& frameSize) |
| 154 |
{ |
| 155 |
DASSERT(m_flags & Initialized); |
| 156 |
DASSERT(m_context == QOpenGLContext::currentContext()); |
| 157 |
|
| 158 |
m_bitmapText.bindProgram(); |
| 159 |
if (m_flags & DirtyText) { |
| 160 |
parseText(); |
| 161 |
m_bitmapText.setText(m_parsedText); |
| 162 |
m_flags &= ~DirtyText; |
| 163 |
} |
| 164 |
if (m_frameSize != frameSize) { |
| 165 |
updateWindowMetrics(m_windowId, frameSize); |
| 166 |
m_bitmapText.setTransform(frameSize, position); |
| 167 |
m_frameSize = frameSize; |
| 168 |
} |
| 169 |
if (m_flags & DirtyProcessEvent) { |
| 170 |
updateProcessMetrics(); |
| 171 |
m_flags &= ~DirtyProcessEvent; |
| 172 |
} |
| 173 |
updateFrameMetrics(frameEvent); |
| 174 |
m_bitmapText.render(); |
| 175 |
} |
| 176 |
|
| 177 |
// Writes a 64-bit unsigned integer as text. The string is right |
| 178 |
// aligned. Returns the remaining width. |
| 179 |
static int integerMetricToText(quint64 metric, char* text, int width) |
| 180 |
{ |
| 181 |
DASSERT(text); |
| 182 |
DASSERT(width > 0); |
| 183 |
|
| 184 |
do { |
| 185 |
text[--width] = (metric % 10) + '0'; |
| 186 |
if (width == 0) return 0; |
| 187 |
metric /= 10; |
| 188 |
} while (metric != 0); |
| 189 |
|
| 190 |
return width; |
| 191 |
} |
| 192 |
|
| 193 |
// Writes a 64-bit unsigned integer representing time in nanoseconds as text in |
| 194 |
// milliseconds with two decimal digits. The string is right aligned. Returns |
| 195 |
// the remaining width. |
| 196 |
static int timeMetricToText(quint64 metric, char* text, int width) |
| 197 |
{ |
| 198 |
DASSERT(text); |
| 199 |
DASSERT(width > 0); |
| 200 |
|
| 201 |
metric /= 10000; // 10^−9 to 10^−5 (to keep 2 valid decimal digits). |
| 202 |
const int decimalCount = 2; |
| 203 |
const char decimalPoint = '.'; |
| 204 |
int i = 0; |
| 205 |
|
| 206 |
do { |
| 207 |
// Handle the decimal digits part. |
| 208 |
text[--width] = (metric % 10) + '0'; |
| 209 |
if (width == 0) return 0; |
| 210 |
metric /= 10; |
| 211 |
} while (++i < decimalCount && metric != 0); |
| 212 |
if (metric != 0) { |
| 213 |
// Handle the decimal point and integer parts. |
| 214 |
text[--width] = decimalPoint; |
| 215 |
if (width > 0) { |
| 216 |
do { |
| 217 |
text[--width] = (metric % 10) + '0'; |
| 218 |
metric /= 10; |
| 219 |
} while (metric != 0 && width > 0); |
| 220 |
} |
| 221 |
} else { |
| 222 |
// Handle metric ms value less than decimalCount digits. |
| 223 |
if (i == 1) { |
| 224 |
text[--width] = '0'; |
| 225 |
if (width == 0) return 0; |
| 226 |
} |
| 227 |
text[--width] = decimalPoint; |
| 228 |
if (width > 0) { |
| 229 |
text[--width] = '0'; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
return width; |
| 234 |
} |
| 235 |
|
| 236 |
void Overlay::updateFrameMetrics(const UMEvent& event) |
| 237 |
{ |
| 238 |
DASSERT(m_flags & Initialized); |
| 239 |
Q_STATIC_ASSERT(IS_POWER_OF_TWO(maxMetricWidth)); |
| 240 |
|
| 241 |
char* text = static_cast<char*>(m_buffer); |
| 242 |
for (int i = 0; i < m_metricsSize[UMEvent::Frame]; i++) { |
| 243 |
int textWidth = m_metrics[UMEvent::Frame][i].width; |
| 244 |
DASSERT(textWidth <= maxMetricWidth); |
| 245 |
memset(text, ' ', maxMetricWidth); |
| 246 |
|
| 247 |
switch (m_metrics[UMEvent::Frame][i].index) { |
| 248 |
case FrameNumber: |
| 249 |
integerMetricToText(event.frame.number, text, textWidth); |
| 250 |
break; |
| 251 |
case DeltaTime: |
| 252 |
timeMetricToText(event.frame.deltaTime, text, textWidth); |
| 253 |
break; |
| 254 |
case SyncTime: |
| 255 |
timeMetricToText(event.frame.syncTime, text, textWidth); |
| 256 |
break; |
| 257 |
case RenderTime: |
| 258 |
timeMetricToText(event.frame.renderTime, text, textWidth); |
| 259 |
break; |
| 260 |
case GpuTime: |
| 261 |
if (event.frame.gpuTime > 0) { |
| 262 |
timeMetricToText(event.frame.gpuTime, text, textWidth); |
| 263 |
} else { |
| 264 |
const char* const na = "N/A"; |
| 265 |
int naSize = sizeof("N/A") - 1; |
| 266 |
do { text[--textWidth] = na[--naSize]; } while (textWidth > 0 && naSize > 0); |
| 267 |
} |
| 268 |
break; |
| 269 |
case TotalTime: { |
| 270 |
const quint64 time = |
| 271 |
event.frame.syncTime + event.frame.renderTime + event.frame.gpuTime; |
| 272 |
timeMetricToText(time, text, textWidth); |
| 273 |
break; |
| 274 |
} |
| 275 |
default: |
| 276 |
DNOT_REACHED(); |
| 277 |
break; |
| 278 |
} |
| 279 |
|
| 280 |
m_bitmapText.updateText( |
| 281 |
text, m_metrics[UMEvent::Frame][i].textIndex, |
| 282 |
m_metrics[UMEvent::Frame][i].width); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
void Overlay::updateWindowMetrics(quint32 windowId, const QSize& frameSize) |
| 287 |
{ |
| 288 |
DASSERT(m_flags & Initialized); |
| 289 |
Q_STATIC_ASSERT(IS_POWER_OF_TWO(maxMetricWidth)); |
| 290 |
|
| 291 |
char* text = static_cast<char*>(m_buffer); |
| 292 |
for (int i = 0; i < m_metricsSize[UMEvent::Window]; i++) { |
| 293 |
int textWidth = m_metrics[UMEvent::Window][i].width; |
| 294 |
DASSERT(textWidth <= maxMetricWidth); |
| 295 |
memset(text, ' ', maxMetricWidth); |
| 296 |
|
| 297 |
switch (m_metrics[UMEvent::Window][i].index) { |
| 298 |
case WindowId: |
| 299 |
textWidth = integerMetricToText(windowId, text, textWidth); |
| 300 |
break; |
| 301 |
case WindowSize: { |
| 302 |
textWidth = integerMetricToText(frameSize.height(), text, textWidth); |
| 303 |
if (textWidth >= 2) { |
| 304 |
text[textWidth - 1] = 'x'; |
| 305 |
integerMetricToText(frameSize.width(), text, textWidth - 1); |
| 306 |
} else if (textWidth == 1) { |
| 307 |
text[textWidth - 1] = 'x'; |
| 308 |
} |
| 309 |
break; |
| 310 |
} |
| 311 |
default: |
| 312 |
DNOT_REACHED(); |
| 313 |
break; |
| 314 |
} |
| 315 |
|
| 316 |
m_bitmapText.updateText( |
| 317 |
text, m_metrics[UMEvent::Window][i].textIndex, |
| 318 |
m_metrics[UMEvent::Window][i].width); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
void Overlay::updateProcessMetrics() |
| 323 |
{ |
| 324 |
DASSERT(m_flags & Initialized); |
| 325 |
Q_STATIC_ASSERT(IS_POWER_OF_TWO(maxMetricWidth)); |
| 326 |
|
| 327 |
char* text = static_cast<char*>(m_buffer); |
| 328 |
for (int i = 0; i < m_metricsSize[UMEvent::Process]; i++) { |
| 329 |
int textWidth = m_metrics[UMEvent::Process][i].width; |
| 330 |
DASSERT(textWidth <= maxMetricWidth); |
| 331 |
memset(text, ' ', maxMetricWidth); |
| 332 |
|
| 333 |
switch (m_metrics[UMEvent::Process][i].index) { |
| 334 |
case CpuUsage: |
| 335 |
integerMetricToText(m_processEvent.process.cpuUsage, text, textWidth); |
| 336 |
break; |
| 337 |
case ThreadCount: |
| 338 |
integerMetricToText(m_processEvent.process.threadCount, text, textWidth); |
| 339 |
break; |
| 340 |
case VszMemory: |
| 341 |
integerMetricToText(m_processEvent.process.vszMemory, text, textWidth); |
| 342 |
break; |
| 343 |
case RssMemory: |
| 344 |
integerMetricToText(m_processEvent.process.rssMemory, text, textWidth); |
| 345 |
break; |
| 346 |
default: |
| 347 |
DNOT_REACHED(); |
| 348 |
break; |
| 349 |
} |
| 350 |
|
| 351 |
m_bitmapText.updateText( |
| 352 |
text, m_metrics[UMEvent::Process][i].textIndex, |
| 353 |
m_metrics[UMEvent::Process][i].width); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
static int cpuModel(char* buffer, int bufferSize) |
| 358 |
{ |
| 359 |
DASSERT(buffer); |
| 360 |
DASSERT(bufferSize > 0); |
| 361 |
|
| 362 |
const char* architecture = QSysInfo::currentCpuArchitecture().toLatin1().constData(); |
| 363 |
const int sourceBufferSize = 128; |
| 364 |
char sourceBuffer[sourceBufferSize]; |
| 365 |
int index = 0; |
| 366 |
|
| 367 |
if (!strncmp(architecture, "x86", 3) || !strncmp(architecture, "X86", 3)) { |
| 368 |
// /proc/cpuinfo depends on the architecture, this only works on x86. |
| 369 |
int fd = open("/proc/cpuinfo", O_RDONLY); |
| 370 |
if (fd == -1) { |
| 371 |
DWARN("ApplicationMonitor: Can't open '/proc/cpuinfo'."); |
| 372 |
return 0; |
| 373 |
} |
| 374 |
if (read(fd, sourceBuffer, sourceBufferSize) != sourceBufferSize) { |
| 375 |
DWARN("ApplicationMonitor: Can't read '/proc/cpuinfo'."); |
| 376 |
close(fd); |
| 377 |
return 0; |
| 378 |
} |
| 379 |
close(fd); |
| 380 |
|
| 381 |
// Skip the five first ': ' occurences to reach model name. |
| 382 |
int sourceIndex = 0, colonCount = 0; |
| 383 |
while (colonCount < 5) { |
| 384 |
if (sourceIndex < sourceBufferSize - 1) { |
| 385 |
if (sourceBuffer[sourceIndex] != ':' || sourceBuffer[sourceIndex + 1] != ' ') { |
| 386 |
sourceIndex++; |
| 387 |
} else { |
| 388 |
sourceIndex += 2; |
| 389 |
colonCount++; |
| 390 |
} |
| 391 |
} else { |
| 392 |
DNOT_REACHED(); // Consider increasing sourceBufferSize. |
| 393 |
return 0; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
while (sourceBuffer[sourceIndex] != '\n' && index < bufferSize) { |
| 398 |
if (sourceIndex < sourceBufferSize) { |
| 399 |
buffer[index++] = sourceBuffer[sourceIndex++]; |
| 400 |
} else { |
| 401 |
DNOT_REACHED(); // Consider increasing sourceBufferSize. |
| 402 |
index = 0; |
| 403 |
break; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
} else { |
| 408 |
// Symply use the CPU architecture. |
| 409 |
for (; index < bufferSize; index++) { |
| 410 |
if (architecture[index] == '\0') break; |
| 411 |
buffer[index] = architecture[index]; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
// Add the core count. |
| 416 |
const int cpuOnlineCores = sysconf(_SC_NPROCESSORS_ONLN); |
| 417 |
if (cpuOnlineCores > 1) { |
| 418 |
const int maxSize = sizeof(" (%d cores)") - 2 + 3; // Adds space for a 3 digits core count. |
| 419 |
const int size = snprintf(sourceBuffer, maxSize, " (%d cores)", cpuOnlineCores); |
| 420 |
if (index + size < bufferSize) { |
| 421 |
memcpy(&buffer[index], sourceBuffer, size); |
| 422 |
index += size; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
return index; |
| 427 |
} |
| 428 |
|
| 429 |
// Stores the keyword string corresponding to the given index in a preallocated |
| 430 |
// buffer of size bufferSize, the terminating null byte ('\0') is not |
| 431 |
// written. Returns the number of characters written. Requires an OpenGL context |
| 432 |
// to be bound to the current thread. |
| 433 |
int Overlay::keywordString(int index, char* buffer, int bufferSize) |
| 434 |
{ |
| 435 |
DASSERT(index < KeywordCount); |
| 436 |
DASSERT(buffer); |
| 437 |
DASSERT(bufferSize > 0); |
| 438 |
|
| 439 |
int size = 0; |
| 440 |
|
| 441 |
switch (index) { |
| 442 |
case QtVersion: { |
| 443 |
const char* version = "Qt " QT_VERSION_STR; |
| 444 |
for (; size < bufferSize; size++) { |
| 445 |
if (version[size] == '\0') break; |
| 446 |
buffer[size] = version[size]; |
| 447 |
} |
| 448 |
break; |
| 449 |
} |
| 450 |
case QtPlatform: { |
| 451 |
const char* platform = QGuiApplication::platformName().toLatin1().constData(); |
| 452 |
for (; size < bufferSize; size++) { |
| 453 |
if (platform[size] == '\0') break; |
| 454 |
buffer[size] = platform[size]; |
| 455 |
} |
| 456 |
break; |
| 457 |
} |
| 458 |
case GlVersion: { |
| 459 |
QOpenGLFunctions* functions = QOpenGLContext::currentContext()->functions(); |
| 460 |
const char* version = reinterpret_cast<const char*>(functions->glGetString(GL_VERSION)); |
| 461 |
if (size < (bufferSize - 7) && QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) { |
| 462 |
memcpy(&buffer[size], "OpenGL ", 7); |
| 463 |
size += 7; |
| 464 |
} |
| 465 |
for (int i = 0; size < bufferSize; i++, size++) { |
| 466 |
if (version[i] == '\0') break; |
| 467 |
buffer[size] = version[i]; |
| 468 |
} |
| 469 |
break; |
| 470 |
} |
| 471 |
case CpuModel: { |
| 472 |
if (cpuModelStringSize == 0) { |
| 473 |
cpuModelStringSize = cpuModel(cpuModelString, maxKeywordStringSize); |
| 474 |
if (cpuModelStringSize == 0) { |
| 475 |
const char* const defaultModel = "Unknown CPU"; |
| 476 |
const int defaultModelSize = sizeof("Unknown CPU") - 1; |
| 477 |
memcpy(cpuModelString, defaultModel, defaultModelSize); |
| 478 |
cpuModelStringSize = defaultModelSize; |
| 479 |
} |
| 480 |
} |
| 481 |
memcpy(buffer, cpuModelString, cpuModelStringSize); |
| 482 |
size = cpuModelStringSize; |
| 483 |
break; |
| 484 |
} |
| 485 |
case GpuModel: { |
| 486 |
QOpenGLFunctions* functions = QOpenGLContext::currentContext()->functions(); |
| 487 |
const char* vendor = reinterpret_cast<const char*>(functions->glGetString(GL_VENDOR)); |
| 488 |
const char* renderer = reinterpret_cast<const char*>(functions->glGetString(GL_RENDERER)); |
| 489 |
for (int i = 0; size < bufferSize; i++, size++) { |
| 490 |
if (vendor[i] == '\0') break; |
| 491 |
buffer[size] = vendor[i]; |
| 492 |
} |
| 493 |
if (size < (bufferSize - 1)) { |
| 494 |
buffer[size++] = ' '; |
| 495 |
} |
| 496 |
for (int i = 0; size < bufferSize; i++, size++) { |
| 497 |
if (renderer[i] == '\0') break; |
| 498 |
buffer[size] = renderer[i]; |
| 499 |
} |
| 500 |
break; |
| 501 |
} |
| 502 |
default: |
| 503 |
DNOT_REACHED(); |
| 504 |
break; |
| 505 |
} |
| 506 |
|
| 507 |
return size; |
| 508 |
} |
| 509 |
|
| 510 |
void Overlay::parseText() |
| 511 |
{ |
| 512 |
QByteArray textLatin1 = m_text.toLatin1(); |
| 513 |
const char* const text = textLatin1.constData(); |
| 514 |
const int textSize = textLatin1.size(); |
| 515 |
char* keywordBuffer = static_cast<char*>(m_buffer); |
| 516 |
int characters = 0; |
| 517 |
|
| 518 |
for (int i = 0; i <= textSize; i++) { |
| 519 |
const char character = text[i]; |
| 520 |
if (character != '%') { |
| 521 |
// Common case. |
| 522 |
m_parsedText[characters++] = character; |
| 523 |
} else if (text[i+1] == '%') { |
| 524 |
// "%%" outputs "%". |
| 525 |
m_parsedText[characters++] = '%'; |
| 526 |
i++; |
| 527 |
} else { |
| 528 |
bool keywordFound = false; |
| 529 |
// Search for keywords. |
| 530 |
for (int j = 0; j < KeywordCount; j++) { |
| 531 |
if (!strncmp(&text[i+1], keywordInfo[j].name, keywordInfo[j].size)) { |
| 532 |
const int stringSize = keywordString(j, keywordBuffer, maxKeywordStringSize); |
| 533 |
if (stringSize < maxParsedTextSize - characters) { |
| 534 |
strcpy(&m_parsedText[characters], keywordBuffer); |
| 535 |
characters += stringSize; |
| 536 |
i += keywordInfo[j].size; |
| 537 |
} |
| 538 |
keywordFound = true; |
| 539 |
break; |
| 540 |
} |
| 541 |
} |
| 542 |
// Search for metrics. |
| 543 |
if (!keywordFound) { |
| 544 |
int width, widthOffset = 0; |
| 545 |
if (!isdigit(text[i+1+widthOffset])) { |
| 546 |
width = -1; |
| 547 |
} else { |
| 548 |
width = text[i+1+widthOffset] - '0'; |
| 549 |
widthOffset++; |
| 550 |
if (isdigit(text[i+1+widthOffset])) { |
| 551 |
width = width * 10 + text[i+1+widthOffset] - '0'; |
| 552 |
widthOffset++; |
| 553 |
} |
| 554 |
width = qBound(1, width, maxMetricWidth); |
| 555 |
} |
| 556 |
for (int j = 0; j < MetricCount; j++) { |
| 557 |
const int type = metricInfo[j].type; |
| 558 |
DASSERT(type >= 0); |
| 559 |
DASSERT(type < UMEvent::TypeCount); |
| 560 |
if (m_metricsSize[type] < maxMetricsPerType && |
| 561 |
!strncmp(&text[i+1+widthOffset], metricInfo[j].name, metricInfo[j].size)) { |
| 562 |
if (width == -1) { |
| 563 |
width = metricInfo[j].defaultWidth; |
| 564 |
} |
| 565 |
if (width < maxParsedTextSize - characters) { |
| 566 |
m_metrics[type][m_metricsSize[type]].index = j; |
| 567 |
m_metrics[type][m_metricsSize[type]].textIndex = characters; |
| 568 |
m_metrics[type][m_metricsSize[type]].width = width; |
| 569 |
// Must be initialized since it might contain non |
| 570 |
// printable characters and break setText otherwise. |
| 571 |
memset(&m_parsedText[characters], '?', width); |
| 572 |
characters += width; |
| 573 |
i += widthOffset + metricInfo[j].size; |
| 574 |
m_metricsSize[type]++; |
| 575 |
} |
| 576 |
break; |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
} |
| 581 |
// Set string terminator and quit once the max size is reached. |
| 582 |
if (characters >= (maxParsedTextSize - 1)) { |
| 583 |
m_parsedText[maxParsedTextSize - 1] = '\0'; |
| 584 |
break; |
| 585 |
} |
| 586 |
} |
| 587 |
} |