A nifty feature of GDK (one of the libraries that underlying
gtkmm) is the ability to have it check for data on a file descriptor
for you. This is especially useful for networking applications. The
following method is used to do this:
Connection Gtk::Main::input.connect(const SlotType& sd, int source,
GdkInputCondition condition);
The first argument is a slot you wish to have called when then the
specified event (see argument 3) occurs on the file descriptor you
specify using argument two. Argument three may be one or a combination
(using |) of:
GDK_INPUT_READ - Call your method when there is data ready for
reading on your file descriptor.
GDK_INPUT_WRITE - Call your method when the file descriptor is
ready for writing.
GDK_INPUT_EXCEPTION - Call your method when an exception happened
on the file descriptor.
The return value is a Connection that may be used to stop monitoring
this file descriptor using the disconnect following method.
The signal handler should be declared as follows:
void input_callback(int source, GdkInputCondition condition);
where source and condition are as specified above. As usual
the slot is created with slot() and can be a member method
of an object.
A little (and somewhat dirty) example follows as usual. To use
the example just execute it from a terminal; it doesn't create a
window. It will create a pipe named testpipe in the current
directory. Then start another shell and execute cat
>testpipe. The example will print each line you enter until you
type quit.
Source location: examples/input/input.cc
#include <gtkmm/main.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fstream>
#include <iostream>
#include <memory>
using std::istream;
using std::auto_ptr;
using SigC::slot;
auto_ptr<istream> input;
// this will be our signal handler for read operations
// there is not much to say. just read a string,
// print it and quit the application if the string was quit
void MyCallback(int, GdkInputCondition) {
Gtk::string dummy;
do {
(*input) >> dummy;
cout << dummy << endl;
if(dummy == "quit") Gtk::Main::quit();
} while(input->fail());
}
int main (int argc, char *argv[])
{
// the usual Gtk::Main object
Gtk::Main app(argc, argv);
// create a fifo for testing purposes
if (mkfifo("testfifo",0666) != 0) {
cerr << "error creating fifo" << endl;
return -1;
}
// open the fifo
input=new ifstream("testfifo");
// int fd = open("testfifo", 0);
// if (fd == -1) {
// cerr << "error opening fifo" << endl;
// return -1;
// }
// assign the fifo's filedescriptor to our ifstream object
//This sucks; it will only ever work with libstdc++-v3, as
// both istream::__filebuf_type and the basic_filebuf contructor
// that takes an fd are libstdc++-v3 specific.
//input=new istream(new ifstream::__filebuf_type(fd,"testfifo"));
// connect the signal handler
app.input.connect(slot(MyCallback), fd, GDK_INPUT_READ);
// and last but not least - run the application main loop
app.run();
// now remove the temporary fifo
if(unlink("testfifo"))
cerr << "error removing fifo" << endl;
return 0;
}
|