File: HACKING.md

package info (click to toggle)
goodvibes 0.8.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,784 kB
  • sloc: ansic: 16,310; sh: 978; python: 21; makefile: 19
file content (409 lines) | stat: -rw-r--r-- 13,780 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
HACKING
=======

Here's the place to get started when hacking Goodvibes.



Table of Contents
-----------------

1. [Download](#download)
2. [Compilation](#compilation)
3. [Program Invocation](#program-invocation)
4. [Additional Tools](#additional-tools)
5. [Code Overview](#code-overview)
6. [How To Code That](#how-to-code-that)
7. [Coding Style](#coding-style)
8. [Contribution](#contribution)



Download
--------

To get the latest source, clone the repository from GitLab.

    git clone https://gitlab.com/goodvibes/goodvibes.git

Additional repositories are available:

    # Unofficial Debian packaging
    git clone https://gitlab.com/goodvibes/goodvibes-debian.git
    # Flatpak packaging
    git clone https://github.com/flathub/io.gitlab.Goodvibes.git



Compilation
-----------

Goodvibes is built using [Meson][] and [Ninja][]. The build commands are:

    meson setup build
    cd build
    ninja

(from now on, we assume that `PWD=<goodvibes-dir>/build`)

In order to install in a staging directory:

    DESTDIR=../staging ninja install

Goodvibes build is quite modular. Features that require an external library can
be disabled at build time. To see all the options available, enter the build
directory and run:

    meson configure

For example, to disable the hotkeys feature:

    meson configure -Dfeat-hotkeys=false

To disable compilation of the user interface (also disable every ui-related
features):

    meson configure -Dui=false

Additionally, it's possible to run a few tests:

    meson test      # -v for details

You might as well want to generate tag files for your favorite editor:

    ninja etags     # for emacs
    ninja ctags     # for vim

[meson]: http://mesonbuild.com
[ninja]: https://ninja-build.org/



Program Invocation
------------------

#### From the source tree (no installation)

If you want to run Goodvibes from the source tree, without installing it, use
the launcher script.

    ./goodvibes-launcher.sh

The script assumes that the build directory is named `build`.

Why do you need a script? Some libraries expect shared resources to be
installed in some standard locations. To be more accurate:

- GLib expects schemas to be installed at `/usr/share/glib-2.0/schemas/` or
  similar.
- GTK expects icons to be installed at `/usr/share/icons` or similar.

It's possible to customize this behavior with environment variables though, and
that's what the launcher script does. Have a look at it for details.

Also, don't expect any kind of desktop integration to work when you run from
the source tree. Ie, the desktop won't find any desktop file installed in the
standard location (`/usr/share/applications`), and various things might not
work: notifications, icon display here or there, etc.

#### From a non-standard directory

If for some reason you install Goodvibes in a non-standard directory (`/opt` or
`/home/user` or whatever), you will be hit by the same problems mentioned
above: the shared resources won't be found at run-time. So please refer to the
launcher script to see which environment variables you need to set before
running Goodvibes.

#### Command-line options

To get a brief overview of the command-line options available, use `--help`.

The option you will use the most is `-l` to change the log level. Here is a
typical line:

    goodvibes -l dbg

Colors are enabled by default, but you can disable it with `-c` if it hurts
your eyes. Colors are automatically disabled when logs are redirected to a
file.

Logs are all sent to `stderr`, regardless of the log level.

Internally, we use GLib to ouput log messages. For more details, refer to
[GLib Message Output and Debugging Functions][].

Some of the libraries we use provide additional command-line options. To see
them all, invoke with `--help-all`. For more details, refer to:

- [Running GLib Applications][]
- [Running GStreamer Applications][]
- [Running GTK Applications][]

Advanced GTK debugging can be done with the [GtkInspector][]:

    goodvibes --gtk-debug=interactive

[glib message output and debugging functions]: https://developer.gnome.org/glib/stable/glib-Message-Logging.html
[running glib applications]: https://developer.gnome.org/glib/stable/glib-running.html
[running gstreamer applications]: https://gstreamer.freedesktop.org/documentation/gstreamer/running.html
[running gtk applications]: https://developer.gnome.org/gtk3/stable/gtk-running.html
[gtkinspector]: https://wiki.gnome.org/Projects/GTK/Inspector

#### Change the language

For testing, it can be convenient to change the language. For that, use the
`LANGUAGE` environment variable.

    LANGUAGE=fr goodvibes

Note that gettext will look for the message files in the path given to
`bindtextdomain(3)`, so make sure that this path contains the message files.
(hint: this path is logged when Goodvibes starts).



Additional Tools
----------------

[GSettings][] is part of GLib, and is the component in charge of handling the
application settings. It comes with a command-line tool named - as you can
guess - `gsettings`. This is useful to witness Goodvibes reading and writing its
configuration, and also to get or set some values.

    gsettings monitor io.gitlab.Goodvibes.Core
    gsettings list-recursively io.gitlab.Goodvibes.Core

[DConf][] is the backend for GSettings. It's possible to play directly with the
`dconf` command, therefore by-passing completely GSettings.

    dconf watch /io/gitlab/Goodvibes/
    dconf reset -f /io/gitlab/Goodvibes/

[GStreamer tools][] come in handy as soon as a radio station doesn't work. It's
often a good starting point to try to play a stream with `gst-launch-1.0`, and
see what comes out.

    gst-launch-1.0 playbin3 uri=https://direct.fipradio.fr/live/fip-midfi.mp3

[Valgrind][] can be used to check for memory leaks, however using it is not
exactly easy, so there's a wrapper script. It requires a number of so-called
"suppression files" that are not necessarily distributed by libraries (eg.
GStreamer), so you'll need to get them from source, and adjust the paths that
are defined in the wrapper script... Anyway, here's how to invoke it:

    ./scripts/valgrind.sh

[gsettings]: https://developer.gnome.org/gio/stable/GSettings.html
[dconf]: https://wiki.gnome.org/Projects/dconf
[gstreamer tools]: https://gstreamer.freedesktop.org/documentation/tutorials/basic/gstreamer-tools.html
[valgrind]: https://valgrind.org



Code Overview
-------------

Goodvibes is written in C/GObject. The code is heavily object-oriented, and if
you're not familiar with GObject, you will have to accept that some magic is
going on, here and there. Good luck with that ;)

The code is neatly split into different parts:

- `base`: the basic stuff we need.
- `core`: the core of Goodvibes, basically enough to have the software up and
  running, without the ui.
- `ui`: the GTK user interface.
- `feat`: features that can be enable/disabled at compile-time. I guess it's
  quite similar to the plugins you often find in music players on GNU/Linux.
  Except that I didn't dare to call it plugin, for plugins are usually
  something discovered and loaded at run-time, not at compile-time.



How To Code That
----------------

#### Adding a new setting

Settings are mapped directly to GObject properties. You must ensure that:

- The object must be a global instance, part of the `core`, `ui` or `feat`
  system.
- The object must implement the `GvConfigurable` interface.

You must bind the property to a setting using `g_settings_bind()`. This **must
NOT be done** at construct-time, but later on in the `configure()` function
(implemented from the GvConfigurable interface).

You must understand that the startup procedure is made in two steps:

1. Every objects are *created*. No errors are expected at this stage.
2. Every objects are *configured*. Serious things start to happen, there might
   be errors, and it's important to be ready to report it to the user. That's
   why the configure step is run *after* everything is created.

#### Reporting an error to the user

Here's how to report an error to the user from an object:

- The object must have been registered using the function
  `gv_base_register_object()`.
- The object must implement the `GvErrorable` interface.

Then, it's just a matter of invoking the function `gv_errorable_emit_error()`.

A few things to notice:

- You shouldn't call this function too early (aka during construction), as the
  system is not ready yet.
- The user message **must be** translatable as it's intended for *users*.
- It's up to you to drop an additional log (`WARNING()` should be used).
- The log message **must NOT be** translatable as it's intended for
  *developers*.



Coding Style
------------

#### Code formatting

In order to keep the codebase in good shape, we rely on [editorconfig][] and
[clang-format][].

Editorconfig tells your editor how to behave, assuming that your editor has
support for it. If it does not, you should install the corresponding plugin:

    # Using Emacs?
    apt install elpa-editorconfig
    # Using Vim?
    apt install vim-editorconfig

Editorconfig only gives very rough guidelines though. In our case, it mostly
means two things: 1) use an indent size of **8 columns** and 2) indent with
**tabs**. For the rest, it's your job to keep the coding style consistent. It's
not very difficult though, just use your common sense and try to match the
surroundings.

On top of that, we use `clang-format` (>= 12) to format the whole codebase. You
can install it with:

    apt install clang-format

clang-format can reformat a whole file, but what's more interesting is that it
can also be used to reformat only certain git commits, using the command `git
format-clang`. Here's a quick how to, taken as is from `systemd/.clang-format`:

Option 1: Fix up formatting before committing
1. Edit and stage your files.
2. Run `git clang-format`.
3. Verify + correct + (un)stage changes.
4. Commit.

Option 2: Fix up formatting after committing
1. Commit your changes.
2. Run `git clang-format HEAD~` - Refer the commit *before* your changes here.
3. Verify + correct changes, `git difftool -d` can help here.
4. Stage + commit, potentially with `--amend` (means to fixup the last commit).

It can also be useful to format the whole codebase:

    git ls-files 'src/*.c' | xargs clang-format -i

Just make sure to run this command from the root of the source tree. Also,
note that we only format the code (`.c` files), not the headers (`.h` files).
Header files are indented manually for better readability.

[editorconfig]: https://editorconfig.org/
[clang-format]: https://clang.llvm.org/docs/ClangFormat.html

#### Comments

For comments, always use C style (aka `/* ... */`).

C++ style comments (aka.`// ...`) are reserved for tool control (things like
`// clang-format off` or `// fall through`) and codetags.

#### Codetags

For codetags, always use C++ style (aka `//`).

Here are the codetags currently in use:

- `WISHED` Things I wish, but will probably never do.
- `TODO`   Things that should be done pretty soon.
- `FIXME`  Things obviously broken.

Always try to make it a one-liner if possible, or at least describe the problem
in one line, then add more details on the following lines.

Stick to these conventions, and then getting a list of things is easy:

    # TODO list
    ack 'TODO'
    # List everything
    ack '// '

Here are some links that discuss codetags:

- <http://legacy.python.org/dev/peps/pep-0350/>
- <http://stackoverflow.com/q/1452934/776208>

#### GObject C Files Layout

If you find yourself writing a new file, therefore creating a new object, fear
not! There's a script that generates all the boilerplate.

    ./scripts/code/gv-object-make.sh

Have a look at the structure of the C file created, and please notice a few
things.

Functions are ordered from low-level to high-level, to avoid the need of
declaring functions before defining it. So basically, you read the file from
bottom to top.

Functions are grouped in sections, titled by a comment such as:

    /*
     * Section name
     */

Most of the time, these sections are always the same, because implementing a
GObject always boils down to the same thing, more or less: GObject inherited
methods, property accessors, signal handlers, public methods, private methods,
helpers. Sometimes we implement an interface, and that's all there is to it. So
I try to show this consistent layout and this simplicity in the C file, and to
have it always the same way, instead of throwing code in a complete mess.

If you stick to these conventions, then it's easy to find your way in the code:
whatever file you're looking at, they all look the same.



Contribution
------------

Contributing is better done through GitLab, please follow the usual workflow:

- Have your account setup
- Fork the project
- Create a branch for your work
- Work
- Create a merge request

If you hack around for your own use-case, and think that other users could
benefit from your hacks, feel free to share it and discuss it on GitLab.

If we decide that your hacks should be integrated in Goodvibes proper, then you
will have to make sure it looks good, with meaningful commits that fit into the
existing design, proper commit messages, respect for the coding style, and so
on.

Please note that my first goal for Goodvibes is to ensure a long-term
maintenance. There are more than enough software with a short lifetime around,
and I don't want to add another to the list. It means I want the code to be
clean, readable, maintainable. Clean design and zero hacks are my goals. Dirty
hacks are OK to solve an outstanding problem quickly, or to demonstrate a
possible solution. But they are not OK for long-term maintenance, and therefore
won't make it upstream.