OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/idle.h" | 5 #include "chrome/browser/idle.h" |
6 | 6 |
| 7 #include <gdk/gdk.h> |
| 8 #include <gdk/gdkx.h> |
| 9 |
| 10 #include <vector> |
| 11 |
| 12 #include "base/basictypes.h" |
7 #include "chrome/browser/sync/engine/idle_query_linux.h" | 13 #include "chrome/browser/sync/engine/idle_query_linux.h" |
| 14 #include "chrome/browser/ui/gtk/gtk_util.h" |
| 15 #include "ui/base/x/x11_util.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 class ScreensaverWindowFinder : public ui::EnumerateWindowsDelegate { |
| 20 public: |
| 21 ScreensaverWindowFinder() |
| 22 : exists_(false) {} |
| 23 |
| 24 bool exists() const { return exists_; } |
| 25 |
| 26 protected: |
| 27 virtual bool ShouldStopIterating(XID window) { |
| 28 if (!ui::IsWindowVisible(window) || !isScreensaverWindow(window)) |
| 29 return false; |
| 30 exists_ = true; |
| 31 return true; |
| 32 } |
| 33 |
| 34 private: |
| 35 bool isScreensaverWindow(XID window) const { |
| 36 // It should occupy full screen. |
| 37 if (!ui::IsX11WindowFullScreen(window)) |
| 38 return false; |
| 39 |
| 40 // For xscreensaver, the window should have _SCREENSAVER_VERSION property. |
| 41 if (ui::PropertyExists(window, "_SCREENSAVER_VERSION")) |
| 42 return true; |
| 43 |
| 44 // For all others, like gnome-screensaver, the window's WM_CLASS property |
| 45 // should contains "screensaver". |
| 46 std::string value; |
| 47 if (ui::GetStringProperty(window, "WM_CLASS", &value)) { |
| 48 size_t pos = 0, len = value.length(); |
| 49 while (pos < len) { |
| 50 std::string str(value.c_str() + pos); |
| 51 if (str.find("screensaver") != std::string::npos) |
| 52 return true; |
| 53 pos += str.length() + 1; |
| 54 } |
| 55 } |
| 56 |
| 57 return false; |
| 58 } |
| 59 |
| 60 bool exists_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(ScreensaverWindowFinder); |
| 63 }; |
| 64 |
| 65 bool ExistsScreensaverWindow() { |
| 66 ScreensaverWindowFinder finder; |
| 67 gtk_util::EnumerateTopLevelWindows(&finder); |
| 68 return finder.exists(); |
| 69 } |
| 70 |
| 71 bool IsScreensaverRunning() { |
| 72 gdk_error_trap_push(); |
| 73 bool result = ExistsScreensaverWindow(); |
| 74 bool got_error = gdk_error_trap_pop(); |
| 75 return result && !got_error; |
| 76 } |
| 77 |
| 78 } |
8 | 79 |
9 IdleState CalculateIdleState(unsigned int idle_threshold) { | 80 IdleState CalculateIdleState(unsigned int idle_threshold) { |
| 81 // Usually screensaver is used to lock the screen. So we do not need to check |
| 82 // if the workstation is locked. |
| 83 if (IsScreensaverRunning()) |
| 84 return IDLE_STATE_LOCKED; |
| 85 |
10 browser_sync::IdleQueryLinux idle_query; | 86 browser_sync::IdleQueryLinux idle_query; |
11 unsigned int idle_time = idle_query.IdleTime(); | 87 unsigned int idle_time = idle_query.IdleTime(); |
12 if (idle_time >= idle_threshold) | 88 if (idle_time >= idle_threshold) |
13 return IDLE_STATE_IDLE; | 89 return IDLE_STATE_IDLE; |
14 return IDLE_STATE_ACTIVE; | 90 return IDLE_STATE_ACTIVE; |
15 } | 91 } |
OLD | NEW |