LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 02-28-2012, 05:02 PM   #1
Dr_Lion
LQ Newbie
 
Registered: Oct 2011
Posts: 4

Rep: Reputation: Disabled
C - Gtk + timer + sockets + cairo


My ideia is to draw a circle with a radius "radius" and i'm thinking about using a gtk window for it. I need to redraw it from time to time or everytime that comes a new value from a socket message. My problem is i don't know where to put the socket code, to be constantly reading the socket messages.

I thought puting the code after the gtk function but the program hang on, and only jump to the socket piece of code after closing the gtk window.


Does anyone knows where to paste the code? i had another ideia and it would be to create two threads and put gtk window in one thread and the socket receiver code in another, what you think?



Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <cairo.h>
#include <gtk/gtk.h>
#include <time.h>


#define M_PI 3.1415
#define MYPORT "4950"    // the port users will be connecting to
#define MAX_EST 70 
#define MAXBUFLEN 100

double radius1 = 100.0;

static char buffer[256];

static gboolean on_expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
	double xc1 = 300.0;
	double yc1 = 225.0;


	double angle1 = 0.0  * (M_PI/180.0);  /* angles are specified */
	double angle2 = 360.0 * (M_PI/180.0);  /* in radians           */

	cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 500, 500);
	//cairo_t *cr = cairo_create (surface);

	cairo_t *cr;
	cr = gdk_cairo_create(widget->window);
			
	//circ 1 
	cairo_set_line_width (cr, 10.0);
	cairo_arc (cr, xc1, yc1, radius1, angle1, angle2);
	cairo_stroke (cr);
		
	cairo_destroy(cr);

  return FALSE;
}


static gboolean time_handler(GtkWidget *widget)
{
  if (widget->window == NULL) return FALSE;

  time_t curtime;
  struct tm *loctime;

  curtime = time(NULL);
  loctime = localtime(&curtime);
  strftime(buffer, 256, "%T", loctime);
  
	//printf("\n %f", radius1);
  gtk_widget_queue_draw(widget);
  return TRUE;
}

// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa)
{
    if (sa->sa_family == AF_INET) {
        return &(((struct sockaddr_in*)sa)->sin_addr);
    }
 
    return &(((struct sockaddr_in6*)sa)->sin6_addr);
}

int main (int argc, char *argv[])
{
	
	int sockfd;
    struct addrinfo hints, *servinfo, *p;
    int rv;
    int numbytes;
    struct sockaddr_storage their_addr;
    char buf[MAXBUFLEN];
    socklen_t addr_len;
    char s[INET6_ADDRSTRLEN];
    
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_flags = AI_PASSIVE; // use my IP
 
    if ((rv = getaddrinfo(NULL, MYPORT, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }
 
    // loop through all the results and bind to the first we can
    for(p = servinfo; p != NULL; p = p->ai_next) {
        if ((sockfd = socket(p->ai_family, p->ai_socktype,
                p->ai_protocol)) == -1) {
            perror("listener: socket");
            continue;
        }
 
        if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
            close(sockfd);
            perror("listener: bind");
            continue;
        }
 
        break;
    }
    
    if (p == NULL) {
        fprintf(stderr, "listener: failed to bind socket\n");
        return 2;
    }
 
    freeaddrinfo(servinfo);

  GtkWidget *window;
  GtkWidget *darea;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  darea = gtk_drawing_area_new();
  gtk_container_add(GTK_CONTAINER (window), darea);

  g_signal_connect(darea, "expose-event",
      G_CALLBACK(on_expose_event), NULL);
  g_signal_connect(window, "destroy",
      G_CALLBACK(gtk_main_quit), NULL);

  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 600, 600);

  gtk_window_set_title(GTK_WINDOW(window), "timer");
  g_timeout_add(1000, (GSourceFunc) time_handler, (gpointer) window);
  gtk_widget_show_all(window);
  time_handler(window);

  gtk_main();
  
  //codigo socket
  
   printf("listener: waiting to recvfrom...\n");
	
	while (1) {	
		printf("dentro do while\n");
		addr_len = sizeof their_addr;
		if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0, (struct sockaddr *) &their_addr, &addr_len)) == -1) {
			perror("recvfrom");
			exit(1);
		}
	 
	 
		printf("listener: got packet from %s\n", inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *) &their_addr), s, sizeof s));

		printf("listener: packet is %d bytes long\n", numbytes);
		buf[numbytes] = '\0';
		printf("RECEBIDO: \"%s\"\n", buf);

		radius1 = atoi(buf);
	
		printf("\n %f", radius1);
	
	}
  
  //fim do codigo socket

  return 0;
}
Thanks in advance, and sorry my bad english.
 
Old 02-29-2012, 06:19 PM   #2
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X, Raspbian
Posts: 90

Rep: Reputation: 18
GIOChannels is a facility the fully supports the GTK environment. You can research more example using "GIOChannel
as the keyword.

Hope that gets you started.
 
  


Reply



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
[SOLVED]gtk theme doesnt't change using cairo dock launcher michaelinux Linux - Software 1 05-25-2010 06:42 PM
Having trouble debugging a program. Uses gtk, cairo, and allegro. Winter Knight Programming 1 07-26-2007 01:46 AM
font problem after gtk, pango and cairo update Lord_Grave Linux - Software 0 06-03-2007 05:21 AM
gnome + gtk + cairo keratos Ubuntu 0 05-18-2007 10:40 AM
gtk+2.8 and cairo John Velman Slackware 3 10-19-2005 04:16 PM

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

All times are GMT -5. The time now is 02:34 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