diff options
author | Drew DeVault <sir@cmpwn.com> | 2015-11-27 09:18:46 -0500 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2015-11-27 09:18:46 -0500 |
commit | 87e37781253101b8d38be573b29aa7ffb26fe525 (patch) | |
tree | 19484f0d64d43d0117bce23a5127ac8f27068d20 /CONTRIBUTING.md | |
parent | Write down style guidelines (diff) | |
download | sway-87e37781253101b8d38be573b29aa7ffb26fe525.tar.gz sway-87e37781253101b8d38be573b29aa7ffb26fe525.tar.zst sway-87e37781253101b8d38be573b29aa7ffb26fe525.zip |
Update CONTRIBUTING.md
Diffstat (limited to 'CONTRIBUTING.md')
-rw-r--r-- | CONTRIBUTING.md | 255 |
1 files changed, 129 insertions, 126 deletions
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3b394788..3844ecad 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md | |||
@@ -9,7 +9,8 @@ your plans. | |||
9 | 9 | ||
10 | Sway is written in C. The style guidelines is [kernel | 10 | Sway is written in C. The style guidelines is [kernel |
11 | style](https://www.kernel.org/doc/Documentation/CodingStyle), but all braces go | 11 | style](https://www.kernel.org/doc/Documentation/CodingStyle), but all braces go |
12 | on the same line (*"but K&R!" is silly*). Some points to note: | 12 | on the same line (*"but K&R says so!" is a silly to justify something*). Some |
13 | points to note: | ||
13 | 14 | ||
14 | * Do not use typedefs unless you have a good reason | 15 | * Do not use typedefs unless you have a good reason |
15 | * Do not use macros unless you have a *really* good reason | 16 | * Do not use macros unless you have a *really* good reason |
@@ -19,6 +20,8 @@ on the same line (*"but K&R!" is silly*). Some points to note: | |||
19 | * Use logging with reckless abandon | 20 | * Use logging with reckless abandon |
20 | * Always include braces for if/for/while/etc, even for one-liners | 21 | * Always include braces for if/for/while/etc, even for one-liners |
21 | 22 | ||
23 | An example of well formatted code: | ||
24 | |||
22 | ```C | 25 | ```C |
23 | #include <stdio.h> | 26 | #include <stdio.h> |
24 | #include <stdlib.h> | 27 | #include <stdlib.h> |
@@ -26,133 +29,133 @@ on the same line (*"but K&R!" is silly*). Some points to note: | |||
26 | #include "example.h" | 29 | #include "example.h" |
27 | 30 | ||
28 | struct foobar { | 31 | struct foobar { |
29 | char *foo; | 32 | char *foo; |
30 | int bar; | 33 | int bar; |
31 | long baz; | 34 | long baz; |
32 | }; // Do not typedef without a good reason | 35 | }; // Do not typedef without a good reason |
33 | 36 | ||
34 | int main(int argc, const char **argv) { | 37 | int main(int argc, const char **argv) { |
35 | if (argc != 4) { | 38 | if (argc != 4) { |
36 | sway_abort("Do not run this program manually. See man 5 sway and look for output options."); | 39 | sway_abort("Do not run this program manually. See man 5 sway and look for output options."); |
37 | } | 40 | } |
38 | 41 | ||
39 | if (!registry->desktop_shell) { | 42 | if (!registry->desktop_shell) { |
40 | sway_abort("swaybg requires the compositor to support the desktop-shell extension."); | 43 | sway_abort("swaybg requires the compositor to support the desktop-shell extension."); |
41 | } | 44 | } |
42 | 45 | ||
43 | int desired_output = atoi(argv[1]); | 46 | int desired_output = atoi(argv[1]); |
44 | sway_log(L_INFO, "Using output %d of %d", desired_output, registry->outputs->length); | 47 | sway_log(L_INFO, "Using output %d of %d", desired_output, registry->outputs->length); |
45 | int i; | 48 | int i; |
46 | struct output_state *output = registry->outputs->items[desired_output]; | 49 | struct output_state *output = registry->outputs->items[desired_output]; |
47 | struct window *window = window_setup(registry, 100, 100, false); | 50 | struct window *window = window_setup(registry, 100, 100, false); |
48 | if (!window) { | 51 | if (!window) { |
49 | sway_abort("Failed to create surfaces."); | 52 | sway_abort("Failed to create surfaces."); |
50 | } | 53 | } |
51 | window->width = output->width; | 54 | window->width = output->width; |
52 | window->height = output->height; | 55 | window->height = output->height; |
53 | desktop_shell_set_background(registry->desktop_shell, output->output, window->surface); | 56 | desktop_shell_set_background(registry->desktop_shell, output->output, window->surface); |
54 | list_add(surfaces, window); | 57 | list_add(surfaces, window); |
55 | 58 | ||
56 | cairo_surface_t *image = cairo_image_surface_create_from_png(argv[2]); | 59 | cairo_surface_t *image = cairo_image_surface_create_from_png(argv[2]); |
57 | double width = cairo_image_surface_get_width(image); | 60 | double width = cairo_image_surface_get_width(image); |
58 | double height = cairo_image_surface_get_height(image); | 61 | double height = cairo_image_surface_get_height(image); |
59 | 62 | ||
60 | const char *scaling_mode_str = argv[3]; | 63 | const char *scaling_mode_str = argv[3]; |
61 | enum scaling_mode scaling_mode; | 64 | enum scaling_mode scaling_mode; |
62 | if (strcmp(scaling_mode_str, "stretch") == 0) { | 65 | if (strcmp(scaling_mode_str, "stretch") == 0) { |
63 | scaling_mode = SCALING_MODE_STRETCH; | 66 | scaling_mode = SCALING_MODE_STRETCH; |
64 | } else if (strcmp(scaling_mode_str, "fill") == 0) { | 67 | } else if (strcmp(scaling_mode_str, "fill") == 0) { |
65 | scaling_mode = SCALING_MODE_FILL; | 68 | scaling_mode = SCALING_MODE_FILL; |
66 | } else if (strcmp(scaling_mode_str, "fit") == 0) { | 69 | } else if (strcmp(scaling_mode_str, "fit") == 0) { |
67 | scaling_mode = SCALING_MODE_FIT; | 70 | scaling_mode = SCALING_MODE_FIT; |
68 | } else if (strcmp(scaling_mode_str, "center") == 0) { | 71 | } else if (strcmp(scaling_mode_str, "center") == 0) { |
69 | scaling_mode = SCALING_MODE_CENTER; | 72 | scaling_mode = SCALING_MODE_CENTER; |
70 | } else if (strcmp(scaling_mode_str, "tile") == 0) { | 73 | } else if (strcmp(scaling_mode_str, "tile") == 0) { |
71 | scaling_mode = SCALING_MODE_TILE; | 74 | scaling_mode = SCALING_MODE_TILE; |
72 | } else { | 75 | } else { |
73 | sway_abort("Unsupported scaling mode: %s", scaling_mode_str); | 76 | sway_abort("Unsupported scaling mode: %s", scaling_mode_str); |
74 | } | 77 | } |
75 | 78 | ||
76 | for (i = 0; i < surfaces->length; ++i) { | 79 | for (i = 0; i < surfaces->length; ++i) { |
77 | struct window *window = surfaces->items[i]; | 80 | struct window *window = surfaces->items[i]; |
78 | if (window_prerender(window) && window->cairo) { | 81 | if (window_prerender(window) && window->cairo) { |
79 | switch (scaling_mode) { | 82 | switch (scaling_mode) { |
80 | case SCALING_MODE_STRETCH: | 83 | case SCALING_MODE_STRETCH: |
81 | cairo_scale(window->cairo, | 84 | cairo_scale(window->cairo, |
82 | (double) window->width / width, | 85 | (double) window->width / width, |
83 | (double) window->height / height); | 86 | (double) window->height / height); |
84 | cairo_set_source_surface(window->cairo, image, 0, 0); | 87 | cairo_set_source_surface(window->cairo, image, 0, 0); |
85 | break; | 88 | break; |
86 | case SCALING_MODE_FILL: | 89 | case SCALING_MODE_FILL: |
87 | { | 90 | { |
88 | double window_ratio = (double) window->width / window->height; | 91 | double window_ratio = (double) window->width / window->height; |
89 | double bg_ratio = width / height; | 92 | double bg_ratio = width / height; |
90 | 93 | ||
91 | if (window_ratio > bg_ratio) { | 94 | if (window_ratio > bg_ratio) { |
92 | double scale = (double) window->width / width; | 95 | double scale = (double) window->width / width; |
93 | cairo_scale(window->cairo, scale, scale); | 96 | cairo_scale(window->cairo, scale, scale); |
94 | cairo_set_source_surface(window->cairo, image, | 97 | cairo_set_source_surface(window->cairo, image, |
95 | 0, | 98 | 0, |
96 | (double) window->height/2 / scale - height/2); | 99 | (double) window->height/2 / scale - height/2); |
97 | } else { | 100 | } else { |
98 | double scale = (double) window->height / height; | 101 | double scale = (double) window->height / height; |
99 | cairo_scale(window->cairo, scale, scale); | 102 | cairo_scale(window->cairo, scale, scale); |
100 | cairo_set_source_surface(window->cairo, image, | 103 | cairo_set_source_surface(window->cairo, image, |
101 | (double) window->width/2 / scale - width/2, | 104 | (double) window->width/2 / scale - width/2, |
102 | 0); | 105 | 0); |
103 | } | 106 | } |
104 | } | 107 | } |
105 | break; | 108 | break; |
106 | case SCALING_MODE_FIT: | 109 | case SCALING_MODE_FIT: |
107 | { | 110 | { |
108 | double window_ratio = (double) window->width / window->height; | 111 | double window_ratio = (double) window->width / window->height; |
109 | double bg_ratio = width / height; | 112 | double bg_ratio = width / height; |
110 | 113 | ||
111 | if (window_ratio > bg_ratio) { | 114 | if (window_ratio > bg_ratio) { |
112 | double scale = (double) window->height / height; | 115 | double scale = (double) window->height / height; |
113 | cairo_scale(window->cairo, scale, scale); | 116 | cairo_scale(window->cairo, scale, scale); |
114 | cairo_set_source_surface(window->cairo, image, | 117 | cairo_set_source_surface(window->cairo, image, |
115 | (double) window->width/2 / scale - width/2, | 118 | (double) window->width/2 / scale - width/2, |
116 | 0); | 119 | 0); |
117 | } else { | 120 | } else { |
118 | double scale = (double) window->width / width; | 121 | double scale = (double) window->width / width; |
119 | cairo_scale(window->cairo, scale, scale); | 122 | cairo_scale(window->cairo, scale, scale); |
120 | cairo_set_source_surface(window->cairo, image, | 123 | cairo_set_source_surface(window->cairo, image, |
121 | 0, | 124 | 0, |
122 | (double) window->height/2 / scale - height/2); | 125 | (double) window->height/2 / scale - height/2); |
123 | } | 126 | } |
124 | } | 127 | } |
125 | break; | 128 | break; |
126 | case SCALING_MODE_CENTER: | 129 | case SCALING_MODE_CENTER: |
127 | cairo_set_source_surface(window->cairo, image, | 130 | cairo_set_source_surface(window->cairo, image, |
128 | (double) window->width/2 - width/2, | 131 | (double) window->width/2 - width/2, |
129 | (double) window->height/2 - height/2); | 132 | (double) window->height/2 - height/2); |
130 | break; | 133 | break; |
131 | case SCALING_MODE_TILE: | 134 | case SCALING_MODE_TILE: |
132 | { | 135 | { |
133 | cairo_pattern_t *pattern = cairo_pattern_create_for_surface(image); | 136 | cairo_pattern_t *pattern = cairo_pattern_create_for_surface(image); |
134 | cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT); | 137 | cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT); |
135 | cairo_set_source(window->cairo, pattern); | 138 | cairo_set_source(window->cairo, pattern); |
136 | } | 139 | } |
137 | break; | 140 | break; |
138 | default: | 141 | default: |
139 | sway_abort("Scaling mode '%s' not implemented yet!", scaling_mode_str); | 142 | sway_abort("Scaling mode '%s' not implemented yet!", scaling_mode_str); |
140 | } | 143 | } |
141 | 144 | ||
142 | cairo_paint(window->cairo); | 145 | cairo_paint(window->cairo); |
143 | 146 | ||
144 | window_render(window); | 147 | window_render(window); |
145 | } | 148 | } |
146 | } | 149 | } |
147 | 150 | ||
148 | while (wl_display_dispatch(registry->display) != -1); | 151 | while (wl_display_dispatch(registry->display) != -1); |
149 | 152 | ||
150 | for (i = 0; i < surfaces->length; ++i) { | 153 | for (i = 0; i < surfaces->length; ++i) { |
151 | struct window *window = surfaces->items[i]; | 154 | struct window *window = surfaces->items[i]; |
152 | window_teardown(window); | 155 | window_teardown(window); |
153 | } | 156 | } |
154 | list_free(surfaces); | 157 | list_free(surfaces); |
155 | registry_teardown(registry); | 158 | registry_teardown(registry); |
156 | return 0; | 159 | return 0; |
157 | } | 160 | } |
158 | ``` | 161 | ``` |