Debian Codegraph @

bindings/glib/examples/watch_line_info_glib.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 // SPDX-FileCopyrightText: 2023-2024 Bartosz Golaszewski <[email protected]>
3
4 /* Minimal example of watching for requests on particular lines. */
5
6 #include <glib.h>
7 #include <gpiod-glib.h>
8 #include <stdlib.h>
9
10 static void on_info_event(GpiodglibChip *chip G_GNUC_UNUSED,
11 GpiodglibInfoEvent *event,
12 gpointer data G_GNUC_UNUSED)
13 {
14 g_autoptr(GpiodglibLineInfo) info = NULL;
15 g_autoptr(GString) event_name = NULL;
16 guint offset;
17
18 event_name = g_string_new(
19 g_enum_to_string(GPIODGLIB_INFO_EVENT_TYPE_TYPE,
20 gpiodglib_info_event_get_event_type(event)));
21 g_string_replace(event_name, "GPIODGLIB_INFO_EVENT_LINE_", "", 0);
22 info = gpiodglib_info_event_get_line_info(event);
23 offset = gpiodglib_line_info_get_offset(info);
24
25 g_print("%s %u\n", event_name->str, offset);
26 }
27
28 int main(void)
29 {
30 static const gchar *const chip_path = "/dev/gpiochip1";
31 static const guint line_offsets[] = { 5, 3, 7 };
32 static const gsize num_lines = 3;
33
34 g_autoptr(GMainLoop) loop = NULL;
35 g_autoptr(GpiodglibChip) chip = NULL;
36 g_autoptr(GError) err = NULL;
37 guint i;
38
39 chip = gpiodglib_chip_new(chip_path, &err);
40 if (!chip) {
41 g_printerr("unable to open %s: %s\n", chip_path, err->message);
42 return EXIT_FAILURE;
43 }
44
45 for (i = 0; i < num_lines; i++) {
46 g_autoptr(GpiodglibLineInfo) info =
47 gpiodglib_chip_watch_line_info(chip, line_offsets[i],
48 &err);
49 if (!info) {
50 g_printerr("unable to watch line info for offset %u: %s",
51 line_offsets[1], err->message);
52 return EXIT_FAILURE;
53 }
54 }
55
56 loop = g_main_loop_new(NULL, FALSE);
57
58 g_signal_connect(chip, "info-event", G_CALLBACK(on_info_event), NULL);
59
60 g_main_loop_run(loop);
61
62 return EXIT_SUCCESS;
63 }