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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! This crate provides an easy to use interface for the zwp_input_method_v2 protocol.
//! It allows a wayland client to serve as an input method for other wayland-clients. This could be used for virtual keyboards
//!
#[macro_use]
extern crate log;

use std::convert::TryInto;
use std::num::Wrapping;
use std::sync::{Arc, Mutex};
use wayland_client::{protocol::wl_seat::WlSeat, Filter, Main};
use wayland_protocols::unstable::text_input::v3::client::zwp_text_input_v3::{
    ChangeCause, ContentHint, ContentPurpose,
};
use zwp_input_method::input_method_unstable_v2::zwp_input_method_manager_v2::ZwpInputMethodManagerV2;
use zwp_input_method::input_method_unstable_v2::zwp_input_method_v2::{
    Event as InputMethodEvent, ZwpInputMethodV2,
};

#[derive(Debug, Clone)]
/// Error when sending a request to the wayland-client
pub enum SubmitError {
    /// Input method was not activ
    NotActive,
}

// Mandatory conversion to apply filter to ZwpInputMethodV2
mod event_enum {
    use wayland_client::event_enum;
    use zwp_input_method::input_method_unstable_v2::zwp_input_method_v2::ZwpInputMethodV2;
    event_enum!(
        Events |
        InputMethod => ZwpInputMethodV2
    );
}

/// Trait to get notified when the keyboard should be shown or hidden
///
/// If the user clicks for example on a text field, the method show_keyboard() is called
pub trait KeyboardVisibility {
    fn show_keyboard(&self);
    fn hide_keyboard(&self);
}

pub trait ReceiveSurroundingText {
    fn text_changed(&self, string_left_of_cursor: String, string_right_of_cursor: String);
}

/// Trait to get notified when the hint or the purpose of the content changes
pub trait HintPurpose {
    fn set_hint_purpose(&self, content_hint: ContentHint, content_purpose: ContentPurpose);
}

/// Describes the desired state of the input method as requested by the server
#[derive(Clone, Debug)]
struct IMProtocolState {
    surrounding_text: String,
    cursor: u32,
    content_purpose: ContentPurpose,
    content_hint: ContentHint,
    text_change_cause: ChangeCause,
    active: bool,
}

impl Default for IMProtocolState {
    fn default() -> IMProtocolState {
        IMProtocolState {
            surrounding_text: String::new(),
            cursor: 0,
            content_hint: ContentHint::None,
            content_purpose: ContentPurpose::Normal,
            text_change_cause: ChangeCause::InputMethod,
            active: false,
        }
    }
}

#[derive(Clone, Debug)]
/// Manages the pending state and the current state of the input method.
///
/// It is called IMServiceArc and not IMService because the new() method
/// wraps IMServiceArc and returns Arc<Mutex<IMServiceArc<T>>>. This is required because it's state could get changed by multiple threads.
/// One thread could handle requests while the other one handles events from the wayland-server
struct IMServiceArc<
    T: 'static + KeyboardVisibility + HintPurpose,
    D: 'static + ReceiveSurroundingText,
> {
    im: Main<ZwpInputMethodV2>,
    ui_connector: T,
    content_connector: D,
    pending: IMProtocolState,
    current: IMProtocolState,
    serial: Wrapping<u32>,
}

