### Full Test Suite Example with Fixtures Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/testing.md This example demonstrates creating a test suite with two tests that utilize fixtures for setup and teardown. It includes custom fixture structures and functions for managing test environments. ```c #include #include typedef struct { MyObject *obj; OtherObject *helper; } MyObjectFixture; static void my_object_fixture_set_up (MyObjectFixture *fixture, gconstpointer user_data) { fixture->obj = my_object_new (); my_object_set_prop1 (fixture->obj, "some-value"); my_object_do_some_complex_setup (fixture->obj, user_data); fixture->helper = other_object_new (); } static void my_object_fixture_tear_down (MyObjectFixture *fixture, gconstpointer user_data) { g_clear_object (&fixture->helper); g_clear_object (&fixture->obj); } static void test_my_object_test1 (MyObjectFixture *fixture, gconstpointer user_data) { g_assert_cmpstr (my_object_get_property (fixture->obj), ==, "initial-value"); } static void test_my_object_test2 (MyObjectFixture *fixture, gconstpointer user_data) { my_object_do_some_work_using_helper (fixture->obj, fixture->helper); g_assert_cmpstr (my_object_get_property (fixture->obj), ==, "updated-value"); } int main (int argc, char *argv[]) { setlocale (LC_ALL, ""); g_test_init (&argc, &argv, NULL); // Define the tests. g_test_add ("/my-object/test1", MyObjectFixture, "some-user-data", my_object_fixture_set_up, test_my_object_test1, my_object_fixture_tear_down); g_test_add ("/my-object/test2", MyObjectFixture, "some-user-data", my_object_fixture_set_up, test_my_object_test2, my_object_fixture_tear_down); return g_test_run (); } ``` -------------------------------- ### GOption Example Setup and Parsing Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/goption.md Demonstrates how to set up GOption entries, create a context, add entries and groups, and parse commandline arguments. Requires GLib and GTK. ```c static gint repeats = 2; static gint max_size = 8; static gboolean verbose = FALSE; static gboolean beep = FALSE; static gboolean randomize = FALSE; static GOptionEntry entries[] = { { "repeats", 'r', 0, G_OPTION_ARG_INT, &repeats, "Average over N repetitions", "N" }, { "max-size", 'm', 0, G_OPTION_ARG_INT, &max_size, "Test up to 2^M items", "M" }, { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL }, { "beep", 'b', 0, G_OPTION_ARG_NONE, &beep, "Beep when done", NULL }, { "rand", 0, 0, G_OPTION_ARG_NONE, &randomize, "Randomize the data", NULL }, G_OPTION_ENTRY_NULL }; int main (int argc, char *argv[]) { GError *error = NULL; GOptionContext *context; context = g_option_context_new ("- test tree model performance"); g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); g_option_context_add_group (context, gtk_get_option_group (TRUE)); if (!g_option_context_parse (context, &argc, &argv, &error)) { g_print ("option parsing failed: %s\n", error->message); exit (1); } ... ``` -------------------------------- ### GValue Initialization and Usage Example Source: https://github.com/gnome/glib/blob/main/docs/reference/gobject/gvalue.md Demonstrates initializing, setting, getting, and unsetting GValues for different types (string, int). Shows type transformation capabilities. ```c #include static void int2string ( const GValue *src_value, GValue *dest_value) { if (g_value_get_int (src_value) == 42) g_value_set_static_string (dest_value, "An important number"); else g_value_set_static_string (dest_value, "What's that?"); } int main (int argc, char *argv[]) { // GValues must be initialized GValue a = G_VALUE_INIT; GValue b = G_VALUE_INIT; const char *message; // The GValue starts empty g_assert (!G_VALUE_HOLDS_STRING (&a)); // Put a string in it g_value_init (&a, G_TYPE_STRING); g_assert (G_VALUE_HOLDS_STRING (&a)); g_value_set_static_string (&a, "Hello, world!"); g_printf ("%s\n", g_value_get_string (&a)); // Reset it to its pristine state g_value_unset (&a); // It can then be reused for another type g_value_init (&a, G_TYPE_INT); g_value_set_int (&a, 42); // Attempt to transform it into a GValue of type STRING g_value_init (&b, G_TYPE_STRING); // An INT is transformable to a STRING g_assert (g_value_type_transformable (G_TYPE_INT, G_TYPE_STRING)); g_value_transform (&a, &b); g_printf ("%s\n", g_value_get_string (&b)); // Attempt to transform it again using a custom transform function g_value_register_transform_func (G_TYPE_INT, G_TYPE_STRING, int2string); g_value_transform (&a, &b); g_printf ("%s\n", g_value_get_string (&b)); g_value_unset (&b); g_value_unset (&a); return 0; } ``` -------------------------------- ### Standard GLib Build and Install Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/building.md Use these commands for a typical build and installation of GLib with Meson. Ensure you are in the GLib source directory. ```bash meson setup _build meson compile -C _build meson install -C _build ``` -------------------------------- ### Shell script for application wrapper Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/gapplication.rst A simplified shell script example that can be installed in /usr/bin to handle arguments and use gapplication. ```sh #!/bin/sh case "$1" in --help) echo "see ‘man fooview’ for more information" ;; --version) echo "fooview 1.2" ;; --gallery) gapplication action org.example.fooview gallery ;; --create) gapplication action org.example.fooview create ;; esac ``` -------------------------------- ### Compile and Install GLib from Source Source: https://github.com/gnome/glib/blob/main/INSTALL.md Standard procedure for unpacking, configuring, building, and installing GLib using Meson. Ensure you have the necessary build tools and dependencies. ```sh tar xf glib-*.tar.gz # unpack the sources cd glib-* # change to the toplevel directory meson setup _build # configure the build meson compile -C _build # build GLib # Become root if necessary meson install -C _build # install GLib ``` -------------------------------- ### Launch an application Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/gapplication.rst Use this to start an application that supports D-Bus activation. ```bash gapplication launch org.example.fooview ``` -------------------------------- ### Meson Setup Command with Cross File Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/cross-compiling.md Command to initiate the Meson build system setup using a specified cross file. This command configures the build directory for cross-compilation. ```bash meson setup --cross-file cross_file.txt builddir ``` -------------------------------- ### Install GSettings Schema with Meson Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/migrating-gconf.md This Meson build system snippet shows how to install a GSettings schema file. It ensures the schema is placed in the correct directory for GIO to find. ```meson install_data('my.app.gschema.xml', install_dir: get_option('datadir') / 'glib-2.0/schemas') ``` -------------------------------- ### GConf to GSettings Keyfile Mapping Example Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/migrating-gconf.md Example of a keyfile used by `gsettings-data-convert` to map GSettings keys to GConf paths. It demonstrates direct mapping, key name modification, and handling of relocatable schemas. ```ini [org.gnome.fonts] antialiasing = /desktop/gnome/font_rendering/antialiasing dpi = /desktop/gnome/font_rendering/dpi hinting = /desktop/gnome/font_rendering/hinting rgba-order = /desktop/gnome/font_rendering/rgba_order [apps.myapp:/path/to/myapps/] some-odd-key1 = /apps/myapp/some_ODD-key1 ``` -------------------------------- ### String Array Example Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/gvariant-specification-1.0.rst Illustrates the byte representation of an array of strings with the type signature 'as'. ```text 'i \0 'c 'a 'n \0 'h 'a 's \0 's 't 'r 'i 'n 'g 's '? \0 02 06 0a 13 ``` -------------------------------- ### Installing Construction Properties for GObject Source: https://github.com/gnome/glib/blob/main/docs/reference/gobject/tutorial.md Installs custom construction properties for a GObject, including setting property identifiers, types, and attributes like G_PARAM_CONSTRUCT_ONLY. This allows for specialized object creation with initial values. ```c enum { PROP_FILENAME = 1, PROP_ZOOM_LEVEL, N_PROPERTIES }; static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, }; static void viewer_file_class_init (ViewerFileClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS (klass); object_class->set_property = viewer_file_set_property; object_class->get_property = viewer_file_get_property; obj_properties[PROP_FILENAME] = g_param_spec_string ("filename", "Filename", "Name of the file to load and display from.", NULL /* default value */, G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); obj_properties[PROP_ZOOM_LEVEL] = g_param_spec_uint ("zoom-level", "Zoom level", "Zoom level to view the file at.", 0 /* minimum value */, 10 /* maximum value */, 2 /* default value */, G_PARAM_READWRITE); g_object_class_install_properties (object_class, N_PROPERTIES, obj_properties); } ``` -------------------------------- ### Simple Tuple Example Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/gvariant-specification-1.0.rst Demonstrates the byte representation of a simple tuple ('foo', -1) with the type signature 'is'. ```text 'f 'o 'o \0 ff ff ff ff 04 ``` -------------------------------- ### Set GLib Version Macros in Meson Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/version.md Example of how to define GLIB_VERSION_MIN_REQUIRED and GLIB_VERSION_MAX_ALLOWED in a Meson build configuration for C language. ```meson add_project_arguments([ '-DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_82', '-DGLIB_VERSION_MAX_ALLOWED=(G_ENCODE_VERSION(2,84))', ], language: 'c', ) ``` -------------------------------- ### Creating and Getting Tuple GVariants Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/gvariant-format-strings.md Demonstrates how to create GVariant instances for tuples, including nested tuples, and how to retrieve their values using g_variant_get. ```c GVariant *value1, *value2; value1 = g_variant_new ("(s(ii))", "Hello", 55, 77); value2 = g_variant_new ("() "); { gchar *string; gint x, y; g_variant_get (value1, "(s(ii))", &string, &x, &y); g_print ("%s, %d, %d\n", string, x, y); g_free (string); g_variant_get (value2, "() "); /* do nothing... */ } ``` -------------------------------- ### Call Method with File Descriptor Argument Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/gdbus.rst Example of calling a method that accepts file descriptors. Replace file.foo with an actual file path. ```bash $ gdbus call --session \ --dest org.example.foo \ --object-path /org/example/foo \ --method SendFDs \ 1 \ 10 \ 10 ``` If `gdbus-codegen` is used on this file like this: ```bash gdbus-codegen --generate-c-code myapp-generated \ --c-namespace MyApp \ --interface-prefix net.corp.MyApp. \ net.Corp.MyApp.Frobber.xml ``` two files called `myapp-generated.[ch]` are generated. The files provide an abstract `GTypeInterface` derived type called `MyAppFrobber` as well as two instantiatable types with the same name but suffixed with `Proxy` and `Skeleton`. The generated file, roughly, contains the following facilities: ```c /* GType macros for the three generated types */ #define MY_APP_TYPE_FROBBER (my_app_frobber_get_type ()) #define MY_APP_TYPE_FROBBER_SKELETON (my_app_frobber_skeleton_get_type ()) #define MY_APP_TYPE_FROBBER_PROXY (my_app_frobber_proxy_get_type ()) typedef struct _MyAppFrobber MyAppFrobber; /* Dummy typedef */ typedef struct { GTypeInterface parent_iface; /* Signal handler for the ::notification signal */ void (*notification) (MyAppFrobber *proxy, GVariant *icon_blob, gint height, const gchar* const *messages); /* Signal handler for the ::handle-hello-world signal */ gboolean (*handle_hello_world) (MyAppFrobber *proxy, GDBusMethodInvocation *invocation, const gchar *greeting); } MyAppFrobberIface; /* Asynchronously calls HelloWorld() */ void my_app_frobber_call_hello_world (MyAppFrobber *proxy, const gchar *greeting, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data); gboolean ``` -------------------------------- ### D-Bus Introspection XML Example Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/gdbus-codegen.rst An example of D-Bus introspection XML defining an interface with a method, a signal, and a property. ```xml ``` -------------------------------- ### Open a file with an application Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/gapplication.rst Launches an application and passes a file to open. ```bash gapplication launch org.example.fooview ~/file.foo ``` -------------------------------- ### Specify Schemas to Install in Autotools Makefile.am Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/migrating-gconf.md Defines the list of GSettings schema XML files to be installed by your Autotools build. ```makefile # gsettings_SCHEMAS is a list of all the schemas you want to install gsettings_SCHEMAS = my.app.gschema.xml ``` -------------------------------- ### Compile GLib Hello World Example Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/compiling.md A simple command to compile a GLib 'Hello, World!' program using shell backticks for command substitution. ```bash cc `pkg-config --cflags glib-2.0` hello.c -o hello `pkg-config --libs glib-2.0` ``` -------------------------------- ### Add Schema Compilation Script to Meson Install Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/migrating-gconf.md Integrates the schema compilation script into the Meson build system's installation process. ```meson meson.add_install_script('build-aux/compile-schemas.py') ``` -------------------------------- ### Get Verbose Property Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/gdbus-codegen.rst Gets the value of the :verbose GObject property, which corresponds to the Verbose D-Bus property. This operation does not perform any blocking I/O. ```APIDOC ## my_app_frobber_get_verbose ### Description Gets the :verbose GObject property / Verbose D-Bus property. Does no blocking I/O. ### Method `gboolean my_app_frobber_get_verbose (MyAppFrobber *object)` ### Parameters * `object` (MyAppFrobber *) - The object. ### Returns `TRUE` if verbose is enabled, `FALSE` otherwise. ``` -------------------------------- ### GDBus Property - Get Verbose Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/gdbus-codegen.rst Gets the 'verbose' GObject property, which corresponds to the 'Verbose' D-Bus property. This operation does not perform blocking I/O. ```c gboolean my_app_frobber_get_verbose (MyAppFrobber *object); ``` -------------------------------- ### Configure GLib build with Meson Source: https://github.com/gnome/glib/blob/main/docs/win32-build.md Use this command to set up the GLib build environment with Meson. Specify build type and installation prefix. The Visual Studio backend can be optionally used. ```cmd > meson .. --buildtype= --prefix= [--backend=vs] ``` -------------------------------- ### End Boundary Precedes Start Boundary Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/gvariant-specification-1.0.rst In an array of strings '(as)', if an element's end boundary precedes its start boundary, it defaults to the empty string. ```text 'f 'o 'o \0 'b 'a 'r \0 'b 'a 'z \0 04 00 0c ``` -------------------------------- ### Desktop Entry Exec line for launching Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/gapplication.rst Example of how to use gapplication launch within a .desktop file's Exec line for backwards compatibility. ```ini [Desktop Entry] Version=1.1 Type=Application Name=Foo Viewer DBusActivatable=true MimeType=image/x-foo; Exec=gapplication launch org.example.fooview %F Actions=gallery;create; [Desktop Action gallery] Name=Browse Gallery Exec=gapplication action org.example.fooview gallery [Desktop Action create] Name=Create a new Foo! Exec=gapplication action org.example.fooview create ``` -------------------------------- ### Non-normal Serialised Data Example Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/gvariant-specification-1.0.rst Illustrates non-normal serialised data in little-endian byte order for a type string '(ssn)'. This example highlights potential issues with in-place byteswapping. ```text 78 00 00 02 ``` -------------------------------- ### Sample C Function and Main Source: https://github.com/gnome/glib/blob/main/docs/reference/gobject/concepts.md A basic C function and a main function that calls it. This demonstrates a standard C function call. ```c static void function_foo (int foo) { } int main (int argc, char *argv[]) { function_foo (10); return 0; } ``` -------------------------------- ### Configure and Compile GLib with Meson Source: https://github.com/gnome/glib/blob/main/CONTRIBUTING.md Set up the build environment using Meson and compile the GLib source code. This is a necessary step before running tests or making modifications. ```sh meson setup _builddir . meson compile -C _builddir ``` -------------------------------- ### Server-side Usage Example Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/gdbus-codegen.rst Illustrates how to implement D-Bus interfaces using generated skeleton classes. This covers handling method calls, managing properties, and emitting signals. ```APIDOC ## Server-side Usage ### Implementing Interfaces Implement generated interfaces by setting vfuncs in the `MyAppFrobberIface` structure or by subclassing `MyAppFrobberSkeleton`. ### Handling Method Invocations Connect to `::handle-*` signals on the skeleton object to handle incoming D-Bus method calls. ```c static gboolean on_handle_hello_world ( MyAppFrobber *interface, GDBusMethodInvocation *invocation, const gchar *greeting, gpointer user_data) { if (g_strcmp0 (greeting, "Boo") != 0) { gchar *response; response = g_strdup_printf ("Word! You said ‘%s’.", greeting); my_app_complete_hello_world (interface, invocation, response); g_free (response); } else { g_dbus_method_invocation_return_error ( invocation, MY_APP_ERROR, MY_APP_ERROR_NO_WHINING, "Hey, %s, there will be no whining!", g_dbus_method_invocation_get_sender (invocation)); } return TRUE; } /* ... */ interface = my_app_frobber_skeleton_new (); my_app_frobber_set_verbose (interface, TRUE); g_signal_connect ( interface, "handle-hello-world", G_CALLBACK (on_handle_hello_world), some_user_data); /* ... */ error = NULL; if (!g_dbus_interface_skeleton_export ( G_DBUS_INTERFACE_SKELETON (interface), connection, "/path/of/dbus_object", &error)) { /* handle error */ } ``` ### Managing Properties Override the `:verbose` `GObject` property from a subclass or use `g_object_get()` and `g_object_set()` with the skeleton. The skeleton has an internal property bag. ### Emitting Signals Use `my_app_emit_signal()` or `g_signal_emit_by_name()` to emit signals. ### Property Change Handling `GObject::notify` signals are queued for atomic changesets. Use `g_dbus_interface_skeleton_flush()` or `g_dbus_object_skeleton_flush()` to drain the queue immediately. Use `g_object_freeze_notify()` and `g_object_thaw_notify()` for atomic changesets on different threads. ``` -------------------------------- ### Run GLib Tests with Meson Source: https://github.com/gnome/glib/blob/main/CONTRIBUTING.md Ensure your changes pass all tests using the Meson test harness. The `--setup=valgrind` option can be used for memory error checking. ```sh meson test -C _builddir meson test -C _builddir --setup=valgrind ``` -------------------------------- ### Get Interface Info Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/gdbus-codegen.rst Retrieves the GDBusInterfaceInfo for the MyAppFrobber interface. ```APIDOC ## my_app_frobber_interface_info ### Description Gets the interface info for the MyAppFrobber D-Bus interface. ### Method `GDBusInterfaceInfo *my_app_frobber_interface_info (void)` ### Returns A pointer to the GDBusInterfaceInfo structure. ``` -------------------------------- ### Basic Commandline Option Parsing Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/goption.md This snippet demonstrates the basic structure for setting up and parsing commandline arguments using GOptionContext. It includes platform-specific handling for commandline retrieval on Windows. ```c int main (int argc, char **argv) { GError *error = NULL; GOptionContext *context; gchar **args; #ifdef G_OS_WIN32 args = g_win32_get_command_line (); #else args = g_strdupv (argv); #endif // set up context if (!g_option_context_parse_strv (context, &args, &error)) { // error happened } ... g_strfreev (args); ... ``` -------------------------------- ### Client-side Usage Example Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/gdbus-codegen.rst Demonstrates how to use generated proxy objects to interact with D-Bus services. This includes creating a proxy, calling methods, connecting to signals, and accessing properties. ```APIDOC ## Client-side Usage ### Creating a Proxy Use generated constructors like `my_app_frobber_proxy_new_for_bus_sync` to create a proxy object for a D-Bus service. ```c MyAppFrobber *proxy; GError *error; error = NULL; proxy = my_app_frobber_proxy_new_for_bus_sync ( G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, "net.Corp.MyApp", /* bus name */ "/net/Corp/MyApp/SomeFrobber", /* object */ NULL, /* GCancellable* */ &error); /* do stuff with proxy */ g_object_unref (proxy); ``` ### Invoking Methods Use generated methods like `my_app_frobber_call_hello_world()` to invoke D-Bus methods. ### Handling Signals Connect to generated `GObject` signals (e.g., `::notification`) to receive D-Bus signals. ### Accessing Properties Get and set D-Bus properties using generated methods (e.g., `my_app_get_verbose()`, `my_app_set_verbose()`) or the `GObject` property interface (`:verbose`). Property access uses a cache and setting properties is asynchronous. ``` -------------------------------- ### Deprecated API Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/file-utils.md Deprecated function for getting the basename of a path. ```APIDOC ## Deprecated API ### Function * `GLib.basename` (Deprecated) ``` -------------------------------- ### GLib Build and Install on FreeBSD Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/building.md This command sequence is for building GLib on FreeBSD, requiring specific environment variables and Meson options to handle system differences. ```bash env CPPFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib -Wl,--disable-new-dtags" \ meson setup \ -Dxattr=false \ -Dinstalled_tests=true \ -Db_lundef=false \ _build meson compile -C _build ``` -------------------------------- ### Open multiple files with an application Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/gapplication.rst Launches an application and passes multiple files to open. Wildcards can be used. ```bash gapplication launch org.example.fooview ~/foos/*.foo ``` -------------------------------- ### Array of Integers Example Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/gvariant-specification-1.0.rst Illustrates the byte format for an array of integers with the type signature 'ai'. ```text 04 00 00 00 02 01 00 00 ``` -------------------------------- ### Array of Bytes Example Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/gvariant-specification-1.0.rst Presents the byte representation of an array of bytes with the type signature 'ay'. ```text 04 05 06 07 ``` -------------------------------- ### Post-installation Schema Compilation with Meson Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/migrating-gconf.md This Meson configuration uses the GNOME module to automatically compile GSettings schemas after installation. This is the recommended approach for Meson versions 0.57 and newer. ```meson gnome.post_install(glib_compile_schemas: true) ``` -------------------------------- ### Initialize gettext() for applications Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/i18n.md For applications, call `setlocale()`, `bindtextdomain()`, `bind_textdomain_codeset()`, and `textdomain()` early in `main()` to enable `gettext()`. ```c #include #include int main (int argc, char **argv) { setlocale (LC_ALL, ""); bindtextdomain (GETTEXT_PACKAGE, DATADIR "/locale"); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); // Rest of your application. } ``` -------------------------------- ### Get GLib Micro Version with GLIB_MICRO_VERSION Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/macros.md GLIB_MICRO_VERSION evaluates to the micro component of the GLib version. ```c GLIB_MICRO_VERSION ``` -------------------------------- ### Standard GPL Copyright Notice Source: https://github.com/gnome/glib/blob/main/LICENSES/GPL-2.0-or-later.txt Include this notice at the beginning of each source file to state copyright and licensing terms. It ensures the program is free software and specifies redistribution conditions. ```text one line to give the program's name and an idea of what it does. Copyright (C) yyyy name of author 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. ``` -------------------------------- ### Interactive Program Startup Notice Source: https://github.com/gnome/glib/blob/main/LICENSES/GPL-2.0-or-later.txt Display this notice when a program starts in interactive mode. It informs users about the software's warranty status and redistribution rights, directing them to specific commands for details. ```text Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. ``` -------------------------------- ### Get GLib Minor Version with GLIB_MINOR_VERSION Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/macros.md GLIB_MINOR_VERSION evaluates to the minor component of the GLib version. ```c GLIB_MINOR_VERSION ``` -------------------------------- ### Get GLib Major Version with GLIB_MAJOR_VERSION Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/macros.md GLIB_MAJOR_VERSION evaluates to the major component of the GLib version. ```c GLIB_MAJOR_VERSION ``` -------------------------------- ### GLibUnix.Pipe.open Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/unix.md Opens a new Unix pipe and initializes a GLibUnix.Pipe structure. ```APIDOC ## GLibUnix.Pipe.open ### Description Opens a new Unix pipe and initializes a GLibUnix.Pipe structure. ### Method [Not specified in source] ### Endpoint [Not specified in source] ### Parameters - **pipe** (GLibUnix.Pipe) - The GLibUnix.Pipe structure to initialize. ### Request Example [Not specified in source] ### Response #### Success Response [Not specified in source] #### Response Example [Not specified in source] ``` -------------------------------- ### Implement Interfaces with Prerequisites Source: https://github.com/gnome/glib/blob/main/docs/reference/gobject/tutorial.md Implement methods for both the primary interface and its prerequisite interface. Ensure interfaces are added in the correct order, with prerequisites added last. ```c static void viewer_file_editable_lossy_compress (ViewerEditableLossy *editable) { ViewerFile *self = VIEWER_FILE (editable); g_print ("File implementation of lossy editable interface compress method: %s.\n", self->filename); } static void viewer_file_editable_lossy_interface_init (ViewerEditableLossyInterface *iface) { iface->compress = viewer_file_editable_lossy_compress; } static void viewer_file_editable_save (ViewerEditable *editable, GError **error) { ViewerFile *self = VIEWER_FILE (editable); g_print ("File implementation of editable interface save method: %s.\n", self->filename); } static void viewer_file_editable_undo (ViewerEditable *editable, guint n_steps) { ViewerFile *self = VIEWER_FILE (editable); g_print ("File implementation of editable interface undo method: %s.\n", self->filename); } static void viewer_file_editable_redo (ViewerEditable *editable, guint n_steps) { ViewerFile *self = VIEWER_FILE (editable); g_print ("File implementation of editable interface redo method: %s.\n", self->filename); } static void viewer_file_editable_interface_init (ViewerEditableInterface *iface) { iface->save = viewer_file_editable_save; iface->undo = viewer_file_editable_undo; iface->redo = viewer_file_editable_redo; } static void viewer_file_class_init (ViewerFileClass *klass) { /* Nothing here. */ } static void viewer_file_init (ViewerFile *self) { /* Instance variable initialisation code. */ } G_DEFINE_TYPE_WITH_CODE (ViewerFile, viewer_file, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE (VIEWER_TYPE_EDITABLE, viewer_file_editable_interface_init) G_IMPLEMENT_INTERFACE (VIEWER_TYPE_EDITABLE_LOSSY, viewer_file_editable_lossy_interface_init)) ``` -------------------------------- ### Positional Parameters in GVariant Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/gvariant-text-format.md Example of using positional parameters with g_variant_new_parsed to construct a dictionary with variable arguments. ```c char *t = "xyz"; gboolean en = false; GVariant *value; value = g_variant_new_parsed ("{'title': <%s>, 'enabled': <%b>}", t, en); ``` -------------------------------- ### Handle GApplication Command Line Arguments Source: https://github.com/gnome/glib/blob/main/docs/reference/gio/gapplication.rst Example of a shell script handling command line arguments for a GApplication. It defines an action for 'org.example.fooview create' and a default case for launching the application with other arguments. ```shell gapplication action org.example.fooview create ;; -*) echo "unrecognized commandline argument" exit 1 ;; *) gapplication launch org.example.fooview "$@" ;; esac ``` -------------------------------- ### Get GLib Linker Flags Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/compiling.md Use pkg-config to retrieve the necessary linker flags for GLib libraries. ```bash pkg-config --libs glib-2.0 ``` -------------------------------- ### Creating a GOptionContext Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/goption.md Initializes a new GOptionContext for parsing command-line arguments. ```c GOptionContext *context = g_option_context_new("Usage string"); ``` -------------------------------- ### Example of UTF-8 File Name Encoding Source: https://github.com/gnome/glib/blob/main/docs/reference/glib/character-set.md Shows the hexadecimal representation of the same Spanish file name encoded in UTF-8. ```text Character: P r e s e n t a c i ó n . s x i Hex code: 50 72 65 73 65 6e 74 61 63 69 c3 b3 6e 2e 73 78 69 ```