i3 - improved tiling WM


adding option (-C) to enable centering of an image

Patch status: rejected

Patch by Richard Leitner

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

b/i3lock.1

15
@@ -24,6 +24,7 @@ i3lock \- improved screen locker
16
 .RB [\|\-c
17
 .IR color \|]
18
 .RB [\|\-t\|]
19
+.RB [\|\-C\|]
20
 .RB [\|\-p
21
 .IR pointer\|]
22
 .RB [\|\-u\|]
23
@@ -90,6 +91,12 @@ If an image is specified (via \-i) it will display the image tiled all over the
24
 (if it is a multi-monitor setup, the image is visible on all screens).
25
 
26
 .TP
27
+.B \-C, \-\-centering
28
+If an image is specified (via \-i) it will display the image centered.
29
+(if it is a multi-monitor setup, the image is centered over all screens).
30
+If tiling (-t) and centering (-C) is specified, only tiling will be used.
31
+
32
+.TP
33
 .BI \-p\  win|default \fR,\ \fB\-\-pointer= win|default
34
 If you specify "default",
35
 .B i3lock

b/i3lock.c

40
@@ -68,6 +68,7 @@ static struct xkb_keymap *xkb_keymap;
41
 
42
 cairo_surface_t *img = NULL;
43
 bool tile = false;
44
+bool center = false;
45
 bool ignore_empty_password = false;
46
 
47
 /* isutf, u8_dec © 2005 Jeff Bezanson, public domain */
48
@@ -659,6 +660,7 @@ int main(int argc, char *argv[]) {
49
         {"no-unlock-indicator", no_argument, NULL, 'u'},
50
         {"image", required_argument, NULL, 'i'},
51
         {"tiling", no_argument, NULL, 't'},
52
+        {"centering", no_argument, NULL, 'C'},
53
         {"ignore-empty-password", no_argument, NULL, 'e'},
54
         {NULL, no_argument, NULL, 0}
55
     };
56
@@ -666,7 +668,7 @@ int main(int argc, char *argv[]) {
57
     if ((username = getenv("USER")) == NULL)
58
         errx(1, "USER environment variable not set, please set it.\n");
59
 
60
-    while ((o = getopt_long(argc, argv, "hvnbdc:p:ui:te", longopts, &optind)) != -1) {
61
+    while ((o = getopt_long(argc, argv, "hvnbdc:p:ui:tCe", longopts, &optind)) != -1) {
62
         switch (o) {
63
         case 'v':
64
             errx(EXIT_SUCCESS, "version " VERSION " © 2010-2012 Michael Stapelberg");
65
@@ -700,6 +702,9 @@ int main(int argc, char *argv[]) {
66
         case 't':
67
             tile = true;
68
             break;
69
+        case 'C':
70
+            center = true;
71
+            break;
72
         case 'p':
73
             if (!strcmp(optarg, "win")) {
74
                 curs_choice = CURS_WIN;
75
@@ -718,11 +723,16 @@ int main(int argc, char *argv[]) {
76
             break;
77
         default:
78
             errx(1, "Syntax: i3lock [-v] [-n] [-b] [-d] [-c color] [-u] [-p win|default]"
79
-            " [-i image.png] [-t] [-e]"
80
+            " [-i image.png] [-t|C] [-e]"
81
             );
82
         }
83
     }
84
 
85
+    /* warn if tile and center is set */
86
+    if (tile && center) {
87
+        fprintf(stderr, "You have specified tile (-t) and center (-C), so center will be ignored!\n");
88
+    }
89
+
90
     /* We need (relatively) random numbers for highlighting a random part of
91
      * the unlock indicator upon keypresses. */
92
     srand(time(NULL));

b/unlock_indicator.c

97
@@ -45,6 +45,8 @@ extern cairo_surface_t *img;
98
 
99
 /* Whether the image should be tiled. */
100
 extern bool tile;
101
+/* Whether the image should be centered. */
102
+extern bool center;
103
 /* The background color to use (in hex). */
104
 extern char color[7];
105
 
106
@@ -81,10 +83,7 @@ xcb_pixmap_t draw_image(uint32_t *resolution) {
107
     cairo_t *xcb_ctx = cairo_create(xcb_output);
108
 
109
     if (img) {
110
-        if (!tile) {
111
-            cairo_set_source_surface(xcb_ctx, img, 0, 0);
112
-            cairo_paint(xcb_ctx);
113
-        } else {
114
+        if (tile) {
115
             /* create a pattern and fill a rectangle as big as the screen */
116
             cairo_pattern_t *pattern;
117
             pattern = cairo_pattern_create_for_surface(img);
118
@@ -93,6 +92,17 @@ xcb_pixmap_t draw_image(uint32_t *resolution) {
119
             cairo_rectangle(xcb_ctx, 0, 0, resolution[0], resolution[1]);
120
             cairo_fill(xcb_ctx);
121
             cairo_pattern_destroy(pattern);
122
+        } else if (center) {
123
+            /* draw the image at the center of the screen */
124
+            cairo_set_source_surface(xcb_ctx, img,
125
+                    resolution[0]/2 - (int) cairo_image_surface_get_width(img)/2,
126
+                    resolution[1]/2 - (int) cairo_image_surface_get_height(img)/2
127
+                    );
128
+            cairo_paint(xcb_ctx);
129
+        } else {
130
+            /* draw image "normally" */
131
+            cairo_set_source_surface(xcb_ctx, img, 0, 0);
132
+            cairo_paint(xcb_ctx);
133
         }
134
     } else {
135
         char strgroups[3][3] = {{color[0], color[1], '\0'},