Welcome to mirror list, hosted at ThFree Co, Russian Federation.

program_keyb.cpp « dos « src - github.com/dosbox-staging/dosbox-staging.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1d72ef0882b183ba162cc0ddbf53991bcceb568c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *
 *  Copyright (C) 2002-2021  The DOSBox Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "program_keyb.h"

#include "dos_keyboard_layout.h"
#include "string_utils.h"

void KEYB::Run(void) {
	auto log_keyboard_code = [&](const KeyboardErrorCode rcode, const std::string &layout, const int codepage) {
		switch (rcode) {
		case KEYB_NOERROR:
			WriteOut(MSG_Get("PROGRAM_KEYB_NOERROR"), layout.c_str(), dos.loaded_codepage);
			break;
		case KEYB_FILENOTFOUND:
			WriteOut(MSG_Get("PROGRAM_KEYB_FILENOTFOUND"), layout.c_str());
			break;
		case KEYB_INVALIDFILE: WriteOut(MSG_Get("PROGRAM_KEYB_INVALIDFILE"), layout.c_str()); break;
		case KEYB_LAYOUTNOTFOUND:
			WriteOut(MSG_Get("PROGRAM_KEYB_LAYOUTNOTFOUND"), layout.c_str(), codepage);
			break;
		case KEYB_INVALIDCPFILE:
			WriteOut(MSG_Get("PROGRAM_KEYB_INVCPFILE"), layout.c_str());
			break;
		default:
			LOG(LOG_DOSMISC, LOG_ERROR)
			("KEYB:Invalid returncode %x", static_cast<uint8_t>(rcode));
			break;
		}
	};

	// No arguments: print codepage info and possibly loaded layout ID
	if (!cmd->FindCommand(1, temp_line)) {
		const char *layout_name = DOS_GetLoadedLayout();
		if (!layout_name) {
			WriteOut(MSG_Get("PROGRAM_KEYB_INFO"),dos.loaded_codepage);
		} else {
			WriteOut(MSG_Get("PROGRAM_KEYB_INFO_LAYOUT"),dos.loaded_codepage,layout_name);
		}
		return;
	}

	// One argument: asked for help
	if (HelpRequested()) {
		WriteOut(MSG_Get("PROGRAM_KEYB_HELP_LONG"));
		return;
	}

	// One argument: The language/country. We'll infer the codepage
	int32_t tried_cp        = -1; // default to auto
	KeyboardErrorCode rcode = KEYB_LAYOUTNOTFOUND;
	if (cmd->GetCount() == 1) {
		rcode = DOS_LoadKeyboardLayoutFromLanguage(temp_line.c_str());
	}

	// Two or more arguments: language/country and a specific codepage
	else if (std::string cp_string; cmd->FindCommand(2, cp_string)) {
		// second parameter is codepage number
		tried_cp = atoi(cp_string.c_str());

		// Possibly a third parameter, the codepage file
		std::string cp_filename = "auto";
		(void)cmd->FindCommand(3, cp_filename); // fallback to auto

		rcode = DOS_LoadKeyboardLayout(temp_line.c_str(),
		                               tried_cp,
		                               cp_filename.c_str());
	}

	// Switch if loading the layout succeeded
	if (rcode == KEYB_NOERROR) {
		rcode = DOS_SwitchKeyboardLayout(temp_line.c_str(), tried_cp);
	}

	log_keyboard_code(rcode, temp_line, tried_cp);
}

void KEYB::AddMessages() {
	MSG_Add("PROGRAM_KEYB_INFO","Codepage %i has been loaded\n");
	MSG_Add("PROGRAM_KEYB_INFO_LAYOUT","Codepage %i has been loaded for layout %s\n");
	MSG_Add("PROGRAM_KEYB_HELP_LONG",
	        "Configures a keyboard for a specific language.\n"
	        "\n"
	        "Usage:\n"
	        "  [color=green]keyb[reset] [color=cyan][LANGCODE][reset]\n"
	        "  [color=green]keyb[reset] [color=cyan]LANGCODE[reset] [color=white]CODEPAGE[reset] [CODEPAGEFILE]\n"
	        "\n"
	        "Where:\n"
	        "  [color=cyan]LANGCODE[reset]     is a language code or keyboard layout ID.\n"
	        "  [color=white]CODEPAGE[reset]     is a code page number, such as [color=white]437[reset] and [color=white]850[reset].\n"
	        "  CODEPAGEFILE is a file containing information for a code page.\n"
	        "\n"
	        "Notes:\n"
	        "  Running [color=green]keyb[reset] without an argument shows the currently loaded keyboard layout\n"
	        "  and code page. It will change to [color=cyan]LANGCODE[reset] if provided, optionally with a\n"
	        "  [color=white]CODEPAGE[reset] and an additional CODEPAGEFILE to load the specified code page\n"
	        "  number and code page file if provided. This command is especially useful if\n"
	        "  you use a non-US keyboard, and [color=cyan]LANGCODE[reset] can also be set in the configuration\n"
	        "  file under the [dos] section using the \"keyboardlayout = [color=cyan]LANGCODE[reset]\" setting.\n"
	        "\n"
	        "Examples:\n"
	        "  [color=green]KEYB[reset]\n"
	        "  [color=green]KEYB[reset] [color=cyan]uk[reset]\n"
	        "  [color=green]KEYB[reset] [color=cyan]sp[reset] [color=white]850[reset]\n"
	        "  [color=green]KEYB[reset] [color=cyan]de[reset] [color=white]858[reset] mycp.cpi\n");
	MSG_Add("PROGRAM_KEYB_NOERROR","Keyboard layout %s loaded for codepage %i\n");
	MSG_Add("PROGRAM_KEYB_FILENOTFOUND","Keyboard file %s not found\n");
	MSG_Add("PROGRAM_KEYB_INVALIDFILE","Keyboard file %s invalid\n");
	MSG_Add("PROGRAM_KEYB_LAYOUTNOTFOUND","No layout in %s for codepage %i\n");
	MSG_Add("PROGRAM_KEYB_INVCPFILE","None or invalid codepage file for layout %s\n");
}