summaryrefslogtreecommitdiffstats
path: root/swaylock/main.c
diff options
context:
space:
mode:
authorLibravatar Calvin Lee <cyrus296@gmail.com>2017-02-21 12:49:22 -0700
committerLibravatar Calvin Lee <cyrus296@gmail.com>2017-02-21 14:12:31 -0700
commit34e2c70abcbc9ba496ab32f1f742dd7bae0ce0fe (patch)
tree41068bbf6d412c8501a8040c577321798ff8991c /swaylock/main.c
parentMerge pull request #1075 from zandrmartin/floating-positioning (diff)
downloadsway-34e2c70abcbc9ba496ab32f1f742dd7bae0ce0fe.tar.gz
sway-34e2c70abcbc9ba496ab32f1f742dd7bae0ce0fe.tar.zst
sway-34e2c70abcbc9ba496ab32f1f742dd7bae0ce0fe.zip
Feature for #1078: Configurable swaylock colors
Colors are configured through the command line so that swaylock conforms to the i3lock fork 'github.com/chrjguill/i3lock-color'. Differences from it are that one letter options '-r' and '-s' are not implimentend because '-s' is already used by '--scaling' in swaylock. This commit also fixed whitespace in 'include/swaylock/swaylock.h' and changed `parse_color` in 'common/util.h' so that it can accept colors that do not start with a hash. This was done to keep compatability with the i3lock fork.
Diffstat (limited to 'swaylock/main.c')
-rw-r--r--swaylock/main.c211
1 files changed, 174 insertions, 37 deletions
diff --git a/swaylock/main.c b/swaylock/main.c
index f004a8d5..a8d61d88 100644
--- a/swaylock/main.c
+++ b/swaylock/main.c
@@ -18,9 +18,11 @@
18#include "swaylock/swaylock.h" 18#include "swaylock/swaylock.h"
19#include "ipc-client.h" 19#include "ipc-client.h"
20#include "log.h" 20#include "log.h"
21#include "util.h"
21 22
22struct registry *registry; 23struct registry *registry;
23struct render_data render_data; 24struct render_data render_data;
25struct lock_config *config;
24bool show_indicator = true; 26bool show_indicator = true;
25 27
26void wl_dispatch_events() { 28void wl_dispatch_events() {
@@ -35,7 +37,7 @@ void sigalarm_handler(int sig) {
35 signal(SIGALRM, SIG_IGN); 37 signal(SIGALRM, SIG_IGN);
36 // Hide typing indicator 38 // Hide typing indicator
37 render_data.auth_state = AUTH_STATE_IDLE; 39 render_data.auth_state = AUTH_STATE_IDLE;
38 render(&render_data); 40 render(&render_data, config);
39 wl_display_flush(registry->display); 41 wl_display_flush(registry->display);
40 signal(SIGALRM, sigalarm_handler); 42 signal(SIGALRM, sigalarm_handler);
41} 43}
@@ -55,6 +57,36 @@ void sway_terminate(int exit_code) {
55 57
56char *password; 58char *password;
57int password_size; 59int password_size;
60int line_source = 0;
61
62struct lock_config *init_config() {
63 struct lock_config *config = calloc(1, sizeof(struct lock_config));
64
65 config->font = strdup("sans-serif");
66 config->colors.text = 0x000000FF;
67
68 config->colors.line = 0x000000FF;
69 config->colors.separator = 0x000000FF;
70
71 config->colors.input_cursor = 0x33DB00FF;
72 config->colors.backspace_cursor = 0xDB3300FF;
73
74 config->colors.normal.inner_ring = 0x000000BF;
75 config->colors.normal.outer_ring = 0x337D00FF;
76
77 config->colors.validating.inner_ring = 0x0072FFBF;
78 config->colors.validating.outer_ring = 0x3300FAFF;
79
80 config->colors.invalid.inner_ring = 0xFA0000BF;
81 config->colors.invalid.outer_ring = 0x7D3300FF;
82
83 return config;
84}
85
86void free_config(struct lock_config *config) {
87 free(config->font);
88 free(config);
89}
58 90
59int function_conversation(int num_msg, const struct pam_message **msg, 91int function_conversation(int num_msg, const struct pam_message **msg,
60 struct pam_response **resp, void *appdata_ptr) { 92 struct pam_response **resp, void *appdata_ptr) {
@@ -123,7 +155,7 @@ void notify_key(enum wl_keyboard_key_state state, xkb_keysym_t sym, uint32_t cod
123 case XKB_KEY_Return: 155 case XKB_KEY_Return:
124 render_data.auth_state = AUTH_STATE_VALIDATING; 156 render_data.auth_state = AUTH_STATE_VALIDATING;
125 157
126 render(&render_data); 158 render(&render_data, config);
127 // Make sure our render call will actually be displayed on the screen 159 // Make sure our render call will actually be displayed on the screen
128 wl_dispatch_events(); 160 wl_dispatch_events();
129 161
@@ -207,7 +239,7 @@ void notify_key(enum wl_keyboard_key_state state, xkb_keysym_t sym, uint32_t cod
207 } 239 }
208 } 240 }
209 if (redraw_screen) { 241 if (redraw_screen) {
210 render(&render_data); 242 render(&render_data, config);
211 wl_dispatch_events(); 243 wl_dispatch_events();
212 // Hide the indicator after a couple of seconds 244 // Hide the indicator after a couple of seconds
213 alarm(5); 245 alarm(5);
@@ -315,6 +347,7 @@ int main(int argc, char **argv) {
315 const char *scaling_mode_str = "fit", *socket_path = NULL; 347 const char *scaling_mode_str = "fit", *socket_path = NULL;
316 int i; 348 int i;
317 void *images = NULL; 349 void *images = NULL;
350 config = init_config();
318 351
319 render_data.num_images = 0; 352 render_data.num_images = 0;
320 render_data.color_set = 0; 353 render_data.color_set = 0;
@@ -335,21 +368,47 @@ int main(int argc, char **argv) {
335 {"socket", required_argument, NULL, 'p'}, 368 {"socket", required_argument, NULL, 'p'},
336 {"no-unlock-indicator", no_argument, NULL, 'u'}, 369 {"no-unlock-indicator", no_argument, NULL, 'u'},
337 {"daemonize", no_argument, NULL, 'f'}, 370 {"daemonize", no_argument, NULL, 'f'},
371 {"font", required_argument, NULL, 0},
372 {"line-uses-ring", no_argument, NULL, 0},
373 {"line-uses-inside", no_argument, NULL, 0},
374 {"textcolor", required_argument, NULL, 0},
375 {"insidevercolor", required_argument, NULL, 0},
376 {"insidewrongcolor", required_argument, NULL, 0},
377 {"insidecolor", required_argument, NULL, 0},
378 {"ringvercolor", required_argument, NULL, 0},
379 {"ringwrongcolor", required_argument, NULL, 0},
380 {"ringcolor", required_argument, NULL, 0},
381 {"linecolor", required_argument, NULL, 0},
382 {"separatorcolor", required_argument, NULL, 0},
383 {"keyhlcolor", required_argument, NULL, 0},
384 {"bshlcolor", required_argument, NULL, 0},
338 {0, 0, 0, 0} 385 {0, 0, 0, 0}
339 }; 386 };
340 387
341 const char *usage = 388 const char *usage =
342 "Usage: swaylock [options...]\n" 389 "Usage: swaylock [options...]\n"
343 "\n" 390 "\n"
344 " -h, --help Show help message and quit.\n" 391 " -h, --help Show help message and quit.\n"
345 " -c, --color <rrggbb[aa]> Turn the screen into the given color instead of white.\n" 392 " -c, --color <rrggbb[aa]> Turn the screen into the given color instead of white.\n"
346 " -s, --scaling Scaling mode: stretch, fill, fit, center, tile.\n" 393 " -s, --scaling Scaling mode: stretch, fill, fit, center, tile.\n"
347 " -t, --tiling Same as --scaling=tile.\n" 394 " -t, --tiling Same as --scaling=tile.\n"
348 " -v, --version Show the version number and quit.\n" 395 " -v, --version Show the version number and quit.\n"
349 " -i, --image [<output>:]<path> Display the given image.\n" 396 " -i, --image [<output>:]<path> Display the given image.\n"
350 " -u, --no-unlock-indicator Disable the unlock indicator.\n" 397 " -u, --no-unlock-indicator Disable the unlock indicator.\n"
351 " -f, --daemonize Detach from the controlling terminal.\n" 398 " -f, --daemonize Detach from the controlling terminal.\n"
352 " --socket <socket> Use the specified socket.\n"; 399 " --socket <socket> Use the specified socket.\n"
400 " --font <font> Use the specified font instead of sans-serif.\n"
401 " --textcolor <rrggbb[aa]> Sets the color of the text.\n"
402 " --insidevercolor <rrggbb[aa]> Sets the color of the verifying indicator circle.\n"
403 " --insidewrongcolor <rrggbb[aa]> Sets the color of the invalid indicator circle.\n"
404 " --insidecolor <rrggbb[aa]> Sets the color of the typing or idle indicator circle.\n"
405 " --ringvercolor <rrggbb[aa]> Sets the color of the verifying indicator ring.\n"
406 " --ringwrongcolor <rrggbb[aa]> Sets the color of the invalid indicator ring.\n"
407 " --ringcolor <rrggbb[aa]> Sets the color of the typing or idle indicator ring.\n"
408 " --linecolor <rrggbb[aa]> Sets the color of the line that separates the indicator.\n"
409 " --separatorcolor <rrggbb[aa]> Sets the color of the line that separates highlight segments.\n"
410 " --keyhlcolor <rrggbb[aa]> Sets the color of keypress highlight segments.\n"
411 " --bshlcolor <rrggbb[aa]> Sets the color of keypress highlight segments.\n";
353 412
354 413
355 registry = registry_poll(); 414 registry = registry_poll();
@@ -364,18 +423,8 @@ int main(int argc, char **argv) {
364 switch (c) { 423 switch (c) {
365 case 'c': 424 case 'c':
366 { 425 {
367 int colorlen = strlen(optarg); 426 render_data.color = parse_color(optarg);
368 if (colorlen < 6 || colorlen == 7 || colorlen > 8) {
369 sway_log(L_ERROR, "color must be specified in 3 or 4 byte format, i.e. rrggbb or rrggbbaa");
370 exit(EXIT_FAILURE);
371 }
372 render_data.color = strtol(optarg, NULL, 16);
373 render_data.color_set = 1; 427 render_data.color_set = 1;
374
375 if (colorlen == 6) {
376 render_data.color <<= 8;
377 render_data.color |= 0xFF;
378 }
379 break; 428 break;
380 } 429 }
381 case 'i': 430 case 'i':
@@ -431,6 +480,59 @@ int main(int argc, char **argv) {
431 exit(EXIT_FAILURE); 480 exit(EXIT_FAILURE);
432 } 481 }
433 break; 482 break;
483 case 0:
484 if (strcmp(long_options[option_index].name, "font") == 0) {
485 free(config->font);
486 config->font = strdup(optarg);
487 }
488 else if (strcmp(long_options[option_index].name, "line-uses-ring") == 0) {
489 if (line_source != 0) {
490 sway_log(L_ERROR, "Line source options conflict");
491 exit(EXIT_FAILURE);
492 }
493 line_source = 1;
494 }
495 else if (strcmp(long_options[option_index].name, "line-uses-inside") == 0) {
496 if (line_source != 0) {
497 sway_log(L_ERROR, "Line source options conflict");
498 exit(EXIT_FAILURE);
499 }
500 line_source = 2;
501 }
502 else if (strcmp(long_options[option_index].name, "textcolor") == 0) {
503 config->colors.text = parse_color(optarg);
504 }
505 else if (strcmp(long_options[option_index].name, "insidevercolor") == 0) {
506 config->colors.validating.inner_ring = parse_color(optarg);
507 }
508 else if (strcmp(long_options[option_index].name, "insidewrongcolor") == 0) {
509 config->colors.invalid.inner_ring = parse_color(optarg);
510 }
511 else if (strcmp(long_options[option_index].name, "insidecolor") == 0) {
512 config->colors.normal.inner_ring = parse_color(optarg);
513 }
514 else if (strcmp(long_options[option_index].name, "ringvercolor") == 0) {
515 config->colors.validating.outer_ring = parse_color(optarg);
516 }
517 else if (strcmp(long_options[option_index].name, "ringwrongcolor") == 0) {
518 config->colors.invalid.outer_ring = parse_color(optarg);
519 }
520 else if (strcmp(long_options[option_index].name, "ringcolor") == 0) {
521 config->colors.normal.outer_ring = parse_color(optarg);
522 }
523 else if (strcmp(long_options[option_index].name, "linecolor") == 0) {
524 config->colors.line = parse_color(optarg);
525 }
526 else if (strcmp(long_options[option_index].name, "separatorcolor") == 0) {
527 config->colors.separator = parse_color(optarg);
528 }
529 else if (strcmp(long_options[option_index].name, "keyhlcolor") == 0) {
530 config->colors.input_cursor = parse_color(optarg);
531 }
532 else if (strcmp(long_options[option_index].name, "bshlcolor") == 0) {
533 config->colors.backspace_cursor = parse_color(optarg);
534 }
535 break;
434 default: 536 default:
435 fprintf(stderr, "%s", usage); 537 fprintf(stderr, "%s", usage);
436 exit(EXIT_FAILURE); 538 exit(EXIT_FAILURE);
@@ -524,7 +626,7 @@ int main(int argc, char **argv) {
524 free(displays_paths); 626 free(displays_paths);
525 } 627 }
526 628
527 render(&render_data); 629 render(&render_data, config);
528 bool locked = false; 630 bool locked = false;
529 while (wl_display_dispatch(registry->display) != -1) { 631 while (wl_display_dispatch(registry->display) != -1) {
530 if (!locked) { 632 if (!locked) {
@@ -556,10 +658,12 @@ int main(int argc, char **argv) {
556 list_free(render_data.surfaces); 658 list_free(render_data.surfaces);
557 registry_teardown(registry); 659 registry_teardown(registry);
558 660
661 free_config(config);
662
559 return 0; 663 return 0;
560} 664}
561 665
562void render(struct render_data *render_data) { 666void render(struct render_data *render_data, struct lock_config *config) {
563 int i; 667 int i;
564 for (i = 0; i < render_data->surfaces->length; ++i) { 668 for (i = 0; i < render_data->surfaces->length; ++i) {
565 sway_log(L_DEBUG, "Render surface %d of %d", i, render_data->surfaces->length); 669 sway_log(L_DEBUG, "Render surface %d of %d", i, render_data->surfaces->length);
@@ -609,21 +713,21 @@ void render(struct render_data *render_data) {
609 switch (render_data->auth_state) { 713 switch (render_data->auth_state) {
610 case AUTH_STATE_INPUT: 714 case AUTH_STATE_INPUT:
611 case AUTH_STATE_BACKSPACE: { 715 case AUTH_STATE_BACKSPACE: {
612 cairo_set_source_rgba(window->cairo, 0, 0, 0, 0.75); 716 cairo_set_source_u32(window->cairo, config->colors.normal.inner_ring);
613 cairo_fill_preserve(window->cairo); 717 cairo_fill_preserve(window->cairo);
614 cairo_set_source_rgb(window->cairo, 51.0 / 255, 125.0 / 255, 0); 718 cairo_set_source_u32(window->cairo, config->colors.normal.outer_ring);
615 cairo_stroke(window->cairo); 719 cairo_stroke(window->cairo);
616 } break; 720 } break;
617 case AUTH_STATE_VALIDATING: { 721 case AUTH_STATE_VALIDATING: {
618 cairo_set_source_rgba(window->cairo, 0, 114.0 / 255, 255.0 / 255, 0.75); 722 cairo_set_source_u32(window->cairo, config->colors.validating.inner_ring);
619 cairo_fill_preserve(window->cairo); 723 cairo_fill_preserve(window->cairo);
620 cairo_set_source_rgb(window->cairo, 51.0 / 255, 0, 250.0 / 255); 724 cairo_set_source_u32(window->cairo, config->colors.validating.outer_ring);
621 cairo_stroke(window->cairo); 725 cairo_stroke(window->cairo);
622 } break; 726 } break;
623 case AUTH_STATE_INVALID: { 727 case AUTH_STATE_INVALID: {
624 cairo_set_source_rgba(window->cairo, 250.0 / 255, 0, 0, 0.75); 728 cairo_set_source_u32(window->cairo, config->colors.invalid.inner_ring);
625 cairo_fill_preserve(window->cairo); 729 cairo_fill_preserve(window->cairo);
626 cairo_set_source_rgb(window->cairo, 125.0 / 255, 51.0 / 255, 0); 730 cairo_set_source_u32(window->cairo, config->colors.invalid.outer_ring);
627 cairo_stroke(window->cairo); 731 cairo_stroke(window->cairo);
628 } break; 732 } break;
629 default: break; 733 default: break;
@@ -631,8 +735,8 @@ void render(struct render_data *render_data) {
631 735
632 // Draw a message 736 // Draw a message
633 char *text = NULL; 737 char *text = NULL;
634 cairo_set_source_rgb(window->cairo, 0, 0, 0); 738 cairo_set_source_u32(window->cairo, config->colors.text);
635 cairo_select_font_face(window->cairo, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); 739 cairo_select_font_face(window->cairo, config->font, CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
636 cairo_set_font_size(window->cairo, ARC_RADIUS/3.0f); 740 cairo_set_font_size(window->cairo, ARC_RADIUS/3.0f);
637 switch (render_data->auth_state) { 741 switch (render_data->auth_state) {
638 case AUTH_STATE_VALIDATING: 742 case AUTH_STATE_VALIDATING:
@@ -664,14 +768,14 @@ void render(struct render_data *render_data) {
664 highlight_start += (rand() % (int)(M_PI * 100)) / 100.0 + M_PI * 0.5; 768 highlight_start += (rand() % (int)(M_PI * 100)) / 100.0 + M_PI * 0.5;
665 cairo_arc(window->cairo, wwidth/2, wheight/2, ARC_RADIUS, highlight_start, highlight_start + TYPE_INDICATOR_RANGE); 769 cairo_arc(window->cairo, wwidth/2, wheight/2, ARC_RADIUS, highlight_start, highlight_start + TYPE_INDICATOR_RANGE);
666 if (render_data->auth_state == AUTH_STATE_INPUT) { 770 if (render_data->auth_state == AUTH_STATE_INPUT) {
667 cairo_set_source_rgb(window->cairo, 51.0 / 255, 219.0 / 255, 0); 771 cairo_set_source_u32(window->cairo, config->colors.input_cursor);
668 } else { 772 } else {
669 cairo_set_source_rgb(window->cairo, 219.0 / 255, 51.0 / 255, 0); 773 cairo_set_source_u32(window->cairo, config->colors.backspace_cursor);
670 } 774 }
671 cairo_stroke(window->cairo); 775 cairo_stroke(window->cairo);
672 776
673 // Draw borders 777 // Draw borders
674 cairo_set_source_rgb(window->cairo, 0, 0, 0); 778 cairo_set_source_u32(window->cairo, config->colors.separator);
675 cairo_arc(window->cairo, wwidth/2, wheight/2, ARC_RADIUS, highlight_start, highlight_start + TYPE_INDICATOR_BORDER_THICKNESS); 779 cairo_arc(window->cairo, wwidth/2, wheight/2, ARC_RADIUS, highlight_start, highlight_start + TYPE_INDICATOR_BORDER_THICKNESS);
676 cairo_stroke(window->cairo); 780 cairo_stroke(window->cairo);
677 781
@@ -679,8 +783,41 @@ void render(struct render_data *render_data) {
679 cairo_stroke(window->cairo); 783 cairo_stroke(window->cairo);
680 } 784 }
681 785
786 if (line_source == 1) {
787 switch(render_data->auth_state) {
788 case AUTH_STATE_VALIDATING: {
789 cairo_set_source_u32(window->cairo, config->colors.validating.outer_ring);
790 break;
791 }
792 case AUTH_STATE_INVALID: {
793 cairo_set_source_u32(window->cairo, config->colors.invalid.outer_ring);
794 break;
795 }
796 default: {
797 cairo_set_source_u32(window->cairo, config->colors.normal.outer_ring);
798 }
799 }
800 }
801 else if (line_source == 2) {
802 switch(render_data->auth_state) {
803 case AUTH_STATE_VALIDATING: {
804 cairo_set_source_u32(window->cairo, config->colors.validating.inner_ring);
805 break;
806 }
807 case AUTH_STATE_INVALID: {
808 cairo_set_source_u32(window->cairo, config->colors.invalid.inner_ring);
809 break;
810 }
811 default: {
812 cairo_set_source_u32(window->cairo, config->colors.normal.inner_ring);
813 break;
814 }
815 }
816 }
817 else {
818 cairo_set_source_u32(window->cairo, config->colors.line);
819 }
682 // Draw inner + outer border of the circle 820 // Draw inner + outer border of the circle
683 cairo_set_source_rgb(window->cairo, 0, 0, 0);
684 cairo_set_line_width(window->cairo, 2.0); 821 cairo_set_line_width(window->cairo, 2.0);
685 cairo_arc(window->cairo, wwidth/2, wheight/2, ARC_RADIUS - ARC_THICKNESS/2, 0, 2*M_PI); 822 cairo_arc(window->cairo, wwidth/2, wheight/2, ARC_RADIUS - ARC_THICKNESS/2, 0, 2*M_PI);
686 cairo_stroke(window->cairo); 823 cairo_stroke(window->cairo);