You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

407 lines
14 KiB

  1. /*
  2. * vim:ts=8:expandtab
  3. *
  4. * i3lock - an improved version of slock
  5. *
  6. * i3lock © 2009 Michael Stapelberg and contributors
  7. * i3lock © 2009 Jan-Erik Rediger
  8. * slock © 2006-2008 Anselm R Garbe
  9. *
  10. * See file LICENSE for license information.
  11. *
  12. * Note that on any error (calloc is out of memory for example)
  13. * we do not do anything so that the user can fix the error by
  14. * himself (kill X to get more free memory or stop some other
  15. * program using SSH/console).
  16. *
  17. */
  18. #define _XOPEN_SOURCE 500
  19. #include <ctype.h>
  20. #include <stdarg.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include <math.h>
  26. #include <unistd.h>
  27. #include <sys/types.h>
  28. #include <X11/keysym.h>
  29. #include <X11/Xlib.h>
  30. #include <X11/Xutil.h>
  31. #include <X11/xpm.h>
  32. #include <X11/extensions/dpms.h>
  33. #include <stdbool.h>
  34. #include <getopt.h>
  35. #include <err.h>
  36. #include <security/pam_appl.h>
  37. #include "cursors.h"
  38. static char passwd[256];
  39. /*
  40. * displays an xpm image tiled over the whole screen
  41. * (the image will be visible on all screens
  42. * when using a multi monitor setup)
  43. *
  44. */
  45. static void tile_image(XpmImage *image, int disp_height, int disp_width,
  46. Display *dpy, Pixmap pix, Window w, GC gc)
  47. {
  48. int rows = (int)ceil(disp_height / (float)image->height),
  49. cols = (int)ceil(disp_width / (float)image->width);
  50. for (int y = 0; y < rows; y++) {
  51. for (int x = 0; x < cols; x++) {
  52. XCopyArea(dpy, pix, w, gc, 0, 0,
  53. image->width, image->height,
  54. image->width * x, image->height * y);
  55. }
  56. }
  57. }
  58. /*
  59. * Returns the colorpixel to use for the given hex color (think of HTML).
  60. *
  61. * The hex_color may not start with #, for example FF00FF works.
  62. *
  63. * NOTE that get_colorpixel() does _NOT_ check the given color code for validity.
  64. * This has to be done by the caller.
  65. *
  66. */
  67. static uint32_t get_colorpixel(char *hex) {
  68. char strgroups[3][3] = {{hex[0], hex[1], '\0'},
  69. {hex[2], hex[3], '\0'},
  70. {hex[4], hex[5], '\0'}};
  71. uint32_t rgb16[3] = {(strtol(strgroups[0], NULL, 16)),
  72. (strtol(strgroups[1], NULL, 16)),
  73. (strtol(strgroups[2], NULL, 16))};
  74. return (rgb16[0] << 16) + (rgb16[1] << 8) + rgb16[2];
  75. }
  76. /*
  77. * Check if given file can be opened => exists
  78. *
  79. */
  80. static bool file_exists(const char *filename)
  81. {
  82. FILE * file = fopen(filename, "r");
  83. if(file)
  84. {
  85. fclose(file);
  86. return true;
  87. }
  88. return false;
  89. }
  90. /*
  91. * Puts the given XPM error code to stderr
  92. *
  93. */
  94. static void print_xpm_error(int err)
  95. {
  96. switch (err) {
  97. case XpmColorError:
  98. fprintf(stderr, "XPM: Could not parse or alloc requested color\n");
  99. break;
  100. case XpmOpenFailed:
  101. fprintf(stderr, "XPM: Cannot open file\n");
  102. break;
  103. case XpmFileInvalid:
  104. fprintf(stderr, "XPM: invalid XPM file\n");
  105. break;
  106. case XpmNoMemory:
  107. fprintf(stderr, "XPM: Not enough memory\n");
  108. break;
  109. case XpmColorFailed:
  110. fprintf(stderr, "XPM: Color not found\n");
  111. break;
  112. }
  113. }
  114. /*
  115. * Callback function for PAM. We only react on password request callbacks.
  116. *
  117. */
  118. static int conv_callback(int num_msg, const struct pam_message **msg,
  119. struct pam_response **resp, void *appdata_ptr)
  120. {
  121. if (num_msg == 0)
  122. return 1;
  123. /* PAM expects an arry of responses, one for each message */
  124. if ((*resp = calloc(num_msg, sizeof(struct pam_message))) == NULL) {
  125. perror("calloc");
  126. return 1;
  127. }
  128. for (int c = 0; c < num_msg; c++) {
  129. if (msg[c]->msg_style != PAM_PROMPT_ECHO_OFF &&
  130. msg[c]->msg_style != PAM_PROMPT_ECHO_ON)
  131. continue;
  132. /* return code is currently not used but should be set to zero */
  133. resp[c]->resp_retcode = 0;
  134. if ((resp[c]->resp = strdup(passwd)) == NULL) {
  135. perror("strdup");
  136. return 1;
  137. }
  138. }
  139. return 0;
  140. }
  141. int main(int argc, char *argv[])
  142. {
  143. char buf[32];
  144. char *username;
  145. int num, screen;
  146. unsigned int len;
  147. bool running = true;
  148. /* By default, fork, don’t beep and don’t turn off monitor */
  149. bool dont_fork = false;
  150. bool beep = false;
  151. bool dpms = false;
  152. bool xpm_image = false;
  153. bool tiling = false;
  154. char xpm_image_path[256];
  155. char color[7] = "ffffff"; // white
  156. unsigned char *curs = NULL;
  157. unsigned char *mask = NULL;
  158. unsigned int curs_w, curs_h;
  159. Cursor cursor;
  160. Display *dpy;
  161. KeySym ksym;
  162. Pixmap px_curs;
  163. Pixmap px_mask;
  164. Window root, w;
  165. XColor black, white, dummy;
  166. XEvent ev;
  167. XSetWindowAttributes wa;
  168. pam_handle_t *handle;
  169. struct pam_conv conv = {conv_callback, NULL};
  170. char opt;
  171. int optind = 0;
  172. static struct option long_options[] = {
  173. {"version", no_argument, NULL, 'v'},
  174. {"nofork", no_argument, NULL, 'n'},
  175. {"beep", no_argument, NULL, 'b'},
  176. {"dpms", no_argument, NULL, 'd'},
  177. {"image", required_argument, NULL, 'i'},
  178. {"color", required_argument, NULL, 'c'},
  179. {"tiling", no_argument, NULL, 't'},
  180. {"pointer", required_argument, NULL , 'p'},
  181. {NULL, no_argument, NULL, 0}
  182. };
  183. curs = curs_invisible_bits;
  184. mask = curs_invisible_bits;
  185. curs_w = curs_invisible_width;
  186. curs_h = curs_invisible_height;
  187. while ((opt = getopt_long(argc, argv, "vnbdi:c:tp:", long_options, &optind)) != -1) {
  188. switch (opt) {
  189. case 'v':
  190. errx(0, "i3lock-"VERSION", © 2009 Michael Stapelberg\n"
  191. "based on slock, which is © 2006-2008 Anselm R Garbe\n");
  192. case 'n':
  193. dont_fork = true;
  194. break;
  195. case 'b':
  196. beep = true;
  197. break;
  198. case 'd':
  199. dpms = true;
  200. break;
  201. case 'i':
  202. strncpy(xpm_image_path, optarg, 255);
  203. xpm_image = true;
  204. break;
  205. case 'c':
  206. {
  207. char *arg = optarg;
  208. /* Skip # if present */
  209. if (arg[0] == '#')
  210. arg++;
  211. if (strlen(arg) != 6 || sscanf(arg, "%06[0-9a-fA-F]", color) != 1)
  212. errx(1, "color is invalid, color must be given in 6-byte format: rrggbb\n");
  213. break;
  214. }
  215. case 't':
  216. tiling = true;
  217. break;
  218. case 'p':
  219. if (!strcmp(optarg, "default")) {
  220. curs = NULL;
  221. break;
  222. }
  223. if (!strcmp(optarg, "win")) {
  224. curs = curs_windows_bits;
  225. mask = mask_windows_bits;
  226. curs_w = curs_windows_width;
  227. curs_h = curs_windows_height;
  228. break;
  229. }
  230. break;
  231. default:
  232. errx(1, "i3lock: Unknown option. Syntax: i3lock [-v] [-n] [-b] [-d] [-i image.xpm] [-c color] [-t] [-p win|default]\n");
  233. }
  234. }
  235. if ((username = getenv("USER")) == NULL)
  236. errx(1, "USER environment variable not set, please set it.\n");
  237. int ret = pam_start("i3lock", username, &conv, &handle);
  238. if (ret != PAM_SUCCESS)
  239. errx(1, "PAM: %s\n", pam_strerror(handle, ret));
  240. if(!(dpy = XOpenDisplay(0)))
  241. errx(1, "i3lock: cannot open display\n");
  242. screen = DefaultScreen(dpy);
  243. root = RootWindow(dpy, screen);
  244. if (!dont_fork) {
  245. if (fork() != 0)
  246. return 0;
  247. }
  248. /* init */
  249. wa.override_redirect = 1;
  250. wa.background_pixel = get_colorpixel(color);
  251. w = XCreateWindow(dpy, root, 0, 0, DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
  252. 0, DefaultDepth(dpy, screen), CopyFromParent,
  253. DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixel, &wa);
  254. XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "black", &black, &dummy);
  255. XAllocNamedColor(dpy, DefaultColormap(dpy, screen), "white", &white, &dummy);
  256. if (curs != NULL) {
  257. px_curs = XCreateBitmapFromData(dpy, w, (char*) curs, curs_w, curs_h);
  258. px_mask = XCreateBitmapFromData(dpy, w, (char*) mask, curs_w, curs_h);
  259. cursor = XCreatePixmapCursor(dpy, px_curs, px_mask, &white, &black, 0, 0);
  260. XDefineCursor(dpy, w, cursor);
  261. } else {
  262. px_curs = 0;
  263. px_mask = 0;
  264. cursor = 0;
  265. }
  266. XMapRaised(dpy, w);
  267. if (xpm_image && file_exists(xpm_image_path)) {
  268. GC gc = XDefaultGC(dpy, 0);
  269. int depth = DefaultDepth(dpy, screen);
  270. int disp_width = DisplayWidth(dpy, screen);
  271. int disp_height = DisplayHeight(dpy, screen);
  272. Pixmap pix = XCreatePixmap(dpy, w, disp_width, disp_height, depth);
  273. XpmImage xpm_image;
  274. XpmInfo xpm_info;
  275. int err = XpmReadFileToXpmImage(xpm_image_path, &xpm_image, &xpm_info);
  276. if (err != 0) {
  277. print_xpm_error(err);
  278. return 1;
  279. }
  280. err = XpmCreatePixmapFromXpmImage(dpy, w, &xpm_image, &pix, 0, 0);
  281. if (err != 0) {
  282. print_xpm_error(err);
  283. return 1;
  284. }
  285. if (tiling)
  286. tile_image(&xpm_image, disp_height, disp_width, dpy, pix, w, gc);
  287. else
  288. XCopyArea(dpy, pix, w, gc, 0, 0, disp_width, disp_height, 0, 0);
  289. }
  290. for(len = 1000; len; len--) {
  291. if(XGrabPointer(dpy, root, False, ButtonPressMask | ButtonReleaseMask,
  292. GrabModeAsync, GrabModeAsync, None, cursor, CurrentTime) == GrabSuccess)
  293. break;
  294. usleep(1000);
  295. }
  296. if((running = running && (len > 0))) {
  297. for(len = 1000; len; len--) {
  298. if(XGrabKeyboard(dpy, root, True, GrabModeAsync, GrabModeAsync, CurrentTime)
  299. == GrabSuccess)
  300. break;
  301. usleep(1000);
  302. }
  303. running = (len > 0);
  304. }
  305. len = 0;
  306. XSync(dpy, False);
  307. /* main event loop */
  308. while(running && !XNextEvent(dpy, &ev)) {
  309. if (len == 0 && dpms && DPMSCapable(dpy)) {
  310. DPMSEnable(dpy);
  311. DPMSForceLevel(dpy, DPMSModeOff);
  312. }
  313. if(ev.type != KeyPress)
  314. continue;
  315. buf[0] = 0;
  316. num = XLookupString(&ev.xkey, buf, sizeof buf, &ksym, 0);
  317. if(IsKeypadKey(ksym)) {
  318. if(ksym == XK_KP_Enter)
  319. ksym = XK_Return;
  320. else if(ksym >= XK_KP_0 && ksym <= XK_KP_9)
  321. ksym = (ksym - XK_KP_0) + XK_0;
  322. }
  323. if(IsFunctionKey(ksym) ||
  324. IsKeypadKey(ksym) ||
  325. IsMiscFunctionKey(ksym) ||
  326. IsPFKey(ksym) ||
  327. IsPrivateKeypadKey(ksym))
  328. continue;
  329. switch(ksym) {
  330. case XK_Return:
  331. passwd[len] = 0;
  332. /* Skip empty passwords */
  333. if (len == 0)
  334. continue;
  335. if ((ret = pam_authenticate(handle, 0)) == PAM_SUCCESS)
  336. running = false;
  337. else {
  338. fprintf(stderr, "PAM: %s\n", pam_strerror(handle, ret));
  339. if (beep)
  340. XBell(dpy, 100);
  341. }
  342. len = 0;
  343. break;
  344. case XK_Escape:
  345. len = 0;
  346. break;
  347. case XK_BackSpace:
  348. if (len > 0)
  349. len--;
  350. break;
  351. default:
  352. if(num && !iscntrl((int) buf[0]) && (len + num < sizeof passwd)) {
  353. memcpy(passwd + len, buf, num);
  354. len += num;
  355. }
  356. break;
  357. }
  358. }
  359. XUngrabPointer(dpy, CurrentTime);
  360. if (px_curs != 0) {
  361. XFreePixmap(dpy, px_curs);
  362. XFreePixmap(dpy, px_mask);
  363. }
  364. XDestroyWindow(dpy, w);
  365. XCloseDisplay(dpy);
  366. return 0;
  367. }