summaryrefslogtreecommitdiff
path: root/hardinfo2/arch/linux/common/resources.h
blob: bd3fc221e3d7d9995209ee033b323dc7abd5511e (plain)
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
/*
 *    HardInfo - Displays System Information
 *    Copyright (C) 2003-2008 Leandro A. F. Pereira <leandro@hardinfo.org>
 *
 *    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, version 2.
 *
 *    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
 */

gchar *_resources = NULL;

#if GLIB_CHECK_VERSION(2,14,0)
static GRegex *_regex_pci = NULL,
              *_regex_module = NULL;

static gchar *_resource_obtain_name(gchar *name)
{
    gchar *temp;

    if (!_regex_pci && !_regex_module) {
      _regex_pci = g_regex_new("^[0-9a-fA-F]{4}:[0-9a-fA-F]{2}:"
                               "[0-9a-fA-F]{2}\\.[0-9a-fA-F]{1}$",
                               0, 0, NULL);
      _regex_module = g_regex_new("^[0-9a-zA-Z\\_\\-]+$", 0, 0, NULL);
    }
    
    name = g_strstrip(name);
    
    if (g_regex_match(_regex_pci, name, 0, NULL)) {
      temp = module_call_method_param("devices::getPCIDeviceDescription", name);
      if (temp) {
          return temp;
      }
    } else if (g_regex_match(_regex_module, name, 0, NULL)) {
      temp = module_call_method_param("computer::getKernelModuleDescription", name);
      if (temp) {
          return temp;
      }
    }
    
    return g_strdup(name);
}
#else
static gchar *_resource_obtain_name(gchar *name)
{
    return name;
}
#endif

void scan_device_resources(gboolean reload)
{
    SCAN_START();
    FILE *io;
    gchar buffer[256];
    
    g_free(_resources);
    _resources = g_strdup("");
    
    if ((io = fopen("/proc/ioports", "r"))) {
      _resources = h_strconcat(_resources, "[I/O Ports]\n", NULL);

      while (fgets(buffer, 256, io)) {
        gchar **temp = g_strsplit(buffer, ":", 2);
        gchar *name = _resource_obtain_name(temp[1]);
        
        _resources = h_strdup_cprintf("%s=%s\n", _resources,
                                      temp[0], name);
        
        g_strfreev(temp);
        g_free(name);
      }

      fclose(io);
    }
    
    if ((io = fopen("/proc/iomem", "r"))) {
      _resources = h_strconcat(_resources, "[Memory]\n", NULL);

      while (fgets(buffer, 256, io)) {
        gchar **temp = g_strsplit(buffer, ":", 2);
        gchar *name = _resource_obtain_name(temp[1]);
        
        _resources = h_strdup_cprintf("%s=%s\n", _resources,
                                      temp[0], name);
        
        g_strfreev(temp);
        g_free(name);
      }

      fclose(io);
    }
    
    if ((io = fopen("/proc/dma", "r"))) {
      _resources = h_strconcat(_resources, "[DMA]\n", NULL);

      while (fgets(buffer, 256, io)) {
        gchar **temp = g_strsplit(buffer, ":", 2);
        gchar *name = _resource_obtain_name(temp[1]);
        
        _resources = h_strdup_cprintf("%s=%s\n", _resources,
                                      temp[0], name);
        
        g_strfreev(temp);
        g_free(name);
      }

      fclose(io);
    }    
    
    SCAN_END();
}

gchar *callback_device_resources(void)
{
    return _resources;
}