Workshop Study Notes (2)

Source: Internet
Author: User
Tags gtk

1. About the volume binding
Volume binding is a further encapsulation of volume lower-API and provides more convenient interfaces. Some bindings also integrate the event loop, which is more concise to use. Here we mainly discuss some implementations of glib binding.
Ii. Some Basic Concepts
1. Proxy)
A proxy is the representation of objects in a local process in a remote process in other applications, previously we saw that during the underlying API Message Passing Process, we manually created a message and sent it again, then waiting for the response. If a proxy is used, the proxy is used as a local object. However, when you trigger a method call, the proxy is converted into a message and sent to other application processes for response, and obtain the return value.
2. gobject
Gobject is an object-oriented function library written in C language. It supports intercommunication between multiple languages. It is the basis of gnome and is widely used in GTK + and other applications. Before GTK + 2.0, gobject was a part of gobject. Later developers moved most of the parts unrelated to the GUI to gobject. For details, go to http://zh.wikipedia.org/wiki/gobject.

3. Use XML to describe the callback Interface

Because our main purpose is to use the delimiter to communicate between gobject objects. We will introduce the simplest way to implement it. Plugin-bindings-tool. This tool uses XML files as input to conveniently generate server and client code.

XML interface description file:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE node PUBLIC  "-//freedesktop//DTD D-Bus Object Introspection 1.0//EN"  "http://standards.freedesktop.org/dbus/1.0/introspect.dtd"><node>  <interface name="org.maemo.Value"><method name="getvalue1">  <arg type="i" name="cur_value" direction="out"/></method><method name="getvalue2">  <arg type="d" name="cur_value" direction="out"/></method><method name="setvalue1">  <arg type="i" name="new_value" direction="in"/></method><method name="setvalue2">  <arg type="d" name="new_value" direction="in"/></method>  </interface></node>

Note: here we have implemented four interfaces under the org. maemo. Value interface to obtain and set the variable values respectively. Each method ends with a method keyword. The interface is similar to node, and node can be understood as an object. Arg indicates the parameter, type indicates the parameter type, name indicates the name, and direction indicates the direction. Out indicates the return value. You can add any number of interfaces and Methods. Note that the Plugin-binding-tool automatically adds the introspection interface (default ). In addition, we also need to define a DTD for the file type so that the verification tool can easily identify the file.

MAKEFILE file:

interface_xml := value-dbus-interface.xmlcheckxml: $(interface_xml)@(xmllint) --valid --noout $(interface_xml)@(echo) $< check out ok

Note: here we need to use an XML file verification tool named xmllint to conveniently verify whether syntax exists during the XML writing process.

LC @ LC-Lenovo :~ /Exam Study Notes $ make checkxmlvalue-dbus-interface.xml check out OK

4. Generate intermediate glue and code (glub code)

The ing code must be available between the bindings using glib. We can use the export-binding-tool to generate the ing code. The MAKEFILE file is as follows:

files := client-glub.h\ server-glub.hclient-glub.h:$(interface_xml)dbus-binding-tool --prefix=value_object --mode=glib-client  $< > $@server-glub.h:$(interface_xml)dbus-binding-tool --prefix=value_object --mode=glib-server  $< > $@

Note: -- prefix specifies the prefix of the generated glub code, and -- Mode specifies whether it is a client or a server.

LC @ LC-Lenovo :~ /Override study notes $ make value-server-stub.h value-client-stub.h value-dbus-interface.xml Plugin-binding-tool -- prefix = value_object -- mode = glib-Server \ value-server-stub.hdbus-binding-tool> value-dbus-interface.xml -- prefix = value_object -- mode = glib-client \> value-client-stub.hlc @ LC-Lenovo: ~ /Workshop study notes $ lssag Study Notes (2) makefile value-dbus-interface.xmlDBus Study Notes (1) value-client-stub.h value-server-stub.h

We can easily use the generated glue and code later when starting the signal. Here we mainly talk about several problems: Marshal (column set) the function is to convert the data into the stream format unmarshaling (scattered set) is a reverse operation. In the bound file, feature-binding-tool automatically generates a function to restore the method parameters from the callback message, that is, the scattered set is implemented. Glib has some standard column set functions, which are defined in gmarshal. h. If gmarshal. h does not provide a suitable column set function, we can use a tool named glib-genstmal to automatically generate a column set function.

