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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
//! This crate acts as a manager of a list of windows. It defines a `WindowManager` structure and an instance of it. 
//!
//! A window manager holds a set of `WindowInner` objects, including an active window, a list of shown windows and a list of hidden windows. The hidden windows are totally overlapped by others.
//!
//! A window manager owns a bottom framebuffer and a top framebuffer. The bottom is the background of the desktop and the top framebuffer contains a floating window border and a mouse arrow. 
//! A window manager also contains a final framebuffer which is mapped to the screen. In refreshing an area, the manager will render all the framebuffers to the final one in order: bottom -> hide list -> showlist -> active -> top.
//!
//! The window manager provides methods to update within some bounding boxes rather than the whole screen for better performance.

#![no_std]

extern crate spin;
#[macro_use] extern crate log;
extern crate alloc;
extern crate mpmc;
extern crate event_types;
extern crate compositor;
extern crate framebuffer;
extern crate framebuffer_compositor;
extern crate framebuffer_drawer;
extern crate keycodes_ascii;
extern crate mod_mgmt;
extern crate mouse_data;
extern crate path;
extern crate scheduler; 
extern crate spawn;
extern crate window_inner;
extern crate shapes;
extern crate color;

use alloc::collections::VecDeque;
use alloc::string::ToString;
use alloc::sync::{Arc, Weak};
use alloc::vec::Vec;
use compositor::{Compositor, FramebufferUpdates, CompositableRegion};

use mpmc::Queue;
use event_types::{Event, MousePositionEvent};
use framebuffer::{Framebuffer, AlphaPixel};
use color::Color;
use shapes::{Coord, Rectangle};
use framebuffer_compositor::{FRAME_COMPOSITOR};
use keycodes_ascii::{KeyAction, KeyEvent, Keycode};
use mouse_data::MouseEvent;
use spin::{Mutex, Once};
use window_inner::{WindowInner, WindowMovingStatus};

/// The instance of the default window manager
pub static WINDOW_MANAGER: Once<Mutex<WindowManager>> = Once::new();

/// The width and height size of mouse in number of pixels.
const MOUSE_POINTER_SIZE_Y: usize = 18;
const MOUSE_POINTER_SIZE_X: usize = 11;
/// The mouse pointer image defined as a 2-D pixel array.
static MOUSE_POINTER_IMAGE: [[Color; MOUSE_POINTER_SIZE_Y]; MOUSE_POINTER_SIZE_X] = {
    const T: Color = color::TRANSPARENT;
    const C: Color = color::BLACK; // Cursor
    const B: Color = color::WHITE; // Border
    [
        [B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, B, T, T],
        [T, B, C, C, C, C, C, C, C, C, C, C, C, C, B, T, T, T],
        [T, T, B, C, C, C, C, C, C, C, C, C, C, B, T, T, T, T],
        [T, T, T, B, C, C, C, C, C, C, C, C, B, T, T, T, T, T],
        [T, T, T, T, B, C, C, C, C, C, C, C, C, B, B, T, T, T],
        [T, T, T, T, T, B, C, C, C, C, C, C, C, C, C, B, B, T],
        [T, T, T, T, T, T, B, C, C, C, C, B, B, C, C, C, C, B],
        [T, T, T, T, T, T, T, B, C, C, B, T, T, B, B, C, B, T],
        [T, T, T, T, T, T, T, T, B, C, B, T, T, T, T, B, B, T],
        [T, T, T, T, T, T, T, T, T, B, B, T, T, T, T, T, T, T],
        [T, T, T, T, T, T, T, T, T, T, B, T, T, T, T, T, T, T],
    ]
};

// the border indicating new window position and size
const WINDOW_BORDER_SIZE: usize = 3;
// border's inner color
const WINDOW_BORDER_COLOR_INNER: Color = Color::new(0x00CA6F1E);

