Blender V5.3
space_console.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cstdio>
11#include <cstring>
12
13#include "DNA_space_types.h"
14
15#include "MEM_guardedalloc.h"
16
17#include "BLI_listbase.hh"
18#include "BLI_string.hh"
19#include "BLI_string_utf8.hh"
20
21#include "BKE_context.hh"
22#include "BKE_screen.hh"
23
24#include "ED_screen.hh"
25#include "ED_space_api.hh"
26
27#include "RNA_access.hh"
28#include "RNA_path.hh"
29
30#include "WM_api.hh"
31#include "WM_types.hh"
32
33#include "UI_resources.hh"
34#include "UI_view2d.hh"
35
36#include "BLO_read_write.hh"
37
38#include "console_intern.hh" /* own include */
39
40namespace blender {
41
42/* ******************** default callbacks for console space ***************** */
43
44static SpaceLink *console_create(const ScrArea * /*area*/, const Scene * /*scene*/)
45{
46 ARegion *region;
47 SpaceConsole *sconsole;
48
49 sconsole = MEM_new<SpaceConsole>("initconsole");
50 sconsole->spacetype = SPACE_CONSOLE;
51
52 sconsole->line_height = 14;
53
54 sconsole->runtime = MEM_new<SpaceConsole_Runtime>(__func__);
55
56 /* header */
57 region = BKE_area_region_new();
58
59 BLI_addtail(&sconsole->regionbase, region);
62
63 /* main region */
64 region = BKE_area_region_new();
65
66 BLI_addtail(&sconsole->regionbase, region);
68
69 /* keep in sync with info */
71 region->v2d.align |= V2D_ALIGN_NO_NEG_X | V2D_ALIGN_NO_NEG_Y; /* align bottom left */
72 region->v2d.keepofs |= V2D_LOCKOFS_X;
75 region->v2d.minzoom = region->v2d.maxzoom = 1.0f;
76
77 /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
78 // region->v2d.keepzoom = (V2D_KEEPASPECT|V2D_LIMITZOOM);
79
80 return reinterpret_cast<SpaceLink *>(sconsole);
81}
82
83/* Doesn't free the space-link itself. */
84static void console_free(SpaceLink *sl)
85{
86 SpaceConsole *sc = reinterpret_cast<SpaceConsole *>(sl);
87
88 while (sc->scrollback.first_) {
89 console_scrollback_free(sc, sc->scrollback.first());
90 }
91
92 while (sc->history.first_) {
93 console_history_free(sc, sc->history.first());
94 }
95
96 MEM_delete(sc->runtime);
97}
98
99/* spacetype; init callback */
100static void console_init(wmWindowManager * /*wm*/, ScrArea * /*area*/) {}
101
103{
104 SpaceConsole *sconsolen = MEM_dupalloc(reinterpret_cast<SpaceConsole *>(sl));
105
106 /* clear or remove stuff from old */
107
108 /* TODO: duplicate?, then we also need to duplicate the py namespace. */
109 sconsolen->scrollback.clear_no_delete();
110 sconsolen->history.clear_no_delete();
111
112 /* Add its own runtime data. */
113 sconsolen->runtime = MEM_new<SpaceConsole_Runtime>(__func__);
114
115 return reinterpret_cast<SpaceLink *>(sconsolen);
116}
117
118/* add handlers, stuff you only do once or on area/region changes */
120{
121 wmKeyMap *keymap;
122 ListBaseT<wmDropBox> *lb;
123
124 const float prev_y_min = region->v2d.cur.ymin; /* so re-sizing keeps the cursor visible */
125
126 view2d_region_reinit(&region->v2d, ui::V2D_COMMONVIEW_CUSTOM, region->winx, region->winy);
127
128 /* always keep the bottom part of the view aligned, less annoying */
129 if (prev_y_min != region->v2d.cur.ymin) {
130 const float cur_y_range = BLI_rctf_size_y(&region->v2d.cur);
131 region->v2d.cur.ymin = prev_y_min;
132 region->v2d.cur.ymax = prev_y_min + cur_y_range;
133 }
134
135 /* own keymap */
136 keymap = WM_keymap_ensure(wm->runtime->defaultconf, "Console", SPACE_CONSOLE, RGN_TYPE_WINDOW);
138
139 /* Include after "Console" so cursor motion keys such as "Home" isn't overridden. */
140 keymap = WM_keymap_ensure(
141 wm->runtime->defaultconf, "View2D Buttons List", SPACE_EMPTY, RGN_TYPE_WINDOW);
143
144 /* add drop boxes */
146
148}
149
150/* same as 'text_cursor' */
151static void console_cursor(wmWindow *win, ScrArea * /*area*/, ARegion *region)
152{
153 int wmcursor = WM_CURSOR_TEXT_EDIT;
154 const wmEvent *event = win->runtime->eventstate;
155 if (ui::view2d_mouse_in_scrollers(region, &region->v2d, event->xy)) {
156 wmcursor = WM_CURSOR_DEFAULT;
157 }
158
159 WM_cursor_set(win, wmcursor);
160}
161
162/* ************* dropboxes ************* */
163
164static bool console_drop_id_poll(bContext * /*C*/, wmDrag *drag, const wmEvent * /*event*/)
165{
166 return WM_drag_get_local_ID(drag, 0) != nullptr;
167}
168
169static void console_drop_id_copy(bContext * /*C*/, wmDrag *drag, wmDropBox *drop)
170{
171 ID *id = WM_drag_get_local_ID(drag, 0);
172
173 /* copy drag path to properties */
174 std::string text = RNA_path_full_ID_py(id);
175 RNA_string_set(drop->ptr, "text", text.c_str());
176}
177
178static bool console_drop_path_poll(bContext * /*C*/, wmDrag *drag, const wmEvent * /*event*/)
179{
180 return (drag->type == WM_DRAG_PATH);
181}
182
183static void console_drop_path_copy(bContext * /*C*/, wmDrag *drag, wmDropBox *drop)
184{
185 char pathname[FILE_MAX + 2];
186 SNPRINTF(pathname, "\"%s\"", WM_drag_get_single_path(drag));
187 RNA_string_set(drop->ptr, "text", pathname);
188}
189
190static bool console_drop_string_poll(bContext * /*C*/, wmDrag *drag, const wmEvent * /*event*/)
191{
192 return (drag->type == WM_DRAG_STRING);
193}
194
195static void console_drop_string_copy(bContext * /*C*/, wmDrag *drag, wmDropBox *drop)
196{
197 /* NOTE(@ideasman42): Only a single line is supported, multiple lines could be supported
198 * but this implies executing all lines except for the last. While we could consider that,
199 * there are some security implications for this, so just drop one line for now. */
200 std::string str = WM_drag_get_string_firstline(drag);
201 RNA_string_set(drop->ptr, "text", str.c_str());
202}
203
204/* this region dropbox definition */
205static void console_dropboxes()
206{
207 ListBaseT<wmDropBox> *lb = WM_dropboxmap_find("Console", SPACE_CONSOLE, RGN_TYPE_WINDOW);
208
210 lb, "CONSOLE_OT_insert", console_drop_id_poll, console_drop_id_copy, nullptr, nullptr);
212 lb, "CONSOLE_OT_insert", console_drop_path_poll, console_drop_path_copy, nullptr, nullptr);
214 "CONSOLE_OT_insert",
217 nullptr,
218 nullptr);
219}
220
221/* ************* end drop *********** */
222
223#ifdef WITH_INPUT_IME
224static std::optional<ARegionIMECursorState> console_main_region_cursor_ime(
225 wmWindow * /*win*/, const ScrArea *area, const ARegion *region, ARegionIMECursor *r_cursor)
226{
227 /* The position is pending during View2D navigation (pan, zoom, scroll). */
228 if (region->v2d.flag & V2D_IS_NAVIGATING) {
230 }
231 SpaceConsole *sc = area->spacedata.first_as<SpaceConsole>();
232 /* Font metrics are cached during draw; zero means the region hasn't been drawn yet. */
233 const int line_height = sc->runtime->line_height_px;
234 if (line_height == 0) {
236 }
237 const ConsoleLine *cl = sc->history.last();
238 if (cl == nullptr) {
239 return std::nullopt;
240 }
241 const std::optional<blender::int2> xy_region = console_cursor_region_xy_get(
242 sc, region, cl->cursor);
243 if (!xy_region) {
244 return std::nullopt;
245 }
246 /* Convert to pixel space, X is pinned by #V2D_LOCKOFS_X. */
247 const int2 xy = {xy_region->x, xy_region->y - int(region->v2d.cur.ymin)};
248
249 /* Extend up by the line height.
250 * The caller clamps to the region since the cursor may be scrolled out of view. */
251 r_cursor->rect = {
252 .xmin = xy.x,
253 .xmax = xy.x,
254 .ymin = xy.y,
255 .ymax = xy.y + line_height,
256 };
257 /* The console draws text smaller than the line height, see #textview_font_begin. */
258 r_cursor->font_size = int(0.8f * float(line_height));
260}
261
262#endif
263
264static void console_main_region_draw(const bContext *C, ARegion *region)
265{
266 /* draw entirely, view changes should be handled here */
268 View2D *v2d = &region->v2d;
269
270 if (sc->scrollback.is_empty()) {
271 WM_operator_name_call(const_cast<bContext *>(C),
272 "CONSOLE_OT_banner",
274 nullptr,
275 nullptr);
276 }
277
278 /* clear and setup matrix */
280
281 /* Works best with no view2d matrix set. */
283
284 /* data... */
285
286 console_history_verify(C); /* make sure we have some command line */
287 console_textview_main(sc, region);
288
289 /* reset view matrix */
291
292 /* scrollers */
293 ui::view2d_scrollers_draw(v2d, nullptr);
294}
295
320
321static void console_keymap(wmKeyConfig *keyconf)
322{
323 WM_keymap_ensure(keyconf, "Console", SPACE_CONSOLE, RGN_TYPE_WINDOW);
324}
325
326/****************** header region ******************/
327
328/* add handlers, stuff you only do once or on area/region changes */
330{
331 ED_region_header_init(region);
332}
333
334static void console_header_region_draw(const bContext *C, ARegion *region)
335{
336 ED_region_header(C, region);
337}
338
340{
341 ScrArea *area = params->area;
342 ARegion *region = params->region;
343 const wmNotifier *wmn = params->notifier;
344
345 /* context changes */
346 switch (wmn->category) {
347 case NC_SPACE: {
348 if (wmn->data == ND_SPACE_CONSOLE) {
349 if (wmn->action == NA_EDITED) {
350 if ((wmn->reference && area) && (wmn->reference == area->spacedata.first_)) {
351 /* we've modified the geometry (font size), re-calculate rect */
352 console_textview_update_rect(static_cast<SpaceConsole *>(wmn->reference), region);
353 ED_region_tag_redraw(region);
354 }
355 }
356 else {
357 /* generic redraw request */
358 ED_region_tag_redraw(region);
359 }
360 }
361 break;
362 }
363 }
364}
365
367{
368 SpaceConsole *sconsole = reinterpret_cast<SpaceConsole *>(sl);
369
370 sconsole->runtime = MEM_new<SpaceConsole_Runtime>(__func__);
371
372 BLO_read_struct_list(reader, ConsoleLine, &sconsole->scrollback);
373 BLO_read_struct_list(reader, ConsoleLine, &sconsole->history);
374
375 /* Comma expressions, (e.g. expr1, expr2, expr3) evaluate each expression,
376 * from left to right. the right-most expression sets the result of the comma
377 * expression as a whole. */
378 for (ConsoleLine &cl : sconsole->history.items_mutable()) {
379 if (BLO_read_array(reader, &cl.line, size_t(cl.len) + 1) && cl.line) {
380 /* The allocated length is not written, so reset here. */
381 cl.len_alloc = cl.len + 1;
382 }
383 else {
384 BLI_remlink(&sconsole->history, &cl);
385 MEM_delete(&cl);
386 }
387 }
388}
389
391{
392 SpaceConsole *con = reinterpret_cast<SpaceConsole *>(sl);
393
394 for (ConsoleLine &cl : con->history) {
395 /* 'len_alloc' is invalid on write, set from 'len' on read */
396 writer->write_struct(&cl);
397 writer->write_char_array(size_t(cl.len) + 1, cl.line);
398 }
399 writer->write_struct_cast<SpaceConsole>(sl, [](BlendStructWriter<SpaceConsole> &struct_writer) {
400 struct_writer.shallow_data.runtime = nullptr;
401 });
402}
403
405{
406 std::unique_ptr<SpaceType> st = std::make_unique<SpaceType>();
407 ARegionType *art;
408
409 st->spaceid = SPACE_CONSOLE;
410 STRNCPY_UTF8(st->name, "Console");
411 st->struct_size = sizeof(SpaceConsole);
412
413 st->create = console_create;
414 st->free = console_free;
415 st->init = console_init;
416 st->duplicate = console_duplicate;
417 st->operatortypes = console_operatortypes;
418 st->keymap = console_keymap;
419 st->dropboxes = console_dropboxes;
420 st->blend_read_data = console_blend_read_data;
421 st->blend_write = console_space_blend_write;
422
423 /* regions: main window */
424 art = MEM_new_zeroed<ARegionType>("spacetype console region");
427
430 art->cursor = console_cursor;
431 art->event_cursor = true;
433#ifdef WITH_INPUT_IME
434 art->cursor_ime = console_main_region_cursor_ime;
435#endif
436
437 BLI_addhead(&st->regiontypes, art);
438
439 /* regions: header */
440 art = MEM_new_zeroed<ARegionType>("spacetype console region");
442 art->prefsizey = HEADERY;
444
447
448 BLI_addhead(&st->regiontypes, art);
449
450 BKE_spacetype_register(std::move(st));
451}
452
453} // namespace blender
#define FILE_MAX
#define SNPRINTF(dst, format,...)
#define STRNCPY_UTF8(dst, src)
#define BLO_read_struct_list(reader, struct_name, list)
#define HEADERY
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define NA_EDITED
Definition WM_types.hh:634
#define ND_SPACE_CONSOLE
Definition WM_types.hh:562
#define NC_SPACE
Definition WM_types.hh:405
#define str(s)
void frame_buffer_clear(int colorid)
void view2d_view_ortho(const View2D *v2d)
Definition view2d.cc:1080
void view2d_view_restore(const bContext *C)
Definition view2d.cc:1147
char char view2d_mouse_in_scrollers(const ARegion *region, const View2D *v2d, const int xy[2]) ATTR_NONNULL(1
Definition view2d.cc:2044
void view2d_scrollers_draw(View2D *v2d, const rcti *mask_custom)
Definition view2d.cc:1487
@ V2D_COMMONVIEW_CUSTOM
Definition UI_view2d.hh:56
BMesh Iterators.
UserDef U
Definition blender.cc:58
void CONSOLE_OT_history_cycle(wmOperatorType *ot)
void WM_operatortype_append(void(*opfunc)(wmOperatorType *))
const char * WM_drag_get_single_path(const wmDrag *drag)
wmDropBox * WM_dropbox_add(ListBaseT< wmDropBox > *lb, const char *idname, bool(*poll)(bContext *C, wmDrag *drag, const wmEvent *event), void(*copy)(bContext *C, wmDrag *drag, wmDropBox *drop), void(*cancel)(Main *bmain, wmDrag *drag, wmDropBox *drop), WMDropboxTooltipFunc tooltip)
static void console_header_region_draw(const bContext *C, ARegion *region)
void CONSOLE_OT_scrollback_append(wmOperatorType *ot)
SpaceConsole * CTX_wm_space_console(const bContext *C)
void BLI_addhead(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:98
static void console_header_region_init(wmWindowManager *, ARegion *region)
void CONSOLE_OT_paste(wmOperatorType *ot)
static void console_drop_id_copy(bContext *, wmDrag *drag, wmDropBox *drop)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
void CONSOLE_OT_select_set(wmOperatorType *ot)
static void console_space_blend_write(BlendWriter *writer, SpaceLink *sl)
static void console_drop_string_copy(bContext *, wmDrag *drag, wmDropBox *drop)
static bool console_drop_string_poll(bContext *, wmDrag *drag, const wmEvent *)
@ WM_DRAG_STRING
Definition WM_types.hh:1311
@ WM_DRAG_PATH
Definition WM_types.hh:1302
void CONSOLE_OT_select_all(wmOperatorType *ot)
ConsoleLine * console_history_verify(const bContext *C)
static bool console_drop_path_poll(bContext *, wmDrag *drag, const wmEvent *)
void ED_spacetype_console()
static SpaceLink * console_create(const ScrArea *, const Scene *)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
void CONSOLE_OT_select_word(wmOperatorType *ot)
static void console_keymap(wmKeyConfig *keyconf)
void CONSOLE_OT_copy(wmOperatorType *ot)
static void console_cursor(wmWindow *win, ScrArea *, ARegion *region)
void CONSOLE_OT_unindent(wmOperatorType *ot)
void CONSOLE_OT_clear_line(wmOperatorType *ot)
static void console_init(wmWindowManager *, ScrArea *)
wmEventHandler_Dropbox * WM_event_add_dropbox_handler(ListBaseT< wmEventHandler > *handlers, ListBaseT< wmDropBox > *dropboxes)
VecBase< int32_t, 2 > int2
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
void BLI_remlink(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:138
std::optional< blender::int2 > console_cursor_region_xy_get(const SpaceConsole *sc, const ARegion *region, const int offset)
wmEventHandler_Keymap * WM_event_add_keymap_handler_v2d_mask(ListBaseT< wmEventHandler > *handlers, wmKeyMap *keymap)
std::string RNA_path_full_ID_py(ID *id)
Definition rna_path.cc:1290
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
static void console_free(SpaceLink *sl)
wmKeyMap * WM_keymap_ensure(wmKeyConfig *keyconf, const char *idname, int spaceid, int regionid)
Definition wm_keymap.cc:935
void CONSOLE_OT_history_append(wmOperatorType *ot)
void console_history_free(SpaceConsole *sc, ConsoleLine *cl)
void CONSOLE_OT_indent(wmOperatorType *ot)
wmOperatorStatus WM_operator_name_call(bContext *C, const char *opstring, wm::OpCallContext context, PointerRNA *properties, const wmEvent *event)
void console_textview_update_rect(SpaceConsole *sc, ARegion *region)
wmEventHandler_Keymap * WM_event_add_keymap_handler(ListBaseT< wmEventHandler > *handlers, wmKeyMap *keymap)
static void console_main_region_listener(const wmRegionListenerParams *params)
ARegion * BKE_area_region_new()
Definition screen.cc:522
static SpaceLink * console_duplicate(SpaceLink *sl)
bool BLO_read_array(BlendDataReader *reader, T **ptr_p, const SizeT array_size, const int elems=1)
@ ED_KEYMAP_UI
Definition ED_screen.hh:852
@ ED_KEYMAP_HEADER
Definition ED_screen.hh:858
@ ED_KEYMAP_VIEW2D
Definition ED_screen.hh:855
static bool console_drop_id_poll(bContext *, wmDrag *drag, const wmEvent *)
void CONSOLE_OT_clear(wmOperatorType *ot)
void CONSOLE_OT_indent_or_autocomplete(wmOperatorType *ot)
@ V2D_SCROLL_VERTICAL_HIDE
@ WM_CURSOR_TEXT_EDIT
Definition wm_cursors.hh:18
@ WM_CURSOR_DEFAULT
Definition wm_cursors.hh:17
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void BKE_spacetype_register(std::unique_ptr< SpaceType > st)
Definition screen.cc:379
void CONSOLE_OT_move(wmOperatorType *ot)
void ED_region_header_init(ARegion *region)
Definition area.cc:4406
void CONSOLE_OT_insert(wmOperatorType *ot)
static void console_drop_path_copy(bContext *, wmDrag *drag, wmDropBox *drop)
static void console_blend_read_data(BlendDataReader *reader, SpaceLink *sl)
void console_textview_main(SpaceConsole *sc, const ARegion *region)
void WM_cursor_set(wmWindow *win, int curs)
static void console_dropboxes()
std::string WM_drag_get_string_firstline(const wmDrag *drag)
ID * WM_drag_get_local_ID(const wmDrag *drag, short idcode)
ListBaseT< wmDropBox > * WM_dropboxmap_find(const char *idname, int spaceid, int regionid)
void console_scrollback_free(SpaceConsole *sc, ConsoleLine *cl)
void ED_region_header(const bContext *C, ARegion *region)
Definition area.cc:4391
static void console_main_region_init(wmWindowManager *wm, ARegion *region)
static void console_main_region_draw(const bContext *C, ARegion *region)
static void console_operatortypes()
void CONSOLE_OT_delete(wmOperatorType *ot)
void(*) cursor(wmWindow *win, ScrArea *area, ARegion *region)
void(*) draw(const bContext *C, ARegion *region)
void(*) listener(const wmRegionListenerParams *params)
std::optional< ARegionIMECursorState >(*) cursor_ime(wmWindow *win, const ScrArea *area, const ARegion *region, ARegionIMECursor *r_cursor)
void(*) init(wmWindowManager *wm, ARegion *region)
bke::ARegionRuntime * runtime
eRegion_Alignment alignment
eRegion_Type regiontype
void write_char_array(int64_t num, const char *data)
void write_struct_cast(const void *data, BlendStructWriterTypedFn< T > fn=nullptr)
void write_struct(const T *data, BlendStructWriterTypedFn< T > fn=nullptr)
ListBaseT< SpaceLink > spacedata
ListBaseT< ARegion > regionbase
ListBaseT< ConsoleLine > scrollback
SpaceConsole_Runtime * runtime
ListBaseT< ConsoleLine > history
eView2D_KeepTot keeptot
eView2D_KeepOfs keepofs
eView2D_KeepZoom keepzoom
eView2D_Scroll scroll
eView2D_Align align
ListBaseT< wmEventHandler > handlers
eWM_DragDataType type
Definition WM_types.hh:1424
PointerRNA * ptr
Definition WM_types.hh:1524
unsigned int action
Definition WM_types.hh:371
unsigned int category
Definition WM_types.hh:371
unsigned int data
Definition WM_types.hh:371
bke::WindowRuntime * runtime