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

Re: gtk+ and pthread



On Mon, 25 Mar 2002, Ro Stellar wrote:

> I am currently writing an application which, among other things, needs to
> continually receive data over a UDP connection and display this data in a
> gtk pixmap.  I am having problems running  pthread_create()  (for receiving
> data via UDP) and gtk_main()  (for display and other buttons) at the same
> time.
>
> As an alternative, I would like to do the following:
> 1. Put the UDP receive code into a callback function for a button
> 2. Associate this function with the click event for a button that will be
> created but not displayed
> 3. And... before I call gtk_main() set something so that this button always
> appears to be clicked or at least being clicked a few times a second or
> greater

This is not the way to do it. You should use gdk_input_add(....).

I attache an example on this.

Göran Hasse

>
> Is this, specifically the third step, possible?  If so, a push/kick in the
> right direction would be greatly appreciated.
>
> Cheers,
> David
>
>
> _________________________________________________________________
> Send and receive Hotmail on your mobile device: http://mobile.msn.com
>
> _______________________________________________
> gtk-app-devel-list mailing list
> gtk-app-devel-list@gnome.org
> http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
>

----------------------------------------------------------------
Göran Hasse            email: gh@raditex.se     Tel: 08-6949270
Raditex AB             http://www.raditex.se
Sickla Alle 7, 1tr                              Mob: 070-5530148
131 34  NACKA, SWEDEN

/**********************************************************************
 *
 *   $Id: InPanelUDP.c,v 1.2 2001/03/28 22:23:59 gh Exp $
 *
 *   InPanelUDP.c    1.0
 *   Göran Hasse, Raditex AB, 1.0
 *
 *   This lissens for mulitcast packets!
 *
 *   $Log: InPanelUDP.c,v $
 *   Revision 1.2  2001/03/28 22:23:59  gh
 *   Strange conflict in previous checkin
 *
 *   Revision 1.1  2000/03/29 00:18:12  gh
 *   Added a listwidget that listens for Multicasts
 *
 *
 ***********************************************************************/

/* #include <arpa/inet.h> */

#include <gtk/gtk.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <fcntl.h>
#include <math.h>
#include <netdb.h>
#include <netinet/in.h> /* contains IP #defines */
#include <sys/errno.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <arpa/inet.h>
#include <string.h>

int radius;

int s,  len, nbytes, one = 1;
static struct sockaddr_in sin;
static struct ip_mreq imr;
char buf[100];

GtkWidget *showrows[10];
GtkWidget *messrow;
GtkWidget *entry;

struct padcmd {
  int fd;
  char string[60];
  gpointer data;
};

#define PORT    6060
#define GROUP   "224.0.0.0"

void quit()
{
  gtk_exit(0);
}
 
int file_function( struct padcmd *cmdinfo )
{

  gchar *entry_text;
  gchar messtext[60];
  static int i;
  int infile, ret_status;
  char string[80];

  infile = cmdinfo->fd;

  strcpy(messtext, "No more messages in queue");
  printf("****\n");
  printf("--->Fildescriptor - i funk %d\n", infile );
  printf("Funktionen har anropats %d\n", i++);

  len = sizeof (sin);

  ret_status =  read(infile, string, sizeof(string));

  printf("Retur status %d , string -> %s", ret_status, string );


  strncpy(cmdinfo->string, string, sizeof(cmdinfo->string));

  
  gtk_entry_set_text( GTK_ENTRY(messrow), string  );

	for( i = 9 ; i > 0 ; i-- )
	  {
	    entry_text = gtk_entry_get_text( GTK_ENTRY(showrows[i-1]));
	    gtk_entry_set_text( GTK_ENTRY(showrows[i]), entry_text );
	  }

	gtk_entry_set_text( GTK_ENTRY(showrows[0]), string  );
  
  fflush(stdout);

  return 0;

}


void clear_callback(GtkWidget *widget, GtkWidget *entry)
{


  int i;
  gchar messtext[60];

        strcpy(messtext, "Clear all messages");
	gtk_entry_set_text( GTK_ENTRY(messrow), messtext  );

	for( i = 9 ; i >= 0 ; i-- )
	  {

	    gtk_entry_set_text( GTK_ENTRY(showrows[i]), " " );
	  }

}


