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 thread_local! {
15 static IS_MAIN_THREAD: Cell<bool> = Cell::new(false)
16 }
17
18 static INITIALIZED: AtomicBool = ATOMIC_BOOL_INIT;
19
20 /// Asserts that this is the main thread and `gtk::init` has been called.
21 macro_rules! assert_initialized_main_thread {
22 () => {
23 if !::rt::is_initialized_main_thread() {
24 if ::rt::is_initialized() {
25 panic!("GTK may only be used from the main thread.");
26 } else {
27 panic!("GTK has not been initialized. Call `gtk::init` first.");
28 }
29 }
30 };
31 }
32
33 /// No-op.
34 macro_rules! skip_assert_initialized {
35 () => {};
36 }
37
38 /// Asserts that `gtk::init` has not been called.
39 #[allow(unused_macros)]
40 macro_rules! assert_not_initialized {
41 () => {
42 if ::rt::is_initialized() {
43 panic!("This function has to be called before `gtk::init`.");
44 }
45 };
46 }
47
48 /// Returns `true` if GTK has been initialized.
49 #[inline]
is_initialized() -> bool50 pub fn is_initialized() -> bool {
51 skip_assert_initialized!();
52 INITIALIZED.load(Ordering::Acquire)
53 }
54
55 /// Returns `true` if GTK has been initialized and this is the main thread.
56 #[inline]
is_initialized_main_thread() -> bool57 pub fn is_initialized_main_thread() -> bool {
58 skip_assert_initialized!();
59 IS_MAIN_THREAD.with(|c| c.get())
60 }
61
62 /// Informs this crate that GTK has been initialized and the current thread is the main one.
set_initialized()63 pub unsafe fn set_initialized() {
64 skip_assert_initialized!();
65 if is_initialized_main_thread() {
66 return;
67 } else if is_initialized() {
68 panic!("Attempted to initialize GTK from two different threads.");
69 }
70 gdk::set_initialized();
71 INITIALIZED.store(true, Ordering::Release);
72 IS_MAIN_THREAD.with(|c| c.set(true));
73 }
74
75 /// Tries to initialize GTK+.
76 ///
77 /// Call either this function or [`Application::new`][new] before using any
78 /// other GTK+ functions.
79 ///
80 /// [new]: struct.Application.html#method.new
81 ///
82 /// Note that this function calls `gtk_init_check()` rather than `gtk_init()`,
83 /// so will not cause the program to terminate if GTK could not be initialized.
84 /// Instead, an Ok is returned if the windowing system was successfully
85 /// initialized otherwise an Err is returned.
init() -> Result<(), glib::BoolError>86 pub fn init() -> Result<(), glib::BoolError> {
87 skip_assert_initialized!();
88 if is_initialized_main_thread() {
89 return Ok(());
90 } else if is_initialized() {
91 panic!("Attempted to initialize GTK from two different threads.");
92 }
93 unsafe {
94 if from_glib(gtk_sys::gtk_init_check(ptr::null_mut(), ptr::null_mut())) {
95 if !glib::MainContext::default().acquire() {
96 return Err(glib_bool_error!("Failed to acquire default main context"));
97 }
98
99 set_initialized();
100 Ok(())
101 } else {
102 Err(glib_bool_error!("Failed to initialize GTK"))
103 }
104 }
105 }
106
main_quit()107 pub fn main_quit() {
108 assert_initialized_main_thread!();
109 unsafe {
110 if gtk_sys::gtk_main_level() > 0 {
111 gtk_sys::gtk_main_quit();
112 } else if cfg!(debug_assertions) {
113 panic!("Attempted to quit a GTK main loop when none is running.");
114 }
115 }
116 }
117
get_major_version() -> u32118 pub fn get_major_version() -> u32 {
119 skip_assert_initialized!();
120 unsafe { gtk_sys::gtk_get_major_version() as u32 }
121 }
122
get_minor_version() -> u32123 pub fn get_minor_version() -> u32 {
124 skip_assert_initialized!();
125 unsafe { gtk_sys::gtk_get_minor_version() as u32 }
126 }
127
get_micro_version() -> u32128 pub fn get_micro_version() -> u32 {
129 skip_assert_initialized!();
130 unsafe { gtk_sys::gtk_get_micro_version() as u32 }
131 }
132
get_binary_age() -> u32133 pub fn get_binary_age() -> u32 {
134 skip_assert_initialized!();
135 unsafe { gtk_sys::gtk_get_binary_age() as u32 }
136 }
137
get_interface_age() -> u32138 pub fn get_interface_age() -> u32 {
139 skip_assert_initialized!();
140 unsafe { gtk_sys::gtk_get_interface_age() as u32 }
141 }
142
check_version( required_major: u32, required_minor: u32, required_micro: u32, ) -> Option<String>143 pub fn check_version(
144 required_major: u32,
145 required_minor: u32,
146 required_micro: u32,
147 ) -> Option<String> {
148 skip_assert_initialized!();
149 unsafe {
150 from_glib_none(gtk_sys::gtk_check_version(
151 required_major as c_uint,
152 required_minor as c_uint,
153 required_micro as c_uint,
154 ))
155 }
156 }
157