GlobalHotkeys.cpp
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25  */
26 
32 #include "GlobalHotkeys.h"
33 #include <nsStringGlue.h>
34 
35 // CLASSES ====================================================================
36 //=============================================================================
37 // CGlobalHotkeys Class
38 //=============================================================================
39 
40 //-----------------------------------------------------------------------------
41 /* Implementation file */
43 
44 #ifdef XP_WIN
45 
46 #define GLOBALHOTKEYS_WNDCLASS NS_L("sbGlobalHotkeys")
47 PRInt32 CGlobalHotkeys::m_autoinc = 1;
48 
49 //-----------------------------------------------------------------------------
50 static LRESULT CALLBACK GlobalHotkeysProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
51 {
52  CGlobalHotkeys *_this = reinterpret_cast<CGlobalHotkeys*>(GetWindowLong(hWnd, GWL_USERDATA));
53  return _this->WndProc(hWnd, uMsg, wParam, lParam);
54 } // GlobalHotkeysProc
55 
56 #endif
57 
58 //-----------------------------------------------------------------------------
60 {
61 #ifdef XP_WIN
62  m_window = NULL;
63 
64  WNDCLASS wndClass;
65  memset(&wndClass, 0, sizeof(wndClass));
66  wndClass.hInstance = GetModuleHandle(NULL);
67  wndClass.lpfnWndProc = GlobalHotkeysProc;
68  wndClass.lpszClassName = GLOBALHOTKEYS_WNDCLASS;
69  RegisterClass(&wndClass);
70 
71  m_window = CreateWindow(GLOBALHOTKEYS_WNDCLASS, NULL, WS_POPUP, 0, 0, 1, 1, NULL, NULL, GetModuleHandle(NULL), NULL);
72  SetWindowLong(m_window, GWL_USERDATA, (LPARAM)this);
73 #endif
74 } // ctor
75 
76 //-----------------------------------------------------------------------------
78 {
79 #ifdef XP_WIN
80  RemoveAllHotkeys();
81  DestroyWindow(m_window);
82  m_window = NULL;
83  UnregisterClass(GLOBALHOTKEYS_WNDCLASS, GetModuleHandle(NULL));
84 #endif
85 } // dtor
86 
87 //-----------------------------------------------------------------------------
88 NS_IMETHODIMP CGlobalHotkeys::AddHotkey(PRInt32 keyCode, PRBool altKey, PRBool ctrlKey, PRBool shiftKey, PRBool metaKey, const nsAString &keyid, sbIGlobalHotkeyCallback *callback)
89 {
90  HOTKEY_HANDLE handle = registerHotkey(keyCode, altKey, ctrlKey, shiftKey, metaKey);
91  if (handle == NULL) return NS_ERROR_FAILURE;
92  NS_ADDREF(callback);
94  entry->m_callback = callback;
95  entry->m_keyid = keyid;
96  entry->m_handle = handle;
97  m_hotkeys.push_back(entry);
98  return NS_OK;
99 } // AddHotkey
100 
101 std::list<GlobalHotkeyEntry *> CGlobalHotkeys::m_hotkeys;
102 
103 //-----------------------------------------------------------------------------
104 NS_IMETHODIMP CGlobalHotkeys::RemoveHotkey(const nsAString &keyid)
105 {
106  GlobalHotkeyEntry *entry = findHotkeyById(keyid);
107  if (!entry) return NS_ERROR_FAILURE;
108  removeEntry(entry);
109  return NS_OK;
110 } // RemoveHotkey
111 
112 //-----------------------------------------------------------------------------
114  if (!entry) return;
115  m_hotkeys.remove(entry);
116  NS_RELEASE(entry->m_callback);
117  unregisterHotkey(entry->m_handle);
118  delete entry;
119 }
120 
121 //-----------------------------------------------------------------------------
122 NS_IMETHODIMP CGlobalHotkeys::RemoveAllHotkeys()
123 {
124  std::list<GlobalHotkeyEntry*>::iterator iter;
125  while (m_hotkeys.size() > 0) {
126  GlobalHotkeyEntry *entry = *m_hotkeys.begin();
127  removeEntry(entry);
128  }
129  return NS_OK;
130 } // RemoveAllHotkeys
131 
132 
133 #ifdef XP_WIN
134 
135 //-----------------------------------------------------------------------------
136 LRESULT CGlobalHotkeys::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
137 {
138  if (uMsg == WM_HOTKEY)
139  {
140  int idHotKey = (int) wParam;
141  GlobalHotkeyEntry *entry = findHotkeyByHandle(idHotKey);
142  if (entry) {
143  entry->m_callback->OnHotkey(entry->m_keyid);
144  }
145  }
146  return DefWindowProc(hWnd, uMsg, wParam, lParam);
147 } // WndProc
148 
149 #endif
150 
151 //-----------------------------------------------------------------------------
152 HOTKEY_HANDLE CGlobalHotkeys::registerHotkey(PRInt32 keyCode, PRBool altKey, PRBool ctrlKey, PRBool shiftKey, PRBool metaKey)
153 {
154 #ifdef XP_WIN
155  if (m_autoinc == 0xBFFF) return NULL;
156  RegisterHotKey(m_window, m_autoinc, makeWin32Mask(altKey, ctrlKey, shiftKey, metaKey), keyCode);
157  return m_autoinc++;
158 #endif
159  return NULL;
160 }
161 
162 //-----------------------------------------------------------------------------
164 {
165 #ifdef XP_WIN
166  UnregisterHotKey(m_window, handle);
167 #endif
168 }
169 
170 //-----------------------------------------------------------------------------
172 {
173  std::list<GlobalHotkeyEntry*>::iterator iter;
174  for (iter = m_hotkeys.begin(); iter != m_hotkeys.end(); iter++)
175  {
176  GlobalHotkeyEntry *entry = *iter;
177  if (entry->m_keyid.Equals(keyid)) return entry;
178  }
179  return NULL;
180 }
181 
182 //-----------------------------------------------------------------------------
184 {
185  std::list<GlobalHotkeyEntry*>::iterator iter;
186  for (iter = m_hotkeys.begin(); iter != m_hotkeys.end(); iter++)
187  {
188  GlobalHotkeyEntry *entry = *iter;
189  if (entry->m_handle == handle) return entry;
190  }
191  return NULL;
192 }
193 
194 #ifdef XP_WIN
195 //-----------------------------------------------------------------------------
196 UINT CGlobalHotkeys::makeWin32Mask(PRBool altKey, PRBool ctrlKey, PRBool shiftKey, PRBool metaKey)
197 {
198  return (altKey ? MOD_ALT : 0) |
199  (ctrlKey ? MOD_CONTROL : 0) |
200  (shiftKey ? MOD_SHIFT : 0) |
201  (metaKey ? MOD_WIN : 0);
202 
203 }
204 #endif
205 
classDescription entry
Definition: FeedWriter.js:1427
sbIGlobalHotkeyCallback * m_callback
Definition: GlobalHotkeys.h:67
void removeEntry(GlobalHotkeyEntry *entry)
return NS_OK
static std::list< GlobalHotkeyEntry * > m_hotkeys
Definition: GlobalHotkeys.h:96
Global Hotkeys Manager.
Global hotkey registration/unregistration interface This interface is used to register or unregister ...
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
Global hotkeys callback interface This interface is used by callback objects to receive a notificatio...
function handle(request, response)
int registerHotkey(PRInt32 keyCode, PRBool altKey, PRBool ctrlKey, PRBool shiftKey, PRBool metaKey)
return e ctrlKey(chr<' '||!chars||chars.indexOf(chr)>-1)
var _this
grep callback
GlobalHotkeyEntry * findHotkeyById(const nsAString &keyid)
GlobalHotkeyEntry * findHotkeyByHandle(int handle)
void unregisterHotkey(int handle)
Definition: GlobalHotkeys.h:62
#define HOTKEY_HANDLE
Definition: GlobalHotkeys.h:50
nsString m_keyid
Definition: GlobalHotkeys.h:66
int m_handle
Definition: GlobalHotkeys.h:65
virtual ~CGlobalHotkeys()