int main( int argc, char *argv[] )
{

  struct padcmd cmdinfo;

  int i;
  GtkWidget *window;
  GtkWidget *vbox, *vbox1, *hbox2;

  GtkWidget *exit_button, *clear_button;


	if ((s = socket (AF_INET, SOCK_DGRAM, 0)) == -1)
	{
		perror ("socket");
		return 1;
	}

	if ( setsockopt (s, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof (one)) == -1 )
	{
		perror("setsockopt: SO_REUSEADDR");
		exit(1);
	}

#ifdef SO_REUSEPORT
/*
 * This must be added for OSF1 v2.x (and BSD 4.3)
 */
    if ( setsockopt (s, SOL_SOCKET, SO_REUSEPORT, (char *) &one, sizeof (one))
 == -1 )
    {
        perror("setsockopt: SO_REUSEADDR");
        exit(1);
    }
#endif

	sin.sin_family = AF_INET;
	sin.sin_port = htons (PORT);
	sin.sin_addr.s_addr = htonl (INADDR_ANY);

	if (bind (s, (struct sockaddr *) & sin, sizeof (sin)) == -1)
	{
		perror ("bind");
		return 1;
	}

	/*
	 * the original posting was = htonl(inet_addr (GROUP)) 
	 * which is wrong.
	 *
	 * Send greeting message to multicast group: 
	 */
	sin.sin_addr.s_addr = inet_addr (GROUP);
	if (sendto (s, "Hi!", 4, 0, (struct sockaddr *) & sin, sizeof (sin)) == -1)
	{
		perror ("socket");
		return 1;
	}

	/*
	 * Join the group: 
	 * IP multicast address of group:
	 * local IP address of interface:
	 */ 
	imr.imr_multiaddr = sin.sin_addr;
	imr.imr_interface.s_addr = htonl (INADDR_ANY);

	if (setsockopt (s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &imr,
			sizeof (imr)) == -1)
	{
		/*
		 * The original posting did not include this:
		 * landmark@cs.tu-berlin.de (Torsten Kerschat) stated:
		 * Using setsockopt again with the same options on the
		 * same socket would fail, although it is correct. Therefore:
		 */
		if (errno != EADDRINUSE)
		{
			perror("setsockopt: IPPROTO_IP, IP_ADD_MEMBERSHIP");
			return 1;
		}

	}


  gtk_init(&argc, &argv);


  cmdinfo.fd = s;

  gdk_input_add( s, GDK_INPUT_READ, GTK_SIGNAL_FUNC(file_function), &cmdinfo );


  /* create a new window */
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  gtk_widget_set_usize( GTK_WIDGET (window), 600, 325);
  gtk_window_set_title(GTK_WINDOW (window), "Very simple listwindow");
  gtk_signal_connect(GTK_OBJECT (window), "delete_event",
		     (GtkSignalFunc) gtk_exit, NULL);

  vbox = gtk_vbox_new (FALSE, 0);

  vbox1 = gtk_vbox_new (FALSE, 0);
  hbox2 = gtk_hbox_new (FALSE, 0);

  gtk_container_add (GTK_CONTAINER (window), vbox);
  gtk_container_border_width (GTK_CONTAINER (window), 10);

  gtk_widget_show(vbox1);
  gtk_widget_show(hbox2);
  gtk_widget_show(vbox);

  /*----------------------------------------------------------------------*/

  messrow = gtk_entry_new_with_max_length (60); 
 
  for( i = 9 ; i >= 0 ; i-- )
    {
     showrows[i] = gtk_entry_new_with_max_length (60);

     gtk_entry_set_editable( ( GtkEntry * ) showrows[i], FALSE);

     gtk_box_pack_start (GTK_BOX (vbox1), showrows[i], TRUE, TRUE, 0);
    }

   gtk_box_pack_start (GTK_BOX (vbox1), messrow, TRUE, TRUE, 0);

   gtk_box_pack_start (GTK_BOX (vbox), vbox1, TRUE, TRUE, 0);

  /*----------------------------------------------------------------------*/

  exit_button = gtk_button_new_with_label("Exit");
  gtk_signal_connect_object (GTK_OBJECT (exit_button), "clicked",
			     GTK_SIGNAL_FUNC(gtk_exit),
			     GTK_OBJECT (window));

  clear_button = gtk_button_new_with_label ("Clear all");

  gtk_signal_connect(GTK_OBJECT(clear_button), "clicked",
		     GTK_SIGNAL_FUNC(clear_callback),
		     showrows[0]);

  gtk_box_pack_start (GTK_BOX (hbox2), exit_button, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (hbox2), clear_button, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (vbox), hbox2, TRUE, TRUE, 0);

  gtk_container_border_width (GTK_CONTAINER (vbox), 0);
  
 
  /***********************************************************************/

  for( i = 0 ; i < 10 ; i++ )
    {
      gtk_widget_show (showrows[i]);
    }
  gtk_widget_show (messrow);
  gtk_widget_show (exit_button);
  gtk_widget_show (clear_button);


  gtk_widget_show(vbox1);
  gtk_widget_show(hbox2);
  gtk_widget_show(vbox);

  gtk_widget_show(window);

  gtk_main();

  return(0);

  gtk_main();

  return 0;


}







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