#ifndef _G_ROCCAT_HELPER_H_
#define _G_ROCCAT_HELPER_H_

/*
 * This file is part of roccat-tools.
 *
 * roccat-tools is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * roccat-tools is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with roccat-tools. If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>

G_BEGIN_DECLS

#define G_ROCCAT_MSEC_PER_SEC 1000
#define G_ROCCAT_USEC_PER_MSEC 1000
#define G_ROCCAT_NSEC_PER_MSEC 1000000
#define G_ROCCAT_NSEC_PER_SEC (G_ROCCAT_MSEC_PER_SEC * G_ROCCAT_NSEC_PER_MSEC)

#define G_ROCCAT_STRUCT_CONTAINER_OF(ptr, type, member) \
	(type*)((guint8*)(ptr) - G_STRUCT_OFFSET(type,member))

static inline GSList *g_roccat_data_to_slist(gpointer data) {
	return (GSList *)( (char *)data - ((size_t) &((GSList *)0)->data) );
}

void g_roccat_list_free_full(GList *list, GDestroyNotify freefunc);

GList *g_roccat_list_find_custom_backwards(GList *list, gconstpointer data, GCompareFunc func);

/*
 * Assuming that g_list_sort uses as stable sorting algorithm means relying
 * on undocumented behavior.
 * This stable sorting implementation is not meant for large datasets though.
 */
GList *g_roccat_list_stable_sort(GList *list, GCompareFunc compare_func);

/*!
 * \brief converts hex string from key_file to binary data
 * \retval If successful returns pointer to data, that has to be freed with g_free().
 *         On error returns +NULL+ with error set.
 */
gpointer roccat_key_file_get_binary(GKeyFile *key_file, gchar const *group_name, gchar const *key, gsize size, GError **error);

/*!
 * \brief converts binary data to hex string and puts it in key_file
 */
void roccat_key_file_set_binary(GKeyFile *key_file, gchar const *group_name, gchar const *key, gconstpointer data, gsize size);

/*!
 * \retval copy of \e src
 */
GKeyFile *roccat_key_file_dup(GKeyFile *src);

gchar *g_roccat_unichar_to_utf8(gunichar c);

G_END_DECLS

#endif
