|
| 1 | +use std::ffi::{CStr, CString}; |
| 2 | + |
| 3 | +use libc::{c_char, int32_t, uint32_t, size_t}; |
| 4 | +extern crate xi_modal_input; |
| 5 | +use xi_modal_input::{XiCore, EventCtx, KeyEvent, EventPayload, Plumber, OneView, Vim}; |
| 6 | + |
| 7 | +#[repr(C)] |
| 8 | +pub struct XiLine { |
| 9 | + text: *const c_char, |
| 10 | + cursor: int32_t, |
| 11 | + selection: [int32_t; 2], |
| 12 | +} |
| 13 | + |
| 14 | +#[no_mangle] |
| 15 | +pub extern "C" fn xiCoreCreate( |
| 16 | + rpc_callback: extern "C" fn(*const c_char), |
| 17 | + invalidate_callback: extern "C" fn(size_t, size_t), |
| 18 | +) -> *const XiCore { |
| 19 | + let r = Box::into_raw(Box::new(XiCore { |
| 20 | + rpc_callback, |
| 21 | + invalidate_callback, |
| 22 | + state: OneView::new(), |
| 23 | + plumber: None, |
| 24 | + handler: None, |
| 25 | + })); |
| 26 | + eprintln!("xiCore alloc {:?}", &r); |
| 27 | + r |
| 28 | +} |
| 29 | + |
| 30 | +#[no_mangle] |
| 31 | +pub extern "C" fn xiCoreRegisterEventHandler( |
| 32 | + ptr: *mut XiCore, |
| 33 | + event_cb: extern "C" fn(*const EventPayload, bool), |
| 34 | + action_cb: extern "C" fn(*const c_char), |
| 35 | + timer_cb: extern "C" fn(*const EventPayload, uint32_t) -> uint32_t, |
| 36 | + cancel_timer_cb: extern "C" fn(uint32_t), |
| 37 | +) { |
| 38 | + let core = unsafe { |
| 39 | + assert!(!ptr.is_null(), "null pointer in xiCoreRegisterEventHandler"); |
| 40 | + &mut *ptr |
| 41 | + }; |
| 42 | + |
| 43 | + let machine = Vim::new(); |
| 44 | + let plumber = Plumber::new(event_cb, action_cb, timer_cb, cancel_timer_cb); |
| 45 | + core.plumber = Some(plumber); |
| 46 | + core.handler = Some(Box::new(machine)); |
| 47 | +} |
| 48 | + |
| 49 | +#[no_mangle] |
| 50 | +pub extern "C" fn xiCoreHandleInput( |
| 51 | + ptr: *mut XiCore, |
| 52 | + modifiers: uint32_t, |
| 53 | + characters: *const c_char, |
| 54 | + payload: *const EventPayload, |
| 55 | +) { |
| 56 | + let core = unsafe { |
| 57 | + assert!(!ptr.is_null()); |
| 58 | + &mut *ptr |
| 59 | + }; |
| 60 | + |
| 61 | + let cstr = unsafe { |
| 62 | + assert!(!characters.is_null()); |
| 63 | + CStr::from_ptr(characters) |
| 64 | + }; |
| 65 | + |
| 66 | + let characters = match cstr.to_str() { |
| 67 | + Ok(s) => s, |
| 68 | + Err(e) => { |
| 69 | + eprintln!("invalid cstr: {}, {:?}", e, cstr.to_bytes()); |
| 70 | + "" |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + let event = KeyEvent { |
| 75 | + characters, |
| 76 | + modifiers, |
| 77 | + payload, |
| 78 | + }; |
| 79 | + |
| 80 | + let ctx = EventCtx { |
| 81 | + plumber: core.plumber.as_ref().unwrap(), |
| 82 | + state: &mut core.state, |
| 83 | + }; |
| 84 | + |
| 85 | + let needs_render = core.handler.as_mut().unwrap().handle_event(event, ctx); |
| 86 | + if needs_render { |
| 87 | + core.send_update(); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +#[no_mangle] |
| 92 | +pub extern "C" fn xiCoreClearPending(ptr: *mut XiCore, token: uint32_t) { |
| 93 | + let core = unsafe { |
| 94 | + assert!(!ptr.is_null()); |
| 95 | + &mut *ptr |
| 96 | + }; |
| 97 | + |
| 98 | + if core.handler.is_none() { |
| 99 | + eprintln!("unexpected None in xiCoreClearPending"); |
| 100 | + } |
| 101 | + |
| 102 | + core.handler.as_mut().map(|h| h.clear_pending(token)); |
| 103 | +} |
| 104 | + |
| 105 | +#[no_mangle] |
| 106 | +pub extern "C" fn xiCoreGetLine(ptr: *mut XiCore, idx: uint32_t) -> *const XiLine { |
| 107 | + let core = unsafe { |
| 108 | + assert!(!ptr.is_null(), "null pointer in xiCoreGetLine"); |
| 109 | + &mut *ptr |
| 110 | + }; |
| 111 | + |
| 112 | + let (line, cursor, sel) = core.state.get_line(idx as usize).unwrap(); |
| 113 | + let cstring = CString::new(line.as_ref()).expect("bad string, very sad"); |
| 114 | + Box::into_raw(Box::new(XiLine { |
| 115 | + text: cstring.into_raw(), |
| 116 | + cursor, |
| 117 | + selection: [sel.0, sel.1], |
| 118 | + })) |
| 119 | +} |
| 120 | + |
| 121 | +#[no_mangle] |
| 122 | +pub extern "C" fn xiCoreFree(ptr: *mut XiCore) { |
| 123 | + eprintln!("xiCore free {:?}", &ptr); |
| 124 | + if ptr.is_null() { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + unsafe { |
| 129 | + Box::from_raw(ptr); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +#[no_mangle] |
| 134 | +pub extern "C" fn xiCoreSendMessage(ptr: *mut XiCore, msg: *const c_char) { |
| 135 | + let core = unsafe { |
| 136 | + assert!(!ptr.is_null(), "null pointer in xiCoreSendMessage"); |
| 137 | + &mut *ptr |
| 138 | + }; |
| 139 | + |
| 140 | + let cstr = unsafe { |
| 141 | + assert!(!ptr.is_null(), "null msg pointer in xiCoreSendMessage"); |
| 142 | + CStr::from_ptr(msg) |
| 143 | + }; |
| 144 | + |
| 145 | + let msg = match cstr.to_str() { |
| 146 | + Ok(s) => s, |
| 147 | + Err(e) => { |
| 148 | + eprintln!("invalid cstr: {}, {:?}", e, cstr.to_bytes()); |
| 149 | + return; |
| 150 | + } |
| 151 | + }; |
| 152 | + |
| 153 | + core.handle_message(msg); |
| 154 | +} |
0 commit comments