]> git.sur5r.net Git - i3/i3lock/blobdiff - i3lock.c
Document dependency on libpam in README
[i3/i3lock] / i3lock.c
index 2c95929c355f68ca98ea92dcdd83b5df0bb6f9db..d136ad2e46b05089b9ed47cb628bfae3f58fd6a4 100644 (file)
--- a/i3lock.c
+++ b/i3lock.c
@@ -4,6 +4,7 @@
  * i3lock - an improved version of slock
  *
  * i3lock © 2009 Michael Stapelberg and contributors
+ * i3lock © 2009 Jan-Erik Rediger
  * slock  © 2006-2008 Anselm R Garbe
  *
  * See file LICENSE for license information.
 #include <stdarg.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <stdint.h>
 #include <string.h>
+#include <math.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <X11/keysym.h>
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
+#include <X11/xpm.h>
 #include <X11/extensions/dpms.h>
 #include <stdbool.h>
 #include <getopt.h>
+#include <err.h>
 
 #include <security/pam_appl.h>
 
 static char passwd[256];
 
-static void die(const char *errstr, ...) {
-        va_list ap;
+/*
+ * displays an xpm image tiled over the whole screen
+ * (the image will be visible on all screens
+ * when using a multi monitor setup)
+ *
+ */
+static void tile_image(XpmImage *image, int disp_height, int disp_width,
+                       Display *dpy, Pixmap pix, Window w, GC gc)
+{
+        int rows = (int)ceil(disp_height / (float)image->height),
+            cols = (int)ceil(disp_width / (float)image->width);
+
+        for (int y = 0; y < rows; y++) {
+                for (int x = 0; x < cols; x++) {
+                        XCopyArea(dpy, pix, w, gc, 0, 0,
+                                  image->width, image->height,
+                                  image->width * x, image->height * y);
+                }
+        }
+}
+
+/*
+ * Returns the colorpixel to use for the given hex color (think of HTML).
+ *
+ * The hex_color may not start with #, for example FF00FF works.
+ *
+ * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
+ * This has to be done by the caller.
+ *
+ */
+static uint32_t get_colorpixel(char *hex) {
+        char strgroups[3][3] = {{hex[0], hex[1], '\0'},
+                                {hex[2], hex[3], '\0'},
+                                {hex[4], hex[5], '\0'}};
+        uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
+                             (strtol(strgroups[1], NULL, 16)),
+                             (strtol(strgroups[2], NULL, 16))};
+
+        return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2];
+}
 
-        va_start(ap, errstr);
-        vfprintf(stderr, errstr, ap);
-        va_end(ap);
-        exit(EXIT_FAILURE);
+/*
+ * Check if given file can be opened => exists
+ *
+ */
+static bool file_exists(const char *filename)
+{
+        FILE * file = fopen(filename, "r");
+        if(file)
+        {
+                fclose(file);
+                return true;
+        }
+        return false;
 }
 
