Added darwin with working battery meter

This commit is contained in:
David Hunt 2015-07-12 13:47:43 -05:00 committed by Hisham Muhammad
parent feb7a01fd3
commit 70e7c8db59
12 changed files with 475 additions and 0 deletions

View File

@ -13,6 +13,7 @@ pixmapdir = $(datadir)/pixmaps
pixmap_DATA = htop.png
htop_CFLAGS = -pedantic -Wall $(wextra_flag) -std=c99 -D_XOPEN_SOURCE_EXTENDED -DSYSCONFDIR=\"$(sysconfdir)\" -I"$(top_srcdir)/$(my_htop_platform)"
htop_LDFLAGS =
AM_CPPFLAGS = -DNDEBUG
myhtopsources = AvailableMetersPanel.c CategoriesPanel.c CheckItem.c \
@ -50,6 +51,15 @@ myhtopplatheaders = freebsd/Platform.h freebsd/FreeBSDProcessList.h \
freebsd/FreeBSDProcess.h freebsd/FreeBSDCRT.h freebsd/Battery.h
endif
if HTOP_DARWIN
htop_LDFLAGS += -framework IOKit -framework CoreFoundation
myhtopplatsources = darwin/Platform.c darwin/DarwinProcess.c \
darwin/DarwinProcessList.c darwin/DarwinCRT.c darwin/Battery.c
myhtopplatheaders = darwin/Platform.h darwin/DarwinProcess.h \
darwin/DarwinProcessList.h darwin/DarwinCRT.h darwin/Battery.h
endif
if HTOP_UNSUPPORTED
myhtopplatsources = unsupported/Platform.c \
unsupported/UnsupportedProcess.c unsupported/UnsupportedProcessList.c \

View File

