Created
September 27, 2013 02:27
-
-
Save jouyouyun/6723410 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (c) 2011 ~ 2012 Deepin, Inc. | |
* 2011 ~ 2012 jouyouyun | |
* | |
* Author: jouyouyun <[email protected]> | |
* Maintainer: jouyouyun <[email protected]> | |
* | |
* This program is free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation; either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program; if not, see <http://www.gnu.org/licenses/>. | |
**/ | |
#include "set_power_plan.h" | |
int main (int argc, char* argv[]) | |
{ | |
init_reset_power (); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (c) 2011 ~ 2012 Deepin, Inc. | |
* 2011 ~ 2012 jouyouyun | |
* | |
* Author: jouyouyun <[email protected]> | |
* Maintainer: jouyouyun <[email protected]> | |
* | |
* This program is free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation; either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program; if not, see <http://www.gnu.org/licenses/>. | |
**/ | |
#define WNCK_I_KNOW_THIS_IS_UNSTABLE | |
#include <libwnck/libwnck.h> | |
#include <string.h> | |
#include "set_power_plan.h" | |
static void on_active_window_changed (WnckScreen* screen, | |
WnckWindow* pre_active_window, gpointer data); | |
static void on_state_changed (WnckWindow* window, | |
WnckWindowState changed_mask, WnckWindowState new_state, | |
gpointer data); | |
static GMainLoop* loop = NULL; | |
static int active_set_power = 0; | |
static int has_set_flag = 0; | |
gchar* user_plan = NULL; | |
void init_reset_power () | |
{ | |
gdk_init (NULL, NULL); | |
loop = g_main_loop_new (NULL, FALSE); | |
WnckScreen* screen = wnck_screen_get_default (); | |
g_signal_connect (screen, "active-window-changed", | |
G_CALLBACK (on_active_window_changed), NULL); | |
g_main_loop_run (loop); | |
g_main_loop_unref (loop); | |
g_free (user_plan); | |
} | |
void finalize_reset_power () | |
{ | |
g_main_loop_quit (loop); | |
} | |
static void on_active_window_changed (WnckScreen* screen, | |
WnckWindow* pre_active_window, gpointer data) | |
{ | |
gulong pid = 0; | |
gboolean is_change = FALSE; | |
gchar* cur_plan = NULL; | |
wnck_screen_force_update (screen); | |
WnckWindow* active_window = wnck_screen_get_active_window (screen); | |
if ( !active_window ) { | |
g_warning ("No Active Window!\n"); | |
return ; | |
} | |
g_print ("Curren Window Name: %s\n", | |
wnck_window_get_name (active_window)); | |
g_print ("Curren Window XID: %lu\n", | |
wnck_window_get_xid (active_window)); | |
g_print ("Curren Window Class: %s\n\n", | |
wnck_window_get_class_group_name (active_window)); | |
g_signal_connect (active_window, "state-changed", | |
G_CALLBACK (on_state_changed), NULL); | |
if ( wnck_window_is_fullscreen (active_window) ) { | |
g_print ("set flag: %d\n", has_set_flag); | |
pid = wnck_window_get_pid (active_window); | |
g_print ("current pid: %lu\n", pid); | |
is_change = power_setting_is_change (pid); | |
if ( is_change ) { | |
active_set_power = 1; | |
cur_plan = get_user_power_plan (); | |
user_plan = g_strdup (cur_plan); | |
g_free (cur_plan); | |
g_print ("Set power plan for high performance!\n\n"); | |
set_power_plan (HIGH_PERFORMANCER); | |
} | |
} else { | |
if ( active_set_power ) { | |
active_set_power = 0; | |
g_print ("**pre state is full screen###!\n"); | |
set_power_plan (user_plan); | |
} | |
} | |
} | |
static void on_state_changed (WnckWindow* window, | |
WnckWindowState changed_mask, WnckWindowState new_state, | |
gpointer data) | |
{ | |
gulong pid = 0; | |
gboolean is_change = FALSE; | |
gchar* cur_plan = NULL; | |
g_print ("\npre state: %d\n", changed_mask); | |
g_print ("cur state: %d\n", new_state); | |
if ( changed_mask == WNCK_WINDOW_STATE_FULLSCREEN ) { | |
g_print ("set flag: %d\n", has_set_flag); | |
pid = wnck_window_get_pid (window); | |
g_print ("current pid: %lu\n", pid); | |
is_change = power_setting_is_change (pid); | |
if ( is_change ) { | |
if ( !has_set_flag ) { | |
has_set_flag = 1; | |
cur_plan = get_user_power_plan (); | |
user_plan = g_strdup (cur_plan); | |
g_free (cur_plan); | |
g_print ("Set power plan for high performance!\n"); | |
set_power_plan (HIGH_PERFORMANCER); | |
} else { | |
has_set_flag = 0; | |
g_print ("**pre state is full screen###!\n"); | |
set_power_plan (user_plan); | |
} | |
} | |
} else { | |
g_print ("Test ********\n"); | |
} | |
return ; | |
} | |
gchar* get_user_power_plan () | |
{ | |
gchar* cur_plan = NULL; | |
GSettings* power_settings = NULL; | |
power_settings = g_settings_new ("org.gnome.settings-daemon.plugins.power"); | |
cur_plan = g_settings_get_string (power_settings, "current-plan"); | |
g_print ("current plan: %s\n", cur_plan); | |
g_object_unref (power_settings); | |
return cur_plan; | |
} | |
int set_power_plan (const gchar* plan) | |
{ | |
gboolean is_ok; | |
GSettings* power_settings = NULL; | |
if ( !plan ) { | |
g_warning ("args error in set power plan!\n"); | |
return -1; | |
} | |
power_settings = g_settings_new ("org.gnome.settings-daemon.plugins.power"); | |
is_ok = g_settings_set_string (power_settings, "current-plan", plan); | |
if ( !is_ok ) { | |
g_warning ("Set power plan failed: %s\n", plan); | |
return -1; | |
} | |
g_settings_sync (); | |
g_object_unref (power_settings); | |
return 0; | |
} | |
gboolean power_setting_is_change (gulong pid) | |
{ | |
int i = 0; | |
int exist_flag = 0; | |
gchar* file_path = NULL; | |
gchar* content = NULL; | |
gsize length; | |
GError* error = NULL; | |
gboolean is_ok; | |
file_path = g_strdup_printf ("/proc/%lu/cmdline", pid); | |
is_ok = g_file_get_contents (file_path, &content, &length, &error); | |
g_free (file_path); | |
if ( !is_ok ) { | |
g_warning ("Get file content failed: %s\n", error->message); | |
g_error_free (error); | |
return FALSE; | |
} | |
g_print ("size: %d\n", length); | |
for (; i < length; i++ ) { | |
if ( memcmp("libflash", (content + i), 8) == 0 ) { | |
g_print ("flash exist!\n"); | |
exist_flag = 1; | |
break; | |
} else if ( memcmp("chrome", (content + i), 6) == 0 ) { | |
g_print ("chrome exist!\n"); | |
exist_flag = 1; | |
break; | |
} | |
} | |
g_free (content); | |
if ( !exist_flag ) { | |
g_warning ("current app not in lists of allow!\n"); | |
return FALSE; | |
} | |
return TRUE; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (c) 2011 ~ 2012 Deepin, Inc. | |
* 2011 ~ 2012 jouyouyun | |
* | |
* Author: jouyouyun <[email protected]> | |
* Maintainer: jouyouyun <[email protected]> | |
* | |
* This program is free software; you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation; either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program; if not, see <http://www.gnu.org/licenses/>. | |
**/ | |
#ifndef __SET_POWER_PLAN_H__ | |
#define __SET_POWER_PLAN_H__ | |
#include <gio/gio.h> | |
#define BALANCE "balance" | |
#define SAVING "saving" | |
#define HIGH_PERFORMANCER "high-performance" | |
void init_reset_power (); | |
void finalize_reset_power (); | |
gchar* get_user_power_plan (); | |
int set_power_plan (const gchar* plan); | |
gboolean power_setting_is_change (gulong pid); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment