LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-24-2008, 12:43 AM   #1
arun.tayal
LQ Newbie
 
Registered: Oct 2007
Posts: 14

Rep: Reputation: 0
Help required with system tray application


Hi All,

I am developing a system tray application ,Which will listen to a port for any incoming connection.

Also there is a menu attached with this system tray application, through which user can perform operations.

I have forked a process which will listen to the socket for connection and then perform operations and in the parent thread I am handling the GUI of system tray by calling gtk_main().

What is happening is at the accept() function the code is getting blocked and now even the parent process is not able to function because of this the menu of system tray hangs.

Plz help.


Regards
Arun Tayal

Last edited by arun.tayal; 09-24-2008 at 09:07 AM.
 
Old 09-24-2008, 10:19 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,520

Rep: Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944Reputation: 7944
Quote:
Originally Posted by arun.tayal View Post
Hi All,

I am developing a system tray application ,Which will listen to a port for any incoming connection.

Also there is a menu attached with this system tray application, through which user can perform operations.

I have forked a process which will listen to the socket for connection and then perform operations and in the parent thread I am handling the GUI of system tray by calling gtk_main().

What is happening is at the accept() function the code is getting blocked and now even the parent process is not able to function because of this the menu of system tray hangs.

Plz help.
If you wrote this application, and you don't post any of the code for folks to look at, how can we help you?
 
Old 09-24-2008, 10:58 PM   #3
arun.tayal
LQ Newbie
 
Registered: Oct 2007
Posts: 14

Original Poster
Rep: Reputation: 0
Code:
int main (int argc, char *argv[])
{

	gtk_init (&argc, &argv);
	tray_create ();

   
   signal(SIGCHLD, sigchld_handler); 


	gtk_main();
    
	tray_destroy ();

	return 0;
}

void tray_create ()
{
	GtkWidget *box;

	if (!menu)
		create_menu ();

	tray_icon = egg_tray_icon_new ("Linux Scan Agent");
	box = gtk_event_box_new ();
	icon = gtk_image_new ();

	/*add icon pix*/
	gtk_container_add (GTK_CONTAINER (box), icon);
	gtk_container_add (GTK_CONTAINER (tray_icon), box);
	gtk_widget_show_all (GTK_WIDGET (tray_icon));

	/*event handler*/
	g_signal_connect (G_OBJECT (box), "button-press-event", G_CALLBACK (clicked), NULL);

	/*load pix*/
	tray_icon_load ();
}

static void tray_icon_load ()
{
	GdkPixbuf *pixbuf;

	pixbuf = gtk_image_get_pixbuf (GTK_IMAGE (icon));
 
	if (pixbuf)
		g_object_unref (G_OBJECT (pixbuf));

	pixbuf = gdk_pixbuf_new_from_file ("/root/trial/SANE/AfterPrototype/Scan_icon.png", NULL);

	gtk_image_set_from_pixbuf (GTK_IMAGE (icon), pixbuf);
}

static void create_menu ()
{
	GtkWidget *menuitem;

	menu = gtk_menu_new ();

	menuitem = gtk_menu_item_new_with_label ("Start");
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
	//g_signal_connect (G_OBJECT (menuitem), "activate", G_CALLBACK (xyz), 0);
	g_signal_connect (G_OBJECT (menuitem), "activate", G_CALLBACK (Start_Connection), 0);

	menuitem = gtk_menu_item_new_with_label ("Stop");
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
	g_signal_connect (G_OBJECT (menuitem), "activate", G_CALLBACK (close_cdrom), 0);
	gtk_widget_set_sensitive(menuitem,0);

	menuitem = gtk_separator_menu_item_new ();
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);

	menuitem = gtk_menu_item_new_with_label ("Exit");
	g_signal_connect (G_OBJECT (menuitem), "activate", G_CALLBACK (gtk_main_quit), 0);
	gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);

	gtk_widget_show_all (menu);
}

static void Start_Connection()
{
  
   struct sockaddr_in my_addr; // my address information
   struct sockaddr_in their_addr; // connectors address information
   int sin_size;
   struct sigaction sa;	
   int yes=1;
   char buff[255];
   int rc ;
   FILE *fp;
   char str[50];
   int qw = 0;
   char sReturndata[256];
   long arg;
   
   struct timeval tv;   
   fd_set socket_read;
   fd_set socket_write;
   
   FD_ZERO(&socket_read);
   FD_SET(0,&socket_read);
   
   FD_ZERO(&socket_write);
   FD_SET(0,&socket_write);
   
   
   tv.tv_sec =1;
   tv.tv_usec = 0;
      
pid = fork();

if (pid>0)
{   
   


if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
{
perror("socket");
exit(1);
}



if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct

if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))
== -1) {
perror("bind");
exit(1);
}

if ((rc=listen(sockfd, BACKLOG)) == -1) {
perror("listen");
exit(1);
}
sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
//while(1) { // main accept() loop


		new_fd = accept(sockfd, (struct sockaddr *)&their_addr,&sin_size);
		if (new_fd == -1)
		 {
		 perror("accept");
//		 continue;		 
		 }
		 else
		 {
		 printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));		

		}


xsane_main();//for initializing the scanner
strcpy(str,"Enter in Function::ReadFromSocket");
printf("\n %s",str);
//xsane_back_gtk_info("Reading start!!!", TRUE); 
while(1) 
{

  ReadFromSocket(new_fd,buff); // to read from socket
 

  if(strcmp(buff,"DoScanning")==0) // Application Logic
  {
      DoScanning();
      Scanflag=1;
  }
 else    // Application Logic
 {
   ParseProperty(buff);
   Scanflag=0;
 }   
 sprintf(sReturndata,"%s%s%s",buff,",",szReturn);
 WriteToSocket(new_fd,sReturndata); //Wite back to socket
         
}  
if (xsane.dev)
{
    sane_close(xsane.dev);
}

  flush(buff);
  flush(new_fd);
  flush(sockfd);

}

else
	{
		gtk_main();
	}
}
In this application , there is a system tray with a menu attached with it. When we select Start from menu the function the function Start_Connection get called. Basically this function forks a new process which listens to a socket for connection. In the mean time the parent process is handling the system tray . and any event thrown on system tray is handled by parent process.

Now when the child process is waiting for a connection , the parent process also hangs and the system tray does not pops menu. After the connection gets established everything works fine.

Plz help me how to fix this...

Regards
Arun Tayal

Last edited by arun.tayal; 09-25-2008 at 12:40 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ssh tunnel system tray application Moebius Linux - Software 3 04-30-2007 05:26 AM
LXer: Dock any application to the system tray LXer Syndicated Linux News 0 06-21-2006 01:54 AM
Linux System Tray Application Mr_Munkey Programming 7 11-23-2005 02:49 PM
Things for KDE system tray in gnome system tray Boffy Linux - General 3 09-17-2004 07:56 AM
Putting my application in the system tray chaturang Linux - Software 2 04-02-2004 09:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration