1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
2 /*
3  * nimf-types.c
4  * This file is part of Nimf.
5  *
6  * Copyright (C) 2015-2019 Hodong Kim <cogniti@gmail.com>
7  *
8  * Nimf is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * Nimf is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program;  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "nimf-types.h"
23 #include "nimf-enum-types-private.h"
24 
25 /**
26  * SECTION:nimf-types
27  * @title: Types
28  * @section_id: nimf-types
29  */
30 
31 /**
32  * nimf_key_new:
33  *
34  * Creates a new #NimfKey. All fields are set to 0.
35  *
36  * Return: a new #NimfKey, which should be freed with nimf_key_free().
37  */
38 NimfKey *
nimf_key_new()39 nimf_key_new ()
40 {
41   g_debug (G_STRLOC ": %s", G_STRFUNC);
42 
43   return g_slice_new0 (NimfKey);
44 }
45 
46 /**
47  * nimf_key_new_from_nicks:
48  * @nicks: an array of gchar
49  *
50  * Return: a new #NimfKey, which should be freed with nimf_key_free().
51  */
52 NimfKey *
nimf_key_new_from_nicks(const gchar ** nicks)53 nimf_key_new_from_nicks (const gchar **nicks)
54 {
55   g_debug (G_STRLOC ": %s", G_STRFUNC);
56 
57   NimfKey     *key = g_slice_new0 (NimfKey);
58   GEnumValue  *enum_value;  /* Do not free */
59   GFlagsValue *flags_value; /* Do not free */
60   GFlagsClass *flags_class = g_type_class_ref (NIMF_TYPE_MODIFIER_TYPE);
61   GEnumClass  *enum_class  = g_type_class_ref (NIMF_TYPE_KEY_SYM);
62 
63   gint i;
64   for (i = 0; nicks[i] != NULL; i++)
65   {
66     if (g_str_has_prefix (nicks[i], "<"))
67     {
68       flags_value = g_flags_get_value_by_nick (flags_class, nicks[i]);
69 
70       if (flags_value)
71         key->state |= flags_value->value;
72       else
73         g_warning ("NimfModifierType doesn't have a member with that nickname: %s", nicks[i]);
74     }
75     else
76     {
77       enum_value = g_enum_get_value_by_nick (enum_class, nicks[i]);
78 
79       if (enum_value)
80         key->keyval = enum_value->value;
81       else
82         g_warning ("NimfKeySym doesn't have a member with that nickname: %s", nicks[i]);
83     }
84   }
85 
86   g_type_class_unref (flags_class);
87   g_type_class_unref (enum_class);
88 
89   return key;
90 }
91 
92 /**
93  * nimf_key_newv:
94  * @keys: an array of gchar
95  *
96  * Creates a new array of #NimfKey.
97  *
98  * Returns: a new array of #NimfKey, which should be freed with
99  *   nimf_key_freev().
100  */
101 NimfKey **
nimf_key_newv(const gchar ** keys)102 nimf_key_newv (const gchar **keys)
103 {
104   NimfKey **nimf_keys = g_malloc0_n (1, sizeof (NimfKey *));
105 
106   gint i;
107   for (i = 0; keys[i] != NULL; i++)
108   {
109     gchar **nicks = g_strsplit (keys[i], " ", -1);
110     NimfKey *key = nimf_key_new_from_nicks ((const gchar **) nicks);
111     g_strfreev (nicks);
112 
113     nimf_keys = g_realloc_n (nimf_keys, sizeof (NimfKey *), i + 2);
114     nimf_keys[i] = key;
115     nimf_keys[i + 1] = NULL;
116   }
117 
118   return nimf_keys;
119 }
120 
121 /**
122  * nimf_key_freev:
123  * @keys: an array of #NimfKey
124  *
125  * Frees an array of @keys
126  */
127 void
nimf_key_freev(NimfKey ** keys)128 nimf_key_freev (NimfKey **keys)
129 {
130   g_debug (G_STRLOC ": %s", G_STRFUNC);
131 
132   if (keys)
133   {
134     int i;
135     for (i = 0; keys[i] != NULL; i++)
136       nimf_key_free (keys[i]);
137 
138     g_free (keys);
139   }
140 }
141 
142 /**
143  * nimf_key_free:
144  * @key: a #NimfKey
145  *
146  * Frees a @key
147  */
148 void
nimf_key_free(NimfKey * key)149 nimf_key_free (NimfKey *key)
150 {
151   g_debug (G_STRLOC ": %s", G_STRFUNC);
152 
153   g_return_if_fail (key != NULL);
154 
155   g_slice_free (NimfKey, key);
156 }
157 
158 /**
159  * nimf_preedit_attr_new:
160  * @type: a #NimfPreeditAttrType
161  * @start_index: start index in characters
162  * @end_index: end index in characters; The character at this index is not
163  *   included.
164  *
165  * Returns: a new #NimfPreeditAttr, which should be freed with
166  *   nimf_preedit_attr_free().
167  */
168 NimfPreeditAttr *
nimf_preedit_attr_new(NimfPreeditAttrType type,guint start_index,guint end_index)169 nimf_preedit_attr_new (NimfPreeditAttrType type,
170                        guint               start_index,
171                        guint               end_index)
172 {
173   g_debug (G_STRLOC ": %s", G_STRFUNC);
174 
175   NimfPreeditAttr *attr;
176 
177   attr = g_new (NimfPreeditAttr, 1);
178   attr->type        = type;
179   attr->start_index = start_index;
180   attr->end_index   = end_index;
181 
182   return attr;
183 }
184 
185 /**
186  * nimf_preedit_attrs_copy:
187  * @attrs: an array of #NimfPreeditAttr
188  *
189  * Returns: a new array of #NimfPreeditAttr, which should be freed with
190  *   nimf_preedit_attr_freev().
191  */
192 NimfPreeditAttr **
nimf_preedit_attrs_copy(NimfPreeditAttr ** attrs)193 nimf_preedit_attrs_copy (NimfPreeditAttr **attrs)
194 {
195   g_debug (G_STRLOC ": %s", G_STRFUNC);
196 
197   NimfPreeditAttr **preedit_attrs;
198   gint              i;
199 
200   g_return_val_if_fail (attrs, NULL);
201 
202   preedit_attrs = g_malloc0_n (1, sizeof (NimfPreeditAttr *));
203 
204   for (i = 0; attrs[i] != NULL; i++)
205   {
206     preedit_attrs = g_realloc_n (preedit_attrs, 1 + i + 1, sizeof (NimfPreeditAttr *));
207     preedit_attrs[i] = g_memdup (attrs[i], sizeof (NimfPreeditAttr));
208     preedit_attrs[i + 1] = NULL;
209   }
210 
211   return preedit_attrs;
212 }
213 
214 /**
215  * nimf_preedit_attr_free:
216  * @attr: a #NimfPreeditAttr
217  *
218  * Frees a @attr
219  */
220 void
nimf_preedit_attr_free(NimfPreeditAttr * attr)221 nimf_preedit_attr_free (NimfPreeditAttr *attr)
222 {
223   g_debug (G_STRLOC ": %s", G_STRFUNC);
224 
225   g_free (attr);
226 }
227 
228 /**
229  * nimf_preedit_attr_freev:
230  * @attrs: an array of #NimfPreeditAttr
231  *
232  * Frees an array of @attrs
233  */
234 void
nimf_preedit_attr_freev(NimfPreeditAttr ** attrs)235 nimf_preedit_attr_freev (NimfPreeditAttr **attrs)
236 {
237   g_debug (G_STRLOC ": %s", G_STRFUNC);
238 
239   if (attrs)
240   {
241     int i;
242     for (i = 0; attrs[i] != NULL; i++)
243       nimf_preedit_attr_free (attrs[i]);
244 
245     g_free (attrs);
246   }
247 }
248 
249 /**
250  * nimf_method_info_new:
251  *
252  * Returns: a new #NimfMethodInfo, which should be freed with
253  *   nimf_method_info_free().
254  */
255 NimfMethodInfo *
nimf_method_info_new()256 nimf_method_info_new ()
257 {
258   g_debug (G_STRLOC ": %s", G_STRFUNC);
259 
260   return g_slice_new0 (NimfMethodInfo);
261 }
262 
263 /**
264  * nimf_method_info_free:
265  * @info: a #NimfMethodInfo
266  *
267  * Frees an @info.
268  */
269 void
nimf_method_info_free(NimfMethodInfo * info)270 nimf_method_info_free (NimfMethodInfo *info)
271 {
272   g_debug (G_STRLOC ": %s", G_STRFUNC);
273 
274   if (info)
275     g_slice_free (NimfMethodInfo, info);
276 }
277 
278 /**
279  * nimf_method_info_freev:
280  * @infos: an array of #NimfMethodInfo
281  *
282  * Frees an array of @infos.
283  */
284 void
nimf_method_info_freev(NimfMethodInfo ** infos)285 nimf_method_info_freev (NimfMethodInfo **infos)
286 {
287   g_debug (G_STRLOC ": %s", G_STRFUNC);
288 
289   if (infos)
290   {
291     int i;
292     for (i = 0; infos[i]; i++)
293       nimf_method_info_free (infos[i]);
294 
295     g_free (infos);
296   }
297 }
298