1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
/*
* Copyright (c) 2016-2020 Paul Mattes.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Paul Mattes, nor his contributors may be used to
* endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY PAUL MATTES "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL PAUL MATTES, JEFF SPARKES OR GTRC BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* model.c
* Support for changing model and oversize at run-time.
*/
#include "globals.h"
#include "appres.h"
#include "resources.h"
#include "ctlrc.h"
#include "popups.h"
#include "screen.h"
#include "telnet.h"
#include "toggles.h"
#include "utils.h"
#include "model.h"
static char *pending_model;
static char *pending_oversize;
/*
* Canonical representation of the model, returning color and extended
* state.
*/
static char *
canonical_model_x(const char *res, int *model, bool *is_color,
bool *is_extended)
{
size_t sl;
char *digitp = NULL;
char *colorp = "9";
bool extended = false;
if (res == NULL) {
return NULL;
}
sl = strlen(res);
if ((sl != 1 && sl != 6 && sl != 8) ||
(sl == 1 &&
(digitp = strchr("2345", res[0])) == NULL) ||
(((sl == 6) || (sl == 8)) &&
(strncmp(res, "327", 3) ||
(colorp = strchr("89", res[3])) == NULL ||
res[4] != '-' ||
(digitp = strchr("2345", res[5])) == NULL)) ||
((sl == 8) &&
(res[6] != '-' || strchr("Ee", res[7]) == NULL))) {
return NULL;
}
if (sl == 1 || sl == 8) {
extended = true;
}
*model = *digitp - '0';
*is_color = (*colorp == '9');
*is_extended = extended;
return xs_buffer("327%c-%c%s", *colorp, *digitp, extended? "-E": "");
}
/*
* Canonical representation of the model.
*/
static char *
canonical_model(const char *res)
{
int model;
bool color, extended;
return canonical_model_x(res, &model, &color, &extended);
}
/*
* Toggle the model.
*/
static bool
toggle_model(const char *name _is_unused, const char *value)
{
if (!model_can_change()) {
popup_an_error("Cannot change " ResModel);
return false;
}
Replace(pending_model, *value? NewString(value): NULL);
return true;
}
/*
* Toggle oversize.
*/
static bool
toggle_oversize(const char *name _is_unused, const char *value)
{
if (!model_can_change()) {
popup_an_error("Cannot change " ResOversize);
return false;
}
Replace(pending_oversize, NewString(value));
return true;
}
/*
* Done function for changing the model and oversize.
*/
static bool
toggle_model_done(bool success)
{
unsigned ovr = 0, ovc = 0;
int model_number = model_num;
bool is_color = mode.m3279;
bool is_extended = mode.extended;
struct {
int model_num;
int rows;
int cols;
int ov_cols;
int ov_rows;
bool m3279;
bool alt;
bool extended;
} old;
bool oversize_was_pending = (pending_oversize != NULL);
bool res = true;
if (!success ||
(pending_model == NULL &&
pending_oversize == NULL)) {
goto done;
}
if (PCONNECTED) {
popup_an_error("Cannot change %s or %s while connected",
ResModel, ResOversize);
goto fail;
}
if (pending_model != NULL && !strcmp(pending_model, appres.model)) {
Replace(pending_model, NULL);
}
if (pending_oversize != NULL &&
appres.oversize != NULL &&
!strcmp(pending_oversize, appres.oversize)) {
Replace(pending_oversize, NULL);
}
if (pending_model == NULL && pending_oversize == NULL) {
goto done;
}
/* Reconcile simultaneous changes. */
if (pending_model != NULL) {
char *canon = canonical_model_x(pending_model, &model_number,
&is_color, &is_extended);
if (canon == NULL) {
popup_an_error("%s value must be 327{89}-{2345}[-E]",
ResModel);
goto fail;
}
Replace(pending_model, canon);
}
if (!is_extended) {
/* Without extended, no oversize. */
Replace(pending_oversize, NewString(""));
}
if (pending_oversize != NULL) {
if (*pending_oversize) {
char x, junk;
if (sscanf(pending_oversize, "%u%c%u%c", &ovc, &x, &ovr, &junk) != 3
|| x != 'x') {
popup_an_error("%s value must be <cols>x<rows>",
ResOversize);
goto fail;
}
} else {
ovc = 0;
ovr = 0;
}
} else {
ovc = ov_cols;
ovr = ov_rows;
}
/* Save the current settings. */
old.model_num = model_num;
old.rows = ROWS;
old.cols = COLS;
old.ov_rows = ov_rows;
old.ov_cols = ov_cols;
old.m3279 = mode.m3279;
old.alt = screen_alt;
old.extended = mode.extended;
/* Change settings. */
mode.m3279 = is_color;
mode.extended = is_extended;
set_rows_cols(model_number, ovc, ovr);
if (model_num != model_number ||
ov_rows != (int)ovr ||
ov_cols != (int)ovc) {
/* Failed. Restore the old settings. */
mode.m3279 = old.m3279;
set_rows_cols(old.model_num, old.ov_cols, old.ov_rows);
ROWS = old.rows;
COLS = old.cols;
screen_alt = old.alt;
mode.extended = old.extended;
return false;
}
ROWS = maxROWS;
COLS = maxCOLS;
ctlr_reinit(MODEL_CHANGE);
/* Reset the screen state. */
screen_init();
ctlr_erase(true);
/* Report the new terminal name. */
if (appres.termname == NULL) {
st_changed(ST_TERMINAL_NAME, appres.termname != NULL);
}
if (pending_model != NULL) {
Replace(appres.model, pending_model);
}
pending_model = NULL;
if (pending_oversize != NULL) {
if (*pending_oversize) {
Replace(appres.oversize, pending_oversize);
pending_oversize = NULL;
} else {
bool force = !oversize_was_pending && appres.oversize != NULL;
Replace(appres.oversize, NULL);
if (force) {
/* Turning off extended killed oversize. */
force_toggle_notify(ResOversize, IA_NONE);
}
}
}
goto done;
fail:
res = false;
done:
Replace(pending_model, NULL);
Replace(pending_oversize, NULL);
return res;
}
/*
* Terminal name toggle.
*/
static bool
toggle_terminal_name(const char *name _is_unused, const char *value)
{
if (PCONNECTED) {
popup_an_error("%s cannot change while connected",
ResTermName);
return false;
}
appres.termname = clean_termname(*value? value: NULL);
net_set_default_termtype();
return true;
}
/*
* Toggle the NOP interval.
*/
static bool
toggle_nop_seconds(const char *name _is_unused, const char *value)
{
unsigned long l;
char *end;
int secs;
if (!*value) {
appres.nop_seconds = 0;
net_nop_seconds();
return true;
}
l = strtoul(value, &end, 10);
secs = (int)l;
if (*end != '\0' || (unsigned long)secs != l || secs < 0) {
popup_an_error("Invalid %s value", ResNopSeconds);
return false;
}
appres.nop_seconds = secs;
net_nop_seconds();
return true;
}
/**
* Module registration.
*/
void
model_register(void)
{
/* Register the toggles. */
register_extended_toggle(ResModel, toggle_model, toggle_model_done,
canonical_model, (void **)&appres.model, XRM_STRING);
register_extended_toggle(ResOversize, toggle_oversize, toggle_model_done,
NULL, (void **)&appres.oversize, XRM_STRING);
register_extended_toggle(ResTermName, toggle_terminal_name, NULL, NULL,
(void **)&appres.termname, XRM_STRING);
register_extended_toggle(ResNopSeconds, toggle_nop_seconds, NULL,
NULL, (void **)&appres.nop_seconds, XRM_INT);
}
|