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
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

use std::str::from_utf8;

pub mod config;
pub mod ffi;
pub mod helpers;
pub mod post_build;
#[cfg(not(target_os = "linux"))]
pub use config::kill_filemaker;
pub use ffi::*;
pub use helpers::log;
use helpers::*;

pub trait Plugin {
    fn id() -> &'static [u8; 4];
    fn name() -> &'static str;
    fn description() -> &'static str;
    fn url() -> &'static str;
    fn enable_configure_button() -> bool {
        false
    }
    fn enable_init_and_shutdown() -> bool {
        true
    }
    fn enable_idle() -> bool {
        false
    }
    fn enable_shutdown() -> bool {
        false
    }
    fn register_functions() -> Vec<ExternalFunction>;

    /// # Safety
    /// talks to C
    unsafe fn get_string(
        which_string: ExternStringType,
        _win_lang_id: u32,
        out_buffer_size: u32,
        out_buffer: *mut u16,
    ) {
        use ExternStringType::*;
        let string = match which_string {
            Name => Self::name().to_string(),
            AppConfig => Self::description().to_string(),
            Options => {
                let mut options: String = from_utf8(Self::id()).unwrap().to_string();
                options.push('1');
                options.push(if Self::enable_configure_button() {
                    'Y'
                } else {
                    'n'
                });
                options.push('n');
                options.push(if Self::enable_init_and_shutdown() {
                    'Y'
                } else {
                    'n'
                });
                options.push(if Self::enable_idle() { 'Y' } else { 'n' });
                options.push(if Self::enable_shutdown() { 'Y' } else { 'n' });
                options.push('n');
                options
            }
            HelpUrl => Self::url().to_string(),
            Blank => "".to_string(),
        };
        write_to_u16_buff(out_buffer, out_buffer_size, &string)
    }

    fn initialize(version: ExternVersion) -> u64 {
        let plugin_id = QuadChar::new(Self::id());

        if version < ExternVersion::V160 {
            return ExternVersion::DoNotEnable as u64;
        }

        for f in Self::register_functions() {
            if f.register(&plugin_id) != FMError::NoError {
                return ExternVersion::DoNotEnable as u64;
            }
        }
        ExternVersion::V190 as u64
    }

    fn session_notifications(_session_id: fmx_ptrtype);

    fn file_notifications(_session_id: fmx_ptrtype, _file_id: fmx_ptrtype);

    fn preferences();

    fn shutdown(version: ExternVersion) {
        let plugin_id = QuadChar::new(Self::id());
        if version >= ExternVersion::V160 {
            for f in Self::register_functions() {
                f.unregister(&plugin_id);
            }
        }
    }

    fn idle_callback(idle_level: fmx_IdleLevel, _session_id: fmx_ptrtype) {
        use IdleType::*;
        match IdleType::from(idle_level) {
            Idle => Self::idle(),
            NotIdle => {}
            ScriptPaused => {}
            ScriptRunning => {}
            Unsafe => {}
        }
    }

    fn idle();
    fn not_idle();
    fn script_paused();
    fn script_running();
    fn un_safe();
}

#[macro_export]
macro_rules! register_plugin {
    ($x:ident) => {
        #[no_mangle]
        pub static mut gfmx_ExternCallPtr: *mut fmx_ExternCallStruct = std::ptr::null_mut();

        #[no_mangle]
        unsafe extern "C" fn FMExternCallProc(pb: *mut fmx_ExternCallStruct) {
            // Setup global defined in fmxExtern.h (this will be obsoleted in a later header file)
            gfmx_ExternCallPtr = pb;
            use FMExternCallType::*;

            // Message dispatcher
            match FMExternCallType::from((*pb).whichCall) {
                Init => (*pb).result = $x::initialize((*pb).extnVersion),
                Idle => $x::idle_callback((*pb).parm1, (*pb).parm2),
                Shutdown => $x::shutdown((*pb).extnVersion),
                AppPrefs => $x::preferences(),
                GetString => $x::get_string(
                    (*pb).parm1.into(),
                    (*pb).parm2 as u32,
                    (*pb).parm3 as u32,
                    (*pb).result as *mut u16,
                ),
                SessionShutdown => $x::session_notifications((*pb).parm2),
                FileShutdown => $x::file_notifications((*pb).parm2, (*pb).parm3),
            }
        }
    };
}