impl<T: 'static + KeyboardVisibility + HintPurpose, D: 'static + ReceiveSurroundingText>
    IMServiceArc<T, D>
{
    /// Creates a new IMServiceArc wrapped in Arc<Mutex<Self>>
    fn new(
        seat: &WlSeat,
        im_manager: Main<ZwpInputMethodManagerV2>,
        ui_connector: T,
        content_connector: D,
    ) -> Arc<Mutex<IMServiceArc<T, D>>> {
        // Get ZwpInputMethodV2 from ZwpInputMethodManagerV2
        let im = im_manager.get_input_method(seat);

        // Create IMServiceArc with default values
        let im_service = IMServiceArc {
            im,
            ui_connector,
            content_connector,
            pending: IMProtocolState::default(),
            current: IMProtocolState::default(),
            serial: Wrapping(0u32),
        };

        // Wrap IMServiceArc to allow mutability from multiple threads
        let im_service = Arc::new(Mutex::new(im_service));

        // Clone the reference to move it to the filter
        let im_service_ref = Arc::clone(&im_service);
        // Assigns a filter to the wayland event queue to handle events for ZwpInputMethodV2
        im_service.lock().unwrap().assign_filter(im_service_ref);
        info!("New IMService was created");
        // Return the wrapped IMServiceArc
        im_service
    }

    /// Assigns a filter to the wayland event queue to allow IMServiceArc to handle events from ZwpInputMethodV2
    fn assign_filter(&self, im_service: Arc<Mutex<IMServiceArc<T, D>>>) {
        let filter = Filter::new(move |event, _, _| match event {
            event_enum::Events::InputMethod { event, .. } => match event {
                InputMethodEvent::Activate => im_service.lock().unwrap().handle_activate(),
                InputMethodEvent::Deactivate => im_service.lock().unwrap().handle_deactivate(),
                InputMethodEvent::SurroundingText {
                    text,
                    cursor,
                    anchor,
                } => im_service
                    .lock()
                    .unwrap()
                    .handle_surrounding_text(text, cursor, anchor),
                InputMethodEvent::TextChangeCause { cause } => {
                    im_service.lock().unwrap().handle_text_change_cause(cause)
                }
                InputMethodEvent::ContentType { hint, purpose } => im_service
                    .lock()
                    .unwrap()
                    .handle_content_type(hint, purpose),
                InputMethodEvent::Done => im_service.lock().unwrap().handle_done(),
                InputMethodEvent::Unavailable => im_service.lock().unwrap().handle_unavailable(),
                _ => (),
            },
        });
        self.im.assign(filter);
        info!("The filter was assigned to Main<ZwpInputMethodV2>");
    }

    /// Sends a 'commit_string' request to the wayland-server
    ///
    /// INPUTS: text -> Text that will be committed
    fn commit_string(&mut self, text: String) -> Result<(), SubmitError> {
        info!("Commit string '{}'", text);
        // Check if proxy is still alive. If the proxy was dead, the requests would fail silently
        match self.current.active {
            true => {
                let cursor_position = self.pending.cursor.try_into().unwrap(); // Converts u32 of cursor to usize
                                                                               // Append 'text' to the pending surrounding_text
                self.pending
                    .surrounding_text
                    .insert_str(cursor_position, &text);
                // Update the cursor
                self.pending.cursor += text.len() as u32;
                // Send the request to the wayland-server
                self.im.commit_string(text);
                Ok(())
            }
            false => Err(SubmitError::NotActive),
        }
    }

    /// Sends a 'delete_surrounding_text' request to the wayland server
    ///
    /// INPUTS:
    ///
    /// before -> number of chars to delete from the surrounding_text going left from the cursor
    ///
    /// after  -> number of chars to delete from the surrounding_text going right from the cursor
    fn delete_surrounding_text(&mut self, before: u32, after: u32) -> Result<(), SubmitError> {
        info!(
            "Send a request to the wayland server to delete {} chars before and {} after the cursor at {} from the surrounding text",
            before, after, self.pending.cursor
        );
        // Check if proxy is still alive. If the proxy was dead, the requests would fail silently
        match self.current.active {
            true => {
                // Limit 'before' and 'after' if they exceed the maximum
                let (before, after) = self.limit_before_after(before, after);
                // Update self.pending.surrounging_text and self.pending.cursor
                self.update_cursor_and_surrounding_text(before, after);
                // Send the delete_surrounding_text request to the wayland-server
                self.im.delete_surrounding_text(before as u32, after as u32);
                Ok(())
            }
            false => Err(SubmitError::NotActive),
        }
    }

    /// Sends a 'commit' request to the wayland server
    ///
    /// This makes the pending changes permanent
    fn commit(&mut self) -> Result<(), SubmitError> {
        info!("Commit the changes");
        // Check if proxy is still alive. If the proxy was dead, the requests would fail silently
        match self.current.active {
            true => {
                // Send request to wayland-server
                self.im.commit(self.serial.0);
                // Increase the serial
                self.serial += Wrapping(1u32);
                // Make pending changes permanent
                self.pending_becomes_current();
                Ok(())
            }
            false => Err(SubmitError::NotActive),
        }
    }

    /// Returns if the input method is currently active
    fn is_active(&self) -> bool {
        self.current.active
    }

    /// Returns a tuple of the current strings left and right of the cursor
    fn get_surrounding_text(&self) -> (String, String) {
        info!("Requested surrounding text");
        let (left_str, right_str) = self
            .pending
            .surrounding_text
            .split_at(self.current.cursor.try_into().unwrap());
        (left_str.to_string(), right_str.to_string())
    }

    /// Handles the 'activate' event sent from the wayland server
    ///
    /// This method should never be called from the client
    fn handle_activate(&mut self) {
        info!("handle_activate() was called");
        self.pending = IMProtocolState {
            active: true,
            ..IMProtocolState::default()
        };
    }

    /// Handles the 'deactivate' event sent from the wayland server
    ///
    /// This method should never be called from the client
    fn handle_deactivate(&mut self) {
        info!("handle_deactivate() was called");
        self.pending.active = false;
    }

    /// Handles the 'surrounding_text' event sent from the wayland server
    ///
    /// This method should never be called from the client
    fn handle_surrounding_text(&mut self, text: String, cursor: u32, anchor: u32) {
        info!("handle_surrounding_text(text: '{}', cursor: {}) was called",text, cursor);
        self.pending.surrounding_text = text;
        self.pending.cursor = cursor;
    }

    /// Handles the 'text_change_cause' event sent from the wayland server
    ///
    /// This method should never be called from the client
    fn handle_text_change_cause(&mut self, cause: ChangeCause) {
        info!("handle_text_change_cause() was called");
        self.pending.text_change_cause = cause;
    }

    /// Handles the 'content_type' event sent from the wayland server
    ///
    /// This method should never be called from the client
    fn handle_content_type(&mut self, hint: ContentHint, purpose: ContentPurpose) {
        info!("handle_content_type() was called");
        self.pending.content_hint = hint;
        self.pending.content_purpose = purpose;
    }

    /// Handles the 'done' event sent from the wayland server
    ///
    /// This method should never be called from the client
    fn handle_done(&mut self) {
        info!("handle_done() was called");
        self.pending_becomes_current();
    }

    /// Handles the 'unavailable' event sent from the wayland server
    ///
    /// This method should never be called from the client
    fn handle_unavailable(&mut self) {
        info!("handle_unavailable() was called");
        self.im.destroy();
        self.current.active = false;
        self.ui_connector.hide_keyboard();
    }

    /// This is a helper method
    ///
    /// It moves the values of self.pending to self.current and notifies the connector, to show or hide the keyboard.
    ///
    /// It should only be called if the wayland-server or the client committed the pending changes
    fn pending_becomes_current(&mut self) {
        info!("The pending protocol state became the current state");
        let active_changed = self.current.active ^ self.pending.active;

        let text_changed = self.current.surrounding_text != self.pending.surrounding_text;

        // Make pending changes permanent
        self.current = self.pending.clone();

        if text_changed {
            info!(
                "The surrounding text changed to '{}'",
                self.current.surrounding_text
            );
            let (left_str, right_str) = self
                .current
                .surrounding_text
                .split_at(self.current.cursor.try_into().unwrap());
            let (left_str, right_str) = (left_str.to_string(), right_str.to_string());
            self.content_connector.text_changed(left_str, right_str);
        }

        // Notify connector about changes
        if active_changed {
            if self.current.active {
                self.ui_connector.show_keyboard();
                self.ui_connector
                    .set_hint_purpose(self.current.content_hint, self.current.content_purpose);
            } else {
                self.ui_connector.hide_keyboard();
            };
        }
    }

    /// This is a helper method for the delete_surrounding_text method
    ///
    /// INPUTS:
    ///
    /// before -> number of chars to delete from the surrounding_text going left from the cursor
    ///
    /// after  -> number of chars to delete from the surrounding_text going right from the cursor
    ///
    ///
    /// OUTPUTS:
    ///
    /// before (limited) -> number of chars to delete from the surrounding_text going left from the cursor  (limited)
    ///
    /// after  (limited) -> number of chars to delete from the surrounding_text going right from the cursor (limited)
    ///
    ///
    /// The wayland server ignores 'delete_surrounding_text' requests under the following conditions:
    ///
    /// A: cursor_position < before
    ///
    ///   or
    ///
    /// B: cursor_position + after > surrounding_text.len()
    ///
    /// This method limits the values of before and after to those maximums so no requests will be ignored.
    fn limit_before_after(&self, before: u32, after: u32) -> (u32, u32) {
        let cursor_position = self.pending.cursor;
        let before = if cursor_position > before {
            before
        } else {
            cursor_position
        };
        let after = if cursor_position.saturating_add(after)
            <= self.pending.surrounding_text.len().try_into().unwrap()
        {
            after
        } else {
            self.pending.surrounding_text.len() as u32 - cursor_position
        };
        (before, after)
    }

    /// This is a helper method for the delete_surrounding_text method
    ///
    /// INPUTS:
    ///
    /// before -> number of chars to delete from the surrounding_text going left from the cursor
    ///
    /// after  -> number of chars to delete from the surrounding_text going right from the cursor
    ///
    /// This method removes the amount of chars requested from self.pending.surrounding_text. This deletion not only affects the surrounding_text
    /// but also the cursor position.
    fn update_cursor_and_surrounding_text(&mut self, before: u32, after: u32) {
        let cursor_position = self.pending.cursor as usize;

        // Get str left and right of the cursor to remove the requested amount of chars from each of them
        let (string_left_of_cursor, old_string_right_of_cursor) =
            self.pending.surrounding_text.split_at(cursor_position);

        // Make a String from the reference
        let mut string_left_of_cursor = String::from(string_left_of_cursor);
        let mut string_right_of_cursor = String::from("");

        // Pop of as many chars as requested with the before parameter
        // The result is the string on the left side of the cursor for the new surrounding_text
        for _ in 0..before {
            string_left_of_cursor.pop();
        }

        // Skip as many chars as requested with the after parameter and then add all remaining chars
        // The result is the string on the right side of the cursor for the new surrounding_text
        for character in old_string_right_of_cursor.chars().skip(after as usize) {
            string_right_of_cursor.push(character);
        }

        // Get the new position of the cursor
        let new_cursor_position = string_left_of_cursor.len() as u32;

        // Join the string of the left and the right sides to make the new surrounding_text
        let mut new_surrounding_text = string_left_of_cursor;
        new_surrounding_text.push_str(&string_right_of_cursor);

        // Apply the new values of the cursor and the new surrounding_text to self
        self.pending.surrounding_text = new_surrounding_text;
        self.pending.cursor = new_cursor_position;
    }
}

