The coding style used is primarily the GNU flavor of the GNOME coding style, with some additions described below.
-
Use this code style on new code. When changing old code with a different code style, feel free to also adjust it to use this code style.
-
Use regular C types and
stdint.h
types instead of GLib fundamental types, except forgboolean
, andguint
/gulong
for GSource IDs and signal handler IDs. That means e.g.uint64_t
instead ofguint64
,int
instead ofgint
,unsigned int
instead ofguint
if unsignedness is of importance,uint8_t
instead ofguchar
, and so on. -
Try to to limit line length to 80 characters, although it's not a strict limit.
-
Usage of
g_autofree
andg_autoptr
is encouraged. The style to use isg_autofree char *text = NULL; g_autoptr (MetaSomeThing) thing = NULL; text = g_strdup_printf ("The text: %d", a_number); thing = g_object_new (META_TYPE_SOME_THING, "text", text, NULL); thinger_use_thing (rocket, thing);
-
Declare variables at the top of the block they are used, but avoid non-trivial logic among variable declarations. Non-trivial logic can be getting a pointer that may be
NULL
, any kind of math, or anything that may have side effects. -
Instead of boolean arguments in functions, prefer enums or flags when they're more expressive. The naming convention for flags is
typedef _MetaSomeThingFlags { META_SOME_THING_FLAG_NONE = 0, META_SOME_THING_FLAG_ALTER_REALITY = 1 << 0, META_SOME_THING_FLAG_MANIPULATE_PERCEPTION = 1 << 1, } MetaSomeThingFlags;
-
Use
g_new0 ()
etc. instead ofg_slice_new0 ()
. -
Initialize and assign floating point variables (i.e.
float
ordouble
) using the the correct precision format, and don't omit zeros. For examplefloat ratio = 2.0f; double pi = 3.14159; float fraction = 0.2f;
- For object instance pointers, use a descriptive name instead of
self
, e.g.
G_DEFINE_TYPE (MetaPlaceholder, meta_placeholder, G_TYPE_OBJECT)
...
void
meta_placeholder_hold_place (MetaPlaceholder *placeholder)
{
...
}
- When object instance pointers are pointers to non-generic implementations of a generalized type, the convention is to suffix the variable name with the sub-type name. E.g.
G_DEFINE_TYPE (MetaPlaceholderWayland, meta_placeholder_wayland,
META_TYPE_PLACEHOLDER)
...
void
meta_placeholder_wayland_get_waylandy (MetaPlaceholderWayland *placeholder_wayland)
{
...
}
- The return type and
*
are separated by a space. - Function name starts one space after the last
*
. - Parenthesis comes one space after the function name.
As an example, this is how functions in a header file should look like:
gboolean meta_udev_is_drm_device (MetaUdev *udev,
GUdevDevice *device);
GList * meta_udev_list_drm_devices (MetaUdev *udev,
GError **error);
MetaUdev * meta_udev_new (MetaBackendNative *backend_native);
Keep functions in the following order in source files:
- GPL header
- Include header files
- Enums
- Structures
- Function prototypes
G_DEFINE_TYPE()
- Static variables
- Auxiliary functions
- Callbacks
- Interface implementations
- Parent vfunc overrides
- class_init and init
- Public API
Source files should use the header include order of the following example:
meta-example.c
:
#include "config.h"
#include "meta-example-private.h"
#include <glib-object.h>
#include <stdint.h>
#ifdef HAVE_WAYLAND
#include <wayland-server-core.h>
#endif
#include "clutter/clutter.h"
#include "backends/meta-backend-private.h"
#include "mtk/mtk.h"
#ifdef HAVE_WAYLAND
#include "wayland/meta-wayland-surface-private.h"
#endif
#include "meta-dbus-file-generated-by-gdbus-codegen.h"
Include paths for non-system includes should be relative to the corresponding
modules source root; i.e. "backends/meta-backend-private.h"
for
src/backends/
and "clutter/clutter.h"
for clutter/clutter/clutter.h
.
Each structure field has a space after their type name. Structure fields aren't aligned. For example:
struct _MetaFooBar
{
MetaFoo parent;
MetaBar *bar;
MetaSomething *something;
};
Function prototypes must be formatted just like in header files.
When overriding parent class vfuncs, or implementing an interface, vfunc overrides should be named as a composition of the current class prefix, followed by the vfunc name. For example:
static void
meta_bar_spawn_unicorn (MetaParent *parent)
{
/* ... */
}
static void
meta_bar_dispose (GObject *object)
{
/* ... */
}
static void
meta_bar_finalize (GObject *object)
{
/* ... */
}
static void
meta_bar_class_init (MetaBarClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
MetaParentClass *parent_class = META_PARENT_CLASS (klass);
object_class->dispose = meta_bar_dispose;
object_class->finalize = meta_bar_finalize;
parent_class->spawn_unicorn = meta_bar_spawn_unicorn;
}
When implementing interfaces, two groups of functions are involved: the init function, and the overrides.
The interface init function is named after the interface type in snake case,
followed by the _iface_init
suffix. For example:
static void meta_foo_iface_init (MetaFooInterface *foo_iface);
G_DEFINE_TYPE_WITH_CODE (MetaBar, meta_bar, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (META_TYPE_FOO,
meta_foo_iface_init));
Then, when implementing each vfunc of the interface, follow the same pattern of the Overrides section. Here's an example:
static void
meta_bar_do_something (MetaFoo *foo)
{
/* ... */
}
static void
meta_foo_iface_init (MetaFooInterface *foo_iface)
{
foo_iface->do_something = meta_bar_do_something;
}
Auxiliary functions are above every other functions to minimize the number of function prototypes in the file. These functions often grow when factoring out the same code between two or more functions:
static void
do_something_on_data (Foo *data,
Bar *bar)
{
/* ... */
}
static void
random_function (Foo *foo)
{
do_something_on_data (foo, bar);
}
static void
another_random_function (Foo *foo)
{
do_something_on_data (foo, bar);
}
Sometimes, however, auxiliary functions are created to break down otherwise large functions - in this case, it is appropriate to keep these auxiliary functions close to the function they are tightly related to.
Auxiliary function names must have a verb in the imperative form, and should
always perform an action over something. They usually don't have the class
prefix (meta_
, clutter_
, or cogl_
). For example:
static void
do_something_on_data (Foo *data,
Bar *bar)
{
/* ... */
}
Exceptionally, when converting between types, auxiliary function names may have the class prefix to this rule. For example:
static MetaFoo *
meta_foo_from_bar (Bar *bar)
{
/* ... */
}
Callback function names should have the name of the action in imperative
form. They don't have any prefix, but have a _func
suffix. For example:
static void
filter_something_func (Foo *foo,
Bar *bar,
gpointer user_data)
{
/* ... */
}
Signal callbacks generally have the signal name. They should be prefixed with
on_
, or suffixed with _cb
, but not both. For example:
static void
on_realize (ClutterActor *actor,
gpointer user_data)
{
/* ... */
}
static void
destroy_cb (ClutterActor *actor,
gpointer user_data)
{
/* ... */
}
When the callback is named after the object that generated it, and the signal, then passive voice is used. For example:
static void
click_action_clicked_cb (ClutterClickAction *click_action,
ClutterActor *actor,
gpointer user_data)
{
/* ... */
}
Trace spans should be named in C++ style, i.e.
Namespace::Class::method(args)
. For example:
static void
meta_wayland_surface_commit (MetaWaylandSurface *surface)
{
/* ... */
COGL_TRACE_BEGIN_SCOPED (MetaWaylandSurfaceCommit,
"Meta::WaylandSurface::commit()");
/* ... */
}
This is to facilitate automatic name shrinking in profilers that will cut out the least important parts of the name (args, then namespaces in order, then class) to fit the name on screen when zoomed-out.
If you need to annotate multiple spans within a function, you can append their
name to the function name, delimited by a #
sign. For example:
void
meta_thing_do_stuff (MetaThing *thing)
{
COGL_TRACE_BEGIN_SCOPED (OneThing, "Meta::Thing::do_stuff#one_thing()");
/* Code that does one thing that takes time */
COGL_TRACE_END (OneThing);
COGL_TRACE_BEGIN_SCOPED (OtherThing, "Meta::Thing::do_stuff#other_thing()");
/* Code that does other thing that takes time */
COGL_TRACE_END (OtherThing);
}
Keeping in the entire method name helps in profiler views that don't show parent-child relationships, i.e. a global span statistics view.