screen.c 20.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
/*
 * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270  e X3270
 * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a
 * aplicativos mainframe. Registro no INPI sob o nome G3270. Registro no INPI sob o nome G3270.
 *
 * Copyright (C) <2008> <Banco do Brasil S.A.>
 *
 * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob
 * os termos da GPL v.2 - Licença Pública Geral  GNU,  conforme  publicado  pela
 * Free Software Foundation.
 *
 * Este programa é distribuído na expectativa de  ser  útil,  mas  SEM  QUALQUER
 * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou  de  ADEQUAÇÃO
 * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para
 * obter mais detalhes.
 *
 * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este
 * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin
 * St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Este programa está nomeado como screen.c e possui 894 linhas de código.
 *
 * Contatos:
 *
 * perry.werneck@gmail.com	(Alexandre Perry de Souza Werneck)
 * erico.mendonca@gmail.com	(Erico Mascarenhas Mendonça)
 *
 */

/**
 *	@file screen.c
 *
 *	@brief A callback based 3270 Terminal Emulator Screen drawing
 *
 */

#include <internals.h>
#include <signal.h>
#include "3270ds.h"
//#include "resources.h"
#include "ctlrc.h"
#include "hostc.h"
#include "kybdc.h"
#include "screenc.h"
#include "trace_dsc.h"
#include "utilc.h"
#include "widec.h"
#include "xioc.h"
#include "screen.h"
#include "errno.h"
#include "statusc.h"
#include "togglesc.h"
#include <lib3270/actions.h>
#include <lib3270/log.h>
#include <lib3270/toggle.h>

#if defined(_WIN32)
#include <windows.h>
#include <wincon.h>
#include "winversc.h"
#else
#include <stdarg.h>
#endif


#define get_color_pair(fg,bg) (((bg&0x0F) << 4) | (fg&0x0F))
#define DEFCOLOR_MAP(f) ((((f) & FA_PROTECT) >> 4) | (((f) & FA_INT_HIGH_SEL) >> 3))

/*--[ Implement ]------------------------------------------------------------------------------------*/

static void status_connect(H3270 *session, int ignored, void *dunno);
static void status_3270_mode(H3270 *session, int ignored, void *dunno);
static unsigned short color_from_fa(H3270 *hSession, unsigned char fa);

/*--[ Implement ]------------------------------------------------------------------------------------*/

static void addch(H3270 *session, int baddr, unsigned char c, unsigned short attr, int *first, int *last) {
	// If set to keep selection adjust corresponding flag based on the current state
	if(lib3270_get_toggle(session,LIB3270_TOGGLE_KEEP_SELECTED))
		attr |= (session->text[baddr].attr & LIB3270_ATTR_SELECTED);

	if(session->text[baddr].chr == c && session->text[baddr].attr == attr)
		return;

	if(*first < 0)
		*first = baddr;
	*last = baddr;

	/* Converted char has changed, update it */
	session->text[baddr].chr  = c;
	session->text[baddr].attr = attr;

	session->cbk.update(session,baddr,c,attr,baddr == session->cursor_addr);
}

LIB3270_EXPORT LIB3270_ATTR lib3270_get_attribute_at_address(H3270 *hSession, unsigned int baddr) {
	if(check_online_session(hSession))
		return (LIB3270_ATTR) -1;

	if(!hSession->text ||baddr > (hSession->view.rows*hSession->view.cols)) {
		errno = EOVERFLOW;
		return (LIB3270_ATTR) -1;
	}

	return hSession->text[baddr].attr;
}

LIB3270_EXPORT int lib3270_is_selected(H3270 *hSession, unsigned int baddr) {
	FAIL_IF_NOT_ONLINE(hSession);

	if(!hSession->text || baddr > (hSession->view.rows * hSession->view.cols)) {
		errno = EOVERFLOW;
		return -1;
	}

	return (hSession->text[baddr].attr & LIB3270_ATTR_SELECTED) != 0;
}

LIB3270_EXPORT int lib3270_get_element(H3270 *hSession, unsigned int baddr, unsigned char *c, unsigned short *attr) {
	FAIL_IF_NOT_ONLINE(hSession);

	if(!hSession->text || baddr > (hSession->view.rows * hSession->view.cols)) {
		errno = EOVERFLOW;
		return -1;
	}

	*c		= hSession->text[baddr].chr;
	*attr	= hSession->text[baddr].attr;

	return 0;
}

/**
 * Initialize the screen.
 *
 * @return 0 if ok, non zero if not
 *
 */
