3019270d577d3a181a0a75aa7102fa389107739a
[framework/uifw/elementary.git] / src / examples / efl_thread_3.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    // inside the pthread function lets loop forever incrimenting a time point
24    for (;;)
25      {
26         struct info *inf = malloc(sizeof(struct info));
27         
28         if (inf)
29           {
30              inf->x = 200 + (200 * sin(t));
31              inf->y = 200 + (200 * cos(t));
32              // now call a function in the mainloop and pass it our allocated
33              // data that it will free when it gets it
34              ecore_main_loop_thread_safe_call_async
35                 (my_thread_mainloop_code, inf);
36           }
37         // and sleep and loop
38         usleep(1000);
39         t += 0.02;
40      }
41    return NULL;
42 }
43 //
44 // END - code running in my custom pthread instance
45 static void
46 my_thread_new(void)
47 {
48    pthread_attr_t attr;
49    
50    if (pthread_attr_init(&attr) != 0)
51       perror("pthread_attr_init");
52    if (pthread_create(&thread_id, &attr, my_thread_run, NULL) != 0)
53       perror("pthread_create");
54 }
55
56 static void
57 my_thread_mainloop_code(void *data)
58 {
59    struct info *inf = data;
60    evas_object_move(rect, inf->x - 50, inf->y - 50);
61    free(inf);
62 }
63
64 int
65 elm_main(int argc, char **argv)
66 {
67    Evas_Object *o, *bg;
68    
69    win = elm_win_add(NULL, "efl-thread-3", ELM_WIN_BASIC);
70    elm_win_title_set(win, "EFL Thread 3");
71    evas_object_resize(win, 400, 400);
72    evas_object_show(win);
73    
74    bg = elm_bg_add(win);
75    elm_win_resize_object_add(win, bg);
76    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
77    evas_object_show(bg);
78    
79    o = evas_object_rectangle_add(evas_object_evas_get(win));
80    evas_object_color_set(o, 50, 80, 180, 255);
81    evas_object_resize(o, 100, 100);
82    evas_object_show(o);
83    rect = o;
84    
85    // create custom thread to do some "work on the side"
86    my_thread_new();
87    
88    elm_run();
89    return 0;
90 }
91
92 ELM_MAIN()
93