Fixing bugs in examples.
[framework/uifw/elementary.git] / src / examples / efl_thread_5.c
1 //Compile with:
2 //gcc -o efl_thread_5 efl_thread_5.c -g `pkg-config --cflags --libs elementary`
3 #include <Elementary.h>
4
5 static Ecore_Thread *th = NULL;
6
7 static Evas_Object *win = NULL;
8 static Evas_Object *rect = NULL;
9
10 struct info
11 {
12    double x, y;
13 };
14
15 // BEGIN - code running in my custom pthread instance
16 //
17 static void
18 th_do(void *data, Ecore_Thread *th)
19 {
20    double t = 0.0;
21    
22    // inside our "do" function for the ecore thread, lets do the real work
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              // now we have recorded the timepoint we pass it as feedback
32              // back to the mainloop. it will free it when done
33              ecore_thread_feedback(th, inf);
34           }
35         // and sleep and loop
36         usleep(1000);
37         t += 0.02;
38         // in case someone has asked us to cancel - then cancel this loop
39         // co-operatively (cancelling is co-operative)
40         if (ecore_thread_check(th)) break;
41      }
42 }
43 //
44 // END - code running in my custom pthread instance
45
46 static void // when mainloop gets feedback from worker
47 th_feedback(void *data, Ecore_Thread *th, void *msg)
48 {
49    struct info *inf = msg;
50    evas_object_move(rect, inf->x - 50, inf->y - 50);
51    free(inf);
52 }
53
54 // BONUS (optional): called after th_do returns and has NOT been cancelled
55 static void th_end(void *data, Ecore_Thread *th) { printf("thread ended\n"); }
56 // BONUS (optional): called in mainloop AFTER thread has finished cancelling
57 static void th_cancel(void *data, Ecore_Thread *th) { printf("thread cancelled\n"); }
58
59 // just test cancelling the thread worker
60 static void
61 down(void *data, Evas *e, Evas_Object *obj, void *event_info)
62 {
63    ecore_thread_cancel(th);
64 }
65
66 int
67 elm_main(int argc, char **argv)
68 {
69    Evas_Object *o, *bg;
70    
71    win = elm_win_add(NULL, "efl-thread-5", ELM_WIN_BASIC);
72    elm_win_title_set(win, "EFL Thread 5");
73    elm_win_autodel_set(win, EINA_TRUE);
74    elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
75    evas_object_resize(win, 400, 400);
76    evas_object_show(win);
77    
78    bg = elm_bg_add(win);
79    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
80    elm_win_resize_object_add(win, bg);
81    evas_object_show(bg);
82    
83    o = evas_object_rectangle_add(evas_object_evas_get(win));
84    evas_object_color_set(o, 50, 80, 180, 255);
85    evas_object_resize(o, 100, 100);
86    evas_object_show(o);
87    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, down, NULL);
88    rect = o;
89    
90    // explicitly create ecore thread to do some "work on the side" and pass
91    // in NULL as data ptr to callbacks and true at the end means to actually
92    // make a new thread and not use the thread pool (there is a thread pool
93    // with as many thread workers as there are cpu's so this means you do not
94    // overload the cpu's with more work than you actually have processing
95    // units *IF* your threads do actually spend their time doing actual
96    // heavy computation)
97    th = ecore_thread_feedback_run(th_do, th_feedback, th_end, th_cancel, 
98                                   NULL, EINA_TRUE);
99    elm_run();
100    return 0;
101 }
102
103 ELM_MAIN()