Re: GTK and devices over TCP and serial ports




On 2004.05.07 09:34 Zbigniew Wasik wrote:
OK  I already found reference to GSource (GLib as I thought)
but anyway I am asking whether you have maybe an example of use the
GSOurce

I don't have anything quite like you need. The closest I have is for
a TCP/IP server which uses a GSource to allow the mainloop to watch
for incoming connections. It goes something like this:

struct avgs_listen_watch {
    GSource source;
    GPollFD poll;
};

static gboolean avgs_listen_prepare(GSource *source,gint *timeout)
{
    struct avgs_listen_watch *watch=(struct avgs_listen_watch *)source;
    *timeout=-1;
#ifdef DEBUG
    fprintf(stderr,"avgs_listen_prepare()=FALSE\n");
#endif
    return FALSE;
}

static gboolean avgs_listen_check(GSource *source)
{
    struct avgs_listen_watch *watch=(struct avgs_listen_watch *)source;
#ifdef DEBUG
    fprintf(stderr,"avgs_listen_check()=%s\n",
      watch->poll.revents&G_IO_IN?"TRUE":"FALSE");
#endif
    return watch->poll.revents&G_IO_IN;
}

static gboolean avgs_listen_dispatch(GSource *source,GSourceFunc callback,
  gpointer user_data)
{
    int fd;
    struct avgs_listen_watch *watch=(struct avgs_listen_watch *)source;
    struct avgs_rpc_watch *rpcw;
#ifdef DEBUG
    fprintf(stderr,"avgs_listen_dispatch()\n");
#endif
    fd=accept(watch->poll.fd,NULL,NULL);
    if (fd>=0)
    {
	/* deal with the incoming connection */
    }
    return TRUE;
}

static void avgs_listen_finalize(GSource *source)
{
}

GSourceFuncs avgs_listen_funcs = {
    avgs_listen_prepare,
    avgs_listen_check,
    avgs_listen_dispatch,
    avgs_listen_finalize,
};

int main(int argc,char **argv)
{
    short *modes;
    struct avgs_listen_watch *listenw;
    struct addrinfo hints,*res;
    char *s,*node;
    int port,fd,retval;
    if (argc<2)
    {
        fprintf(stderr,"Usage: %s <remote>\n",argv[0]);
        exit(1);
    }
    s=strchr(argv[1],':');
    if (!s)
    {
        fprintf(stderr,"%s: Missing port number in remote\n",argv[0]);
        exit(1);
    }
    port=atoi(s+1);
    if (port<=0 || port>65535)
    {
fprintf(stderr,"%s: Illegal port number in remote (%d)\n",argv[0],port);
        exit(1);
    }
    node=malloc(s-argv[1]+1);
    memcpy(node,argv[1],s-argv[1]);
    node[s-argv[1]]='\0';
    hints.ai_family=PF_UNSPEC;
    hints.ai_socktype=SOCK_STREAM;
    hints.ai_protocol=0;
    hints.ai_flags=0;
    retval=getaddrinfo(node,s+1,&hints,&res);
    free(node);
    if (retval)
    {
        fprintf(stderr,"%s: %s\n",argv[0],gai_strerror(retval));
        exit(1);
    }
    fd=socket(res->ai_family,res->ai_socktype,res->ai_protocol);
    if (fd<0)
    {
        perror(argv[0]);
        freeaddrinfo(res);
        exit(1);
    }
    if (bind(fd,res->ai_addr,res->ai_addrlen))
    {
        perror(argv[0]);
        freeaddrinfo(res);
        close(fd);
        exit(1);
    }
    if (listen(fd,0))
    {
        perror(argv[0]);
        freeaddrinfo(res);
        close(fd);
        exit(1);
    }
    freeaddrinfo(res);
    listenw=(struct avgs_listen_watch *)
      g_source_new(&avgs_listen_funcs,sizeof(*listenw));
    if (!listenw)
    {
        fprintf(stderr,"%s: Not enough memory\n",argv[0]);
        exit(1);
    }
    listenw->poll.fd=fd;
    listenw->poll.events=G_IO_IN;
    g_source_add_poll((GSource *)listenw,&listenw->poll);
    g_source_attach((GSource *)listenw,NULL);
    ml=g_main_loop_new(NULL,FALSE);
    g_main_loop_run(ml);
    g_main_loop_unref(ml);
    exit(0);
}

Also do you have experience with showing images? If so what you think
what is better to use Gdk-Pixbuff or GdkRGB  ?

I guess it depends. I use GdkRGB since that's easy for me and GdkPixbuf
is implemented using GdkRGB so it can't be any faster.

HTH,

--
J. Ali Harlow
ali juiblex co uk



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]