{"action":"created","comment":{"url":"https://api.github.com/repos/OpenXT/xctools/pulls/comments/65400522","id":65400522,"diff_hunk":"@@ -0,0 +1,311 @@\n+/*\n+ * screen-module.c\n+ *\n+ * XCPMD module that provides display power management actions.\n+ *\n+ * Copyright (c) 2015 Assured Information Security, Inc.\n+ *\n+ * Author:\n+ * Jennifer Temkin <temkinj@ainfosec.com>\n+ *\n+ * This program is free software; you can redistribute it and/or\n+ * modify it under the terms of the GNU General Public License version 2\n+ * as published by the Free Software Foundation\n+ *\n+ * This program is distributed in the hope that it will be useful,\n+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\n+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+ * GNU General Public License for more details.\n+ *\n+ * You should have received a copy of the GNU General Public License\n+ * along with this program; if not, write to the Free Software\n+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n+ */\n+\n+#include \"project.h\"\n+#include \"xcpmd.h\"\n+#include \"rpcgen/surfman_client.h\"\n+#include \"rules.h\"\n+#include <math.h>\n+\n+//Function prototypes\n+void screen_on(struct arg_node * args);\n+void screen_off(struct arg_node * args);\n+void set_backlight(struct arg_node * args);\n+void increase_backlight(struct arg_node * args);\n+void decrease_backlight(struct arg_node * args);\n+static int get_brightness(void);\n+static void set_brightness(int percentage);\n+\n+\n+//Private data structures\n+struct action_table_row {\n+    char * name;\n+    void (* func)(struct arg_node *);\n+    char * prototype;\n+    char * pretty_prototype;\n+};\n+\n+\n+//Private data\n+static struct action_table_row action_table[] = {\n+    {\"screenOn\"          , screen_on          , \"n\" , \"void\"                    } ,\n+    {\"screenOff\"         , screen_off         , \"n\" , \"void\"                    } ,\n+    {\"setBacklight\"      , set_backlight      , \"i\" , \"int backlight_percent\"   } ,\n+    {\"increaseBacklight\" , increase_backlight , \"i\" , \"int percent_to_increase\" } ,\n+    {\"decreaseBacklight\" , decrease_backlight , \"i\" , \"int percent_to_decrease\" }\n+};\n+\n+static unsigned int num_action_types = sizeof(action_table) / sizeof(action_table[0]);\n+static int times_loaded = 0;\n+\n+\n+//Registers this module's action types.\n+//The constructor attribute causes this function to run at load (dlopen()) time.\n+__attribute__ ((constructor)) static void init_module() {\n+\n+    unsigned int i;\n+\n+    if (times_loaded > 0)\n+        return;\n+\n+    for (i=0; i < num_action_types; ++i) {\n+        add_action_type(action_table[i].name, action_table[i].func, action_table[i].prototype, action_table[i].pretty_prototype);\n+    }\n+}\n+\n+\n+//Cleans up after this module.\n+//The destructor attribute causes this to run at unload (dlclose()) time.\n+__attribute__ ((destructor)) static void uninit_module() {\n+\n+    --times_loaded;\n+\n+    //if (times_loaded > 0)\n+    //    return;\n+\n+    return;\n+}\n+\n+\n+//Actions\n+//Turns all screens on.\n+void screen_on(struct arg_node * args) {\n+    com_citrix_xenclient_surfman_dpms_on_(xcdbus_conn, SURFMAN_SERVICE, SURFMAN_PATH);\n+}\n+\n+\n+//Turns all screens off.\n+void screen_off(struct arg_node * args) {\n+    com_citrix_xenclient_surfman_dpms_off_(xcdbus_conn, SURFMAN_SERVICE, SURFMAN_PATH);\n+}\n+\n+\n+//Sets the backlight to as close to the desired percentage as surfman allows.\n+void set_backlight(struct arg_node * args) {\n+\n+    struct arg_node * node = get_arg(args, 0);\n+    set_brightness(node->arg.i);\n+}\n+\n+\n+//Increases the backlight by as close to the current percentage as surfman allows.\n+void increase_backlight(struct arg_node * args) {\n+\n+    struct arg_node * node = get_arg(args, 0);\n+    unsigned int old_backlight, new_backlight;\n+\n+    old_backlight = get_brightness();\n+    new_backlight = old_backlight + node->arg.i;\n+\n+    if (new_backlight > 100) {\n+        new_backlight = 100;\n+    }\n+    else if (new_backlight < old_backlight) {\n+        new_backlight = old_backlight;\n+    }\n+\n+    set_brightness(new_backlight);\n+}\n+\n+\n+//Increases the backlight by as close to the current percentage as surfman allows.\n+void decrease_backlight(struct arg_node * args) {\n+\n+    struct arg_node * node = get_arg(args, 0);\n+    unsigned int old_backlight;\n+    int new_backlight;\n+\n+    old_backlight = get_brightness();\n+    new_backlight = old_backlight - node->arg.i;\n+\n+    if (new_backlight < 0) {\n+        new_backlight = 0;\n+    }\n+    else if ((unsigned)new_backlight > old_backlight) {\n+        new_backlight = old_backlight;\n+    }\n+\n+    set_brightness((unsigned int)new_backlight);\n+}\n+\n+\n+//Get the current backlight in percent.\n+static int get_brightness(void) {\n+\n+    DIR *sys_dir;\n+    struct dirent * dp;\n+    FILE * file;\n+    char data[128];\n+    char *device_name = NULL, *path = NULL;\n+    int brightness, max_brightness, percent;\n+\n+    //Get the backlight directory.\n+    sys_dir = opendir(BACKLIGHT_PATH);\n+    if (!sys_dir) {\n+        xcpmd_log(LOG_WARNING, \"Couldn't open backlight dir %s - error %d\\n\", BACKLIGHT_PATH, errno);\n+        return 0;\n+    }\n+\n+    //There can be more than one backlight device, and the names of these\n+    //devices may vary from platform to platform. They should all report\n+    //correct values, so just choose the first one.\n+    while ((dp = readdir(sys_dir)) != NULL) {\n+        if (dp->d_type == DT_LNK) {\n+            device_name = clone_string(dp->d_name);","path":"xcpmd/src/screen-module.c","position":175,"original_position":175,"commit_id":"1fe205f4eb108d8248666e52c95ad3522edf388d","original_commit_id":"eb305819e7a873d1cdc7db33f6bcab2250dc8c3d","user":{"login":"jean-edouard","id":7863904,"avatar_url":"https://avatars.githubusercontent.com/u/7863904?v=3","gravatar_id":"","url":"https://api.github.com/users/jean-edouard","html_url":"https://github.com/jean-edouard","followers_url":"https://api.github.com/users/jean-edouard/followers","following_url":"https://api.github.com/users/jean-edouard/following{/other_user}","gists_url":"https://api.github.com/users/jean-edouard/gists{/gist_id}","starred_url":"https://api.github.com/users/jean-edouard/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jean-edouard/subscriptions","organizations_url":"https://api.github.com/users/jean-edouard/orgs","repos_url":"https://api.github.com/users/jean-edouard/repos","events_url":"https://api.github.com/users/jean-edouard/events{/privacy}","received_events_url":"https://api.github.com/users/jean-edouard/received_events","type":"User","site_admin":false},"body":"device_name never free()ed?","created_at":"2016-06-01T16:57:14Z","updated_at":"2016-06-01T16:57:14Z","html_url":"https://github.com/OpenXT/xctools/pull/29#discussion_r65400522","pull_request_url":"https://api.github.com/repos/OpenXT/xctools/pulls/29","_links":{"self":{"href":"https://api.github.com/repos/OpenXT/xctools/pulls/comments/65400522"},"html":{"href":"https://github.com/OpenXT/xctools/pull/29#discussion_r65400522"},"pull_request":{"href":"https://api.github.com/repos/OpenXT/xctools/pulls/29"}}},"pull_request":{"url":"https://api.github.com/repos/OpenXT/xctools/pulls/29","id":72203276,"html_url":"https://github.com/OpenXT/xctools/pull/29","diff_url":"https://github.com/OpenXT/xctools/pull/29.diff","patch_url":"https://github.com/OpenXT/xctools/pull/29.patch","issue_url":"https://api.github.com/repos/OpenXT/xctools/issues/29","number":29,"state":"open","locked":false,"title":"Default policy [OXT-449] and screen brightness module [OXT-450]","user":{"login":"jctemkin","id":2059200,"avatar_url":"https://avatars.githubusercontent.com/u/2059200?v=3","gravatar_id":"","url":"https://api.github.com/users/jctemkin","html_url":"https://github.com/jctemkin","followers_url":"https://api.github.com/users/jctemkin/followers","following_url":"https://api.github.com/users/jctemkin/following{/other_user}","gists_url":"https://api.github.com/users/jctemkin/gists{/gist_id}","starred_url":"https://api.github.com/users/jctemkin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jctemkin/subscriptions","organizations_url":"https://api.github.com/users/jctemkin/orgs","repos_url":"https://api.github.com/users/jctemkin/repos","events_url":"https://api.github.com/users/jctemkin/events{/privacy}","received_events_url":"https://api.github.com/users/jctemkin/received_events","type":"User","site_admin":false},"body":"Plus a bunch of relatively minor bugfixes.\r\n\r\nThe default policy here is to dim the screen when unplugged, dim it further when battery is low, dim even further when battery is critical, and restore to full brightness when plugged in; the \"low\" and \"critical\" thresholds are stored in variables and are easily adjustable. The screen will turn off (DPMS off, not just backlight) when the lid is closed, and turns on when the lid is opened. The previous brightness key behavior has been preserved and moved into the policy file as well.\r\n\r\nThe policy will be read from /usr/share/default.rules only if there is no \"/power-management/rules\" node in the DB; in this case, policy will be loaded from the aforementioned file and written back to the DB. If policy exists in the DB at startup, the DB policy will be loaded instead.\r\n\r\nRequires SELinux policy in [xenclient-oe PR318](https://github.com/OpenXT/xenclient-oe/pull/318).","created_at":"2016-06-01T15:36:58Z","updated_at":"2016-06-01T16:57:15Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c6b747c07dc5d7866a6c13ec275fb760f2fa1bf9","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/OpenXT/xctools/pulls/29/commits","review_comments_url":"https://api.github.com/repos/OpenXT/xctools/pulls/29/comments","review_comment_url":"https://api.github.com/repos/OpenXT/xctools/pulls/comments{/number}","comments_url":"https://api.github.com/repos/OpenXT/xctools/issues/29/comments","statuses_url":"https://api.github.com/repos/OpenXT/xctools/statuses/1fe205f4eb108d8248666e52c95ad3522edf388d","head":{"label":"jctemkin:oxt-default-policy","ref":"oxt-default-policy","sha":"1fe205f4eb108d8248666e52c95ad3522edf388d","user":{"login":"jctemkin","id":2059200,"avatar_url":"https://avatars.githubusercontent.com/u/2059200?v=3","gravatar_id":"","url":"https://api.github.com/users/jctemkin","html_url":"https://github.com/jctemkin","followers_url":"https://api.github.com/users/jctemkin/followers","following_url":"https://api.github.com/users/jctemkin/following{/other_user}","gists_url":"https://api.github.com/users/jctemkin/gists{/gist_id}","starred_url":"https://api.github.com/users/jctemkin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jctemkin/subscriptions","organizations_url":"https://api.github.com/users/jctemkin/orgs","repos_url":"https://api.github.com/users/jctemkin/repos","events_url":"https://api.github.com/users/jctemkin/events{/privacy}","received_events_url":"https://api.github.com/users/jctemkin/received_events","type":"User","site_admin":false},"repo":{"id":40375490,"name":"xctools","full_name":"jctemkin/xctools","owner":{"login":"jctemkin","id":2059200,"avatar_url":"https://avatars.githubusercontent.com/u/2059200?v=3","gravatar_id":"","url":"https://api.github.com/users/jctemkin","html_url":"https://github.com/jctemkin","followers_url":"https://api.github.com/users/jctemkin/followers","following_url":"https://api.github.com/users/jctemkin/following{/other_user}","gists_url":"https://api.github.com/users/jctemkin/gists{/gist_id}","starred_url":"https://api.github.com/users/jctemkin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jctemkin/subscriptions","organizations_url":"https://api.github.com/users/jctemkin/orgs","repos_url":"https://api.github.com/users/jctemkin/repos","events_url":"https://api.github.com/users/jctemkin/events{/privacy}","received_events_url":"https://api.github.com/users/jctemkin/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jctemkin/xctools","description":"","fork":true,"url":"https://api.github.com/repos/jctemkin/xctools","forks_url":"https://api.github.com/repos/jctemkin/xctools/forks","keys_url":"https://api.github.com/repos/jctemkin/xctools/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jctemkin/xctools/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jctemkin/xctools/teams","hooks_url":"https://api.github.com/repos/jctemkin/xctools/hooks","issue_events_url":"https://api.github.com/repos/jctemkin/xctools/issues/events{/number}","events_url":"https://api.github.com/repos/jctemkin/xctools/events","assignees_url":"https://api.github.com/repos/jctemkin/xctools/assignees{/user}","branches_url":"https://api.github.com/repos/jctemkin/xctools/branches{/branch}","tags_url":"https://api.github.com/repos/jctemkin/xctools/tags","blobs_url":"https://api.github.com/repos/jctemkin/xctools/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jctemkin/xctools/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jctemkin/xctools/git/refs{/sha}","trees_url":"https://api.github.com/repos/jctemkin/xctools/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jctemkin/xctools/statuses/{sha}","languages_url":"https://api.github.com/repos/jctemkin/xctools/languages","stargazers_url":"https://api.github.com/repos/jctemkin/xctools/stargazers","contributors_url":"https://api.github.com/repos/jctemkin/xctools/contributors","subscribers_url":"https://api.github.com/repos/jctemkin/xctools/subscribers","subscription_url":"https://api.github.com/repos/jctemkin/xctools/subscription","commits_url":"https://api.github.com/repos/jctemkin/xctools/commits{/sha}","git_commits_url":"https://api.github.com/repos/jctemkin/xctools/git/commits{/sha}","comments_url":"https://api.github.com/repos/jctemkin/xctools/comments{/number}","issue_comment_url":"https://api.github.com/repos/jctemkin/xctools/issues/comments{/number}","contents_url":"https://api.github.com/repos/jctemkin/xctools/contents/{+path}","compare_url":"https://api.github.com/repos/jctemkin/xctools/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jctemkin/xctools/merges","archive_url":"https://api.github.com/repos/jctemkin/xctools/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jctemkin/xctools/downloads","issues_url":"https://api.github.com/repos/jctemkin/xctools/issues{/number}","pulls_url":"https://api.github.com/repos/jctemkin/xctools/pulls{/number}","milestones_url":"https://api.github.com/repos/jctemkin/xctools/milestones{/number}","notifications_url":"https://api.github.com/repos/jctemkin/xctools/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jctemkin/xctools/labels{/name}","releases_url":"https://api.github.com/repos/jctemkin/xctools/releases{/id}","deployments_url":"https://api.github.com/repos/jctemkin/xctools/deployments","created_at":"2015-08-07T18:41:04Z","updated_at":"2016-05-26T20:12:00Z","pushed_at":"2016-06-01T15:34:55Z","git_url":"git://github.com/jctemkin/xctools.git","ssh_url":"git@github.com:jctemkin/xctools.git","clone_url":"https://github.com/jctemkin/xctools.git","svn_url":"https://github.com/jctemkin/xctools","homepage":null,"size":2656,"stargazers_count":0,"watchers_count":0,"language":"C","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"OpenXT:master","ref":"master","sha":"967f45629e3ebac05b523044361c09e74527a332","user":{"login":"OpenXT","id":7283187,"avatar_url":"https://avatars.githubusercontent.com/u/7283187?v=3","gravatar_id":"","url":"https://api.github.com/users/OpenXT","html_url":"https://github.com/OpenXT","followers_url":"https://api.github.com/users/OpenXT/followers","following_url":"https://api.github.com/users/OpenXT/following{/other_user}","gists_url":"https://api.github.com/users/OpenXT/gists{/gist_id}","starred_url":"https://api.github.com/users/OpenXT/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/OpenXT/subscriptions","organizations_url":"https://api.github.com/users/OpenXT/orgs","repos_url":"https://api.github.com/users/OpenXT/repos","events_url":"https://api.github.com/users/OpenXT/events{/privacy}","received_events_url":"https://api.github.com/users/OpenXT/received_events","type":"Organization","site_admin":false},"repo":{"id":20779275,"name":"xctools","full_name":"OpenXT/xctools","owner":{"login":"OpenXT","id":7283187,"avatar_url":"https://avatars.githubusercontent.com/u/7283187?v=3","gravatar_id":"","url":"https://api.github.com/users/OpenXT","html_url":"https://github.com/OpenXT","followers_url":"https://api.github.com/users/OpenXT/followers","following_url":"https://api.github.com/users/OpenXT/following{/other_user}","gists_url":"https://api.github.com/users/OpenXT/gists{/gist_id}","starred_url":"https://api.github.com/users/OpenXT/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/OpenXT/subscriptions","organizations_url":"https://api.github.com/users/OpenXT/orgs","repos_url":"https://api.github.com/users/OpenXT/repos","events_url":"https://api.github.com/users/OpenXT/events{/privacy}","received_events_url":"https://api.github.com/users/OpenXT/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/OpenXT/xctools","description":"","fork":false,"url":"https://api.github.com/repos/OpenXT/xctools","forks_url":"https://api.github.com/repos/OpenXT/xctools/forks","keys_url":"https://api.github.com/repos/OpenXT/xctools/keys{/key_id}","collaborators_url":"https://api.github.com/repos/OpenXT/xctools/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/OpenXT/xctools/teams","hooks_url":"https://api.github.com/repos/OpenXT/xctools/hooks","issue_events_url":"https://api.github.com/repos/OpenXT/xctools/issues/events{/number}","events_url":"https://api.github.com/repos/OpenXT/xctools/events","assignees_url":"https://api.github.com/repos/OpenXT/xctools/assignees{/user}","branches_url":"https://api.github.com/repos/OpenXT/xctools/branches{/branch}","tags_url":"https://api.github.com/repos/OpenXT/xctools/tags","blobs_url":"https://api.github.com/repos/OpenXT/xctools/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/OpenXT/xctools/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/OpenXT/xctools/git/refs{/sha}","trees_url":"https://api.github.com/repos/OpenXT/xctools/git/trees{/sha}","statuses_url":"https://api.github.com/repos/OpenXT/xctools/statuses/{sha}","languages_url":"https://api.github.com/repos/OpenXT/xctools/languages","stargazers_url":"https://api.github.com/repos/OpenXT/xctools/stargazers","contributors_url":"https://api.github.com/repos/OpenXT/xctools/contributors","subscribers_url":"https://api.github.com/repos/OpenXT/xctools/subscribers","subscription_url":"https://api.github.com/repos/OpenXT/xctools/subscription","commits_url":"https://api.github.com/repos/OpenXT/xctools/commits{/sha}","git_commits_url":"https://api.github.com/repos/OpenXT/xctools/git/commits{/sha}","comments_url":"https://api.github.com/repos/OpenXT/xctools/comments{/number}","issue_comment_url":"https://api.github.com/repos/OpenXT/xctools/issues/comments{/number}","contents_url":"https://api.github.com/repos/OpenXT/xctools/contents/{+path}","compare_url":"https://api.github.com/repos/OpenXT/xctools/compare/{base}...{head}","merges_url":"https://api.github.com/repos/OpenXT/xctools/merges","archive_url":"https://api.github.com/repos/OpenXT/xctools/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/OpenXT/xctools/downloads","issues_url":"https://api.github.com/repos/OpenXT/xctools/issues{/number}","pulls_url":"https://api.github.com/repos/OpenXT/xctools/pulls{/number}","milestones_url":"https://api.github.com/repos/OpenXT/xctools/milestones{/number}","notifications_url":"https://api.github.com/repos/OpenXT/xctools/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/OpenXT/xctools/labels{/name}","releases_url":"https://api.github.com/repos/OpenXT/xctools/releases{/id}","deployments_url":"https://api.github.com/repos/OpenXT/xctools/deployments","created_at":"2014-06-12T18:57:34Z","updated_at":"2016-02-16T19:03:45Z","pushed_at":"2016-06-01T15:36:59Z","git_url":"git://github.com/OpenXT/xctools.git","ssh_url":"git@github.com:OpenXT/xctools.git","clone_url":"https://github.com/OpenXT/xctools.git","svn_url":"https://github.com/OpenXT/xctools","homepage":null,"size":2674,"stargazers_count":0,"watchers_count":0,"language":"C","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":8,"mirror_url":null,"open_issues_count":1,"forks":8,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/OpenXT/xctools/pulls/29"},"html":{"href":"https://github.com/OpenXT/xctools/pull/29"},"issue":{"href":"https://api.github.com/repos/OpenXT/xctools/issues/29"},"comments":{"href":"https://api.github.com/repos/OpenXT/xctools/issues/29/comments"},"review_comments":{"href":"https://api.github.com/repos/OpenXT/xctools/pulls/29/comments"},"review_comment":{"href":"https://api.github.com/repos/OpenXT/xctools/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/OpenXT/xctools/pulls/29/commits"},"statuses":{"href":"https://api.github.com/repos/OpenXT/xctools/statuses/1fe205f4eb108d8248666e52c95ad3522edf388d"}}}}
{"actor":{"display_login":"jean-edouard"}}