int screen_init(H3270 *session) {
	CHECK_SESSION_HANDLE(session);

	/* Set up callbacks for state changes. */
	lib3270_register_schange(session,LIB3270_STATE_CONNECT, status_connect,0);
	lib3270_register_schange(session,LIB3270_STATE_3270_MODE, status_3270_mode,0);
//	lib3270_register_schange(session,LIB3270_STATE_PRINTER, status_printer,0);

	/* Set up the controller. */
	ctlr_init(session,-1);
	ctlr_model_changed(session);

	/* Finish screen initialization. */
	session->cbk.suspend(session);

	return 0;
}

/* Map a field attribute to its default colors. */
static unsigned short color_from_fa(H3270 *hSession, unsigned char fa) {
	if (hSession->m3279)
		return get_color_pair(DEFCOLOR_MAP(fa),0) | LIB3270_ATTR_FIELD;

	// Green on black
	return get_color_pair(0,0) | LIB3270_ATTR_FIELD | ((FA_IS_HIGH(fa)) ? LIB3270_ATTR_INTENSIFY : 0);
}

/*
 * Find the display attributes for a baddr, fa_addr and fa.
 */
static unsigned short calc_attrs(H3270 *session, int baddr, int fa_addr, int fa) {
	unsigned short fg=0, bg=0, a;
	int gr;

	/* Compute the color. */

	/* Monochrome is easy, and so is color if nothing is specified. */
	if (!session->m3279 ||
	        (!session->ea_buf[baddr].fg &&
	         !session->ea_buf[fa_addr].fg &&
	         !session->ea_buf[baddr].bg &&
	         !session->ea_buf[fa_addr].bg)) {
		a = color_from_fa(session,fa);
	} else {

		/* The current location or the fa specifies the fg or bg. */
		if (session->ea_buf[baddr].fg) {
			fg = session->ea_buf[baddr].fg & 0x0f;
		} else if (session->ea_buf[fa_addr].fg) {
			fg = session->ea_buf[fa_addr].fg & 0x0f;
		} else {
			fg = DEFCOLOR_MAP(fa);
		}

		if (session->ea_buf[baddr].bg)
			bg = session->ea_buf[baddr].bg & 0x0f;
		else if (session->ea_buf[fa_addr].bg)
			bg = session->ea_buf[fa_addr].bg & 0x0f;
		else
			bg = 0;

		a = get_color_pair(fg, bg);
	}

	/* Compute the display attributes. */

	if (session->ea_buf[baddr].gr)
		gr = session->ea_buf[baddr].gr;
	else if (session->ea_buf[fa_addr].gr)
		gr = session->ea_buf[fa_addr].gr;
	else
		gr = 0;

	if(gr & GR_BLINK)
		a |= LIB3270_ATTR_BLINK;

	if( (gr & GR_UNDERLINE) && lib3270_get_toggle(session,LIB3270_TOGGLE_UNDERLINE))
		a |= LIB3270_ATTR_UNDERLINE;

	if(session->m3279 && (gr & (GR_BLINK | GR_UNDERLINE)) && !(gr & GR_REVERSE))
		a |= LIB3270_ATTR_BACKGROUND_INTENSITY;

	if(!session->m3279 &&	((gr & GR_INTENSIFY) || FA_IS_HIGH(fa)))
		a |= LIB3270_ATTR_INTENSIFY;

	if (gr & GR_REVERSE)
		a = get_color_pair(((a & 0xF0) >> 4),(a & 0x0F)) | (a&0xFF00); // a = reverse_colors(a);

	return a;
}

LIB3270_EXPORT unsigned int lib3270_get_length(const H3270 *h) {
	return h->view.rows * h->view.cols;
}

LIB3270_EXPORT void lib3270_get_screen_size(const H3270 *h, unsigned int *r, unsigned int *c) {
	*r = h->view.rows;
	*c = h->view.cols;
}

LIB3270_EXPORT unsigned int lib3270_get_width(const H3270 *h) {
	return h->view.cols;
}

LIB3270_EXPORT unsigned int lib3270_get_height(const H3270 *h) {
	return h->view.rows;
}

LIB3270_EXPORT unsigned int lib3270_get_max_width(const H3270 *h) {
	return h->max.cols;
}

LIB3270_EXPORT unsigned int lib3270_get_max_height(const H3270 *h) {
	return h->max.rows;
}

