Mobile native

[UI Sample] Ecore Thread 1 Sample Overview

The Ecore Thread 1 sample demonstrates how to move a button using EFL thread-related functions. The functions act on the Ecore main loop itself or on events and infrastructures directly linked to it.

The following figure illustrates the Ecore Thread 1 sample screen.

Figure: Ecore Thread 1 screen

Ecore Thread 1 screen

Implementation

The create_base_gui() function creates a thread using the pthread_create() function and registering a thread_run() callback function while passing a button to the callback.

#include <Elementary.h>
#include <pthread.h>

static void
create_base_gui(appdata_s *ad)
{
   // Window
   ad->win = elm_win_util_standard_add(PACKAGE, PACKAGE);
   elm_win_autodel_set(ad->win, EINA_TRUE);

   evas_object_smart_callback_add(ad->win, "delete,request", win_delete_request_cb, NULL);
   eext_object_event_callback_add(ad->win, EEXT_CALLBACK_BACK, win_back_cb, ad);

   Evas_Object *btn;

   // Create a button
   btn = elm_button_add(ad->win);
   elm_object_text_set(btn, "Ecore</br>Thread</br>Main</br>Loop");
   evas_object_resize(btn, 100, 100);
   evas_object_show(btn);

   // Create a thread
   if (!pthread_create(&thread_id, NULL, thread_run, btn))
      perror("pthread_create!\n");

   // Show the window after the base GUI is set up
   evas_object_show(ad->win);
}

The ecore_thread_main_loop_begin() and ecore_thread_main_loop_end() functions allow you to call the evas_object_move() function in critical sections. The ecore_thread_main_loop_begin() function can block the section for a while, and mutual exclusion is performed in the main loop.

static void *thread_run(void *arg)
{
   Evas_Object *btn = arg;
   double t = 0.0;
   Evas_Coord x, y;

   while (!thread_finish)
   {
      x = 150 + (150 * sin(t));
      y = 150 + (150 * cos(t));

      // Begin critical section
      ecore_thread_main_loop_begin();
      {
         evas_object_move(btn, x, y);
      }
      // End critical section
      ecore_thread_main_loop_end();

      usleep(1000);
      t += 0.001;
   }

   pthread_exit(NULL);

   return NULL;
}

static void win_del_cb(void *data, Evas_Object *obj, void *event_info)
{
   void *thread_result;
   thread_finish = EINA_TRUE;
   pthread_join(thread_id, &thread_result);
   elm_exit();
}
Go to top