The elm_toolbar_iteams_count have to return only the number of manually added items.
[framework/uifw/elementary.git] / src / examples / efl_thread_4.c
1 //Compile with:
2 //gcc -o efl_thread_4 efl_thread_4.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 struct info
10 {
11    double x, y;
12 };
13
14 static void my_thread_mainloop_code(void *data);
15
16 static pthread_t thread_id;
17 static pthread_mutex_t th_lock;
18 static int th_exit = 0;
19
20 // BEGIN - code running in my custom pthread instance
21 //
22 static void *
23 my_thread_run(void *arg)
24 {
25    double t = 0.0;
26
27    // inside the pthread function lets loop forever incrimenting a time point
28    for (;;)
29      {
30         struct info *inf = malloc(sizeof(struct info));
31         int do_exit;
32
33         if (inf)
34           {
35              inf->x = 200 + (200 * sin(t));
36              inf->y = 200 + (200 * cos(t));
37              // now call a function in the mainloop and pass it our allocated
38              // data that it will free when it gets it
39              ecore_main_loop_thread_safe_call_async
40                 (my_thread_mainloop_code, inf);
41           }
42         // and sleep and loop
43         usleep(1000);
44         t += 0.02;
45         // in case someone has asked us to cancel - then cancel this loop
46         // co-operatively (cancelling is co-operative)
47         pthread_mutex_lock(&th_lock);
48         do_exit = th_exit;
49         pthread_mutex_unlock(&th_lock);
50         if (do_exit) break;
51      }
52    return NULL;
53 }
54 //
55 // END - code running in my custom pthread instance
56
57 static void
58 my_thread_new(void)
59 {
60    pthread_attr_t attr;
61
62    pthread_mutex_init(&th_lock, NULL);
63    if (pthread_attr_init(&attr) != 0)
64       perror("pthread_attr_init");
65    if (pthread_create(&thread_id, &attr, my_thread_run, NULL) != 0)
66       perror("pthread_create");
67 }
68
69 static void
70 my_thread_mainloop_code(void *data)
71 {
72    struct info *inf = data;
73    evas_object_move(rect, inf->x - 50, inf->y - 50);
74    free(inf);
75 }
76
77 // just test cancelling the thread
78 static void
79 down(void *data, Evas *e, Evas_Object *obj, void *event_info)
80 {
81    pthread_mutex_lock(&th_lock);
82    th_exit = 1;
83    pthread_mutex_unlock(&th_lock);
84 }
85
86 EAPI_MAIN int
87 elm_main(int argc, char **argv)
88 {
89    Evas_Object *o, *bg;
90
91    win = elm_win_add(NULL, "efl-thread-4", ELM_WIN_BASIC);
92    elm_win_title_set(win, "EFL Thread 4");
93    elm_win_autodel_set(win, EINA_TRUE);
94    elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
95    evas_object_resize(win, 400, 400);
96    evas_object_show(win);
97
98    bg = elm_bg_add(win);
99    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
100    elm_win_resize_object_add(win, bg);
101    evas_object_show(bg);
102
103    o = evas_object_rectangle_add(evas_object_evas_get(win));
104    evas_object_color_set(o, 50, 80, 180, 255);
105    evas_object_resize(o, 100, 100);
106    evas_object_show(o);
107    // new in the examples - we have a mouse down on the blue box cancel
108    // the thread
109    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, down, NULL);
110    rect = o;
111
112    // create custom thread to do some "work on the side"
113    my_thread_new();
114
115    elm_run();
116    elm_shutdown();
117
118    return 0;
119 }
120 ELM_MAIN()