1717#import " HUDBackdropLabel.h"
1818#import " TrollSpeed-Swift.h"
1919
20+ #ifdef __cplusplus
21+ extern " C" {
22+ #endif
23+ CFIndex CARenderServerGetDirtyFrameCount (void *);
24+ #ifdef __cplusplus
25+ }
26+ #endif
27+
2028#pragma mark -
2129
2230#import " FBSOrientationUpdate.h"
2937#define NOTIFY_UI_LOCKSTATE " com.apple.springboard.lockstate"
3038#define NOTIFY_LS_APP_CHANGED " com.apple.LaunchServices.ApplicationsChanged"
3139
40+ static BOOL needsBaselineReset = YES ;
41+ static BOOL needsFPSBaselineReset = YES ;
42+
3243static void LaunchServicesApplicationStateChanged
3344(CFNotificationCenterRef center,
3445 void *observer,
7889
7990 if (!isLocked)
8091 {
92+ needsBaselineReset = YES ;
93+ needsFPSBaselineReset = YES ;
8194 [rootViewController.view setHidden: NO ];
8295 [rootViewController resetLoopTimer ];
8396 }
116129static uint8_t HUD_SHOW_SECOND_SPEED_IN_NEW_LINE = 0 ;
117130static const char *HUD_UPLOAD_PREFIX = " ▲" ;
118131static const char *HUD_DOWNLOAD_PREFIX = " ▼" ;
132+ static uint8_t HUD_DISPLAY_MODE = 0 ; // 0=Speed, 1=FPS
119133
120134typedef struct {
121135 uint64_t inputBytes;
@@ -310,6 +324,7 @@ static UpDownBytes getUpDownBytes()
310324
311325static BOOL shouldUpdateSpeedLabel;
312326static uint64_t prevOutputBytes = 0 , prevInputBytes = 0 ;
327+ static CFIndex prevDirtyFrameCount = 0 ;
313328static NSAttributedString *attributedUploadPrefix = nil ;
314329static NSAttributedString *attributedDownloadPrefix = nil ;
315330static NSAttributedString *attributedInlineSeparator = nil ;
@@ -335,6 +350,15 @@ static UpDownBytes getUpDownBytes()
335350 uint64_t upDiff;
336351 uint64_t downDiff;
337352
353+ if (needsBaselineReset && !isFocused)
354+ {
355+ prevOutputBytes = upDownBytes.outputBytes ;
356+ prevInputBytes = upDownBytes.inputBytes ;
357+ needsBaselineReset = NO ;
358+ shouldUpdateSpeedLabel = NO ;
359+ return nil ;
360+ }
361+
338362 if (isFocused)
339363 {
340364 upDiff = upDownBytes.outputBytes ;
@@ -413,6 +437,44 @@ static UpDownBytes getUpDownBytes()
413437 }
414438}
415439
440+ static NSAttributedString *formattedFPSAttributedString (BOOL isFocused)
441+ {
442+ @autoreleasepool
443+ {
444+ CFIndex dirtyFrameCount = CARenderServerGetDirtyFrameCount (NULL );
445+
446+ if (needsFPSBaselineReset)
447+ {
448+ prevDirtyFrameCount = dirtyFrameCount;
449+ needsFPSBaselineReset = NO ;
450+ shouldUpdateSpeedLabel = YES ;
451+
452+ NSString *fpsString = @" 0 FPS" ;
453+ return [[NSAttributedString alloc ] initWithString: fpsString attributes: @{
454+ NSFontAttributeName : [UIFont monospacedDigitSystemFontOfSize: HUD_FONT_SIZE weight: HUD_FONT_WEIGHT]
455+ }];
456+ }
457+
458+ CFIndex frameDiff = dirtyFrameCount - prevDirtyFrameCount;
459+ prevDirtyFrameCount = dirtyFrameCount;
460+
461+ if (frameDiff < 0 ) frameDiff = 0 ;
462+
463+ double fps = (double )frameDiff / UPDATE_INTERVAL;
464+ double maxFPS = (double )[UIScreen mainScreen ].maximumFramesPerSecond ;
465+ if (fps > maxFPS) fps = maxFPS;
466+
467+ shouldUpdateSpeedLabel = YES ;
468+
469+ NSString *fpsString = [NSString stringWithFormat: @" %.0f FPS" , fps];
470+ NSAttributedString *attributedString = [[NSAttributedString alloc ] initWithString: fpsString attributes: @{
471+ NSFontAttributeName : [UIFont monospacedDigitSystemFontOfSize: HUD_FONT_SIZE weight: HUD_FONT_WEIGHT]
472+ }];
473+
474+ return attributedString;
475+ }
476+ }
477+
416478#pragma mark - HUDRootViewController
417479
418480@interface HUDRootViewController (Troll)
@@ -548,8 +610,14 @@ - (void)reloadUserDefaults
548610 [_containerView setupContainerAsDisplayContentInScreenshots ];
549611 }
550612
613+ BOOL displayMode = [self displayMode ];
614+ HUD_DISPLAY_MODE = displayMode;
615+
551616 prevInputBytes = 0 ;
552617 prevOutputBytes = 0 ;
618+ needsBaselineReset = YES ;
619+ prevDirtyFrameCount = 0 ;
620+ needsFPSBaselineReset = YES ;
553621 attributedUploadPrefix = nil ;
554622 attributedDownloadPrefix = nil ;
555623
@@ -601,6 +669,13 @@ - (BOOL)singleLineMode
601669 return mode != nil ? [mode boolValue ] : NO ;
602670}
603671
672+ - (BOOL )displayMode
673+ {
674+ [self loadUserDefaults: NO ];
675+ NSNumber *mode = [_userDefaults objectForKey: HUDUserDefaultsKeyDisplayMode];
676+ return mode != nil ? [mode boolValue ] : NO ;
677+ }
678+
604679- (BOOL )usesBitrate
605680{
606681 [self loadUserDefaults: NO ];
@@ -759,7 +834,12 @@ - (void)dealloc
759834- (void )updateSpeedLabel
760835{
761836 log_debug (OS_LOG_DEFAULT, " updateSpeedLabel" );
762- NSAttributedString *attributedText = formattedAttributedString (_isFocused);
837+ NSAttributedString *attributedText;
838+ if (HUD_DISPLAY_MODE == 1 ) {
839+ attributedText = formattedFPSAttributedString (_isFocused);
840+ } else {
841+ attributedText = formattedAttributedString (_isFocused);
842+ }
763843 if (attributedText) {
764844 [_speedLabel setAttributedText: attributedText];
765845 }
0 commit comments