/// Window manager structure which maintains a list of windows and a mouse.
pub struct WindowManager {
    /// those window currently not shown on screen
    hide_list: VecDeque<Weak<Mutex<WindowInner>>>,
    /// those window shown on screen that may overlapping each other
    show_list: VecDeque<Weak<Mutex<WindowInner>>>,
    /// the only active window, receiving all keyboard events (except for those remained for WM)
    active: Weak<Mutex<WindowInner>>, // this one is not in show_list
    /// current mouse position
    mouse: Coord,
    /// If a window is being repositioned (e.g., by dragging it), this is the position of that window's border
    repositioned_border: Option<Rectangle>,
    /// The bottom framebuffer typically contains the background/wallpaper image, 
    /// which is displayed by default when no other windows exist on top of it.
    bottom_fb: Framebuffer<AlphaPixel>,
    /// The top framebuffer is used for overlaying visual elements atop the rest of the windows, 
    /// e.g., the mouse pointer, the border of a window being dragged/moved, etc. 
    top_fb: Framebuffer<AlphaPixel>,
    /// The final framebuffer which is mapped to the screen (the actual display device).
    pub final_fb: Framebuffer<AlphaPixel>,
}

impl WindowManager {
    /// Sets one window as active, push last active (if exists) to top of show_list. if `refresh` is `true`, will then refresh the window's area.
    /// Returns whether this window is the first active window in the manager.
    /// 
    /// TODO FIXME: (kevinaboos) remove this dumb notion of "first active". This is a bad hack. 
    pub fn set_active(
        &mut self,
        inner_ref: &Arc<Mutex<WindowInner>>,
        refresh: bool,
    ) -> Result<bool, &'static str> {
        // if it is currently actived, just return
        let first_active = match self.active.upgrade() {
            Some(current_active) => {
                if Arc::ptr_eq(&current_active, inner_ref) {
                    return Ok(true); // do nothing
                } else {
                    // save this to show_list
                    self.show_list.push_front(self.active.clone());
                }
                false
            }
            None => true,
        };
        
