sbScreenSaverSuppressor.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
27 //------------------------------------------------------------------------------
28 //------------------------------------------------------------------------------
29 //
30 // Songbird screen saver suppressor services.
31 //
32 // For documentation on the Gnome screen saver D-Bus interface, see
33 // http://www.gnome.org/~mccann/gnome-screensaver/docs/gnome-screensaver.html
34 //
35 //------------------------------------------------------------------------------
36 //------------------------------------------------------------------------------
37 
43 //------------------------------------------------------------------------------
44 //
45 // Songbird screen saver suppressor imported services.
46 //
47 //------------------------------------------------------------------------------
48 
49 // Self imports.
51 
52 // Local imports.
53 #include "sbDBus.h"
54 
55 // Mozilla imports.
56 #include <nsAutoPtr.h>
57 
58 
59 //------------------------------------------------------------------------------
60 //
61 // Songbird screen saver suppressor nsISupports services.
62 //
63 //------------------------------------------------------------------------------
64 
67 
68 
69 //------------------------------------------------------------------------------
70 //
71 // Songbird screen saver suppressor sbBaseScreenSaverSuppressor services.
72 //
73 //------------------------------------------------------------------------------
74 
75 //-------------------------------------
76 //
77 // OnSuppress
78 //
79 
80 /*virtual*/ nsresult
81 sbScreenSaverSuppressor::OnSuppress(PRBool aSuppress)
82 {
83  nsresult rv;
84 
85  // Suppress or unsupress the screen saver.
86  if (aSuppress) {
87  rv = Suppress();
88  NS_ENSURE_SUCCESS(rv, rv);
89  }
90  else {
91  rv = Unsuppress();
92  NS_ENSURE_SUCCESS(rv, rv);
93  }
94 
95  return NS_OK;
96 }
97 
98 
99 //------------------------------------------------------------------------------
100 //
101 // Public Songbird screen saver suppressor services.
102 //
103 //------------------------------------------------------------------------------
104 
105 //-------------------------------------
106 //
107 // sbScreenSaverSuppressor
108 //
109 
111  mSuppressed(PR_FALSE)
112 {
113 }
114 
115 
116 //-------------------------------------
117 //
118 // ~sbScreenSaverSuppressor
119 //
120 
122 {
123  // Unsuppress.
124  Unsuppress();
125 }
126 
127 
128 //------------------------------------------------------------------------------
129 //
130 // Private Songbird screen saver suppressor services.
131 //
132 //------------------------------------------------------------------------------
133 
134 //-------------------------------------
135 //
136 // Suppress
137 //
138 
139 nsresult
140 sbScreenSaverSuppressor::Suppress()
141 {
142  nsresult rv;
143 
144  // Do nothing if already suppressed.
145  if (mSuppressed)
146  return NS_OK;
147 
148  // Open a D-Bus connection to the Gnome screen saver.
149  nsAutoPtr<sbDBusConnection> dBusConnection;
150  rv = sbDBusConnection::New(getter_Transfers(dBusConnection),
151  DBUS_BUS_SESSION,
152  "org.gnome.ScreenSaver",
153  "/org/gnome/ScreenSaver",
154  "org.gnome.ScreenSaver");
155  NS_ENSURE_SUCCESS(rv, rv);
156 
157  // Inhibit the screen saver.
158  static const char* name = "Songbird";
159  static const char* reason = "Playing video";
160  nsAutoPtr<sbDBusMessage> reply;
161  rv = dBusConnection->InvokeMethod("Inhibit",
162  getter_Transfers(reply),
163  DBUS_TYPE_STRING,
164  &name,
165  DBUS_TYPE_STRING,
166  &reason,
167  DBUS_TYPE_INVALID);
168  NS_ENSURE_SUCCESS(rv, rv);
169 
170  // Get the screen saver inhibit cookie.
171  rv = reply->GetArgs(DBUS_TYPE_UINT32, &mInhibitCookie, DBUS_TYPE_INVALID);
172  NS_ENSURE_SUCCESS(rv, rv);
173 
174  // Screen saver is now suppressed.
175  mSuppressed = PR_TRUE;
176 
177  return NS_OK;
178 }
179 
180 
181 //-------------------------------------
182 //
183 // Unsuppress
184 //
185 
186 nsresult
187 sbScreenSaverSuppressor::Unsuppress()
188 {
189  nsresult rv;
190 
191  // Do nothing if not suppressed.
192  if (!mSuppressed)
193  return NS_OK;
194 
195  // Open a D-Bus connection to the Gnome screen saver.
196  nsAutoPtr<sbDBusConnection> dBusConnection;
197  rv = sbDBusConnection::New(getter_Transfers(dBusConnection),
198  DBUS_BUS_SESSION,
199  "org.gnome.ScreenSaver",
200  "/org/gnome/ScreenSaver",
201  "org.gnome.ScreenSaver");
202  NS_ENSURE_SUCCESS(rv, rv);
203 
204  // Uninhibit the screen saver.
205  rv = dBusConnection->InvokeMethod("UnInhibit",
206  NULL,
207  DBUS_TYPE_UINT32,
208  &mInhibitCookie,
209  DBUS_TYPE_INVALID);
210  NS_ENSURE_SUCCESS(rv, rv);
211 
212  // Screen saver is no longer suppressed.
213  mSuppressed = PR_FALSE;
214 
215  return NS_OK;
216 }
217 
static nsresult New(sbDBusConnection **aConnection, DBusBusType aBusType, const char *aDestination, const char *aPath, const char *aInterface)
Definition: sbDBus.cpp:157
return NS_OK
NS_IMPL_ISUPPORTS_INHERITED0(sbScreenSaverSuppressor, sbBaseScreenSaverSuppressor) nsresult sbScreenSaverSuppressor
Songbird D-Bus Service Definitions.