File: singleton_hwnd_hot_key_observer.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (97 lines) | stat: -rw-r--r-- 3,100 bytes parent folder | download | duplicates (9)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/win/singleton_hwnd_hot_key_observer.h"

#include <optional>

#include "base/containers/flat_set.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/no_destructor.h"
#include "ui/gfx/win/singleton_hwnd.h"

namespace gfx {

namespace {

base::flat_set<int>& GetUsedHotKeyIDs() {
  static base::NoDestructor<base::flat_set<int>> used_hot_key_ids;
  return *used_hot_key_ids;
}

std::optional<int> GetAvailableHotKeyID() {
  // Valid hot key IDs are in the range 0x0000 to 0xBFFF. See
  // https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-registerhotkey
  for (int i = 0x0000; i < 0xBFFF; i++) {
    if (!GetUsedHotKeyIDs().contains(i))
      return i;
  }
  return std::nullopt;
}

void SetHotKeyIDUsed(int id) {
  DCHECK(!GetUsedHotKeyIDs().contains(id));
  GetUsedHotKeyIDs().insert(id);
}

void SetHotKeyIDAvailable(int id) {
  DCHECK(GetUsedHotKeyIDs().contains(id));
  GetUsedHotKeyIDs().erase(id);
}

}  // anonymous namespace

std::unique_ptr<SingletonHwndHotKeyObserver>
SingletonHwndHotKeyObserver::Create(
    const SingletonHwndObserver::WndProc& wnd_proc,
    UINT key_code,
    int modifiers) {
  std::optional<int> hot_key_id = GetAvailableHotKeyID();

  // If there are no available hot key IDs, return null.
  if (!hot_key_id.has_value())
    return nullptr;

  // If we fail to register the hot key, return null. Most likely reason for
  // failure is that another application has already registered the hot key.
  if (!RegisterHotKey(gfx::SingletonHwnd::GetInstance()->hwnd(), *hot_key_id,
                      modifiers, key_code)) {
    return nullptr;
  }

  return base::WrapUnique(
      new SingletonHwndHotKeyObserver(wnd_proc, *hot_key_id));
}

SingletonHwndHotKeyObserver::SingletonHwndHotKeyObserver(
    const SingletonHwndObserver::WndProc& wnd_proc,
    int hot_key_id)
    : observer_(base::BindRepeating(&SingletonHwndHotKeyObserver::OnWndProc,
                                    base::Unretained(this))),
      wnd_proc_(wnd_proc),
      hot_key_id_(hot_key_id) {
  SetHotKeyIDUsed(hot_key_id);
}

SingletonHwndHotKeyObserver::~SingletonHwndHotKeyObserver() {
  bool success = !!UnregisterHotKey(gfx::SingletonHwnd::GetInstance()->hwnd(),
                                    hot_key_id_);
  // This call should always succeed, as long as we pass in the right HWND and
  // an id we've used to register before.
  DCHECK(success);
  SetHotKeyIDAvailable(hot_key_id_);
}

void SingletonHwndHotKeyObserver::OnWndProc(HWND hwnd,
                                            UINT message,
                                            WPARAM wparam,
                                            LPARAM lparam) {
  // Only propagate WM_HOTKEY messages for this particular hot key to the owner
  // of this observer.
  if (message == WM_HOTKEY && static_cast<int>(wparam) == hot_key_id_)
    wnd_proc_.Run(hwnd, message, wparam, lparam);
}

}  // namespace gfx