        if let Some(i) = self.is_window_in_show_list(inner_ref) {
            self.show_list.remove(i);
        }
        if let Some(i) = self.is_window_in_hide_list(inner_ref) {
            self.hide_list.remove(i);
        }
        self.active = Arc::downgrade(inner_ref);
        let area = {
            let window = inner_ref.lock();
            let top_left = window.get_position();
            let (width, height) = window.get_size();          
            Rectangle {
                top_left,
                bottom_right: top_left + (width as isize, height as isize)
            }
        };
        if refresh {
            self.refresh_bottom_windows(Some(area), true)?;
        }
        Ok(first_active)
    }

    /// Returns the index of a window if it is in the show list
    fn is_window_in_show_list(&mut self, window: &Arc<Mutex<WindowInner>>) -> Option<usize> {
        for (i, item) in self.show_list.iter().enumerate() {
            if let Some(item_ptr) = item.upgrade() {
                if Arc::ptr_eq(&item_ptr, window) {
                    return Some(i);
                }
            }
        }
        None
    }

    /// Returns the index of a window if it is in the hide list
    fn is_window_in_hide_list(&mut self, window: &Arc<Mutex<WindowInner>>) -> Option<usize> {
        for (i, item) in self.hide_list.iter().enumerate() {
            if let Some(item_ptr) = item.upgrade() {
                if Arc::ptr_eq(&item_ptr, window) {
                    return Some(i);
                }
            }
        }
        None
    }

    /// delete a window and refresh its region
    pub fn delete_window(&mut self, inner_ref: &Arc<Mutex<WindowInner>>) -> Result<(), &'static str> {
        let (top_left, bottom_right) = {
            let inner = inner_ref.lock();
            let top_left = inner.get_position();
            let (width, height) = inner.get_size();
            let bottom_right = top_left + (width as isize, height as isize);
            (top_left, bottom_right)
        };
        let area = Some(
            Rectangle {
                top_left,
                bottom_right
            }
        );

        if let Some(current_active) = self.active.upgrade() {
            if Arc::ptr_eq(&current_active, inner_ref) {
                self.refresh_bottom_windows(area, false)?;
                if let Some(window) = self.show_list.remove(0) {
                    self.active = window;
                } else if let Some(window) = self.hide_list.remove(0) {
                    self.active = window;
                } else {
                    self.active = Weak::new(); // delete reference
                }
                return Ok(());
            }
        }
        
        if let Some(index) = self.is_window_in_show_list(inner_ref) {
            self.show_list.remove(index);
            self.refresh_windows(area)?;
            return Ok(())        
        }

        if let Some(index) = self.is_window_in_hide_list(inner_ref) {
            self.hide_list.remove(index);
            return Ok(())        
        }
        Err("cannot find this window")
    }

    /// Refresh the region in `bounding_box`. Only render the bottom final framebuffer and windows. Ignore the active window if `active` is false.
    pub fn refresh_bottom_windows<B: CompositableRegion + Clone>(
        &mut self, 
        bounding_box: impl IntoIterator<Item = B> + Clone,
        active: bool,
    ) -> Result<(), &'static str> {
        // bottom framebuffer
        let bottom_fb_area = FramebufferUpdates {
            src_framebuffer: &self.bottom_fb,
            coordinate_in_dest_framebuffer: Coord::new(0, 0),
        };

        // list of windows to be updated
        let mut window_ref_list = Vec::new();
        for window in &self.hide_list {
            if let Some(window_ref) = window.upgrade() {
                window_ref_list.push(window_ref);
            }
        }
        for window in &self.show_list {
            if let Some(window_ref) = window.upgrade() {
                window_ref_list.push(window_ref);
            }
        }
        if active {
            if let Some(window_ref) = self.active.upgrade() {
                window_ref_list.push(window_ref)
            }
        }

        // lock windows
        let locked_window_list = &window_ref_list.iter().map(|x| x.lock()).collect::<Vec<_>>();

        // create updated framebuffer info objects
        let window_bufferlist = locked_window_list.iter().map(|window| {
            FramebufferUpdates {
                src_framebuffer: window.framebuffer(),
                coordinate_in_dest_framebuffer: window.get_position(),
            }
        });
        
        let buffer_iter = Some(bottom_fb_area).into_iter().chain(window_bufferlist);
        FRAME_COMPOSITOR.lock().composite(buffer_iter, &mut self.final_fb, bounding_box)?;
        
        Ok(())
    }

    /// Refresh the region of `bounding_box` in the top framebuffer
    pub fn refresh_top<B: CompositableRegion + Clone>(
        &mut self, 
        bounding_box: impl IntoIterator<Item = B> + Clone
    ) -> Result<(), &'static str> {
        let top_buffer = FramebufferUpdates {
            src_framebuffer: &self.top_fb,
            coordinate_in_dest_framebuffer: Coord::new(0, 0),
        }; 

        FRAME_COMPOSITOR.lock().composite(Some(top_buffer), &mut self.final_fb, bounding_box)
    }

    /// Refresh the part in `bounding_box` of every window. `bounding_box` is a region relative to the top-left of the screen. Refresh the whole screen if the bounding box is None.
    pub fn refresh_windows<B: CompositableRegion + Clone>(
        &mut self, 
        bounding_box: impl IntoIterator<Item = B> + Clone,
    ) -> Result<(), &'static str> {
        // reference of windows
        let mut window_ref_list = Vec::new();
        for window in &self.hide_list {
            if let Some(window_ref) = window.upgrade() {
                window_ref_list.push(window_ref);
            }
        }
        for window in &self.show_list {
            if let Some(window_ref) = window.upgrade() {
                window_ref_list.push(window_ref);
            }
        }

        if let Some(window_ref) = self.active.upgrade() {
            window_ref_list.push(window_ref)
        }

        // lock windows
        let locked_window_list = &window_ref_list.iter().map(|x| x.lock()).collect::<Vec<_>>();
        // create updated framebuffer info objects
        let bufferlist = locked_window_list.iter().map(|window| {
            FramebufferUpdates {
                src_framebuffer: window.framebuffer(),
                coordinate_in_dest_framebuffer: window.get_position(),
            }
        });

        FRAME_COMPOSITOR.lock().composite(bufferlist, &mut self.final_fb, bounding_box)
    }


    /// Refresh the part in `bounding_box` of the active window. `bounding_box` is a region relative to the top-left of the screen. Refresh the whole screen if the bounding box is None.
    pub fn refresh_active_window(&mut self, bounding_box: Option<Rectangle>) -> Result<(), &'static str> {
        if let Some(window_ref) = self.active.upgrade() {
            let window = window_ref.lock();
            let buffer_update = FramebufferUpdates {
                src_framebuffer: window.framebuffer(),
                coordinate_in_dest_framebuffer: window.get_position(),
            };
            FRAME_COMPOSITOR.lock().composite(Some(buffer_update), &mut self.final_fb, bounding_box)
        } else {
            Ok(())
        } 
    }
    
    /// Passes the given keyboard event to the currently active window.
    fn pass_keyboard_event_to_window(&self, key_event: KeyEvent) -> Result<(), &'static str> {
        let active_window = self.active.upgrade().ok_or("no window was set as active to receive a keyboard event")?;
        active_window.lock().send_event(Event::new_keyboard_event(key_event))
            .map_err(|_e| "Failed to enqueue the keyboard event; window event queue was full.")?;
        Ok(())
    }

    /// Passes the given mouse event to the window that the mouse is currently over. 
    /// 
    /// If the mouse is not over any window, an error is returned; 
    /// however, this error is quite common and expected when the mouse is not positioned within a window,
    /// and is not a true failure. 
    fn pass_mouse_event_to_window(&self, mouse_event: MouseEvent) -> Result<(), &'static str> {
        let coordinate = { &self.mouse };
        let mut event: MousePositionEvent = MousePositionEvent {
            coordinate: Coord::new(0, 0),
            gcoordinate: *coordinate,
            scrolling_up: mouse_event.movement.scroll_movement > 0, //TODO: might be more beneficial to save scroll_movement here
            scrolling_down: mouse_event.movement.scroll_movement < 0, //FIXME: also might be the wrong way around
            left_button_hold: mouse_event.buttons.left(),
            right_button_hold: mouse_event.buttons.right(),
            fourth_button_hold: mouse_event.buttons.fourth(),
            fifth_button_hold: mouse_event.buttons.fifth(),
        };

        // TODO: FIXME:  improve this logic to just send the mouse event to the top-most window in the entire WM list,
        //               not just necessarily the active one. (For example, scroll wheel events can be sent to non-active windows).


        // first check the active one
        if let Some(current_active) = self.active.upgrade() {
            let current_active_win = current_active.lock();
            let current_coordinate = current_active_win.get_position();
            if current_active_win.contains(*coordinate - current_coordinate) || matches!(current_active_win.moving, WindowMovingStatus::Moving(_))
            {
                event.coordinate = *coordinate - current_coordinate;
                // debug!("pass to active: {}, {}", event.x, event.y);
                current_active_win.send_event(Event::MousePositionEvent(event))
                    .map_err(|_e| "Failed to enqueue the mouse event; window event queue was full.")?;
                return Ok(());
            }
        }

        // TODO FIXME: (kevinaboos): the logic below here is actually incorrect -- it could send mouse events to an invisible window below others.

        // then check show_list
        for i in 0..self.show_list.len() {
            if let Some(now_inner_mutex) = self.show_list[i].upgrade() {
                let now_inner = now_inner_mutex.lock();
                let current_coordinate = now_inner.get_position();
                if now_inner.contains(*coordinate - current_coordinate) {
                    event.coordinate = *coordinate - current_coordinate;
                    now_inner.send_event(Event::MousePositionEvent(event))
                        .map_err(|_e| "Failed to enqueue the mouse event; window event queue was full.")?;
                    return Ok(());
                }
            }
        }

        Err("the mouse position does not fall within the bounds of any window")
    }

    /// Refresh the floating border, which is used to show the outline of a window while it is being moved. 
    /// `show` indicates whether to show the border or not.
    /// `new_border` defines the rectangular outline of the border.
    fn refresh_floating_border(
        &mut self,
        show: bool,
        new_border: Rectangle,
    ) -> Result<(), &'static str> {
        // first clear old border if exists
        if let Some(border) = self.repositioned_border {
            let pixels = self.draw_floating_border(&border, color::TRANSPARENT);
            self.refresh_bottom_windows(pixels.into_iter(), true)?;
        }

        // then draw current border
        if show {
            let pixels = self.draw_floating_border(&new_border, WINDOW_BORDER_COLOR_INNER);
            self.refresh_top(pixels.into_iter())?;
            self.repositioned_border = Some(new_border);
        } else {
            self.repositioned_border = None;
        }

        Ok(())
    }

    /// draw the floating border with `pixel`. Return the list of coordinates of pixels that were updated.
    /// `border` indicates the position of the border as a rectangle.
    /// `color` is the color of the floating border.
    fn draw_floating_border(&mut self, border: &Rectangle, color: Color) -> Vec<Coord> {
        let mut coordinates = Vec::new();
        let pixel = color.into();
        for i in 0..(WINDOW_BORDER_SIZE) as isize {
            let width = (border.bottom_right.x - border.top_left.x) - 2 * i;
            let height = (border.bottom_right.y - border.top_left.y) - 2 * i;
            let coordinate = border.top_left + (i, i);
            if width <= 0 || height <= 0 {
                break;
            }
            framebuffer_drawer::draw_rectangle(
                &mut self.top_fb, 
                coordinate, 
                width as usize, 
                height as usize, 
                pixel
            );

            for m in 0..width {
                coordinates.push(coordinate + (m, 0));
                coordinates.push(coordinate + (m, height));
            }            
            
            for m in 1..height - 1 {
                coordinates.push(coordinate + (0, m));
                coordinates.push(coordinate + (width, m));
            }            
        }

        coordinates
    }

    /// take active window's base position and current mouse, move the window with delta
    pub fn move_active_window(&mut self) -> Result<(), &'static str> {
        if let Some(current_active) = self.active.upgrade() {
            let border = Rectangle { 
                top_left: Coord::new(0, 0), 
                bottom_right: Coord::new(0, 0) 
            };
            self.refresh_floating_border(false, border)?;

            let (old_top_left, old_bottom_right, new_top_left, new_bottom_right) = {
                let mut current_active_win = current_active.lock();
                let (current_x, current_y) = {
                    let m = &self.mouse;
                    (m.x, m.y)
                };
                match current_active_win.moving {
                    WindowMovingStatus::Moving(base) => {
                        let old_top_left = current_active_win.get_position();
                        let new_top_left = old_top_left + ((current_x - base.x), (current_y - base.y));
                        let (width, height) = current_active_win.get_size();
                        let old_bottom_right = old_top_left + (width as isize, height as isize);
                        let new_bottom_right = new_top_left + (width as isize, height as isize);
                        current_active_win.set_position(new_top_left);
                        (old_top_left, old_bottom_right, new_top_left, new_bottom_right)        
                    },
                    WindowMovingStatus::Stationary => {
                        return Err("The window is not moving");
                    }
                }
            };
            self.refresh_bottom_windows(Some(Rectangle{top_left: old_top_left, bottom_right: old_bottom_right}), false)?;

            self.refresh_active_window(Some(Rectangle{top_left: new_top_left, bottom_right: new_bottom_right}))?;
            self.refresh_mouse()?;
        } else {
            return Err("cannot find active window to move");
        }
        Ok(())
    }

    /// Refresh the mouse display
    pub fn refresh_mouse(&mut self) -> Result<(), &'static str> {
        let bounding_box = Some(Rectangle {
            top_left: self.mouse,
            bottom_right: self.mouse + (MOUSE_POINTER_SIZE_X as isize, MOUSE_POINTER_SIZE_Y as isize)
        });
        
        self.refresh_top(bounding_box)
    }

    /// Move mouse. `relative` indicates the new position relative to current position.
    fn move_mouse(&mut self, relative: Coord) -> Result<(), &'static str> {
        let old = self.mouse;
        let mut new = old + relative;
        
        let (screen_width, screen_height) = self.get_screen_size();
        if new.x < 0 {
            new.x = 0;
        }
        if new.y < 0 {
            new.y = 0;
        }

        // keep mouse pointer border in the screen when it is at the right or bottom side.
        const MOUSE_POINTER_BORDER: isize = 3;
        new.x = core::cmp::min(new.x, screen_width as isize - MOUSE_POINTER_BORDER);
        new.y = core::cmp::min(new.y, screen_height as isize - MOUSE_POINTER_BORDER);
            
        self.move_mouse_to(new)
    }
    
    // Move mouse to absolute position `new`
    fn move_mouse_to(&mut self, new: Coord) -> Result<(), &'static str> {
        // clear old mouse
        for y in self.mouse.y..self.mouse.y + MOUSE_POINTER_SIZE_Y as isize {
            for x in
                self.mouse.x..self.mouse.x + MOUSE_POINTER_SIZE_X as isize {
                let coordinate = Coord::new(x, y);
                self.top_fb.overwrite_pixel(coordinate, color::TRANSPARENT.into());
            }
        }
        let bounding_box = Some(Rectangle {
            top_left: self.mouse,
            bottom_right: self.mouse + (MOUSE_POINTER_SIZE_X as isize, MOUSE_POINTER_SIZE_Y as isize)
        });
        self.refresh_bottom_windows(bounding_box.into_iter(), true)?;

        // draw new mouse
        self.mouse = new;
        for y in new.y..new.y + MOUSE_POINTER_SIZE_Y as isize {
            for x in new.x..new.x + MOUSE_POINTER_SIZE_X as isize {
                let coordinate = Coord::new(x, y);
                let pixel = MOUSE_POINTER_IMAGE[(x - new.x) as usize][(y - new.y) as usize].into();
                self.top_fb.overwrite_pixel(coordinate, pixel);
            }
        }
        self.refresh_mouse()?;

        Ok(())
    }

    /// Move the floating border when a window is moving.
    pub fn move_floating_border(&mut self) -> Result<(), &'static str> {
        let (new_x, new_y) = {
            let m = &self.mouse;
            (m.x, m.y)
        };
        
        if let Some(current_active) = self.active.upgrade() {
            let (is_draw, border_start, border_end) = {
                let current_active_win = current_active.lock();
                match current_active_win.moving {
                    WindowMovingStatus::Moving(base) => {
                        // move this window
                        // for better performance, while moving window, only border is shown for indication
                        let coordinate = current_active_win.get_position();
                        // let (current_x, current_y) = (coordinate.x, coordinate.y);
                        let (width, height) = current_active_win.get_size();
                        let border_start = coordinate + (new_x - base.x, new_y - base.y);
                        let border_end = border_start + (width as isize, height as isize);
                        (true, border_start, border_end)
                    }
                    WindowMovingStatus::Stationary => (false, Coord::new(0, 0), Coord::new(0, 0)),
                }
            };
            let border = Rectangle {
                top_left: border_start,
                bottom_right: border_end,
            };
            self.refresh_floating_border(is_draw, border)?;
        } else {
            let border = Rectangle {
                top_left: Coord::new(0, 0),
                bottom_right: Coord::new(0, 0),
            };
            self.refresh_floating_border(false, border)?;
        }

        Ok(())
    }

    /// Returns true if the given `window` is the currently active window.
    pub fn is_active(&self, window: &Arc<Mutex<WindowInner>>) -> bool {
        self.active.upgrade()
            .map(|active| Arc::ptr_eq(&active, window))
            .unwrap_or(false)
    }

    /// Returns the `(width, height)` in pixels of the screen itself (the final framebuffer).
    pub fn get_screen_size(&self) -> (usize, usize) {
        self.final_fb.get_size()
    }
}