void update_model_info(H3270 *hSession, unsigned int model, unsigned int cols, unsigned int rows) {
	if(model == hSession->model_num && hSession->max.rows == rows && hSession->max.cols == cols)
		return;

	hSession->max.cols  	= cols;
	hSession->max.rows  	= rows;
	hSession->model_num	= model;

	/* Update the model name. */
	(void) sprintf(hSession->model_name, "327%c-%d%s",hSession->m3279 ? '9' : '8',hSession->model_num,hSession->extended ? "-E" : "");

	if (hSession->termname != CN)
		hSession->termtype = hSession->termname;
	else if(hSession->oversize.rows || hSession->oversize.cols)
		hSession->termtype = "IBM-DYNAMIC";
	else
		hSession->termtype = hSession->full_model_name;

	trace("Termtype: %s",hSession->termtype);

	hSession->cbk.update_model(hSession, hSession->model_name,hSession->model_num,rows,cols);
}

LIB3270_EXPORT int lib3270_get_contents(H3270 *h, int first, int last, unsigned char *chr, unsigned short *attr) {
	int baddr;
	int len;

	CHECK_SESSION_HANDLE(h);

	len = h->view.rows * h->view.cols;

	if(first > len || last > len || first < 0 || last < 0)
		return EFAULT;

	for(baddr = first; baddr <= last; baddr++) {
		*(chr++)  = h->text[baddr].chr ? h->text[baddr].chr : ' ';
		*(attr++) = h->text[baddr].attr;
	}

	return 0;
}

/* Display what's in the buffer. */
void screen_update(H3270 *session, int bstart, int bend) {
	int				baddr;
	unsigned short	a;
	int				attr = COLOR_GREEN;
	unsigned char	fa;
	int				fa_addr;
	int				first	= -1;
	int				last	= -1;

	fa		= get_field_attribute(session,bstart);
	a  		= color_from_fa(session,fa);
	fa_addr = lib3270_field_addr(session,bstart); // may be -1, that's okay

	for(baddr = bstart; baddr < bend; baddr++) {
		if(session->ea_buf[baddr].fa) {
			// Field attribute.
			fa_addr = baddr;
			fa = session->ea_buf[baddr].fa;
			a = calc_attrs(session, baddr, baddr, fa);
			addch(session,baddr,' ',(attr = COLOR_GREEN)|LIB3270_ATTR_MARKER,&first,&last);
		} else if (FA_IS_ZERO(fa)) {
			// Blank.
			addch(session,baddr,' ',attr=a,&first,&last);
		} else {
			// Normal text.
			if (!(session->ea_buf[baddr].gr || session->ea_buf[baddr].fg || session->ea_buf[baddr].bg)) {
				attr = a;
			} else {
				attr = calc_attrs(session, baddr, fa_addr, fa);
			}

			if (session->ea_buf[baddr].cs == CS_LINEDRAW) {
				addch(session,baddr,session->ea_buf[baddr].cc,attr,&first,&last);
			} else if (session->ea_buf[baddr].cs == CS_APL || (session->ea_buf[baddr].cs & CS_GE)) {
				addch(session,baddr,session->ea_buf[baddr].cc,attr|LIB3270_ATTR_CG,&first,&last);
			} else {
				if(lib3270_get_toggle(session,LIB3270_TOGGLE_MONOCASE))
					addch(session,baddr,session->charset.asc2uc[session->charset.ebc2asc[session->ea_buf[baddr].cc]],attr,&first,&last);
				else
					addch(session,baddr,session->charset.ebc2asc[session->ea_buf[baddr].cc],attr,&first,&last);
			}
		}
	}

	if(first >= 0) {
		int len = (last - first)+1;
		int f;

		for(f=first; f<last; f++) {
			if(f%session->view.cols == 0)
				len++;
		}

		session->cbk.changed(session,first,len);
	}

	if(session->starting && session->formatted && !session->kybdlock && lib3270_in_3270(session)) {
		session->starting = 0;

//		cursor_move(session,next_unprotected(session,0));
//		lib3270_emulate_input(session,"\\n",-1,0);
		session->cbk.autostart(session);

#ifdef DEBUG
		{
			char *text = lib3270_get_string_at_address(session,0,-1,'\n');
			trace("First screen:\n%s\n",text);
			lib3270_free(text);
		}
#endif
	}

//	trace("%s ends",__FUNCTION__);

}

LIB3270_EXPORT int lib3270_get_cursor_address(const H3270 *hSession) {
	int state = check_online_session(hSession);
	return state ? -state : hSession->cursor_addr;
}

