Skip to content

Proposal: Send battery level to JS #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Classes/BrowserComponents/SEBBrowserWindow.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#import "NSURL+KKDomain.h"
#import "HUDPanel.h"
#import "NSScreen+SEBScreen.h"
#include <IOKit/ps/IOPowerSources.h>
#include <IOKit/ps/IOPSKeys.h>

#include <CoreServices/CoreServices.h>

Expand Down Expand Up @@ -171,6 +173,25 @@ - (void) awakeFromNib
}
}

- (void) updateBattery :(NSTimer *)timer
{
CFArrayRef sources = IOPSCopyPowerSourcesList(IOPSCopyPowerSourcesInfo());
CFDictionaryRef powerSource = NULL;

long numOfSources = CFArrayGetCount(sources);
if (numOfSources == 0) return;

// Use first power source if there are several available
powerSource = IOPSGetPowerSourceDescription(sources, CFArrayGetValueAtIndex(sources, 0));
int curCapacity = 0;
int maxCapacity = 0;

CFNumberGetValue((CFNumberRef)CFDictionaryGetValue(powerSource, CFSTR(kIOPSCurrentCapacityKey)), kCFNumberSInt32Type, &curCapacity);
CFNumberGetValue((CFNumberRef)CFDictionaryGetValue(powerSource, CFSTR(kIOPSMaxCapacityKey)), kCFNumberSInt32Type, &maxCapacity);

[self.webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat:@"window.SEB.battery = %f", (double)curCapacity/(double)maxCapacity]];
}


- (void) setCalculatedFrame
{
Expand Down Expand Up @@ -1030,6 +1051,9 @@ - (void)webView:(SEBWebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
[self backForwardButtonsSetEnabled];

[self stopProgressIndicatorAnimation];

NSTimer *batteryTimer = [NSTimer scheduledTimerWithTimeInterval:30.0f target:self selector:@selector(updateBattery:) userInfo:nil repeats:YES];
[batteryTimer fire];
}


Expand Down