5. Create a gobject object

Each gobject object has an instance class and an object class. All instances in the object class have members, and the instance class members belong to a single member.

typedef struct {  /* The parent class object state. */  GObject parent;  /* Our first per-object state variable. */  gint value1;  /* Our second per-object state variable. */  gdouble value2;} ValueObject;/* Per class state.typedef struct {  /* The parent class state. */  GObjectClass parent;} ValueObjectClass;

Note: We have defined two data members in the instance class. Next we will write method to operate two variables.

Gtype value_object_get_type (void);/* defines some common macros of gobject objects */# define value_type_object (value_object_get_type () # define value_object (object) \ (object ), \ value_type_object, valueobject) # define value_object_class (Klass) \ (struct (Klass), \ value_type_object, valueobjectclass) # define value_is_object (object) \ (struct (object ), \ value_type_object) # define value_is_object_class (Klass) \ (struct (Klass), \ value_type_object) # define value_object_get_class (OBJ) \ (struct (OBJ), \ value_type_object, valueobjectclass)/* utility macro to define the value_object gtype structure. */g_define_type (valueobject, value_object, g_type_object)

Method declaration:

gboolean value_object_getvalue1(ValueObject* obj, gint* value_out,                                                  GError** error);gboolean value_object_getvalue2(ValueObject* obj, gdouble* value_out,                                                  GError** error);gboolean value_object_setvalue1(ValueObject* obj, gint value_in,                                                  GError** error);gboolean value_object_setvalue2(ValueObject* obj, gdouble value_in,                                                  GError** error);

Instance class initialization:

static void value_object_init(ValueObject* obj) {  dbg("Called");  g_assert(obj != NULL);  obj->value1 = 0;  obj->value2 = 0.0;}

Object class initialization:

Static void value_object_class_init (valueobjectclass * Klass) {dbg ("called"); g_assert (Klass! = NULL); dbg ("binding to glib/D-Bus");/* install a custom object. */dbus_g_object_type_install_info (value_type_object, & dbus_glib_value_object_object_info); dbg ("done ");}

Producer path interface definition:

/* Well-known name for this service. */#define VALUE_SERVICE_NAME        "org.maemo.Platdev_ex"/* Object path to the provided object. */#define VALUE_SERVICE_OBJECT_PATH "/GlobalValue"/* And we're interested in using it through this interface.   This must match the entry in the interface definition XML. */#define VALUE_SERVICE_INTERFACE   "org.maemo.Value"

Main function:

# Include "common-defs.h" int main (INT argc, char ** argv) {/* initialize the object system. */g_type_init (); g_print (progname ": main connecting to the Session D-bus. \ n "); bus = dbus_g_bus_get (dbus_bus_session, & error); If (error! = NULL) {handleerror ("couldn't connect to session bus", error-> message, true );}
G_print (progname ": main registering the well-known name (% s) \ n", value_service_name);/* register the proxy dbus_service_region = "org. freedesktop. export "dbus_path_dbus ="/org/freedesktop/export "dbus_interface_dbus =" org. freedesktop. authorization "*/busproxy = Authorization (bus, dbus_service_dbus, dbus_path_dbus, dbus_interface_xy); If (busproxy = NULL) {handleerror (" failed to get a proxy for D-Bus ", "unkn Own (dbus_g_proxy_new_for_name) ", true);}/* Application name. */If (! Trim (busproxy, "requestname", & error, g_type_string, value_service_name, 0, g_type_invalid, g_type_uint, & result, g_type_invalid) {handleerror ("D-Bus.RequestName RPC failed ", error-> message, true);} g_print (progname ": Main requestname returned % d. \ n ", result); If (result! = 1) {handleerror ("failed to get the primary well-known name.", "requestname result! = 1 ", true );}
valueObj = g_object_new(VALUE_TYPE_OBJECT, NULL);  if (valueObj == NULL) {    handleError("Failed to create one Value instance.",                "Unknown(OOM?)", TRUE);  }  g_print(PROGNAME ":main Registering it on the D-Bus.\n");    dbus_g_connection_register_g_object(bus,                                      VALUE_SERVICE_OBJECT_PATH,                                      G_OBJECT(valueObj));  g_print(PROGNAME ":main Ready to serve requests (daemonizing).\n");}

Finally, compile and use producer-send to send messages and call the method for testing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.