Initialize Tizen 2.3
[framework/uifw/elementary.git] / src / examples / efl_thread_1.c
1 //Compile with:
2 //gcc -o efl_thread_1 efl_thread_1.c -g `pkg-config --cflags --libs elementary`
3 #include <Elementary.h>
4 #include <pthread.h>
5
6 static Evas_Object *win = NULL;
7 static Evas_Object *rect = NULL;
8
9 static pthread_t thread_id;
10
11 // BEGIN - code running in my custom pthread instance
12 //
13 static void *
14 my_thread_run(void *arg)
15 {
16    double t = 0.0;
17
18    for (;;)
19      {
20         ecore_thread_main_loop_begin(); // begin critical
21           { // indented for illustration of "critical" block
22              Evas_Coord x, y;
23
24              x = 200 + (200 * sin(t));
25              y = 200 + (200 * cos(t));
26              evas_object_move(rect, x - 50, y - 50);
27           }
28         ecore_thread_main_loop_end(); // end critical
29         usleep(1000);
30         t += 0.02;
31      }
32    return NULL;
33 }
34 //
35 // END - code running in my custom pthread instance
36
37 static void
38 my_thread_new(void)
39 {
40    pthread_attr_t attr;
41
42    if (pthread_attr_init(&attr) != 0)
43       perror("pthread_attr_init");
44    if (pthread_create(&thread_id, &attr, my_thread_run, NULL) != 0)
45       perror("pthread_create");
46 }
47
48 EAPI_MAIN int
49 elm_main(int argc, char **argv)
50 {
51    Evas_Object *o, *bg;
52
53    win = elm_win_add(NULL, "efl-thread-1", ELM_WIN_BASIC);
54    elm_win_title_set(win, "EFL Thread 1");
55    elm_win_autodel_set(win, EINA_TRUE);
56    elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
57    evas_object_resize(win, 400, 400);
58    evas_object_show(win);
59
60    bg = elm_bg_add(win);
61    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
62    elm_win_resize_object_add(win, bg);
63    evas_object_show(bg);
64
65    o = evas_object_rectangle_add(evas_object_evas_get(win));
66    evas_object_color_set(o, 50, 80, 180, 255);
67    evas_object_resize(o, 100, 100);
68    evas_object_show(o);
69    rect = o;
70
71    // create custom thread to do some "work on the side"
72    my_thread_new();
73
74    elm_run();
75    elm_shutdown();
76
77    return 0;
78 }
79 ELM_MAIN()