Minor code cleanups
[libjaylink.git] / libjaylink / core.c
bloba25b217d845fb7e219d034e11dfda56f07d494fa
1 /*
2 * This file is part of the libjaylink project.
4 * Copyright (C) 2014-2016 Marc Schink <jaylink-dev@marcschink.de>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdlib.h>
21 #include <libusb.h>
23 #include "libjaylink.h"
24 #include "libjaylink-internal.h"
26 /**
27 * @file
29 * Core library functions.
32 /**
33 * Initialize libjaylink.
35 * This function must be called before any other libjaylink function is called.
37 * @param[out] ctx Newly allocated libjaylink context on success, and undefined
38 * on failure.
40 * @retval JAYLINK_OK Success.
41 * @retval JAYLINK_ERR_ARG Invalid arguments.
42 * @retval JAYLINK_ERR_MALLOC Memory allocation error.
43 * @retval JAYLINK_ERR Other error conditions.
45 * @since 0.1.0
47 JAYLINK_API int jaylink_init(struct jaylink_context **ctx)
49 int ret;
50 struct jaylink_context *context;
52 if (!ctx)
53 return JAYLINK_ERR_ARG;
55 context = malloc(sizeof(struct jaylink_context));
57 if (!context)
58 return JAYLINK_ERR_MALLOC;
60 if (libusb_init(&context->usb_ctx) != LIBUSB_SUCCESS) {
61 free(context);
62 return JAYLINK_ERR;
65 context->devs = NULL;
66 context->discovered_devs = NULL;
68 /* Show error and warning messages by default. */
69 context->log_level = JAYLINK_LOG_LEVEL_WARNING;
71 context->log_callback = &log_vprintf;
72 context->log_callback_data = NULL;
74 ret = jaylink_log_set_domain(context, JAYLINK_LOG_DOMAIN_DEFAULT);
76 if (ret != JAYLINK_OK) {
77 free(context);
78 return ret;
81 *ctx = context;
83 return JAYLINK_OK;
86 /**
87 * Shutdown libjaylink.
89 * @param[in,out] ctx libjaylink context.
91 * @since 0.1.0
93 JAYLINK_API void jaylink_exit(struct jaylink_context *ctx)
95 struct list *item;
97 if (!ctx)
98 return;
100 item = ctx->discovered_devs;
102 while (item) {
103 jaylink_unref_device((struct jaylink_device *)item->data);
104 item = item->next;
107 list_free(ctx->discovered_devs);
108 list_free(ctx->devs);
110 libusb_exit(ctx->usb_ctx);
111 free(ctx);