Blender  V2.93
keyconfig.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 
25 #include "RNA_types.h"
26 
27 #include "BLI_ghash.h"
28 #include "BLI_listbase.h"
29 #include "BLI_string.h"
30 #include "BLI_utildefines.h"
31 
32 #include "DNA_listBase.h"
33 #include "DNA_userdef_types.h"
35 
36 #include "BKE_idprop.h"
37 #include "BKE_keyconfig.h" /* own include */
38 
39 #include "MEM_guardedalloc.h"
40 
41 /* -------------------------------------------------------------------- */
47 wmKeyConfigPref *BKE_keyconfig_pref_ensure(UserDef *userdef, const char *kc_idname)
48 {
50  &userdef->user_keyconfig_prefs, kc_idname, offsetof(wmKeyConfigPref, idname));
51  if (kpt == NULL) {
52  kpt = MEM_callocN(sizeof(*kpt), __func__);
53  STRNCPY(kpt->idname, kc_idname);
54  BLI_addtail(&userdef->user_keyconfig_prefs, kpt);
55  }
56  if (kpt->prop == NULL) {
57  IDPropertyTemplate val = {0};
58  kpt->prop = IDP_New(IDP_GROUP, &val, kc_idname); /* name is unimportant */
59  }
60  return kpt;
61 }
62 
65 /* -------------------------------------------------------------------- */
72 
74 {
75  if (idname[0]) {
77 
79  if (kpt_rt) {
80  return kpt_rt;
81  }
82 
83  if (!quiet) {
84  printf("search for unknown keyconfig-pref '%s'\n", idname);
85  }
86  }
87  else {
88  if (!quiet) {
89  printf("search for empty keyconfig-pref\n");
90  }
91  }
92 
93  return NULL;
94 }
95 
97 {
98  BLI_ghash_insert(global_keyconfigpreftype_hash, kpt_rt->idname, kpt_rt);
99 }
100 
102 {
104 }
105 
107 {
110 }
111 
113 {
116 }
117 
120 /* -------------------------------------------------------------------- */
124 /* Set select mouse, for versioning code. */
125 void BKE_keyconfig_pref_set_select_mouse(UserDef *userdef, int value, bool override)
126 {
128  IDProperty *idprop = IDP_GetPropertyFromGroup(kpt->prop, "select_mouse");
129  if (!idprop) {
130  IDPropertyTemplate tmp = {
131  .i = value,
132  };
133  IDP_AddToGroup(kpt->prop, IDP_New(IDP_INT, &tmp, "select_mouse"));
134  }
135  else if (override) {
136  IDP_Int(idprop) = value;
137  }
138 }
139 
141 {
143  if (kmi->ptr) {
144  MEM_freeN(kmi->ptr);
145  }
146  MEM_freeN(kmi);
147 }
148 
150 {
151  if (kmdi->add_item) {
152  keymap_item_free(kmdi->add_item);
153  }
154  if (kmdi->remove_item) {
156  }
157  MEM_freeN(kmdi);
158 }
159 
161  const struct wmKeyConfigFilterItemParams *params,
162  bool (*filter_fn)(wmKeyMapItem *kmi, void *user_data),
163  void *user_data)
164 {
165  if (params->check_diff_item_add || params->check_diff_item_remove) {
166  for (wmKeyMapDiffItem *kmdi = keymap->diff_items.first, *kmdi_next; kmdi; kmdi = kmdi_next) {
167  kmdi_next = kmdi->next;
168  bool remove = false;
169 
170  if (params->check_diff_item_add) {
171  if (kmdi->add_item) {
172  if (filter_fn(kmdi->add_item, user_data)) {
173  remove = true;
174  }
175  }
176  }
177 
178  if (!remove && params->check_diff_item_remove) {
179  if (kmdi->remove_item) {
180  if (filter_fn(kmdi->remove_item, user_data)) {
181  remove = true;
182  }
183  }
184  }
185 
186  if (remove) {
187  BLI_remlink(&keymap->diff_items, kmdi);
188  keymap_diff_item_free(kmdi);
189  }
190  }
191  }
192 
193  if (params->check_item) {
194  for (wmKeyMapItem *kmi = keymap->items.first, *kmi_next; kmi; kmi = kmi_next) {
195  kmi_next = kmi->next;
196  if (filter_fn(kmi, user_data)) {
197  BLI_remlink(&keymap->items, kmi);
198  keymap_item_free(kmi);
199  }
200  }
201  }
202 }
203 
209  const struct wmKeyConfigFilterItemParams *params,
210  bool (*filter_fn)(wmKeyMapItem *kmi, void *user_data),
211  void *user_data)
212 {
213  LISTBASE_FOREACH (wmKeyMap *, keymap, &userdef->user_keymaps) {
215  }
216 }
217 
#define IDP_Int(prop)
Definition: BKE_idprop.h:154
void IDP_FreeProperty(struct IDProperty *prop)
Definition: idprop.c:1040
bool IDP_AddToGroup(struct IDProperty *group, struct IDProperty *prop) ATTR_NONNULL()
Definition: idprop.c:643
struct IDProperty * IDP_GetPropertyFromGroup(const struct IDProperty *prop, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
struct IDProperty * IDP_New(const char type, const IDPropertyTemplate *val, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition: idprop.c:907
struct wmKeyConfigPrefType_Runtime wmKeyConfigPrefType_Runtime
Definition: BKE_keyconfig.h:43
#define BLI_assert(a)
Definition: BLI_assert.h:58
GHash * BLI_ghash_str_new(const char *info) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT
bool BLI_ghash_remove(GHash *gh, const void *key, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:900
void BLI_ghash_insert(GHash *gh, void *key, void *val)
Definition: BLI_ghash.c:756
void BLI_ghash_free(GHash *gh, GHashKeyFreeFP keyfreefp, GHashValFreeFP valfreefp)
Definition: BLI_ghash.c:1008
void * BLI_ghash_lookup(GHash *gh, const void *key) ATTR_WARN_UNUSED_RESULT
Definition: BLI_ghash.c:803
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
void * BLI_findstring(const struct ListBase *listbase, const char *id, const int offset) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
void BLI_remlink(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:133
#define STRNCPY(dst, src)
Definition: BLI_string.h:163
@ IDP_INT
Definition: DNA_ID.h:98
@ IDP_GROUP
Definition: DNA_ID.h:101
These structs are the foundation for all linked lists in the library system.
#define WM_KEYCONFIG_STR_DEFAULT
Read Guarded memory(de)allocation.
void * user_data
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void BKE_keyconfig_pref_type_init(void)
Definition: keyconfig.c:106
void BKE_keyconfig_pref_type_free(void)
Definition: keyconfig.c:112
void BKE_keyconfig_keymap_filter_item(wmKeyMap *keymap, const struct wmKeyConfigFilterItemParams *params, bool(*filter_fn)(wmKeyMapItem *kmi, void *user_data), void *user_data)
Definition: keyconfig.c:160
void BKE_keyconfig_pref_filter_items(struct UserDef *userdef, const struct wmKeyConfigFilterItemParams *params, bool(*filter_fn)(wmKeyMapItem *kmi, void *user_data), void *user_data)
Definition: keyconfig.c:208
wmKeyConfigPref * BKE_keyconfig_pref_ensure(UserDef *userdef, const char *kc_idname)
Definition: keyconfig.c:47
void BKE_keyconfig_pref_type_remove(const wmKeyConfigPrefType_Runtime *kpt_rt)
Definition: keyconfig.c:101
void BKE_keyconfig_pref_set_select_mouse(UserDef *userdef, int value, bool override)
Definition: keyconfig.c:125
static void keymap_diff_item_free(wmKeyMapDiffItem *kmdi)
Definition: keyconfig.c:149
static GHash * global_keyconfigpreftype_hash
Definition: keyconfig.c:71
static void keymap_item_free(wmKeyMapItem *kmi)
Definition: keyconfig.c:140
wmKeyConfigPrefType_Runtime * BKE_keyconfig_pref_type_find(const char *idname, bool quiet)
Definition: keyconfig.c:73
void BKE_keyconfig_pref_type_add(wmKeyConfigPrefType_Runtime *kpt_rt)
Definition: keyconfig.c:96
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void * first
Definition: DNA_listBase.h:47
struct ListBase user_keymaps
struct ListBase user_keyconfig_prefs
struct PointerRNA * ptr
struct wmKeyMapItem * next