diff options
Diffstat (limited to 'tools/perf/util/newt.c')
| -rw-r--r-- | tools/perf/util/newt.c | 306 |
1 files changed, 5 insertions, 301 deletions
diff --git a/tools/perf/util/newt.c b/tools/perf/util/newt.c index 37fe8eb811ca..266a9e06525b 100644 --- a/tools/perf/util/newt.c +++ b/tools/perf/util/newt.c | |||
| @@ -23,6 +23,7 @@ | |||
| 23 | #include "session.h" | 23 | #include "session.h" |
| 24 | #include "sort.h" | 24 | #include "sort.h" |
| 25 | #include "symbol.h" | 25 | #include "symbol.h" |
| 26 | #include "ui/browser.h" | ||
| 26 | 27 | ||
| 27 | #if SLANG_VERSION < 20104 | 28 | #if SLANG_VERSION < 20104 |
| 28 | #define slsmg_printf(msg, args...) SLsmg_printf((char *)msg, ##args) | 29 | #define slsmg_printf(msg, args...) SLsmg_printf((char *)msg, ##args) |
| @@ -35,6 +36,8 @@ | |||
| 35 | #define sltt_set_color SLtt_set_color | 36 | #define sltt_set_color SLtt_set_color |
| 36 | #endif | 37 | #endif |
| 37 | 38 | ||
| 39 | newtComponent newt_form__new(void); | ||
| 40 | |||
| 38 | struct ui_progress { | 41 | struct ui_progress { |
| 39 | newtComponent form, scale; | 42 | newtComponent form, scale; |
| 40 | }; | 43 | }; |
| @@ -190,7 +193,7 @@ static void newt_form__set_exit_keys(newtComponent self) | |||
| 190 | newtFormAddHotKey(self, CTRL('c')); | 193 | newtFormAddHotKey(self, CTRL('c')); |
| 191 | } | 194 | } |
| 192 | 195 | ||
| 193 | static newtComponent newt_form__new(void) | 196 | newtComponent newt_form__new(void) |
| 194 | { | 197 | { |
| 195 | newtComponent self = newtForm(NULL, NULL, 0); | 198 | newtComponent self = newtForm(NULL, NULL, 0); |
| 196 | if (self) | 199 | if (self) |
| @@ -290,172 +293,6 @@ static void ui__error_window(const char *fmt, ...) | |||
| 290 | va_end(ap); | 293 | va_end(ap); |
| 291 | } | 294 | } |
| 292 | 295 | ||
| 293 | #define HE_COLORSET_TOP 50 | ||
| 294 | #define HE_COLORSET_MEDIUM 51 | ||
| 295 | #define HE_COLORSET_NORMAL 52 | ||
| 296 | #define HE_COLORSET_SELECTED 53 | ||
| 297 | #define HE_COLORSET_CODE 54 | ||
| 298 | |||
| 299 | static int ui_browser__percent_color(double percent, bool current) | ||
| 300 | { | ||
| 301 | if (current) | ||
| 302 | return HE_COLORSET_SELECTED; | ||
| 303 | if (percent >= MIN_RED) | ||
| 304 | return HE_COLORSET_TOP; | ||
| 305 | if (percent >= MIN_GREEN) | ||
| 306 | return HE_COLORSET_MEDIUM; | ||
| 307 | return HE_COLORSET_NORMAL; | ||
| 308 | } | ||
| 309 | |||
| 310 | struct ui_browser { | ||
| 311 | newtComponent form, sb; | ||
| 312 | u64 index, first_visible_entry_idx; | ||
| 313 | void *first_visible_entry, *entries; | ||
| 314 | u16 top, left, width, height; | ||
| 315 | void *priv; | ||
| 316 | unsigned int (*refresh)(struct ui_browser *self); | ||
| 317 | void (*write)(struct ui_browser *self, void *entry, int row); | ||
| 318 | void (*seek)(struct ui_browser *self, | ||
| 319 | off_t offset, int whence); | ||
| 320 | u32 nr_entries; | ||
| 321 | }; | ||
| 322 | |||
| 323 | static void ui_browser__list_head_seek(struct ui_browser *self, | ||
| 324 | off_t offset, int whence) | ||
| 325 | { | ||
| 326 | struct list_head *head = self->entries; | ||
| 327 | struct list_head *pos; | ||
| 328 | |||
| 329 | switch (whence) { | ||
| 330 | case SEEK_SET: | ||
| 331 | pos = head->next; | ||
| 332 | break; | ||
| 333 | case SEEK_CUR: | ||
| 334 | pos = self->first_visible_entry; | ||
| 335 | break; | ||
| 336 | case SEEK_END: | ||
| 337 | pos = head->prev; | ||
| 338 | break; | ||
| 339 | default: | ||
| 340 | return; | ||
| 341 | } | ||
| 342 | |||
| 343 | if (offset > 0) { | ||
| 344 | while (offset-- != 0) | ||
| 345 | pos = pos->next; | ||
| 346 | } else { | ||
| 347 | while (offset++ != 0) | ||
| 348 | pos = pos->prev; | ||
| 349 | } | ||
| 350 | |||
| 351 | self->first_visible_entry = pos; | ||
| 352 | } | ||
| 353 | |||
| 354 | static void ui_browser__rb_tree_seek(struct ui_browser *self, | ||
| 355 | off_t offset, int whence) | ||
| 356 | { | ||
| 357 | struct rb_root *root = self->entries; | ||
| 358 | struct rb_node *nd; | ||
| 359 | |||
| 360 | switch (whence) { | ||
| 361 | case SEEK_SET: | ||
| 362 | nd = rb_first(root); | ||
| 363 | break; | ||
| 364 | case SEEK_CUR: | ||
| 365 | nd = self->first_visible_entry; | ||
| 366 | break; | ||
| 367 | case SEEK_END: | ||
| 368 | nd = rb_last(root); | ||
| 369 | break; | ||
| 370 | default: | ||
| 371 | return; | ||
| 372 | } | ||
| 373 | |||
| 374 | if (offset > 0) { | ||
| 375 | while (offset-- != 0) | ||
| 376 | nd = rb_next(nd); | ||
| 377 | } else { | ||
| 378 | while (offset++ != 0) | ||
| 379 | nd = rb_prev(nd); | ||
| 380 | } | ||
| 381 | |||
| 382 | self->first_visible_entry = nd; | ||
| 383 | } | ||
| 384 | |||
| 385 | static unsigned int ui_browser__rb_tree_refresh(struct ui_browser *self) | ||
| 386 | { | ||
| 387 | struct rb_node *nd; | ||
| 388 | int row = 0; | ||
| 389 | |||
| 390 | if (self->first_visible_entry == NULL) | ||
| 391 | self->first_visible_entry = rb_first(self->entries); | ||
| 392 | |||
| 393 | nd = self->first_visible_entry; | ||
| 394 | |||
| 395 | while (nd != NULL) { | ||
| 396 | SLsmg_gotorc(self->top + row, self->left); | ||
| 397 | self->write(self, nd, row); | ||
| 398 | if (++row == self->height) | ||
| 399 | break; | ||
| 400 | nd = rb_next(nd); | ||
| 401 | } | ||
| 402 | |||
| 403 | return row; | ||
| 404 | } | ||
| 405 | |||
| 406 | static bool ui_browser__is_current_entry(struct ui_browser *self, unsigned row) | ||
| 407 | { | ||
| 408 | return (self->first_visible_entry_idx + row) == self->index; | ||
| 409 | } | ||
| 410 | |||
| 411 | static void ui_browser__refresh_dimensions(struct ui_browser *self) | ||
| 412 | { | ||
| 413 | int cols, rows; | ||
| 414 | newtGetScreenSize(&cols, &rows); | ||
| 415 | |||
| 416 | if (self->width > cols - 4) | ||
| 417 | self->width = cols - 4; | ||
| 418 | self->height = rows - 5; | ||
| 419 | if (self->height > self->nr_entries) | ||
| 420 | self->height = self->nr_entries; | ||
| 421 | self->top = (rows - self->height) / 2; | ||
| 422 | self->left = (cols - self->width) / 2; | ||
| 423 | } | ||
| 424 | |||
| 425 | static void ui_browser__reset_index(struct ui_browser *self) | ||
| 426 | { | ||
| 427 | self->index = self->first_visible_entry_idx = 0; | ||
| 428 | self->seek(self, 0, SEEK_SET); | ||
| 429 | } | ||
| 430 | |||
| 431 | static int ui_browser__show(struct ui_browser *self, const char *title) | ||
| 432 | { | ||
| 433 | if (self->form != NULL) { | ||
| 434 | newtFormDestroy(self->form); | ||
| 435 | newtPopWindow(); | ||
| 436 | } | ||
| 437 | ui_browser__refresh_dimensions(self); | ||
| 438 | newtCenteredWindow(self->width, self->height, title); | ||
| 439 | self->form = newt_form__new(); | ||
| 440 | if (self->form == NULL) | ||
| 441 | return -1; | ||
| 442 | |||
| 443 | self->sb = newtVerticalScrollbar(self->width, 0, self->height, | ||
| 444 | HE_COLORSET_NORMAL, | ||
| 445 | HE_COLORSET_SELECTED); | ||
| 446 | if (self->sb == NULL) | ||
| 447 | return -1; | ||
| 448 | |||
| 449 | newtFormAddHotKey(self->form, NEWT_KEY_UP); | ||
| 450 | newtFormAddHotKey(self->form, NEWT_KEY_DOWN); | ||
| 451 | newtFormAddHotKey(self->form, NEWT_KEY_PGUP); | ||
| 452 | newtFormAddHotKey(self->form, NEWT_KEY_PGDN); | ||
| 453 | newtFormAddHotKey(self->form, NEWT_KEY_HOME); | ||
| 454 | newtFormAddHotKey(self->form, NEWT_KEY_END); | ||
| 455 | newtFormAddComponent(self->form, self->sb); | ||
| 456 | return 0; | ||
| 457 | } | ||
| 458 | |||
| 459 | static void annotate_browser__write(struct ui_browser *self, void *entry, int row) | 296 | static void annotate_browser__write(struct ui_browser *self, void *entry, int row) |
| 460 | { | 297 | { |
| 461 | struct objdump_line *ol = rb_entry(entry, struct objdump_line, node); | 298 | struct objdump_line *ol = rb_entry(entry, struct objdump_line, node); |
| @@ -507,98 +344,6 @@ static void annotate_browser__write(struct ui_browser *self, void *entry, int ro | |||
| 507 | slsmg_write_nstring(ol->line, width - 18); | 344 | slsmg_write_nstring(ol->line, width - 18); |
| 508 | } | 345 | } |
| 509 | 346 | ||
| 510 | static int ui_browser__refresh(struct ui_browser *self) | ||
| 511 | { | ||
| 512 | int row; | ||
| 513 | |||
| 514 | newtScrollbarSet(self->sb, self->index, self->nr_entries - 1); | ||
| 515 | row = self->refresh(self); | ||
| 516 | SLsmg_set_color(HE_COLORSET_NORMAL); | ||
| 517 | SLsmg_fill_region(self->top + row, self->left, | ||
| 518 | self->height - row, self->width, ' '); | ||
| 519 | |||
| 520 | return 0; | ||
| 521 | } | ||
| 522 | |||
| 523 | static int ui_browser__run(struct ui_browser *self, struct newtExitStruct *es) | ||
| 524 | { | ||
| 525 | if (ui_browser__refresh(self) < 0) | ||
| 526 | return -1; | ||
| 527 | |||
| 528 | while (1) { | ||
| 529 | off_t offset; | ||
| 530 | |||
| 531 | newtFormRun(self->form, es); | ||
| 532 | |||
| 533 | if (es->reason != NEWT_EXIT_HOTKEY) | ||
| 534 | break; | ||
| 535 | if (is_exit_key(es->u.key)) | ||
| 536 | return es->u.key; | ||
| 537 | switch (es->u.key) { | ||
| 538 | case NEWT_KEY_DOWN: | ||
| 539 | if (self->index == self->nr_entries - 1) | ||
| 540 | break; | ||
| 541 | ++self->index; | ||
| 542 | if (self->index == self->first_visible_entry_idx + self->height) { | ||
| 543 | ++self->first_visible_entry_idx; | ||
| 544 | self->seek(self, +1, SEEK_CUR); | ||
| 545 | } | ||
| 546 | break; | ||
| 547 | case NEWT_KEY_UP: | ||
| 548 | if (self->index == 0) | ||
| 549 | break; | ||
| 550 | --self->index; | ||
| 551 | if (self->index < self->first_visible_entry_idx) { | ||
| 552 | --self->first_visible_entry_idx; | ||
| 553 | self->seek(self, -1, SEEK_CUR); | ||
| 554 | } | ||
| 555 | break; | ||
| 556 | case NEWT_KEY_PGDN: | ||
| 557 | case ' ': | ||
| 558 | if (self->first_visible_entry_idx + self->height > self->nr_entries - 1) | ||
| 559 | break; | ||
| 560 | |||
| 561 | offset = self->height; | ||
| 562 | if (self->index + offset > self->nr_entries - 1) | ||
| 563 | offset = self->nr_entries - 1 - self->index; | ||
| 564 | self->index += offset; | ||
| 565 | self->first_visible_entry_idx += offset; | ||
| 566 | self->seek(self, +offset, SEEK_CUR); | ||
| 567 | break; | ||
| 568 | case NEWT_KEY_PGUP: | ||
| 569 | if (self->first_visible_entry_idx == 0) | ||
| 570 | break; | ||
| 571 | |||
| 572 | if (self->first_visible_entry_idx < self->height) | ||
| 573 | offset = self->first_visible_entry_idx; | ||
| 574 | else | ||
| 575 | offset = self->height; | ||
| 576 | |||
| 577 | self->index -= offset; | ||
| 578 | self->first_visible_entry_idx -= offset; | ||
| 579 | self->seek(self, -offset, SEEK_CUR); | ||
| 580 | break; | ||
| 581 | case NEWT_KEY_HOME: | ||
| 582 | ui_browser__reset_index(self); | ||
| 583 | break; | ||
| 584 | case NEWT_KEY_END: | ||
| 585 | offset = self->height - 1; | ||
| 586 | if (offset >= self->nr_entries) | ||
| 587 | offset = self->nr_entries - 1; | ||
| 588 | |||
| 589 | self->index = self->nr_entries - 1; | ||
| 590 | self->first_visible_entry_idx = self->index - offset; | ||
| 591 | self->seek(self, -offset, SEEK_END); | ||
| 592 | break; | ||
| 593 | default: | ||
| 594 | return es->u.key; | ||
| 595 | } | ||
| 596 | if (ui_browser__refresh(self) < 0) | ||
| 597 | return -1; | ||
| 598 | } | ||
| 599 | return 0; | ||
| 600 | } | ||
| 601 | |||
| 602 | static char *callchain_list__sym_name(struct callchain_list *self, | 347 | static char *callchain_list__sym_name(struct callchain_list *self, |
| 603 | char *bf, size_t bfsize) | 348 | char *bf, size_t bfsize) |
| 604 | { | 349 | { |
| @@ -609,27 +354,6 @@ static char *callchain_list__sym_name(struct callchain_list *self, | |||
| 609 | return bf; | 354 | return bf; |
| 610 | } | 355 | } |
| 611 | 356 | ||
| 612 | static unsigned int ui_browser__list_head_refresh(struct ui_browser *self) | ||
| 613 | { | ||
| 614 | struct list_head *pos; | ||
| 615 | struct list_head *head = self->entries; | ||
| 616 | int row = 0; | ||
| 617 | |||
| 618 | if (self->first_visible_entry == NULL || self->first_visible_entry == self->entries) | ||
| 619 | self->first_visible_entry = head->next; | ||
| 620 | |||
| 621 | pos = self->first_visible_entry; | ||
| 622 | |||
| 623 | list_for_each_from(pos, head) { | ||
| 624 | SLsmg_gotorc(self->top + row, self->left); | ||
| 625 | self->write(self, pos, row); | ||
| 626 | if (++row == self->height) | ||
| 627 | break; | ||
| 628 | } | ||
| 629 | |||
| 630 | return row; | ||
| 631 | } | ||
| 632 | |||
| 633 | int hist_entry__tui_annotate(struct hist_entry *self) | 357 | int hist_entry__tui_annotate(struct hist_entry *self) |
| 634 | { | 358 | { |
| 635 | struct newtExitStruct es; | 359 | struct newtExitStruct es; |
| @@ -1093,20 +817,6 @@ int hists__tui_browse_tree(struct rb_root *self, const char *help) | |||
| 1093 | return key; | 817 | return key; |
| 1094 | } | 818 | } |
| 1095 | 819 | ||
| 1096 | static struct newtPercentTreeColors { | ||
| 1097 | const char *topColorFg, *topColorBg; | ||
| 1098 | const char *mediumColorFg, *mediumColorBg; | ||
| 1099 | const char *normalColorFg, *normalColorBg; | ||
| 1100 | const char *selColorFg, *selColorBg; | ||
| 1101 | const char *codeColorFg, *codeColorBg; | ||
| 1102 | } defaultPercentTreeColors = { | ||
| 1103 | "red", "lightgray", | ||
| 1104 | "green", "lightgray", | ||
| 1105 | "black", "lightgray", | ||
| 1106 | "lightgray", "magenta", | ||
| 1107 | "blue", "lightgray", | ||
| 1108 | }; | ||
| 1109 | |||
| 1110 | static void newt_suspend(void *d __used) | 820 | static void newt_suspend(void *d __used) |
| 1111 | { | 821 | { |
| 1112 | newtSuspend(); | 822 | newtSuspend(); |
| @@ -1116,8 +826,6 @@ static void newt_suspend(void *d __used) | |||
| 1116 | 826 | ||
| 1117 | void setup_browser(void) | 827 | void setup_browser(void) |
| 1118 | { | 828 | { |
| 1119 | struct newtPercentTreeColors *c = &defaultPercentTreeColors; | ||
| 1120 | |||
| 1121 | if (!isatty(1) || !use_browser || dump_trace) { | 829 | if (!isatty(1) || !use_browser || dump_trace) { |
| 1122 | use_browser = 0; | 830 | use_browser = 0; |
| 1123 | setup_pager(); | 831 | setup_pager(); |
| @@ -1129,11 +837,7 @@ void setup_browser(void) | |||
| 1129 | newtCls(); | 837 | newtCls(); |
| 1130 | newtSetSuspendCallback(newt_suspend, NULL); | 838 | newtSetSuspendCallback(newt_suspend, NULL); |
| 1131 | ui_helpline__puts(" "); | 839 | ui_helpline__puts(" "); |
| 1132 | sltt_set_color(HE_COLORSET_TOP, NULL, c->topColorFg, c->topColorBg); | 840 | ui_browser__init(); |
| 1133 | sltt_set_color(HE_COLORSET_MEDIUM, NULL, c->mediumColorFg, c->mediumColorBg); | ||
| 1134 | sltt_set_color(HE_COLORSET_NORMAL, NULL, c->normalColorFg, c->normalColorBg); | ||
| 1135 | sltt_set_color(HE_COLORSET_SELECTED, NULL, c->selColorFg, c->selColorBg); | ||
| 1136 | sltt_set_color(HE_COLORSET_CODE, NULL, c->codeColorFg, c->codeColorBg); | ||
| 1137 | } | 841 | } |
| 1138 | 842 | ||
| 1139 | void exit_browser(bool wait_for_ok) | 843 | void exit_browser(bool wait_for_ok) |