LIB3270_EXPORT int lib3270_get_cursor_position(const H3270 *hSession, unsigned short *row, unsigned short *col) {
	int state = check_online_session(hSession);
	if(state) {
		*row = *col = 9;
		return state;
	}

	unsigned short addr = (unsigned short) hSession->cursor_addr;

	if(row)
		*row = (addr / ((unsigned short) hSession->view.cols))+1;

	if(col)
		*col = (addr % ((unsigned short) hSession->view.cols))+1;

	return 0;
}


/**
 * @brief Converts row/col in a buffer address.
 *
 * @param hSession	TN3270 Session.
 * @param row		Row inside the screen.
 * @param col		Col inside the screen.
 *
 * @return Current address or -1 if invalid (sets errno).
 *
 */
LIB3270_EXPORT int lib3270_translate_to_address(const H3270 *hSession, unsigned int row, unsigned int col) {
	FAIL_IF_NOT_ONLINE(hSession);

	row--;
	col--;

	if(row > hSession->view.rows || col > hSession->view.cols)
		return - (errno = EOVERFLOW);

	return (row * hSession->view.cols) + col;
}


LIB3270_EXPORT int lib3270_set_cursor_address(H3270 *hSession, int baddr) {
	FAIL_IF_NOT_ONLINE(hSession);

	trace("%s(%d)",__FUNCTION__,baddr);

	if( ((unsigned int) baddr) > (hSession->view.rows * hSession->view.cols))
		return - (errno = EOVERFLOW);

	if(hSession->selected && !lib3270_get_toggle(hSession,LIB3270_TOGGLE_KEEP_SELECTED))
		lib3270_unselect(hSession);

	return cursor_move(hSession,baddr);
}

LIB3270_EXPORT int lib3270_set_cursor_position(H3270 *hSession, unsigned int row, unsigned int col) {
	int baddr = lib3270_translate_to_address(hSession, row, col);
	if(baddr < 0)
		return -errno;

	return lib3270_set_cursor_address(hSession,baddr);
}

/**
 * @brief Move cursor to a new position.
 *
 * @see lib3270_set_cursor_address
 *
 * @param hSession	TN3270 session.
 * @param baddr		New cursor position.
 *
 * @return Old cursor position.
 *
 */
int cursor_move(H3270 *hSession, int baddr) {
	int ret = hSession->cursor_addr;

	if(ret != baddr && baddr >= 0) {
		hSession->cursor_addr = baddr;
		hSession->cbk.update_cursor(
		    hSession,
		    (unsigned short) (baddr/hSession->view.cols),
		    (unsigned short) (baddr%hSession->view.cols),
		    hSession->text[baddr].chr,
		    hSession->text[baddr].attr
		);
	}

	return ret;
}

/**
 * @brief Status line stuff.
 */
void set_status(H3270 *session, LIB3270_FLAG id, Boolean on) {
	session->oia.flag[id] = (on != 0);
	session->cbk.update_oia(session,id,session->oia.flag[id]);
}

void status_ctlr_done(H3270 *session) {
	CHECK_SESSION_HANDLE(session);
	set_status(session,LIB3270_FLAG_UNDERA,True);
	session->cbk.ctlr_done(session);
}

void status_oerr(H3270 *session, int error_type) {
	LIB3270_MESSAGE sts = LIB3270_MESSAGE_USER;

	CHECK_SESSION_HANDLE(session);

	switch (error_type) {
	case KL_OERR_PROTECTED:
		sts = LIB3270_MESSAGE_PROTECTED;
		break;
	case KL_OERR_NUMERIC:
		sts = LIB3270_MESSAGE_NUMERIC;
		break;
	case KL_OERR_OVERFLOW:
		sts = LIB3270_MESSAGE_OVERFLOW;
		break;

	default:
		return;
	}

	status_changed(session,sts);

}

/**
 * @brief Resolving DNS name.
 *
 */
void status_resolving(H3270 *hSession) {
	debug("%s",__FUNCTION__);

	mcursor_set(hSession,LIB3270_POINTER_LOCKED);

	lib3270_st_changed(hSession, LIB3270_STATE_RESOLVING, True);
	status_changed(hSession, LIB3270_MESSAGE_RESOLVING);
}

void status_connecting(H3270 *hSession) {
	debug("%s",__FUNCTION__);

	mcursor_set(hSession,LIB3270_POINTER_LOCKED);

	lib3270_st_changed(hSession, LIB3270_STATE_CONNECTING, True);
	status_changed(hSession, LIB3270_MESSAGE_CONNECTING);
}

