]> git.sur5r.net Git - i3/i3lock/blobdiff - i3lock.c
Only output text when in debug mode (fixes problems with xautolock)
[i3/i3lock] / i3lock.c
index b694f6e884771163839c7640529bfe3cd6c9b826..bda40a0a826c37907ecaa929fc696537263c6cb4 100644 (file)
--- a/i3lock.c
+++ b/i3lock.c
@@ -19,6 +19,7 @@
 #include <assert.h>
 #include <security/pam_appl.h>
 /* FIXME: can we get rid of this header? */
+#include <X11/Xutil.h>
 #include <X11/keysym.h>
 #include <getopt.h>
 #include <string.h>
 #include <cairo/cairo-xcb.h>
 #endif
 
+#include "i3lock.h"
 #include "keysym2ucs.h"
 #include "ucs2_to_utf8.h"
 #include "xcb.h"
 #include "cursors.h"
 #include "unlock_indicator.h"
+#include "xinerama.h"
 
 char color[7] = "ffffff";
 uint32_t last_resolution[2];
@@ -43,30 +46,25 @@ xcb_window_t win;
 static xcb_cursor_t cursor;
 static xcb_key_symbols_t *symbols;
 static pam_handle_t *pam_handle;
-static int input_position = 0;
+int input_position = 0;
 /* Holds the password you enter (in UTF-8). */
 static char password[512];
 static bool modeswitch_active = false;
 static bool iso_level3_shift_active = false;
 static bool iso_level5_shift_active = false;
-static int modeswitchmask;
 static int numlockmask;
+static int shiftlockmask;
+static int capslockmask;
 static bool beep = false;
-static bool debug_mode = false;
+bool debug_mode = false;
 static bool dpms = false;
 bool unlock_indicator = true;
 static bool dont_fork = false;
-static struct ev_loop *main_loop;
+struct ev_loop *main_loop;
 static struct ev_timer *clear_pam_wrong_timeout;
-static struct ev_timer *clear_indicator_timeout;
 extern unlock_state_t unlock_state;
 extern pam_state_t pam_state;
 
-#define DEBUG(fmt, ...) do { \
-    if (debug_mode) \
-        printf("[i3lock-debug] " fmt, ##__VA_ARGS__); \
-} while (0)
-
 #ifndef NOLIBCAIRO
 cairo_surface_t *img = NULL;
 bool tile = false;
@@ -100,36 +98,11 @@ static void clear_pam_wrong(EV_P_ ev_timer *w, int revents) {
     pam_state = STATE_PAM_IDLE;
     unlock_state = STATE_STARTED;
     redraw_screen();
-}
-
-/*
- * Hides the unlock indicator completely when there is no content in the
- * password buffer.
- *
- */
-static void clear_indicator(EV_P_ ev_timer *w, int revents) {
-    if (input_position == 0) {
-        DEBUG("Clear indicator\n");
-        unlock_state = STATE_STARTED;
-    } else unlock_state = STATE_KEY_PRESSED;
-    redraw_screen();
-}
 