@ -33,6 +33,9 @@ case "$target" in
*freebsd*)
my_htop_platform=freebsd
;;
*darwin*)
my_htop_platform=darwin
;;
*)
my_htop_platform=unsupported
;;
@ -203,6 +206,7 @@ AC_DEFINE_UNQUOTED(COPYRIGHT, "(C) 2004-$year Hisham Muhammad", [Copyright messa
# ----------------------------------------------------------------------
AM_CONDITIONAL([HTOP_LINUX], [test "$my_htop_platform" = linux])
AM_CONDITIONAL([HTOP_FREEBSD], [test "$my_htop_platform" = freebsd])
AM_CONDITIONAL([HTOP_DARWIN], [test "$my_htop_platform" = darwin])
AM_CONDITIONAL([HTOP_UNSUPPORTED], [test "$my_htop_platform" = unsupported])
AC_SUBST(my_htop_platform)
AC_CONFIG_FILES([Makefile htop.1])

75
darwin/Battery.c Normal file
View File

@ -0,0 +1,75 @@
#include "BatteryMeter.h"
#include <CoreFoundation/CoreFoundation.h>
#include <CoreFoundation/CFString.h>
#include <IOKit/ps/IOPowerSources.h>
#include <IOKit/ps/IOPSKeys.h>
void Battery_getData(double* level, ACPresence* isOnAC) {
CFTypeRef power_sources = IOPSCopyPowerSourcesInfo();
*level = -1;
*isOnAC = AC_ERROR;
if(NULL == power_sources) {
return;
}
if(power_sources != NULL) {
CFArrayRef list = IOPSCopyPowerSourcesList(power_sources);
CFDictionaryRef battery = NULL;
int len;
if(NULL == list) {
CFRelease(power_sources);
return;
}
len = CFArrayGetCount(list);
/* Get the battery */
for(int i = 0; i < len && battery == NULL; ++i) {
CFDictionaryRef candidate = IOPSGetPowerSourceDescription(power_sources,
CFArrayGetValueAtIndex(list, i)); /* GET rule */
CFStringRef type;
if(NULL != candidate) {
type = (CFStringRef) CFDictionaryGetValue(candidate,
CFSTR(kIOPSTransportTypeKey)); /* GET rule */
if(kCFCompareEqualTo == CFStringCompare(type, CFSTR(kIOPSInternalType), 0)) {
CFRetain(candidate);
battery = candidate;
}
}
}
if(NULL != battery) {
/* Determine the AC state */
CFStringRef power_state = CFDictionaryGetValue(battery, CFSTR(kIOPSPowerSourceStateKey));
*isOnAC = (kCFCompareEqualTo == CFStringCompare(power_state, CFSTR(kIOPSACPowerValue), 0))
? AC_PRESENT
: AC_ABSENT;
/* Get the percentage remaining */
double current;
double max;
CFNumberGetValue(CFDictionaryGetValue(battery, CFSTR(kIOPSCurrentCapacityKey)),
kCFNumberDoubleType, &current);
CFNumberGetValue(CFDictionaryGetValue(battery, CFSTR(kIOPSMaxCapacityKey)),
kCFNumberDoubleType, &max);
*level = (current * 100.0) / max;
CFRelease(battery);
}
CFRelease(list);
CFRelease(power_sources);
}
}

9
darwin/Battery.h Normal file
View File

@ -0,0 +1,9 @@
/* Do not edit this file. It was automatically generated. */
#ifndef HEADER_Battery
#define HEADER_Battery
void Battery_getData(double* level, ACPresence* isOnAC);
#endif

34
darwin/DarwinCRT.c Normal file
View File

@ -0,0 +1,34 @@
/*
htop - DarwinCRT.c
(C) 2014 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "config.h"
#include "CRT.h"
#include <stdio.h>
#include <stdlib.h>
void CRT_handleSIGSEGV(int sgn) {
(void) sgn;
CRT_done();
#ifdef __APPLE__
fprintf(stderr, "\n\nhtop " VERSION " aborting. Please report bug at http://hisham.hm/htop\n");
#ifdef HAVE_EXECINFO_H
size_t size = backtrace(backtraceArray, sizeof(backtraceArray) / sizeof(void *));
fprintf(stderr, "\n Please include in your report the following backtrace: \n");
backtrace_symbols_fd(backtraceArray, size, 2);
fprintf(stderr, "\nAdditionally, in order to make the above backtrace useful,");
fprintf(stderr, "\nplease also run the following command to generate a disassembly of your binary:");
fprintf(stderr, "\n\n otool -tvV `which htop` > ~/htop.otool");
fprintf(stderr, "\n\nand then attach the file ~/htop.otool to your bug report.");
fprintf(stderr, "\n\nThank you for helping to improve htop!\n\n");
#endif
#else
fprintf(stderr, "\nUnfortunately, you seem to be using an unsupported platform!");
fprintf(stderr, "\nPlease contact your platform package mantainer!\n\n");
#endif
abort();
}

15
darwin/DarwinCRT.h Normal file
View File

@ -0,0 +1,15 @@
/* Do not edit this file. It was automatically generated. */
#ifndef HEADER_DarwinCRT
#define HEADER_DarwinCRT
/*
htop - DarwinCRT.h
(C) 2014 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
void CRT_handleSIGSEGV(int sgn);
#endif

33
darwin/DarwinProcess.c Normal file
View File

@ -0,0 +1,33 @@
/*
htop - DarwinProcess.c
(C) 2015 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "Process.h"
#include "DarwinProcess.h"
#include <stdlib.h>
/*{
#include "Settings.h"
#define Process_delete UnsupportedProcess_delete
}*/
Process* DarwinProcess_new(Settings* settings) {
Process* this = calloc(sizeof(Process), 1);
Object_setClass(this, Class(Process));
Process_init(this, settings);
return this;
}
void DarwinProcess_delete(Object* cast) {
Process* this = (Process*) cast;
Object_setClass(this, Class(Process));
Process_done((Process*)cast);
// free platform-specific fields here
free(this);
}

22
darwin/DarwinProcess.h Normal file
View File

@ -0,0 +1,22 @@
/* Do not edit this file. It was automatically generated. */
#ifndef HEADER_DarwinProcess
#define HEADER_DarwinProcess
/*
htop - DarwinProcess.h
(C) 2015 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "Settings.h"
#define Process_delete DarwinProcess_delete
Process* DarwinProcess_new(Settings* settings);
void DarwinProcess_delete(Object* cast);
#endif

View File

@ -0,0 +1,71 @@
/*
htop - DarwinProcessList.c
(C) 2014 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "ProcessList.h"
#include "DarwinProcess.h"
#include "DarwinProcessList.h"
#include <stdlib.h>
#include <string.h>
/*{
}*/
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
ProcessList* this = calloc(1, sizeof(ProcessList));
ProcessList_init(this, Class(Process), usersTable, pidWhiteList, userId);
return this;
}
void ProcessList_delete(ProcessList* this) {
ProcessList_done(this);
free(this);
}
void ProcessList_goThroughEntries(ProcessList* super) {
bool preExisting = true;
Process *proc;
proc = ProcessList_getProcess(super, 1, &preExisting, DarwinProcess_new);
/* Empty values */
proc->time = proc->time + 10;
proc->pid = 1;
proc->ppid = 1;
proc->tgid = 0;
proc->comm = "<unsupported architecture>";
proc->basenameOffset = 0;
proc->updated = true;
proc->state = 'R';
proc->show = true; /* Reflected in proc->settings-> "hideXXX" really */
proc->pgrp = 0;
proc->session = 0;
proc->tty_nr = 0;
proc->tpgid = 0;
proc->st_uid = 0;
proc->flags = 0;
proc->processor = 0;
proc->percent_cpu = 2.5;
proc->percent_mem = 2.5;
proc->user = "nobody";
proc->priority = 0;
proc->nice = 0;
proc->nlwp = 1;
strncpy(proc->starttime_show, "Jun 01 ", sizeof(proc->starttime_show));
proc->starttime_ctime = 1433116800; // Jun 01, 2015
proc->m_size = 100;
proc->m_resident = 100;
proc->minflt = 20;
proc->majflt = 20;
}

View File

@ -0,0 +1,20 @@
/* Do not edit this file. It was automatically generated. */
#ifndef HEADER_DarwinProcessList
#define HEADER_DarwinProcessList
/*
htop - DarwinProcessList.h
(C) 2014 Hisham H. Muhammad
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId);
void ProcessList_delete(ProcessList* this);
void ProcessList_goThroughEntries(ProcessList* super);
#endif

136
darwin/Platform.c Normal file
View File

@ -0,0 +1,136 @@
/*
htop - darwin/Platform.c
(C) 2014 Hisham H. Muhammad
(C) 2015 David C. Hunt
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "Platform.h"
#include "CPUMeter.h"
#include "MemoryMeter.h"
#include "SwapMeter.h"
#include "TasksMeter.h"
#include "LoadAverageMeter.h"
#include "ClockMeter.h"
#include "HostnameMeter.h"
#include "UptimeMeter.h"
/*{
#include "Action.h"
#include "BatteryMeter.h"
#include "DarwinProcess.h"
}*/
ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_SIZE, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 };
ProcessFieldData Process_fields[] = {
[0] = { .name = "", .title = NULL, .description = NULL, .flags = 0, },
[PID] = { .name = "PID", .title = " PID ", .description = "Process/thread ID", .flags = 0, },
[COMM] = { .name = "Command", .title = "Command ", .description = "Command line", .flags = 0, },
[STATE] = { .name = "STATE", .title = "S ", .description = "Process state (S sleeping, R running, D disk, Z zombie, T traced, W paging)", .flags = 0, },
[PPID] = { .name = "PPID", .title = " PPID ", .description = "Parent process ID", .flags = 0, },
[PGRP] = { .name = "PGRP", .title = " PGRP ", .description = "Process group ID", .flags = 0, },
[SESSION] = { .name = "SESSION", .title = " SESN ", .description = "Process's session ID", .flags = 0, },
[TTY_NR] = { .name = "TTY_NR", .title = " TTY ", .description = "Controlling terminal", .flags = 0, },
[TPGID] = { .name = "TPGID", .title = " TPGID ", .description = "Process ID of the fg process group of the controlling terminal", .flags = 0, },
[MINFLT] = { .name = "MINFLT", .title = " MINFLT ", .description = "Number of minor faults which have not required loading a memory page from disk", .flags = 0, },
[MAJFLT] = { .name = "MAJFLT", .title = " MAJFLT ", .description = "Number of major faults which have required loading a memory page from disk", .flags = 0, },
[PRIORITY] = { .name = "PRIORITY", .title = "PRI ", .description = "Kernel's internal priority for the process", .flags = 0, },
[NICE] = { .name = "NICE", .title = " NI ", .description = "Nice value (the higher the value, the more it lets other processes take priority)", .flags = 0, },
[STARTTIME] = { .name = "STARTTIME", .title = "START ", .description = "Time the process was started", .flags = 0, },
[PROCESSOR] = { .name = "PROCESSOR", .title = "CPU ", .description = "Id of the CPU the process last executed on", .flags = 0, },
[M_SIZE] = { .name = "M_SIZE", .title = " VIRT ", .description = "Total program size in virtual memory", .flags = 0, },
[M_RESIDENT] = { .name = "M_RESIDENT", .title = " RES ", .description = "Resident set size, size of the text and data sections, plus stack usage", .flags = 0, },
[ST_UID] = { .name = "ST_UID", .title = " UID ", .description = "User ID of the process owner", .flags = 0, },
[PERCENT_CPU] = { .name = "PERCENT_CPU", .title = "CPU% ", .description = "Percentage of the CPU time the process used in the last sampling", .flags = 0, },
[PERCENT_MEM] = { .name = "PERCENT_MEM", .title = "MEM% ", .description = "Percentage of the memory the process is using, based on resident memory size", .flags = 0, },
[USER] = { .name = "USER", .title = "USER ", .description = "Username of the process owner (or user ID if name cannot be determined)", .flags = 0, },
[TIME] = { .name = "TIME", .title = " TIME+ ", .description = "Total time the process has spent in user and system time", .flags = 0, },
[NLWP] = { .name = "NLWP", .title = "NLWP ", .description = "Number of threads in the process", .flags = 0, },
[TGID] = { .name = "TGID", .title = " TGID ", .description = "Thread group ID (i.e. process ID)", .flags = 0, },
[100] = { .name = "*** report bug! ***", .title = NULL, .description = NULL, .flags = 0, },
};
MeterClass* Platform_meterTypes[] = {
&CPUMeter_class,
&ClockMeter_class,
&LoadAverageMeter_class,
&LoadMeter_class,
&MemoryMeter_class,
&SwapMeter_class,
&TasksMeter_class,
&BatteryMeter_class,
&HostnameMeter_class,
&UptimeMeter_class,
&AllCPUsMeter_class,
&AllCPUs2Meter_class,
&LeftCPUsMeter_class,
&RightCPUsMeter_class,
&LeftCPUs2Meter_class,
&RightCPUs2Meter_class,
&BlankMeter_class,
NULL
};
void Platform_setBindings(Htop_Action* keys) {
(void) keys;
}
int Platform_numberOfFields = 100;
char* Process_pidFormat = "%7u ";
char* Process_tpgidFormat = "%7u ";
int Platform_getUptime() {
return 0;
}
void Platform_getLoadAverage(double* one, double* five, double* fifteen) {
*one = 0;
*five = 0;
*fifteen = 0;
}
int Platform_getMaxPid() {
return 1;
}
void Process_setupColumnWidths() {
int maxPid = Platform_getMaxPid();
if (maxPid == -1) return;
if (maxPid > 99999) {
Process_fields[PID].title = " PID ";
Process_fields[PPID].title = " PPID ";
Process_fields[TPGID].title = " TPGID ";
Process_fields[TGID].title = " TGID ";
Process_fields[PGRP].title = " PGRP ";
Process_fields[SESSION].title = " SESN ";
Process_pidFormat = "%7u ";
Process_tpgidFormat = "%7d ";
} else {
Process_fields[PID].title = " PID ";
Process_fields[PPID].title = " PPID ";
Process_fields[TPGID].title = "TPGID ";
Process_fields[TGID].title = " TGID ";
Process_fields[PGRP].title = " PGRP ";
Process_fields[SESSION].title = " SESN ";
Process_pidFormat = "%5u ";
Process_tpgidFormat = "%5d ";
}
}
double Platform_setCPUValues(Meter* this, int cpu) {
return 0.0;
}
void Platform_setMemoryValues(Meter* this) {
}
void Platform_setSwapValues(Meter* this) {
}
bool Process_isThread(Process* this) {
return false;
}

46
darwin/Platform.h Normal file
View File

@ -0,0 +1,46 @@
/* Do not edit this file. It was automatically generated. */
#ifndef HEADER_Platform
#define HEADER_Platform
/*
htop - darwin/Platform.h
(C) 2014 Hisham H. Muhammad
(C) 2015 David C. Hunt
Released under the GNU GPL, see the COPYING file
in the source distribution for its full text.
*/
#include "Action.h"
#include "BatteryMeter.h"
#include "DarwinProcess.h"
extern ProcessField Platform_defaultFields[];
extern ProcessFieldData Process_fields[];
extern MeterClass* Platform_meterTypes[];
void Platform_setBindings(Htop_Action* keys);
extern int Platform_numberOfFields;
extern char* Process_pidFormat;
extern char* Process_tpgidFormat;
int Platform_getUptime();
void Platform_getLoadAverage(double* one, double* five, double* fifteen);
int Platform_getMaxPid();
void Process_setupColumnWidths();
double Platform_setCPUValues(Meter* this, int cpu);
void Platform_setMemoryValues(Meter* this);
void Platform_setSwapValues(Meter* this);
bool Process_isThread(Process* this);
#endif