1 /* 2 * Copyright 2008-2009, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "DataSource.h" 8 9 #include <stdio.h> 10 #include <stdint.h> 11 12 #include <Catalog.h> 13 #include <OS.h> 14 #include <String.h> 15 #include <StringForRate.h> 16 17 #include "SystemInfo.h" 18 19 #undef B_TRANSLATION_CONTEXT 20 #define B_TRANSLATION_CONTEXT "DataSource" 21 22 23 const DataSource* kSources[] = { 24 new UsedMemoryDataSource(), 25 new CachedMemoryDataSource(), 26 new SwapSpaceDataSource(), 27 new PageFaultsDataSource(), 28 new CPUUsageDataSource(), 29 new CPUCombinedUsageDataSource(), 30 new NetworkUsageDataSource(true), 31 new NetworkUsageDataSource(false), 32 new BlockCacheDataSource(), 33 new SemaphoresDataSource(), 34 new PortsDataSource(), 35 new ThreadsDataSource(), 36 new TeamsDataSource(), 37 new RunningAppsDataSource(), 38 new ClipboardSizeDataSource(false), 39 new ClipboardSizeDataSource(true), 40 new MediaNodesDataSource() 41 }; 42 const size_t kSourcesCount = sizeof(kSources) / sizeof(kSources[0]); 43 44 45 DataSource::DataSource(int64 initialMin, int64 initialMax) 46 : 47 fMinimum(initialMin), 48 fMaximum(initialMax), 49 fInterval(1000000LL), 50 fColor((rgb_color){200, 0, 0}) 51 { 52 } 53 54 55 DataSource::DataSource() 56 : 57 fMinimum(0), 58 fMaximum(100), 59 fInterval(1000000LL), 60 fColor((rgb_color){200, 0, 0}) 61 { 62 } 63 64 65 DataSource::DataSource(const DataSource& other) 66 { 67 fMinimum = other.fMinimum; 68 fMaximum = other.fMaximum; 69 fInterval = other.fInterval; 70 fColor = other.fColor; 71 } 72 73 74 DataSource::~DataSource() 75 { 76 } 77 78 79 DataSource* 80 DataSource::Copy() const 81 { 82 return NULL; 83 // this class cannot be copied 84 } 85 86 87 DataSource* 88 DataSource::CopyForCPU(int32 cpu) const 89 { 90 return Copy(); 91 } 92 93 94 int64 95 DataSource::Minimum() const 96 { 97 return fMinimum; 98 } 99 100 101 int64 102 DataSource::Maximum() const 103 { 104 return fMaximum; 105 } 106 107 108 bigtime_t 109 DataSource::RefreshInterval() const 110 { 111 return fInterval; 112 } 113 114 115 void 116 DataSource::SetLimits(int64 min, int64 max) 117 { 118 fMinimum = min; 119 fMaximum = max; 120 } 121 122 123 void 124 DataSource::SetRefreshInterval(bigtime_t interval) 125 { 126 fInterval = interval; 127 } 128 129 130 void 131 DataSource::SetColor(rgb_color color) 132 { 133 fColor = color; 134 } 135 136 137 int64 138 DataSource::NextValue(SystemInfo& info) 139 { 140 return 0; 141 } 142 143 144 void 145 DataSource::Print(BString& text, int64 value) const 146 { 147 text = ""; 148 text << value; 149 } 150 151 152 const char* 153 DataSource::ShortLabel() const 154 { 155 return Label(); 156 } 157 158 159 const char* 160 DataSource::Name() const 161 { 162 return Label(); 163 } 164 165 166 const char* 167 DataSource::Label() const 168 { 169 return ""; 170 } 171 172 173 const char* 174 DataSource::Unit() const 175 { 176 return ""; 177 } 178 179 180 rgb_color 181 DataSource::Color() const 182 { 183 return fColor; 184 } 185 186 187 bool 188 DataSource::AdaptiveScale() const 189 { 190 return false; 191 } 192 193 194 scale_type 195 DataSource::ScaleType() const 196 { 197 return kNoScale; 198 } 199 200 201 int32 202 DataSource::CPU() const 203 { 204 return 0; 205 } 206 207 208 bool 209 DataSource::PerCPU() const 210 { 211 return false; 212 } 213 214 215 bool 216 DataSource::MultiCPUOnly() const 217 { 218 return false; 219 } 220 221 222 bool 223 DataSource::Primary() const 224 { 225 return false; 226 } 227 228 229 /*static*/ int32 230 DataSource::CountSources() 231 { 232 return kSourcesCount; 233 } 234 235 236 /*static*/ const DataSource* 237 DataSource::SourceAt(int32 index) 238 { 239 if (index >= (int32)kSourcesCount || index < 0) 240 return NULL; 241 242 return kSources[index]; 243 } 244 245 246 /*static*/ const DataSource* 247 DataSource::FindSource(const char* internalName) 248 { 249 for (uint32 i = 0; i < kSourcesCount; i++) { 250 const DataSource* source = kSources[i]; 251 if (!strcmp(source->InternalName(), internalName)) 252 return source; 253 } 254 255 return NULL; 256 } 257 258 259 /*static*/ int32 260 DataSource::IndexOf(const DataSource* source) 261 { 262 const char* name = source->Name(); 263 264 for (uint32 i = 0; i < kSourcesCount; i++) { 265 if (!strcmp(kSources[i]->Name(), name)) 266 return i; 267 } 268 269 return -1; 270 } 271 272 273 // #pragma mark - 274 275 276 MemoryDataSource::MemoryDataSource() 277 { 278 SystemInfo info; 279 280 fMinimum = 0; 281 fMaximum = info.MaxMemory(); 282 } 283 284 285 MemoryDataSource::~MemoryDataSource() 286 { 287 } 288 289 290 void 291 MemoryDataSource::Print(BString& text, int64 value) const 292 { 293 char buffer[32]; 294 snprintf(buffer, sizeof(buffer), B_TRANSLATE("%.1f MiB"), value / 1048576.0); 295 296 text = buffer; 297 } 298 299 300 const char* 301 MemoryDataSource::Unit() const 302 { 303 return B_TRANSLATE("MiB"); 304 } 305 306 307 // #pragma mark - 308 309 310 UsedMemoryDataSource::UsedMemoryDataSource() 311 { 312 } 313 314 315 UsedMemoryDataSource::~UsedMemoryDataSource() 316 { 317 } 318 319 320 DataSource* 321 UsedMemoryDataSource::Copy() const 322 { 323 return new UsedMemoryDataSource(*this); 324 } 325 326 327 int64 328 UsedMemoryDataSource::NextValue(SystemInfo& info) 329 { 330 return info.UsedMemory(); 331 } 332 333 334 const char* 335 UsedMemoryDataSource::InternalName() const 336 { 337 return "Used memory"; 338 } 339 340 341 const char* 342 UsedMemoryDataSource::Label() const 343 { 344 return B_TRANSLATE("Used memory"); 345 } 346 347 348 const char* 349 UsedMemoryDataSource::ShortLabel() const 350 { 351 return B_TRANSLATE("Memory"); 352 } 353 354 355 bool 356 UsedMemoryDataSource::Primary() const 357 { 358 return true; 359 } 360 361 362 // #pragma mark - 363 364 365 CachedMemoryDataSource::CachedMemoryDataSource() 366 { 367 fColor = (rgb_color){0, 200, 0}; 368 } 369 370 371 CachedMemoryDataSource::~CachedMemoryDataSource() 372 { 373 } 374 375 376 DataSource* 377 CachedMemoryDataSource::Copy() const 378 { 379 return new CachedMemoryDataSource(*this); 380 } 381 382 383 int64 384 CachedMemoryDataSource::NextValue(SystemInfo& info) 385 { 386 return info.CachedMemory(); 387 } 388 389 390 const char* 391 CachedMemoryDataSource::InternalName() const 392 { 393 return "Cached memory"; 394 } 395 396 397 const char* 398 CachedMemoryDataSource::Label() const 399 { 400 return B_TRANSLATE("Cached memory"); 401 } 402 403 404 const char* 405 CachedMemoryDataSource::ShortLabel() const 406 { 407 return B_TRANSLATE("Cache"); 408 } 409 410 411 bool 412 CachedMemoryDataSource::Primary() const 413 { 414 return true; 415 } 416 417 418 // #pragma mark - 419 420 421 SwapSpaceDataSource::SwapSpaceDataSource() 422 { 423 SystemInfo info; 424 425 fColor = (rgb_color){0, 120, 0}; 426 fMaximum = info.MaxSwapSpace(); 427 } 428 429 430 SwapSpaceDataSource::~SwapSpaceDataSource() 431 { 432 } 433 434 435 DataSource* 436 SwapSpaceDataSource::Copy() const 437 { 438 return new SwapSpaceDataSource(*this); 439 } 440 441 442 int64 443 SwapSpaceDataSource::NextValue(SystemInfo& info) 444 { 445 return info.UsedSwapSpace(); 446 } 447 448 449 const char* 450 SwapSpaceDataSource::InternalName() const 451 { 452 return "Swap space"; 453 } 454 455 456 const char* 457 SwapSpaceDataSource::Label() const 458 { 459 return B_TRANSLATE("Swap space"); 460 } 461 462 463 const char* 464 SwapSpaceDataSource::ShortLabel() const 465 { 466 return B_TRANSLATE("Swap"); 467 } 468 469 470 bool 471 SwapSpaceDataSource::Primary() const 472 { 473 return true; 474 } 475 476 477 // #pragma mark - 478 479 480 BlockCacheDataSource::BlockCacheDataSource() 481 { 482 fColor = (rgb_color){0, 0, 120}; 483 } 484 485 486 BlockCacheDataSource::~BlockCacheDataSource() 487 { 488 } 489 490 491 DataSource* 492 BlockCacheDataSource::Copy() const 493 { 494 return new BlockCacheDataSource(*this); 495 } 496 497 498 int64 499 BlockCacheDataSource::NextValue(SystemInfo& info) 500 { 501 return info.BlockCacheMemory(); 502 } 503 504 505 const char* 506 BlockCacheDataSource::InternalName() const 507 { 508 return "Block cache memory"; 509 } 510 511 512 const char* 513 BlockCacheDataSource::Label() const 514 { 515 return B_TRANSLATE("Block cache memory"); 516 } 517 518 519 const char* 520 BlockCacheDataSource::ShortLabel() const 521 { 522 return B_TRANSLATE("Block cache"); 523 } 524 525 526 // #pragma mark - 527 528 529 SemaphoresDataSource::SemaphoresDataSource() 530 { 531 SystemInfo info; 532 533 fMinimum = 0; 534 fMaximum = info.MaxSemaphores(); 535 536 fColor = (rgb_color){100, 200, 100}; 537 } 538 539 540 SemaphoresDataSource::~SemaphoresDataSource() 541 { 542 } 543 544 545 DataSource* 546 SemaphoresDataSource::Copy() const 547 { 548 return new SemaphoresDataSource(*this); 549 } 550 551 552 int64 553 SemaphoresDataSource::NextValue(SystemInfo& info) 554 { 555 return info.UsedSemaphores(); 556 } 557 558 559 const char* 560 SemaphoresDataSource::InternalName() const 561 { 562 return "Semaphores"; 563 } 564 565 566 const char* 567 SemaphoresDataSource::Label() const 568 { 569 return B_TRANSLATE("Semaphores"); 570 } 571 572 573 const char* 574 SemaphoresDataSource::ShortLabel() const 575 { 576 return B_TRANSLATE("Sems"); 577 } 578 579 580 bool 581 SemaphoresDataSource::AdaptiveScale() const 582 { 583 return true; 584 } 585 586 587 // #pragma mark - 588 589 590 PortsDataSource::PortsDataSource() 591 { 592 SystemInfo info; 593 594 fMinimum = 0; 595 fMaximum = info.MaxPorts(); 596 597 fColor = (rgb_color){180, 200, 180}; 598 } 599 600 601 PortsDataSource::~PortsDataSource() 602 { 603 } 604 605 606 DataSource* 607 PortsDataSource::Copy() const 608 { 609 return new PortsDataSource(*this); 610 } 611 612 613 int64 614 PortsDataSource::NextValue(SystemInfo& info) 615 { 616 return info.UsedPorts(); 617 } 618 619 620 const char* 621 PortsDataSource::InternalName() const 622 { 623 return "Ports"; 624 } 625 626 627 const char* 628 PortsDataSource::Label() const 629 { 630 return B_TRANSLATE("Ports"); 631 } 632 633 634 bool 635 PortsDataSource::AdaptiveScale() const 636 { 637 return true; 638 } 639 640 641 // #pragma mark - 642 643 644 ThreadsDataSource::ThreadsDataSource() 645 { 646 SystemInfo info; 647 648 fMinimum = 0; 649 fMaximum = info.MaxThreads(); 650 651 fColor = (rgb_color){0, 0, 200}; 652 } 653 654 655 ThreadsDataSource::~ThreadsDataSource() 656 { 657 } 658 659 660 DataSource* 661 ThreadsDataSource::Copy() const 662 { 663 return new ThreadsDataSource(*this); 664 } 665 666 667 int64 668 ThreadsDataSource::NextValue(SystemInfo& info) 669 { 670 return info.UsedThreads(); 671 } 672 673 674 const char* 675 ThreadsDataSource::InternalName() const 676 { 677 return "Threads"; 678 } 679 680 681 const char* 682 ThreadsDataSource::Label() const 683 { 684 return B_TRANSLATE("Threads"); 685 } 686 687 688 bool 689 ThreadsDataSource::AdaptiveScale() const 690 { 691 return true; 692 } 693 694 695 // #pragma mark - 696 697 698 TeamsDataSource::TeamsDataSource() 699 { 700 SystemInfo info; 701 702 fMinimum = 0; 703 fMaximum = info.MaxTeams(); 704 705 fColor = (rgb_color){0, 150, 255}; 706 } 707 708 709 TeamsDataSource::~TeamsDataSource() 710 { 711 } 712 713 714 DataSource* 715 TeamsDataSource::Copy() const 716 { 717 return new TeamsDataSource(*this); 718 } 719 720 721 int64 722 TeamsDataSource::NextValue(SystemInfo& info) 723 { 724 return info.UsedTeams(); 725 } 726 727 728 const char* 729 TeamsDataSource::InternalName() const 730 { 731 return "Teams"; 732 } 733 734 735 const char* 736 TeamsDataSource::Label() const 737 { 738 return B_TRANSLATE("Teams"); 739 } 740 741 742 bool 743 TeamsDataSource::AdaptiveScale() const 744 { 745 return true; 746 } 747 748 749 // #pragma mark - 750 751 752 RunningAppsDataSource::RunningAppsDataSource() 753 { 754 SystemInfo info; 755 756 fMinimum = 0; 757 fMaximum = info.MaxRunningApps(); 758 759 fColor = (rgb_color){100, 150, 255}; 760 } 761 762 763 RunningAppsDataSource::~RunningAppsDataSource() 764 { 765 } 766 767 768 DataSource* 769 RunningAppsDataSource::Copy() const 770 { 771 return new RunningAppsDataSource(*this); 772 } 773 774 775 int64 776 RunningAppsDataSource::NextValue(SystemInfo& info) 777 { 778 return info.UsedRunningApps(); 779 } 780 781 782 const char* 783 RunningAppsDataSource::InternalName() const 784 { 785 return "Running applications"; 786 } 787 788 789 const char* 790 RunningAppsDataSource::Label() const 791 { 792 return B_TRANSLATE("Running applications"); 793 } 794 795 796 const char* 797 RunningAppsDataSource::ShortLabel() const 798 { 799 return B_TRANSLATE("Apps"); 800 } 801 802 803 bool 804 RunningAppsDataSource::AdaptiveScale() const 805 { 806 return true; 807 } 808 809 810 // #pragma mark - 811 812 813 CPUUsageDataSource::CPUUsageDataSource(int32 cpu) 814 : 815 fPreviousActive(0), 816 fPreviousTime(0) 817 { 818 fMinimum = 0; 819 fMaximum = 1000; 820 821 _SetCPU(cpu); 822 } 823 824 825 CPUUsageDataSource::CPUUsageDataSource(const CPUUsageDataSource& other) 826 : DataSource(other) 827 { 828 fPreviousActive = other.fPreviousActive; 829 fPreviousTime = other.fPreviousTime; 830 fCPU = other.fCPU; 831 fLabel = other.fLabel; 832 fShortLabel = other.fShortLabel; 833 } 834 835 836 CPUUsageDataSource::~CPUUsageDataSource() 837 { 838 } 839 840 841 DataSource* 842 CPUUsageDataSource::Copy() const 843 { 844 return new CPUUsageDataSource(*this); 845 } 846 847 848 DataSource* 849 CPUUsageDataSource::CopyForCPU(int32 cpu) const 850 { 851 CPUUsageDataSource* copy = new CPUUsageDataSource(*this); 852 copy->_SetCPU(cpu); 853 854 return copy; 855 } 856 857 858 void 859 CPUUsageDataSource::Print(BString& text, int64 value) const 860 { 861 char buffer[32]; 862 snprintf(buffer, sizeof(buffer), "%.1f%%", value / 10.0); 863 864 text = buffer; 865 } 866 867 868 int64 869 CPUUsageDataSource::NextValue(SystemInfo& info) 870 { 871 bigtime_t active = info.CPUActiveTime(fCPU); 872 873 int64 percent = int64(1000.0 * (active - fPreviousActive) 874 / (info.Time() - fPreviousTime)); 875 if (percent < 0) 876 percent = 0; 877 if (percent > 1000) 878 percent = 1000; 879 880 fPreviousActive = active; 881 fPreviousTime = info.Time(); 882 883 return percent; 884 } 885 886 887 const char* 888 CPUUsageDataSource::Label() const 889 { 890 return fLabel.String(); 891 } 892 893 894 const char* 895 CPUUsageDataSource::ShortLabel() const 896 { 897 return fShortLabel.String(); 898 } 899 900 901 const char* 902 CPUUsageDataSource::InternalName() const 903 { 904 return "CPU usage"; 905 } 906 907 908 const char* 909 CPUUsageDataSource::Name() const 910 { 911 return B_TRANSLATE("CPU usage"); 912 } 913 914 915 int32 916 CPUUsageDataSource::CPU() const 917 { 918 return fCPU; 919 } 920 921 922 bool 923 CPUUsageDataSource::PerCPU() const 924 { 925 return true; 926 } 927 928 929 bool 930 CPUUsageDataSource::Primary() const 931 { 932 return true; 933 } 934 935 936 void 937 CPUUsageDataSource::_SetCPU(int32 cpu) 938 { 939 fCPU = cpu; 940 fLabel = B_TRANSLATE("CPU"); 941 if (SystemInfo().CPUCount() > 1) 942 fLabel << " " << cpu + 1; 943 fShortLabel = fLabel; 944 945 fLabel << " " << B_TRANSLATE("usage"); 946 947 const rgb_color kColors[] = { 948 // TODO: find some better defaults... 949 {200, 0, 200}, 950 {0, 200, 200}, 951 {80, 80, 80}, 952 {230, 150, 50}, 953 {255, 0, 0}, 954 {0, 255, 0}, 955 {0, 0, 255}, 956 {0, 150, 230} 957 }; 958 const uint32 kNumColors = sizeof(kColors) / sizeof(kColors[0]); 959 960 fColor = kColors[cpu % kNumColors]; 961 } 962 963 964 // #pragma mark - 965 966 967 CPUCombinedUsageDataSource::CPUCombinedUsageDataSource() 968 : 969 fPreviousActive(0), 970 fPreviousTime(0) 971 { 972 fMinimum = 0; 973 fMaximum = 1000; 974 975 fColor = (rgb_color){200, 200, 0}; 976 } 977 978 979 CPUCombinedUsageDataSource::CPUCombinedUsageDataSource( 980 const CPUCombinedUsageDataSource& other) 981 : DataSource(other) 982 { 983 fPreviousActive = other.fPreviousActive; 984 fPreviousTime = other.fPreviousTime; 985 } 986 987 988 CPUCombinedUsageDataSource::~CPUCombinedUsageDataSource() 989 { 990 } 991 992 993 DataSource* 994 CPUCombinedUsageDataSource::Copy() const 995 { 996 return new CPUCombinedUsageDataSource(*this); 997 } 998 999 1000 void 1001 CPUCombinedUsageDataSource::Print(BString& text, int64 value) const 1002 { 1003 char buffer[32]; 1004 snprintf(buffer, sizeof(buffer), "%.1f%%", value / 10.0); 1005 1006 text = buffer; 1007 } 1008 1009 1010 int64 1011 CPUCombinedUsageDataSource::NextValue(SystemInfo& info) 1012 { 1013 int32 running = 0; 1014 bigtime_t active = 0; 1015 1016 for (uint32 cpu = 0; cpu < info.CPUCount(); cpu++) { 1017 active += info.CPUActiveTime(cpu); 1018 running++; 1019 // TODO: take disabled CPUs into account 1020 } 1021 1022 int64 percent = int64(1000.0 * (active - fPreviousActive) 1023 / (running * (info.Time() - fPreviousTime))); 1024 if (percent < 0) 1025 percent = 0; 1026 if (percent > 1000) 1027 percent = 1000; 1028 1029 fPreviousActive = active; 1030 fPreviousTime = info.Time(); 1031 1032 return percent; 1033 } 1034 1035 1036 const char* 1037 CPUCombinedUsageDataSource::Label() const 1038 { 1039 return B_TRANSLATE("CPU usage"); 1040 } 1041 1042 1043 const char* 1044 CPUCombinedUsageDataSource::ShortLabel() const 1045 { 1046 return B_TRANSLATE("CPU"); 1047 } 1048 1049 1050 const char* 1051 CPUCombinedUsageDataSource::InternalName() const 1052 { 1053 return "CPU usage (combined)"; 1054 } 1055 1056 1057 const char* 1058 CPUCombinedUsageDataSource::Name() const 1059 { 1060 return B_TRANSLATE("CPU usage (combined)"); 1061 } 1062 1063 1064 bool 1065 CPUCombinedUsageDataSource::MultiCPUOnly() const 1066 { 1067 return true; 1068 } 1069 1070 1071 bool 1072 CPUCombinedUsageDataSource::Primary() const 1073 { 1074 return true; 1075 } 1076 1077 1078 // #pragma mark - 1079 1080 1081 PageFaultsDataSource::PageFaultsDataSource() 1082 : 1083 fPreviousFaults(0), 1084 fPreviousTime(0) 1085 { 1086 SystemInfo info; 1087 NextValue(info); 1088 1089 fMinimum = 0; 1090 fMaximum = 1000000000LL; 1091 1092 fColor = (rgb_color){200, 0, 150, 0}; 1093 } 1094 1095 1096 PageFaultsDataSource::PageFaultsDataSource(const PageFaultsDataSource& other) 1097 : DataSource(other) 1098 { 1099 fPreviousFaults = other.fPreviousFaults; 1100 fPreviousTime = other.fPreviousTime; 1101 } 1102 1103 1104 PageFaultsDataSource::~PageFaultsDataSource() 1105 { 1106 } 1107 1108 1109 DataSource* 1110 PageFaultsDataSource::Copy() const 1111 { 1112 return new PageFaultsDataSource(*this); 1113 } 1114 1115 1116 void 1117 PageFaultsDataSource::Print(BString& text, int64 value) const 1118 { 1119 char buffer[32]; 1120 snprintf(buffer, sizeof(buffer), B_TRANSLATE("%.1f faults/s"), 1121 value / 1024.0); 1122 1123 text = buffer; 1124 } 1125 1126 1127 int64 1128 PageFaultsDataSource::NextValue(SystemInfo& info) 1129 { 1130 uint64 faults = info.PageFaults(); 1131 1132 int64 faultsPerSecond = uint64(1024 * double(faults - fPreviousFaults) 1133 / (info.Time() - fPreviousTime) * 1000000.0); 1134 1135 fPreviousFaults = faults; 1136 fPreviousTime = info.Time(); 1137 1138 return faultsPerSecond; 1139 } 1140 1141 1142 const char* 1143 PageFaultsDataSource::Label() const 1144 { 1145 return B_TRANSLATE("Page faults"); 1146 } 1147 1148 1149 const char* 1150 PageFaultsDataSource::ShortLabel() const 1151 { 1152 return B_TRANSLATE("P-faults"); 1153 } 1154 1155 1156 const char* 1157 PageFaultsDataSource::InternalName() const 1158 { 1159 return "Page faults"; 1160 } 1161 1162 1163 const char* 1164 PageFaultsDataSource::Name() const 1165 { 1166 return B_TRANSLATE("Page faults"); 1167 } 1168 1169 1170 bool 1171 PageFaultsDataSource::AdaptiveScale() const 1172 { 1173 return true; 1174 } 1175 1176 1177 bool 1178 PageFaultsDataSource::Primary() const 1179 { 1180 return false; 1181 } 1182 1183 1184 // #pragma mark - 1185 1186 1187 NetworkUsageDataSource::NetworkUsageDataSource(bool in) 1188 : 1189 fIn(in), 1190 fPreviousBytes(0), 1191 fPreviousTime(0) 1192 { 1193 SystemInfo info; 1194 NextValue(info); 1195 1196 fMinimum = 0; 1197 fMaximum = 1000000000LL; 1198 1199 fColor = fIn ? (rgb_color){200, 150, 0} : (rgb_color){200, 220, 0}; 1200 } 1201 1202 1203 NetworkUsageDataSource::NetworkUsageDataSource( 1204 const NetworkUsageDataSource& other) 1205 : DataSource(other) 1206 { 1207 fIn = other.fIn; 1208 fPreviousBytes = other.fPreviousBytes; 1209 fPreviousTime = other.fPreviousTime; 1210 } 1211 1212 1213 NetworkUsageDataSource::~NetworkUsageDataSource() 1214 { 1215 } 1216 1217 1218 DataSource* 1219 NetworkUsageDataSource::Copy() const 1220 { 1221 return new NetworkUsageDataSource(*this); 1222 } 1223 1224 1225 void 1226 NetworkUsageDataSource::Print(BString& text, int64 value) const 1227 { 1228 char buffer[32]; 1229 string_for_rate(value, buffer, sizeof(buffer)); 1230 1231 text = buffer; 1232 } 1233 1234 1235 int64 1236 NetworkUsageDataSource::NextValue(SystemInfo& info) 1237 { 1238 uint64 transferred = fIn ? info.NetworkReceived() : info.NetworkSent(); 1239 1240 int64 bytesPerSecond = uint64(double(transferred - fPreviousBytes) 1241 / (info.Time() - fPreviousTime) * 1000000.0); 1242 1243 fPreviousBytes = transferred; 1244 fPreviousTime = info.Time(); 1245 1246 return bytesPerSecond; 1247 } 1248 1249 1250 const char* 1251 NetworkUsageDataSource::Label() const 1252 { 1253 return fIn ? B_TRANSLATE("Receiving") : B_TRANSLATE("Sending"); 1254 } 1255 1256 1257 const char* 1258 NetworkUsageDataSource::ShortLabel() const 1259 { 1260 return fIn ? B_TRANSLATE_COMMENT("RX", "Shorter version for Receiving.") : 1261 B_TRANSLATE_COMMENT("TX", "Shorter version for Sending"); 1262 } 1263 1264 1265 const char* 1266 NetworkUsageDataSource::InternalName() const 1267 { 1268 return fIn ? "Network receive" : "Network send"; 1269 } 1270 1271 1272 const char* 1273 NetworkUsageDataSource::Name() const 1274 { 1275 return fIn ? B_TRANSLATE("Network receive") : B_TRANSLATE("Network send"); 1276 } 1277 1278 1279 bool 1280 NetworkUsageDataSource::AdaptiveScale() const 1281 { 1282 return true; 1283 } 1284 1285 1286 scale_type 1287 NetworkUsageDataSource::ScaleType() const 1288 { 1289 return kBytePerSecondScale; 1290 } 1291 1292 1293 bool 1294 NetworkUsageDataSource::Primary() const 1295 { 1296 return true; 1297 } 1298 1299 1300 // #pragma mark - 1301 1302 1303 ClipboardSizeDataSource::ClipboardSizeDataSource(bool text) 1304 { 1305 fMinimum = 0; 1306 fMaximum = UINT32_MAX; 1307 fText = text; 1308 1309 fColor = (rgb_color){0, 150, 255}; 1310 } 1311 1312 1313 ClipboardSizeDataSource::ClipboardSizeDataSource( 1314 const ClipboardSizeDataSource& other) 1315 : DataSource(other) 1316 { 1317 fText = other.fText; 1318 } 1319 1320 1321 ClipboardSizeDataSource::~ClipboardSizeDataSource() 1322 { 1323 } 1324 1325 1326 DataSource* 1327 ClipboardSizeDataSource::Copy() const 1328 { 1329 return new ClipboardSizeDataSource(*this); 1330 } 1331 1332 1333 int64 1334 ClipboardSizeDataSource::NextValue(SystemInfo& info) 1335 { 1336 if (fText) 1337 return info.ClipboardTextSize()/* / 1024*/; 1338 return info.ClipboardSize()/* / 1024*/; 1339 } 1340 1341 1342 const char* 1343 ClipboardSizeDataSource::InternalName() const 1344 { 1345 return fText ? "Text clipboard size" : "Raw clipboard size"; 1346 } 1347 1348 1349 const char* 1350 ClipboardSizeDataSource::Label() const 1351 { 1352 return fText ? B_TRANSLATE("Text clipboard") 1353 : B_TRANSLATE("Raw clipboard"); 1354 } 1355 1356 1357 const char* 1358 ClipboardSizeDataSource::Unit() const 1359 { 1360 return "bytes"/*"KB"*/; 1361 } 1362 1363 1364 bool 1365 ClipboardSizeDataSource::AdaptiveScale() const 1366 { 1367 return true; 1368 } 1369 1370 1371 // #pragma mark - 1372 1373 1374 MediaNodesDataSource::MediaNodesDataSource() 1375 { 1376 SystemInfo info; 1377 1378 fMinimum = 0; 1379 fMaximum = INT32_MAX; 1380 1381 fColor = (rgb_color){255, 150, 225}; 1382 } 1383 1384 1385 MediaNodesDataSource::~MediaNodesDataSource() 1386 { 1387 } 1388 1389 1390 DataSource* 1391 MediaNodesDataSource::Copy() const 1392 { 1393 return new MediaNodesDataSource(*this); 1394 } 1395 1396 1397 int64 1398 MediaNodesDataSource::NextValue(SystemInfo& info) 1399 { 1400 return info.MediaNodes(); 1401 } 1402 1403 1404 const char* 1405 MediaNodesDataSource::InternalName() const 1406 { 1407 return "Media nodes"; 1408 } 1409 1410 1411 const char* 1412 MediaNodesDataSource::Label() const 1413 { 1414 return B_TRANSLATE("Media nodes"); 1415 } 1416 1417 1418 bool 1419 MediaNodesDataSource::AdaptiveScale() const 1420 { 1421 return true; 1422 } 1423 1424 1425