~ubuntu-branches/ubuntu/trusty/fcitx/trusty-proposed

« back to all changes in this revision

Viewing changes to src/lib/fcitx/module.h

  • Committer: Package Import Robot
  • Author(s): Aron Xu
  • Date: 2013-02-10 17:03:56 UTC
  • mfrom: (1.3.18) (33.1.3 experimental)
  • Revision ID: package-import@ubuntu.com-20130210170356-2yuv6xy3ed378kn0
Tags: 1:4.2.7-1
* New upstream release.
* New binary packages:
  - fcitx-libs-gclient: D-Bus client library for Glib
  - fcitx-libs-qt: D-Bus client library for Qt
  - fcitx-module-quickphrase-editor: Quick Phrase editor module

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/***************************************************************************
2
 
 *   Copyright (C) 2010~2010 by CSSlayer                                   *
 
2
 *   Copyright (C) 2010~2012 by CSSlayer                                   *
3
3
 *   wengxt@gmail.com                                                      *
 
4
 *   Copyright (C) 2012~2013 by Yichao Yu                                  *
 
5
 *   yyc1992@gmail.com                                                     *
4
6
 *                                                                         *
5
7
 *   This program is free software; you can redistribute it and/or modify  *
6
8
 *   it under the terms of the GNU General Public License as published by  *
27
29
#define _FCITX_MODULE_H
28
30
#include <fcitx-config/fcitx-config.h>
29
31
#include <fcitx-utils/utarray.h>
 
32
#include <fcitx-utils/utils.h>
 
33
#include <fcitx/addon.h>
 
34
#include <fcitx/fcitx.h>
30
35
 
31
36
#ifdef __cplusplus
32
37
extern "C" {
33
38
#endif
34
39
 
35
40
    struct _FcitxInstance;
36
 
    struct _FcitxAddon;
37
41
 
38
42
    /**
39
43
     * A misc module in Fcitx, it can register hook, or add it's own event
71
75
         **/
72
76
        void* args[10];
73
77
    } FcitxModuleFunctionArg;
 
78
    typedef void *(*FcitxModuleFunction)(void *self, FcitxModuleFunctionArg);
74
79
 
75
80
    /**
76
81
     * load all modules
81
86
    void FcitxModuleLoad(struct _FcitxInstance* instance);
82
87
 
83
88
    /**
 
89
     * Find a exported function of a addon.
 
90
     *
 
91
     * @param addon addon
 
92
     * @param functionId function index
 
93
     * @return FcitxModuleFunction
 
94
     **/
 
95
    FcitxModuleFunction FcitxModuleFindFunction(FcitxAddon *addon,
 
96
                                                int func_id);
 
97
 
 
98
    void *FcitxModuleInvokeOnAddon(FcitxAddon *addon, FcitxModuleFunction func,
 
99
                                   FcitxModuleFunctionArg *args);
 
100
    /**
84
101
     * invode inter module function wiht addon pointer, returns NULL when fails (the function itself can also return NULL)
85
102
     *
86
103
     * @param addon addon
88
105
     * @param args arguments
89
106
     * @return void*
90
107
     **/
91
 
    void* FcitxModuleInvokeFunction(struct _FcitxAddon* addon, int functionId, FcitxModuleFunctionArg args);
 
108
    FCITX_DEPRECATED
 
109
    void* FcitxModuleInvokeFunction(FcitxAddon* addon, int functionId, FcitxModuleFunctionArg args);
 
110
#define FcitxModuleInvokeVaArgs(addon, functionId, ARGV...)             \
 
111
    (FcitxModuleInvokeFunction(addon, functionId,                       \
 
112
                               (FcitxModuleFunctionArg){ {ARGV} }))
92
113
 
93
114
    /**
94
115
     * invoke inter module function with addon name, returns NULL when fails (the function itself can also return NULL)
99
120
     * @param args arguments
100
121
     * @return void*
101
122
     **/
 
123
    FCITX_DEPRECATED
102
124
    void* FcitxModuleInvokeFunctionByName(struct _FcitxInstance* instance, const char* name, int functionId, FcitxModuleFunctionArg args);
 
125
#define FcitxModuleInvokeVaArgsByName(instance, name, functionId, ARGV...) \
 
126
    (FcitxModuleInvokeFunctionByName(instance, name, functionId,        \
 
127
                               (FcitxModuleFunctionArg){ {ARGV} }))
103
128
 
104
129
/** call a function provides by other addon */
105
 
