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
//! Utility macros for use in the rest of penrose.
//! Not intended for general use

/// kick off an external program as part of a key/mouse binding.
/// explicitly redirects stderr to /dev/null
#[macro_export]
macro_rules! run_external(
    ($cmd:tt) => {
        {
            Box::new(move |_: &mut $crate::manager::WindowManager| {
                $crate::helpers::spawn($cmd);
            }) as $crate::bindings::FireAndForget
        }
    };
);

/// kick off an internal method on the window manager as part of a key binding
#[macro_export]
macro_rules! run_internal(
    ($func:ident) => {
        Box::new(|wm: &mut $crate::manager::WindowManager| {
            wm.$func();
        }) as $crate::bindings::FireAndForget
    };

    ($func:ident, $($arg:expr),+) => {
        Box::new(move |wm: &mut $crate::manager::WindowManager| {
            wm.$func($($arg),+);
        }) as $crate::bindings::FireAndForget
    };
);

/// make creating a hash-map a little less verbose
#[macro_export]
macro_rules! map(
    {} => { ::std::collections::HashMap::new(); };

    { $($key:expr => $value:expr),+, } => {
        {
            let mut _map = ::std::collections::HashMap::new();
            $(_map.insert($key, $value);)+
            _map
        }
    };
);

/// make creating all of the key bindings less verbose
#[macro_export]
macro_rules! gen_keybindings(
    // parse a single simple key binding
    {   @parse $map:expr, $codes:expr,
        $binding:expr => $action:expr;
        $($tail:tt)*
    } => {
        match $crate::helpers::parse_key_binding($binding, &$codes) {
            None => panic!("invalid key binding: {}", $binding),
            Some(key_code) => $map.insert(key_code, $action),
        };
        gen_keybindings!(@parse $map, $codes, $($tail)*);
    };

    // parse a map block for WindowManager methods with a single arg
    {   @parse $map:expr, $codes:expr,
        map [ $from:expr ] in { $($patt:expr => $method:ident [ $to:expr ];)+ };
        $($tail:tt)*
    } => {
        {
            $(
                for (k, arg) in $from.into_iter().zip($to.clone()) {
                    let binding = format!($patt, k);
                    match $crate::helpers::parse_key_binding(binding.clone(), &$codes) {
                        None => panic!("invalid key binding: {}", binding),
                        Some(key_code) => $map.insert(key_code, run_internal!($method, arg)),
                    };
                }
            )+
            gen__keybindings!(@parse $map, $codes, $($tail)*);
        }
    };

    // parse a map by reference block for WindowManager methods with a single ref arg
    {   @parse $map:expr, $codes:expr,
        refmap [ $from:expr ] in { $($patt:expr => $method:ident [ $to:expr ];)+ };
        $($tail:tt)*
    } => {
        {
            $(
                for (k, arg) in $from.into_iter().zip($to.clone()) {
                    let binding = format!($patt, k);
                    match $crate::helpers::parse_key_binding(binding.clone(), &$codes) {
                        None => panic!("invalid key binding: {}", binding),
                        Some(key_code) => $map.insert(key_code, run_internal!($method, &arg)),
                    };
                }
            )+
            gen_keybindings!(@parse $map, $codes, $($tail)*);
        }
    };

    // base case (out of tokens)
    {   @parse $map:expr, $codes:expr,
        $($tail:tt)*
    } => {
        // shouldn't have any tokens here!
        $(compile_error!(
            stringify!(unexpected tokens in keybinging macro: $tail)
        );)*
    };

    // TODO: remove this depricated method of doing keybindings
    {   $($binding:expr => $action:expr),+;
        $(forall_workspaces: $ws_array:expr => { $($ws_binding:expr => $ws_action:tt),+, })+
    } => {
        gen_keybindings_depricated!(
            $($binding => $action),+;
            $(forall_workspaces: $ws_array => { $($ws_binding => $ws_action),+, })+
        );
    };

    // NOTE: this is the public entry point to the macro
    { $($tokens:tt)+ } => {
        {
            let mut map = ::std::collections::HashMap::new();
            let codes = $crate::helpers::keycodes_from_xmodmap();
            gen_keybindings!(@parse map, codes, $($tokens)+);
            map
        }
    };
);

/// depricated: please use [gen_keybindings] as shown in the examples.
#[deprecated(
    since = "0.0.11",
    note = "This macro will be removed entirely in an upcoming release."
)]
#[macro_export]
macro_rules! gen_keybindings_depricated(
    {
        $($binding:expr => $action:expr),+;
        $(forall_workspaces: $ws_array:expr => { $($ws_binding:expr => $ws_action:tt),+, })+
    } => {
        {
            let mut _map = ::std::collections::HashMap::new();
            let keycodes = $crate::helpers::keycodes_from_xmodmap();

            $(
                match $crate::helpers::parse_key_binding($binding, &keycodes) {
                    None => panic!("invalid key binding: {}", $binding),
                    Some(key_code) => _map.insert(key_code, $action),
                };
            )+

            $(for i in 0..$ws_array.len() {
                $(
                    let for_ws = format!($ws_binding, i+1);
                    match $crate::helpers::parse_key_binding(for_ws.clone(), &keycodes) {
                        None => panic!("invalid key binding: {}", for_ws),
                        Some(key_code) => _map.insert(
                            key_code,
                            run_internal!(
                                $ws_action,
                                &$crate::core::ring::Selector::Index(i)
                            )
                        ),
                    };
                )+
            })+

            _map
        }
    };
);

/// make creating all of the mouse bindings less verbose
#[macro_export]
macro_rules! gen_mousebindings(
    {
        $($kind:ident $button:ident + [$($modifier:ident),+] => $action:expr),+
    } => {
        {
            // HashMap<(MouseEventKind, MouseState), MouseEventHandler>
            let mut _map = ::std::collections::HashMap::new();

            $(
                let mut modifiers = Vec::new();
                $(modifiers.push($crate::core::bindings::ModifierKey::$modifier);)+

                let state = $crate::core::bindings::MouseState::new(
                    $crate::core::bindings::MouseButton::$button,
                    modifiers
                );

                let kind = $crate::core::bindings::MouseEventKind::$kind;
                _map.insert(
                    (kind, state),
                    Box::new($action) as $crate::bindings::MouseEventHandler
                );
            )+

            _map
        }
    };
);