wm_ipc.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 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_WM_IPC_H_
6#define CHROME_BROWSER_CHROMEOS_WM_IPC_H_
7
8#include <gtk/gtk.h>
9#include <map>
10#include <string>
11#include <vector>
12
13#include "base/logging.h"
14#include "base/singleton.h"
15#include "cros/chromeos_wm_ipc_enums.h"
16
17typedef unsigned long Atom;
18typedef unsigned long XID;
19
20namespace chromeos {
21
22class WmIpc {
23 public:
24  enum AtomType {
25    ATOM_CHROME_LOGGED_IN = 0,
26    ATOM_CHROME_WINDOW_TYPE,
27    ATOM_CHROME_WM_MESSAGE,
28    ATOM_MANAGER,
29    ATOM_STRING,
30    ATOM_UTF8_STRING,
31    ATOM_WM_S0,
32    kNumAtoms,
33  };
34
35  struct Message {
36   public:
37    Message() {
38      Init(WM_IPC_MESSAGE_UNKNOWN);
39    }
40    // WmIpcMessageType is defined in chromeos_wm_ipc_enums.h.
41    explicit Message(WmIpcMessageType type) {
42      Init(type);
43    }
44
45    WmIpcMessageType type() const { return type_; }
46    void set_type(WmIpcMessageType type) { type_ = type; }
47
48    inline int max_params() const {
49      return arraysize(params_);
50    }
51    long param(int index) const {
52      DCHECK_GE(index, 0);
53      DCHECK_LT(index, max_params());
54      return params_[index];
55    }
56    void set_param(int index, long value) {
57      DCHECK_GE(index, 0);
58      DCHECK_LT(index, max_params());
59      params_[index] = value;
60    }
61
62   private:
63    // Common initialization code shared between constructors.
64    void Init(WmIpcMessageType type) {
65      set_type(type);
66      for (int i = 0; i < max_params(); ++i) {
67        set_param(i, 0);
68      }
69    }
70
71    // Type of message that was sent.
72    WmIpcMessageType type_;
73
74    // Type-specific data.  This is bounded by the number of 32-bit values
75    // that we can pack into a ClientMessageEvent -- it holds five, but we
76    // use the first one to store the message type.
77    long params_[4];
78  };
79
80  // Returns the single instance of WmIpc.
81  static WmIpc* instance();
82
83  // Gets or sets a property describing a window's type.
84  // WmIpcMessageType is defined in chromeos_wm_ipc_enums.h.  Type-specific
85  // parameters may also be supplied.  The caller is responsible for trapping
86  // errors from the X server.
87  bool SetWindowType(GtkWidget* widget,
88                     WmIpcWindowType type,
89                     const std::vector<int>* params);
90
91  // Gets the type of the window, and any associated parameters. The
92  // caller is responsible for trapping errors from the X server.  If
93  // the parameters are not interesting to the caller, NULL may be
94  // passed for |params|.
95  WmIpcWindowType GetWindowType(GtkWidget* widget, std::vector<int>* params);
96
97  // Sends a message to the WM.
98  void SendMessage(const Message& msg);
99
100  // If |event| is a valid Message it is decoded into |msg| and true is
101  // returned. If false is returned, |event| is not a valid Message.
102  bool DecodeMessage(const GdkEventClient& event, Message* msg);
103
104  // Handles ClientMessage events that weren't decodable using DecodeMessage().
105  // Specifically, this catches messages about the WM_S0 selection that get sent
106  // when a window manager process starts (so that we can re-run InitWmInfo()).
107  // See ICCCM 2.8 for more info about MANAGER selections.
108  void HandleNonChromeClientMessageEvent(const GdkEventClient& event);
109
110  // Sets a _CHROME_LOGGED_IN property on the root window describing whether
111  // the user is currently logged in or not.
112  void SetLoggedInProperty(bool logged_in);
113
114 private:
115  friend struct DefaultSingletonTraits<WmIpc>;
116
117  WmIpc();
118
119  // Initialize 'wm_' and send the window manager a message telling it the
120  // version of the IPC protocol that we support.  This is called in our
121  // constructor, but needs to be re-run if the window manager gets restarted.
122  void InitWmInfo();
123
124  // Maps from our Atom enum to the X server's atom IDs and from the
125  // server's IDs to atoms' string names.  These maps aren't necessarily in
126  // sync; 'atom_to_xatom_' is constant after the constructor finishes but
127  // GetName() caches additional string mappings in 'xatom_to_string_'.
128  std::map<AtomType, Atom> type_to_atom_;
129  std::map<Atom, std::string> atom_to_string_;
130
131  // Cached value of type_to_atom_[ATOM_CHROME_WM_MESSAGE].
132  Atom wm_message_atom_;
133
134  // Handle to the wm. Used for sending messages.
135  XID wm_;
136
137  DISALLOW_COPY_AND_ASSIGN(WmIpc);
138};
139
140}  // namespace chromeos
141
142#endif  // CHROME_BROWSER_CHROMEOS_WM_IPC_H_
143