/// Initialize the window manager. It returns (keyboard_producer, mouse_producer) for the I/O devices.
pub fn init() -> Result<(Queue<Event>, Queue<Event>), &'static str> {
    let final_fb: Framebuffer<AlphaPixel> = framebuffer::init()?;
    let (width, height) = final_fb.get_size();

    let mut bottom_fb = Framebuffer::new(width, height, None)?;
    let mut top_fb = Framebuffer::new(width, height, None)?;
    let (screen_width, screen_height) = bottom_fb.get_size();
    bottom_fb.fill(color::LIGHT_GRAY.into());
    top_fb.fill(color::TRANSPARENT.into()); 

    // Initial position for the mouse
    let mouse = Coord {
        x: screen_width as isize / 2,
        y: screen_height as isize / 2,
    }; 

    // Initialize static window manager
    let window_manager = WindowManager {
        hide_list: VecDeque::new(),
        show_list: VecDeque::new(),
        active: Weak::new(),
        mouse,
        repositioned_border: None,
        bottom_fb,
        top_fb,
        final_fb,
    };
    WINDOW_MANAGER.call_once(|| Mutex::new(window_manager));

    // keyinput queue initialization
    let key_consumer: Queue<Event> = Queue::with_capacity(100);
    let key_producer = key_consumer.clone();

    // mouse input queue initialization
    let mouse_consumer: Queue<Event> = Queue::with_capacity(100);
    let mouse_producer = mouse_consumer.clone();

    spawn::new_task_builder(window_manager_loop, (key_consumer, mouse_consumer))
        .name("window_manager_loop".to_string())
        .spawn()?;

    Ok((key_producer, mouse_producer))
}

