diff options
author | Wu Xiaotian <[email protected]> | 2020-10-12 20:57:13 +0800 |
---|---|---|
committer | Xiaotian Wu <[email protected]> | 2023-10-05 23:05:54 +0800 |
commit | eb483646daff28c3ac58de10e4b850fc7e0a5dad (patch) | |
tree | 30fb26bfbf943aa1101a2111f67a412fd089326e | |
parent | e174117cb48ec90b041a2faa28016964c5a0b92b (diff) | |
download | mate-desktop-dev-dmi.tar.bz2 mate-desktop-dev-dmi.tar.xz |
mate-dmi: add new api to get dmi infodev-dmi
-rw-r--r-- | libmate-desktop/Makefile.am | 10 | ||||
-rw-r--r-- | libmate-desktop/mate-dmi.c | 163 | ||||
-rw-r--r-- | libmate-desktop/mate-dmi.h | 43 | ||||
-rw-r--r-- | libmate-desktop/meson.build | 8 | ||||
-rw-r--r-- | libmate-desktop/test-dmi.c | 30 |
5 files changed, 253 insertions, 1 deletions
diff --git a/libmate-desktop/Makefile.am b/libmate-desktop/Makefile.am index 44b8b9b..726841c 100644 --- a/libmate-desktop/Makefile.am +++ b/libmate-desktop/Makefile.am @@ -4,6 +4,7 @@ libmate_desktop_HEADERS = \ mate-desktop-utils.h \ mate-desktop-item.h \ mate-dconf.h \ + mate-dmi.h \ mate-gsettings.h \ mate-bg.h \ mate-bg-crossfade.h \ @@ -32,7 +33,7 @@ AM_CPPFLAGS = \ AM_CFLAGS = $(WARN_CFLAGS) -noinst_PROGRAMS = test-desktop-thumbnail test-ditem test test-languages test-image-menu-item +noinst_PROGRAMS = test-desktop-thumbnail test-ditem test test-languages test-image-menu-item test-dmi CLEANFILES = @@ -40,6 +41,7 @@ introspection_sources = \ mate-desktop-utils.c \ mate-desktop-thumbnail.c \ mate-dconf.c \ + mate-dmi.c \ mate-gsettings.c \ mate-bg.c \ mate-bg-crossfade.c \ @@ -106,6 +108,12 @@ test_image_menu_item_LDADD = \ libmate-desktop-2.la \ $(MATE_DESKTOP_LIBS) +test_dmi_SOURCES = test-dmi.c + +test_dmi_LDADD = \ + libmate-desktop-2.la \ + $(MATE_DESKTOP_LIBS) + pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = mate-desktop-2.0.pc diff --git a/libmate-desktop/mate-dmi.c b/libmate-desktop/mate-dmi.c new file mode 100644 index 0000000..085620f --- /dev/null +++ b/libmate-desktop/mate-dmi.c @@ -0,0 +1,163 @@ +/* + * Copyright (C) 2009-2011 Richard Hughes <[email protected]> + * + * Licensed under the GNU General Public License Version 2 + * + * 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. + */ + +#define MATE_DESKTOP_USE_UNSTABLE_API + +#include <glib-object.h> +#include <math.h> +#include <string.h> +#include <gio/gio.h> +#include <stdlib.h> + +#include "mate-dmi.h" + +static void mate_dmi_finalize (GObject *object); + +struct _MateDmi +{ + GObject parent; + gchar *name; + gchar *version; + gchar *vendor; +}; + +static gpointer mate_dmi_object = NULL; + +G_DEFINE_TYPE (MateDmi, mate_dmi, G_TYPE_OBJECT) + +static gchar * mate_dmi_get_from_filename (const gchar *filename) +{ + gboolean ret; + GError *error = NULL; + gchar *data = NULL; + + /* get the contents */ + ret = g_file_get_contents (filename, &data, NULL, &error); + if (!ret) { + if (!g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT)) + g_warning ("failed to get contents of %s: %s", filename, error->message); + g_error_free (error); + } + + /* process the random chars and trailing spaces */ + if (data != NULL) { + g_strdelimit (data, "\t_", ' '); + g_strdelimit (data, "\n\r", '\0'); + g_strchomp (data); + } + + /* don't return an empty string */ + if (data != NULL && data[0] == '\0') { + g_free (data); + data = NULL; + } + + return data; +} + +static gchar * mate_dmi_get_from_filenames (const gchar * const * filenames) +{ + guint i; + gchar *tmp = NULL; + + /* try each one in preference order */ + for (i = 0; filenames[i] != NULL; i++) { + tmp = mate_dmi_get_from_filename (filenames[i]); + if (tmp != NULL) + break; + } + return tmp; +} + +const gchar * mate_dmi_get_name (MateDmi *dmi) +{ + g_return_val_if_fail (MATE_IS_DMI (dmi), NULL); + return dmi->name; +} + +const gchar * mate_dmi_get_version (MateDmi *dmi) +{ + g_return_val_if_fail (MATE_IS_DMI (dmi), NULL); + return dmi->version; +} + +const gchar * mate_dmi_get_vendor (MateDmi *dmi) +{ + g_return_val_if_fail (MATE_IS_DMI (dmi), NULL); + return dmi->vendor; +} + +static void mate_dmi_class_init (MateDmiClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + object_class->finalize = mate_dmi_finalize; +} + +static void mate_dmi_init (MateDmi *dmi) +{ +#if defined(__linux__) + const gchar *sysfs_name[] = { + "/sys/class/dmi/id/product_name", + "/sys/class/dmi/id/board_name", + NULL}; + const gchar *sysfs_version[] = { + "/sys/class/dmi/id/product_version", + "/sys/class/dmi/id/chassis_version", + "/sys/class/dmi/id/board_version", + NULL}; + const gchar *sysfs_vendor[] = { + "/sys/class/dmi/id/sys_vendor", + "/sys/class/dmi/id/chassis_vendor", + "/sys/class/dmi/id/board_vendor", + NULL}; +#else +#warning Please add dmi support for your OS + const gchar *sysfs_name[] = { NULL }; + const gchar *sysfs_version[] = { NULL }; + const gchar *sysfs_vendor[] = { NULL }; +#endif + + /* get all the possible data now */ + dmi->name = mate_dmi_get_from_filenames (sysfs_name); + dmi->version = mate_dmi_get_from_filenames (sysfs_version); + dmi->vendor = mate_dmi_get_from_filenames (sysfs_vendor); +} + +static void mate_dmi_finalize (GObject *object) +{ + MateDmi *dmi = MATE_DMI (object); + + g_free (dmi->name); + g_free (dmi->version); + g_free (dmi->vendor); + + G_OBJECT_CLASS (mate_dmi_parent_class)->finalize (object); +} + +MateDmi* mate_dmi_new (void) +{ + if (mate_dmi_object != NULL) { + g_object_ref (mate_dmi_object); + } else { + mate_dmi_object = g_object_new (MATE_TYPE_DMI, NULL); + g_object_add_weak_pointer (mate_dmi_object, &mate_dmi_object); + } + return MATE_DMI (mate_dmi_object); +} diff --git a/libmate-desktop/mate-dmi.h b/libmate-desktop/mate-dmi.h new file mode 100644 index 0000000..e5b6ea2 --- /dev/null +++ b/libmate-desktop/mate-dmi.h @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2009-2010 Richard Hughes <[email protected]> + * + * Licensed under the GNU General Public License Version 2 + * + * 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. + */ + +#ifndef __MATE_DMI_H +#define __MATE_DMI_H + +#ifndef MATE_DESKTOP_USE_UNSTABLE_API +#error This is unstable API. You must define MATE_DESKTOP_USE_UNSTABLE_API before including mate-dmi.h +#endif + +#include <glib-object.h> + +G_BEGIN_DECLS + +#define MATE_TYPE_DMI (mate_dmi_get_type ()) +G_DECLARE_FINAL_TYPE (MateDmi, mate_dmi, MATE, DMI, GObject) + +MateDmi *mate_dmi_new (void); + +const gchar *mate_dmi_get_name (MateDmi *dmi); +const gchar *mate_dmi_get_vendor (MateDmi *dmi); +const gchar *mate_dmi_get_version (MateDmi *dmi); + +G_END_DECLS + +#endif /* __MATE_DMI_H */ diff --git a/libmate-desktop/meson.build b/libmate-desktop/meson.build index b11f242..f0e3ec8 100644 --- a/libmate-desktop/meson.build +++ b/libmate-desktop/meson.build @@ -5,6 +5,7 @@ headers = files( 'mate-desktop-utils.h', 'mate-desktop-item.h', 'mate-dconf.h', + 'mate-dmi.h', 'mate-gsettings.h', 'mate-bg.h', 'mate-bg-crossfade.h', @@ -30,6 +31,7 @@ sources = files( 'mate-desktop-utils.c', 'mate-desktop-thumbnail.c', 'mate-dconf.c', + 'mate-dmi.c', 'mate-gsettings.c', 'mate-bg.c', 'mate-bg-crossfade.c', @@ -168,3 +170,9 @@ test_image_menu_item = executable('test-image-menu-item', dependencies : libmate_desktop_dep, install : false, ) + +test_dmi = executable('test-dmi', + sources : 'test-dmi.c', + dependencies : libmate_desktop_dep, + install : false, +) diff --git a/libmate-desktop/test-dmi.c b/libmate-desktop/test-dmi.c new file mode 100644 index 0000000..96ce299 --- /dev/null +++ b/libmate-desktop/test-dmi.c @@ -0,0 +1,30 @@ +/* vi: set sw=4 ts=4 wrap ai: */ +/* + * 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 <stdio.h> +#define MATE_DESKTOP_USE_UNSTABLE_API +#include "mate-dmi.h" + +int main(int argc, char **argv) +{ + MateDmi *dmi; + dmi = mate_dmi_new (); + printf("dmi name: %s\n", mate_dmi_get_name (dmi)); + printf("dmi version: %s\n", mate_dmi_get_version (dmi)); + printf("dmi vendor: %s\n", mate_dmi_get_vendor (dmi)); + return 0; +} |