Unify sysinfo plugins and add osx support

This does remove the net* commands from the
Unix version that may return at a later date
with OSX and Windows support.

This commit also makes numerious other changes
such as code cleanup, reformatting, etc.

Closes #829
wip/sysinfo-display
TingPing 2015-02-14 13:35:02 -05:00
parent 363321dc33
commit 5e3355a6c3
28 changed files with 1681 additions and 2232 deletions

1
.gitignore vendored
View File

@ -2,6 +2,7 @@
# Unix generated files
.deps/
.libs/
.dirstamp
Makefile
Makefile.in
aclocal.m4

View File

@ -40,6 +40,7 @@ AH_VERBATIM([PREFIX],[#undef PREFIX])
AH_VERBATIM([HEXCHATLIBDIR],[#undef HEXCHATLIBDIR])
AH_VERBATIM([HEXCHATSHAREDIR],[#undef HEXCHATSHAREDIR])
AH_VERBATIM([USE_LIBPROXY],[#undef USE_LIBPROXY])
AH_VERBATIM([HAVE_LIBPCI],[#undef HAVE_LIBPCI])
AH_VERBATIM([HAVE_ISO_CODES],[#undef HAVE_ISO_CODES])
AH_VERBATIM([HAVE_GTK_MAC],[#undef HAVE_GTK_MAC])
AH_VERBATIM([USE_LIBNOTIFY],[#undef USE_LIBNOTIFY])
@ -73,9 +74,12 @@ else
fi
platform_win32=no
platform_osx=no
case $host_os in
*mingw*|*cygwin*|*msys*)
platform_win32=yes;;
darwin*)
platform_osx=yes;;
*);;
esac
@ -492,7 +496,14 @@ if test "$sysinfo" != "no"; then
AC_MSG_CHECKING(for plugin interface used by SysInfo)
if test "$plugin" = yes; then
AC_MSG_RESULT([yes])
PKG_CHECK_MODULES(LIBPCI, libpci >= 3.0.0, [sysinfo=yes], [sysinfo=no])
if test "$platform_osx" = yes; then
sysinfo=yes
else
PKG_CHECK_MODULES(LIBPCI, libpci >= 3.0.0, [
sysinfo=yes
AC_DEFINE(HAVE_LIBPCI)
], [sysinfo=no])
fi
else
AC_MSG_RESULT([plugins are disabled, use the --enable-plugin option for SysInfo])
sysinfo=no
@ -600,6 +611,7 @@ AM_CONDITIONAL(USE_DBUS, test "x$dbus" = "xyes")
AM_CONDITIONAL(HAVE_ISO_CODES, test "x$isocodes" = "xyes")
AM_CONDITIONAL(HAVE_GTK_MAC, test "x$_gdk_tgt" = xquartz)
AM_CONDITIONAL(WITH_TM, test "x$theme_manager" != "xno")
AM_CONDITIONAL(PLATFORM_OSX, test "x$platform_osx" == "xyes")
dnl *********************************************************************
dnl ** GCC FLAGS ********************************************************

View File

@ -1,7 +1,17 @@
libdir = $(hexchatlibdir)
sources = sysinfo.c format.c shared/df.c
if PLATFORM_OSX
sources += osx/backend.m
else
sources += unix/backend.c unix/match.c unix/parse.c unix/pci.c
endif
EXTRA_DIST = osx unix win32 shared format.h sysinfo.h sysinfo-backend.h
lib_LTLIBRARIES = sysinfo.la
sysinfo_la_SOURCES = match.c parse.c pci.c xsys.c
sysinfo_la_SOURCES = $(sources)
sysinfo_la_LDFLAGS = $(PLUGIN_LDFLAGS) -module
sysinfo_la_LIBADD = $(LIBPCI_LIBS) $(GLIB_LIBS)
sysinfo_la_CFLAGS = $(LIBPCI_CFLAGS) $(GLIB_CFLAGS) -I$(srcdir)/../../src/common
AM_CPPFLAGS = $(LIBPCI_CFLAGS) $(GLIB_CFLAGS) -I$(top_srcdir)/src/common -I$(srcdir)/shared

95
plugins/sysinfo/format.c Normal file
View File

@ -0,0 +1,95 @@
/*
* SysInfo - sysinfo plugin for HexChat
* Copyright (c) 2015 Patrick Griffis.
*
* This program is free software you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <glib.h>
char *
sysinfo_format_uptime (gint64 uptime)
{
char buffer[128];
int weeks;
int days;
int hours;
int minutes;
int seconds;
seconds = uptime%60;
minutes = (uptime/60)%60;
hours = (uptime/3600)%24;
days = (uptime/86400)%7;
weeks = uptime/604800;
if (minutes != 0 || hours != 0 || days != 0 || weeks != 0)
{
if (hours != 0 || days != 0 || weeks != 0)
{
if (days !=0 || weeks != 0)
{
if (weeks != 0)
g_snprintf (buffer, sizeof(buffer), "%dw %dd %dh %dm %ds", weeks, days, hours, minutes, seconds);
else
g_snprintf (buffer, sizeof(buffer), "%dd %dh %dm %ds", days, hours, minutes, seconds);
}
else
{
g_snprintf (buffer, sizeof(buffer), "%dh %dm %ds", hours, minutes, seconds);
}
}
else
{
g_snprintf (buffer, sizeof(buffer), "%dm %ds", minutes, seconds);
}
}
return g_strdup (buffer);
}
char *
sysinfo_format_memory (guint64 totalmem, guint64 freemem)
{
char *total_fmt, *free_fmt, *ret;
total_fmt = g_format_size_full (totalmem, G_FORMAT_SIZE_IEC_UNITS);
free_fmt = g_format_size_full (freemem, G_FORMAT_SIZE_IEC_UNITS);
ret = g_strdup_printf ("%s Total (%s Free)", total_fmt, free_fmt);
g_free (total_fmt);
g_free (free_fmt);
return ret;
}
char *
sysinfo_format_disk (guint64 total, guint64 free)
{
char *total_fmt, *free_fmt, *used_fmt, *ret;
GFormatSizeFlags format_flags = G_FORMAT_SIZE_DEFAULT;
#ifdef WIN32 /* Windows uses IEC size (with SI format) */
format_flags = G_FORMAT_SIZE_IEC_UNITS;
#endif
total_fmt = g_format_size_full (total, format_flags);
free_fmt = g_format_size_full (free, format_flags);
used_fmt = g_format_size_full (total - free, format_flags);
ret = g_strdup_printf ("%s / %s (%s Free)", used_fmt, total_fmt, free_fmt);
g_free (total_fmt);
g_free (free_fmt);
g_free (used_fmt);
return ret;
}

28
plugins/sysinfo/format.h Normal file
View File

@ -0,0 +1,28 @@
/*
* SysInfo - sysinfo plugin for HexChat
* Copyright (c) 2015 Patrick Griffis.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef FORMAT_H
#define FORMAT_H
char *sysinfo_format_uptime(gint64 uptime);
char *sysinfo_format_memory(guint64 totalmem, guint64 freemem);
char *sysinfo_format_disk(guint64 total, guint64 free);
#endif

View File

@ -1,228 +0,0 @@
/*
* match.c - matching functions for X-Sys
* Copyright (C) 2005, 2006, 2007 Tony Vroon
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <glib.h>
#include "xsys.h"
float percentage(unsigned long long *free, unsigned long long *total)
{
unsigned long long result = (*free) * (unsigned long long)1000 / (*total);
return result / 10.0;
}
char *pretty_freespace(const char *desc, unsigned long long *free_k, unsigned long long *total_k)
{
char *quantities[] = { "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB", 0 };
char *result, **quantity;
double free_space, total_space;
free_space = *free_k;
total_space = *total_k;
result = g_new(char, bsize);
if (total_space == 0)
{
g_snprintf(result, bsize, "%s: none", desc);
return result;
}
quantity = quantities;
while (total_space > 1023 && *(quantity + 1))
{
quantity++;
free_space = free_space / 1024;
total_space = total_space / 1024;
}
if (sysinfo_get_percent () != 0)
g_snprintf(result, bsize, "%s: %.1f%s, %.1f%% free",
desc, total_space, *quantity,
percentage(free_k, total_k));
else
g_snprintf(result, bsize, "%s: %.1f%s/%.1f%s free",
desc, free_space, *quantity, total_space, *quantity);
return result;
}
void remove_leading_whitespace(char *buffer)
{
char *p;
if (buffer == NULL)
return;
for (p = buffer; *p && isspace (*p); p++)
;
memmove (buffer, p, strlen (p) + 1);
}
char *decruft_filename(char *buffer)
{
char *match, *match_end;
while ((match = strstr(buffer, "%20")))
{
match_end = match + 3;
*match++ = ' ';
while (*match_end)
*match++ = *match_end++;
*match = 0;
}
return buffer;
}
void find_match_char(char *buffer, char *match, char *result)
{
char *position;
remove_leading_whitespace(buffer);
if(strstr(buffer, match) == strstr(buffer, buffer))
{
position = strpbrk(buffer, delims);
if (position != NULL) {
position += 1;
strcpy(result, position);
position = strstr(result, "\n");
*(position) = '\0';
remove_leading_whitespace(result);
}
else
strcpy(result, "\0");
}
}
void find_match_double(char *buffer, char *match, double *result)
{
char *position;
remove_leading_whitespace(buffer);
if(strstr(buffer, match) == strstr(buffer, buffer))
{
position = strpbrk(buffer, delims);
if (position != NULL) {
position += 1;
*result = strtod(position, NULL);
}
else
*result = 0;
}
}
void find_match_double_hex(char *buffer, char *match, double *result)
{
char *position;
remove_leading_whitespace(buffer);
if(strstr(buffer, match) == strstr(buffer, buffer))
{
position = strpbrk(buffer, delims);
if (position != NULL) {
memcpy(position,"0x",2);
*result = strtod(position,NULL);
}
else
*result = 0;
}
}
void find_match_int(char *buffer, char *match, unsigned int *result)
{
char *position;
remove_leading_whitespace(buffer);
if(strstr(buffer, match) == strstr(buffer, buffer))
{
position = strpbrk(buffer, delims);
if (position != NULL) {
position += 1;
*result = atoi(position);
}
else
*result = 0;
}
}
void find_match_ll(char *buffer, char *match, unsigned long long *result)
{
char *position;
remove_leading_whitespace(buffer);
if(strstr(buffer, match) == strstr(buffer, buffer))
{
position = strpbrk(buffer, delims);
if (position != NULL) {
position += 1;
*result = strtoll(position, NULL, 10);
}
else
*result = 0;
}
}
void format_output(const char *arg, char *string, char *format)
{
char *pos1, *pos2, buffer[bsize];
pos1 = &format[0];
strncpy(buffer, string, bsize);
string[0] = '\0';
while((pos2 = strstr(pos1, "%")) != NULL)
{
strncat(string, pos1, (size_t)(pos2-pos1));
if(*(pos2+1) == '1')
strcat(string, arg);
else if(*(pos2+1) == '2')
strcat(string, buffer);
else if(*(pos2+1) == 'C' || *(pos2+1) == 'c')
strcat(string, "\003");
else if(*(pos2+1) == 'B' || *(pos2+1) == 'b')
strcat(string, "\002");
else if(*(pos2+1) == 'R' || *(pos2+1) == 'r')
strcat(string, "\026");
else if(*(pos2+1) == 'O' || *(pos2+1) == 'o')
strcat(string, "\017");
else if(*(pos2+1) == 'U' || *(pos2+1) == 'u')
strcat(string, "\037");
else if(*(pos2+1) == '%')
strcat(string, "%");
pos1=pos2+2;
}
strcat(string, pos1);
}
void flat_format_output(const char *arg, char *string, char *format)
{
char *pos1, *pos2, buffer[bsize];
pos1 = &format[0];
strncpy(buffer, string, bsize);
string[0] = '\0';
while((pos2 = strstr(pos1, "%")) != NULL)
{
strncat(string, pos1, (size_t)(pos2-pos1));
if(*(pos2+1) == '1')
strcat(string, arg);
else if(*(pos2+1) == '2')
strcat(string, buffer);
else if(*(pos2+1) == '%')
strcat(string, "%");
pos1=pos2+2;
}
strcat(string, pos1);
}

View File

@ -0,0 +1,263 @@
/*
* SysInfo - sysinfo plugin for HexChat
* Copyright (c) 2015 Patrick Griffis.
*
* This program is free software you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
* Some snippets based upon Textual's System Profiler plugin.
* https://github.com/Codeux-Software/Textual
*/
#import <Cocoa/Cocoa.h>
#include <sys/sysctl.h>
#include <mach/mach.h>
#include <mach/mach_host.h>
#include <mach/host_info.h>
#include <mach/mach_vm.h>
#include <glib.h>
#include "format.h"
#include "df.h"
static char *
get_os (void)
{
NSDictionary *systemversion = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"];
NSString *build = [systemversion objectForKey:@"ProductBuildVersion"];
if (!build)
return NULL;
NSString *version = [systemversion objectForKey:@"ProductUserVisibleVersion"];
if (!version)
{
[build release];
return NULL;
}
NSDictionary *profiler = [NSDictionary dictionaryWithContentsOfFile:[@"~/Library/Preferences/com.apple.SystemProfiler.plist" stringByExpandingTildeInPath]];
NSDictionary *names = [profiler objectForKey:@"OS Names"];
NSString *os_name = nil;
for (NSString *name in names)
{
if ([name hasPrefix:build])
{
os_name = [names objectForKey:name];
break;
}
}
[build release];
if (!os_name)
{
[version release];
return NULL;
}
char *ret = g_strdup_printf ("%s %s", [os_name UTF8String], [version UTF8String]);
[version release];
return ret;
}
static char *
get_os_fallback (void)
{
NSProcessInfo *info = [NSProcessInfo processInfo];
NSOperatingSystemVersion version = [info operatingSystemVersion];
return g_strdup_printf ("OS X %ld.%ld.%ld", version.majorVersion, version.minorVersion, version.patchVersion);
}
char *
sysinfo_backend_get_os(void)
{
static char *os_str = NULL;
if (!os_str)
{
os_str = get_os();
if (!os_str)
os_str = get_os_fallback();
}
return g_strdup (os_str);
}
char *
sysinfo_backend_get_disk(void)
{
gint64 total, free_space;
if (xs_parse_df (&total, &free_space))
{
return NULL;
}
return sysinfo_format_disk (total, free_space);
}
static guint64
get_free_memory (void)
{
mach_msg_type_number_t infoCount = (sizeof(vm_statistics_data_t) / sizeof(natural_t));
vm_size_t pagesize;
vm_statistics_data_t vm_stat;
host_page_size(mach_host_self(), &pagesize);
if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vm_stat, &infoCount) == KERN_SUCCESS)
return ((vm_stat.inactive_count + vm_stat.free_count) * pagesize);
return 0;
}
char *
sysinfo_backend_get_memory(void)
{
NSProcessInfo *info = [NSProcessInfo processInfo];
guint64 totalmem, freemem;
totalmem = [info physicalMemory];
if ((freemem = get_free_memory()) == 0)
return NULL;
return sysinfo_format_memory (totalmem, freemem);
}
char *
sysinfo_backend_get_cpu(void)
{
guint64 cpu_clock_uint = 0;
double cpu_clock;
char cpu_string[256];
gsize len;
gboolean giga = FALSE;
len = sizeof(cpu_string);
if (sysctlbyname ("machdep.cpu.brand_string", cpu_string, &len, NULL, 0) != 0)
return NULL;
cpu_string[sizeof(cpu_string) - 1] = '\0';
len = sizeof(cpu_clock_uint);
if (sysctlbyname("hw.cpufrequency", &cpu_clock_uint, &len, NULL, 0) < 0)
return NULL;
cpu_clock = cpu_clock_uint / 1000000;
if (cpu_clock > 1000)
{
cpu_clock /= 1000;
giga = TRUE;
}
if (giga)
return g_strdup_printf ("%s (%.2fGHz)", cpu_string, cpu_clock);
else
return g_strdup_printf ("%s (%.0fMHz)", cpu_string, cpu_clock);
}
static char *
get_gpu(void)
{
CFMutableDictionaryRef pciDevices = IOServiceMatching("IOPCIDevice");
io_iterator_t entry_iterator, serviceObject;
if (IOServiceGetMatchingServices(kIOMasterPortDefault, pciDevices, &entry_iterator) != kIOReturnSuccess)
return NULL;
GString *gpu_list = g_string_new(NULL);
while ((serviceObject = IOIteratorNext(entry_iterator)))
{
CFMutableDictionaryRef serviceDictionary;
kern_return_t status = IORegistryEntryCreateCFProperties(serviceObject, &serviceDictionary,
kCFAllocatorDefault, kNilOptions);
if (status != kIOReturnSuccess)
{
IOObjectRelease(serviceObject);
continue;
}
const void *class = CFDictionaryGetValue(serviceDictionary, @"class-code");
if (!class || *(guint32*)CFDataGetBytePtr(class) != 0x30000) /* DISPLAY_VGA */
{
CFRelease(serviceDictionary);
continue;
}
const void *model = CFDictionaryGetValue(serviceDictionary, @"model");
if (model)
{
if (CFGetTypeID(model) == CFDataGetTypeID() && CFDataGetLength(model) > 1)
{
if (gpu_list->len != 0)
g_string_append (gpu_list, ", ");
g_string_append_len (gpu_list, (const char*)CFDataGetBytePtr(model), CFDataGetLength(model) - 1);
}
}
CFRelease(serviceDictionary);
}
if (gpu_list->len == 0)
{
g_string_free (gpu_list, TRUE);
return NULL;
}
/* The string may contain nul-chars we must replace */
int i;
for (i = 0; i < gpu_list->len; i++)
{
if (gpu_list->str[i] == '\0')
gpu_list->str[i] = ' ';
}
return g_string_free (gpu_list, FALSE);
}
char *
sysinfo_backend_get_gpu(void)
{
static char *gpu_str = NULL;
if (!gpu_str)
gpu_str = get_gpu();
return g_strdup (gpu_str);
}
char *
sysinfo_backend_get_sound(void)
{
return NULL;
}
char *
sysinfo_backend_get_uptime(void)
{
NSProcessInfo *info = [NSProcessInfo processInfo];
double uptime = [info systemUptime];
return sysinfo_format_uptime ((gint64)uptime);
}
char *
sysinfo_backend_get_network(void)
{
return NULL;
}

View File

@ -0,0 +1,53 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include "sysinfo.h"
int xs_parse_df(gint64 *out_total, gint64 *out_free)
{
FILE *pipe;
char buffer[bsize];
pipe = popen("df -k -l -P", "r");
if(pipe==NULL)
return 1;
*out_total = *out_free = 0;
while(fgets(buffer, bsize, pipe) != NULL)
{
long long int avail, total;
/* Filesystem 1024-blocks Used Available Capacity Mounted-on */
if (sscanf (buffer, "%*s %lld %*lld %lld %*s %*s", &total, &avail) == 2)
{
*out_total += total;
*out_free += avail;
}
}
/* Convert to bytes */
*out_total *= 1000;
*out_free *= 1000;
pclose(pipe);
return 0;
}

View File

@ -0,0 +1,23 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef SYSINFO_SHARED_H
#define SYSINFO_SHARED_H
int xs_parse_df(gint64 *total_bytes, gint64 *free_bytes);
#endif

View File

@ -0,0 +1,33 @@
/*
* SysInfo - sysinfo plugin for HexChat
* Copyright (c) 2015 Patrick Griffis.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef SYSINFO_BACKEND_H
#define SYSINFO_BACKEND_H
char *sysinfo_backend_get_os(void);
char *sysinfo_backend_get_disk(void);
char *sysinfo_backend_get_memory(void);
char *sysinfo_backend_get_cpu(void);
char *sysinfo_backend_get_gpu(void);
char *sysinfo_backend_get_sound(void);
char *sysinfo_backend_get_uptime(void);
char *sysinfo_backend_get_network(void);
#endif

View File

@ -1,567 +1,277 @@
/* HexChat
* Copyright (c) 2011-2012 Berke Viktor.
/*
* SysInfo - sysinfo plugin for HexChat
* Copyright (c) 2012 Berke Viktor.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* xsys.c - main functions for X-Sys 2
* by mikeshoup
* Copyright (C) 2003, 2004, 2005 Michael Shoup
* Copyright (C) 2005, 2006, 2007 Tony Vroon
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdio.h>
#include <windows.h>
#include <wbemidl.h>
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "hexchat-plugin.h"
#include "sysinfo-backend.h"
#include "sysinfo.h"
static hexchat_plugin *ph; /* plugin handle */
#define _(x) hexchat_gettext(ph,x)
#define DEFAULT_ANNOUNCE TRUE
static char name[] = "SysInfo";
static hexchat_plugin *ph;
static char name[] = "Sysinfo";
static char desc[] = "Display info about your hardware and OS";
static char version[] = "1.1";
static char helptext[] = "USAGE: /SYSINFO - Sends info about your hardware and OS to current channel.";
static char version[] = "1.0";
static char sysinfo_help[] = "SysInfo Usage:\n /SYSINFO [-e|-o] [CLIENT|OS|CPU|RAM|DISK|VGA|SOUND|ETHERNET|UPTIME], print various details about your system or print a summary without arguments\n /SYSINFO SET <variable>\n";
/* Cache the info for subsequent invocations of /SYSINFO */
static int cpu_arch = 0;
static char *os_name = NULL;
static char *cpu_info = NULL;
static char *vga_name = NULL;
static int command_callback (char *word[], char *word_eol[], void *user_data);
typedef enum
typedef struct
{
QUERY_WMI_OS,
QUERY_WMI_CPU,
QUERY_WMI_VGA,
QUERY_WMI_HDD,
} QueryWmiType;
const char *name; /* Lower case name used for prefs */
const char *title; /* Used for the end formatting */
char *(*callback) (void);
gboolean def; /* Hide by default? */
} hwinfo;
void print_info (void);
int get_cpu_arch (void);
char *query_wmi (QueryWmiType mode);
char *read_os_name (IWbemClassObject *object);
char *read_cpu_info (IWbemClassObject *object);
char *read_vga_name (IWbemClassObject *object);
static char *
get_client (void)
{
return g_strdup_printf ("HexChat %s", hexchat_get_info(ph, "version"));
}
guint64 hdd_capacity;
guint64 hdd_free_space;
char *read_hdd_info (IWbemClassObject *object);
static hwinfo hwinfos[] = {
{"client", "Client", get_client},
{"os", "OS", sysinfo_backend_get_os},
{"cpu", "CPU", sysinfo_backend_get_cpu},
{"memory", "Memory", sysinfo_backend_get_memory},
{"storage", "Storage", sysinfo_backend_get_disk},
{"vga", "VGA", sysinfo_backend_get_gpu},
{"sound", "Sound", sysinfo_backend_get_sound, TRUE},
{"ethernet", "Ethernet", sysinfo_backend_get_network, TRUE},
{"uptime", "Uptime", sysinfo_backend_get_uptime},
{NULL, NULL},
};
char *get_memory_info (void);
char *bstr_to_utf8 (BSTR bstr);
guint64 variant_to_uint64 (VARIANT *variant);
static gboolean sysinfo_get_bool_pref (const char *pref, gboolean def);
int hexchat_plugin_init (hexchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg)
static gboolean
should_show_info (hwinfo info)
{
char hide_pref[32];
g_snprintf (hide_pref, sizeof(hide_pref), "hide_%s", info.name);
return !sysinfo_get_bool_pref (hide_pref, info.def);
}
static void
print_summary (gboolean announce)
{
char **strings = g_new0 (char*, G_N_ELEMENTS(hwinfos));
int i, x;
char *output;
for (i = 0, x = 0; hwinfos[i].name != NULL; i++)
{
if (should_show_info (hwinfos[i]))
{
char *str = hwinfos[i].callback();
if (str)
{
strings[x++] = g_strdup_printf ("\002%s\002: %s", hwinfos[i].title, str);
g_free (str);
}
}
}
output = g_strjoinv (" \002\342\200\242\002 ", strings);
hexchat_commandf (ph, "%s %s", announce ? "SAY" : "ECHO", output);
g_strfreev (strings);
g_free (output);
}
static void
print_info (char *info, gboolean announce)
{
int i;
for (i = 0; hwinfos[i].name != NULL; i++)
{
if (!g_ascii_strcasecmp (info, hwinfos[i].name))
{
char *str = hwinfos[i].callback();
if (str)
{
hexchat_commandf (ph, "%s \002%s\002: %s", announce ? "SAY" : "ECHO",
hwinfos[i].title, str);
g_free (str);
}
else
hexchat_print (ph, _("Sysinfo: Failed to get info. Either not supported or error."));
return;
}
}
hexchat_print (ph, _("Sysinfo: No info by that name\n"));
}
/*
* Simple wrapper for backend specific options.
* Ensure dest >= 512.
*/
int
sysinfo_get_str_pref (const char *pref, char *dest)
{
return hexchat_pluginpref_get_str (ph, pref, dest);
}
static gboolean
sysinfo_get_bool_pref (const char *pref, gboolean def)
{
int value = hexchat_pluginpref_get_int (ph, pref);
if (value != -1)
return value;
return def;
}
static void
sysinfo_set_pref_real (const char *pref, char *value, gboolean def)
{
if (value && value[0])
{
guint64 i = g_ascii_strtoull (value, NULL, 0);
hexchat_pluginpref_set_int (ph, pref, i != 0);
hexchat_printf (ph, _("Sysinfo: %s is set to: %d\n"), pref, i != 0);
}
else
{
hexchat_printf (ph, _("Sysinfo: %s is set to: %d\n"), pref,
sysinfo_get_bool_pref(pref, def));
}
}
static void
sysinfo_set_pref (char *key, char *value)
{
if (!key || !key[0])
{
hexchat_print (ph, _("Sysinfo: Valid settings are: announce and hide_* for each piece of information. e.g. hide_os. Without a value it will show current (or default) setting.\n"));
return;
}
if (!strcmp (key, "announce"))
{
sysinfo_set_pref_real (key, value, DEFAULT_ANNOUNCE);
return;
}
#ifdef HAVE_LIBPCI
else if (!strcmp (key, "pciids"))
{
if (value && value[0])
{
hexchat_pluginpref_set_str (ph, "pciids", value);
hexchat_printf (ph, _("Sysinfo: pciids is set to: %s\n"), value);
}
else
{
char buf[512];
if (hexchat_pluginpref_get_str (ph, "pciids", buf) == 0)
strcpy (buf, DEFAULT_PCIIDS);
hexchat_printf (ph, _("Sysinfo: pciids is set to: %s\n"), buf);
}
return;
}
#endif
else if (g_str_has_prefix (key, "hide_"))
{
int i;
for (i = 0; hwinfos[i].name != NULL; i++)
{
if (!strcmp (key + 5, hwinfos[i].name))
{
sysinfo_set_pref_real (key, value, hwinfos[i].def);
return;
}
}
}
hexchat_print (ph, _("Sysinfo: Invalid variable name\n"));
}
static int
sysinfo_cb (char *word[], char *word_eol[], void *userdata)
{
gboolean announce = sysinfo_get_bool_pref("announce", DEFAULT_ANNOUNCE);
int offset = 0, channel_type;
char *cmd;
/* Allow overriding global announce setting */
if (!strcmp ("-e", word[2]))
{
announce = FALSE;
offset++;
}
else if (!strcmp ("-o", word[2]))
{
announce = TRUE;
offset++;
}
/* Cannot send to server tab */
channel_type = hexchat_list_int (ph, NULL, "type");
if (channel_type != 2 /* SESS_CHANNEL */ && channel_type != 3 /* SESS_DIALOG */)
announce = FALSE;
cmd = word[2+offset];
if (!g_ascii_strcasecmp ("SET", cmd))
sysinfo_set_pref (word[3+offset], word_eol[4+offset]);
else if (!cmd || !cmd[0])
print_summary (announce);
else
print_info (cmd, announce);
return HEXCHAT_EAT_ALL;
}
int
hexchat_plugin_init (hexchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg)
{
ph = plugin_handle;
*plugin_name = name;
*plugin_desc = desc;
*plugin_version = version;
hexchat_hook_command (ph, "SYSINFO", HEXCHAT_PRI_NORM, command_callback, helptext, NULL);
hexchat_command (ph, "MENU -ishare\\system.png ADD \"Window/Send System Info\" \"SYSINFO\"");
hexchat_printf (ph, "%s plugin loaded\n", name);
hexchat_hook_command (ph, "SYSINFO", HEXCHAT_PRI_NORM, sysinfo_cb, sysinfo_help, NULL);
hexchat_command (ph, "MENU ADD \"Window/Send System Info\" \"SYSINFO\"");
hexchat_printf (ph, _("%s plugin loaded\n"), name);
return 1;
}
int hexchat_plugin_deinit (void)
int
hexchat_plugin_deinit (void)
{
g_free (os_name);
g_free (cpu_info);
g_free (vga_name);
hexchat_command (ph, "MENU DEL \"Window/Display System Info\"");
hexchat_printf (ph, "%s plugin unloaded\n", name);
hexchat_printf (ph, _("%s plugin unloaded\n"), name);
return 1;
}
static int command_callback (char *word[], char *word_eol[], void *user_data)
{
print_info ();
return HEXCHAT_EAT_HEXCHAT;
}
static void print_info (void)
{
char *memory_info;
char *hdd_info;
int channel_type;
#ifdef _WIN64
const char *build_arch = "x64";
#else
const char *build_arch = "x86";
#endif
/* Load information if not already loaded */
if (cpu_arch == 0)
{
cpu_arch = get_cpu_arch ();
}
if (os_name == NULL)
{
os_name = query_wmi (QUERY_WMI_OS);
if (os_name == NULL)
{
hexchat_printf (ph, "%s - Error while getting OS info.\n", name);
os_name = g_strdup ("Unknown");
}
}
if (cpu_info == NULL)
{
cpu_info = query_wmi (QUERY_WMI_CPU);
if (cpu_info == NULL)
{
hexchat_printf (ph, "%s - Error while getting CPU info.\n", name);
cpu_info = g_strdup ("Unknown");
}
}
if (vga_name == NULL)
{
vga_name = query_wmi (QUERY_WMI_VGA);
if (vga_name == NULL)
{
hexchat_printf (ph, "%s - Error while getting VGA info.\n", name);
vga_name = g_strdup ("Unknown");
}
}
/* Memory information is always loaded dynamically since it includes the current amount of free memory */
memory_info = get_memory_info ();
if (memory_info == NULL)
{
hexchat_printf (ph, "%s - Error while getting memory info.\n", name);
memory_info = g_strdup ("Unknown");
}
/* HDD information is always loaded dynamically since it includes the current amount of free space */
hdd_capacity = 0;
hdd_free_space = 0;
hdd_info = query_wmi (QUERY_WMI_HDD);
if (hdd_info == NULL)
{
hexchat_printf (ph, "%s - Error while getting disk info.\n", name);
hdd_info = g_strdup ("Unknown");
}
else
{
gfloat total_gb = hdd_capacity / 1000.f / 1000.f / 1000.f;
gfloat used_gb = (hdd_capacity - hdd_free_space) / 1000.f / 1000.f / 1000.f;
gfloat free_gb = hdd_free_space / 1000.f / 1000.f / 1000.f;
hdd_info = g_strdup_printf ("%.2f GB / %.2f GB (%.2f GB Free)", used_gb, total_gb, free_gb);
}
channel_type = hexchat_list_int (ph, NULL, "type");
if (channel_type == 2 /* SESS_CHANNEL */ || channel_type == 3 /* SESS_DIALOG */)
{
hexchat_commandf (
ph,
"ME ** SysInfo ** Client: HexChat %s (%s) ** OS: %s (x%d) ** CPU: %s ** RAM: %s ** VGA: %s ** HDD: %s ** Uptime: %.2f Hours **",
hexchat_get_info (ph, "version"), build_arch,
os_name, cpu_arch,
cpu_info,
memory_info,
vga_name,
hdd_info,
(float) GetTickCount64 () / 1000 / 60 / 60);
}
else
{
hexchat_printf (ph, " * Client: HexChat %s (%s)\n", hexchat_get_info (ph, "version"), build_arch);
hexchat_printf (ph, " * OS: %s (x%d)\n", os_name, cpu_arch);
hexchat_printf (ph, " * CPU: %s\n", cpu_info);
hexchat_printf (ph, " * RAM: %s\n", memory_info);
hexchat_printf (ph, " * VGA: %s\n", vga_name);
hexchat_printf (ph, " * HDD: %s\n", hdd_info);
hexchat_printf (ph, " * Uptime: %.2f Hours\n", (float) GetTickCount64 () / 1000 / 60 / 60);
}
g_free (memory_info);
}
static int get_cpu_arch (void)
{
SYSTEM_INFO si;
GetNativeSystemInfo (&si);
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
{
return 64;
}
else
{
return 86;
}
}
/* http://msdn.microsoft.com/en-us/site/aa390423 */
static char *query_wmi (QueryWmiType type)
{
GString *result = NULL;
HRESULT hr;
IWbemLocator *locator = NULL;
BSTR namespaceName = NULL;
BSTR queryLanguageName = NULL;
BSTR query = NULL;
IWbemServices *namespace = NULL;
IUnknown *namespaceUnknown = NULL;
IEnumWbemClassObject *enumerator = NULL;
int i;
gboolean atleast_one_appended = FALSE;
hr = CoCreateInstance (&CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, &IID_IWbemLocator, (LPVOID *) &locator);
if (FAILED (hr))
{
goto exit;
}
namespaceName = SysAllocString (L"root\\CIMV2");
hr = locator->lpVtbl->ConnectServer (locator, namespaceName, NULL, NULL, NULL, 0, NULL, NULL, &namespace);
if (FAILED (hr))
{
goto release_locator;
}
hr = namespace->lpVtbl->QueryInterface (namespace, &IID_IUnknown, &namespaceUnknown);
if (FAILED (hr))
{
goto release_namespace;
}
hr = CoSetProxyBlanket (namespaceUnknown, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE);
if (FAILED (hr))
{
goto release_namespaceUnknown;
}
queryLanguageName = SysAllocString (L"WQL");
switch (type)
{
case QUERY_WMI_OS:
query = SysAllocString (L"SELECT Caption FROM Win32_OperatingSystem");
break;
case QUERY_WMI_CPU:
query = SysAllocString (L"SELECT Name, MaxClockSpeed FROM Win32_Processor");
break;
case QUERY_WMI_VGA:
query = SysAllocString (L"SELECT Name FROM Win32_VideoController");
break;
case QUERY_WMI_HDD:
query = SysAllocString (L"SELECT Name, Capacity, FreeSpace FROM Win32_Volume");
break;
default:
goto release_queryLanguageName;
}
hr = namespace->lpVtbl->ExecQuery (namespace, queryLanguageName, query, WBEM_FLAG_FORWARD_ONLY, NULL, &enumerator);
if (FAILED (hr))
{
goto release_query;
}
result = g_string_new ("");
for (i = 0;; i++)
{
ULONG numReturned = 0;
IWbemClassObject *object;
char *line;
hr = enumerator->lpVtbl->Next (enumerator, WBEM_INFINITE, 1, &object, &numReturned);
if (FAILED (hr) || numReturned == 0)
{
break;
}
switch (type)
{
case QUERY_WMI_OS:
line = read_os_name (object);
break;
case QUERY_WMI_CPU:
line = read_cpu_info (object);
break;
case QUERY_WMI_VGA:
line = read_vga_name (object);
break;
case QUERY_WMI_HDD:
line = read_hdd_info (object);
break;
default:
break;
}
object->lpVtbl->Release (object);
if (line != NULL)
{
if (atleast_one_appended)
{
g_string_append (result, ", ");
}
g_string_append (result, line);
g_free (line);
atleast_one_appended = TRUE;
}
}
enumerator->lpVtbl->Release (enumerator);
release_query:
SysFreeString (query);
release_queryLanguageName:
SysFreeString (queryLanguageName);
release_namespaceUnknown:
namespaceUnknown->lpVtbl->Release (namespaceUnknown);
release_namespace:
namespace->lpVtbl->Release (namespace);
release_locator:
locator->lpVtbl->Release (locator);
SysFreeString (namespaceName);
exit:
if (result == NULL)
{
return NULL;
}
return g_string_free (result, FALSE);
}
static char *read_os_name (IWbemClassObject *object)
{
HRESULT hr;
VARIANT caption_variant;
char *caption_utf8;
hr = object->lpVtbl->Get (object, L"Caption", 0, &caption_variant, NULL, NULL);
if (FAILED (hr))
{
return NULL;
}
caption_utf8 = bstr_to_utf8 (caption_variant.bstrVal);
VariantClear(&caption_variant);
if (caption_utf8 == NULL)
{
return NULL;
}
g_strchomp (caption_utf8);
return caption_utf8;
}
static char *read_cpu_info (IWbemClassObject *object)
{
HRESULT hr;
VARIANT name_variant;
char *name_utf8;
VARIANT max_clock_speed_variant;
guint cpu_freq_mhz;
char *result;
hr = object->lpVtbl->Get (object, L"Name", 0, &name_variant, NULL, NULL);
if (FAILED (hr))
{
return NULL;
}
name_utf8 = bstr_to_utf8 (name_variant.bstrVal);
VariantClear (&name_variant);
if (name_utf8 == NULL)
{
return NULL;
}
hr = object->lpVtbl->Get (object, L"MaxClockSpeed", 0, &max_clock_speed_variant, NULL, NULL);
if (FAILED (hr))
{
g_free (name_utf8)