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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use std::sync::Arc;
use vulkano::swapchain::Surface;
use super::BasaltWindow;
use ::Options as BasaltOptions;
use vulkano::instance::Instance;
use parking_lot::{Mutex,Condvar};
use Basalt;
use std::thread;
use std::mem::transmute;
use bindings::xinput2 as xi;
use input::qwery::*;
use input::{Event,MouseButton};
use std::slice;
use std::sync::atomic::{self,AtomicPtr};
use std::ffi::CString;

#[allow(dead_code)]
pub struct X11Window {
	display: AtomicPtr<xi::Display>,
	window: xi::Window,
	basalt: Mutex<Option<Arc<Basalt>>>,
	basalt_ready: Condvar,
}

impl BasaltWindow for X11Window {
	fn capture_cursor(&self) {
		println!("capture_cursor() not implemented!");
	}
	
	fn release_cursor(&self) {
		println!("release_cursor() not implemented!");
	}
	
	fn cursor_captured(&self) -> bool {
		println!("cursor_captured() not implemented!");
		false
	}
	
	fn enable_fullscreen(&self) {
		println!("enable_fullscreen() not implemented!");
	}
	
	fn disable_fullscreen(&self) {
		println!("disable_fullscreen() not implemented!");
	}
	
	fn request_resize(&self, _width: u32, _height: u32) {
		println!("request_resize() not implemented!");
	}
	
	fn attach_basalt(&self, basalt: Arc<Basalt>) {
		*self.basalt.lock() = Some(basalt);
		self.basalt_ready.notify_one();
	}
}