void status_reset(H3270 *session) {
	CHECK_SESSION_HANDLE(session);

	if (session->kybdlock & KL_ENTER_INHIBIT) {
		trace("%s",__FUNCTION__);
		status_changed(session,LIB3270_MESSAGE_INHIBIT);
	} else if (session->kybdlock & KL_DEFERRED_UNLOCK) {
		trace("%s",__FUNCTION__);
		status_changed(session,LIB3270_MESSAGE_X);
	} else {
		trace("%s",__FUNCTION__);
		mcursor_set(session,LIB3270_POINTER_UNLOCKED);
		status_changed(session,LIB3270_MESSAGE_NONE);
	}

	session->cbk.display(session);

}

/**
 * @brief Query the updated terminal status.
 *
 * @return status-code.
 *
 * @see LIB3270_MESSAGE
 */
LIB3270_EXPORT LIB3270_MESSAGE lib3270_get_program_message(const H3270 *hSession) {
	debug("OIA Status=%d",hSession->oia.status);
	return hSession->oia.status;
}

/**
 * Check if the terminal is ready for commands.
 *
 * @param h	Session handle.
 *
 * @return 0 if the terminal is ready (no message, keyboard unlocked), LIB3270_MESSAGE if not
 *
 * @retval LIB3270_MESSAGE_KYBDLOCK	Keyboard is locked.
 * @retval LIB3270_MESSAGE_NONE		Terminal is ready.
 *
 */
LIB3270_EXPORT LIB3270_MESSAGE lib3270_get_lock_status(const H3270 *hSession) {
	if(hSession->kybdlock)
		return LIB3270_MESSAGE_KYBDLOCK;

	if(hSession->oia.status)
		return hSession->oia.status;

	return LIB3270_MESSAGE_NONE;

}

/**
 * Check if terminal is ready for actions.
 *
 * @param h	Session handle.
 *
 * @return Non zero if terminal is ready for commands.
 *
 */
LIB3270_EXPORT int lib3270_is_ready(const H3270 *hSession) {
	return lib3270_get_lock_status(hSession) == LIB3270_MESSAGE_NONE;
}


void status_changed(H3270 *hSession, LIB3270_MESSAGE id) {
	if(id == hSession->oia.status || id < 0)
		return;

	trace_dsn(
	    hSession,
	    "Status changed to %d.\n",
	    (int) id
	);

	hSession->oia.status = id;
	hSession->cbk.update_status(hSession,id);
}

void status_twait(H3270 *session) {
	CHECK_SESSION_HANDLE(session);
	set_status(session,LIB3270_FLAG_UNDERA,False);
	status_changed(session,LIB3270_MESSAGE_TWAIT);
}

void set_viewsize(H3270 *session, unsigned int rows, unsigned int cols) {
	CHECK_SESSION_HANDLE(session);

	if(rows == session->view.rows && session->view.cols == cols)
		return;

	session->view.rows = rows;
	session->view.cols = cols;

	trace("View size changes to %dx%d (configure=%p)",rows,cols,session->cbk.configure);

	if(session->cbk.configure)
		session->cbk.configure(session,session->view.rows,session->view.cols);

}

void status_lu(H3270 *session, const char *lu) {
	CHECK_SESSION_HANDLE(session);

	if(session->cbk.update_luname)
		session->cbk.update_luname(session,lu);

}

static void status_connect(H3270 *hSession, int connected, void GNUC_UNUSED(*dunno)) {
	LIB3270_MESSAGE id = LIB3270_MESSAGE_USER;

	ctlr_erase(hSession,1);

	if (connected) {
		set_status(hSession,LIB3270_FLAG_BOXSOLID,IN_3270 && !IN_SSCP);

		if (hSession->kybdlock & KL_AWAITING_FIRST)
			id = LIB3270_MESSAGE_AWAITING_FIRST;
		else
			id = LIB3270_MESSAGE_CONNECTED;

	} else {
		set_status(hSession,LIB3270_FLAG_BOXSOLID,False);
		id = LIB3270_MESSAGE_DISCONNECTED;
	}

	status_changed(hSession,id);

}

static void status_3270_mode(H3270 *hSession, int GNUC_UNUSED(ignored), void GNUC_UNUSED(*dunno)) {
	Boolean oia_boxsolid = (IN_3270 && !IN_SSCP);

	CHECK_SESSION_HANDLE(hSession);

	if(oia_boxsolid)
		set_status(hSession,LIB3270_FLAG_UNDERA,True);
	set_status(hSession,LIB3270_FLAG_BOXSOLID,oia_boxsolid);

}