#[derive(Clone, Debug)]
/// Manages the pending state and the current state of the input method.
pub struct IMService<
    T: 'static + KeyboardVisibility + HintPurpose,
    D: 'static + ReceiveSurroundingText,
> {
    im_service_arc: Arc<Mutex<IMServiceArc<T, D>>>, // provides an easy to use interface by hiding the Arc<Mutex<>>
}

impl<T: 'static + KeyboardVisibility + HintPurpose, D: 'static + ReceiveSurroundingText>
    IMService<T, D>
{
    /// Create a new IMService. The connector must implement the traits KeyboardVisibility and HintPurpose
    pub fn new(
        seat: &WlSeat,
        im_manager: Main<ZwpInputMethodManagerV2>,
        ui_connector: T,
        content_connector: D,
    ) -> IMService<T, D> {
        let im_service_arc = IMServiceArc::new(seat, im_manager, ui_connector, content_connector);
        IMService { im_service_arc }
    }

    /// Sends a 'commit_string' request to the wayland-server
    ///
    /// INPUTS:
    ///
    /// text -> Text that will be committed
    pub fn commit_string(&self, text: String) -> Result<(), SubmitError> {
        self.im_service_arc.lock().unwrap().commit_string(text)
    }

    /// Sends a 'delete_surrounding_text' request to the wayland server
    ///
    /// INPUTS:
    ///
    /// before -> number of chars to delete from the surrounding_text going left from the cursor
    ///
    /// after  -> number of chars to delete from the surrounding_text going right from the cursor
    pub fn delete_surrounding_text(&self, before: u32, after: u32) -> Result<(), SubmitError> {
        info!("Delete surrounding text ");
        self.im_service_arc
            .lock()
            .unwrap()
            .delete_surrounding_text(before, after)
    }

    /// Sends a 'commit' request to the wayland server
    ///
    /// This makes the pending changes permanent
    pub fn commit(&self) -> Result<(), SubmitError> {
        self.im_service_arc.lock().unwrap().commit()
    }

    /// Returns if the input method is currently active
    pub fn is_active(&self) -> bool {
        self.im_service_arc.lock().unwrap().is_active()
    }

    /// Returns a tuple of the current strings left and right of the cursor
    pub fn get_surrounding_text(&self) -> (String, String) {
        self.im_service_arc.lock().unwrap().get_surrounding_text()
    }
}