pub fn open_surface(ops: BasaltOptions, instance: Arc<Instance>) -> Result<Arc<Surface<Arc<dyn BasaltWindow + Send + Sync>>>, String> {
	unsafe {
		let display = match xi::XOpenDisplay(::std::ptr::null_mut()) {
			e if e.is_null() => panic!("unable to open x display"),
			d => d
		};
		
		let mut opcode = 0;
		let mut event = 0;
		let mut error = 0;
		let ext_name = CString::new("XInputExtension").unwrap();
		
		if xi::XQueryExtension(display, ext_name.as_ptr(), &mut opcode, &mut event, &mut error) == xi::False as i32 {
			return Err(format!("XQueryExtension failed: opcode {}, event {}, error {}", opcode, event, error));
		}
		
		drop(opcode);
		drop(event);
		drop(error);
		drop(ext_name);
		let mut major = xi::XI_2_Major as i32;
		let mut minor = xi::XI_2_Minor as i32;
		
		match xi::XIQueryVersion(display, &mut major, &mut minor) as u32 {
			xi::Success => (),
			xi::BadRequest => return Err(format!("XIQueryVersion BadRequest")),
			_ => return Err(format!("XIQueryVersion Internal Error"))
		}
		
		drop(major);
		drop(minor);
		
		let root = match xi::XDefaultRootWindow(display) {
			0 => panic!("unable to open root window"),
			r => r
		};
		
		let mut attrs: xi::XSetWindowAttributes = ::std::mem::zeroed();
		attrs.event_mask = (xi::XI_RawKeyPressMask
			| xi::XI_RawKeyReleaseMask
			| xi::XI_RawButtonPressMask
			| xi::XI_RawButtonReleaseMask
			| xi::XI_RawMotionMask
			| xi::XI_MotionMask
			| xi::XI_EnterMask
			| xi::XI_LeaveMask
			| xi::XI_FocusInMask
			| xi::XI_FocusOutMask) as i64;
		
		
		let screen = xi::XDefaultScreen(display);
		let window = xi::XCreateWindow(
			display,
			root,
			0,
			0,
			ops.window_size[0],
			ops.window_size[1],
			0,
			xi::CopyFromParent as i32,
			xi::InputOutput,
			::std::ptr::null_mut(),
			xi::CWBorderPixel as u64 | xi::CWColormap as u64 | xi::CWEventMask as u64,
			&mut attrs
		);
			
		
		/*let window = xi::XCreateSimpleWindow(
			display,
			root,
			0,
			0,
			ops.window_size[0],
			ops.window_size[1],
			0,
			xi::XWhitePixel(display, screen), 
			xi::XBlackPixel(display, screen)
		);*/
		
		drop(screen);
		drop(root);
		
		let mut mask = xi::XIEventMask {
			deviceid: xi::XIAllDevices as i32,
			mask_len: 4,
			mask: transmute(&mut (
				  xi::XI_RawKeyPressMask
				| xi::XI_RawKeyReleaseMask
				| xi::XI_RawButtonPressMask
				| xi::XI_RawButtonReleaseMask
				| xi::XI_RawMotionMask
				| xi::XI_MotionMask
				| xi::XI_EnterMask
				| xi::XI_LeaveMask
				| xi::XI_FocusInMask
				| xi::XI_FocusOutMask
			))
		};
		
		match xi::XISelectEvents(display, window, &mut mask, 1) as u32 {
			xi::Success => (),
			xi::BadValue => return Err(format!("XISelectEvents BadValue")),
			xi::BadWindow => return Err(format!("XISelectEvents BadWindow")),
			_ => return Err(format!("XISelectEvents Interal Error"))
		}
		
		::std::mem::forget(mask); // TODO: Don't do this
			
		match xi::XMapWindow(display, window) as u32 {
			xi::BadWindow => return Err(format!("XMapWindow BadWindow")),
			_ => ()
		}
			
		let x11window = Arc::new(X11Window {
			display: AtomicPtr::new(display),
			window,
			basalt: Mutex::new(None),
			basalt_ready: Condvar::new()
		});
		
		let surface = Surface::from_xlib(
			instance,
			display,
			window,
			x11window.clone() as Arc<dyn BasaltWindow + Send + Sync>,
		).map_err(|e| format!("failed to create surface: {}", e))?;
		
		thread::spawn(move || {
			println!("1");
			let mut basalt_lk = x11window.basalt.lock();
			
			while basalt_lk.is_none() {
				x11window.basalt_ready.wait(&mut basalt_lk);
			}
			println!("2");
			
			let basalt = basalt_lk.take().unwrap();
			drop(basalt_lk);
			
			let display = x11window.display.load(atomic::Ordering::Relaxed);
			let event: *mut xi::_XEvent = ::std::ptr::null_mut();
			let mut window_w = 0;
			let mut window_h = 0;
			println!("3");
			
			loop {
				match xi::XNextEvent(transmute(display), event) {
					0 => (),
					e => panic!("native input: XNextEvent failed with: {}", e)
				}
				
				let mut event = *event;
				
				dbg!(event.xany);
				
				match event.type_ as u32 {
					xi::GenericEvent => {
						let cookie: &mut xi::XGenericEventCookie = transmute(&mut event);
						xi::XGetEventData(display, cookie);
						
						match cookie.evtype as u32 {
							xi::XI_RawKeyPress => {
								let ev: &mut xi::XIRawEvent = transmute(cookie.data);
								let keycode = ev.detail - 8;
								
								if keycode < 1 {
									continue;
								}
								
								let key = Qwery::from(keycode as u32);
								basalt.input_ref().send_event(Event::KeyPress(key));
							},
							
							xi::XI_RawKeyRelease => {
								let ev: &mut xi::XIRawEvent = transmute(cookie.data);
								let keycode = ev.detail - 8;
								
								if keycode < 1 {
									continue;
								}
								
								let key = Qwery::from(keycode as u32);
								basalt.input_ref().send_event(Event::KeyRelease(key));
							},
							
							xi::XI_RawButtonPress => {
								let ev: &mut xi::XIRawEvent = transmute(cookie.data);
								let button = match ev.detail {
									1 => MouseButton::Left,
									2 => MouseButton::Middle,
									3 => MouseButton::Right,
									o => MouseButton::Other(o as u8),
								};
								
								basalt.input_ref().send_event(Event::MousePress(button));
							},
							
							xi::XI_RawButtonRelease => {
								let ev: &mut xi::XIRawEvent = transmute(cookie.data);
								let button = match ev.detail {
									1 => MouseButton::Left,
									2 => MouseButton::Middle,
									3 => MouseButton::Right,
									o => MouseButton::Other(o as u8),
								};
								
								basalt.input_ref().send_event(Event::MouseRelease(button));
							},
							
							xi::XI_RawMotion => {
								let ev: &mut xi::XIRawEvent = transmute(cookie.data);
								let mask = slice::from_raw_parts(ev.valuators.mask, ev.valuators.mask_len as usize);
								let mut value = ev.raw_values;
								
								let mut x = 0.0;
								let mut y = 0.0;
								let mut scroll_y = 0.0;
								
								for i in 0..(ev.valuators.mask_len * 8) {
									if (mask[(i >> 3) as usize] & (1 << (i & 7))) != 0 {
										match i {
											0 => x += *value,
											1 => y += *value,
											3 => scroll_y += *value,
											_ => ()
										}
										
										value = value.offset(1);
									}
								}
								
								if x != 0.0 || y != 0.0 {
									basalt.input_ref().send_event(Event::MouseMotion(x as f32, y as f32));
								}
								
								if scroll_y != 0.0 {
									basalt.input_ref().send_event(Event::MouseScroll(scroll_y as f32));
								}
							},
							
							xi::XI_Motion => {
								let ev: &mut xi::XIDeviceEvent = transmute(cookie.data);
								basalt.input_ref().send_event(Event::MousePosition(ev.event_x as f32, ev.event_y as f32));
							},
							
							xi::XI_Enter => {
								basalt.input_ref().send_event(Event::MouseEnter);
							},
							
							xi::XI_Leave => {
								basalt.input_ref().send_event(Event::MouseLeave);
							},
							
							xi::XI_FocusIn => {
								basalt.input_ref().send_event(Event::WindowFocused);
							},
							
							xi::XI_FocusOut => {
								basalt.input_ref().send_event(Event::WindowLostFocus);
							},
								
							_ => ()
						}
						
						xi::XFreeEventData(display, cookie);
					},
					
					xi::ConfigureNotify => {
						let ev: &mut xi::XConfigureEvent = transmute(&mut event);
						
						if ev.width != window_w || ev.height != window_h {
							window_w = ev.width;
							window_h = ev.height;
							basalt.input_ref().send_event(Event::WindowResized);
						}
					},
					
					xi::ClientMessage => {
						println!("Close Request");
						let ev: &mut xi::XClientMessageEvent = transmute(&mut event);
						
						if ev.message_type == 307 {
							basalt.exit();
						}
					},
					
					//e => println!("{}", e)
					_ => ()
				}
			}
		});
		
		Ok(surface)
	}
}