[Issue fix]: background not deleted even when selector was deleted.
[framework/uifw/elementary.git] / src / examples / efl_thread_5.c
1 #include <Elementary.h>
2
3 static Ecore_Thread *th = NULL;
4
5 static Evas_Object *win = NULL;
6 static Evas_Object *rect = NULL;
7
8 struct info
9 {
10    double x, y;
11 };
12
13 // BEGIN - code running in my custom pthread instance
14 //
15 static void
16 th_do(void *data, Ecore_Thread *th)
17 {
18    double t = 0.0;
19    
20    // inside our "do" function for the ecore thread, lets do the real work
21    for (;;)
22      {
23         struct info *inf = malloc(sizeof(struct info));
24         
25         if (inf)
26           {
27              inf->x = 200 + (200 * sin(t));
28              inf->y = 200 + (200 * cos(t));
29              // now we have recorded the timepoint we pass it as feedback
30              // back to the mainloop. it will free it when done
31              ecore_thread_feedback(th, inf);
32           }
33         // and sleep and loop
34         usleep(1000);
35         t += 0.02;
36         // in case someone has asked us to cancel - then cancel this loop
37         // co-operatively (cancelling is co-operative)
38         if (ecore_thread_check(th)) break;
39      }
40 }
41 //
42 // END - code running in my custom pthread instance
43
44 static void // when mainloop gets feedback from worker
45 th_feedback(void *data, Ecore_Thread *th, void *msg)
46 {
47    struct info *inf = msg;
48    evas_object_move(rect, inf->x - 50, inf->y - 50);
49    free(inf);
50 }
51
52 // BONUS (optional): called after th_do returns and has NOT been cancelled
53 static void th_end(void *data, Ecore_Thread *th) { printf("thread ended\n"); }
54 // BONUS (optional): called in mainloop AFTER thread has finished cancelling
55 static void th_cancel(void *data, Ecore_Thread *th) { printf("thread cancelled\n"); }
56
57 // just test cancelling the thread worker
58 static void
59 down(void *data, Evas *e, Evas_Object *obj, void *event_info)
60 {
61    ecore_thread_cancel(th);
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-5", ELM_WIN_BASIC);
70    elm_win_title_set(win, "EFL Thread 5");
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    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, down, NULL);
84    rect = o;
85    
86    // explicitly create ecore thread to do some "work on the side" and pass
87    // in NULL as data ptr to callbacks and true at the end means to actually
88    // make a new thread and not use the thread pool (there is a thread pool
89    // with as many thread workers as there are cpu's so this means you do not
90    // overload the cpu's with more work than you actually have processing
91    // units *IF* your threads do actually spend their time doing actual
92    // heavy computation)
93    th = ecore_thread_feedback_run(th_do, th_feedback, th_end, th_cancel, 
94                                   NULL, EINA_TRUE);
95    elm_run();
96    return 0;
97 }
98
99 ELM_MAIN()