Publish VCC Plus OSS source distribution for 3.6.2 build 60
This commit is contained in:
commit
c652a5276d
1192 files changed
+272368
No files matched your search
@@ -0,0 +1,211 @@
|
||||
|
||||
This describes the cartridge interface design for VCC
|
||||
|
||||
---
|
||||
|
||||
# VCC Cartridge Types
|
||||
|
||||
VCC emulates the physical cartridges that could be plugged into the side expansion slot
|
||||
of a CoCo 3 or into a MultiPak Interface. VCC supports three main categories of cartridges:
|
||||
|
||||
---
|
||||
|
||||
## 1. ROM Cartridges
|
||||
|
||||
ROM cartridges contain only read-only memory and the minimal hardware needed to expose that
|
||||
memory to the CoCo 3.
|
||||
|
||||
**Characteristics:**
|
||||
|
||||
- Typically contain 6809 or 6309 executable code.
|
||||
- The CoCo reads data directly from mapped ROM addresses.
|
||||
- Some cartridges support bank switching by writing to I/O ports.
|
||||
- In VCC, all ROM banking logic is implemented inside the emulator itself.
|
||||
- ROM cartridges require no host-side code.
|
||||
- They are stored as simple binary files.
|
||||
- Some ROM files include headers or signatures to indicate special mapping or banking behavior.
|
||||
- Most common ROM cartridges were 16 KB or smaller and mapped at `0xC000` in ROM mode.
|
||||
|
||||
---
|
||||
|
||||
## 2. Hardware Cartridges
|
||||
|
||||
Hardware cartridges contain actual devices, such as:
|
||||
|
||||
- Disk controllers
|
||||
- Sound synthesizers
|
||||
- RS-232 interfaces
|
||||
- Additional microprocessors
|
||||
- Often, onboard ROM
|
||||
|
||||
These cartridges require host-side x86 code to emulate their hardware behavior.
|
||||
|
||||
**In VCC:**
|
||||
|
||||
- Hardware cartridges are implemented as **Windows DLLs**.
|
||||
- VCC identifies them by checking for the `MZ` signature at the start of the file.
|
||||
- Hardware DLL's must export the PakInitialize call to be valid
|
||||
- ROM data for these cartridges is usually stored separately, though this is not required.
|
||||
- Some hardware cartridges can load multiple ROMs and switch between them under program control.
|
||||
- The DLL is responsible for loading and managing these ROMs.
|
||||
- The CoCo communicates with the emulated hardware by reading and writing specific I/O ports.
|
||||
- Hardware cartridges may generate interrupts or output audio through a dedicated sound channel.
|
||||
- Hardware cartridges cannot load other hardware cartridges.
|
||||
|
||||
---
|
||||
|
||||
## 3. MultiPak Cartridge (MPI)
|
||||
|
||||
MultiPak is a special type of hardware cartridge, not really a true cartridge, it is a
|
||||
bus expansion that acts as hosts for other hardware cartridges.
|
||||
|
||||
**Capabilities:**
|
||||
|
||||
- MultiPak Cartridge DLL must export the init MPI call to be valid
|
||||
- Provide multiple cartridge slots.
|
||||
- Contains no ROM of it's own
|
||||
- Allow slot selection through UI controls or programmatically.
|
||||
|
||||
**VCC support:**
|
||||
|
||||
- VCC currently emulates the original Radio Shack MultiPak Interface.
|
||||
- Multipak can only be inserted Coco slide slot, not into another multipak.
|
||||
- Third-party MultiPak-style devices also exist, including:
|
||||
- Two-slot versions
|
||||
- Hobbyist designs supporting up to eight slots
|
||||
|
||||
---
|
||||
|
||||
# Cartridge Loader: Cartridge Type Identification Logic
|
||||
|
||||
The cartridge loader determines the cartridge type using the following sequence:
|
||||
|
||||
1. **Check if the file can be read**
|
||||
- If it cannot be read, the cartridge is **not loadable**.
|
||||
|
||||
2. **Check if the file is a DLL**
|
||||
- If it is *not* a DLL, the cartridge is a **ROM Cartridge**.
|
||||
|
||||
3. **Check if the DLL exports `InitCart`**
|
||||
- If it does *not*, the cartridge is **not loadable**.
|
||||
|
||||
4. **Check if the DLL exports PakGetCapabilities**
|
||||
- If it does not, the cartridge is CART_TYPE_GENERIC
|
||||
|
||||
5. Continue with loading and initialization
|
||||
|
||||
---
|
||||
|
||||
#Cartridge Exports
|
||||
|
||||
DLL's can export any of the following calls
|
||||
|
||||
Initiate pack (REQUIRED)
|
||||
PakInitialize (SLotId, settings_path, hVccWnd, &callbacks);
|
||||
|
||||
Retrieve cart menu item
|
||||
PakGetMenuItem(item, index);
|
||||
|
||||
Get cart capabilities (PROPOSED)
|
||||
PakGetCapabilities (void);
|
||||
|
||||
Terminate pack, prepare for DLL unload
|
||||
PakTerminate ()
|
||||
|
||||
Get pack name
|
||||
PakGetName (name, buf_size)
|
||||
|
||||
Get pack ID
|
||||
PakGetCatalogId (catalogId, buf_size)
|
||||
|
||||
Get pack description
|
||||
PakGetDescription (Description, buf_size)
|
||||
|
||||
Reset the pack
|
||||
PakReset ()
|
||||
|
||||
Horizontal sync occurred
|
||||
PakProcessHorizontalSync ()
|
||||
|
||||
Get pack status line text
|
||||
PakGetStatus (Status, buf_size)
|
||||
|
||||
Write I/O port
|
||||
PakWritePort (Port, data)
|
||||
|
||||
Read I/O port
|
||||
unsigned char PakReadPort (Port)
|
||||
|
||||
Read ROM byte
|
||||
unsigned char PakReadMemoryByte (Address)
|
||||
|
||||
Sample audio line
|
||||
unsigned short PakSampleAudio ()
|
||||
|
||||
A hardware pack menu item was clicked
|
||||
PakMenuItemClicked (menu_item_id)
|
||||
|
||||
---
|
||||
|
||||
#Callbacks
|
||||
|
||||
There are two ways a cartridge can initiate host actions:
|
||||
|
||||
1) Notifications initiated by cartridge UI events like adding menu items or inserting
|
||||
cartridges in slots. These are asyncronous and can rely on DLL exports for details.
|
||||
The mechanism for these is Vcc's main window message loop. The window handle (hVccWnd)
|
||||
has been added to PakInitialize() for this purpose. Two notification messages are
|
||||
defined, one to initiate a VCC reset and another to initiate cartridge menu rebuilds.
|
||||
|
||||
2) Callbacks initiated by 6x09 code running inside the CPU loop such as asserting
|
||||
interrupts, cart lines, or reading and writing memory to handle DMA requests. These
|
||||
are syncronous and must return quickly to not affect emulator timing. Callbacks are
|
||||
determined by Coco3's physical bus and are not likely to change. There are four
|
||||
callbacks:
|
||||
|
||||
void assert_cartridge_line (SlotId, line_state)
|
||||
void assert_interrupt (SlotId, interrupt, interrupt_source)
|
||||
void write_memory_byte (SlotId, value, address)
|
||||
unsigned char read_memory_byte (SlotId, unsigned short address)
|
||||
|
||||
SlotId is used by the MPI to filter assert_cartridge_line. DLL's are passed their slot
|
||||
location in PakInitialize().
|
||||
|
||||
---
|
||||
|
||||
#Proposed New Design Philosophy For The MPI (under consideration)
|
||||
|
||||
VCC and forks of VCC use run-time loading and unloading of dynamic libraries to simulate
|
||||
the inserting and removing of Cartridges.
|
||||
|
||||
A special "cartridge" is the Multipak MPI. On a physical Coco the MPI it is not actually
|
||||
a cartridge. It is more accurately an extension of the bus.
|
||||
|
||||
The pakinterface loads and unloads cartridges. So does the MPI. If both MPI and pakinterface
|
||||
call cartridge DLLs directly there are now two modules, VCC and MPI, that need to know about
|
||||
cartridge lifetimes, handles, exports, and inports. All loading and unloading of carts belongs
|
||||
in one place and that place is the PakInterface part of VCC main.
|
||||
|
||||
If slot management is moved to the Pakinterface the logic that controls the CTS and SCS and
|
||||
the management of saved slot content settings must move too. The pakinterface must share this
|
||||
information with the MPI DLL which becomes purely a user interface.
|
||||
|
||||
The pakinterface slot management concept:
|
||||
|
||||
- The pakinterface maintains a list indexed by slot number of loaded cartridges.
|
||||
|
||||
- Slots are numbered 0-4. Slot 0 is the Boot slot. Slots 1-4 loaded if MPI is in the Boot slot.
|
||||
|
||||
- The pakInterface routes I/O to slots per CTS/SCS using existing capability / callcacks
|
||||
|
||||
- The pakinterface saves slot content to settings.
|
||||
|
||||
- Any change to current CTS triggers a reset. This is detected and effected by the pakinterface
|
||||
Reset stops the CPU, unloads all cartridges, reloads the basic rom, reloads cartridges
|
||||
per settings, and then restarts the CPU.
|
||||
|
||||
- MPI sends changes to startup CTS/SCS via a notification message. Startup CTS/SCS does not
|
||||
affect the running CPU, a reset is required for them to take effect.
|
||||
|
||||
- The MPI requests cartridge loads / unloads by sending messages to WinMain.
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
|
||||
# VCC Export Based Cartridge Menu System
|
||||
|
||||
This replaces per‑item callbacks with a export‑based model for exchanging menu
|
||||
items between cartridge DLLs, Multipak (MPI), and the PakInterface host.
|
||||
|
||||
Concept:
|
||||
|
||||
- Each cartridge DLL creates an internal list of it's menu items as needed.
|
||||
- DLL's expose an export that returns menu items from the list on demand.
|
||||
- MPI aggregates child cartridge menus, applies slot‑based ID biasing, and returns a
|
||||
unified list to the host.
|
||||
- The host pakinterface rebuilds the UI menu from the aggregated list.
|
||||
- DLLs notify the host of menu changes using a single Windows message.
|
||||
- Clear separation of responsibilities:
|
||||
- DLLs own their menu state.
|
||||
- MPI aggregates and biases.
|
||||
- Host renders.
|
||||
- The export signature remains stable even if internal implementations change.
|
||||
|
||||
---
|
||||
|
||||
## Cartridge DLL
|
||||
|
||||
- Sends WM_VCC_UPD_MENU to the host on internal state changes that affect its menu.
|
||||
- Keeps a static list of menu items that represents it's internal state.
|
||||
- Implements a GetMenuItemList(item,index) export that:
|
||||
- Updates the list as required (see note)
|
||||
- Accepts a index that indicate which entry is desired.
|
||||
- Returns the entry at that index in the item buffer.
|
||||
- Returns false if the entry does not exist.
|
||||
|
||||
Notes:
|
||||
|
||||
If the item buffer is a nullptr the DLL returns false.
|
||||
If the index is above MAX_MENU_ITEMS DLL returns false.
|
||||
An index of zero indicates the DLL's internal list should be refreshed.
|
||||
- DLL updates / recreates it's internal list as required.
|
||||
- DLL returns the first item if it exists, otherwise false.
|
||||
For indexes above zero the DLL simply returns the item or false.
|
||||
Cartridge plumbing guarantees that DLL's who don't expose the actual
|
||||
export will return false when call is attempted.
|
||||
|
||||
---
|
||||
|
||||
## Multipak (MPI)
|
||||
|
||||
MPI acts as an aggregator and applies deterministic slot‑based menu ID biasing.
|
||||
The MPI is only permitted to be installed in the boot slot. (Coco side slot)
|
||||
|
||||
MPI:
|
||||
|
||||
- Maintains references to each child cartridge’s menu export.
|
||||
- When its export is called:
|
||||
- If index is zero it refreshes it's own menu items.
|
||||
- Calls each child cartridge’s export to retrieve their items.
|
||||
- Applies slot‑based menu ID biasing:
|
||||
- Merges all items into a single unified list.
|
||||
- Returns item from the list indicated by the index.
|
||||
|
||||
---
|
||||
|
||||
## VCC
|
||||
|
||||
The host:
|
||||
|
||||
- Creates the master menu item list from which the cartridge menu is drawn
|
||||
- Listens for WM_VCC_UPD_MENU from any DLL including the MPI.
|
||||
- On receiving the message, updates it's list using calls to the boot slot's
|
||||
(slot 0) export and rebuilds the menu.
|
||||
|
||||
The host only sees a flat, final list of menu items.
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
//#define USE_LOGGING
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#include <vcc/bus/basic_cartridge.h>
|
||||
#include <vcc/util/logger.h>
|
||||
#include <typeinfo>
|
||||
|
||||
namespace VCC::Core
|
||||
{
|
||||
|
||||
basic_cartridge::name_type basic_cartridge::name() const
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
basic_cartridge::catalog_id_type basic_cartridge::catalog_id() const
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
basic_cartridge::description_type basic_cartridge:: description() const
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
void basic_cartridge::start()
|
||||
{
|
||||
DLOG_C("basic_cartridge::start type: %s cart ptr: %p\n",
|
||||
typeid(*this).name(), this);
|
||||
initialize_pak();
|
||||
initialize_bus();
|
||||
}
|
||||
|
||||
void basic_cartridge::stop()
|
||||
{}
|
||||
|
||||
void basic_cartridge::reset()
|
||||
{}
|
||||
|
||||
void basic_cartridge::process_horizontal_sync()
|
||||
{}
|
||||
|
||||
void basic_cartridge::write_port(unsigned char port_id, unsigned char value)
|
||||
{}
|
||||
|
||||
unsigned char basic_cartridge::read_port(unsigned char port_id)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
unsigned char basic_cartridge::read_memory_byte(unsigned short memory_address)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
void basic_cartridge::status(char* text_buffer, size_t buffer_size)
|
||||
{
|
||||
*text_buffer = 0;
|
||||
}
|
||||
|
||||
unsigned short basic_cartridge::sample_audio()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
void basic_cartridge::menu_item_clicked(unsigned char menu_item_id)
|
||||
{}
|
||||
|
||||
void basic_cartridge::initialize_pak()
|
||||
{}
|
||||
|
||||
void basic_cartridge::initialize_bus()
|
||||
{}
|
||||
|
||||
bool basic_cartridge::get_menu_item( menu_item_entry* item, size_t index)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
#include <vcc/bus/cartridge.h>
|
||||
|
||||
namespace VCC::Core
|
||||
{
|
||||
|
||||
struct basic_cartridge : public ::VCC::Core::cartridge
|
||||
{
|
||||
public:
|
||||
|
||||
using name_type = std::string;
|
||||
using catalog_id_type = std::string;
|
||||
|
||||
public:
|
||||
|
||||
using cartridge::cartridge;
|
||||
|
||||
name_type name() const override;
|
||||
catalog_id_type catalog_id() const override;
|
||||
description_type description() const override;
|
||||
|
||||
void start() override;
|
||||
void stop() override;
|
||||
|
||||
void reset() override;
|
||||
void process_horizontal_sync() override;
|
||||
void write_port(unsigned char port_id, unsigned char value) override;
|
||||
unsigned char read_port(unsigned char port_id) override;
|
||||
unsigned char read_memory_byte(unsigned short memory_address) override;
|
||||
void status(char* text_buffer, size_t buffer_size) override;
|
||||
unsigned short sample_audio() override;
|
||||
void menu_item_clicked(unsigned char menu_item_id) override;
|
||||
bool get_menu_item(menu_item_entry* item, size_t index) override;
|
||||
|
||||
protected:
|
||||
|
||||
virtual void initialize_pak();
|
||||
virtual void initialize_bus();
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
#include <vcc/bus/cartridge_menuitem.h>
|
||||
#include <string>
|
||||
|
||||
namespace VCC::Core
|
||||
{
|
||||
struct cartridge
|
||||
{
|
||||
public:
|
||||
using name_type = std::string;
|
||||
using catalog_id_type = std::string;
|
||||
using description_type = std::string;
|
||||
|
||||
public:
|
||||
// Protect cartridge objects by deleting move constructers
|
||||
cartridge() = default;
|
||||
cartridge(const cartridge&) = delete;
|
||||
cartridge(cartridge&&) = delete;
|
||||
|
||||
// Default destructor ensures deleting a cartridge* calls
|
||||
// the destructor of cpak_cartridge or rom_cartridge
|
||||
virtual ~cartridge() = default;
|
||||
|
||||
virtual name_type name() const = 0;
|
||||
virtual catalog_id_type catalog_id() const = 0;
|
||||
virtual description_type description() const = 0;
|
||||
|
||||
virtual void start() = 0;
|
||||
virtual void stop() = 0;
|
||||
virtual void reset() = 0;
|
||||
virtual void process_horizontal_sync() = 0;
|
||||
virtual void write_port(unsigned char port_id, unsigned char value) = 0;
|
||||
virtual unsigned char read_port(unsigned char port_id) = 0;
|
||||
virtual unsigned char read_memory_byte(unsigned short memory_address) = 0;
|
||||
virtual void status(char* text_buffer, size_t buffer_size) = 0;
|
||||
virtual unsigned short sample_audio() = 0;
|
||||
virtual void menu_item_clicked(unsigned char menu_item_id) = 0;
|
||||
|
||||
// TODO following is no longer used. Should be cleaned up
|
||||
virtual bool get_menu_item(menu_item_entry* item, size_t index) = 0;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
#include "../../../../../src/core/Interrupts.h"
|
||||
#include <string>
|
||||
|
||||
enum MenuItemType;
|
||||
|
||||
namespace VCC::Core
|
||||
{
|
||||
struct cartridge_callbacks
|
||||
{
|
||||
using path_type = std::string;
|
||||
|
||||
virtual ~cartridge_callbacks() = default;
|
||||
virtual path_type configuration_path() const = 0;
|
||||
|
||||
virtual void assert_cartridge_line(bool line_state) = 0;
|
||||
virtual void assert_interrupt(Interrupt interrupt, InterruptSource interrupt_source) = 0;
|
||||
virtual void write_memory_byte(unsigned char value, unsigned short address) = 0;
|
||||
virtual unsigned char read_memory_byte(unsigned short address) = 0;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,247 @@
|
||||
//#define USE_LOGGING
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#include <vcc/bus/cartridge_loader.h>
|
||||
#include <vcc/bus/rom_cartridge.h>
|
||||
#include <vcc/bus/cpak_cartridge.h>
|
||||
#include <vcc/util/logger.h>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// The cartridge loader loads the cart's file and creates the cartridge object
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
namespace VCC::Core
|
||||
{
|
||||
namespace
|
||||
{
|
||||
//TODO use existing utility function for this
|
||||
std::string extract_filename(std::string name)
|
||||
{
|
||||
name = name.substr(name.find_last_of("/\\") + 1);
|
||||
const auto endIndex = name.find_last_of('.');
|
||||
if (endIndex > 0)
|
||||
{
|
||||
name = name.substr(0, endIndex);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
cartridge_file_type determine_cartridge_type(const std::string& filename)
|
||||
{
|
||||
std::ifstream input(filename, std::ios::binary);
|
||||
|
||||
if (!input.is_open()) {
|
||||
return cartridge_file_type::not_opened;
|
||||
}
|
||||
|
||||
// Read two bytes from the start of the file
|
||||
unsigned char header[2] = {};
|
||||
input.clear();
|
||||
input.seekg(0, std::ios::beg);
|
||||
input.read(reinterpret_cast<char*>(header), 2);
|
||||
if (!input) {
|
||||
DLOG_C("cartridge_loader.determine_cartridge_type cart too small\n");
|
||||
return cartridge_file_type::rom_image;
|
||||
}
|
||||
|
||||
// Check for magic 'MZ' DLL indicator
|
||||
if (header[0] == 'M' && header[1] == 'Z') {
|
||||
DLOG_C("cartridge_loader.determine_cartridge_type Cpak\n");
|
||||
return cartridge_file_type::library;
|
||||
}
|
||||
|
||||
DLOG_C("cartridge_loader.determine_cartridge_type ROM\n");
|
||||
return cartridge_file_type::rom_image;
|
||||
}
|
||||
|
||||
// Load a ROM cartridge
|
||||
cartridge_loader_result load_rom_cartridge(
|
||||
const std::string& filename,
|
||||
slot_id_type SlotId,
|
||||
const std::string& iniPath,
|
||||
HWND hVccWnd,
|
||||
const cpak_callbacks& cpak_callbacks)
|
||||
{
|
||||
DLOG_C("cartridge_loader.load_rom_cartridge\n");
|
||||
constexpr size_t PAK_MAX_MEM = 0x40000;
|
||||
|
||||
std::vector<uint8_t> romImage;
|
||||
|
||||
romImage.reserve(PAK_MAX_MEM);
|
||||
|
||||
// Open the ROM file, fail if unable to
|
||||
std::ifstream input(filename, std::ios::binary);
|
||||
if (!input.is_open()) {
|
||||
DLOG_C("cartridge_loader.load_rom_cartridge open fail\n");
|
||||
return { nullptr, nullptr, cartridge_loader_status::cannot_open };
|
||||
}
|
||||
|
||||
// Load the file
|
||||
input.unsetf(std::ios::skipws);
|
||||
std::copy(
|
||||
std::istream_iterator<uint8_t>(input),
|
||||
std::istream_iterator<uint8_t>(),
|
||||
back_inserter(romImage));
|
||||
|
||||
if (romImage.empty()) {
|
||||
DLOG_C("cartridge_loader.load_rom_cartridge empty\n");
|
||||
return { nullptr, nullptr, cartridge_loader_status::not_rom };
|
||||
}
|
||||
|
||||
// Force enable bank switching since we don't have a way to detect if
|
||||
// it should be enabled.
|
||||
constexpr bool enable_bank_switching = true;
|
||||
|
||||
// Create rom cartridge object to contain the rom
|
||||
auto rom = std::make_unique<VCC::Core::rom_cartridge>
|
||||
(
|
||||
cpak_callbacks,
|
||||
extract_filename(filename),
|
||||
"",
|
||||
std::move(romImage),
|
||||
enable_bank_switching,
|
||||
SlotId
|
||||
);
|
||||
|
||||
auto result = cartridge_loader_result {
|
||||
nullptr,
|
||||
std::move(rom),
|
||||
cartridge_loader_status::success };
|
||||
|
||||
DLOG_C("cartridge_loader.load_rom_cartridge success %s\n",filename.c_str());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Load a dll cartridge
|
||||
cartridge_loader_result load_cpak_cartridge(
|
||||
const std::string& filename,
|
||||
slot_id_type SlotId,
|
||||
const std::string& iniPath,
|
||||
HWND hVccWnd,
|
||||
const cpak_callbacks& cpak_callbacks)
|
||||
{
|
||||
|
||||
DLOG_C("cartridge_loader.load_cpak_cartridge %s\n",filename.c_str());
|
||||
|
||||
auto h = GetModuleHandle(filename.c_str());
|
||||
if (h != nullptr) {
|
||||
DLOG_C("cartridge_loader.load_cpak_cartridge duplicate load\n");
|
||||
return { nullptr, nullptr, cartridge_loader_status::already_loaded };
|
||||
}
|
||||
|
||||
// Load the cart DLL
|
||||
cartridge_loader_result details;
|
||||
HMODULE hCart = LoadLibrary(filename.c_str());
|
||||
details.dll_module.reset(hCart);
|
||||
|
||||
if (details.dll_module == nullptr)
|
||||
{
|
||||
DLOG_C("cartridge_loader.load_cpak_cartridge dll load failed\n");
|
||||
return { nullptr, nullptr, cartridge_loader_status::cannot_open };
|
||||
}
|
||||
|
||||
// Create cartridge object if DLL has a PakInitialize export
|
||||
if (GetProcAddress(details.dll_module.get(), "PakInitialize") != nullptr)
|
||||
{
|
||||
details.cartridge = std::make_unique<VCC::Core::cpak_cartridge>(
|
||||
details.dll_module.get(),
|
||||
SlotId,
|
||||
iniPath,
|
||||
hVccWnd,
|
||||
cpak_callbacks);
|
||||
details.load_result = cartridge_loader_status::success;
|
||||
DLOG_C("cartridge_loader.load_cpak_cartridge success %p\n",details.cartridge.get());
|
||||
return details;
|
||||
}
|
||||
|
||||
// Else not a valid cartridge plugin DLL
|
||||
DLOG_C("cartridge_loader.load_cpak_cartridge dll initialize missing\n");
|
||||
return { nullptr, nullptr, cartridge_loader_status::not_expansion };
|
||||
}
|
||||
|
||||
// Load a cartridge; either a ROM image or a pak dll. Load cartridge is called
|
||||
// by both mpi/multipak_cartridge.cpp and pakinterface.cpp.
|
||||
// cartridge_loader_result is defined in libcommon/include/vcc/bus/cartridge_loader.h
|
||||
// cartridge_callbacks is defined in libcommon/include/vcc/bus/cartridge_callbacks.h
|
||||
// SlotId is size_t 0-4, 0 = boot slot (side slot), 1-4 = MPI slots. SlotId is
|
||||
// passed to cpak cart DLLs and is returned as first argment of callbacks
|
||||
// cpak_callbacks is used by all hardware paks and is defined in
|
||||
// libcommon/include/vcc/bus/cpak_cartridge_definitions.h.
|
||||
cartridge_loader_result load_cartridge(
|
||||
const std::string& filename,
|
||||
slot_id_type SlotId,
|
||||
const std::string& iniPath,
|
||||
HWND hVccWnd,
|
||||
const cpak_callbacks& cpak_callbacks)
|
||||
{
|
||||
DLOG_C("cartridge_loader.load_cartridge %d %s\n", SlotId,filename.c_str());
|
||||
|
||||
switch (VCC::Core::determine_cartridge_type(filename))
|
||||
{
|
||||
default:
|
||||
case cartridge_file_type::not_opened: // File doesn't exist or can't be opened
|
||||
return { nullptr, nullptr, cartridge_loader_status::cannot_open };
|
||||
|
||||
case cartridge_file_type::rom_image: // File is a ROM image
|
||||
return VCC::Core::load_rom_cartridge(
|
||||
filename,
|
||||
SlotId,
|
||||
iniPath,
|
||||
hVccWnd,
|
||||
cpak_callbacks);
|
||||
|
||||
case cartridge_file_type::library: // File is a DLL
|
||||
return VCC::Core::load_cpak_cartridge(
|
||||
filename,
|
||||
SlotId, // Where cart is inserted
|
||||
iniPath, // Path to vcc ini file
|
||||
hVccWnd, // Vcc message proxy window
|
||||
cpak_callbacks); // DLL callbacks in here
|
||||
}
|
||||
}
|
||||
|
||||
// Return error string per cartridge load status.
|
||||
// TODO: Move and make this generic so it also can be used elsewhere in the project
|
||||
std::string cartridge_load_error_string(const cartridge_loader_status error_status)
|
||||
{
|
||||
switch (error_status)
|
||||
{
|
||||
case cartridge_loader_status::success:
|
||||
return "Success";
|
||||
case cartridge_loader_status::already_loaded:
|
||||
return "Module is already loaded.";
|
||||
case cartridge_loader_status::cannot_open:
|
||||
return "Cannot open module.";
|
||||
case cartridge_loader_status::not_found:
|
||||
return "Module not found.";
|
||||
case cartridge_loader_status::not_loaded:
|
||||
return "Unable to load module.";
|
||||
case cartridge_loader_status::not_rom:
|
||||
return "Module is not a valid ROM image.";
|
||||
case cartridge_loader_status::not_expansion:
|
||||
return "Module is not a valid expansion plugin.";
|
||||
default:
|
||||
return "Unknown error.";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
#include <vcc/bus/cartridge.h>
|
||||
#include <vcc/bus/cartridge_callbacks.h>
|
||||
#include <vcc/bus/cpak_cartridge_definitions.h>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <Windows.h>
|
||||
|
||||
namespace VCC::Core
|
||||
{
|
||||
enum class cartridge_file_type
|
||||
{
|
||||
not_opened,
|
||||
rom_image,
|
||||
library
|
||||
};
|
||||
|
||||
enum class cartridge_loader_status
|
||||
{
|
||||
success,
|
||||
already_loaded,
|
||||
cannot_open,
|
||||
not_found,
|
||||
not_loaded,
|
||||
not_rom,
|
||||
not_expansion
|
||||
};
|
||||
|
||||
struct dll_deleter {
|
||||
void operator()(HMODULE h) const {
|
||||
if (h) FreeLibrary(h);
|
||||
}
|
||||
};
|
||||
|
||||
struct cartridge_loader_result
|
||||
{
|
||||
using dll_module_type =
|
||||
std::unique_ptr<std::remove_pointer_t<HMODULE>,VCC::Core::dll_deleter>;
|
||||
using cartridge_ptr_type = std::unique_ptr<VCC::Core::cartridge>;
|
||||
|
||||
dll_module_type dll_module;
|
||||
cartridge_ptr_type cartridge;
|
||||
cartridge_loader_status load_result = cartridge_loader_status::not_loaded;
|
||||
};
|
||||
|
||||
cartridge_file_type determine_cartridge_type(const std::string& filename);
|
||||
|
||||
//-------------------------------------------------------------------------------
|
||||
// Cartridge Loaders. Determine type and call appropriate loader, rom or cpak
|
||||
// SlotID is the 0-4 slot id; 0 is the boot slot, 1-4 are multipak slots.
|
||||
// iniPath is the name of the ini file (typically vcc.ini)
|
||||
// hVccWnd is the VCC main window handle, used for messaging
|
||||
// cpak_callbacks defines the cartridge callbacks
|
||||
//-------------------------------------------------------------------------------
|
||||
|
||||
cartridge_loader_result load_cartridge(
|
||||
const std::string& filename,
|
||||
slot_id_type SlotId,
|
||||
const std::string& iniPath,
|
||||
HWND hVccWnd,
|
||||
const cpak_callbacks& cpak_callbacks);
|
||||
|
||||
cartridge_loader_result load_cpak_cartridge(
|
||||
const std::string& filename,
|
||||
slot_id_type SlotId,
|
||||
const std::string& iniPath,
|
||||
HWND hVccWnd,
|
||||
const cpak_callbacks& cpak_callbacks);
|
||||
|
||||
// Return load error string per cartridge load status
|
||||
std::string cartridge_load_error_string(
|
||||
const cartridge_loader_status error_status);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
//#define USE_LOGGING
|
||||
//======================================================================
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
// Vcc is Copyright 2015 by Joseph Forgione
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software, you can redistribute
|
||||
// and/or modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation, either version 3 of
|
||||
// the License, or (at your option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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 VCC (Virtual Color Computer). If not, see
|
||||
// <http://www.gnu.org/licenses/>.
|
||||
//======================================================================
|
||||
|
||||
#include <vcc/bus/cartridge_menu.h>
|
||||
#include <vcc/bus/cartridge_menuitem.h>
|
||||
#include <vcc/util/logger.h>
|
||||
#include <vcc/util/coreutil.h>
|
||||
|
||||
namespace VCC::Bus {
|
||||
|
||||
// Global cart menu for vcc.exe
|
||||
// DLL's should never use this
|
||||
cartridge_menu gVccCartMenu;
|
||||
|
||||
// Cart menu for DLL use
|
||||
// Vcc or pakinterface should not use this
|
||||
cartridge_menu gDllCartMenu;
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// Copy a menu item to the API safe structure
|
||||
// ------------------------------------------------------------
|
||||
bool cartridge_menu::copy_item(menu_item_entry& out, size_t index) const
|
||||
{
|
||||
if (index >= menu_.size() || index >= MAX_MENU_ITEMS)
|
||||
return false;
|
||||
|
||||
const auto& src = menu_[index];
|
||||
|
||||
// std::min won't work for legacy builds so ...
|
||||
// size_t n = sizeof(out.name) -1;
|
||||
// n = src.name.size() < n ? src.name.size() : n;
|
||||
size_t n = VCC::Util::min_compat(src.name.size(), sizeof(out.name) - 1);
|
||||
|
||||
memcpy(out.name, src.name.data(), n);
|
||||
out.name[n] = '\0';
|
||||
|
||||
out.menu_id = src.menu_id;
|
||||
out.type = src.type;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// Draw the menu. DLL's should not call this
|
||||
// ------------------------------------------------------------
|
||||
HMENU cartridge_menu::draw(HWND hWnd, int position, const std::string& title)
|
||||
{
|
||||
auto hMenuBar = GetMenu(hWnd);
|
||||
|
||||
// Erase the Existing Cartridge Menu. Vcc.rc defines
|
||||
// a dummy Cartridge menu to preserve it's place.
|
||||
DeleteMenu(hMenuBar,position,MF_BYPOSITION);
|
||||
|
||||
// Create first sub menu item
|
||||
HMENU hMenu = CreatePopupMenu();
|
||||
HMENU hMenu0 = hMenu;
|
||||
|
||||
MENUITEMINFO Mii{};
|
||||
memset(&Mii,0,sizeof(MENUITEMINFO));
|
||||
Mii.cbSize= sizeof(MENUITEMINFO);
|
||||
|
||||
// Create title bar item
|
||||
Mii.fMask = MIIM_TYPE | MIIM_SUBMENU | MIIM_ID; // setting the submenu id
|
||||
Mii.fType = MFT_STRING; // Type is a string
|
||||
Mii.hSubMenu = hMenu;
|
||||
Mii.dwTypeData = const_cast<LPSTR>(title.c_str());
|
||||
Mii.cch = title.size();
|
||||
InsertMenuItem(hMenuBar,position,TRUE,&Mii);
|
||||
|
||||
// Create sub menus in order
|
||||
unsigned int pos = 0u;
|
||||
for (CartMenuItem item : menu_) {
|
||||
DLOG_C("%4d %d '%s'\n",item.menu_id,item.type,item.name.c_str());
|
||||
switch (item.type) {
|
||||
case MIT_Head:
|
||||
hMenu = CreatePopupMenu();
|
||||
Mii.fMask = MIIM_TYPE | MIIM_SUBMENU | MIIM_ID;
|
||||
Mii.fType = MFT_STRING;
|
||||
Mii.hSubMenu = hMenu;
|
||||
Mii.dwTypeData = const_cast<LPSTR>(item.name.c_str());
|
||||
Mii.cch = item.name.size();
|
||||
InsertMenuItem(hMenu0,pos,TRUE,&Mii);
|
||||
pos++;
|
||||
break;
|
||||
case MIT_Slave:
|
||||
Mii.fMask = MIIM_TYPE | MIIM_ID;
|
||||
Mii.fType = MFT_STRING;
|
||||
Mii.wID = item.menu_id;
|
||||
Mii.hSubMenu = hMenu;
|
||||
Mii.dwTypeData = const_cast<LPSTR>(item.name.c_str());
|
||||
Mii.cch=item.name.size();
|
||||
InsertMenuItem(hMenu,0,FALSE,&Mii);
|
||||
break;
|
||||
case MIT_Seperator:
|
||||
Mii.fMask = MIIM_TYPE;
|
||||
Mii.hSubMenu = hMenuBar;
|
||||
Mii.dwTypeData = "";
|
||||
Mii.cch=0;
|
||||
Mii.fType = MF_SEPARATOR;
|
||||
InsertMenuItem(hMenu0,pos,TRUE,&Mii);
|
||||
pos++;
|
||||
break;
|
||||
case MIT_StandAlone:
|
||||
Mii.fMask = MIIM_TYPE | MIIM_ID;
|
||||
Mii.wID = item.menu_id;
|
||||
Mii.hSubMenu = hMenuBar;
|
||||
Mii.dwTypeData = const_cast<LPSTR>(item.name.c_str());
|
||||
Mii.cch=item.name.size();
|
||||
Mii.fType = MFT_STRING;
|
||||
InsertMenuItem(hMenu0,pos,TRUE,&Mii);
|
||||
pos++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
DrawMenuBar(hWnd);
|
||||
return hMenu0;
|
||||
}
|
||||
|
||||
} // namespace VCC::Bus
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
//#define USE_LOGGING
|
||||
//======================================================================
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
// Vcc is Copyright 2015 by Joseph Forgione
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software, you can redistribute
|
||||
// and/or modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation, either version 3 of
|
||||
// the License, or (at your option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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 VCC (Virtual Color Computer). If not, see
|
||||
// <http://www.gnu.org/licenses/>.
|
||||
//======================================================================
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Windows.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <vcc/util/logger.h>
|
||||
#include <vcc/bus/cartridge_menuitem.h>
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// cartridge_menu objects hold cartridge menu lists.
|
||||
//
|
||||
// Every cartridge DLL that uses menu items holds a cartridge_menu
|
||||
// list for it's items. When it modifies what is in it's list it posts
|
||||
// a menu changed message to Vcc main.
|
||||
//
|
||||
// Menu building and drawing is controlled by the PakInterface and
|
||||
// is initiated by VCC startup or by a menu changed message from
|
||||
// one of the catridge DLL's.
|
||||
//
|
||||
// PakInterface builds the master cartridge_menu starting with it's
|
||||
// own items. It then calls PakGetMenuItems to get and append items
|
||||
// from the DLL in slot 0 (AKA the boot or side slot).
|
||||
//
|
||||
// If the MPI is in slot 0 it responds with it's own list plus the
|
||||
// lists from each cartridge it has loaded, each via PakGetMenuItems
|
||||
// calls to them.
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
namespace VCC::Bus {
|
||||
|
||||
// A single menu entry
|
||||
struct CartMenuItem {
|
||||
std::string name;
|
||||
size_t menu_id;
|
||||
MenuItemType type;
|
||||
};
|
||||
|
||||
class cartridge_menu {
|
||||
|
||||
private:
|
||||
std::vector<CartMenuItem> menu_;
|
||||
|
||||
public:
|
||||
|
||||
// Constructor
|
||||
cartridge_menu() {}
|
||||
|
||||
// Clear all items
|
||||
void clear() { menu_.clear(); }
|
||||
|
||||
// Add a single item
|
||||
void add(const std::string& name, unsigned int menu_id, MenuItemType type)
|
||||
{
|
||||
menu_.push_back({ name, menu_id, type });
|
||||
}
|
||||
|
||||
// fetch an item
|
||||
CartMenuItem& operator[](size_t i)
|
||||
{
|
||||
return menu_[i];
|
||||
}
|
||||
|
||||
// Number of items in list
|
||||
size_t size() const { return menu_.size(); }
|
||||
|
||||
// copy an item to API structure
|
||||
bool copy_item(menu_item_entry& out, size_t index) const;
|
||||
|
||||
// debug logging
|
||||
void log()
|
||||
{
|
||||
for (size_t i = 0; i < menu_.size(); ++i) {
|
||||
DLOG_C("%s %d %d\n", menu_[i].name.c_str(),menu_[i].menu_id,menu_[i].type);
|
||||
}
|
||||
}
|
||||
|
||||
// Draw the menu (implemented in catridge_menu.cpp)
|
||||
HMENU draw(HWND hWnd,
|
||||
int position=3,
|
||||
const std::string& title="Cartridge");
|
||||
};
|
||||
|
||||
// instance for Vcc.cpp and pakinterface to use. Host only!
|
||||
extern cartridge_menu gVccCartMenu;
|
||||
|
||||
// instance for dll's to use. DLL's only!
|
||||
extern cartridge_menu gDllCartMenu;
|
||||
|
||||
} // namespace VCC::Bus
|
||||
@@ -0,0 +1,45 @@
|
||||
//======================================================================
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
// Vcc is Copyright 2015 by Joseph Forgione
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software, you can redistribute
|
||||
// and/or modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation, either version 3 of
|
||||
// the License, or (at your option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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 VCC (Virtual Color Computer). If not, see
|
||||
// <http://www.gnu.org/licenses/>.
|
||||
//======================================================================
|
||||
|
||||
#pragma once
|
||||
|
||||
enum MenuItemType
|
||||
{
|
||||
MIT_Head, // Menu header, should have slave items
|
||||
MIT_Slave, // Slave items with associated control in header submenu.
|
||||
MIT_StandAlone, // Menu item with an associated control
|
||||
MIT_Seperator, // Draws a horizontal line to seperate groups of menu items
|
||||
};
|
||||
|
||||
// Define a C API safe struct for DLL ItemList export
|
||||
// PakGetMenuItem(menu_item_entry* items, size_t* count)
|
||||
struct menu_item_entry
|
||||
{
|
||||
char name[256];
|
||||
size_t menu_id;
|
||||
MenuItemType type;
|
||||
};
|
||||
|
||||
// Sanity check maximum menu items
|
||||
constexpr auto MAX_MENU_ITEMS = 200u;
|
||||
|
||||
// MenuItem ID <-> Command ID mapping
|
||||
constexpr auto MID_CONTROL = 5000;
|
||||
constexpr int ControlId(int id) { return MID_CONTROL + id; }
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//----------------------------------------------------
|
||||
// Defines messages sent to VCC WndProc from cart DLL's
|
||||
//----------------------------------------------------
|
||||
|
||||
#pragma once
|
||||
#include <windows.h>
|
||||
#include <cstdint>
|
||||
|
||||
//----------------------------------------------------
|
||||
// Structures for moving data across DLL boundaries
|
||||
// These require a fixed size buffer to contain the data
|
||||
// buffer must be an array of atomic C types
|
||||
// size is the size of the data buffer.
|
||||
//----------------------------------------------------
|
||||
|
||||
struct CartLoadRequest {
|
||||
const uint32_t size;
|
||||
char pluginPath[MAX_PATH];
|
||||
CartLoadRequest() noexcept
|
||||
: size(MAX_PATH), pluginPath{} {}
|
||||
};
|
||||
|
||||
struct CartNameReply {
|
||||
static constexpr uint32_t BUFSIZE = 128;
|
||||
const uint32_t size;
|
||||
char name[BUFSIZE];
|
||||
CartNameReply() noexcept
|
||||
: size(BUFSIZE), name{} {}
|
||||
};
|
||||
|
||||
struct CartDescReply {
|
||||
static constexpr uint32_t BUFSIZE = 512;
|
||||
const uint32_t size;
|
||||
char description[BUFSIZE];
|
||||
CartDescReply() noexcept
|
||||
: size(BUFSIZE), description{} {}
|
||||
};
|
||||
|
||||
//----------------------------------------------------
|
||||
// DLL <-> VCC Messages and helpers
|
||||
//----------------------------------------------------
|
||||
|
||||
// Fatal (any plugin)
|
||||
inline constexpr uint32_t WM_VCC_FATAL = WM_APP + 100;
|
||||
inline LRESULT SendFatalError(HWND hwnd, uint32_t err) {
|
||||
return SendMessage(hwnd,WM_VCC_FATAL, err, 0);
|
||||
}
|
||||
|
||||
// Hard Reset (any plugin)
|
||||
inline constexpr uint32_t WM_VCC_CPU_RESET = WM_APP + 101;
|
||||
inline LRESULT SendHardReset(HWND hwnd) {
|
||||
return SendMessage(hwnd,WM_VCC_CPU_RESET, 0, 0);
|
||||
}
|
||||
|
||||
// Update menu request (any plugin)
|
||||
inline constexpr uint32_t WM_VCC_UPD_MENU = WM_APP + 102;
|
||||
inline LRESULT SendMenuUpdate(HWND hwnd) {
|
||||
return SendMessage(hwnd,WM_VCC_UPD_MENU, 0, 0);
|
||||
}
|
||||
|
||||
// Soft RESET (any plugin)
|
||||
inline constexpr uint32_t WM_VCC_SOFT_RESET = WM_APP + 103;
|
||||
inline LRESULT SendSoftReset(HWND hwnd) {
|
||||
return SendMessage(hwnd,WM_VCC_SOFT_RESET, 0, 0);
|
||||
}
|
||||
|
||||
// Set active slot (MPI)
|
||||
inline constexpr uint32_t WM_VCC_SET_ACTIVE_SLOT = WM_APP + 104;
|
||||
inline LRESULT SendActiveSlot(HWND hwnd, uint32_t slotnum) {
|
||||
return SendMessage(hwnd,WM_VCC_SET_ACTIVE_SLOT, slotnum, 0);
|
||||
}
|
||||
|
||||
// Unload slot request (MPI)
|
||||
inline constexpr uint32_t WM_VCC_UNLOAD_SLOT = WM_APP + 105;
|
||||
inline LRESULT SendUnloadSlot(HWND hwnd, uint32_t slotnum) {
|
||||
return SendMessage(hwnd,WM_VCC_UNLOAD_SLOT, slotnum, 0);
|
||||
}
|
||||
|
||||
// Load slot request (MPI)
|
||||
inline constexpr uint32_t WM_VCC_LOAD_SLOT = WM_APP + 106;
|
||||
inline LRESULT SendLoadSlot(HWND hwnd, uint32_t slot, CartLoadRequest& req) {
|
||||
return SendMessage(
|
||||
hwnd,
|
||||
WM_VCC_LOAD_SLOT,
|
||||
static_cast<WPARAM>(slot),
|
||||
reinterpret_cast<LPARAM>(&req)
|
||||
);
|
||||
}
|
||||
|
||||
// Request name of cartridge in slot (MPI)
|
||||
inline constexpr uint32_t WM_VCC_GET_CART_NAME = WM_APP + 107;
|
||||
inline LRESULT GetCartName(HWND hwnd, uint32_t slot, CartNameReply& rpy) {
|
||||
return SendMessage(
|
||||
hwnd,
|
||||
WM_VCC_GET_CART_NAME,
|
||||
static_cast<WPARAM>(slot),
|
||||
reinterpret_cast<LPARAM>(&rpy)
|
||||
);
|
||||
}
|
||||
|
||||
// Request desription of cartridge in slot (MPI)
|
||||
inline constexpr uint32_t WM_VCC_GET_CART_DESCRIPT = WM_APP + 108;
|
||||
inline LRESULT GetCartDesc(HWND hwnd, uint32_t slot, CartDescReply& rpy) {
|
||||
return SendMessage(
|
||||
hwnd,
|
||||
WM_VCC_GET_CART_DESCRIPT,
|
||||
static_cast<WPARAM>(slot),
|
||||
reinterpret_cast<LPARAM>(&rpy)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,203 @@
|
||||
//#define USE_LOGGING
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <vcc/bus/cpak_cartridge.h>
|
||||
#include <vcc/bus/cartridge_menuitem.h>
|
||||
#include <stdexcept>
|
||||
#include <vcc/util/logger.h>
|
||||
|
||||
namespace VCC::Core
|
||||
{
|
||||
namespace
|
||||
{
|
||||
template<class Type_>
|
||||
Type_ GetImportedProcAddress(HMODULE module, LPCSTR procName, Type_ defaultFunction)
|
||||
{
|
||||
const auto importedFunction(reinterpret_cast<Type_>(
|
||||
GetProcAddress(module, procName))
|
||||
);
|
||||
return importedFunction ? importedFunction : defaultFunction;
|
||||
}
|
||||
|
||||
void default_stop()
|
||||
{}
|
||||
|
||||
void default_menu_item_clicked(unsigned char)
|
||||
{}
|
||||
|
||||
void default_heartbeat()
|
||||
{}
|
||||
|
||||
void default_write_port(unsigned char, unsigned char)
|
||||
{}
|
||||
|
||||
unsigned char default_read_port(unsigned char)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
unsigned char default_read_memory_byte(unsigned short)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
void default_status(char* text_buffer, size_t buffer_size)
|
||||
{
|
||||
*text_buffer = 0;
|
||||
}
|
||||
|
||||
unsigned short default_sample_audio()
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
void default_reset()
|
||||
{}
|
||||
|
||||
const char* default_get_empty_string()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
bool default_get_menu_item(menu_item_entry*, size_t)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
cpak_cartridge::cpak_cartridge(
|
||||
HMODULE module_handle,
|
||||
slot_id_type SlotId,
|
||||
path_type configuration_path,
|
||||
const HWND hVccWnd,
|
||||
const cpak_callbacks& cpak_callbacks)
|
||||
:
|
||||
handle_(module_handle),
|
||||
SlotId_(SlotId),
|
||||
configuration_path_(move(configuration_path)),
|
||||
hVccWnd_(hVccWnd),
|
||||
cpak_callbacks_(cpak_callbacks),
|
||||
initialize_(GetImportedProcAddress<PakInitializeModuleFunction>(
|
||||
module_handle, "PakInitialize", nullptr)),
|
||||
terminate_(GetImportedProcAddress(
|
||||
module_handle, "PakTerminate", default_stop)),
|
||||
get_name_(GetImportedProcAddress(
|
||||
module_handle, "PakGetName", default_get_empty_string)),
|
||||
get_catalog_id_(GetImportedProcAddress(
|
||||
module_handle, "PakGetCatalogId", default_get_empty_string)),
|
||||
get_description_(GetImportedProcAddress(
|
||||
module_handle, "PakGetDescription", default_get_empty_string)),
|
||||
reset_(GetImportedProcAddress(
|
||||
module_handle, "PakReset", default_reset)),
|
||||
heartbeat_(GetImportedProcAddress(
|
||||
module_handle, "PakProcessHorizontalSync", default_heartbeat)),
|
||||
status_(GetImportedProcAddress(
|
||||
module_handle, "PakGetStatus", default_status)),
|
||||
write_port_(GetImportedProcAddress(
|
||||
module_handle, "PakWritePort", default_write_port)),
|
||||
read_port_(GetImportedProcAddress(
|
||||
module_handle, "PakReadPort", default_read_port)),
|
||||
read_memory_byte_(GetImportedProcAddress(
|
||||
module_handle, "PakReadMemoryByte", default_read_memory_byte)),
|
||||
sample_audio_(GetImportedProcAddress(
|
||||
module_handle, "PakSampleAudio", default_sample_audio)),
|
||||
menu_item_clicked_(GetImportedProcAddress(
|
||||
module_handle, "PakMenuItemClicked", default_menu_item_clicked)),
|
||||
get_menu_item_(GetImportedProcAddress(
|
||||
module_handle, "PakGetMenuItem", default_get_menu_item))
|
||||
{
|
||||
if (initialize_ == nullptr)
|
||||
{
|
||||
throw std::invalid_argument("Cannot initialize pak interface. Module does not export PakInitialize.");
|
||||
}
|
||||
}
|
||||
|
||||
cpak_cartridge::name_type cpak_cartridge::name() const
|
||||
{
|
||||
return get_name_();
|
||||
}
|
||||
|
||||
cpak_cartridge::catalog_id_type cpak_cartridge::catalog_id() const
|
||||
{
|
||||
return get_catalog_id_();
|
||||
}
|
||||
|
||||
cpak_cartridge::description_type cpak_cartridge:: description() const
|
||||
{
|
||||
return get_description_();
|
||||
}
|
||||
|
||||
void cpak_cartridge::start()
|
||||
{
|
||||
DLOG_C("+++ cpak_cartridge::start %d +++\n",SlotId_);
|
||||
initialize_(SlotId_, configuration_path_.c_str(), hVccWnd_, &cpak_callbacks_);
|
||||
}
|
||||
|
||||
void cpak_cartridge::stop()
|
||||
{
|
||||
terminate_();
|
||||
}
|
||||
|
||||
void cpak_cartridge::reset()
|
||||
{
|
||||
reset_();
|
||||
}
|
||||
|
||||
void cpak_cartridge::process_horizontal_sync()
|
||||
{
|
||||
heartbeat_();
|
||||
}
|
||||
|
||||
void cpak_cartridge::status(char* text_buffer, size_t buffer_size)
|
||||
{
|
||||
status_(text_buffer, buffer_size);
|
||||
}
|
||||
|
||||
void cpak_cartridge::write_port(unsigned char port_id, unsigned char value)
|
||||
{
|
||||
write_port_(port_id, value);
|
||||
}
|
||||
|
||||
unsigned char cpak_cartridge::read_port(unsigned char port_id)
|
||||
{
|
||||
return read_port_(port_id);
|
||||
}
|
||||
|
||||
unsigned char cpak_cartridge::read_memory_byte(unsigned short memory_address)
|
||||
{
|
||||
return read_memory_byte_(memory_address);
|
||||
}
|
||||
|
||||
unsigned short cpak_cartridge::sample_audio()
|
||||
{
|
||||
return sample_audio_();
|
||||
}
|
||||
|
||||
void cpak_cartridge::menu_item_clicked(unsigned char menu_item_id)
|
||||
{
|
||||
menu_item_clicked_(menu_item_id);
|
||||
}
|
||||
|
||||
bool cpak_cartridge::get_menu_item(menu_item_entry* item, size_t index)
|
||||
{
|
||||
return get_menu_item_(item, index);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
#include <vcc/bus/cartridge.h>
|
||||
#include <vcc/bus/cpak_cartridge_definitions.h>
|
||||
#include <vcc/bus/cartridge_menuitem.h>
|
||||
#include <Windows.h>
|
||||
#include <string>
|
||||
|
||||
namespace VCC::Core
|
||||
{
|
||||
|
||||
class cpak_cartridge: public cartridge
|
||||
{
|
||||
public:
|
||||
using path_type = std::string;
|
||||
|
||||
public:
|
||||
cpak_cartridge(
|
||||
HMODULE module_handle, // Cartridge filename
|
||||
slot_id_type const SlotId, // Slot id
|
||||
path_type configuration_path, // Path of ini file
|
||||
HWND hVccWnd, // Handle to main VCC window proc
|
||||
const cpak_callbacks& cpak_callbacks); // Callbacks
|
||||
|
||||
name_type name() const override;
|
||||
catalog_id_type catalog_id() const override;
|
||||
description_type description() const override;
|
||||
|
||||
void start() override;
|
||||
void stop() override;
|
||||
|
||||
void reset() override;
|
||||
void process_horizontal_sync() override;
|
||||
void status(char* text_buffer, size_t buffer_size) override;
|
||||
void write_port(unsigned char port_id, unsigned char value) override;
|
||||
unsigned char read_port(unsigned char port_id) override;
|
||||
unsigned char read_memory_byte(unsigned short memory_address) override;
|
||||
unsigned short sample_audio() override;
|
||||
void menu_item_clicked(unsigned char menu_item_id) override;
|
||||
bool get_menu_item(menu_item_entry* item, size_t index) override;
|
||||
|
||||
private:
|
||||
|
||||
const HMODULE handle_;
|
||||
slot_id_type SlotId_;
|
||||
const HWND hVccWnd_;
|
||||
const path_type configuration_path_;
|
||||
const cpak_callbacks cpak_callbacks_;
|
||||
|
||||
// imported module functions
|
||||
const PakInitializeModuleFunction initialize_;
|
||||
const PakTerminateModuleFunction terminate_;
|
||||
const PakGetNameModuleFunction get_name_;
|
||||
const PakGetCatalogIdModuleFunction get_catalog_id_;
|
||||
const PakGetDescriptionModuleFunction get_description_;
|
||||
const PakResetModuleFunction reset_;
|
||||
const PakHeartBeatModuleFunction heartbeat_;
|
||||
const PakGetStatusModuleFunction status_;
|
||||
const PakWritePortModuleFunction write_port_;
|
||||
const PakReadPortModuleFunction read_port_;
|
||||
const PakReadMemoryByteModuleFunction read_memory_byte_;
|
||||
const PakSampleAudioModuleFunction sample_audio_;
|
||||
const PakMenuItemClickedModuleFunction menu_item_clicked_;
|
||||
const PakGetMenuItemFunction get_menu_item_;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//===============================================================//
|
||||
// This defines the exports that can be used by plugins //
|
||||
//===============================================================//
|
||||
|
||||
#pragma once
|
||||
#include "../../../../../src/core/Interrupts.h"
|
||||
#include <vcc/bus/plugin_callbacks.h>
|
||||
#include <vcc/bus/cartridge_menuitem.h>
|
||||
#include <windows.h>
|
||||
|
||||
//----------------------------------------------------------------
|
||||
// ABI identity is based on SlotId; 0 = Boot Slot, 1-4 = MPI slots
|
||||
//----------------------------------------------------------------
|
||||
extern "C"
|
||||
{
|
||||
// Cartridge exports. At least PakInitilizeModuleFunction must be implimented
|
||||
using PakInitializeModuleFunction = void (*)(
|
||||
slot_id_type SlotId, // SlotId
|
||||
const char* const configuration_path, // Path of ini file
|
||||
HWND hVccWnd, // VCC proxy handle
|
||||
const cpak_callbacks* const context); // Callbacks
|
||||
using PakTerminateModuleFunction = void (*)();
|
||||
using PakGetNameModuleFunction = const char* (*)();
|
||||
using PakGetCatalogIdModuleFunction = const char* (*)();
|
||||
using PakGetDescriptionModuleFunction = const char* (*)();
|
||||
using PakResetModuleFunction = void (*)();
|
||||
using PakHeartBeatModuleFunction = void (*)();
|
||||
using PakGetStatusModuleFunction = void (*)(char* text_buffer, size_t buffer_size);
|
||||
using PakWritePortModuleFunction = void (*)(unsigned char port, unsigned char value);
|
||||
using PakReadMemoryByteModuleFunction = unsigned char (*)(unsigned short address);
|
||||
using PakReadPortModuleFunction = unsigned char (*)(unsigned char port);
|
||||
using PakSampleAudioModuleFunction = unsigned short (*)();
|
||||
using PakMenuItemClickedModuleFunction = void (*)(unsigned char itemId);
|
||||
using PakGetMenuItemFunction = bool (*)(menu_item_entry* item, size_t index);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
#include <vcc/bus/basic_cartridge.h>
|
||||
|
||||
// Basically not a cartridge but by another name
|
||||
namespace VCC::Core
|
||||
{
|
||||
class null_cartridge : public basic_cartridge
|
||||
{};
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//=============================================================//
|
||||
// This defines the callbacks used by all cartridges (plugins) //
|
||||
//=============================================================//
|
||||
|
||||
#pragma once
|
||||
#include "../../../../../src/core/Interrupts.h"
|
||||
#include <cstddef> // for std::size_t
|
||||
|
||||
//----------------------------------------------------------------
|
||||
// ABI identity is based on SlotId; 0 = Boot Slot, 1-4 = MPI slots
|
||||
//----------------------------------------------------------------
|
||||
using slot_id_type = std::size_t;
|
||||
|
||||
extern "C"
|
||||
{
|
||||
using PakWriteMemoryByteHostCallback = void (*)
|
||||
(slot_id_type SlotId, unsigned char value, unsigned short address);
|
||||
using PakReadMemoryByteHostCallback = unsigned char (*)
|
||||
(slot_id_type SlotId, unsigned short address);
|
||||
using PakAssertCartridgeLineHostCallback = void (*)
|
||||
(slot_id_type SlotId, bool lineState);
|
||||
using PakAssertInteruptHostCallback = void (*)
|
||||
(slot_id_type SlotId, Interrupt interrupt, InterruptSource interrupt_source);
|
||||
|
||||
// Plugin (cartridge) callbacks
|
||||
struct cpak_callbacks
|
||||
{
|
||||
const PakAssertInteruptHostCallback assert_interrupt;
|
||||
const PakAssertCartridgeLineHostCallback assert_cartridge_line;
|
||||
const PakWriteMemoryByteHostCallback write_memory_byte;
|
||||
const PakReadMemoryByteHostCallback read_memory_byte;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <vcc/bus/proxy_msgwin.h>
|
||||
#include <vcc/bus/cartridge_messages.h>
|
||||
#include <vcc/util/logger.h>
|
||||
|
||||
namespace VCC::Core
|
||||
{
|
||||
HWND ProxyMsgWin::h_proxy_ = nullptr;
|
||||
HWND ProxyMsgWin::h_target_ = nullptr;
|
||||
const char PROXY_CLASS[] = "ProxyMessageWindow";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Private message handler forwards messages defined in cartridge_messages.h
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
LRESULT CALLBACK ProxyMsgWin::ProxyProc(
|
||||
HWND hwnd,
|
||||
UINT uMsg,
|
||||
WPARAM wParam,
|
||||
LPARAM lParam)
|
||||
{
|
||||
if (uMsg >= WM_APP + 100 && uMsg <= WM_APP + 200) {
|
||||
DLOG_C("DLL msg %d\n", uMsg);
|
||||
return SendMessageA(h_target_, uMsg, wParam, lParam);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Set up message only proxy for DLL messages. This gets called each time
|
||||
// DirectDrawInterface creates or recreates the VCC WndProc. This happens
|
||||
// at VCC startup and when full screen mode is toggled.
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
HWND ProxyMsgWin::SetProxy(HWND h_VccWndProc)
|
||||
{
|
||||
// Save target handle (VCC WndProc)
|
||||
h_target_ = h_VccWndProc;
|
||||
|
||||
// One time create proxy msg window
|
||||
if (h_proxy_ == nullptr) {
|
||||
|
||||
HINSTANCE h_inst = GetModuleHandle(nullptr);
|
||||
|
||||
WNDCLASSA wc = { };
|
||||
wc.lpfnWndProc = ProxyProc;
|
||||
wc.hInstance = h_inst;
|
||||
wc.lpszClassName = PROXY_CLASS;
|
||||
|
||||
if (!RegisterClassA(&wc)) {
|
||||
const DWORD err = GetLastError();
|
||||
if (err != ERROR_CLASS_ALREADY_EXISTS) {
|
||||
DLOG_C("RegisterClass failed, error = %lu\n", err);
|
||||
return h_VccWndProc; // fallback
|
||||
}
|
||||
}
|
||||
|
||||
h_proxy_ = CreateWindowExA(
|
||||
0, PROXY_CLASS, "ProxyWindow",
|
||||
0, 0, 0, 0, 0, HWND_MESSAGE,
|
||||
nullptr, h_inst, nullptr
|
||||
);
|
||||
|
||||
if (h_proxy_ == nullptr) {
|
||||
const DWORD err = GetLastError();
|
||||
DLOG_C("CreateWindowExA failed, error = %lu\n", err);
|
||||
return h_VccWndProc;
|
||||
}
|
||||
}
|
||||
|
||||
// Return proxy handle
|
||||
return h_proxy_;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// VCC’s main window handle is not stable. When entering or leaving fullscreen
|
||||
// mode, the system may destroy and recreate the main window, causing its HWND
|
||||
// to change. This breaks messaging from other program units or dynamically
|
||||
// loaded DLLs that rely on a fixed window handle.
|
||||
//
|
||||
// To provide a stable messaging endpoint, VCC creates an invisible proxy
|
||||
// message window. This proxy window has a permanent HWND and receives
|
||||
// DLL messages. It forwards these messages to VCC’s main WndProc.
|
||||
//
|
||||
// Whenever the main window is recreated, the proxy updates its forwarding
|
||||
// target to the new HWND.
|
||||
//
|
||||
// The proxy window ensures reliable message delivery to VCC’s WndProc even
|
||||
// when the main window handle changes.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#pragma once
|
||||
#include <windows.h>
|
||||
|
||||
namespace VCC::Core
|
||||
{
|
||||
class ProxyMsgWin
|
||||
{
|
||||
public:
|
||||
static HWND SetProxy(HWND hwnd);
|
||||
private:
|
||||
static LRESULT CALLBACK ProxyProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
|
||||
static HWND h_proxy_;
|
||||
static HWND h_target_;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
//#define USE_LOGGING
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#include <vcc/bus/rom_cartridge.h>
|
||||
#include <vcc/util/logger.h>
|
||||
|
||||
namespace VCC::Core
|
||||
{
|
||||
rom_cartridge::rom_cartridge(
|
||||
const cpak_callbacks& callbacks,
|
||||
name_type name,
|
||||
catalog_id_type catalog_id,
|
||||
buffer_type buffer,
|
||||
bool enable_bank_switching,
|
||||
slot_id_type slotId
|
||||
)
|
||||
: callbacks_(callbacks),
|
||||
name_(move(name)),
|
||||
catalog_id_(move(catalog_id)),
|
||||
buffer_(move(buffer)),
|
||||
enable_bank_switching_(enable_bank_switching),
|
||||
bank_offset_(0),
|
||||
slotId_(slotId)
|
||||
{
|
||||
DLOG_C("rom_cartridge ctor slot %d %s\n",slotId_, name_.c_str());
|
||||
}
|
||||
|
||||
rom_cartridge::name_type rom_cartridge::name() const
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
|
||||
rom_cartridge::catalog_id_type rom_cartridge::catalog_id() const
|
||||
{
|
||||
return catalog_id_;
|
||||
}
|
||||
|
||||
rom_cartridge::description_type rom_cartridge::description() const
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
void rom_cartridge::reset()
|
||||
{
|
||||
DLOG_C("rom_cartridge reset\n");
|
||||
bank_offset_ = 0;
|
||||
}
|
||||
|
||||
void rom_cartridge::write_port(unsigned char port_id, unsigned char value)
|
||||
{
|
||||
if (enable_bank_switching_ && port_id == 0x40)
|
||||
{
|
||||
bank_offset_ = (value & 0x0f) << 14;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char rom_cartridge::read_memory_byte(unsigned short memory_address)
|
||||
{
|
||||
return buffer_[((memory_address & 32767) + bank_offset_) % buffer_.size()];
|
||||
}
|
||||
|
||||
void rom_cartridge::initialize_bus()
|
||||
{
|
||||
DLOG_C("rom_cartridge.initialize_bus assert line slot %d\n",slotId_);
|
||||
callbacks_.assert_cartridge_line(slotId_,true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
#include <vcc/bus/basic_cartridge.h>
|
||||
#include <vcc/bus/cartridge_callbacks.h> //depreciated
|
||||
#include <vcc/bus/plugin_callbacks.h>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace VCC::Core
|
||||
{
|
||||
class rom_cartridge : public basic_cartridge
|
||||
{
|
||||
public:
|
||||
using callbacks_type = ::VCC::Core::cartridge_callbacks;
|
||||
using buffer_type = std::vector<uint8_t>;
|
||||
using size_type = std::size_t;
|
||||
|
||||
public:
|
||||
using basic_cartridge::basic_cartridge;
|
||||
|
||||
rom_cartridge(
|
||||
const cpak_callbacks& callbacks,
|
||||
name_type name,
|
||||
catalog_id_type catalog_id,
|
||||
buffer_type buffer,
|
||||
bool enable_bank_switching,
|
||||
slot_id_type slotId
|
||||
);
|
||||
|
||||
name_type name() const override;
|
||||
catalog_id_type catalog_id() const override;
|
||||
description_type description() const override;
|
||||
|
||||
void reset() override;
|
||||
void write_port(unsigned char port_id, unsigned char value) override;
|
||||
unsigned char read_memory_byte(unsigned short memory_address) override;
|
||||
|
||||
protected:
|
||||
void initialize_bus() override;
|
||||
|
||||
private:
|
||||
const cpak_callbacks& callbacks_;
|
||||
const name_type name_;
|
||||
const catalog_id_type catalog_id_;
|
||||
const buffer_type buffer_;
|
||||
const bool enable_bank_switching_;
|
||||
size_type bank_offset_;
|
||||
const std::string filename_;
|
||||
slot_id_type slotId_;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
Copyright 2015 by Joseph Forgione
|
||||
This file is part of VCC (Virtual Color Computer).
|
||||
|
||||
VCC (Virtual Color Computer) 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 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
VCC (Virtual Color Computer) 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 VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
/************************************************************************
|
||||
* Basically simulates the Dallas DS1315 Real Time Clock *
|
||||
* *
|
||||
* *
|
||||
* *
|
||||
* *
|
||||
* *
|
||||
************************************************************************/
|
||||
#include <vcc/devices/cloud9.h>
|
||||
#include <Windows.h>
|
||||
|
||||
namespace VCC::Device::rtc
|
||||
{
|
||||
|
||||
unsigned char cloud9::read_port(unsigned short port)
|
||||
{
|
||||
unsigned char ret_val = 0;
|
||||
|
||||
//FF78=0 FF79=1 FF7C talkback
|
||||
switch (port)
|
||||
{
|
||||
case 0x78:
|
||||
case 0x79:
|
||||
ret_val = 0;
|
||||
CurrentBit = port & 1;
|
||||
InBuffer = InBuffer >> 1;
|
||||
InBuffer = InBuffer | (CurrentBit << 63);
|
||||
if (CookieRecived == 1)
|
||||
{
|
||||
if (BitCounter == 0)
|
||||
{
|
||||
set_time();
|
||||
CookieRecived = 0;
|
||||
}
|
||||
BitCounter--;
|
||||
}
|
||||
if (InBuffer == 0x5CA33AC55CA33AC5)
|
||||
{
|
||||
GetLocalTime(&now);
|
||||
OutBuffer = 0;
|
||||
OutBuffer = ((now.wYear % 100) / 10) + 10;
|
||||
OutBuffer <<= 4;
|
||||
OutBuffer |= now.wYear % 10;
|
||||
OutBuffer <<= 4;
|
||||
OutBuffer |= now.wMonth / 10;
|
||||
OutBuffer <<= 4;
|
||||
OutBuffer |= now.wMonth % 10;
|
||||
OutBuffer <<= 4;
|
||||
OutBuffer |= now.wDay / 10;
|
||||
OutBuffer <<= 4;
|
||||
OutBuffer |= now.wDay % 10;
|
||||
OutBuffer <<= 4;
|
||||
//Skip Osc and Reset
|
||||
OutBuffer <<= 4;
|
||||
OutBuffer |= now.wDayOfWeek;
|
||||
OutBuffer <<= 4;
|
||||
TempHour = (unsigned char)now.wHour;
|
||||
AmPmBit = 0;
|
||||
if ((FormatBit == 1) & (TempHour > 12)) //1=12 hour mode 1=PM
|
||||
{
|
||||
TempHour -= 12;
|
||||
AmPmBit = 2;
|
||||
}
|
||||
|
||||
OutBuffer |= (3 & ((TempHour / 10) | AmPmBit));
|
||||
OutBuffer <<= 4;
|
||||
|
||||
OutBuffer |= TempHour % 10;
|
||||
OutBuffer <<= 4;
|
||||
OutBuffer |= now.wMinute / 10;
|
||||
OutBuffer <<= 4;
|
||||
OutBuffer |= now.wMinute % 10;
|
||||
OutBuffer <<= 4;
|
||||
OutBuffer |= now.wSecond / 10;
|
||||
OutBuffer <<= 4;
|
||||
OutBuffer |= now.wSecond % 10;
|
||||
OutBuffer <<= 4;
|
||||
OutBuffer |= now.wMilliseconds / 10;
|
||||
OutBuffer <<= 4;
|
||||
OutBuffer |= now.wMilliseconds % 10;
|
||||
InBuffer = 0;
|
||||
BitCounter = 63; //Flag indicating the cookie was recived
|
||||
CookieRecived = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case 0x7C:
|
||||
ret_val = (unsigned char)OutBuffer & 1;
|
||||
|
||||
if (BitCounter > 0)
|
||||
{
|
||||
OutBuffer = (OutBuffer >> 1);
|
||||
BitCounter--;
|
||||
}
|
||||
else
|
||||
{
|
||||
CookieRecived = 0;
|
||||
}
|
||||
break;
|
||||
} //End of port switch
|
||||
|
||||
|
||||
return ret_val;
|
||||
}
|
||||
|
||||
|
||||
void cloud9::set_time()
|
||||
{
|
||||
now.wMilliseconds = (unsigned short)(InBuffer & 15);
|
||||
InBuffer >>= 4;
|
||||
now.wMilliseconds += (unsigned short)((InBuffer & 15) * 10);
|
||||
InBuffer >>= 4;
|
||||
now.wSecond = (unsigned short)(InBuffer & 15);
|
||||
InBuffer >>= 4;
|
||||
now.wSecond += (unsigned short)((InBuffer & 15) * 10);
|
||||
InBuffer >>= 4;
|
||||
now.wMinute = (unsigned short)(InBuffer & 15);
|
||||
InBuffer >>= 4;
|
||||
now.wMinute += (unsigned short)((InBuffer & 15) * 10);
|
||||
InBuffer >>= 4;
|
||||
now.wHour = (unsigned short)(InBuffer & 15);
|
||||
InBuffer >>= 4;
|
||||
TempHour = (unsigned char)(InBuffer & 15); //Here fix me
|
||||
|
||||
FormatBit = (TempHour & 8) >> 3; //1 = 12Hour Mode
|
||||
if (FormatBit == 1)
|
||||
{
|
||||
AmPmBit = (TempHour & 2);
|
||||
now.wHour += (TempHour & 1) * 10; //12 Hour Mode
|
||||
if (AmPmBit == 2)
|
||||
{
|
||||
now.wHour += 12; //convert to 24hour clock
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
now.wHour += (TempHour & 3) * 10; //24 Hour Mode
|
||||
}
|
||||
|
||||
InBuffer >>= 4;
|
||||
now.wDayOfWeek = (unsigned short)(InBuffer & 15);
|
||||
InBuffer >>= 4;
|
||||
InBuffer >>= 4; //Skip OSC and RESET
|
||||
now.wDay = (unsigned short)(InBuffer & 15);
|
||||
InBuffer >>= 4;
|
||||
now.wDay += (unsigned short)((InBuffer & 15) * 10);
|
||||
InBuffer >>= 4;
|
||||
now.wMonth = (unsigned short)(InBuffer & 15);
|
||||
InBuffer >>= 4;
|
||||
now.wMonth += (unsigned short)((InBuffer & 15) * 10);
|
||||
InBuffer >>= 4;
|
||||
now.wYear = (unsigned short)(InBuffer & 15);
|
||||
InBuffer >>= 4;
|
||||
now.wYear += (unsigned short)((InBuffer & 15) * 10);
|
||||
now.wYear += 1900;
|
||||
if (WriteEnabled)
|
||||
{
|
||||
SetLocalTime(&now); // Allow the emulator to fuck with host's time,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void cloud9::set_read_only(bool value)
|
||||
{
|
||||
WriteEnabled = value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
#include <Windows.h>
|
||||
|
||||
namespace VCC::Device::rtc
|
||||
{
|
||||
class cloud9
|
||||
{
|
||||
public:
|
||||
|
||||
void set_read_only(bool value);
|
||||
|
||||
unsigned char read_port(unsigned short port_id);
|
||||
|
||||
private:
|
||||
|
||||
void set_time();
|
||||
|
||||
private:
|
||||
|
||||
SYSTEMTIME now;
|
||||
unsigned __int64 InBuffer = 0;
|
||||
unsigned __int64 OutBuffer = 0;
|
||||
unsigned char BitCounter = 0;
|
||||
unsigned char TempHour = 0;
|
||||
unsigned char AmPmBit = 0;
|
||||
unsigned __int64 CurrentBit = 0;
|
||||
unsigned char FormatBit = 0; //1 = 12Hour Mode
|
||||
unsigned char CookieRecived = 0;
|
||||
unsigned char WriteEnabled = 0;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,309 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#include <vcc/util/DialogOps.h>
|
||||
#include <vcc/util/logger.h>
|
||||
#include <vcc/util/fileutil.h>
|
||||
#include <vcc/util/textutil.h>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
#ifndef _LEGACY_VCC
|
||||
#include <shobjidl.h>
|
||||
#endif
|
||||
|
||||
//-------------------------------------------------------------------------------------------
|
||||
// FileDialog class shows a dialog for user to select a file for open or save.
|
||||
//
|
||||
// There is an effort to standardize VCC using slashes as directory seperators
|
||||
// but GetSaveFileName() and GetOpenFileName() are old windows functions that
|
||||
// do not play well with slashes so there is a bit of converting in and out;
|
||||
//
|
||||
// "path" refers to reverse slash path
|
||||
//
|
||||
//-------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
using namespace VCC::Util;
|
||||
|
||||
FileDialog::FileDialog()
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
void FileDialog::init()
|
||||
{
|
||||
ZeroMemory(&ofn_, sizeof(ofn_));
|
||||
ofn_.lStructSize = sizeof(ofn_);
|
||||
flags_ = OFN_HIDEREADONLY;
|
||||
}
|
||||
|
||||
void FileDialog::setDefExt(const char* DefExt)
|
||||
{
|
||||
sDefext_.assign(DefExt ? DefExt : "");
|
||||
}
|
||||
|
||||
void FileDialog::setInitialDir(const char* InitialDir)
|
||||
{
|
||||
sInitDir_.assign(InitialDir ? InitialDir : "");
|
||||
}
|
||||
|
||||
void FileDialog::setInitialDir(const std::string& InitialDir)
|
||||
{
|
||||
sInitDir_ = InitialDir;
|
||||
}
|
||||
|
||||
void FileDialog::setFilter(const char* Filter)
|
||||
{
|
||||
sFilter_.clear();
|
||||
if (!Filter) return;
|
||||
|
||||
const char* p = Filter;
|
||||
while (*p) p += std::strlen(p) + 1;
|
||||
sFilter_.assign(Filter, p + 1 - Filter);
|
||||
}
|
||||
|
||||
void FileDialog::setFlags(unsigned int Flags)
|
||||
{
|
||||
flags_ = Flags | OFN_HIDEREADONLY;
|
||||
}
|
||||
|
||||
void FileDialog::setTitle(const char* Title)
|
||||
{
|
||||
sTitle_.assign(Title ? Title : "");
|
||||
}
|
||||
|
||||
void FileDialog::setpath(const char* File)
|
||||
{
|
||||
sFile_.assign(File ? File : "");
|
||||
}
|
||||
|
||||
const std::string& FileDialog::getpath() const
|
||||
{
|
||||
return sFile_;
|
||||
}
|
||||
|
||||
std::string FileDialog::getdir() const
|
||||
{
|
||||
return GetDirectoryPart(sFile_);
|
||||
}
|
||||
|
||||
std::string FileDialog::gettype() const
|
||||
{
|
||||
std::string s = sFile_;
|
||||
size_t pos = s.rfind('.');
|
||||
if (pos == std::string::npos || pos == s.size() - 1) return {};
|
||||
return s.substr(pos + 1);
|
||||
}
|
||||
|
||||
void FileDialog::getpath(char* PathCopy, int maxsize) const
|
||||
{
|
||||
if (!PathCopy) return;
|
||||
strncpy(PathCopy, sFile_.c_str(), maxsize - 1);
|
||||
PathCopy[maxsize - 1] = 0;
|
||||
}
|
||||
|
||||
void FileDialog::getupath(char* PathCopy, int maxsize) const
|
||||
{
|
||||
if (!PathCopy) return;
|
||||
strncpy(PathCopy, sFile_.c_str(), maxsize - 1);
|
||||
PathCopy[maxsize - 1] = 0;
|
||||
}
|
||||
|
||||
void FileDialog::getdir(char* Dir, int maxsize) const
|
||||
{
|
||||
if (!Dir) return;
|
||||
std::string d = getdir();
|
||||
strncpy(Dir, d.c_str(), maxsize - 1);
|
||||
Dir[maxsize - 1] = 0;
|
||||
}
|
||||
|
||||
void FileDialog::gettype(char* Type, int maxsize) const
|
||||
{
|
||||
if (!Type) return;
|
||||
std::string t = gettype();
|
||||
strncpy(Type, t.c_str(), maxsize - 1);
|
||||
Type[maxsize - 1] = 0;
|
||||
}
|
||||
|
||||
const char* FileDialog::path() const
|
||||
{
|
||||
return sFile_.c_str();
|
||||
}
|
||||
|
||||
const char* FileDialog::upath() const
|
||||
{
|
||||
return sFile_.c_str();
|
||||
}
|
||||
|
||||
//=============================================
|
||||
// FileDialog::show calls GetOpenFileName() or GetSaveFileName()
|
||||
//=============================================
|
||||
|
||||
bool FileDialog::show(BOOL Save, HWND Owner) {
|
||||
|
||||
char file[MAX_PATH];
|
||||
::VCC::Util::copy_to_char(sFile_,file,sizeof(file));
|
||||
::VCC::Util::RevDirSlashes(file);
|
||||
ofn_.nMaxFile = sizeof(file);
|
||||
ofn_.lpstrFile = file;
|
||||
|
||||
char idir[MAX_PATH];
|
||||
::VCC::Util::copy_to_char(sInitDir_,idir,sizeof(idir));
|
||||
::VCC::Util::RevDirSlashes(idir);
|
||||
ofn_.lpstrInitialDir = idir;
|
||||
|
||||
// instance is that of the current module
|
||||
ofn_.hInstance = GetModuleHandle(nullptr);
|
||||
|
||||
// use active window if owner is null
|
||||
if (Owner != nullptr) {
|
||||
ofn_.hwndOwner = Owner;
|
||||
} else {
|
||||
ofn_.hwndOwner = GetActiveWindow();
|
||||
}
|
||||
|
||||
ofn_.lpstrDefExt = sDefext_.c_str();
|
||||
ofn_.lpstrFilter = sFilter_.c_str();
|
||||
ofn_.lpstrTitle = sTitle_.c_str();
|
||||
ofn_.Flags |= flags_;
|
||||
|
||||
// Call Save or Open per boolean
|
||||
int rc;
|
||||
if (Save) {
|
||||
rc = GetSaveFileName(&ofn_);
|
||||
} else {
|
||||
rc = GetOpenFileName(&ofn_) ;
|
||||
}
|
||||
|
||||
// Copy file selected back
|
||||
if ((rc == 1) && (*file != '\0')) {
|
||||
sFile_.assign(file ? file : "");
|
||||
VCC::Util::FixDirSlashes(sFile_);
|
||||
return true;
|
||||
};
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//==================================================
|
||||
// FileDialog::show_folder is defined if not legacy
|
||||
//==================================================
|
||||
|
||||
#ifndef _LEGACY_VCC
|
||||
bool FileDialog::show_folder(HWND Owner)
|
||||
{
|
||||
HRESULT hr = CoInitializeEx(nullptr,
|
||||
COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
|
||||
|
||||
if (FAILED(hr))
|
||||
return false;
|
||||
|
||||
IFileOpenDialog* dlg = nullptr;
|
||||
hr = CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_INPROC_SERVER,
|
||||
IID_PPV_ARGS(&dlg));
|
||||
|
||||
if (FAILED(hr)) {
|
||||
CoUninitialize();
|
||||
return false;
|
||||
}
|
||||
|
||||
DWORD opts = 0;
|
||||
dlg->GetOptions(&opts);
|
||||
dlg->SetOptions(opts | FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM);
|
||||
|
||||
if (!sTitle_.empty()) {
|
||||
dlg->SetTitle(UTF8ToWide(sTitle_).c_str());
|
||||
}
|
||||
|
||||
if (!sInitDir_.empty()) {
|
||||
IShellItem* folder = nullptr;
|
||||
std::wstring w = UTF8ToWide(sInitDir_);
|
||||
RevDirSlashes(w);
|
||||
if (SUCCEEDED(SHCreateItemFromParsingName(w.c_str(), nullptr,
|
||||
IID_PPV_ARGS(&folder)))) {
|
||||
dlg->SetFolder(folder);
|
||||
folder->Release();
|
||||
}
|
||||
}
|
||||
|
||||
hr = dlg->Show(Owner);
|
||||
if (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED)) {
|
||||
dlg->Release();
|
||||
CoUninitialize();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (FAILED(hr)) {
|
||||
dlg->Release();
|
||||
CoUninitialize();
|
||||
return false;
|
||||
}
|
||||
|
||||
IShellItem* item = nullptr;
|
||||
hr = dlg->GetResult(&item);
|
||||
if (SUCCEEDED(hr)) {
|
||||
PWSTR pszPath = nullptr;
|
||||
if (SUCCEEDED(item->GetDisplayName(SIGDN_FILESYSPATH, &pszPath))) {
|
||||
sFile_ = WideToUTF8(pszPath);
|
||||
CoTaskMemFree(pszPath);
|
||||
}
|
||||
item->Release();
|
||||
}
|
||||
|
||||
dlg->Release();
|
||||
CoUninitialize();
|
||||
return !sFile_.empty();
|
||||
}
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------
|
||||
// Center a dialog box in parent window
|
||||
//------------------------------------------------------------
|
||||
void CenterDialog(HWND hDlg)
|
||||
{
|
||||
RECT rPar;
|
||||
GetWindowRect(GetParent(hDlg), &rPar);
|
||||
|
||||
RECT rDlg;
|
||||
GetWindowRect(hDlg, &rDlg);
|
||||
|
||||
const auto x = rPar.left + (rPar.right - rPar.left - (rDlg.right - rDlg.left)) / 2;
|
||||
const auto y = rPar.top + (rPar.bottom - rPar.top - (rDlg.bottom - rDlg.top)) / 2;
|
||||
SetWindowPos(hDlg, nullptr, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------------------------------
|
||||
// CloseCartDialog should be called by cartridge DLL's when they are unloaded.
|
||||
//-------------------------------------------------------------------------------------------
|
||||
void CloseCartDialog(HWND hDlg)
|
||||
{
|
||||
// Send a close message to a cart configuration dialog if it is Enabled. If the dialog
|
||||
// is disabled it is assumed that VCC must be terminated to avoid a crash. A common
|
||||
// reason a configuration dialog is disabled is it has a modal dialog window open.
|
||||
if (hDlg) {
|
||||
if (IsWindowEnabled(hDlg)) {
|
||||
SendMessage(hDlg,WM_CLOSE,0,0);
|
||||
} else {
|
||||
MessageBox(nullptr,"A system dialog was left open. VCC will close",
|
||||
"Unload Cartridge Error",MB_ICONEXCLAMATION);
|
||||
DWORD pid = GetCurrentProcessId();
|
||||
HANDLE h = OpenProcess(PROCESS_TERMINATE,FALSE,pid);
|
||||
TerminateProcess(h,0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
#include <vcc/util/fileutil.h>
|
||||
#include <vcc/util/logger.h>
|
||||
#include <Windows.h>
|
||||
#include <string>
|
||||
|
||||
//#ifndef LEGACY_BUILD
|
||||
#include <shobjidl.h>
|
||||
//#endif
|
||||
|
||||
//-------------------------------------------------------------------------------------------
|
||||
// CloseCartDialog closes DLL dialog or force exits Vcc if it can not be.
|
||||
// It should be called by cartridge DLL's when they are unloaded.
|
||||
//-------------------------------------------------------------------------------------------
|
||||
void CloseCartDialog(HWND hDlg);
|
||||
void CenterDialog(HWND hDlg);
|
||||
|
||||
//-------------------------------------------------------------------------------------------
|
||||
// FileDialog wraps dialogs for users to select files.
|
||||
//
|
||||
// show() displays the dialog. If Save is TRUE the save dialog is shown
|
||||
// otherwise the open dialog is shown. If "Owner" is NULL GetActiveWindow() is used.
|
||||
// The selected filename is placed in "Path"
|
||||
//
|
||||
//-------------------------------------------------------------------------------------------
|
||||
|
||||
class FileDialog
|
||||
{
|
||||
public:
|
||||
FileDialog();
|
||||
void init();
|
||||
|
||||
// File selection
|
||||
bool show(BOOL Save = FALSE, HWND Owner = nullptr);
|
||||
|
||||
#ifndef _LEGACY_VCC
|
||||
// Folder selection (Win8+ only)
|
||||
bool show_folder(HWND Owner = nullptr);
|
||||
#endif
|
||||
|
||||
void setDefExt(const char* DefExt);
|
||||
void setInitialDir(const char* InitialDir);
|
||||
void setInitialDir(const std::string& InitialDir);
|
||||
void setFilter(const char* Filter);
|
||||
void setFlags(unsigned int Flags);
|
||||
void setTitle(const char* Title);
|
||||
void setpath(const char* File);
|
||||
|
||||
const std::string& getpath() const;
|
||||
std::string getdir() const;
|
||||
std::string gettype() const;
|
||||
|
||||
void getpath(char* pathCopy, int maxsize) const;
|
||||
void getupath(char* pathCopy, int maxsize) const;
|
||||
void getdir(char* dir, int maxsize) const;
|
||||
void gettype(char* type, int maxsize) const;
|
||||
|
||||
template <size_t S> void getpath(char (&pathCopy)[S]) const { getpath(pathCopy, S); }
|
||||
template <size_t S> void getupath(char (&pathCopy)[S]) const { getupath(pathCopy, S); }
|
||||
template <size_t S> void getdir(char (&dir)[S]) const { getdir(dir, S); }
|
||||
template <size_t S> void gettype(char (&type)[S]) const { gettype(type, S); }
|
||||
|
||||
const char* path() const;
|
||||
const char* upath() const;
|
||||
|
||||
private:
|
||||
OPENFILENAME ofn_;
|
||||
std::string sFile_;
|
||||
std::string sInitDir_;
|
||||
std::string sTitle_;
|
||||
std::string sFilter_;
|
||||
std::string sDefext_;
|
||||
DWORD flags_;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// TODO: FileOps should be depreciated and functions moved to fileutil.cpp
|
||||
|
||||
#include <Windows.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <vcc/util/FileOps.h>
|
||||
|
||||
void ValidatePath(char *Path)
|
||||
{
|
||||
char FullExePath[MAX_PATH]="";
|
||||
char TempPath[MAX_PATH]="";
|
||||
|
||||
GetModuleFileName(nullptr,FullExePath,MAX_PATH);
|
||||
PathRemoveFileSpec(FullExePath); //Get path to executable
|
||||
strcpy(TempPath,Path);
|
||||
PathRemoveFileSpec(TempPath); //Get path to Incomming file
|
||||
if (!strcmp(TempPath,FullExePath)) // If they match remove the Path
|
||||
PathStripPath(Path);
|
||||
return;
|
||||
}
|
||||
|
||||
int CheckPath( char *Path) //Return 1 on Error
|
||||
{
|
||||
char TempPath[MAX_PATH]="";
|
||||
HANDLE hr=nullptr;
|
||||
|
||||
if ((strlen(Path)==0) || (strlen(Path) > MAX_PATH))
|
||||
return 1;
|
||||
hr=CreateFile(Path,0,FILE_SHARE_READ,nullptr,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,nullptr);
|
||||
if (hr==INVALID_HANDLE_VALUE) //File Doesn't exist
|
||||
{
|
||||
GetModuleFileName(nullptr,TempPath,MAX_PATH);
|
||||
PathRemoveFileSpec(TempPath);
|
||||
if ( (strlen(TempPath)) + (strlen(Path)) > MAX_PATH) //Resulting path is to large Bail.
|
||||
return 1;
|
||||
|
||||
strcat(TempPath,Path);
|
||||
hr=CreateFile(TempPath,0,FILE_SHARE_READ,nullptr,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,nullptr);
|
||||
if (hr ==INVALID_HANDLE_VALUE)
|
||||
return 1;
|
||||
strcpy(Path,TempPath);
|
||||
}
|
||||
CloseHandle(hr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// These are here to remove dependance on shlwapi.dll. ASCII only
|
||||
void PathStripPath ( char *TextBuffer)
|
||||
{
|
||||
char TempBuffer[MAX_PATH] = "";
|
||||
short Index = (short)strlen(TextBuffer);
|
||||
|
||||
if ((Index > MAX_PATH) || (Index==0)) //Test for overflow
|
||||
return;
|
||||
|
||||
for (; Index >= 0; Index--)
|
||||
if (TextBuffer[Index] == '\\' || TextBuffer[Index] == '/')
|
||||
break;
|
||||
|
||||
if (Index < 0) //delimiter not found
|
||||
return;
|
||||
strcpy(TempBuffer, &TextBuffer[Index + 1]);
|
||||
strcpy(TextBuffer, TempBuffer);
|
||||
}
|
||||
|
||||
BOOL PathRemoveFileSpec(char *Path)
|
||||
{
|
||||
size_t Index=strlen(Path),Lenth=Index;
|
||||
if ( (Index==0) || (Index > MAX_PATH))
|
||||
return false;
|
||||
|
||||
while ( (Index>0) && (Path[Index] != '\\') )
|
||||
Index--;
|
||||
while ( (Index>0) && (Path[Index] == '\\') )
|
||||
Index--;
|
||||
if (Index==0)
|
||||
return false;
|
||||
Path[Index+2]=0;
|
||||
return( !(strlen(Path) == Lenth));
|
||||
}
|
||||
|
||||
BOOL PathRemoveExtension(char *Path)
|
||||
{
|
||||
size_t Index=strlen(Path),Lenth=Index;
|
||||
if ( (Index==0) || (Index > MAX_PATH))
|
||||
return false;
|
||||
|
||||
while ( (Index>0) && (Path[Index--] != '.') );
|
||||
Path[Index+1]=0;
|
||||
return( !(strlen(Path) == Lenth));
|
||||
}
|
||||
|
||||
char* PathFindExtension(char *Path)
|
||||
{
|
||||
size_t Index=strlen(Path);
|
||||
if ( (Index==0) || (Index > MAX_PATH))
|
||||
return(&Path[strlen(Path)+1]);
|
||||
while ( (Index>0) && (Path[Index--] != '.') );
|
||||
return(&Path[Index+1]);
|
||||
}
|
||||
|
||||
BOOL FilePrintf(HANDLE hFile, const char* fmt, ...)
|
||||
{
|
||||
DWORD dummy;
|
||||
va_list args;
|
||||
char msg[512];
|
||||
va_start(args, fmt);
|
||||
vsnprintf(msg, 512, fmt, args);
|
||||
va_end(args);
|
||||
return WriteFile(hFile,msg,strlen(msg),&dummy,nullptr);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
#include <Windows.h>
|
||||
|
||||
void PathStripPath(char*);
|
||||
void ValidatePath(char* Path);
|
||||
int CheckPath(char*);
|
||||
BOOL PathRemoveFileSpec(char*);
|
||||
BOOL PathRemoveExtension(char*);
|
||||
char* PathFindExtension(char*);
|
||||
BOOL FilePrintf(HANDLE, const char*, ...);
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <vcc/util/coreutil.h>
|
||||
#include <string>
|
||||
#include <windows.h>
|
||||
|
||||
namespace VCC::Util
|
||||
{
|
||||
//--------------------------------------------
|
||||
// Load a resource string into a utf8
|
||||
//--------------------------------------------
|
||||
std::string load_string(HINSTANCE instance, UINT id)
|
||||
{
|
||||
const wchar_t* buffer_ptr;
|
||||
// Get len of string to load
|
||||
const int length = LoadStringW(instance, id, reinterpret_cast<LPWSTR>(&buffer_ptr), 0);
|
||||
if (length == 0)
|
||||
return {};
|
||||
|
||||
// Copy load string to wide_str
|
||||
const std::wstring wide_str(buffer_ptr, length);
|
||||
|
||||
// Get len of string when converted
|
||||
const int utf8_len = WideCharToMultiByte(CP_UTF8, 0, wide_str.data(), wide_str.size(), nullptr, 0, nullptr, nullptr);
|
||||
if (utf8_len == 0)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
// Convert string from wide_str to utf8_str
|
||||
std::string utf8_str(utf8_len, '\0');
|
||||
WideCharToMultiByte(CP_UTF8, 0, wide_str.data(), wide_str.size(), utf8_str.data(), utf8_len, nullptr, nullptr);
|
||||
|
||||
return utf8_str;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
#include <Windows.h>
|
||||
#include <string>
|
||||
|
||||
namespace VCC::Util
|
||||
{
|
||||
//--------------------------------------------
|
||||
// Load resource string into utf8 string
|
||||
//--------------------------------------------
|
||||
std::string load_string(HINSTANCE instance, UINT id);
|
||||
|
||||
//--------------------------------------------
|
||||
// Compatible min function that works with legacy compilers
|
||||
//--------------------------------------------
|
||||
template <typename T>
|
||||
inline constexpr const T& min_compat(const T& a, const T& b)
|
||||
{
|
||||
return (b < a) ? b : a;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
#include <Windows.h>
|
||||
|
||||
namespace VCC::Util
|
||||
{
|
||||
|
||||
class critical_section
|
||||
{
|
||||
public:
|
||||
|
||||
critical_section()
|
||||
{
|
||||
InitializeCriticalSection(§ion_);
|
||||
}
|
||||
|
||||
~critical_section()
|
||||
{
|
||||
DeleteCriticalSection(§ion_);
|
||||
}
|
||||
|
||||
critical_section(const critical_section&) = delete;
|
||||
critical_section& operator=(const critical_section&) = delete;
|
||||
|
||||
void lock() const
|
||||
{
|
||||
EnterCriticalSection(§ion_);
|
||||
}
|
||||
|
||||
void unlock() const
|
||||
{
|
||||
LeaveCriticalSection(§ion_);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
mutable CRITICAL_SECTION section_;
|
||||
};
|
||||
|
||||
|
||||
class section_locker
|
||||
{
|
||||
public:
|
||||
|
||||
explicit section_locker(const critical_section& section)
|
||||
: section_(section)
|
||||
{
|
||||
section_.lock();
|
||||
}
|
||||
|
||||
~section_locker()
|
||||
{
|
||||
section_.unlock();
|
||||
}
|
||||
|
||||
section_locker& operator=(const section_locker&) = delete;
|
||||
section_locker& operator=(section_locker&&) = delete;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
const critical_section& section_;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <vcc/util/fileutil.h>
|
||||
#include <vcc/util/filesystem.h>
|
||||
|
||||
//TODO filesystem.cpp is depreciated
|
||||
//TODO move this it is used only in mpi/multipak_cartridge.cpp:
|
||||
|
||||
namespace VCC::Util
|
||||
{
|
||||
std::string find_pak_module_path(std::string path)
|
||||
{
|
||||
if (path.empty())
|
||||
{
|
||||
return path;
|
||||
}
|
||||
|
||||
auto file_handle(CreateFile(path.c_str(), 0, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr));
|
||||
if (file_handle == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
const auto application_path = ::VCC::Util::GetDirectoryPart(::VCC::Util::ModulePath(NULL));
|
||||
const auto alternate_path = application_path + path;
|
||||
file_handle = CreateFile(alternate_path.c_str(), 0, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (file_handle == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
path = alternate_path;
|
||||
}
|
||||
}
|
||||
|
||||
CloseHandle(file_handle);
|
||||
|
||||
return path;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <Windows.h>
|
||||
|
||||
//TODO: depreciate this - it is used only in mpi/multipak_cartridge.cpp:
|
||||
namespace VCC::Util
|
||||
{
|
||||
std::string find_pak_module_path(std::string path);
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
//#define USE_LOGGING
|
||||
//======================================================================
|
||||
// General purpose Host file utilities. EJ Jaquay 2026
|
||||
//
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
// Vcc is Copyright 2015 by Joseph Forgione
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software, you can redistribute
|
||||
// and/or modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation, either version 3 of
|
||||
// the License, or (at your option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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 VCC (Virtual Color Computer). If not, see
|
||||
// <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//======================================================================
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <filesystem>
|
||||
#include <Shlwapi.h>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <vcc/util/logger.h>
|
||||
#include <vcc/util/fileutil.h>
|
||||
#include <vcc/util/textutil.h>
|
||||
#include <Windows.h>
|
||||
|
||||
namespace VCC::Util
|
||||
{
|
||||
//----------------------------------------------------------------------
|
||||
// Get most recent windows error text
|
||||
//----------------------------------------------------------------------
|
||||
std::string LastErrorString()
|
||||
{
|
||||
DWORD error_code = GetLastError();
|
||||
char msg[256];
|
||||
DWORD len = FormatMessageA(
|
||||
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||
nullptr, error_code,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
msg, sizeof(msg), nullptr
|
||||
);
|
||||
if (len == 0) return "Unknown error";
|
||||
return std::string(msg, len);
|
||||
}
|
||||
|
||||
// Only use this overload as argument to debug logger (DLOG)
|
||||
const char* LastErrorTxt()
|
||||
{
|
||||
thread_local static std::string buffer;
|
||||
buffer = LastErrorString();
|
||||
return buffer.c_str();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Get the file path of a loaded module
|
||||
// Current exe path is returned if module_handle is null
|
||||
//-------------------------------------------------------------------
|
||||
std::string ModulePath(HMODULE module_handle)
|
||||
{
|
||||
std::string text(MAX_PATH, '\0');
|
||||
for (;;) {
|
||||
DWORD ret = GetModuleFileName(module_handle, &text[0], text.size());
|
||||
if (ret == 0)
|
||||
return {}; // error
|
||||
|
||||
if (ret < text.size() - 1) {
|
||||
text.resize(ret);
|
||||
return text;
|
||||
}
|
||||
// truncated grow and retry
|
||||
text.resize(text.size() * 2);
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// If file directory matches application directory strip directory
|
||||
//-------------------------------------------------------------------
|
||||
std::string StripModPath(const std::string file)
|
||||
{
|
||||
// Strip path from file if it matches the exe or dll path
|
||||
auto app_dir = GetDirectoryPart(ModulePath(nullptr));
|
||||
auto file_dir = GetDirectoryPart(file);
|
||||
std::error_code ec;
|
||||
if (std::filesystem::equivalent(app_dir, file_dir, ec)) {
|
||||
return GetFileNamePart(file);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------
|
||||
// Fully qualify a file based on current execution directory
|
||||
// Undoes the effect of StripModPath() unless path absolute
|
||||
//---------------------------------------------------------------
|
||||
std::string QualifyModPath(const std::string& path)
|
||||
{
|
||||
if (path.empty()) return {};
|
||||
|
||||
// convert to string and normalize slashes
|
||||
std::string mod = path;
|
||||
FixDirSlashes(mod);
|
||||
|
||||
// Quit if path is absolute
|
||||
if (mod.find('/') != std::string::npos) return mod;
|
||||
|
||||
return GetExecutableDir() + '/' + mod;
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------
|
||||
// Get Version infomation from exe or dll file
|
||||
// Common keys: (these are defined in the .rc file)
|
||||
// "FileVersion" Version infomation
|
||||
// "FileDescription" Short description
|
||||
// "Comments" Detailed description
|
||||
//---------------------------------------------------------------
|
||||
std::string GetVersionInfo(const char* filename, const char* key)
|
||||
{
|
||||
DWORD size = GetFileVersionInfoSizeA(filename, nullptr);
|
||||
if (size == 0) return "";
|
||||
|
||||
std::vector<char> data(size);
|
||||
if (!GetFileVersionInfoA(filename, 0, size, data.data())) return "";
|
||||
|
||||
// Get Translation table (language + codepage)
|
||||
LPVOID trans = nullptr;
|
||||
UINT transLen = 0;
|
||||
if (!VerQueryValueA(data.data(), "\\VarFileInfo\\Translation", &trans, &transLen))
|
||||
return "";
|
||||
|
||||
// Build the complete query for key
|
||||
DWORD langCode = *(DWORD*)trans;
|
||||
WORD lang = LOWORD(langCode);
|
||||
WORD code = HIWORD(langCode);
|
||||
char query[256];
|
||||
sprintf_s(query, "\\StringFileInfo\\%04X%04X\\%s", lang, code, key);
|
||||
|
||||
// Query the key
|
||||
LPVOID value = nullptr;
|
||||
UINT valueLen = 0;
|
||||
if (!VerQueryValueA(data.data(), query, &value, &valueLen)) return "";
|
||||
|
||||
return std::string((char*)value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
//======================================================================
|
||||
// General purpose Host file utilities. EJ Jaquay 2026
|
||||
//
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
// Vcc is Copyright 2015 by Joseph Forgione
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software, you can redistribute
|
||||
// and/or modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation, either version 3 of
|
||||
// the License, or (at your option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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 VCC (Virtual Color Computer). If not, see
|
||||
// <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//======================================================================
|
||||
#pragma once
|
||||
|
||||
// following pragma could be avoided by adjusting project files
|
||||
#pragma comment(lib, "version.lib")
|
||||
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <algorithm>
|
||||
#include <Windows.h>
|
||||
//#include <vcc/util/textutil.h>
|
||||
|
||||
//=========================================================================
|
||||
// Host file utilities. Most of these are general purpose
|
||||
//=========================================================================
|
||||
|
||||
namespace VCC::Util {
|
||||
|
||||
// Get most recent windows error text
|
||||
std::string LastErrorString();
|
||||
const char* LastErrorTxt();
|
||||
|
||||
// Get path of loaded module or current application
|
||||
std::string ModulePath(HMODULE module_handle);
|
||||
|
||||
// If path is in the application directory strip directory
|
||||
std::string StripModPath(std::string path);
|
||||
|
||||
// Fully qualify a file based on execution directory
|
||||
std::string QualifyModPath(const std::string& path);
|
||||
|
||||
// Get version information from EXE or DLL
|
||||
std::string GetVersionInfo(const char* filename, const char* key);
|
||||
|
||||
//------------------------------------------------------------------------
|
||||
// In line functions
|
||||
//------------------------------------------------------------------------
|
||||
|
||||
// Return copy of string with spaces trimmed from end of a string
|
||||
inline std::string trim_right_spaces(const std::string& s)
|
||||
{
|
||||
size_t end = s.find_last_not_of(' ');
|
||||
if (end == std::string::npos) return {};
|
||||
return s.substr(0, end + 1);
|
||||
}
|
||||
|
||||
// Return filename part of a path
|
||||
inline std::string GetFileNamePart(const std::string& input)
|
||||
{
|
||||
std::filesystem::path p(input);
|
||||
return p.filename().string();
|
||||
}
|
||||
|
||||
// Determine if path is a direcory
|
||||
inline bool IsDirectory(const std::string& path)
|
||||
{
|
||||
std::error_code ec;
|
||||
return std::filesystem::is_directory(path, ec) && !ec;
|
||||
}
|
||||
|
||||
// Verify that a file can be opened for read/write
|
||||
inline bool ValidateRWFile(const std::string& path)
|
||||
{
|
||||
HANDLE h = CreateFile
|
||||
(path.c_str(), GENERIC_READ | GENERIC_WRITE,
|
||||
FILE_SHARE_READ, nullptr, OPEN_ALWAYS,
|
||||
FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (h==INVALID_HANDLE_VALUE) return false;
|
||||
CloseHandle(h);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Verify that a file can be opened for read
|
||||
inline bool ValidateRDFile(const std::string& path)
|
||||
{
|
||||
HANDLE h = CreateFile
|
||||
(path.c_str(), GENERIC_READ,
|
||||
FILE_SHARE_READ, nullptr, OPEN_ALWAYS,
|
||||
FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
if (h==INVALID_HANDLE_VALUE) return false;
|
||||
CloseHandle(h);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Convert backslashes to slashes within string
|
||||
inline void FixDirSlashes(std::string& dir)
|
||||
{
|
||||
if (dir.empty()) return;
|
||||
std::replace(dir.begin(), dir.end(), '\\', '/');
|
||||
if (dir.back() == '/') dir.pop_back();
|
||||
}
|
||||
|
||||
// Convert backslashes to slashes within char *
|
||||
inline void FixDirSlashes(char* dir)
|
||||
{
|
||||
if (!dir || !*dir) return;
|
||||
for (char* p = dir; *p; ++p)
|
||||
if (*p == '\\') *p = '/';
|
||||
}
|
||||
|
||||
// Convert slashes to backslashes within char * (for legacy win calls)
|
||||
inline void RevDirSlashes(char* dir)
|
||||
{
|
||||
if (!dir || !*dir) return;
|
||||
for (char* p = dir; *p; ++p)
|
||||
if (*p == '/') *p = '\\';
|
||||
}
|
||||
|
||||
// Overrides for std::string and wstring
|
||||
inline void RevDirSlashes(std::string& dir)
|
||||
{
|
||||
std::replace(dir.begin(), dir.end(), '/', '\\');
|
||||
}
|
||||
inline void RevDirSlashes(std::wstring& dir)
|
||||
{
|
||||
std::replace(dir.begin(), dir.end(), L'/', L'\\');
|
||||
}
|
||||
|
||||
// Return copy of path with backslashes converterd
|
||||
inline std::string FixDirSlashes(const std::string& dir)
|
||||
{
|
||||
std::string s = dir;
|
||||
FixDirSlashes(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
// Strip trailing backslash from directory or path
|
||||
inline void StripTrailingSlash(std::string& dir)
|
||||
{
|
||||
if (dir.back() == '/') dir.pop_back();
|
||||
}
|
||||
|
||||
// Return copy with trailing backslash stripped
|
||||
inline std::string StripTrailingSlash(const std::string& dir)
|
||||
{
|
||||
std::string s = dir;
|
||||
StripTrailingSlash(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
// Conditionally append trailing backslash to directory
|
||||
inline void AppendTrailingSlash(std::string dir)
|
||||
{
|
||||
if (dir.back() != '/') dir += '/';
|
||||
}
|
||||
|
||||
// Return slash normalized directory part of a path
|
||||
inline std::string GetDirectoryPart(const std::string& input)
|
||||
{
|
||||
std::filesystem::path p(input);
|
||||
std::string out = p.parent_path().string();
|
||||
FixDirSlashes(out);
|
||||
return out;
|
||||
}
|
||||
|
||||
// Return slash normalized user directory
|
||||
inline std::string GetUserDir()
|
||||
{
|
||||
const char* p = getenv("USERPROFILE");
|
||||
return p ? FixDirSlashes(std::string(p)) : std::string();
|
||||
}
|
||||
|
||||
// Return slash normaized executable directory
|
||||
inline std::string GetExecutableDir()
|
||||
{
|
||||
return GetDirectoryPart(ModulePath(nullptr));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
|
||||
|
||||
constexpr auto MAX_LOADSTRING = 400u;
|
||||
@@ -0,0 +1,73 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#include <Windows.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <io.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <vcc/util/logger.h>
|
||||
|
||||
static HANDLE hLog_Out = nullptr;
|
||||
static const auto LogFileName = "VccLog.txt";
|
||||
|
||||
// PrintLogC - Put a formatted debug string to the screen
|
||||
void PrintLogC(const char* fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
char msg[512];
|
||||
va_start(args, fmt);
|
||||
vsnprintf(msg, 512, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
// If running under a debugger use it.
|
||||
if (IsDebuggerPresent()) {
|
||||
OutputDebugStringA(msg);
|
||||
return;
|
||||
}
|
||||
|
||||
// If not running debugger use console
|
||||
if (hLog_Out == nullptr) {
|
||||
AllocConsole();
|
||||
hLog_Out = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
SetConsoleTitle("Logging Window");
|
||||
if (hLog_Out == NULL || hLog_Out == INVALID_HANDLE_VALUE) {
|
||||
OutputDebugStringA(msg);
|
||||
return;
|
||||
}
|
||||
}
|
||||
WriteFile(hLog_Out,msg,strlen(msg),nullptr,nullptr);
|
||||
}
|
||||
|
||||
// PrintLogF - Put formatted string to the log file
|
||||
void PrintLogF(const char* fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
char msg[512];
|
||||
va_start(args, fmt);
|
||||
vsnprintf(msg, 512, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
int oflag = _O_CREAT | _O_APPEND | _O_RDWR;
|
||||
int pmode = _S_IREAD | _S_IWRITE;
|
||||
int flog = _open(LogFileName,oflag,pmode);
|
||||
_write(flog, msg, strlen(msg));
|
||||
_close(flog);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
|
||||
// Print to Console or File
|
||||
void PrintLogC(const char* fmt, ...);
|
||||
void PrintLogF(const char* fmt, ...);
|
||||
|
||||
// Debug logging if USE_LOGGING is defined
|
||||
#ifdef USE_LOGGING
|
||||
#define DLOG_C(...) PrintLogC(__VA_ARGS__)
|
||||
#define DLOG_F(...) PrintLogF(__VA_ARGS__)
|
||||
#else
|
||||
#define DLOG_C(...) ((void)0)
|
||||
#define DLOG_F(...) ((void)0)
|
||||
#endif
|
||||
@@ -0,0 +1,106 @@
|
||||
//#define USE_LOGGING
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <vcc/util/settings.h>
|
||||
#include <vcc/util/logger.h>
|
||||
#include <Windows.h>
|
||||
|
||||
namespace VCC::Util
|
||||
{
|
||||
settings::settings(path_type path)
|
||||
: path_(move(path))
|
||||
{}
|
||||
|
||||
// save string value
|
||||
bool settings::write(
|
||||
const std::string& section,
|
||||
const std::string& key,
|
||||
const std::string& value) const
|
||||
{
|
||||
return ::WritePrivateProfileString(
|
||||
section.c_str(),
|
||||
key.c_str(),
|
||||
value.c_str(),
|
||||
path_.c_str());
|
||||
}
|
||||
|
||||
// save int value
|
||||
bool settings::write
|
||||
(const std::string& section,
|
||||
const std::string& key,
|
||||
int value) const
|
||||
{
|
||||
return ::WritePrivateProfileString(
|
||||
section.c_str(),
|
||||
key.c_str(),
|
||||
std::to_string(value).c_str(),
|
||||
path_.c_str());
|
||||
}
|
||||
|
||||
// save bool value
|
||||
bool settings::write_bool
|
||||
(const std::string& section,
|
||||
const std::string& key,
|
||||
bool value) const
|
||||
{
|
||||
int ival = value ? 1 : 0;
|
||||
return write(section, key, ival);
|
||||
}
|
||||
|
||||
// get char * value
|
||||
bool settings::read
|
||||
(const std::string& section,
|
||||
const std::string& key,
|
||||
const std::string& default_value,
|
||||
char* buffer,
|
||||
size_t buffer_size) const
|
||||
{
|
||||
return ::GetPrivateProfileStringA(
|
||||
section.c_str(),
|
||||
key.c_str(),
|
||||
default_value.c_str(),
|
||||
buffer,
|
||||
static_cast<DWORD>(buffer_size),
|
||||
path_.c_str());
|
||||
}
|
||||
|
||||
// get string value
|
||||
std::string settings::read
|
||||
(const std::string& section,
|
||||
const std::string& key,
|
||||
const std::string& default_value) const
|
||||
{
|
||||
char buffer[MAX_PATH] = {};
|
||||
read(section, key, default_value, buffer, sizeof(buffer));
|
||||
return std::string(buffer);
|
||||
}
|
||||
|
||||
// get int value
|
||||
int settings::read
|
||||
(const std::string& section,
|
||||
const std::string& key,
|
||||
int default_value) const
|
||||
{
|
||||
return ::GetPrivateProfileInt(
|
||||
section.c_str(),
|
||||
key.c_str(),
|
||||
default_value,
|
||||
path_.c_str());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Copyright 2015 by Joseph Forgione
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software: you can redistribute itand/or
|
||||
// modify it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or (at your
|
||||
// option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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
|
||||
// VCC (Virtual Color Computer). If not, see <http://www.gnu.org/licenses/>.
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <Windows.h>
|
||||
|
||||
namespace VCC::Util
|
||||
{
|
||||
class settings
|
||||
{
|
||||
public:
|
||||
using path_type = std::string;
|
||||
|
||||
public:
|
||||
explicit settings(path_type path);
|
||||
|
||||
// write string value
|
||||
bool write
|
||||
(const std::string& section,
|
||||
const std::string& key,
|
||||
const std::string& value) const;
|
||||
|
||||
// write int value
|
||||
bool write
|
||||
(const std::string& section,
|
||||
const std::string& key,
|
||||
int value) const;
|
||||
|
||||
// write bool value
|
||||
bool write_bool
|
||||
(const std::string& section,
|
||||
const std::string& key,
|
||||
bool value) const;
|
||||
|
||||
// get char * value
|
||||
bool read
|
||||
(const std::string& section,
|
||||
const std::string& key,
|
||||
const std::string& default_value,
|
||||
char* buffer,
|
||||
size_t buffer_size) const;
|
||||
|
||||
// get string value
|
||||
std::string read
|
||||
(const std::string& section,
|
||||
const std::string& key,
|
||||
const std::string& default_value = {}) const;
|
||||
|
||||
// get int value
|
||||
int read
|
||||
(const std::string& section,
|
||||
const std::string& key,
|
||||
int default_value) const;
|
||||
|
||||
// delete key
|
||||
bool delete_key
|
||||
(const std::string& section,
|
||||
const std::string& key) const
|
||||
{
|
||||
return ::WritePrivateProfileString
|
||||
(section.c_str(), key.c_str(), NULL, path_.c_str());
|
||||
}
|
||||
|
||||
// delete section
|
||||
bool delete_section
|
||||
(const std::string& section) const
|
||||
{
|
||||
return ::WritePrivateProfileString
|
||||
(section.c_str(), NULL, NULL, path_.c_str());
|
||||
}
|
||||
|
||||
// flush cache
|
||||
bool flush () const
|
||||
{
|
||||
return ::WritePrivateProfileString
|
||||
(NULL, NULL, NULL, path_.c_str());
|
||||
}
|
||||
|
||||
private:
|
||||
const path_type path_;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
//======================================================================
|
||||
// General purpose string utilities. EJ Jaquay 2026
|
||||
//
|
||||
// This file is part of VCC (Virtual Color Computer).
|
||||
// Vcc is Copyright 2015 by Joseph Forgione
|
||||
//
|
||||
// VCC (Virtual Color Computer) is free software, you can redistribute
|
||||
// and/or modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation, either version 3 of
|
||||
// the License, or (at your option) any later version.
|
||||
//
|
||||
// VCC (Virtual Color Computer) 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 VCC (Virtual Color Computer). If not, see
|
||||
// <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
//======================================================================
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <Windows.h>
|
||||
|
||||
//=========================================================================
|
||||
// Text utilities. Most of these are general purpose
|
||||
//=========================================================================
|
||||
|
||||
namespace VCC::Util {
|
||||
|
||||
// Return string with case conversion
|
||||
inline std::string to_lower(std::string s) {
|
||||
std::transform(s.begin(), s.end(), s.begin(),
|
||||
[](unsigned char c) {
|
||||
return static_cast<char>(::tolower(c));
|
||||
});
|
||||
return s;
|
||||
}
|
||||
|
||||
inline std::string to_upper(std::string s) {
|
||||
std::transform(s.begin(), s.end(), s.begin(),
|
||||
[](unsigned char c) {
|
||||
return static_cast<char>(::toupper(c));
|
||||
});
|
||||
return s;
|
||||
}
|
||||
|
||||
inline void make_lower(std::string& s) {
|
||||
std::transform(s.begin(), s.end(), s.begin(),
|
||||
[](unsigned char c) {
|
||||
return static_cast<char>(::tolower(c));
|
||||
});
|
||||
}
|
||||
|
||||
inline void make_lower(char* s) {
|
||||
if (!s) return;
|
||||
for (char* p = s; *p; ++p)
|
||||
*p = static_cast<char>(::tolower(static_cast<unsigned char>(*p)));
|
||||
}
|
||||
|
||||
inline void make_upper(std::string& s) {
|
||||
std::transform(s.begin(), s.end(), s.begin(),
|
||||
[](unsigned char c) {
|
||||
return static_cast<char>(::toupper(c));
|
||||
});
|
||||
}
|
||||
|
||||
inline void make_upper(char* s) {
|
||||
if (!s) return;
|
||||
for (char* p = s; *p; ++p)
|
||||
*p = static_cast<char>(::toupper(static_cast<unsigned char>(*p)));
|
||||
}
|
||||
|
||||
inline bool is_null_or_empty(const char* s) {
|
||||
return s == nullptr || *s == '\0';
|
||||
}
|
||||
|
||||
inline bool is_null_or_empty(const std::string& s) {
|
||||
return s.empty();
|
||||
}
|
||||
|
||||
inline void copy_to_char(const std::string& src, char* dst, size_t dst_size)
|
||||
{
|
||||
if (dst_size == 0) return;
|
||||
const size_t n = std::min(src.size(), dst_size - 1);
|
||||
memcpy(dst, src.data(), n);
|
||||
dst[n] = '\0';
|
||||
}
|
||||
|
||||
inline std::string WideToUTF8(const wchar_t* w)
|
||||
{
|
||||
if (!w) return {};
|
||||
|
||||
int len = WideCharToMultiByte(CP_UTF8, 0, w, -1, nullptr, 0, nullptr, nullptr);
|
||||
if (len <= 1) return {};
|
||||
|
||||
std::string out(len - 1, 0);
|
||||
WideCharToMultiByte(CP_UTF8, 0, w, -1, out.data(), len, nullptr, nullptr);
|
||||
return out;
|
||||
}
|
||||
|
||||
inline std::string WideToUTF8(const std::wstring& ws)
|
||||
{
|
||||
return WideToUTF8(ws.c_str());
|
||||
}
|
||||
|
||||
inline std::wstring UTF8ToWide(const std::string& s)
|
||||
{
|
||||
if (s.empty()) return std::wstring();
|
||||
|
||||
int len = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, nullptr, 0);
|
||||
if (len <= 1) return std::wstring();
|
||||
|
||||
std::wstring out(len - 1, 0);
|
||||
MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, out.data(), len);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user