Hi, all:
I am trying write  a touch screen driver in clutter-1.6.20 application,
touch screen like ELO type, it's use serial send 10 bytes data to /dev/ttyS0
.

Now my way is:
Use clutter_texture_new_from_file() create a mouse actor,  create a thread
constantly from /dev/ttyS0 get data, conversion coordinate and then create a
new clutter event,  set mouse  actor position in thread, because clutter API
is not thread safe, so only can set the mouse actor position API a separate
place in a function, by clutter_thread_add_ide_full() calls.

This will cause some problems, hasn't dealt with the mouse position, the new
touch screen data have arrived, but thread haven't deal with the mouse place
of work, and the resulting events have delay, lead to the mouse action is
very slow.
Do you have any ideas method can improve efficiency ?

Here is my code:

static gfloat x=0, y=0;
static ClutterActor *mouse;
GTimer *timer;

static gboolean fun_setmouse_x(gpointer data)
{
        clutter_actor_set_position(mouse, x, y);
        clutter_actor_raise_top(mouse);
        return FALSE;
}

void change_abs(int abs_x, int abs_y, unsigned char up_down)
{
    ClutterEvent *event;
    static gfloat     last_x = 0, last_y = 0;
    static gboolean clicked = FALSE;
     
    clutter_threads_enter (); 
  
    if (!clutter_events_pending())
    {
       
        //if (last_x == abs_x && last_y == abs_y )
                //goto out;
        
        last_x = abs_x;
        last_y = abs_y;
                
                event = clutter_event_new (CLUTTER_NOTHING);
                event->any.stage = CLUTTER_STAGE(
clutter_stage_get_default() );
        
                x = (1280*abs_x)/4096;
                y = (720*abs_y)/4096; 
                  
                event->button.x = x;
                event->button.y = y;
        
                if ((up_down==0x81)  && !clicked)
                {
                        event->button.type = event->type =
CLUTTER_BUTTON_PRESS;
                        event->button.time =
g_timer_elapsed(timer,NULL)*1000;
                        event->button.modifier_state = 0;
                        event->button.button = 1;
        
                        clicked = TRUE;
                }
                else if ((up_down==0x82) && clicked )
                {
                        event->motion.type = event->type = CLUTTER_MOTION;
                        event->motion.time =
g_timer_elapsed(timer,NULL)*1000;
                        if (clicked)
                                event->motion.modifier_state =
CLUTTER_BUTTON1_MASK;
                        else
                                event->motion.modifier_state = 0;
                        
                        //dbg("CLUTTER_MOTION!!!!\n");
                }
                else if ((up_down==0x84) && clicked)
                {
                        event->button.type = event->type =
CLUTTER_BUTTON_RELEASE;
                        event->button.time =
g_timer_elapsed(timer,NULL)*1000;
                        if (clicked)
                                event->button.modifier_state =
CLUTTER_BUTTON1_MASK;
                        else
                                event->motion.modifier_state = 0;
                                        
                        event->button.button = 1;
                        clicked = FALSE;
                        //dbg("CLUTTER_BUTTON_RELEASE!!!!\n");
                }
                
                clutter_event_put(event);  
                clutter_event_free(event); 

        }
        
        event = clutter_event_get();  

        if (event)
        {
                /* forward the event into clutter for emission etc. */
                clutter_threads_add_idle(fun_setmouse_x, NULL);
                clutter_do_event (event);
                clutter_event_free (event);
        }
        
out:
    clutter_threads_leave ();
    return;
}


gpointer touchscreen_thread(gpointer data)
{
        int fd;
        int nread;
        unsigned char buff[64];
        char *dev ="/dev/ttyS0";

                fd_set fds;
        int retval;

        fd = OpenDev(dev);
        if (fd>0)
                set_speed(fd);
        else
        {
                  printf("Can't Open Serial Port!\n");
                  exit(0);
        }
        int idx=0;
        while (1)
        {       
           FD_ZERO(&fds);
            FD_SET(fd,&fds);
            retval = select (fd+1, &fds, NULL, NULL, NULL);
            if (FD_ISSET(fd, &fds)) 
            {
                        nread = read(fd, (char*)(buff), 10);
                        if (nread < 0) 
                        {
                                continue;                       
                        }
                                
                         change_abs( buff[3] | (buff[4]<<8), buff[5] |
(buff[6]<<8), buff[2]); 
                }                                       
        }
        close(fd);
}

void touchscreen_driver_create()
{
        timer = g_timer_new();
        g_timer_start(timer);

        mouse =
clutter_texture_new_from_file("../image/skin/mouse.png",NULL);
        clutter_container_add_actor(CLUTTER_CONTAINER(gStage), mouse);
        GThread *thread = g_thread_create(touchscreen_thread, NULL, FALSE,
NULL);
        
}

_______________________________________________
clutter-app-devel-list mailing list
clutter-app-devel-list@clutter-project.org
http://lists.clutter-project.org/listinfo/clutter-app-devel-list

Reply via email to