~ps-jenkins/unity-settings-daemon/ubuntu-vivid-proposed

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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2009-2011 Richard Hughes <richard@hughsie.com>
 *
 * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <glib-object.h>

#include "gsd-pnp-ids.h"

static void gsd_pnp_ids_finalize (GObject *object);

#define GSD_PNP_IDS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GSD_TYPE_PNP_IDS, GsdPnpIdsPrivate))

struct _GsdPnpIdsPrivate
{
        gchar      *table_data;
        GHashTable *pnp_table;
};

static gpointer gsd_pnp_ids_object = NULL;

G_DEFINE_TYPE (GsdPnpIds, gsd_pnp_ids, G_TYPE_OBJECT)

typedef struct Vendor Vendor;
struct Vendor
{
    const char vendor_id[4];
    const char vendor_name[28];
};

/* This list of vendor codes derived from lshw
 *
 * http://ezix.org/project/wiki/HardwareLiSter
 *
 * Note: we now prefer to use data coming from hwdata (and shipped with
 * gnome-desktop). See
 * http://git.fedorahosted.org/git/?p=hwdata.git;a=blob_plain;f=pnp.ids;hb=HEAD
 * All contributions to the list of vendors should go there.
 */
static const struct Vendor vendors[] =
{
    { "AIC", "AG Neovo" },
    { "ACR", "Acer" },
    { "DEL", "DELL" },
    { "SAM", "SAMSUNG" },
    { "SNY", "SONY" },
    { "SEC", "Epson" },
    { "WAC", "Wacom" },
    { "NEC", "NEC" },
    { "CMO", "CMO" },        /* Chi Mei */
    { "BNQ", "BenQ" },

    { "ABP", "Advansys" },
    { "ACC", "Accton" },
    { "ACE", "Accton" },
    { "ADP", "Adaptec" },
    { "ADV", "AMD" },
    { "AIR", "AIR" },
    { "AMI", "AMI" },
    { "ASU", "ASUS" },
    { "ATI", "ATI" },
    { "ATK", "Allied Telesyn" },
    { "AZT", "Aztech" },
    { "BAN", "Banya" },
    { "BRI", "Boca Research" },
    { "BUS", "Buslogic" },
    { "CCI", "Cache Computers Inc." },
    { "CHA", "Chase" },
    { "CMD", "CMD Technology, Inc." },
    { "COG", "Cogent" },
    { "CPQ", "Compaq" },
    { "CRS", "Crescendo" },
    { "CSC", "Crystal" },
    { "CSI", "CSI" },
    { "CTL", "Creative Labs" },
    { "DBI", "Digi" },
    { "DEC", "Digital Equipment" },
    { "DBK", "Databook" },
    { "EGL", "Eagle Technology" },
    { "ELS", "ELSA" },
    { "ESS", "ESS" },
    { "FAR", "Farallon" },
    { "FDC", "Future Domain" },
    { "HWP", "Hewlett-Packard" },
    { "IBM", "IBM" },
    { "INT", "Intel" },
    { "ISA", "Iomega" },
    { "LEN", "Lenovo" },
    { "MDG", "Madge" },
    { "MDY", "Microdyne" },
    { "MET", "Metheus" },
    { "MIC", "Micronics" },
    { "MLX", "Mylex" },
    { "NVL", "Novell" },
    { "OLC", "Olicom" },
    { "PRO", "Proteon" },
    { "RII", "Racal" },
    { "RTL", "Realtek" },
    { "SCM", "SCM" },
    { "SKD", "SysKonnect" },
    { "SGI", "SGI" },
    { "SMC", "SMC" },
    { "SNI", "Siemens Nixdorf" },
    { "STL", "Stallion Technologies" },
    { "SUN", "Sun" },
    { "SUP", "SupraExpress" },
    { "SVE", "SVEC" },
    { "TCC", "Thomas-Conrad" },
    { "TCI", "Tulip" },
    { "TCM", "3Com" },
    { "TCO", "Thomas-Conrad" },
    { "TEC", "Tecmar" },
    { "TRU", "Truevision" },
    { "TOS", "Toshiba" },
    { "TYN", "Tyan" },
    { "UBI", "Ungermann-Bass" },
    { "USC", "UltraStor" },
    { "VDM", "Vadem" },
    { "VMI", "Vermont" },
    { "WDC", "Western Digital" },
    { "ZDS", "Zeos" },

    /* From http://faydoc.tripod.com/structures/01/0136.htm */
    { "ACT", "Targa" },
    { "ADI", "ADI" },
    { "AOC", "AOC Intl" },
    { "API", "Acer America" },
    { "APP", "Apple Computer" },
    { "ART", "ArtMedia" },
    { "AST", "AST Research" },
    { "CPL", "Compal" },
    { "CTX", "Chuntex Electronic Co." },
    { "DPC", "Delta Electronics" },
    { "DWE", "Daewoo" },
    { "ECS", "ELITEGROUP" },
    { "EIZ", "EIZO" },
    { "FCM", "Funai" },
    { "GSM", "LG Electronics" },
    { "GWY", "Gateway 2000" },
    { "HEI", "Hyundai" },
    { "HIT", "Hitachi" },
    { "HSL", "Hansol" },
    { "HTC", "Hitachi" },
    { "ICL", "Fujitsu ICL" },
    { "IVM", "Idek Iiyama" },
    { "KFC", "KFC Computek" },
    { "LKM", "ADLAS" },
    { "LNK", "LINK Tech" },
    { "LTN", "Lite-On" },
    { "MAG", "MAG InnoVision" },
    { "MAX", "Maxdata" },
    { "MEI", "Panasonic" },
    { "MEL", "Mitsubishi" },
    { "MIR", "miro" },
    { "MTC", "MITAC" },
    { "NAN", "NANAO" },
    { "NEC", "NEC Tech" },
    { "NOK", "Nokia" },
    { "OQI", "OPTIQUEST" },
    { "PBN", "Packard Bell" },
    { "PGS", "Princeton" },
    { "PHL", "Philips" },
    { "REL", "Relisys" },
    { "SDI", "Samtron" },
    { "SMI", "Smile" },
    { "SPT", "Sceptre" },
    { "SRC", "Shamrock Technology" },
    { "STP", "Sceptre" },
    { "TAT", "Tatung" },
    { "TRL", "Royal Information Company" },
    { "TSB", "Toshiba, Inc." },
    { "UNM", "Unisys" },
    { "VSC", "ViewSonic" },
    { "WTC", "Wen Tech" },
    { "ZCM", "Zenith Data Systems" },

    { "???", "Unknown" },
};

