forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.cpp
744 lines (631 loc) · 17.5 KB
/
app.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
// Aseprite
// Copyright (C) 2018-2021 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/app.h"
#include "app/app_mod.h"
#include "app/check_update.h"
#include "app/cli/app_options.h"
#include "app/cli/cli_processor.h"
#include "app/cli/default_cli_delegate.h"
#include "app/cli/preview_cli_delegate.h"
#include "app/color_spaces.h"
#include "app/color_utils.h"
#include "app/commands/commands.h"
#include "app/console.h"
#include "app/crash/data_recovery.h"
#include "app/extensions.h"
#include "app/file/file.h"
#include "app/file/file_formats_manager.h"
#include "app/file_system.h"
#include "app/gui_xml.h"
#include "app/i18n/strings.h"
#include "app/ini_file.h"
#include "app/log.h"
#include "app/modules.h"
#include "app/modules/gfx.h"
#include "app/modules/gui.h"
#include "app/modules/palettes.h"
#include "app/pref/preferences.h"
#include "app/recent_files.h"
#include "app/resource_finder.h"
#include "app/send_crash.h"
#include "app/site.h"
#include "app/tools/active_tool.h"
#include "app/tools/tool_box.h"
#include "app/ui/backup_indicator.h"
#include "app/ui/color_bar.h"
#include "app/ui/doc_view.h"
#include "app/ui/editor/editor.h"
#include "app/ui/editor/editor_view.h"
#include "app/ui/input_chain.h"
#include "app/ui/keyboard_shortcuts.h"
#include "app/ui/main_window.h"
#include "app/ui/status_bar.h"
#include "app/ui/toolbar.h"
#include "app/ui/workspace.h"
#include "app/ui_context.h"
#include "app/util/clipboard.h"
#include "base/exception.h"
#include "base/fs.h"
#include "base/scoped_lock.h"
#include "base/split_string.h"
#include "doc/sprite.h"
#include "fmt/format.h"
#include "os/display.h"
#include "os/error.h"
#include "os/surface.h"
#include "os/system.h"
#include "render/render.h"
#include "ui/intern.h"
#include "ui/ui.h"
#include "ver/info.h"
#ifdef __APPLE__
#include "os/osx/system.h"
#endif
#include <iostream>
#include <memory>
#ifdef ENABLE_SCRIPTING
#include "app/script/engine.h"
#include "app/shell.h"
#endif
#ifdef ENABLE_STEAM
#include "steam/steam.h"
#endif
namespace app {
using namespace ui;
#ifdef ENABLE_SCRIPTING
namespace {
class ConsoleEngineDelegate : public script::EngineDelegate {
public:
void onConsolePrint(const char* text) override {
m_console.printf("%s\n", text);
}
private:
Console m_console;
};
} // anonymous namespace
#endif // ENABLER_SCRIPTING
class App::CoreModules {
public:
#ifdef ENABLE_UI
typedef app::UIContext ContextT;
#else
typedef app::Context ContextT;
#endif
ConfigModule m_configModule;
ContextT m_context;
};
class App::LoadLanguage {
public:
LoadLanguage(Preferences& pref,
Extensions& exts) {
Strings::createInstance(pref, exts);
}
};
class App::Modules {
public:
LoggerModule m_loggerModule;
FileSystemModule m_file_system_module;
Extensions m_extensions;
// Load main language (after loading the extensions)
LoadLanguage m_loadLanguage;
tools::ToolBox m_toolbox;
tools::ActiveToolManager m_activeToolManager;
Commands m_commands;
#ifdef ENABLE_UI
RecentFiles m_recent_files;
InputChain m_inputChain;
clipboard::ClipboardManager m_clipboardManager;
#endif
// This is a raw pointer because we want to delete it explicitly.
// (e.g. if an exception occurs, the ~Modules() doesn't have to
// delete m_recovery)
app::crash::DataRecovery* m_recovery;
Modules(const bool createLogInDesktop,
Preferences& pref)
: m_loggerModule(createLogInDesktop)
, m_loadLanguage(pref, m_extensions)
, m_activeToolManager(&m_toolbox)
#ifdef ENABLE_UI
, m_recent_files(pref.general.recentItems())
#endif
, m_recovery(nullptr) {
}
~Modules() {
ASSERT(m_recovery == nullptr);
}
app::crash::DataRecovery* recovery() {
return m_recovery;
}
void createDataRecovery(Context* ctx) {
#ifdef ENABLE_DATA_RECOVERY
m_recovery = new app::crash::DataRecovery(ctx);
m_recovery->SessionsListIsReady.connect(
[] {
ui::assert_ui_thread();
auto app = App::instance();
if (app && app->mainWindow()) {
// Notify that the list of sessions is ready.
app->mainWindow()->dataRecoverySessionsAreReady();
}
});
#endif
}
void searchDataRecoverySessions() {
#ifdef ENABLE_DATA_RECOVERY
ASSERT(m_recovery);
if (m_recovery)
m_recovery->launchSearch();
#endif
}
void deleteDataRecovery() {
#ifdef ENABLE_DATA_RECOVERY
if (m_recovery) {
delete m_recovery;
m_recovery = nullptr;
}
#endif
}
};
App* App::m_instance = nullptr;
App::App(AppMod* mod)
: m_mod(mod)
, m_coreModules(nullptr)
, m_modules(nullptr)
, m_legacy(nullptr)
, m_isGui(false)
, m_isShell(false)
#ifdef ENABLE_UI
, m_backupIndicator(nullptr)
#endif
#ifdef ENABLE_SCRIPTING
, m_engine(new script::Engine)
#endif
{
ASSERT(m_instance == NULL);
m_instance = this;
}
int App::initialize(const AppOptions& options)
{
os::System* system = os::instance();
#ifdef ENABLE_UI
m_isGui = options.startUI() && !options.previewCLI();
#else
m_isGui = false;
#endif
m_isShell = options.startShell();
m_coreModules = new CoreModules;
#ifdef _WIN32
if (options.disableWintab() ||
!preferences().experimental.loadWintabDriver() ||
preferences().tablet.api() == "pointer") {
system->setTabletAPI(os::TabletAPI::WindowsPointerInput);
}
else if (preferences().tablet.api() == "wintab_packets")
system->setTabletAPI(os::TabletAPI::WintabPackets);
else // preferences().tablet.api() == "wintab"
system->setTabletAPI(os::TabletAPI::Wintab);
#endif
#ifdef __APPLE__
if (!preferences().general.osxAsyncView())
os::osx_set_async_view(false);
#endif
system->setAppName(get_app_name());
system->setAppMode(m_isGui ? os::AppMode::GUI:
os::AppMode::CLI);
if (m_isGui)
m_uiSystem.reset(new ui::UISystem);
bool createLogInDesktop = false;
switch (options.verboseLevel()) {
case AppOptions::kNoVerbose:
base::set_log_level(ERROR);
break;
case AppOptions::kVerbose:
base::set_log_level(INFO);
break;
case AppOptions::kHighlyVerbose:
base::set_log_level(VERBOSE);
createLogInDesktop = true;
break;
}
initialize_color_spaces(preferences());
// Load modules
m_modules = new Modules(createLogInDesktop, preferences());
m_legacy = new LegacyModules(isGui() ? REQUIRE_INTERFACE: 0);
#ifdef ENABLE_UI
m_brushes.reset(new AppBrushes);
#endif
// Data recovery is enabled only in GUI mode
if (isGui() && preferences().general.dataRecovery())
m_modules->createDataRecovery(context());
if (isPortable())
LOG("APP: Running in portable mode\n");
// Load or create the default palette, or migrate the default
// palette from an old format palette to the new one, etc.
load_default_palette();
#ifdef ENABLE_UI
// Initialize GUI interface
if (isGui()) {
LOG("APP: GUI mode\n");
// Set the ClipboardDelegate impl to copy/paste text in the native
// clipboard from the ui::Entry control.
m_uiSystem->setClipboardDelegate(&m_modules->m_clipboardManager);
// Setup the GUI cursor and redraw screen
ui::set_use_native_cursors(preferences().cursor.useNativeCursor());
ui::set_mouse_cursor_scale(preferences().cursor.cursorScale());
ui::set_mouse_cursor(kArrowCursor);
ui::Manager::getDefault()->invalidate();
// Create the main window.
m_mainWindow.reset(new MainWindow);
if (m_mod)
m_mod->modMainWindow(m_mainWindow.get());
// Data recovery is enabled only in GUI mode
if (preferences().general.dataRecovery())
m_modules->searchDataRecoverySessions();
// Default status of the main window.
app_rebuild_documents_tabs();
m_mainWindow->statusBar()->showDefaultText();
// Show the main window (this is not modal, the code continues)
m_mainWindow->openWindow();
// Redraw the whole screen.
ui::Manager::getDefault()->invalidate();
}
#endif // ENABLE_UI
#ifdef ENABLE_SCRIPTING
// Call the init() function from all plugins
LOG("APP: Initializing scripts...\n");
extensions().executeInitActions();
#endif
// Process options
LOG("APP: Processing options...\n");
int code;
{
std::unique_ptr<CliDelegate> delegate;
if (options.previewCLI())
delegate.reset(new PreviewCliDelegate);
else
delegate.reset(new DefaultCliDelegate);
CliProcessor cli(delegate.get(), options);
code = cli.process(context());
}
LOG("APP: Finish launching...\n");
system->finishLaunching();
return code;
}
void App::run()
{
#ifdef ENABLE_UI
// Run the GUI
if (isGui()) {
#ifdef _WIN32
// How to interpret one finger on Windows tablets.
ui::Manager::getDefault()->getDisplay()
->setInterpretOneFingerGestureAsMouseMovement(
preferences().experimental.oneFingerAsMouseMovement());
#endif
#if !defined(_WIN32) && !defined(__APPLE__)
// Setup app icon for Linux window managers
try {
os::Display* display = os::instance()->defaultDisplay();
os::SurfaceList icons;
for (const int size : { 32, 64, 128 }) {
ResourceFinder rf;
rf.includeDataDir(fmt::format("icons/ase{0}.png", size).c_str());
if (rf.findFirst()) {
os::Surface* surf = os::instance()->loadRgbaSurface(rf.filename().c_str());
if (surf)
icons.push_back(surf);
}
}
display->setIcons(icons);
for (auto surf : icons)
surf->dispose();
}
catch (const std::exception&) {
// Just ignore the exception, we couldn't change the app icon, no
// big deal.
}
#endif
// Initialize Steam API
#ifdef ENABLE_STEAM
steam::SteamAPI steam;
if (steam.initialized())
os::instance()->activateApp();
#endif
#if defined(_DEBUG) || defined(ENABLE_DEVMODE)
// On OS X, when we compile Aseprite on devmode, we're using it
// outside an app bundle, so we must active the app explicitly.
if (isGui())
os::instance()->activateApp();
#endif
#ifdef ENABLE_UPDATER
// Launch the thread to check for updates.
app::CheckUpdateThreadLauncher checkUpdate(
m_mainWindow->getCheckUpdateDelegate());
checkUpdate.launch();
#endif
app::SendCrash sendCrash;
sendCrash.search();
// Keep the console alive the whole program execute (just in case
// we've to print errors).
Console console;
#ifdef ENABLE_SCRIPTING
// Use the app::Console() for script errors
ConsoleEngineDelegate delegate;
script::ScopedEngineDelegate setEngineDelegate(m_engine.get(), &delegate);
#endif
// Run the GUI main message loop
ui::Manager::getDefault()->run();
}
#endif // ENABLE_UI
#ifdef ENABLE_SCRIPTING
// Start shell to execute scripts.
if (m_isShell) {
m_engine->printLastResult(); // TODO is this needed?
Shell shell;
shell.run(*m_engine);
}
#endif // ENABLE_SCRIPTING
// ----------------------------------------------------------------------
#ifdef ENABLE_SCRIPTING
// Call the exit() function from all plugins
extensions().executeExitActions();
#endif
#ifdef ENABLE_UI
if (isGui()) {
// Select no document
static_cast<UIContext*>(context())->setActiveView(nullptr);
// Delete backups (this is a normal shutdown, we are not handling
// exceptions, and we are not in a destructor).
m_modules->deleteDataRecovery();
}
#endif
// Destroy all documents from the UIContext.
std::vector<Doc*> docs;
#ifdef ENABLE_UI
for (Doc* doc : static_cast<UIContext*>(context())->getAndRemoveAllClosedDocs())
docs.push_back(doc);
#endif
for (Doc* doc : context()->documents())
docs.push_back(doc);
for (Doc* doc : docs) {
// First we close the document. In this way we receive recent
// notifications related to the document as a app::Doc. If
// we delete the document directly, we destroy the app::Doc
// too early, and then doc::~Document() call
// DocsObserver::onRemoveDocument(). In this way, observers
// could think that they have a fully created app::Doc when
// in reality it's a doc::Document (in the middle of a
// destruction process).
//
// TODO: This problem is because we're extending doc::Document,
// in the future, we should remove app::Doc.
doc->close();
delete doc;
}
#ifdef ENABLE_UI
if (isGui()) {
// Destroy the window.
m_mainWindow.reset(nullptr);
}
#endif
}
// Finishes the Aseprite application.
App::~App()
{
try {
LOG("APP: Exit\n");
ASSERT(m_instance == this);
#ifdef ENABLE_SCRIPTING
// Destroy scripting engine
m_engine.reset(nullptr);
#endif
// Delete file formats.
FileFormatsManager::destroyInstance();
// Fire App Exit signal.
App::instance()->Exit();
#ifdef ENABLE_UI
// Finalize modules, configuration and core.
Editor::destroyEditorSharedInternals();
if (m_backupIndicator) {
delete m_backupIndicator;
m_backupIndicator = nullptr;
}
// Save brushes
m_brushes.reset(nullptr);
#endif
delete m_legacy;
delete m_modules;
// Save preferences only if we are running in GUI mode. when we
// run in batch mode we might want to reset some preferences so
// the scripts have a reproducible behavior. Those reset
// preferences must not be saved.
if (isGui())
preferences().save();
delete m_coreModules;
#ifdef ENABLE_UI
// Destroy the loaded gui.xml data.
delete KeyboardShortcuts::instance();
delete GuiXml::instance();
#endif
m_instance = NULL;
}
catch (const std::exception& e) {
LOG(ERROR, "APP: Error: %s\n", e.what());
os::error_message(e.what());
// no re-throw
}
catch (...) {
os::error_message("Error closing the program.\n(uncaught exception)");
// no re-throw
}
}
Context* App::context()
{
return &m_coreModules->m_context;
}
bool App::isPortable()
{
static bool* is_portable = NULL;
if (!is_portable) {
is_portable =
new bool(
base::is_file(base::join_path(
base::get_file_path(base::get_app_path()),
"aseprite.ini")));
}
return *is_portable;
}
tools::ToolBox* App::toolBox() const
{
ASSERT(m_modules != NULL);
return &m_modules->m_toolbox;
}
tools::Tool* App::activeTool() const
{
return m_modules->m_activeToolManager.activeTool();
}
tools::ActiveToolManager* App::activeToolManager() const
{
return &m_modules->m_activeToolManager;
}
RecentFiles* App::recentFiles() const
{
#ifdef ENABLE_UI
ASSERT(m_modules != NULL);
return &m_modules->m_recent_files;
#else
return nullptr;
#endif
}
Workspace* App::workspace() const
{
if (m_mainWindow)
return m_mainWindow->getWorkspace();
else
return nullptr;
}
ContextBar* App::contextBar() const
{
if (m_mainWindow)
return m_mainWindow->getContextBar();
else
return nullptr;
}
Timeline* App::timeline() const
{
if (m_mainWindow)
return m_mainWindow->getTimeline();
else
return nullptr;
}
Preferences& App::preferences() const
{
return m_coreModules->m_context.preferences();
}
Extensions& App::extensions() const
{
return m_modules->m_extensions;
}
crash::DataRecovery* App::dataRecovery() const
{
return m_modules->recovery();
}
#ifdef ENABLE_UI
void App::showNotification(INotificationDelegate* del)
{
if (m_mainWindow)
m_mainWindow->showNotification(del);
}
void App::showBackupNotification(bool state)
{
assert_ui_thread();
if (state) {
if (!m_backupIndicator)
m_backupIndicator = new BackupIndicator;
m_backupIndicator->start();
}
else {
if (m_backupIndicator)
m_backupIndicator->stop();
}
}
void App::updateDisplayTitleBar()
{
std::string defaultTitle = fmt::format("{} v{}", get_app_name(), get_app_version());
std::string title;
DocView* docView = UIContext::instance()->activeView();
if (docView) {
// Prepend the document's filename.
title += docView->document()->name();
title += " - ";
}
title += defaultTitle;
os::instance()->defaultDisplay()->setTitle(title);
}
InputChain& App::inputChain()
{
return m_modules->m_inputChain;
}
#endif
// Updates palette and redraw the screen.
void app_refresh_screen()
{
#ifdef ENABLE_UI
Context* ctx = UIContext::instance();
ASSERT(ctx != NULL);
Site site = ctx->activeSite();
if (Palette* pal = site.palette())
set_current_palette(pal, false);
else
set_current_palette(nullptr, false);
// Invalidate the whole screen.
ui::Manager::getDefault()->invalidate();
#endif // ENABLE_UI
}
// TODO remove app_rebuild_documents_tabs() and replace it by
// observable events in the document (so a tab can observe if the
// document is modified).
void app_rebuild_documents_tabs()
{
#ifdef ENABLE_UI
if (App::instance()->isGui()) {
App::instance()->workspace()->updateTabs();
App::instance()->updateDisplayTitleBar();
}
#endif // ENABLE_UI
}
PixelFormat app_get_current_pixel_format()
{
Context* ctx = App::instance()->context();
ASSERT(ctx);
Doc* doc = ctx->activeDocument();
if (doc)
return doc->sprite()->pixelFormat();
else
return IMAGE_RGB;
}
int app_get_color_to_clear_layer(Layer* layer)
{
ASSERT(layer != NULL);
app::Color color;
// The `Background' is erased with the `Background Color'
if (layer->isBackground()) {
#ifdef ENABLE_UI
if (ColorBar::instance())
color = ColorBar::instance()->getBgColor();
else
#endif
color = app::Color::fromRgb(0, 0, 0); // TODO get background color color from doc::Settings
}
else // All transparent layers are cleared with the mask color
color = app::Color::fromMask();
return color_utils::color_for_layer(color, layer);
}
} // namespace app