#define InvokeFunction(INST, MODULE, FUNC, ARG)  \
106
 
    ((MODULE##_##FUNC##_RETURNTYPE) FcitxModuleInvokeFunctionByName(INST, MODULE##_NAME, MODULE##_##FUNC, ARG))
107
 
 
108
 
/** add a function to current addon */
109
 
#define AddFunction(ADDON, Realname) \
110
 
    do { \
111
 
        void *temp = Realname; \
112
 
        utarray_push_back(&ADDON->functionList, &temp); \
 
130
#define InvokeFunction(INST, MODULE, FUNC, ARG)                         \
 
131
    ((MODULE##_##FUNC##_RETURNTYPE)FcitxModuleInvokeFunctionByName(INST, MODULE##_NAME, MODULE##_##FUNC, ARG))
 
132
 
 
133
#define InvokeVaArgs(INST, MODULE, FUNC, ARGV...)                       \
 
134
    ((MODULE##_##FUNC##_RETURNTYPE)FcitxModuleInvokeFunctionByName(     \
 
135
        INST, MODULE##_NAME, MODULE##_##FUNC,                           \
 
136
        (FcitxModuleFunctionArg){ {ARGV} }))
 
137
 
 
138
/** add a function to a addon */
 
139
#define AddFunction(ADDON, Realname)                                    \
 
140
    do {                                                                \
 
141
        FCITX_DEPRECATED void *(AddonFunction) = (void*)Realname;       \
 
142
        utarray_push_back(&ADDON->functionList, &(AddonFunction));      \
113
143
    } while(0)
114
144
 
 
145
    /**
 
146
     * add a function to a addon
 
147
     *
 
148
     * @param addon
 
149
     * @param func
 
150
     **/
 
151
    void FcitxModuleAddFunction(FcitxAddon *addon, FcitxModuleFunction func);
 
152
 
 
153
#ifdef __cplusplus
 
154
#define DECLARE_ADDFUNCTIONS(prefix)                                    \
 
155
    extern "C" {                                                        \
 
156
        static inline FcitxAddon*                                       \
 
157
        Fcitx_##prefix##_GetAddon(FcitxInstance *instance);             \
 
158
        static void Fcitx##prefix##AddFunctions(FcitxInstance *instance); \
 
159
    }
 
160
#else
 
161
#define DECLARE_ADDFUNCTIONS(prefix)                                    \
 
162
    static inline FcitxAddon*                                           \
 
163
    Fcitx_##prefix##_GetAddon(FcitxInstance *instance);                 \
 
164
    static void Fcitx##prefix##AddFunctions(FcitxInstance *instance);
 
165
#endif
 
166
 
 
167
// Well won't work if there are multiple instances, but that will also break
 
168
// lots of other things anyway.
 
169
#define DEFINE_GET_ADDON(name, prefix)                           \
 
170
    static inline FcitxAddon*                                    \
 
171
    Fcitx##prefix##GetAddon(FcitxInstance *instance)             \
 
172
    {                                                            \
 
173
        static FcitxAddon *addon = NULL;                         \
 
174
        static FcitxInstance *_instance = NULL;                  \
 
175
        if (fcitx_unlikely(_instance != instance)) {             \
 
176
            _instance = instance;                                \
 
177
            addon = FcitxAddonsGetAddonByName(                   \
 
178
                FcitxInstanceGetAddons(instance), name);         \
 
179
        }                                                        \
 
180
        return addon;                                            \
 
181
    }
 
182
 
 
183
#define DEFINE_GET_AND_INVOKE_FUNC(prefix, suffix, id)                  \
 
184
    DEFINE_GET_AND_INVOKE_FUNC_WITH_ERROR(prefix, suffix, id, NULL)
 
185
#define DEFINE_GET_AND_INVOKE_FUNC_WITH_ERROR(prefix, suffix, id, err_ret) \
 
186
    DEFINE_GET_AND_INVOKE_FUNC_WITH_TYPE_AND_ERROR(prefix, suffix, id,  \
 
187
                                                   intptr_t, (intptr_t)err_ret)
 
188
 
 
189
#define DEFINE_GET_AND_INVOKE_FUNC_WITH_TYPE_AND_ERROR(prefix, suffix,  \
 
190
                                                       id, type, err_ret) \
 
191
    static inline FcitxModuleFunction                                  \
 
192
    Fcitx##prefix##Find##suffix(FcitxAddon *addon)                     \
 
193
    {                                                                  \
 
194
        static FcitxAddon *_addon = NULL;                              \
 
195
        static FcitxModuleFunction func = NULL;                        \
 
196
        if (fcitx_unlikely(addon != _addon)) {                         \
 
197
            _addon = addon;                                            \
 
198
            func = FcitxModuleFindFunction(addon, id);                 \
 
199
        }                                                              \
 
200
        return func;                                                   \
 
201
    }                                                                  \
 
202
    static inline void*                                                \
 
203
    Fcitx##prefix##Invoke##suffix(FcitxInstance *instance,             \
 
204
                                  FcitxModuleFunctionArg args)         \
 
205
    {                                                                  \
 
206
        static type _on_err = (err_ret);                               \
 
207
        FCITX_DEF_CAST_TO_PTR(on_err, type, _on_err);                  \
 
208
        FcitxAddon *addon = Fcitx##prefix##GetAddon(instance);         \
 
209
        if (fcitx_unlikely(!addon))                                    \
 
210
            return on_err;                                             \
 
211
        FcitxModuleFunction func = Fcitx##prefix##Find##suffix(addon); \
 
212
        if (fcitx_unlikely(!func))                                     \
 
213
            return on_err;                                             \
 
214
        return FcitxModuleInvokeOnAddon(addon, func, &args);           \
 
215
    }
 
216
 
 
217
#define FCITX_DEF_MODULE_ARGS(var, ARGV...)             \
 
218
    FcitxModuleFunctionArg var = { {ARGV} }
 
219
    /* void *__##var##_array[] = {ARGV};                                   \ */
 
220
    /* size_t __##var##_length = sizeof(__##var##_array) / sizeof(void*);  \ */
 
221
    /* FcitxModuleFunctionArg var[] = { { .n = __##var##_length,           \ */
 
222
    /*                                    .args = __##var##_array } } */
 
223
 
 
224
#define FCITX_MODULE_FUNCTION_ARGS void *arg, FcitxModuleFunctionArg args
 
225
#define FCITX_MODULE_SELF(NAME, TYPE) TYPE *NAME = (TYPE*)arg
 
226
#define FCITX_MODULE_ARG(NAME, TYPE, INDEX)                     \
 
227
    FCITX_DEF_CAST_FROM_PTR(TYPE, NAME, args.args[(INDEX)])
 
228
 
115
229
#ifdef __cplusplus
116
230
}
117
231
#endif