Cleanup: remove contributors for CMake files
[blender.git] / intern / ghost / intern / GHOST_TaskbarX11.cpp
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file ghost/intern/GHOST_TaskbarX11.cpp
18  *  \ingroup GHOST
19  */
20
21 #include "GHOST_TaskbarX11.h"
22
23 #include <dlfcn.h>
24 #include <cstdio>
25 #include <cassert>
26 #include <cstdlib>
27
28 typedef void*(*unity_get_entry_t)(const char*);
29 typedef void(*unity_set_progress_t)(void*, double);
30 typedef void(*unity_set_progress_visible_t)(void*, int);
31 typedef int(*unity_event_loop_t)(void*, int);
32
33 static unity_get_entry_t unity_get_entry;
34 static unity_set_progress_t unity_set_progress;
35 static unity_set_progress_visible_t unity_set_progress_visible;
36 static unity_event_loop_t unity_event_loop;
37
38 static bool libunity_initialized = false;
39 static bool libunity_available = false;
40 static void *libunity_handle = NULL;
41
42 void GHOST_TaskBarX11::free()
43 {
44         if(libunity_handle) {
45                 dlclose(libunity_handle);
46                 libunity_handle = NULL;
47         }
48 }
49
50 bool GHOST_TaskBarX11::init()
51 {
52         if(libunity_initialized) {
53                 return libunity_available;
54         }
55
56         libunity_initialized = true;
57
58         const char *libunity_names[] = {"libunity.so.4", "libunity.so.6", "libunity.so.9", "libunity.so", NULL};
59         for(int i = 0; libunity_names[i]; i++) {
60                 libunity_handle = dlopen(libunity_names[i], RTLD_LAZY);
61                 if(libunity_handle) {
62                         break;
63                 }
64         }
65
66         if(!libunity_handle) {
67                 return false;
68         }
69
70         unity_get_entry = (unity_get_entry_t) dlsym(libunity_handle, "unity_launcher_entry_get_for_desktop_id");
71         if(!unity_get_entry) {
72                 fprintf(stderr, "failed to load libunity: %s\n", dlerror());
73                 return false;
74         }
75         unity_set_progress = (unity_set_progress_t) dlsym(libunity_handle, "unity_launcher_entry_set_progress");
76         if(!unity_set_progress) {
77                 fprintf(stderr, "failed to load libunity: %s\n", dlerror());
78                 return false;
79         }
80         unity_set_progress_visible = (unity_set_progress_visible_t) dlsym(libunity_handle, "unity_launcher_entry_set_progress_visible");
81         if(!unity_set_progress_visible) {
82                 fprintf(stderr, "failed to load libunity: %s\n", dlerror());
83                 return false;
84         }
85         unity_event_loop = (unity_event_loop_t) dlsym(libunity_handle, "g_main_context_iteration");
86         if(!unity_event_loop) {
87                 fprintf(stderr, "failed to load libunity: %s\n", dlerror());
88                 return false;
89         }
90
91         atexit(GHOST_TaskBarX11::free);
92
93         libunity_available = true;
94         return true;
95 }
96
97 GHOST_TaskBarX11::GHOST_TaskBarX11(const char *name)
98 {
99         if(GHOST_TaskBarX11::init()) {
100                 handle = unity_get_entry(name);
101         }
102         else {
103                 handle = NULL;
104         }
105 }
106
107 bool GHOST_TaskBarX11::is_valid()
108 {
109         return (handle != NULL);
110 }
111
112 void GHOST_TaskBarX11::set_progress(double progress)
113 {
114         assert(is_valid());
115         unity_set_progress(handle, progress);
116 }
117
118 void GHOST_TaskBarX11::set_progress_enabled(bool enabled)
119 {
120         assert(is_valid());
121         unity_set_progress_visible(handle, enabled ? 1 : 0);
122         unity_event_loop(NULL, 0);
123 }