Name of computer


#1

JUCE usually has it all, but I haven’t found a way to get the computer name (SystemStats doesn’t have it)… :slight_smile:


#2

Hmm, I thought I’d have done that too, but I guess not! Will note it down…


#3

Just to get you started… :wink:

Win32:

String SystemStats::GetMachineName()
{
    wchar_t name[MAX_COMPUTERNAME_LENGTH+1];
    DWORD dwSize = sizeof(name);
    if (!GetComputerNameW(name, &dwSize))
    {
        return String::empty;
    }
    return String(name);
}

#4

Ah, always good to have a starting point, thanks!


#5

Don’t forget the laptop battery level while you’re at it!!


#6

If you can give me some linux code to get the battery level, I promise to do it!


#7

int batteryLevel() { int batLev = -1; FILE* f = fopen( "/proc/acpi/battery/BAT0/state", "r" ); if( f ) { fscanf( f, "%i", &batLev ); fclose( f ); } return batLev; }

Or something similar (I’m hoping)


#8

ok… I only have a linux VM, which isn’t ideal for testing things like that, so how confident are you?


#9

ok… I only have a linux VM, which isn’t ideal for testing things like that, so how confident are you?[/quote]

Pretty sure it will only work on some systems, the ones that have acpi. The linux situation of getting the battery level is not so great, as you have probably already noticed. I don’t think there is a universal solution.

It might not be possible to write a routine that reliably gets the battery level on all systems.


#10

[quote=“TheVinn”]It might not be possible to write a routine that reliably gets the battery level on all systems.
[/quote]

Well, it is, but it’s not the 2-liners you’ve written:

// 1) On recent kernel, with sysctl, enumerate this directory /sys/class/power_supply/ and for each entry :
int currentCapacity = File("/sys/class/power_supply/"+entry+"/charge_now").readAsString().getIntValue();
int totalCapacity = File(/sys/class/power_supply/"+entry+"/charge_full").readAsString().getIntValue();


// 2) Else On old PC with a working ACPI resource, you'll need to enumerate the Dir entries in "/proc/acpi/battery/", and for each entry read the "$ENTRY/state" 
int currentCapacity = File("/proc/acpi/battery/" + entry + "/state").readAsString().fromFirstOccurrenceOf("remaining capacity:", false, false).getIntValue();
int totalCapacity = File("/proc/acpi/battery/" + entry + "/info").readAsString().fromFirstOccurrenceOf("design capacity:", false, false).getIntValue();

and extract the battery level by dividing the first by the second one (FYI, my battery is BAT1 not BAT0, so you definitively need to enumerate entries)


// On android, you can parse this file:
/sys/class/power_supply/battery/smem_text
// Contains:
batt_id: 1
batt_vol: 3840
batt_vol_last: 0
batt_temp: 1072
batt_current: 1
batt_current_last: 0
batt_discharge_current: 112
VREF_2: 0
VREF: 1243
ADC4096_VREF: 4073
Rtemp: 70
Temp: 324
Temp_last: 0
pd_M: 20
MBAT_pd: 3860
I_MBAT: -114
pd_temp: 0
percent_last: 57
percent_update: 58
dis_percent: 64
vbus: 0
usbid: 1
charging_source: 0
MBAT_IN: 1
full_bat: 1300000
eval_current: 115
eval_current_last: 0
charging_enabled: 0
timeout: 30
fullcharge: 0
level: 58
delta: 1
chg_time: 0
level_change: 0
sleep_timer_count: 11
OT_led_on: 0
overloading_charge: 0
a2m_cable_type: 0
over_vchg: 0

#11

The Mac machine name may not be the way you’ve tried either. On my machine, this gets stuck. If I break into the debugger, the stack has:

[NSProcessInfo hostName] [NSHost currentHost] getnameinfo si_nameinfo search_host_byaddr _mdns_hostbyaddr _mdns_search _mdns_query_mDNSResponder kevent
And there it sits. mDNS, if you’re not aware, is Bonjour. Even if it didn’t block, I guess it’s not the quickest way to get this info.

Bruce


#12

This 'nixy version worked on my machine:

[code] char name [256] = { 0 };
if (gethostname (name, sizeof (name) - 1) == 0)
return String(name).upToLastOccurrenceOf (".local", false, true);

return String::empty;[/code]

Bruce


#13

Bizarre, it worked fine on my mac… Ok, I guess I’ll go with the unixy version!


#14

How about:

[[NSHost currentHost] localizedName];

Does that work ?