1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#![deny(unused_imports)]
#![deny(unused_variables)]
#![allow(unstable)]
#[cfg(target_os="android")]
extern crate libc;
extern crate servo;
extern crate time;
extern crate "util" as servo_util;
#[cfg(all(feature = "glutin_app",not(test)))]
extern crate "glutin_app" as app;
#[cfg(all(feature = "glfw",not(test)))]
extern crate "glfw_app" as app;
#[cfg(not(test))]
extern crate compositing;
#[cfg(target_os="android")]
#[macro_use]
extern crate android_glue;
#[cfg(target_os="android")]
use libc::c_int;
#[cfg(not(test))]
use servo_util::opts;
#[cfg(not(test))]
use servo::Browser;
#[cfg(not(test))]
use compositing::windowing::WindowEvent;
#[cfg(target_os="android")]
use std::borrow::ToOwned;
#[cfg(not(any(test,target_os="android")))]
use std::os;
#[cfg(not(test))]
struct BrowserWrapper {
browser: Browser<app::window::Window>,
}
#[cfg(target_os="android")]
android_start!(main);
#[cfg(target_os="android")]
fn get_args() -> Vec<String> {
vec![
"servo".to_owned(),
"http://en.wikipedia.org/wiki/Rust".to_owned()
]
}
#[cfg(not(target_os="android"))]
fn get_args() -> Vec<String> {
os::args()
}
#[cfg(target_os="android")]
struct FilePtr(*mut libc::types::common::c95::FILE);
#[cfg(target_os="android")]
unsafe impl Send for FilePtr {}
#[cfg(target_os="android")]
fn redirect_output(file_no: c_int) {
use libc::funcs::posix88::unistd::{pipe, dup2};
use libc::funcs::posix88::stdio::fdopen;
use libc::funcs::c95::stdio::fgets;
use servo_util::task::spawn_named;
use std::mem;
use std::ffi::CString;
use std::str::from_utf8;
unsafe {
let mut pipes: [c_int; 2] = [ 0, 0 ];
pipe(pipes.as_mut_ptr());
dup2(pipes[1], file_no);
let mode = CString::from_slice("r".as_bytes());
let input_file = FilePtr(fdopen(pipes[0], mode.as_ptr()));
spawn_named("android-logger".to_owned(), move || {
loop {
let mut read_buffer: [u8; 1024] = mem::zeroed();
let FilePtr(input_file) = input_file;
fgets(read_buffer.as_mut_ptr() as *mut i8, read_buffer.len() as i32, input_file);
let cs = CString::from_slice(&read_buffer);
match from_utf8(cs.as_bytes()) {
Ok(s) => android_glue::write_log(s),
_ => {}
}
}
});
}
}
#[cfg(target_os="android")]
fn setup_logging() {
use libc::consts::os::posix88::{STDERR_FILENO, STDOUT_FILENO};
//os::setenv("RUST_LOG", "servo,gfx,msg,util,layers,js,std,rt,extra");
redirect_output(STDERR_FILENO);
redirect_output(STDOUT_FILENO);
}
#[cfg(not(target_os="android"))]
fn setup_logging() {
}
fn main() {
if opts::from_cmdline_args(get_args().as_slice()) {
setup_logging();
let window = if opts::get().headless {
None
} else {
Some(app::create_window())
};
let mut browser = BrowserWrapper {
browser: Browser::new(window.clone()),
};
match window {
None => {}
Some(ref window) => {
unsafe {
window.set_nested_event_loop_listener(&mut browser);
}
}
}
browser.browser.handle_event(WindowEvent::InitializeCompositing);
loop {
let should_continue = match window {
None => browser.browser.handle_event(WindowEvent::Idle),
Some(ref window) => {
let event = window.wait_events();
browser.browser.handle_event(event)
}
};
if !should_continue {
break
}
};
match window {
None => {}
Some(ref window) => {
unsafe {
window.remove_nested_event_loop_listener();
}
}
}
let BrowserWrapper {
browser
} = browser;
browser.shutdown();
}
}
impl app::NestedEventLoopListener for BrowserWrapper {
fn handle_event_from_nested_event_loop(&mut self, event: WindowEvent) -> bool {
let is_resize = match event {
WindowEvent::Resize(..) => true,
_ => false,
};
if !self.browser.handle_event(event) {
return false
}
if is_resize {
self.browser.repaint_synchronously()
}
true
}
}
|