-/*
- * (Re-)starts the clear_indicator timeout. Called after pressing backspace or
- * after an unsuccessful authentication attempt.
- *
- */
-static void start_clear_indicator_timeout() {
-    if (clear_indicator_timeout) {
-        ev_timer_stop(main_loop, clear_indicator_timeout);
-        ev_timer_set(clear_indicator_timeout, 1.0, 0.);
-        ev_timer_start(main_loop, clear_indicator_timeout);
-    } else {
-        clear_indicator_timeout = calloc(sizeof(struct ev_timer), 1);
-        ev_timer_init(clear_indicator_timeout, clear_indicator, 1.0, 0.);
-        ev_timer_start(main_loop, clear_indicator_timeout);
-    }
+    /* Now free this timeout. */
+    ev_timer_stop(main_loop, clear_pam_wrong_timeout);
+    free(clear_pam_wrong_timeout);
+    clear_pam_wrong_timeout = NULL;
 }
 
 static void input_done() {
@@ -138,6 +111,7 @@ static void input_done() {
 
     if (clear_pam_wrong_timeout) {
         ev_timer_stop(main_loop, clear_pam_wrong_timeout);
+        free(clear_pam_wrong_timeout);
         clear_pam_wrong_timeout = NULL;
     }
 
@@ -145,12 +119,13 @@ static void input_done() {
     redraw_screen();
 
     if (pam_authenticate(pam_handle, 0) == PAM_SUCCESS) {
-        printf("successfully authenticated\n");
+        DEBUG("successfully authenticated\n");
         clear_password_memory();
         exit(0);
     }
 
-    fprintf(stderr, "Authentication failure\n");
+    if (debug_mode)
+        fprintf(stderr, "Authentication failure\n");
 
     pam_state = STATE_PAM_WRONG;
     redraw_screen();
@@ -158,16 +133,14 @@ static void input_done() {
     /* Clear this state after 2 seconds (unless the user enters another
      * password during that time). */
     ev_now_update(main_loop);
-    clear_pam_wrong_timeout = calloc(sizeof(struct ev_timer), 1);
-    ev_timer_init(clear_pam_wrong_timeout, clear_pam_wrong, 2.0, 0.);
-    ev_timer_start(main_loop, clear_pam_wrong_timeout);
+    if ((clear_pam_wrong_timeout = calloc(sizeof(struct ev_timer), 1))) {
+        ev_timer_init(clear_pam_wrong_timeout, clear_pam_wrong, 2.0, 0.);
+        ev_timer_start(main_loop, clear_pam_wrong_timeout);
+    }
 
     /* Cancel the clear_indicator_timeout, it would hide the unlock indicator
      * too early. */
-    if (clear_indicator_timeout) {
-        ev_timer_stop(main_loop, clear_indicator_timeout);
-        clear_indicator_timeout = NULL;
-    }
+    stop_clear_indicator_timeout();
 
     /* beep on authentication failure, if enabled */
     if (beep) {
@@ -204,6 +177,9 @@ static void handle_key_release(xcb_key_release_event_t *event) {
 
 static void redraw_timeout(EV_P_ ev_timer *w, int revents) {
     redraw_screen();
+
+    ev_timer_stop(main_loop, w);
+    free(w);
 }
 
 /*
@@ -277,6 +253,13 @@ static void handle_key_press(xcb_key_press_event_t *event) {
         input_position = 0;
         clear_password_memory();
         password[input_position] = '\0';
+
+        /* Hide the unlock indicator after a bit if the password buffer is
+         * empty. */
+        start_clear_indicator_timeout();
+        unlock_state = STATE_BACKSPACE_ACTIVE;
+        redraw_screen();
+        unlock_state = STATE_KEY_PRESSED;
         return;
 
     case XK_BackSpace:
@@ -287,26 +270,59 @@ static void handle_key_press(xcb_key_press_event_t *event) {
         u8_dec(password, &input_position);
         password[input_position] = '\0';
 
-        /* Clear this state after 2 seconds (unless the user enters another
-         * password during that time). */
+        /* Hide the unlock indicator after a bit if the password buffer is
+         * empty. */
         start_clear_indicator_timeout();
         unlock_state = STATE_BACKSPACE_ACTIVE;
         redraw_screen();
         unlock_state = STATE_KEY_PRESSED;
-        //printf("new input position = %d, new password = %s\n", input_position, password);
         return;
     }
 
     if ((input_position + 8) >= sizeof(password))
         return;
 
+    /* Whether the user currently holds down the shift key. */
+    bool shift = (event->state & XCB_MOD_MASK_SHIFT);
+
+    /* Whether Caps Lock (all lowercase alphabetic keys will be replaced by
+     * their uppercase variant) is active at the moment. */
+    bool capslock = (event->state & capslockmask);
+
+    /* Whether Shift Lock (shift state is reversed) is active at the moment. */
+    bool shiftlock = (event->state & shiftlockmask);
+
+    /* Whether Caps Lock or Shift Lock is active at the moment. */
+    bool lock = (capslock || shiftlock);
+
+    DEBUG("shift = %d, lock = %d, capslock = %d, shiftlock = %d\n",
+          shift, lock, capslock, shiftlock);
+
     if ((event->state & numlockmask) && xcb_is_keypad_key(sym1)) {
         /* this key was a keypad key */
-        if ((event->state & XCB_MOD_MASK_SHIFT))
+        if (shift || shiftlock)
             sym = sym0;
         else sym = sym1;
     } else {
-        if ((event->state & XCB_MOD_MASK_SHIFT))
+        xcb_keysym_t upper, lower;
+        XConvertCase(sym0, (KeySym*)&lower, (KeySym*)&upper);
+        DEBUG("sym0 = %c (%d), sym1 = %c (%d), lower = %c (%d), upper = %c (%d)\n",
+              sym0, sym0, sym1, sym1, lower, lower, upper, upper);
+        /* If there is no difference between the uppercase and lowercase
+         * variant of this key, we consider Caps Lock off — it is only relevant
+         * for alphabetic keys, unlike Shift Lock. */
+        if (lower == upper) {
+            capslock = false;
+            lock = (capslock || shiftlock);
+            DEBUG("lower == upper, now shift = %d, lock = %d, capslock = %d, shiftlock = %d\n",
+                  shift, lock, capslock, shiftlock);
+        }
+
+        /* In two different cases we need to use the uppercase keysym:
+         * 1) The user holds shift, no lock is active.
+         * 2) Any of the two locks is active.
+         */
+        if ((shift && !lock) || (!shift && lock))
             sym = sym1;
         else sym = sym0;
     }
@@ -330,7 +346,8 @@ static void handle_key_press(xcb_key_press_event_t *event) {
     /* convert the keysym to UCS */
     uint16_t ucs = keysym2ucs(sym);
     if ((int16_t)ucs == -1) {
-        fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
+        if (debug_mode)
+            fprintf(stderr, "Keysym could not be converted to UCS, skipping\n");
         return;
     }
 
@@ -348,13 +365,12 @@ static void handle_key_press(xcb_key_press_event_t *event) {
     unlock_state = STATE_KEY_PRESSED;
 
     struct ev_timer *timeout = calloc(sizeof(struct ev_timer), 1);
-    ev_timer_init(timeout, redraw_timeout, 0.25, 0.);
-    ev_timer_start(main_loop, timeout);
-
-    if (clear_indicator_timeout) {
-        ev_timer_stop(main_loop, clear_indicator_timeout);
-        clear_indicator_timeout = NULL;
+    if (timeout) {
+        ev_timer_init(timeout, redraw_timeout, 0.25, 0.);
+        ev_timer_start(main_loop, timeout);
     }
+
+    stop_clear_indicator_timeout();
 }
 
 /*
@@ -381,7 +397,6 @@ static void handle_visibility_notify(xcb_visibility_notify_event_t *event) {
 static void handle_mapping_notify(xcb_mapping_notify_event_t *event) {
     xcb_refresh_keyboard_mapping(symbols, event);
 
-    modeswitchmask = get_mod_mask(conn, symbols, XK_Mode_switch);
     numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
 }
 
@@ -478,61 +493,55 @@ static void xcb_check_cb(EV_P_ ev_check *w, int revents) {
     while ((event = xcb_poll_for_event(conn)) != NULL) {
         if (event->response_type == 0) {
             xcb_generic_error_t *error = (xcb_generic_error_t*)event;
-            fprintf(stderr, "X11 Error received! sequence 0x%x, error_code = %d\n",
-                    error->sequence, error->error_code);
+            if (debug_mode)
+                fprintf(stderr, "X11 Error received! sequence 0x%x, error_code = %d\n",
+                        error->sequence, error->error_code);
             free(event);
             continue;
         }
 
         /* Strip off the highest bit (set if the event is generated) */
         int type = (event->response_type & 0x7F);
-
-        if (type == XCB_KEY_PRESS) {
-            handle_key_press((xcb_key_press_event_t*)event);
-            continue;
-        }
-
-        if (type == XCB_KEY_RELEASE) {
-            handle_key_release((xcb_key_release_event_t*)event);
-
-            /* If this was the backspace or escape key we are back at an
-             * empty input, so turn off the screen if DPMS is enabled */
-            if (dpms && input_position == 0)
-                dpms_turn_off_screen(conn);
-
-            continue;
-        }
-
-        if (type == XCB_VISIBILITY_NOTIFY) {
-            handle_visibility_notify((xcb_visibility_notify_event_t*)event);
-            continue;
-        }
-
-        if (type == XCB_MAP_NOTIFY) {
-            if (!dont_fork) {
-                /* After the first MapNotify, we never fork again. We don’t
-                 * expect to get another MapNotify, but better be sure… */
-                dont_fork = true;
-
-                /* In the parent process, we exit */
-                if (fork() != 0)
-                    exit(0);
-            }
-            continue;
-        }
-
-        if (type == XCB_MAPPING_NOTIFY) {
-            handle_mapping_notify((xcb_mapping_notify_event_t*)event);
-            continue;
-        }
-
-        if (type == XCB_CONFIGURE_NOTIFY) {
-            handle_screen_resize();
-            continue;
+        switch (type) {
+            case XCB_KEY_PRESS:
+                handle_key_press((xcb_key_press_event_t*)event);
+                break;
+
+            case XCB_KEY_RELEASE:
+                handle_key_release((xcb_key_release_event_t*)event);
+
+                /* If this was the backspace or escape key we are back at an
+                 * empty input, so turn off the screen if DPMS is enabled */
+                if (dpms && input_position == 0)
+                    dpms_turn_off_screen(conn);
+
+                break;
+
+            case XCB_VISIBILITY_NOTIFY:
+                handle_visibility_notify((xcb_visibility_notify_event_t*)event);
+                break;
+
+            case XCB_MAP_NOTIFY:
+                if (!dont_fork) {
+                    /* After the first MapNotify, we never fork again. We don’t
+                     * expect to get another MapNotify, but better be sure… */
+                    dont_fork = true;
+
+                    /* In the parent process, we exit */
+                    if (fork() != 0)
+                        exit(0);
+                }
+                break;
+
+            case XCB_MAPPING_NOTIFY:
+                handle_mapping_notify((xcb_mapping_notify_event_t*)event);
+                break;
+
+            case XCB_CONFIGURE_NOTIFY:
+                handle_screen_resize();
+                break;
         }
 
-        printf("WARNING: unhandled event of type %d\n", type);
-
         free(event);
     }
 }
@@ -652,13 +661,20 @@ int main(int argc, char *argv[]) {
         xcb_connection_has_error(conn))
         errx(EXIT_FAILURE, "Could not connect to X11, maybe you need to set DISPLAY?");
 
+    xinerama_init();
+    xinerama_query_screens();
+
     /* if DPMS is enabled, check if the X server really supports it */
     if (dpms) {
         xcb_dpms_capable_cookie_t dpmsc = xcb_dpms_capable(conn);
         xcb_dpms_capable_reply_t *dpmsr;
-        if ((dpmsr = xcb_dpms_capable_reply(conn, dpmsc, NULL)) && !dpmsr->capable) {
-            fprintf(stderr, "Disabling DPMS, X server not DPMS capable\n");
-            dpms = false;
+        if ((dpmsr = xcb_dpms_capable_reply(conn, dpmsc, NULL))) {
+            if (!dpmsr->capable) {
+                if (debug_mode)
+                    fprintf(stderr, "Disabling DPMS, X server not DPMS capable\n");
+                dpms = false;
+            }
+            free(dpmsr);
         }
     }
 
@@ -687,8 +703,12 @@ int main(int argc, char *argv[]) {
     grab_pointer_and_keyboard(conn, screen, cursor);
 
     symbols = xcb_key_symbols_alloc(conn);
-    modeswitchmask = get_mod_mask(conn, symbols, XK_Mode_switch);
     numlockmask = get_mod_mask(conn, symbols, XK_Num_Lock);
+    shiftlockmask = get_mod_mask(conn, symbols, XK_Shift_Lock);
+    capslockmask = get_mod_mask(conn, symbols, XK_Caps_Lock);
+
+    DEBUG("shift lock mask = %d\n", shiftlockmask);
+    DEBUG("caps lock mask = %d\n", capslockmask);
 
     if (dpms)
         dpms_turn_off_screen(conn);
@@ -711,6 +731,9 @@ int main(int argc, char *argv[]) {
     ev_prepare_init(xcb_prepare, xcb_prepare_cb);
     ev_prepare_start(main_loop, xcb_prepare);
 
-    xcb_flush(conn);
+    /* Invoke the event callback once to catch all the events which were
+     * received up until now. ev will only pick up new events (when the X11
+     * file descriptor becomes readable). */
+    ev_invoke(main_loop, xcb_check, 0);
     ev_loop(main_loop, 0);
 }