/// handles all keyboard and mouse movement in this window manager
fn window_manager_loop(
    (key_consumer, mouse_consumer): (Queue<Event>, Queue<Event>),
) -> Result<(), &'static str> {
    loop {
        let event_opt = key_consumer.pop()
            .or_else(||mouse_consumer.pop())
            .or_else(||{
                scheduler::schedule();
                None
            });

        if let Some(event) = event_opt {
            // Currently, the window manager only cares about keyboard or mouse events
            match event {
                Event::KeyboardEvent(ref input_event) => {
                    let key_input = input_event.key_event;
                    keyboard_handle_application(key_input)?;
                }
                Event::MouseMovementEvent(ref mouse_event) => {
                    //as isize to fit larger values
                    let mut x = mouse_event.movement.x_movement as isize;
                    let mut y = mouse_event.movement.y_movement as isize;

                    // need to combine mouse events if there pending a lot
                    while let Some(next_event) = mouse_consumer.pop() {
                        match next_event {
                            Event::MouseMovementEvent(ref next_mouse_event) => {
                                if next_mouse_event.movement.scroll_movement
                                    == mouse_event.movement.scroll_movement
                                    && next_mouse_event.buttons.left()
                                        == mouse_event.buttons.left()
                                    && next_mouse_event.buttons.right()
                                        == mouse_event.buttons.right()
                                    && next_mouse_event.buttons.fourth()
                                        == mouse_event.buttons.fourth()
                                    && next_mouse_event.buttons.fifth()
                                        == mouse_event.buttons.fifth() {
                                    x = x.saturating_add(next_mouse_event.movement.x_movement as isize);
                                    y = y.saturating_add(next_mouse_event.movement.y_movement as isize);
                                }
                            }
                            _ => {
                                break;
                            }
                        }
                        // next_event.mark_completed();
                    }
                    if x != 0 || y != 0 {
                        let mut wm = WINDOW_MANAGER
                            .get()
                            .ok_or("The static window manager was not yet initialized")?
                            .lock();
                        wm.move_mouse(
                            Coord::new(x, -y)
                        )?;
                    }
                    cursor_handle_application(mouse_event.clone())?; // tell the event to application, or moving window
                }
                _other => {
                    trace!("WINDOW_MANAGER: ignoring unexpected event: {:?}", _other);
                }
            }
        }
    }
}