void mcursor_set(H3270 *hSession,LIB3270_POINTER m) {

	if(hSession->pointer != ((unsigned short) m)) {

		// Pointer changed
		hSession->pointer = (unsigned short) m;
		hSession->cbk.cursor(hSession,m & 0x03);

		// Notify lock state change.
		lib3270_action_group_notify(hSession, LIB3270_ACTION_GROUP_FORMATTED);

	}
}

LIB3270_EXPORT int lib3270_testpattern(H3270 *hSession) {
	static const unsigned char text_pat[] = {
		// Latin-1 chars
		0x48, 0x68, 0x45, 0x51, 0x55, 0xce, 0xde, 0x46, 0xcf, 0x65, 0x71, 0x75, 0xee, 0xfe, 0x66, 0xef,
		0x90, 0x9a, 0x9b, 0xb5, 0x42, 0x52, 0x56, 0xcb, 0xdb, 0x62, 0x72, 0x76, 0xeb, 0xfb,

		// Regular ASCII chars
		0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86,
		0x87, 0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5,
		0xa6, 0xa7, 0xa8, 0xa9, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xd1, 0xd2, 0xd3,
		0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0x7d, 0x5a,
		0x7c, 0x7b, 0x5b, 0x6c, 0x50, 0x5c, 0x4d, 0x5d, 0x60, 0x6d, 0x4e, 0x7e, 0x7d, 0x79, 0xad, 0xc0,
		0xa1, 0xb0, 0xd0, 0xbd, 0x6b, 0x4c, 0x4b, 0x6e, 0x5e, 0x7a, 0x61, 0x6f, 0x7f,

		// End marker
		0x00
	};

	// http://www.prycroft6.com.au/misc/3270eds.html
	// http://www.prycroft6.com.au/graphics/sym0and1.gif
	static const unsigned char cg_pat[] = {
		0x8c, // CG 0xf7, less or equal "≤"
		0xae, // CG 0xd9, greater or equal "≥"
		0xbe, // CG 0x3e, not equal "≠"
		0xad, // "["
		0xbd, // "]"
		0xc6, // CG 0xa5, left tee
		0xd3, // CG 0xab, plus
		0xa2, // CG 0x92, horizontal line
		0xc7, // CG 0xa6, bottom tee
		0xd7, // CG 0xaf, top tee
		0xd6, // CG 0xae, right tee
		0xc5, // CG 0xa4, UL corner
		0xd5, // CG 0xad, UR corner
		0x85, // CG 0x184, vertical line
		0xc4, // CG 0xa3, LL corner
		0xd4, // CG 0xac, LR corner

		0xf0, // 0-9 Superscript
		0xf1,
		0xf2,
		0xf3,
		0xf4,
		0xf5,
		0xf6,
		0xf7,
		0xf8,
		0xf9,

		0xe1, // 1-3 subscript
		0xe2,
		0xe3,

		0xb8, // Division Sign ÷

		0x00

	};

	static const struct _pat {
		unsigned char cs;
		const unsigned char *cc;
	} pat[] = {
		{ CS_APL,	cg_pat		},
		{ 0,		text_pat	},
	};

	static const unsigned char gr[] = { 0, GR_UNDERLINE, GR_BLINK };

	unsigned int row		= 0;
	int max;
	int pos 	= 0;
	unsigned int grpos	= 0;
	int f;
	int fg		= COLOR_BLUE;

	FAIL_IF_ONLINE(hSession);

	max = (hSession->max.rows * hSession->max.cols);
	for(f=0; f<max; f++) {
		if(!pat[row].cc[pos]) {
			if(++row >= (sizeof(pat)/sizeof(struct _pat)) ) {
				row = 0;
				if(++fg > COLOR_WHITE) {
					fg = COLOR_BLUE;
					if(++grpos > (sizeof(gr)/sizeof(gr[0])))
						grpos = 0;
				}
			}
			pos = 0;
		}
		hSession->ea_buf[f].fg = fg;
		hSession->ea_buf[f].bg = (fg == COLOR_BLACK) ? COLOR_WHITE : COLOR_BLACK;
		hSession->ea_buf[f].cs = pat[row].cs;
		hSession->ea_buf[f].cc = pat[row].cc[pos++];
		hSession->ea_buf[f].gr = gr[grpos];
	}

	hSession->cbk.display(hSession);

	return 0;
}