summary refs log tree commit diff stats
path: root/plugins/sysinfo/unix/backend.c
blob: 9d886f09e89ed16633f08a7fed1009cbed6a583c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * 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>
#include "parse.h"
#include "match.h"
#include "sysinfo.h"
#include "format.h"
#include "df.h"

char *sysinfo_backend_get_os(void)
{
	char name[bsize];

	if (xs_parse_distro (name) != 0)
	{
		return NULL;
	}

	return g_strdup(name);
}

char *sysinfo_backend_get_disk(void)
{
	gint64 total, free;

	if (xs_parse_df (&total, &free))
	{
		return NULL;
	}

	return sysinfo_format_disk (total, free);
}

char *sysinfo_backend_get_memory(void)
{
	unsigned long long mem_total;
	unsigned long long mem_free;
	unsigned long long swap_total;
	unsigned long long swap_free;
	char *swap_fmt = NULL, *mem_fmt, *ret;

	if (xs_parse_meminfo (&mem_total, &mem_free, 0) == 1)
	{
		return NULL;
	}
	if (xs_parse_meminfo (&swap_total, &swap_free, 1) != 1)
	{
		swap_fmt = sysinfo_format_memory (swap_total, swap_free);
	}

	mem_fmt = sysinfo_format_memory (mem_total, mem_free);

	if (swap_fmt && swap_total != 0)
	{
		ret = g_strdup_printf ("Physical: %s Swap: %s", mem_fmt, swap_fmt);
		g_free (mem_fmt);
		g_free (swap_fmt);
	}
	else
		ret = mem_fmt;

	return ret;
}

char *sysinfo_backend_get_cpu(void)
{
	char model[bsize];
	char vendor[bsize];
	char buffer[bsize];
	double freq;
	int giga = 0;

	if (xs_parse_cpu (model, vendor, &freq) != 0)
	{
		return NULL;
	}

	if (freq > 1000)
	{
		freq /= 1000;
		giga = 1;
	}

	if (giga)
	{
		g_snprintf (buffer, bsize, "%s (%.2fGHz)", model, freq);
	}
	else
	{
		g_snprintf (buffer, bsize, "%s (%.0fMHz)", model, freq);
	}
	
	return g_strdup (buffer);
}

char *sysinfo_backend_get_gpu(void)
{
	char vid_card[bsize];
	char agp_bridge[bsize];
	char buffer[bsize];
	int ret;

	if ((ret = xs_parse_video (vid_card)) != 0)
	{
		return NULL;
	}

	if (xs_parse_agpbridge (agp_bridge) != 0)
	{
		g_snprintf (buffer, bsize, "%s", vid_card);
	}
	else
	{
		g_snprintf (buffer, bsize, "%s @ %s", vid_card, agp_bridge);
	}

	return g_strdup (buffer);
}

char *sysinfo_backend_get_sound(void)
{
	char sound[bsize];

	if (xs_parse_sound (sound) != 0)
	{
		return NULL;
	}
	return g_strdup (sound);
}

char *sysinfo_backend_get_uptime(void)
{
	gint64 uptime;

	if ((uptime = xs_parse_uptime ()) == 0)
	{
		return NULL;
	}

	return sysinfo_format_uptime (uptime);
}

char *sysinfo_backend_get_network(void)
{
	char ethernet_card[bsize];

	if (xs_parse_ether (ethernet_card))
	{
		g_strlcpy (ethernet_card, "None found", bsize);
	}
	
	return g_strdup (ethernet_card);
}