mirror of
https://github.com/aseprite/aseprite.git
synced 2025-03-29 19:20:09 +00:00
Add shell mode to execute scripts interactively.
- Added Shell class. - Added base::SystemConsole to adjust stdin/stdout on Windows platform.
This commit is contained in:
parent
2acef11c55
commit
6b4591c5fd
@ -175,6 +175,7 @@ add_library(aseprite-library
|
||||
objects_container_impl.cpp
|
||||
recent_files.cpp
|
||||
resource_finder.cpp
|
||||
shell.cpp
|
||||
thumbnail_generator.cpp
|
||||
ui_context.cpp
|
||||
undo_transaction.cpp
|
||||
|
30
src/app.cpp
30
src/app.cpp
@ -49,6 +49,8 @@
|
||||
#include "raster/palette.h"
|
||||
#include "raster/sprite.h"
|
||||
#include "recent_files.h"
|
||||
#include "scripting/engine.h"
|
||||
#include "shell.h"
|
||||
#include "tools/tool_box.h"
|
||||
#include "ui/gui.h"
|
||||
#include "ui/intern.h"
|
||||
@ -65,6 +67,7 @@
|
||||
|
||||
#include <allegro.h>
|
||||
/* #include <allegro/internal/aintern.h> */
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
@ -89,8 +92,9 @@ public:
|
||||
UIContext m_ui_context;
|
||||
RecentFiles m_recent_files;
|
||||
app::DataRecovery m_recovery;
|
||||
scripting::Engine m_scriptingEngine;
|
||||
|
||||
Modules(bool verbose)
|
||||
Modules(bool console, bool verbose)
|
||||
: m_loggerModule(verbose)
|
||||
, m_recovery(&m_ui_context) {
|
||||
}
|
||||
@ -104,14 +108,16 @@ App::App(int argc, const char* argv[])
|
||||
: m_modules(NULL)
|
||||
, m_legacy(NULL)
|
||||
, m_isGui(false)
|
||||
, m_isShell(false)
|
||||
{
|
||||
ASSERT(m_instance == NULL);
|
||||
m_instance = this;
|
||||
|
||||
app::AppOptions options(argc, argv);
|
||||
|
||||
m_modules = new Modules(options.verbose());
|
||||
m_modules = new Modules(!options.startUI(), options.verbose());
|
||||
m_isGui = options.startUI();
|
||||
m_isShell = options.startShell();
|
||||
m_legacy = new LegacyModules(isGui() ? REQUIRE_INTERFACE: 0);
|
||||
m_files = options.files();
|
||||
|
||||
@ -136,7 +142,7 @@ App::App(int argc, const char* argv[])
|
||||
set_default_palette(pal.get());
|
||||
}
|
||||
|
||||
/* set system palette to the default one */
|
||||
// Set system palette to the default one.
|
||||
set_current_palette(NULL, true);
|
||||
}
|
||||
|
||||
@ -159,10 +165,8 @@ int App::run()
|
||||
ui::Manager::getDefault()->invalidate();
|
||||
}
|
||||
|
||||
/* set background mode for non-GUI modes */
|
||||
/* if (!(ase_mode & MODE_GUI)) */
|
||||
/* set_display_switch_mode(SWITCH_BACKAMNESIA); */
|
||||
set_display_switch_mode(SWITCH_BACKGROUND);
|
||||
// Set background mode for non-GUI modes
|
||||
set_display_switch_mode(SWITCH_BACKGROUND);
|
||||
|
||||
// Procress options
|
||||
PRINTF("Processing options...\n");
|
||||
@ -216,6 +220,18 @@ int App::run()
|
||||
// Destroy the window.
|
||||
m_mainWindow.reset(NULL);
|
||||
}
|
||||
// Start shell to execute scripts.
|
||||
else if (m_isShell) {
|
||||
m_systemConsole.prepareShell();
|
||||
|
||||
if (m_modules->m_scriptingEngine.supportEval()) {
|
||||
Shell shell;
|
||||
shell.run(m_modules->m_scriptingEngine);
|
||||
}
|
||||
else {
|
||||
std::cerr << "Your version of " PACKAGE " wasn't compiled with shell support.\n";
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -21,6 +21,7 @@
|
||||
|
||||
#include "base/signal.h"
|
||||
#include "base/string.h"
|
||||
#include "base/system_console.h"
|
||||
#include "base/unique_ptr.h"
|
||||
#include "raster/pixel_format.h"
|
||||
|
||||
@ -73,9 +74,11 @@ private:
|
||||
|
||||
static App* m_instance;
|
||||
|
||||
base::SystemConsole m_systemConsole;
|
||||
Modules* m_modules;
|
||||
LegacyModules* m_legacy;
|
||||
bool m_isGui;
|
||||
bool m_isShell;
|
||||
UniquePtr<MainWindow> m_mainWindow;
|
||||
FileList m_files;
|
||||
};
|
||||
|
@ -31,9 +31,12 @@ typedef base::ProgramOptions::Option Option;
|
||||
AppOptions::AppOptions(int argc, const char* argv[])
|
||||
: m_exeName(base::get_file_name(argv[0]))
|
||||
, m_startUI(true)
|
||||
, m_startShell(false)
|
||||
, m_verbose(false)
|
||||
{
|
||||
Option& palette = m_po.add("palette").requiresValue("GFXFILE").description("Use a specific palette by default");
|
||||
Option& shell = m_po.add("shell").description("Start an interactive console to execute scripts");
|
||||
Option& batch = m_po.add("batch").description("Do not start the UI");
|
||||
Option& verbose = m_po.add("verbose").description("Explain what is being done (in stderr or a log file)");
|
||||
Option& help = m_po.add("help").mnemonic('?').description("Display this help and exits");
|
||||
Option& version = m_po.add("version").description("Output version information and exit");
|
||||
@ -43,6 +46,7 @@ AppOptions::AppOptions(int argc, const char* argv[])
|
||||
|
||||
m_verbose = verbose.enabled();
|
||||
m_paletteFileName = palette.value();
|
||||
m_startShell = shell.enabled();
|
||||
|
||||
if (help.enabled()) {
|
||||
showHelp();
|
||||
@ -52,6 +56,10 @@ AppOptions::AppOptions(int argc, const char* argv[])
|
||||
showVersion();
|
||||
m_startUI = false;
|
||||
}
|
||||
|
||||
if (shell.enabled() || batch.enabled()) {
|
||||
m_startUI = false;
|
||||
}
|
||||
}
|
||||
catch (const std::runtime_error& parseError) {
|
||||
std::cerr << m_exeName << ": " << parseError.what() << '\n'
|
||||
|
@ -32,6 +32,7 @@ public:
|
||||
AppOptions(int argc, const char* argv[]);
|
||||
|
||||
bool startUI() const { return m_startUI; }
|
||||
bool startShell() const { return m_startShell; }
|
||||
bool verbose() const { return m_verbose; }
|
||||
|
||||
const std::string& paletteFileName() const { return m_paletteFileName; }
|
||||
@ -47,6 +48,7 @@ private:
|
||||
std::string m_exeName;
|
||||
base::ProgramOptions m_po;
|
||||
bool m_startUI;
|
||||
bool m_startShell;
|
||||
bool m_verbose;
|
||||
std::string m_paletteFileName;
|
||||
};
|
||||
|
@ -32,6 +32,7 @@ add_library(base-lib
|
||||
sha1_rfc3174.c
|
||||
split_string.cpp
|
||||
string.cpp
|
||||
system_console.cpp
|
||||
temp_dir.cpp
|
||||
thread.cpp
|
||||
trim_string.cpp
|
||||
|
94
src/base/system_console.cpp
Normal file
94
src/base/system_console.cpp
Normal file
@ -0,0 +1,94 @@
|
||||
// ASEPRITE base library
|
||||
// Copyright (C) 2001-2012 David Capello
|
||||
//
|
||||
// This source file is ditributed under a BSD-like license, please
|
||||
// read LICENSE.txt for more information.
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "base/system_console.h"
|
||||
|
||||
#ifdef WIN32 // Windows needs some adjustments to the console if the
|
||||
// process is linked with /subsystem:windows. These
|
||||
// adjustments are not great but are good enough.
|
||||
// See system_console.h for more information.
|
||||
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
|
||||
#include <windows.h>
|
||||
#include <io.h>
|
||||
|
||||
namespace base {
|
||||
|
||||
static bool withConsole = false;
|
||||
|
||||
SystemConsole::SystemConsole()
|
||||
{
|
||||
// If some output handle (stdout/stderr) is not attached to a
|
||||
// console, we can attach the process to the parent process console.
|
||||
bool unknownOut = (::GetFileType(::GetStdHandle(STD_OUTPUT_HANDLE)) == FILE_TYPE_UNKNOWN);
|
||||
bool unknownErr = (::GetFileType(::GetStdHandle(STD_ERROR_HANDLE)) == FILE_TYPE_UNKNOWN);
|
||||
if (unknownOut || unknownErr) {
|
||||
// AttachConsole() can fails if the parent console doesn't have a
|
||||
// console, which is the most common, i.e. when the user
|
||||
// double-click a shortcut to start the program.
|
||||
if (::AttachConsole(ATTACH_PARENT_PROCESS)) {
|
||||
// In this case we're attached to the parent process
|
||||
// (e.g. cmd.exe) console.
|
||||
withConsole = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (withConsole) {
|
||||
// Here we redirect stdout/stderr to use the parent console's ones.
|
||||
if (unknownOut) std::freopen("CONOUT$", "w", stdout);
|
||||
if (unknownErr) std::freopen("CONOUT$", "w", stderr);
|
||||
|
||||
// Synchronize C++'s cout/cerr streams with C's stdout/stderr.
|
||||
std::ios::sync_with_stdio();
|
||||
}
|
||||
}
|
||||
|
||||
SystemConsole::~SystemConsole()
|
||||
{
|
||||
if (withConsole) {
|
||||
::FreeConsole();
|
||||
withConsole = false;
|
||||
}
|
||||
}
|
||||
|
||||
void SystemConsole::prepareShell()
|
||||
{
|
||||
if (withConsole)
|
||||
::FreeConsole();
|
||||
|
||||
// In this case, for a better user experience, here we create a new
|
||||
// console so he can write text in a synchronized way with the
|
||||
// console. (The parent console stdin is not reliable for
|
||||
// interactive command input in the current state, without doing
|
||||
// this the input from the cmd.exe would be executed by cmd.exe and
|
||||
// by our app.)
|
||||
withConsole = true;
|
||||
::AllocConsole();
|
||||
::AttachConsole(::GetCurrentProcessId());
|
||||
|
||||
std::freopen("CONIN$", "r", stdin);
|
||||
std::freopen("CONOUT$", "w", stdout);
|
||||
std::freopen("CONOUT$", "w", stderr);
|
||||
std::ios::sync_with_stdio();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#else // On Unix-like systems the console works just fine
|
||||
|
||||
namespace base {
|
||||
|
||||
SystemConsole::SystemConsole() { }
|
||||
SystemConsole::~SystemConsole() { }
|
||||
void SystemConsole::prepareShell()() { }
|
||||
|
||||
}
|
||||
|
||||
#endif
|
56
src/base/system_console.h
Normal file
56
src/base/system_console.h
Normal file
@ -0,0 +1,56 @@
|
||||
// ASEPRITE base library
|
||||
// Copyright (C) 2001-2012 David Capello
|
||||
//
|
||||
// This source file is ditributed under a BSD-like license, please
|
||||
// read LICENSE.txt for more information.
|
||||
|
||||
#ifndef BASE_SYSTEM_CONSOLE_H_INCLUDED
|
||||
#define BASE_SYSTEM_CONSOLE_H_INCLUDED
|
||||
|
||||
namespace base {
|
||||
|
||||
// This class is needed only for Windows platform.
|
||||
//
|
||||
// Some background: This app is linked with /subsystem:windows flag,
|
||||
// which is the only way to avoid a console when the program is
|
||||
// double-clicked from Windows Explorer. The problem with this is if
|
||||
// the user starts the program from cmd.exe, the output is not shown
|
||||
// anywhere. Generally there is one simple solution for console only
|
||||
// apps: The /subsystem:console flag, but it shows a console when the
|
||||
// program is started from Windows Explorer (anyway we could call
|
||||
// FreeConsole(), but the console is visible some milliseconds which
|
||||
// is not an expected behavior by normal Windows user).
|
||||
//
|
||||
// So this class tries to make some adjustments for apps linked with
|
||||
// /subsystem:windows but that want to display some text in the
|
||||
// console in certain cases (e.g. when --help flag is specified).
|
||||
//
|
||||
// Basically it tries to redirect stdin/stdout handles so the end-user
|
||||
// can have the best of both worlds on Windows:
|
||||
// 1) If the app is executed with double-click, a console isn't shown.
|
||||
// (Because the process was/should be linked with /subsystem:windows flag.)
|
||||
// 2) If the app is executed from a process like cmd.exe, the output is
|
||||
// redirected to the cmd.exe console.
|
||||
//
|
||||
// In the best case, the application should work as a Unix-like
|
||||
// program, blocking the cmd.exe in case 2, but it cannot be
|
||||
// possible. The output/input is deattached as soon as cmd.exe knows
|
||||
// that the program was linked with /subsystem:windows.
|
||||
//
|
||||
class SystemConsole
|
||||
{
|
||||
public:
|
||||
SystemConsole();
|
||||
~SystemConsole();
|
||||
|
||||
// On Windows it creates a console so the user can start typing
|
||||
// commands on it. It's necessary because we link the program with
|
||||
// /subsystem:windows flag (not /subsystem:console), so the
|
||||
// process's stdin starts deattached from the parent process's
|
||||
// console. (On Unix-like systems it does nothing.)
|
||||
void prepareShell();
|
||||
};
|
||||
|
||||
} // namespace base
|
||||
|
||||
#endif
|
@ -24,7 +24,6 @@
|
||||
#include "base/memory_dump.h"
|
||||
#include "console.h"
|
||||
#include "resource_finder.h"
|
||||
#include "scripting/engine.h"
|
||||
#include "she/she.h"
|
||||
#include "ui/base.h"
|
||||
|
||||
@ -89,7 +88,6 @@ int app_main(int argc, char* argv[])
|
||||
MemLeak memleak;
|
||||
ui::GuiSystem guiSystem;
|
||||
App app(argc, const_cast<const char**>(argv));
|
||||
scripting::Engine scriptingEngine;
|
||||
|
||||
// Change the name of the memory dump file
|
||||
{
|
||||
|
44
src/shell.cpp
Normal file
44
src/shell.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
/* ASEPRITE
|
||||
* Copyright (C) 2001-2012 David Capello
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "shell.h"
|
||||
|
||||
#include "scripting/engine.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
Shell::Shell()
|
||||
{
|
||||
}
|
||||
|
||||
Shell::~Shell()
|
||||
{
|
||||
}
|
||||
|
||||
void Shell::run(scripting::Engine& engine)
|
||||
{
|
||||
std::cout << "Welcome to " PACKAGE " v" VERSION " interactive console" << std::endl;
|
||||
std::string line;
|
||||
while (std::getline(std::cin, line)) {
|
||||
engine.eval(line);
|
||||
}
|
||||
std::cout << "Done\n";
|
||||
}
|
33
src/shell.h
Normal file
33
src/shell.h
Normal file
@ -0,0 +1,33 @@
|
||||
/* ASEPRITE
|
||||
* Copyright (C) 2001-2012 David Capello
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef SHELL_H_INCLUDED
|
||||
#define SHELL_H_INCLUDED
|
||||
|
||||
namespace scripting { class Engine; }
|
||||
|
||||
class Shell
|
||||
{
|
||||
public:
|
||||
Shell();
|
||||
~Shell();
|
||||
|
||||
void run(scripting::Engine& engine);
|
||||
};
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user