+/*
+ * Puts the given XPM error code to stderr
+ *
+ */
+static void print_xpm_error(int err)
+{
+        switch (err) {
+                case XpmColorError:
+                        fprintf(stderr, "XPM: Could not parse or alloc requested color\n");
+                        break;
+                case XpmOpenFailed:
+                        fprintf(stderr, "XPM: Cannot open file\n");
+                        break;
+                case XpmFileInvalid:
+                        fprintf(stderr, "XPM: invalid XPM file\n");
+                        break;
+                case XpmNoMemory:
+                        fprintf(stderr, "XPM: Not enough memory\n");
+                        break;
+                case XpmColorFailed:
+                        fprintf(stderr, "XPM: Color not found\n");
+                        break;
+        }
+}
+
+
 /*
  * Callback function for PAM. We only react on password request callbacks.
  *
  */
 static int conv_callback(int num_msg, const struct pam_message **msg,
-                         struct pam_response **resp, void *appdata_ptr) {
+                         struct pam_response **resp, void *appdata_ptr)
+{
         if (num_msg == 0)
                 return 1;
 
@@ -74,7 +153,8 @@ static int conv_callback(int num_msg, const struct pam_message **msg,
         return 0;
 }
 
-int main(int argc, char *argv[]) {
+int main(int argc, char *argv[])
+{
         char curs[] = {0, 0, 0, 0, 0, 0, 0, 0};
         char buf[32];
         char *username;
@@ -87,6 +167,10 @@ int main(int argc, char *argv[]) {
         bool dont_fork = false;
         bool beep = false;
         bool dpms = false;
+        bool xpm_image = false;
+        bool tiling = false;
+        char xpm_image_path[256];
+        char color[7] = "ffffff"; // white
         Cursor invisible;
         Display *dpy;
         KeySym ksym;
@@ -106,13 +190,16 @@ int main(int argc, char *argv[]) {
                 {"nofork", no_argument, NULL, 'n'},
                 {"beep", no_argument, NULL, 'b'},
                 {"dpms", no_argument, NULL, 'd'},
+                {"image", required_argument, NULL, 'i'},
+                {"color", required_argument, NULL, 'c'},
+                {"tiling", no_argument, NULL, 't'},
                 {NULL, no_argument, NULL, 0}
         };
 
-        while ((opt = getopt_long(argc, argv, "vnbd", long_options, &optind)) != -1) {
+        while ((opt = getopt_long(argc, argv, "vnbdi:c:t", long_options, &optind)) != -1) {
                 switch (opt) {
                         case 'v':
-                                die("i3lock-"VERSION", © 2009 Michael Stapelberg\n"
+                                errx(0, "i3lock-"VERSION", © 2009 Michael Stapelberg\n"
                                     "based on slock, which is © 2006-2008 Anselm R Garbe\n");
                         case 'n':
                                 dont_fork = true;
@@ -123,20 +210,39 @@ int main(int argc, char *argv[]) {
                         case 'd':
                                 dpms = true;
                                 break;
+                        case 'i':
+                                strncpy(xpm_image_path, optarg, 255);
+                                xpm_image = true;
+                                break;
+                        case 'c':
+                        {
+                                char *arg = optarg;
+                                /* Skip # if present */
+                                if (arg[0] == '#')
+                                        arg++;
+
+                                if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1)
+                                        errx(1, "color is invalid, color must be given in 6-byte format: rrggbb\n");
+
+                                break;
+                        }
+                        case 't':
+                                tiling = true;
+                                break;
                         default:
-                                die("i3lock: Unknown option. Syntax: i3lock [-v] [-n] [-b] [-d]\n");
+                                errx(1, "i3lock: Unknown option. Syntax: i3lock [-v] [-n] [-b] [-d] [-i image.xpm] [-c color] [-t]\n");
                 }
         }
 
         if ((username = getenv("USER")) == NULL)
-                die("USER environment variable not set, please set it.\n");
+                errx(1, "USER environment variable not set, please set it.\n");
 
         int ret = pam_start("i3lock", username, &conv, &handle);
         if (ret != PAM_SUCCESS)
-                die("PAM: %s\n", pam_strerror(handle, ret));
+                errx(1, "PAM: %s\n", pam_strerror(handle, ret));
 
         if(!(dpy = XOpenDisplay(0)))
-                die("i3lock: cannot open display\n");
+                errx(1, "i3lock: cannot open display\n");
         screen = DefaultScreen(dpy);
         root = RootWindow(dpy, screen);
 
@@ -147,7 +253,7 @@ int main(int argc, char *argv[]) {
 
         /* init */
         wa.override_redirect = 1;
-        wa.background_pixel = WhitePixel(dpy, screen);
+        wa.background_pixel = get_colorpixel(color);
         w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
                         0, DefaultDepth(dpy, screen), CopyFromParent,
                         DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
@@ -156,8 +262,36 @@ int main(int argc, char *argv[]) {
         invisible = XCreatePixmapCursor(dpy, pmap, pmap, &black, &black, 0, 0);
         XDefineCursor(dpy, w, invisible);
         XMapRaised(dpy, w);
+
+        if (xpm_image && file_exists(xpm_image_path)) {
+                GC gc = XDefaultGC(dpy, 0);
+                int depth = DefaultDepth(dpy, screen);
+                int disp_width = DisplayWidth(dpy, screen);
+                int disp_height = DisplayHeight(dpy, screen);
+                Pixmap pix = XCreatePixmap(dpy, w, disp_width, disp_height, depth);
+                XpmImage xpm_image;
+                XpmInfo xpm_info;
+
+                int err = XpmReadFileToXpmImage(xpm_image_path, &xpm_image, &xpm_info);
+                if (err != 0) {
+                        print_xpm_error(err);
+                        return 1;
+                }
+
+                err = XpmCreatePixmapFromXpmImage(dpy, w, &xpm_image, &pix, 0, 0);
+                if (err != 0) {
+                        print_xpm_error(err);
+                        return 1;
+                }
+
+                if (tiling)
+                        tile_image(&xpm_image, disp_height, disp_width, dpy, pix, w, gc);
+                else
+                        XCopyArea(dpy, pix, w, gc, 0, 0, disp_width, disp_height, 0, 0);
+        }
+
         for(len = 1000; len; len--) {
-                if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask | PointerMotionMask,
+                if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask,
                         GrabModeAsync, GrabModeAsync, None, invisible, CurrentTime) == GrabSuccess)
                         break;
                 usleep(1000);