a9da8055fcd95c16f4c418ce02e4fdea69aa4042
[framework/uifw/elementary.git] / src / examples / efl_thread_2.c
1 #include <Elementary.h>
2 #include <pthread.h>
3
4 static Evas_Object *win = NULL;
5 static Evas_Object *rect = NULL;
6
7 struct info
8 {
9    double x, y;
10 };
11
12 static void *my_thread_mainloop_code(void *data);
13
14 static pthread_t thread_id;
15
16 // BEGIN - code running in my custom pthread instance
17 //
18 static void *
19 my_thread_run(void *arg)
20 {
21    double t = 0.0;
22
23    for (;;)
24      {
25         struct info *inf = malloc(sizeof(struct info));
26         
27         if (inf)
28           {
29              inf->x = 200 + (200 * sin(t));
30              inf->y = 200 + (200 * cos(t));
31              ecore_main_loop_thread_safe_call_sync
32                 (my_thread_mainloop_code, inf);
33           }
34         // and sleep and loop
35         usleep(1000);
36         t += 0.02;
37      }
38    return NULL;
39 }
40 //
41 // END - code running in my custom pthread instance
42 static void
43 my_thread_new(void)
44 {
45    pthread_attr_t attr;
46    
47    if (pthread_attr_init(&attr) != 0)
48       perror("pthread_attr_init");
49    if (pthread_create(&thread_id, &attr, my_thread_run, NULL) != 0)
50       perror("pthread_create");
51 }
52
53 static void *
54 my_thread_mainloop_code(void *data)
55 {
56    struct info *inf = data;
57    evas_object_move(rect, inf->x - 50, inf->y - 50);
58    free(inf);
59    return NULL;
60 }
61
62 int
63 elm_main(int argc, char **argv)
64 {
65    Evas_Object *o, *bg;
66    
67    win = elm_win_add(NULL, "efl-thread-2", ELM_WIN_BASIC);
68    elm_win_title_set(win, "EFL Thread 2");
69    evas_object_resize(win, 400, 400);
70    evas_object_show(win);
71    
72    bg = elm_bg_add(win);
73    elm_win_resize_object_add(win, bg);
74    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
75    evas_object_show(bg);
76    
77    o = evas_object_rectangle_add(evas_object_evas_get(win));
78    evas_object_color_set(o, 50, 80, 180, 255);
79    evas_object_resize(o, 100, 100);
80    evas_object_show(o);
81    rect = o;
82    
83    // create custom thread to do some "work on the side"
84    my_thread_new();
85    
86    elm_run();
87    return 0;
88 }
89  
90 ELM_MAIN()