static gboolean
gsd_pnp_ids_load (GsdPnpIds *pnp_ids, GError **error)
{
        gchar *retval = NULL;
        GsdPnpIdsPrivate *priv = pnp_ids->priv;
        guint i;

        /* load the contents */
        g_debug ("loading: %s", PNP_IDS);
        if (g_file_get_contents (PNP_IDS, &priv->table_data, NULL, error) == FALSE)
                return FALSE;

        /* parse into lines */
        retval = priv->table_data;
        for (i = 0; priv->table_data[i] != '\0'; i++) {

                /* ignore */
                if (priv->table_data[i] != '\n')
                        continue;

                /* convert newline to NULL */
                priv->table_data[i] = '\0';

                /* the ID to text is a fixed offset */
                if (retval[0] && retval[1] && retval[2] && retval[3] == '\t' && retval[4]) {
                        retval[3] = '\0';
                        g_hash_table_insert (priv->pnp_table,
                                             retval,
                                             retval+4);
                        retval = &priv->table_data[i+1];
                }
        }

        g_debug ("Added %i items to the vendor hashtable", i);

        return TRUE;
}

static const char *
find_vendor (const char *pnp_id)
{
        guint i;

        for (i = 0; i < G_N_ELEMENTS (vendors); i++) {
                if (g_strcmp0 (vendors[i].vendor_id, pnp_id) == 0)
                        return vendors[i].vendor_name;
        }

        return NULL;
}

/**
 * gsd_pnp_ids_get_pnp_id:
 * @pnp_ids: a #GsdPnpIds object
 * @pnp_id: the PNP ID to look for
 *
 * Find the full manufacturer name for the given PNP ID.
 *
 * Returns: (transfer full): a new string representing the manufacturer name,
 * or %NULL when not found.
 */
gchar *
gsd_pnp_ids_get_pnp_id (GsdPnpIds *pnp_ids, const gchar *pnp_id)
{
        GsdPnpIdsPrivate *priv = pnp_ids->priv;
        const char *found;
        GError *error = NULL;
        guint size;

        g_return_val_if_fail (GSD_IS_PNP_IDS (pnp_ids), NULL);
        g_return_val_if_fail (pnp_id != NULL, NULL);

        /* if table is empty, try to load it */
        size = g_hash_table_size (priv->pnp_table);
        if (size == 0) {
                if (gsd_pnp_ids_load (pnp_ids, &error) == FALSE) {
                        g_warning ("Failed to load PNP ids: %s", error->message);
                        g_error_free (error);
                        return NULL;
                }
        }

        /* look this up in the table */
        found = g_hash_table_lookup (priv->pnp_table, pnp_id);
        if (found == NULL) {
                found = find_vendor (pnp_id);
                if (found == NULL)
                        return NULL;
        }

        return g_strdup (found);
}

static void
gsd_pnp_ids_class_init (GsdPnpIdsClass *klass)
{
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
        object_class->finalize = gsd_pnp_ids_finalize;
        g_type_class_add_private (klass, sizeof (GsdPnpIdsPrivate));
}

static void
gsd_pnp_ids_init (GsdPnpIds *pnp_ids)
{
        pnp_ids->priv = GSD_PNP_IDS_GET_PRIVATE (pnp_ids);

        /* we don't keep malloc'd data in the hash; instead we read it
         * out into priv->table_data and then link to it in the hash */
        pnp_ids->priv->pnp_table = g_hash_table_new_full (g_str_hash,
                                                         g_str_equal,
                                                         NULL,
                                                         NULL);
}

static void
gsd_pnp_ids_finalize (GObject *object)
{
        GsdPnpIds *pnp_ids = GSD_PNP_IDS (object);
        GsdPnpIdsPrivate *priv = pnp_ids->priv;

        g_free (priv->table_data);
        g_hash_table_unref (priv->pnp_table);

        G_OBJECT_CLASS (gsd_pnp_ids_parent_class)->finalize (object);
}

/**
 * gsd_pnp_ids_new:
 *
 * Returns a reference to a #GsdPnpIds object, or creates
 * a new one if none have been created.
 *
 * Returns: (transfer full): a #GsdPnpIds object.
 */
GsdPnpIds *
gsd_pnp_ids_new (void)
{
        if (gsd_pnp_ids_object != NULL) {
                g_object_ref (gsd_pnp_ids_object);
        } else {
                gsd_pnp_ids_object = g_object_new (GSD_TYPE_PNP_IDS, NULL);
                g_object_add_weak_pointer (gsd_pnp_ids_object, &gsd_pnp_ids_object);
        }
        return GSD_PNP_IDS (gsd_pnp_ids_object);
}