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
17 /** \file
18 * \ingroup bke
19 */
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "BLI_listbase.h"
25 #include "BLI_string.h"
26 #include "BLI_string_utils.h"
27 #include "BLI_utildefines.h"
28
29 #include "BLT_translation.h"
30
31 #include "BKE_global.h"
32 #include "BKE_idprop.h"
33 #include "BKE_idtype.h"
34 #include "BKE_lib_id.h"
35 #include "BKE_lib_query.h"
36 #include "BKE_main.h"
37 #include "BKE_object.h"
38 #include "BKE_scene.h"
39 #include "BKE_workspace.h"
40
41 #include "DNA_object_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_screen_types.h"
44 #include "DNA_windowmanager_types.h"
45 #include "DNA_workspace_types.h"
46
47 #include "DEG_depsgraph.h"
48
49 #include "MEM_guardedalloc.h"
50
51 /* -------------------------------------------------------------------- */
52
workspace_free_data(ID * id)53 static void workspace_free_data(ID *id)
54 {
55 WorkSpace *workspace = (WorkSpace *)id;
56
57 BKE_workspace_relations_free(&workspace->hook_layout_relations);
58
59 BLI_freelistN(&workspace->owner_ids);
60 BLI_freelistN(&workspace->layouts);
61
62 while (!BLI_listbase_is_empty(&workspace->tools)) {
63 BKE_workspace_tool_remove(workspace, workspace->tools.first);
64 }
65
66 MEM_SAFE_FREE(workspace->status_text);
67 }
68
workspace_foreach_id(ID * id,LibraryForeachIDData * data)69 static void workspace_foreach_id(ID *id, LibraryForeachIDData *data)
70 {
71 WorkSpace *workspace = (WorkSpace *)id;
72
73 LISTBASE_FOREACH (WorkSpaceLayout *, layout, &workspace->layouts) {
74 BKE_LIB_FOREACHID_PROCESS(data, layout->screen, IDWALK_CB_USER);
75 }
76 }
77
78 IDTypeInfo IDType_ID_WS = {
79 .id_code = ID_WS,
80 .id_filter = FILTER_ID_WS,
81 .main_listbase_index = INDEX_ID_WS,
82 .struct_size = sizeof(WorkSpace),
83 .name = "WorkSpace",
84 .name_plural = "workspaces",
85 .translation_context = BLT_I18NCONTEXT_ID_WORKSPACE,
86 .flags = IDTYPE_FLAGS_NO_COPY | IDTYPE_FLAGS_NO_MAKELOCAL | IDTYPE_FLAGS_NO_ANIMDATA,
87
88 .init_data = NULL,
89 .copy_data = NULL,
90 .free_data = workspace_free_data,
91 .make_local = NULL,
92 .foreach_id = workspace_foreach_id,
93 .foreach_cache = NULL,
94
95 .blend_write = NULL,
96 .blend_read_data = NULL,
97 .blend_read_lib = NULL,
98 .blend_read_expand = NULL,
99 };
100
101 /** \name Internal Utils
102 * \{ */
103
workspace_layout_name_set(WorkSpace * workspace,WorkSpaceLayout * layout,const char * new_name)104 static void workspace_layout_name_set(WorkSpace *workspace,
105 WorkSpaceLayout *layout,
106 const char *new_name)
107 {
108 BLI_strncpy(layout->name, new_name, sizeof(layout->name));
109 BLI_uniquename(&workspace->layouts,
110 layout,
111 "Layout",
112 '.',
113 offsetof(WorkSpaceLayout, name),
114 sizeof(layout->name));
115 }
116
117 /**
118 * This should only be used directly when it is to be expected that there isn't
119 * a layout within \a workspace that wraps \a screen. Usually - especially outside
120 * of BKE_workspace - #BKE_workspace_layout_find should be used!
121 */
workspace_layout_find_exec(const WorkSpace * workspace,const bScreen * screen)122 static WorkSpaceLayout *workspace_layout_find_exec(const WorkSpace *workspace,
123 const bScreen *screen)
124 {
125 return BLI_findptr(&workspace->layouts, screen, offsetof(WorkSpaceLayout, screen));
126 }
127
workspace_relation_add(ListBase * relation_list,void * parent,const int parentid,void * data)128 static void workspace_relation_add(ListBase *relation_list,
129 void *parent,
130 const int parentid,
131 void *data)
132 {
133 WorkSpaceDataRelation *relation = MEM_callocN(sizeof(*relation), __func__);
134 relation->parent = parent;
135 relation->parentid = parentid;
136 relation->value = data;
137 /* add to head, if we switch back to it soon we find it faster. */
138 BLI_addhead(relation_list, relation);
139 }
workspace_relation_remove(ListBase * relation_list,WorkSpaceDataRelation * relation)140 static void workspace_relation_remove(ListBase *relation_list, WorkSpaceDataRelation *relation)
141 {
142 BLI_remlink(relation_list, relation);
143 MEM_freeN(relation);
144 }
145
workspace_relation_ensure_updated(ListBase * relation_list,void * parent,const int parentid,void * data)146 static void workspace_relation_ensure_updated(ListBase *relation_list,
147 void *parent,
148 const int parentid,
149 void *data)
150 {
151 WorkSpaceDataRelation *relation = BLI_listbase_bytes_find(
152 relation_list, &parentid, sizeof(parentid), offsetof(WorkSpaceDataRelation, parentid));
153 if (relation != NULL) {
154 relation->parent = parent;
155 relation->value = data;
156 /* reinsert at the head of the list, so that more commonly used relations are found faster. */
157 BLI_remlink(relation_list, relation);
158 BLI_addhead(relation_list, relation);
159 }
160 else {
161 /* no matching relation found, add new one */
162 workspace_relation_add(relation_list, parent, parentid, data);
163 }
164 }
165
workspace_relation_get_data_matching_parent(const ListBase * relation_list,const void * parent)166 static void *workspace_relation_get_data_matching_parent(const ListBase *relation_list,
167 const void *parent)
168 {
169 WorkSpaceDataRelation *relation = BLI_findptr(
170 relation_list, parent, offsetof(WorkSpaceDataRelation, parent));
171 if (relation != NULL) {
172 return relation->value;
173 }
174
175 return NULL;
176 }
177
178 /**
179 * Checks if \a screen is already used within any workspace. A screen should never be assigned to
180 * multiple WorkSpaceLayouts, but that should be ensured outside of the BKE_workspace module
181 * and without such checks.
182 * Hence, this should only be used as assert check before assigning a screen to a workspace.
183 */
184 #ifndef NDEBUG
workspaces_is_screen_used(const Main * bmain,bScreen * screen)185 static bool workspaces_is_screen_used
186 #else
187 static bool UNUSED_FUNCTION(workspaces_is_screen_used)
188 #endif
189 (const Main *bmain, bScreen *screen)
190 {
191 for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) {
192 if (workspace_layout_find_exec(workspace, screen)) {
193 return true;
194 }
195 }
196
197 return false;
198 }
199
200 /** \} */
201
202 /* -------------------------------------------------------------------- */
203 /** \name Create, Delete, Init
204 * \{ */
205
BKE_workspace_add(Main * bmain,const char * name)206 WorkSpace *BKE_workspace_add(Main *bmain, const char *name)
207 {
208 WorkSpace *new_workspace = BKE_id_new(bmain, ID_WS, name);
209 id_us_ensure_real(&new_workspace->id);
210 return new_workspace;
211 }
212
213 /**
214 * Remove \a workspace by freeing itself and its data. This is a higher-level wrapper that
215 * calls #workspace_free_data (through #BKE_id_free) to free the workspace data, and frees
216 * other data-blocks owned by \a workspace and its layouts (currently that is screens only).
217 *
218 * Always use this to remove (and free) workspaces. Don't free non-ID workspace members here.
219 */
BKE_workspace_remove(Main * bmain,WorkSpace * workspace)220 void BKE_workspace_remove(Main *bmain, WorkSpace *workspace)
221 {
222 for (WorkSpaceLayout *layout = workspace->layouts.first, *layout_next; layout;
223 layout = layout_next) {
224 layout_next = layout->next;
225 BKE_workspace_layout_remove(bmain, workspace, layout);
226 }
227 BKE_id_free(bmain, workspace);
228 }
229
BKE_workspace_instance_hook_create(const Main * bmain,const int winid)230 WorkSpaceInstanceHook *BKE_workspace_instance_hook_create(const Main *bmain, const int winid)
231 {
232 WorkSpaceInstanceHook *hook = MEM_callocN(sizeof(WorkSpaceInstanceHook), __func__);
233
234 /* set an active screen-layout for each possible window/workspace combination */
235 for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) {
236 BKE_workspace_active_layout_set(hook, winid, workspace, workspace->layouts.first);
237 }
238
239 return hook;
240 }
BKE_workspace_instance_hook_free(const Main * bmain,WorkSpaceInstanceHook * hook)241 void BKE_workspace_instance_hook_free(const Main *bmain, WorkSpaceInstanceHook *hook)
242 {
243 /* workspaces should never be freed before wm (during which we call this function).
244 * However, when running in background mode, loading a blend file may allocate windows (that need
245 * to be freed) without creating workspaces. This happens in BlendfileLoadingBaseTest. */
246 BLI_assert(!BLI_listbase_is_empty(&bmain->workspaces) || G.background);
247
248 /* Free relations for this hook */
249 for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) {
250 for (WorkSpaceDataRelation *relation = workspace->hook_layout_relations.first, *relation_next;
251 relation;
252 relation = relation_next) {
253 relation_next = relation->next;
254 if (relation->parent == hook) {
255 workspace_relation_remove(&workspace->hook_layout_relations, relation);
256 }
257 }
258 }
259
260 MEM_freeN(hook);
261 }
262
263 /**
264 * Add a new layout to \a workspace for \a screen.
265 */
BKE_workspace_layout_add(Main * bmain,WorkSpace * workspace,bScreen * screen,const char * name)266 WorkSpaceLayout *BKE_workspace_layout_add(Main *bmain,
267 WorkSpace *workspace,
268 bScreen *screen,
269 const char *name)
270 {
271 WorkSpaceLayout *layout = MEM_callocN(sizeof(*layout), __func__);
272
273 BLI_assert(!workspaces_is_screen_used(bmain, screen));
274 #ifndef DEBUG
275 UNUSED_VARS(bmain);
276 #endif
277 layout->screen = screen;
278 id_us_plus(&layout->screen->id);
279 workspace_layout_name_set(workspace, layout, name);
280 BLI_addtail(&workspace->layouts, layout);
281
282 return layout;
283 }
284
BKE_workspace_layout_remove(Main * bmain,WorkSpace * workspace,WorkSpaceLayout * layout)285 void BKE_workspace_layout_remove(Main *bmain, WorkSpace *workspace, WorkSpaceLayout *layout)
286 {
287 /* Screen should usually be set, but we call this from file reading to get rid of invalid
288 * layouts. */
289 if (layout->screen) {
290 id_us_min(&layout->screen->id);
291 BKE_id_free(bmain, layout->screen);
292 }
293 BLI_freelinkN(&workspace->layouts, layout);
294 }
295
BKE_workspace_relations_free(ListBase * relation_list)296 void BKE_workspace_relations_free(ListBase *relation_list)
297 {
298 for (WorkSpaceDataRelation *relation = relation_list->first, *relation_next; relation;
299 relation = relation_next) {
300 relation_next = relation->next;
301 workspace_relation_remove(relation_list, relation);
302 }
303 }
304
305 /** \} */
306
307 /* -------------------------------------------------------------------- */
308 /** \name General Utils
309 * \{ */
310
BKE_workspace_layout_find(const WorkSpace * workspace,const bScreen * screen)311 WorkSpaceLayout *BKE_workspace_layout_find(const WorkSpace *workspace, const bScreen *screen)
312 {
313 WorkSpaceLayout *layout = workspace_layout_find_exec(workspace, screen);
314 if (layout) {
315 return layout;
316 }
317
318 printf(
319 "%s: Couldn't find layout in this workspace: '%s' screen: '%s'. "
320 "This should not happen!\n",
321 __func__,
322 workspace->id.name + 2,
323 screen->id.name + 2);
324
325 return NULL;
326 }
327
328 /**
329 * Find the layout for \a screen without knowing which workspace to look in.
330 * Can also be used to find the workspace that contains \a screen.
331 *
332 * \param r_workspace: Optionally return the workspace that contains the
333 * looked up layout (if found).
334 */
BKE_workspace_layout_find_global(const Main * bmain,const bScreen * screen,WorkSpace ** r_workspace)335 WorkSpaceLayout *BKE_workspace_layout_find_global(const Main *bmain,
336 const bScreen *screen,
337 WorkSpace **r_workspace)
338 {
339 WorkSpaceLayout *layout;
340
341 if (r_workspace) {
342 *r_workspace = NULL;
343 }
344
345 for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) {
346 if ((layout = workspace_layout_find_exec(workspace, screen))) {
347 if (r_workspace) {
348 *r_workspace = workspace;
349 }
350
351 return layout;
352 }
353 }
354
355 return NULL;
356 }
357
358 /**
359 * Circular workspace layout iterator.
360 *
361 * \param callback: Custom function which gets executed for each layout.
362 * Can return false to stop iterating.
363 * \param arg: Custom data passed to each \a callback call.
364 *
365 * \return the layout at which \a callback returned false.
366 */
BKE_workspace_layout_iter_circular(const WorkSpace * workspace,WorkSpaceLayout * start,bool (* callback)(const WorkSpaceLayout * layout,void * arg),void * arg,const bool iter_backward)367 WorkSpaceLayout *BKE_workspace_layout_iter_circular(const WorkSpace *workspace,
368 WorkSpaceLayout *start,
369 bool (*callback)(const WorkSpaceLayout *layout,
370 void *arg),
371 void *arg,
372 const bool iter_backward)
373 {
374 WorkSpaceLayout *iter_layout;
375
376 if (iter_backward) {
377 LISTBASE_CIRCULAR_BACKWARD_BEGIN (&workspace->layouts, iter_layout, start) {
378 if (!callback(iter_layout, arg)) {
379 return iter_layout;
380 }
381 }
382 LISTBASE_CIRCULAR_BACKWARD_END(&workspace->layouts, iter_layout, start);
383 }
384 else {
385 LISTBASE_CIRCULAR_FORWARD_BEGIN (&workspace->layouts, iter_layout, start) {
386 if (!callback(iter_layout, arg)) {
387 return iter_layout;
388 }
389 }
390 LISTBASE_CIRCULAR_FORWARD_END(&workspace->layouts, iter_layout, start);
391 }
392
393 return NULL;
394 }
395
BKE_workspace_tool_remove(struct WorkSpace * workspace,struct bToolRef * tref)396 void BKE_workspace_tool_remove(struct WorkSpace *workspace, struct bToolRef *tref)
397 {
398 if (tref->runtime) {
399 MEM_freeN(tref->runtime);
400 }
401 if (tref->properties) {
402 IDP_FreeProperty(tref->properties);
403 }
404 BLI_remlink(&workspace->tools, tref);
405 MEM_freeN(tref);
406 }
407
BKE_workspace_owner_id_check(const WorkSpace * workspace,const char * owner_id)408 bool BKE_workspace_owner_id_check(const WorkSpace *workspace, const char *owner_id)
409 {
410 if ((*owner_id == '\0') || ((workspace->flags & WORKSPACE_USE_FILTER_BY_ORIGIN) == 0)) {
411 return true;
412 }
413
414 /* We could use hash lookup, for now this list is highly likely under < ~16 items. */
415 return BLI_findstring(&workspace->owner_ids, owner_id, offsetof(wmOwnerID, name)) != NULL;
416 }
417
BKE_workspace_id_tag_all_visible(Main * bmain,int tag)418 void BKE_workspace_id_tag_all_visible(Main *bmain, int tag)
419 {
420 BKE_main_id_tag_listbase(&bmain->workspaces, tag, false);
421 wmWindowManager *wm = bmain->wm.first;
422 LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
423 WorkSpace *workspace = BKE_workspace_active_get(win->workspace_hook);
424 workspace->id.tag |= tag;
425 }
426 }
427
428 /** \} */
429
430 /* -------------------------------------------------------------------- */
431 /** \name Getters/Setters
432 * \{ */
433
BKE_workspace_active_get(WorkSpaceInstanceHook * hook)434 WorkSpace *BKE_workspace_active_get(WorkSpaceInstanceHook *hook)
435 {
436 return hook->active;
437 }
BKE_workspace_active_set(WorkSpaceInstanceHook * hook,WorkSpace * workspace)438 void BKE_workspace_active_set(WorkSpaceInstanceHook *hook, WorkSpace *workspace)
439 {
440 /* DO NOT check for `hook->active == workspace` here. Caller code is supposed to do it if
441 * that optimization is possible and needed.
442 * This code can be called from places where we might have this equality, but still want to
443 * ensure/update the active layout below.
444 * Known case where this is buggy and will crash later due to NULL active layout: reading
445 * a blend file, when the new read workspace ID happens to have the exact same memory address
446 * as when it was saved in the blend file (extremely unlikely, but possible). */
447
448 hook->active = workspace;
449 if (workspace) {
450 WorkSpaceLayout *layout = workspace_relation_get_data_matching_parent(
451 &workspace->hook_layout_relations, hook);
452 if (layout) {
453 hook->act_layout = layout;
454 }
455 }
456 }
457
458 /**
459 * Get the layout that is active for \a hook (which is the visible layout for the active workspace
460 * in \a hook).
461 */
BKE_workspace_active_layout_get(const WorkSpaceInstanceHook * hook)462 WorkSpaceLayout *BKE_workspace_active_layout_get(const WorkSpaceInstanceHook *hook)
463 {
464 return hook->act_layout;
465 }
466
467 /**
468 * Get the layout to be activated should \a workspace become or be the active workspace in \a hook.
469 */
BKE_workspace_active_layout_for_workspace_get(const WorkSpaceInstanceHook * hook,const WorkSpace * workspace)470 WorkSpaceLayout *BKE_workspace_active_layout_for_workspace_get(const WorkSpaceInstanceHook *hook,
471 const WorkSpace *workspace)
472 {
473 /* If the workspace is active, the active layout can be returned, no need for a lookup. */
474 if (hook->active == workspace) {
475 return hook->act_layout;
476 }
477
478 /* Inactive workspace */
479 return workspace_relation_get_data_matching_parent(&workspace->hook_layout_relations, hook);
480 }
481
482 /**
483 * \brief Activate a layout
484 *
485 * Sets \a layout as active for \a workspace when activated through or already active in \a hook.
486 * So when the active workspace of \a hook is \a workspace, \a layout becomes the active layout of
487 * \a hook too. See #BKE_workspace_active_set().
488 *
489 * \a workspace does not need to be active for this.
490 *
491 * WorkSpaceInstanceHook.act_layout should only be modified directly to update the layout pointer.
492 */
BKE_workspace_active_layout_set(WorkSpaceInstanceHook * hook,const int winid,WorkSpace * workspace,WorkSpaceLayout * layout)493 void BKE_workspace_active_layout_set(WorkSpaceInstanceHook *hook,
494 const int winid,
495 WorkSpace *workspace,
496 WorkSpaceLayout *layout)
497 {
498 hook->act_layout = layout;
499 workspace_relation_ensure_updated(&workspace->hook_layout_relations, hook, winid, layout);
500 }
501
BKE_workspace_active_screen_get(const WorkSpaceInstanceHook * hook)502 bScreen *BKE_workspace_active_screen_get(const WorkSpaceInstanceHook *hook)
503 {
504 return hook->act_layout->screen;
505 }
BKE_workspace_active_screen_set(WorkSpaceInstanceHook * hook,const int winid,WorkSpace * workspace,bScreen * screen)506 void BKE_workspace_active_screen_set(WorkSpaceInstanceHook *hook,
507 const int winid,
508 WorkSpace *workspace,
509 bScreen *screen)
510 {
511 /* we need to find the WorkspaceLayout that wraps this screen */
512 WorkSpaceLayout *layout = BKE_workspace_layout_find(hook->active, screen);
513 BKE_workspace_active_layout_set(hook, winid, workspace, layout);
514 }
515
BKE_workspace_layout_name_get(const WorkSpaceLayout * layout)516 const char *BKE_workspace_layout_name_get(const WorkSpaceLayout *layout)
517 {
518 return layout->name;
519 }
BKE_workspace_layout_name_set(WorkSpace * workspace,WorkSpaceLayout * layout,const char * new_name)520 void BKE_workspace_layout_name_set(WorkSpace *workspace,
521 WorkSpaceLayout *layout,
522 const char *new_name)
523 {
524 workspace_layout_name_set(workspace, layout, new_name);
525 }
526
BKE_workspace_layout_screen_get(const WorkSpaceLayout * layout)527 bScreen *BKE_workspace_layout_screen_get(const WorkSpaceLayout *layout)
528 {
529 return layout->screen;
530 }
531
532 /** \} */
533