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
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::ffi::CString;
use super::broadcaster::SBBroadcaster;
use super::debugger::SBDebugger;
use super::event::SBEvent;
use sys;

/// Listen for debugger events.
#[derive(Debug)]
pub struct SBListener {
    /// The underlying raw `SBListenerRef`.
    pub raw: sys::SBListenerRef,
}

impl SBListener {
    /// Construct a new `SBListener`.
    pub fn new() -> SBListener {
        SBListener::wrap(unsafe { sys::CreateSBListener() })
    }

    /// Construct a new `SBListener`.
    pub fn wrap(raw: sys::SBListenerRef) -> SBListener {
        SBListener { raw: raw }
    }

    /// Construct a new `Some(SBListener)` or `None`.
    pub fn maybe_wrap(raw: sys::SBListenerRef) -> Option<SBListener> {
        if unsafe { sys::SBListenerIsValid(raw) != 0 } {
            Some(SBListener { raw: raw })
        } else {
            None
        }
    }

    /// Check whether or not this is a valid `SBListener` value.
    pub fn is_valid(&self) -> bool {
        unsafe { sys::SBListenerIsValid(self.raw) != 0 }
    }

    #[allow(missing_docs)]
    pub fn start_listening_for_event_class(
        &self,
        debugger: &SBDebugger,
        broadcaster_class: &str,
        event_mask: u32,
    ) -> u32 {
        let bc = CString::new(broadcaster_class).unwrap();
        unsafe {
            sys::SBListenerStartListeningForEventClass(
                self.raw,
                debugger.raw,
                bc.as_ptr(),
                event_mask,
            )
        }
    }

    #[allow(missing_docs)]
    pub fn stop_listening_for_event_class(
        &self,
        debugger: &SBDebugger,
        broadcaster_class: &str,
        event_mask: u32,
    ) -> bool {
        let bc = CString::new(broadcaster_class).unwrap();
        unsafe {
            sys::SBListenerStopListeningForEventClass(
                self.raw,
                debugger.raw,
                bc.as_ptr(),
                event_mask,
            ) != 0
        }
    }

    #[allow(missing_docs)]
    pub fn start_listening_for_events(&self, broadcaster: &SBBroadcaster, event_mask: u32) -> u32 {
        unsafe { sys::SBListenerStartListeningForEvents(self.raw, broadcaster.raw, event_mask) }
    }

    #[allow(missing_docs)]
    pub fn stop_listening_for_events(&self, broadcaster: &SBBroadcaster, event_mask: u32) -> bool {
        unsafe { sys::SBListenerStopListeningForEvents(self.raw, broadcaster.raw, event_mask) != 0 }
    }

    #[allow(missing_docs)]
    pub fn wait_for_event(&self, num_seconds: u32, event: &mut SBEvent) -> bool {
        unsafe { sys::SBListenerWaitForEvent(self.raw, num_seconds, event.raw) != 0 }
    }

    #[allow(missing_docs)]
    pub fn wait_for_event_for_broadcaster(
        &self,
        num_seconds: u32,
        broadcaster: &SBBroadcaster,
        event: &mut SBEvent,
    ) -> bool {
        unsafe {
            sys::SBListenerWaitForEventForBroadcaster(
                self.raw,
                num_seconds,
                broadcaster.raw,
                event.raw,
            ) != 0
        }
    }

    #[allow(missing_docs)]
    pub fn wait_for_event_for_broadcaster_with_type(
        &self,
        num_seconds: u32,
        broadcaster: &SBBroadcaster,
        event_type_mask: u32,
        event: &mut SBEvent,
    ) -> bool {
        unsafe {
            sys::SBListenerWaitForEventForBroadcasterWithType(
                self.raw,
                num_seconds,
                broadcaster.raw,
                event_type_mask,
                event.raw,
            ) != 0
        }
    }

    #[allow(missing_docs)]
    pub fn peek_at_next_event(&self, event: &mut SBEvent) -> bool {
        unsafe { sys::SBListenerPeekAtNextEvent(self.raw, event.raw) != 0 }
    }

    #[allow(missing_docs)]
    pub fn peek_at_next_event_for_broadcaster(
        &self,
        broadcaster: &SBBroadcaster,
        event: &mut SBEvent,
    ) -> bool {
        unsafe {
            sys::SBListenerPeekAtNextEventForBroadcaster(self.raw, broadcaster.raw, event.raw) != 0
        }
    }

    #[allow(missing_docs)]
    pub fn peek_at_next_event_for_broadcaster_with_type(
        &self,
        broadcaster: &SBBroadcaster,
        event_type_mask: u32,
        event: &mut SBEvent,
    ) -> bool {
        unsafe {
            sys::SBListenerPeekAtNextEventForBroadcasterWithType(
                self.raw,
                broadcaster.raw,
                event_type_mask,
                event.raw,
            ) != 0
        }
    }

    #[allow(missing_docs)]
    pub fn get_next_event(&self, event: &mut SBEvent) -> bool {
        unsafe { sys::SBListenerGetNextEvent(self.raw, event.raw) != 0 }
    }

    #[allow(missing_docs)]
    pub fn get_next_event_for_broadcaster(
        &self,
        broadcaster: &SBBroadcaster,
        event: &mut SBEvent,
    ) -> bool {
        unsafe {
            sys::SBListenerGetNextEventForBroadcaster(self.raw, broadcaster.raw, event.raw) != 0
        }
    }

    #[allow(missing_docs)]
    pub fn get_next_event_for_broadcaster_with_type(
        &self,
        broadcaster: &SBBroadcaster,
        event_type_mask: u32,
        event: &mut SBEvent,
    ) -> bool {
        unsafe {
            sys::SBListenerGetNextEventForBroadcasterWithType(
                self.raw,
                broadcaster.raw,
                event_type_mask,
                event.raw,
            ) != 0
        }
    }

    #[allow(missing_docs)]
    pub fn handle_broadcast_event(&self, event: &SBEvent) -> bool {
        unsafe { sys::SBListenerHandleBroadcastEvent(self.raw, event.raw) != 0 }
    }
}

impl Default for SBListener {
    fn default() -> SBListener {
        SBListener::new()
    }
}

impl Drop for SBListener {
    fn drop(&mut self) {
        unsafe { sys::DisposeSBListener(self.raw) };
    }
}