i3 - improved tiling WM


restore original DPMS state after succesful screen unlock

Patch status: rejected

Patch by Bill Kolokithas

To apply this patch, use:
curl http://cr.i3wm.org/patch/469/raw.patch | git am

b/i3lock.c

15
@@ -54,6 +54,7 @@ static char password[512];
16
 static bool beep = false;
17
 bool debug_mode = false;
18
 static bool dpms = false;
19
+static bool dpms_original_state = false;
20
 bool unlock_indicator = true;
21
 static bool dont_fork = false;
22
 struct ev_loop *main_loop;
23
@@ -70,6 +71,8 @@ cairo_surface_t *img = NULL;
24
 bool tile = false;
25
 bool ignore_empty_password = false;
26
 
27
+#define DPMS_STATE_IGNORE true
28
+
29
 /* isutf, u8_dec © 2005 Jeff Bezanson, public domain */
30
 #define isutf(c) (((c) & 0xC0) != 0x80)
31
 
32
@@ -83,12 +86,46 @@ void u8_dec(char *s, int *i) {
33
 
34
 static void turn_monitors_on(void) {
35
     if (dpms)
36
-        dpms_set_mode(conn, XCB_DPMS_DPMS_MODE_ON);
37
+        dpms_set_mode(conn, XCB_DPMS_DPMS_MODE_ON, dpms_original_state);
38
 }
39
 
40
 static void turn_monitors_off(void) {
41
     if (dpms)
42
-        dpms_set_mode(conn, XCB_DPMS_DPMS_MODE_OFF);
43
+        dpms_set_mode(conn, XCB_DPMS_DPMS_MODE_OFF, DPMS_STATE_IGNORE);
44
+}
45
+
46
+/*
47
+ * Find out if DPMS capability is supported
48
+ *
49
+ */
50
+static bool dpms_capable(void) {
51
+    bool capable = false;
52
+
53
+    xcb_dpms_capable_cookie_t dpmsc = xcb_dpms_capable(conn);
54
+    xcb_dpms_capable_reply_t *dpmsr;
55
+    if ((dpmsr = xcb_dpms_capable_reply(conn, dpmsc, NULL))) {
56
+        capable = dpmsr->capable;
57
+        free(dpmsr);
58
+        if (!capable && debug_mode)
59
+            fprintf(stderr, "Disabling DPMS, X server not DPMS capable\n");
60
+    }
61
+    return capable;
62
+}
63
+
64
+/*
65
+ * Check the state of DPMS before we alter it, so we can restore it later
66
+ *
67
+ */
68
+static bool dpms_state(void) {
69
+    bool state = false;
70
+
71
+    xcb_dpms_info_cookie_t dpmsc = xcb_dpms_info(conn);
72
+    xcb_dpms_info_reply_t *dpmsr;
73
+    if ((dpmsr = xcb_dpms_info_reply(conn, dpmsc, NULL))) {
74
+        state = dpmsr->state;
75
+        free(dpmsr);
76
+    }
77
+    return state;
78
 }
79
 
80
 /*
81
@@ -760,18 +797,12 @@ int main(int argc, char *argv[]) {
82
     xinerama_init();
83
     xinerama_query_screens();
84
 
85
-    /* if DPMS is enabled, check if the X server really supports it */
86
+    /* if DPMS is enabled, check if the X server really supports it and store the original state */
87
     if (dpms) {
88
-        xcb_dpms_capable_cookie_t dpmsc = xcb_dpms_capable(conn);
89
-        xcb_dpms_capable_reply_t *dpmsr;
90
-        if ((dpmsr = xcb_dpms_capable_reply(conn, dpmsc, NULL))) {
91
-            if (!dpmsr->capable) {
92
-                if (debug_mode)
93
-                    fprintf(stderr, "Disabling DPMS, X server not DPMS capable\n");
94
-                dpms = false;
95
-            }
96
-            free(dpmsr);
97
-        }
98
+        if (dpms_capable())
99
+            dpms_original_state = dpms_state();
100
+        else
101
+            dpms = false;
102
     }
103
 
104
     screen = xcb_setup_roots_iterator(xcb_get_setup(conn)).data;

b/xcb.c

109
@@ -159,9 +159,13 @@ xcb_window_t open_fullscreen_window(xcb_connection_t *conn, xcb_screen_t *scr, c
110
  * Set the dpms level to 'mode'.
111
  *
112
  */
113
-void dpms_set_mode(xcb_connection_t *conn, xcb_dpms_dpms_mode_t mode) {
114
+void dpms_set_mode(xcb_connection_t *conn, xcb_dpms_dpms_mode_t mode, bool dpms_original_state) {
115
     xcb_dpms_enable(conn);
116
     xcb_dpms_force_level(conn, mode);
117
+
118
+    if (dpms_original_state == 0)
119
+        xcb_dpms_disable(conn);
120
+
121
     xcb_flush(conn);
122
 }
123
 

b/xcb.h

128
@@ -3,6 +3,7 @@
129
 
130
 #include <xcb/xcb.h>
131
 #include <xcb/dpms.h>
132
+#include <stdbool.h>
133
 
134
 extern xcb_connection_t *conn;
135
 extern xcb_screen_t *screen;
136
@@ -11,7 +12,7 @@ xcb_visualtype_t *get_root_visual_type(xcb_screen_t *s);
137
 xcb_pixmap_t create_bg_pixmap(xcb_connection_t *conn, xcb_screen_t *scr, u_int32_t *resolution, char *color);
138
 xcb_window_t open_fullscreen_window(xcb_connection_t *conn, xcb_screen_t *scr, char *color, xcb_pixmap_t pixmap);
139
 void grab_pointer_and_keyboard(xcb_connection_t *conn, xcb_screen_t *screen, xcb_cursor_t cursor);
140
-void dpms_set_mode(xcb_connection_t *conn, xcb_dpms_dpms_mode_t mode);
141
+void dpms_set_mode(xcb_connection_t *conn, xcb_dpms_dpms_mode_t mode, bool dpms_original_state);
142
 xcb_cursor_t create_cursor(xcb_connection_t *conn, xcb_screen_t *screen, xcb_window_t win, int choice);
143
 
144
 #endif