1 // Copyright 2013-2015, The Gtk-rs Project Developers.
2 // See the COPYRIGHT file at the top-level directory of this distribution.
3 // Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
4
5 use gdk;
6 use glib;
7 use glib::translate::*;
8 use gtk_sys;
9 use libc::c_uint;
10 use std::cell::Cell;
11 use std::ptr;
12 use std::sync::atomic::{AtomicBool, Ordering, ATOMIC_BOOL_INIT};
13
14 #[cfg(target_os = "macos")]
15 extern "C" {
pthread_main_np() -> i3216 fn pthread_main_np() -> i32;
17 }
18
19 thread_local! {
20 static IS_MAIN_THREAD: Cell<bool> = Cell::new(false)
21 }
22
23 static INITIALIZED: AtomicBool = ATOMIC_BOOL_INIT;
24
25 /// Asserts that this is the main thread and `gtk::init` has been called.
26 macro_rules! assert_initialized_main_thread {
27 () => {
28 if !::rt::is_initialized_main_thread() {
29 if ::rt::is_initialized() {
30 panic!("GTK may only be used from the main thread.");
31 } else {
32 panic!("GTK has not been initialized. Call `gtk::init` first.");
33 }
34 }
35 };
36 }
37
38 /// No-op.
39 macro_rules! skip_assert_initialized {
40 () => {};
41 }
42
43 /// Asserts that `gtk::init` has not been called.
44 #[allow(unused_macros)]
45 macro_rules! assert_not_initialized {
46 () => {
47 if ::rt::is_initialized() {
48 panic!("This function has to be called before `gtk::init`.");
49 }
50 };
51 }
52
53 /// Returns `true` if GTK has been initialized.
54 #[inline]
is_initialized() -> bool55 pub fn is_initialized() -> bool {
56 skip_assert_initialized!();
57 INITIALIZED.load(Ordering::Acquire)
58 }
59
60 /// Returns `true` if GTK has been initialized and this is the main thread.
61 #[inline]
is_initialized_main_thread() -> bool62 pub fn is_initialized_main_thread() -> bool {
63 skip_assert_initialized!();
64 IS_MAIN_THREAD.with(|c| c.get())
65 }
66
67 /// Informs this crate that GTK has been initialized and the current thread is the main one.
set_initialized()68 pub unsafe fn set_initialized() {
69 skip_assert_initialized!();
70 if is_initialized_main_thread() {
71 return;
72 } else if is_initialized() {
73 panic!("Attempted to initialize GTK from two different threads.");
74 }
75
76 // OS X has its own notion of the main thread and init must be called on that thread.
77 #[cfg(target_os = "macos")]
78 {
79 if pthread_main_np() == 0 {
80 panic!("Attempted to initialize GTK on OSX from non-main thread");
81 }
82 }
83
84 gdk::set_initialized();
85 INITIALIZED.store(true, Ordering::Release);
86 IS_MAIN_THREAD.with(|c| c.set(true));
87 }
88
89 /// Tries to initialize GTK+.
90 ///
91 /// Call either this function or [`Application::new`][new] before using any
92 /// other GTK+ functions.
93 ///
94 /// [new]: struct.Application.html#method.new
95 ///
96 /// Note that this function calls `gtk_init_check()` rather than `gtk_init()`,
97 /// so will not cause the program to terminate if GTK could not be initialized.
98 /// Instead, an Ok is returned if the windowing system was successfully
99 /// initialized otherwise an Err is returned.
init() -> Result<(), glib::BoolError>100 pub fn init() -> Result<(), glib::BoolError> {
101 skip_assert_initialized!();
102 if is_initialized_main_thread() {
103 return Ok(());
104 } else if is_initialized() {
105 panic!("Attempted to initialize GTK from two different threads.");
106 }
107 unsafe {
108 if from_glib(gtk_sys::gtk_init_check(ptr::null_mut(), ptr::null_mut())) {
109 if !glib::MainContext::default().acquire() {
110 return Err(glib_bool_error!("Failed to acquire default main context"));
111 }
112
113 set_initialized();
114 Ok(())
115 } else {
116 Err(glib_bool_error!("Failed to initialize GTK"))
117 }
118 }
119 }
120
main_quit()121 pub fn main_quit() {
122 assert_initialized_main_thread!();
123 unsafe {
124 if gtk_sys::gtk_main_level() > 0 {
125 gtk_sys::gtk_main_quit();
126 } else if cfg!(debug_assertions) {
127 panic!("Attempted to quit a GTK main loop when none is running.");
128 }
129 }
130 }
131
get_major_version() -> u32132 pub fn get_major_version() -> u32 {
133 skip_assert_initialized!();
134 unsafe { gtk_sys::gtk_get_major_version() as u32 }
135 }
136
get_minor_version() -> u32137 pub fn get_minor_version() -> u32 {
138 skip_assert_initialized!();
139 unsafe { gtk_sys::gtk_get_minor_version() as u32 }
140 }
141
get_micro_version() -> u32142 pub fn get_micro_version() -> u32 {
143 skip_assert_initialized!();
144 unsafe { gtk_sys::gtk_get_micro_version() as u32 }
145 }
146
get_binary_age() -> u32147 pub fn get_binary_age() -> u32 {
148 skip_assert_initialized!();
149 unsafe { gtk_sys::gtk_get_binary_age() as u32 }
150 }
151
get_interface_age() -> u32152 pub fn get_interface_age() -> u32 {
153 skip_assert_initialized!();
154 unsafe { gtk_sys::gtk_get_interface_age() as u32 }
155 }
156
check_version( required_major: u32, required_minor: u32, required_micro: u32, ) -> Option<String>157 pub fn check_version(
158 required_major: u32,
159 required_minor: u32,
160 required_micro: u32,
161 ) -> Option<String> {
162 skip_assert_initialized!();
163 unsafe {
164 from_glib_none(gtk_sys::gtk_check_version(
165 required_major as c_uint,
166 required_minor as c_uint,
167 required_micro as c_uint,
168 ))
169 }
170 }
171