GTK gossip: giochannel and pipe

Source: Internet
Author: User
Tags call back gtk

In Linux systems, to send data between two processing programs, pipe must be used. You can use the pipe () function to open pipe, you need to upload two file descriptor:

Gint parent_to_child [2];


If (pipe (parent_to_child)
=-1 ){


G_error ("failed: % s/n", g_strerror (errno ));

 Return 1;




}

After the pipeline pipe is opened, Any shard that enters parent_to_child [1] can be retrieved from
Parent_to_child [1] records.

In
Giochannel and case handling


Medium,
How to Use giochannel
In Linux, many objects or materials are processed as transactions. Therefore, you can use giochannel to Process Pipe resources. You can add watch, refer
Resources in the giochannel can be handled by the callback function when there is information going forward.


You can use
G_io_channel_unix_new () function is used to create a giochannel from the file descriptor of pipe,
For example, if the False Input [0] is the file descriptior of the token in pipe, you can create a giochannel as follows:

Giochannel * channel_read = g_io_channel_unix_new (input [0]);

If (channel_read = NULL ){

G_error ("condition failed: unable to establish giochannels! /N ");

}



To view the giochannel, use the g_io_add_watch () function. For example:

If (! G_io_add_watch (channel_read, g_io_in | g_io_hup,


(Giofunc) iochannel_read, (gpointer) data )){


G_error ("cannot forward/N ");


}


The second parameter is the identifier of the response:

  • G_io_in: Information to be retrieved
  • G_io_out: information can be imported.
  • G_io_pri:
    Urgent information to be obtained
  • G_io_err:
    Launch
    Zookeeper
  • G_io_hup: when connected to zookeeper
  • G_io_nval: invalid. The file descriptor is not enabled.


The third parameter is the callback function generated by the response component, and the fourth parameter is the information sent to the callback component.

You can use
G_io_channel_write_chars () to giochannel, for example:

Giostatus
Ret_value = g_io_channel_write_chars (

Channel, text-> STR,-1, & length, null );


If (ret_value = g_io_status_error ){


G_error ("Pipeline failed: cannot merge into pipe! /N ");


}


Else {


G_io_channel_flush (Channel, null );


}

Available
G_io_channel_read_line:

Ret_value = g_io_channel_read_line (Channel, & message, & length, null, null );


If (ret_value = g_io_status_error ){

G_error ("cannot retrieve failed: cannot retrieve! /N ");

}

The following Program is an example of a combination of giochannel and pipe. The program has a fork subprogram. The parent program sends the information to the subprogram through pipe and uses
Add giochannel to watch. When the parent program imports data, the child program calls the callback function to handle the corresponding data, the program has two internal windows.
Parent and Child programs

Gtkspinbutton

During the pull, the subroutine dialog box displays the corresponding data:

  • Giochannel_pipe_demo.c
# Include <GTK/GTK. h>
# Include <errno. h>
# Include <unistd. h>

Void value_changed_callback (gtkspinbutton * spinbutton, giochannel * channel );
Gboolean iochannel_read (giochannel * channel, giocondition condition, gtklabel * label );
Void setup_parent (Gint output []);
Void setup_child (Gint input []);

Int main (INT argc, char * argv []) {
Gint parent_to_child [2];

If (pipe (parent_to_child) =-1) {// open pipeline Pipe
G_error ("error: % s/n", g_strerror (errno ));
Return 1;
}

// Fork subroutine
Switch (Fork ()){
Case-1:
G_error ("failed: % s/n", g_strerror (errno ));
Break;
Case 0: // This is a subroutine
Gtk_init (& argc, & argv );
Setup_child (parent_to_child );
Break;
Default: // This is the parent program
Gtk_init (& argc, & argv );
Setup_parent (parent_to_child );
}

Gtk_main ();

Return 0;
}

// Call back of gtkspinbutton
Void value_changed_callback (gtkspinbutton * spinbutton, giochannel * channel ){
Giostatus ret_value;
Gint value;
Gstring * text;
Gsize length;

Value = gtk_spin_button_get_value_as_int (spinbutton );
TEXT = g_string_new ("");
G_string_sprintf (text, "% d/N", value );

// Inject data to giochannel
Ret_value = g_io_channel_write_chars (Channel, text-> STR,-1, & length, null );
If (ret_value = g_io_status_error ){
G_error ("too many: Unable to merge into giochannel! /N ");
}
Else {
G_io_channel_flush (Channel, null );
}
}

Gboolean iochannel_read (giochannel * channel,
Giocondition condition, gtklabel * label ){
Giostatus ret_value;
Gchar * message;
Gsize length;

If (condition & g_io_hup ){
G_error ("Pipeline failed: pipe is in progress! /N ");
}

Ret_value = g_io_channel_read_line (Channel, & message, & length, null, null );

If (ret_value = g_io_status_error ){
G_error ("cannot retrieve failed: cannot retrieve! /N ");
}

Message [length-1] = 0;
Gtk_label_set_text (Label, message );

Return true;
}

Void setup_parent (Gint output []) {
Gtkwidget * window;
Gtkwidget * spinbutton;
Giochannel * channel_write;

Window = gtk_window_new (gtk_window_toplevel );
Gtk_window_set_title (gtk_window (window), "parent ");
Gtk_window_set_default_size (gtk_window (window), 250, 50 );

Spinbutton = gtk_spin_button_new_with_range (0.0, 100.0, 1.0 );
Gtk_container_add (gtk_container (window), spinbutton );

G_signal_connect (gtk_object (window), "Destroy ",
G_callback (gtk_main_quit), null );

// Extract the pipe that is not used by the related user
Close (output [0]);

// Create a giochannel
Channel_write = g_io_channel_unix_new (output [1]);
If (channel_write = NULL ){
G_error ("condition failed: unable to establish giochannels! /N ");
}

G_signal_connect (g_object (spinbutton), "value_changed ",
G_callback (value_changed_callback), (gpointer) channel_write );

Gtk_widget_show_all (window );
}

Void setup_child (Gint input []) {
Gtkwidget * window;
Gtkwidget * label;
Giochannel * channel_read;

Window = gtk_window_new (gtk_window_toplevel );
Gtk_window_set_title (gtk_window (window), "child ");
Gtk_window_set_default_size (gtk_window (window), 250, 50 );

Label = gtk_label_new ("0 ");
Gtk_container_add (gtk_container (window), label );

G_signal_connect (gtk_object (window), "Destroy ",
G_callback (gtk_main_quit), null );

// Skip unnecessary pipe Injection
Close (input [1]);

Channel_read = g_io_channel_unix_new (input [0]);
If (channel_read = NULL ){
G_error ("condition failed: unable to establish giochannels! /N ");
}

If (! G_io_add_watch (channel_read, g_io_in | g_io_hup,
(Giofunc) iochannel_read, (gpointer) label )){
G_error ("cannot add watch to giochannel! /N ");
}

Gtk_widget_show_all (window );
}


The result of a response line is as follows:

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.