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