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