/// handle keyboard event, push it to the active window if one exists
fn keyboard_handle_application(key_input: KeyEvent) -> Result<(), &'static str> {
    let win_mgr = WINDOW_MANAGER.get().ok_or("The window manager was not yet initialized")?;
    
    // First, we handle keyboard shortcuts understood by the window manager.
    
    // "Super + Arrow" will resize and move windows to the specified half of the screen (left, right, top, or bottom)
    if key_input.modifiers.is_super_key() && key_input.action == KeyAction::Pressed {
        let screen_dimensions = win_mgr.lock().get_screen_size();
        let (width, height) = (screen_dimensions.0 as isize, screen_dimensions.1 as isize);
        let new_position: Option<Rectangle> = match key_input.keycode {
            Keycode::Left => Some(Rectangle {
                top_left:     Coord { x: 0, y: 0 },
                bottom_right: Coord { x: width / 2, y: height },
            }),
            Keycode::Right => Some(Rectangle {
                top_left:     Coord { x: width / 2, y: 0 },
                bottom_right: Coord { x: width, y: height },
            }),
            Keycode::Up => Some(Rectangle {
                top_left:     Coord { x: 0, y: 0 },
                bottom_right: Coord { x: width, y: height / 2 },
            }),
            Keycode::Down => Some(Rectangle {
                top_left:     Coord { x: 0, y: height / 2 },
                bottom_right: Coord { x: width, y: height },
            }),
            _ => None,
        };
        
        if let Some(position) = new_position {
            let mut wm = win_mgr.lock();
            if let Some(active_window) = wm.active.upgrade() {
                debug!("window_manager: resizing active window to {:?}", new_position);
                active_window.lock().resize(position)?;

                // force refresh the entire screen for now
                // TODO: perform a proper screen refresh here: only refresh the area that contained the active_window's old bounds.
                wm.refresh_bottom_windows(Option::<Rectangle>::None, true)?;
            }
        }

        return Ok(());
    }

    // Spawn a new terminal via Ctrl+Alt+T
    if key_input.modifiers.is_control()
        && key_input.modifiers.is_alt()
        && key_input.keycode == Keycode::T
        && key_input.action == KeyAction::Pressed
    {
        // Because this task (the window manager loop) runs in a kernel-only namespace,
        // we have to create a new application namespace in order to be able to actually spawn a shell.

        let new_app_namespace = mod_mgmt::create_application_namespace(None)?;
        let shell_objfile = new_app_namespace.dir().get_file_starting_with("shell-")
            .ok_or("Couldn't find shell application file to run upon Ctrl+Alt+T")?;
        let path = shell_objfile.lock().get_absolute_path();
        spawn::new_application_task_builder(path.as_ref(), Some(new_app_namespace))?
            .name("shell".to_string())
            .spawn()?;

        debug!("window_manager: spawned new shell app in new app namespace.");
        return Ok(());
    }

    // Any keyboard event unhandled above should be passed to the active window.
    if let Err(_e) = win_mgr.lock().pass_keyboard_event_to_window(key_input) {
        warn!("window_manager: failed to pass keyboard event to active window. Error: {:?}", _e);
        // If no window is currently active, then something might be potentially wrong, 
        // but we can likely recover in the future when another window becomes active.
        // Thus, we don't need to return a hard error here. 
    }
    Ok(())
}

/// handle mouse event, push it to related window or anyone asked for it
fn cursor_handle_application(mouse_event: MouseEvent) -> Result<(), &'static str> {
    let wm = WINDOW_MANAGER.get().ok_or("The static window manager was not yet initialized")?.lock();
    if wm.pass_mouse_event_to_window(mouse_event).is_err() {
        // the mouse event should be passed to the window that satisfies:
        // 1. the mouse position is currently in the window area
        // 2. the window is the top one (active window or show_list windows) under the mouse pointer
        // if no window is found in this position, that is system background area. Add logic to handle those events later
    }
    Ok(())
}