File indexing completed on 2024-04-14 05:39:53

0001 /*****************************************************************************
0002  *   Copyright 2015 - 2015 Yichao Yu <yyc1992@gmail.com>                     *
0003  *                                                                           *
0004  *   This program is free software; you can redistribute it and/or modify    *
0005  *   it under the terms of the GNU Lesser General Public License as          *
0006  *   published by the Free Software Foundation; either version 2.1 of the    *
0007  *   License, or (at your option) version 3, or any later version accepted   *
0008  *   by the membership of KDE e.V. (or its successor approved by the         *
0009  *   membership of KDE e.V.), which shall act as a proxy defined in          *
0010  *   Section 6 of version 3 of the license.                                  *
0011  *                                                                           *
0012  *   This program is distributed in the hope that it will be useful,         *
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU       *
0015  *   Lesser General Public License for more details.                         *
0016  *                                                                           *
0017  *   You should have received a copy of the GNU Lesser General Public        *
0018  *   License along with this library. If not,                                *
0019  *   see <http://www.gnu.org/licenses/>.                                     *
0020  *****************************************************************************/
0021 
0022 #ifndef __QTCURVE_GTK_GDBUS_H__
0023 #define __QTCURVE_GTK_GDBUS_H__
0024 
0025 #include <gio/gio.h>
0026 #include <stdint.h>
0027 #include <utility>
0028 
0029 namespace QtCurve {
0030 namespace GDBus {
0031 
0032 void _callMethod(const char *bus_name, const char *path, const char *iface,
0033                  const char *method, GVariant *params);
0034 
0035 #define DEF_GVARIANT_CONVERT(type, gname)       \
0036     static inline GVariant*                     \
0037     toGVariant(type val)                        \
0038     {                                           \
0039         return g_variant_new_##gname(val);      \
0040     }
0041 
0042 DEF_GVARIANT_CONVERT(bool, boolean)
0043 
0044 DEF_GVARIANT_CONVERT(signed char, byte)
0045 DEF_GVARIANT_CONVERT(unsigned char, byte)
0046 
0047 DEF_GVARIANT_CONVERT(int16_t, int16)
0048 DEF_GVARIANT_CONVERT(uint16_t, uint16)
0049 
0050 DEF_GVARIANT_CONVERT(int32_t, int32)
0051 DEF_GVARIANT_CONVERT(uint32_t, uint32)
0052 
0053 DEF_GVARIANT_CONVERT(int64_t, int64)
0054 DEF_GVARIANT_CONVERT(uint64_t, uint64)
0055 
0056 DEF_GVARIANT_CONVERT(double, double)
0057 
0058 DEF_GVARIANT_CONVERT(const char*, string)
0059 
0060 static inline GVariant*
0061 toGVariant(GVariant *val)
0062 {
0063     return val;
0064 }
0065 
0066 #undef DEF_GVARIANT_CONVERT
0067 
0068 template<typename... Args>
0069 static inline void
0070 callMethod(const char *bus_name, const char *path, const char *iface,
0071            const char *method, Args&&... args)
0072 {
0073     GVariant *g_args[] = {toGVariant(std::forward<Args>(args))...};
0074     _callMethod(bus_name, path, iface, method,
0075                 g_variant_new_tuple(g_args, sizeof...(args)));
0076 }
0077 
0078 }
0079 }
0080 
0081 #endif