Advertisement
Guest User

fix-upower-backlight.patch

a guest
Dec 16th, 2010
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.97 KB | None | 0 0
  1. Index: daemon/BackendConfig.cmake
  2. ===================================================================
  3. --- daemon/BackendConfig.cmake (revision 1206705)
  4. +++ daemon/BackendConfig.cmake (working copy)
  5. @@ -33,6 +33,13 @@
  6.  
  7. set(powerdevilupowerbackend_LIBS ${X11_LIBRARIES} ${QT_QTGUI_LIBRARY} ${X11_Xrandr_LIB})
  8.  
  9. +## backlight helper executable
  10. +kde4_add_executable(backlighthelper backends/upower/backlighthelper.cpp ${backlighthelper_mocs})
  11. +target_link_libraries(backlighthelper ${KDE4_KDECORE_LIBS})
  12. +install(TARGETS backlighthelper DESTINATION ${LIBEXEC_INSTALL_DIR})
  13. +kde4_install_auth_helper_files(backlighthelper org.kde.powerdevil.backlighthelper root)
  14. +kde4_install_auth_actions(org.kde.powerdevil.backlighthelper ${CMAKE_CURRENT_SOURCE_DIR}/backends/upower/backlight_helper_actions.actions)
  15. +
  16. ########################## HAL Backend #####################################
  17.  
  18. include_directories(${CMAKE_CURRENT_SOURCE_DIR}/backends/hal
  19. Index: daemon/backends/upower/backlighthelper.h
  20. ===================================================================
  21. --- daemon/backends/upower/backlighthelper.h (revision 0)
  22. +++ daemon/backends/upower/backlighthelper.h (revision 0)
  23. @@ -0,0 +1,44 @@
  24. +/* This file is part of the KDE project
  25. + * Copyright (C) 2010 Lukas Tinkl <ltinkl@redhat.com>
  26. + *
  27. + * This library is free software; you can redistribute it and/or
  28. + * modify it under the terms of the GNU Library General Public
  29. + * License version 2 as published by the Free Software Foundation.
  30. + *
  31. + * This library is distributed in the hope that it will be useful,
  32. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  34. + * Library General Public License for more details.
  35. + *
  36. + * You should have received a copy of the GNU Library General Public License
  37. + * along with this library; see the file COPYING.LIB. If not, write to
  38. + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  39. + * Boston, MA 02110-1301, USA.
  40. + *
  41. + */
  42. +
  43. +#ifndef BACKLIGHTHELPER_H
  44. +#define BACKLIGHTHELPER_H
  45. +
  46. +#include <QObject>
  47. +#include <kauth.h>
  48. +
  49. +using namespace KAuth;
  50. +
  51. +class BacklightHelper: public QObject
  52. +{
  53. + Q_OBJECT
  54. +public:
  55. + BacklightHelper(QObject * parent = 0);
  56. +
  57. +public slots:
  58. + ActionReply brightness(const QVariantMap & args);
  59. + ActionReply setbrightness(const QVariantMap & args);
  60. +private:
  61. + void init();
  62. + int maxBrightness() const;
  63. + bool m_isSupported;
  64. + QString m_dirname;
  65. +};
  66. +
  67. +#endif // BACKLIGHTHELPER_H
  68. Index: daemon/backends/upower/backlighthelper.cpp
  69. ===================================================================
  70. --- daemon/backends/upower/backlighthelper.cpp (revision 0)
  71. +++ daemon/backends/upower/backlighthelper.cpp (revision 0)
  72. @@ -0,0 +1,144 @@
  73. +/* This file is part of the KDE project
  74. + * Copyright (C) 2010 Lukas Tinkl <ltinkl@redhat.com>
  75. + *
  76. + * This library is free software; you can redistribute it and/or
  77. + * modify it under the terms of the GNU Library General Public
  78. + * License version 2 as published by the Free Software Foundation.
  79. + *
  80. + * This library is distributed in the hope that it will be useful,
  81. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  82. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  83. + * Library General Public License for more details.
  84. + *
  85. + * You should have received a copy of the GNU Library General Public License
  86. + * along with this library; see the file COPYING.LIB. If not, write to
  87. + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  88. + * Boston, MA 02110-1301, USA.
  89. + *
  90. + */
  91. +
  92. +#include "backlighthelper.h"
  93. +
  94. +#include <QtCore/QDir>
  95. +#include <QtCore/QDebug>
  96. +
  97. +#define PREFIX "/sys/class/backlight/"
  98. +
  99. +BacklightHelper::BacklightHelper(QObject * parent)
  100. + : QObject(parent), m_isSupported(false)
  101. +{
  102. + init();
  103. +}
  104. +
  105. +void BacklightHelper::init()
  106. +{
  107. + // find the first existing device with backlight support
  108. + QStringList interfaces;
  109. + interfaces << "nv_backlight" << "asus_laptop" << "toshiba"
  110. + << "eeepc" << "thinkpad_screen" << "acpi_video1"
  111. + << "mbp_backlight" << "acpi_video0"
  112. + << "fujitsu-laptop" << "sony" << "samsung";
  113. +
  114. + QDir dir;
  115. + foreach (const QString & interface, interfaces) {
  116. + dir.setPath(PREFIX + interface);
  117. + //qDebug() << "searching dir:" << dir;
  118. + if (dir.exists()) {
  119. + m_dirname = dir.path();
  120. + qDebug() << "kernel backlight support found in" << m_dirname;
  121. + break;
  122. + }
  123. + }
  124. +
  125. + if (m_dirname.isEmpty()) {
  126. + qWarning() << "no kernel backlight interface found";
  127. + return;
  128. + }
  129. +
  130. + m_isSupported = true;
  131. +
  132. + //brightness(QVariantMap());
  133. +}
  134. +
  135. +ActionReply BacklightHelper::brightness(const QVariantMap & args)
  136. +{
  137. + Q_UNUSED(args);
  138. +
  139. + ActionReply reply;
  140. +
  141. + if (!m_isSupported) {
  142. + reply = ActionReply::HelperErrorReply;
  143. + return reply;
  144. + }
  145. +
  146. + // current brightness
  147. + QFile file(m_dirname + "/brightness");
  148. + if (!file.open(QIODevice::ReadOnly)) {
  149. + reply = ActionReply::HelperErrorReply;
  150. + reply.setErrorCode(file.error());
  151. + qWarning() << "reading brightness failed with error code " << file.error() << file.errorString();
  152. + return reply;
  153. + }
  154. +
  155. + QTextStream stream(&file);
  156. + int brightness;
  157. + stream >> brightness;
  158. + qDebug() << "brightness:" << brightness;
  159. + file.close();
  160. +
  161. + reply.addData("brightness", brightness * 100 / maxBrightness());
  162. + qDebug() << "data contains:" << reply.data()["brightness"];
  163. +
  164. + return reply;
  165. +}
  166. +
  167. +ActionReply BacklightHelper::setbrightness(const QVariantMap & args)
  168. +{
  169. + ActionReply reply;
  170. +
  171. + if (!m_isSupported) {
  172. + reply = ActionReply::HelperErrorReply;
  173. + return reply;
  174. + }
  175. +
  176. + QFile file(m_dirname + "/brightness");
  177. + if (!file.open(QIODevice::WriteOnly)) {
  178. + reply = ActionReply::HelperErrorReply;
  179. + reply.setErrorCode(file.error());
  180. + qWarning() << "writing brightness failed with error code " << file.error() << file.errorString();
  181. + return reply;
  182. + }
  183. +
  184. + int actual_brightness = args["brightness"].toFloat() * maxBrightness() / 100;
  185. + qDebug() << "setting brightness:" << actual_brightness;
  186. + int result = file.write(QByteArray::number(actual_brightness));
  187. + file.close();
  188. +
  189. + if (result == -1) {
  190. + reply = ActionReply::HelperErrorReply;
  191. + reply.setErrorCode(file.error());
  192. + qWarning() << "writing brightness failed with error code " << file.error() << file.errorString();
  193. + }
  194. +
  195. + return reply;
  196. +}
  197. +
  198. +int BacklightHelper::maxBrightness() const
  199. +{
  200. + // maximum brightness
  201. + QFile file(m_dirname + "/max_brightness");
  202. + if (!file.open(QIODevice::ReadOnly)) {
  203. + qWarning() << "reading max brightness failed with error code " << file.error() << file.errorString();
  204. + return -1; // some non-zero value
  205. + }
  206. +
  207. + QTextStream stream(&file);
  208. + int max_brightness;
  209. + stream >> max_brightness;
  210. + qDebug() << "max brightness:" << max_brightness;
  211. + file.close();
  212. +
  213. + return max_brightness;
  214. +}
  215. +
  216. +KDE4_AUTH_HELPER_MAIN("org.kde.powerdevil.backlighthelper", BacklightHelper)
  217. Index: daemon/backends/upower/powerdevilupowerbackend.cpp
  218. ===================================================================
  219. --- daemon/backends/upower/powerdevilupowerbackend.cpp (revision 1206705)
  220. +++ daemon/backends/upower/powerdevilupowerbackend.cpp (working copy)
  221. @@ -27,10 +27,13 @@
  222.  
  223. #include <KDebug>
  224. #include <KPluginFactory>
  225. +#include <KAuth/Action>
  226.  
  227. #include "xrandrbrightness.h"
  228. #include "upowersuspendjob.h"
  229.  
  230. +#define HELPER_ID "org.kde.powerdevil.backlighthelper"
  231. +
  232. PowerDevilUPowerBackend::PowerDevilUPowerBackend(QObject* parent)
  233. : BackendInterface(parent),
  234. m_brightNessControl(0),
  235. @@ -98,9 +101,7 @@
  236.  
  237. // Brightness Controls available
  238. BrightnessControlsList controls;
  239. - if (m_brightNessControl->isSupported()) {
  240. - controls.insert(QLatin1String("LVDS1"), Screen);
  241. - }
  242. + controls.insert(QLatin1String("LVDS1"), Screen);
  243.  
  244. if (m_kbdBacklight->isValid())
  245. controls.insert(QLatin1String("KBD"), Keyboard);
  246. @@ -177,22 +178,52 @@
  247.  
  248. float PowerDevilUPowerBackend::brightness(PowerDevil::BackendInterface::BrightnessControlType type) const
  249. {
  250. + float result = 0.0;
  251. +
  252. if (type == Screen) {
  253. - kDebug() << "Screen brightness: " << m_brightNessControl->brightness();
  254. - return m_brightNessControl->brightness();
  255. + if (m_brightNessControl->isSupported()) {
  256. + //kDebug() << "Calling xrandr brightness";
  257. + result = m_brightNessControl->brightness();
  258. + } else {
  259. + //kDebug() << "Falling back to helper to get brightness";
  260. + KAuth::Action action("org.kde.powerdevil.backlighthelper.brightness");
  261. + action.setHelperID(HELPER_ID);
  262. + KAuth::ActionReply reply = action.execute();
  263. + if (reply.succeeded()) {
  264. + result = reply.data()["brightness"].toFloat();
  265. + //kDebug() << "org.kde.powerdevil.backlighthelper.brightness succeeded: " << reply.data()["brightness"];
  266. + }
  267. + else
  268. + kWarning() << "org.kde.powerdevil.backlighthelper.brightness failed";
  269. +
  270. + }
  271. + kDebug() << "Screen brightness: " << result;
  272. } else if (type == Keyboard) {
  273. kDebug() << "Kbd backlight brightness: " << m_kbdBacklight->GetBrightness();
  274. - return m_kbdBacklight->GetBrightness() / m_kbdBacklight->GetMaxBrightness() * 100;
  275. + result = m_kbdBacklight->GetBrightness() / m_kbdBacklight->GetMaxBrightness() * 100;
  276. }
  277.  
  278. - return 0.0;
  279. + return result;
  280. }
  281.  
  282. bool PowerDevilUPowerBackend::setBrightness(float brightnessValue, PowerDevil::BackendInterface::BrightnessControlType type)
  283. {
  284. if (type == Screen) {
  285. kDebug() << "set screen brightness: " << brightnessValue;
  286. - m_brightNessControl->setBrightness(brightnessValue);
  287. + if (m_brightNessControl->isSupported()) {
  288. + m_brightNessControl->setBrightness(brightnessValue);
  289. + } else {
  290. + //kDebug() << "Falling back to helper to set brightness";
  291. + KAuth::Action action("org.kde.powerdevil.backlighthelper.setbrightness");
  292. + action.setHelperID(HELPER_ID);
  293. + action.addArgument("brightness", brightnessValue);
  294. + KAuth::ActionReply reply = action.execute();
  295. + if (reply.failed()) {
  296. + kWarning() << "org.kde.powerdevil.backlighthelper.setbrightness failed";
  297. + return false;
  298. + }
  299. + }
  300. +
  301. float newBrightness = brightness(Screen);
  302. if (!qFuzzyCompare(newBrightness, m_cachedBrightness)) {
  303. m_cachedBrightness = newBrightness;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement