Home | History | Annotate | Download | only in chromeos
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_CHROMEOS_SYSTEM_KEY_EVENT_LISTENER_H_
      6 #define CHROME_BROWSER_CHROMEOS_SYSTEM_KEY_EVENT_LISTENER_H_
      7 
      8 #include "base/memory/singleton.h"
      9 #include "base/message_loop/message_loop.h"
     10 
     11 typedef union _XEvent XEvent;
     12 
     13 namespace chromeos {
     14 
     15 class SystemKeyEventListener : public base::MessageLoopForUI::Observer {
     16  public:
     17   // Observer for caps lock state changes.
     18   class CapsLockObserver {
     19    public:
     20     virtual void OnCapsLockChange(bool enabled) = 0;
     21 
     22    protected:
     23     CapsLockObserver() {}
     24     virtual ~CapsLockObserver() {}
     25 
     26     DISALLOW_COPY_AND_ASSIGN(CapsLockObserver);
     27   };
     28 
     29   // Observer for modifier keys' state changes.
     30   class ModifiersObserver {
     31    public:
     32     enum {
     33       SHIFT_PRESSED = 1,
     34       CTRL_PRESSED = 1 << 1,
     35       ALT_PRESSED = 1 << 2,
     36     };
     37 
     38     // |pressed_modifiers| is a bitmask of SHIFT_PRESSED, CTRL_PRESSED and
     39     // ALT_PRESSED.
     40     virtual void OnModifiersChange(int pressed_modifiers) = 0;
     41 
     42    protected:
     43     ModifiersObserver() {}
     44     virtual ~ModifiersObserver() {}
     45 
     46     DISALLOW_COPY_AND_ASSIGN(ModifiersObserver);
     47   };
     48 
     49   static void Initialize();
     50   static void Shutdown();
     51   // GetInstance returns NULL if not initialized or if already shutdown.
     52   static SystemKeyEventListener* GetInstance();
     53 
     54   void Stop();
     55 
     56   void AddCapsLockObserver(CapsLockObserver* observer);
     57   void AddModifiersObserver(ModifiersObserver* observer);
     58   void RemoveCapsLockObserver(CapsLockObserver* observer);
     59   void RemoveModifiersObserver(ModifiersObserver* observer);
     60 
     61  private:
     62   // Defines the delete on exit Singleton traits we like.  Best to have this
     63   // and const/dest private as recommended for Singletons.
     64   friend struct DefaultSingletonTraits<SystemKeyEventListener>;
     65   friend class SystemKeyEventListenerTest;
     66 
     67   SystemKeyEventListener();
     68   virtual ~SystemKeyEventListener();
     69 
     70   // MessageLoopForUI::Observer overrides.
     71   virtual base::EventStatus WillProcessEvent(
     72       const base::NativeEvent& event) OVERRIDE;
     73   virtual void DidProcessEvent(const base::NativeEvent& event) OVERRIDE;
     74 
     75   void OnCapsLock(bool enabled);
     76   void OnModifiers(int state);
     77 
     78   // Returns true if the event was processed, false otherwise.
     79   virtual bool ProcessedXEvent(XEvent* xevent);
     80 
     81   bool stopped_;
     82 
     83   unsigned int num_lock_mask_;
     84   bool caps_lock_is_on_;
     85   int pressed_modifiers_;
     86   ObserverList<CapsLockObserver> caps_lock_observers_;
     87   ObserverList<ModifiersObserver> modifiers_observers_;
     88 
     89   // Base X ID for events from the XKB extension.
     90   int xkb_event_base_;
     91 
     92   DISALLOW_COPY_AND_ASSIGN(SystemKeyEventListener);
     93 };
     94 
     95 }  // namespace chromeos
     96 
     97 #endif  // CHROME_BROWSER_CHROMEOS_SYSTEM_KEY_EVENT_LISTENER_H_
     98