[access] move mouse postion if an object has a highlight by highlight_cycle(); not...
[framework/uifw/elementary.git] / src / examples / efl_thread_win32_4.c
1 //Compile with:
2 //gcc -o efl_thread_4 efl_thread_win32_4.c -g `pkg-config --cflags --libs elementary`
3 #include <Elementary.h>
4 #define WIN32_LEAN_AND_MEAN
5 #include <windows.h>
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 static void my_thread_mainloop_code(void *data);
16
17 static HANDLE thread;
18 static CRITICAL_SECTION lock;
19 static int th_exit = 0;
20
21 // BEGIN - code running in my custom win32 thread instance
22 //
23 static DWORD WINAPI
24 my_thread_run(LPVOID arg)
25 {
26    double t = 0.0;
27
28    // inside the thread function lets loop forever incrimenting a time point
29    for (;;)
30      {
31         struct info *inf = malloc(sizeof(struct info));
32         int do_exit;
33
34         if (inf)
35           {
36              inf->x = 200 + (200 * sin(t));
37              inf->y = 200 + (200 * cos(t));
38              // now call a function in the mainloop and pass it our allocated
39              // data that it will free when it gets it
40              ecore_main_loop_thread_safe_call_async
41                 (my_thread_mainloop_code, inf);
42           }
43         // and sleep and loop
44         usleep(1000);
45         t += 0.02;
46         // in case someone has asked us to cancel - then cancel this loop
47         // co-operatively (cancelling is co-operative)
48         EnterCriticalSection(&lock);
49         do_exit = th_exit;
50         LeaveCriticalSection(&lock);
51         if (do_exit) break;
52      }
53    DeleteCriticalSection(&lock);
54    return NULL;
55 }
56 //
57 // END - code running in my custom win32 thread instance
58
59 static void
60 my_thread_new(void)
61 {
62   InitializeCriticalSection(&lock);
63   thread = CreateThread(NULL, 0, my_thread_run, NULL, 0, NULL);
64   if (!thread)
65     {
66        char *str = evil_last_error_get();
67        if (str)
68          {
69             fprintf("thread creation failed: %s\n", str);
70             free(str);
71          }
72     }
73 }
74
75 static void
76 my_thread_mainloop_code(void *data)
77 {
78    struct info *inf = data;
79    evas_object_move(rect, inf->x - 50, inf->y - 50);
80    free(inf);
81 }
82
83 // just test cancelling the thread
84 static void
85 down(void *data, Evas *e, Evas_Object *obj, void *event_info)
86 {
87    EnterCriticalSection(&lock);
88    th_exit = 1;
89    LeaveCriticalSection(&lock);
90 }
91
92 EAPI_MAIN int
93 elm_main(int argc, char **argv)
94 {
95    Evas_Object *o, *bg;
96
97    win = elm_win_add(NULL, "efl-thread-4", ELM_WIN_BASIC);
98    elm_win_title_set(win, "EFL Thread 4");
99    elm_win_autodel_set(win, EINA_TRUE);
100    elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
101    evas_object_resize(win, 400, 400);
102    evas_object_show(win);
103
104    bg = elm_bg_add(win);
105    evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
106    elm_win_resize_object_add(win, bg);
107    evas_object_show(bg);
108
109    o = evas_object_rectangle_add(evas_object_evas_get(win));
110    evas_object_color_set(o, 50, 80, 180, 255);
111    evas_object_resize(o, 100, 100);
112    evas_object_show(o);
113    // new in the examples - we have a mouse down on the blue box cancel
114    // the thread
115    evas_object_event_callback_add(o, EVAS_CALLBACK_MOUSE_DOWN, down, NULL);
116    rect = o;
117
118    // create custom thread to do some "work on the side"
119    my_thread_new();
120
121    elm_run();
122    elm_shutdown();
123
124    return 0;
125 }
126 ELM_MAIN()