remote.cc 18.1 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 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
/*
 * "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.
 *
 * 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., 59 Temple
 * Place, Suite 330, Boston, MA, 02111-1307, USA
 *
 * Este programa está nomeado como remote.cc e possui - linhas de código.
 *
 * Contatos:
 *
 * perry.werneck@gmail.com	(Alexandre Perry de Souza Werneck)
 * erico.mendonca@gmail.com	(Erico Mascarenhas Mendonça)
 *
 */

 #include "rx3270.h"

#if defined(HAVE_DBUS)
	#include <stdio.h>
	#include <dbus/dbus.h>
#endif // HAVE_DBUS

#if defined(WIN32)
	#include <pw3270/ipcpackets.h>
#endif // WIN32

 #include <time.h>
 #include <string.h>
 #include <ctype.h>

/*--[ Class definition ]-----------------------------------------------------------------------------*/

 class remote : public rx3270
 {
 public:
	remote(const char *session);
	~remote();

	char			* get_revision(void);
	LIB3270_CSTATE	  get_cstate(void);
	int				  disconnect(void);
	int				  connect(const char *uri, bool wait = true);
	bool			  is_connected(void);
	bool			  is_ready(void);

	int				  iterate(bool wait);
	int				  wait(int seconds);
	int				  wait_for_ready(int seconds);

	char 			* get_text_at(int row, int col, size_t sz);
	int				  cmp_text_at(int row, int col, const char *text);
	int 			  set_text_at(int row, int col, const char *str);

	int				  set_cursor_position(int row, int col);

	void 			  set_toggle(LIB3270_TOGGLE ix, bool value);

	int				  enter(void);
	int				  pfkey(int key);
	int				  pakey(int key);

 private:
#if defined(WIN32)

	HANDLE			  hPipe;

#elif defined(HAVE_DBUS)

	DBusConnection	* conn;
	char			* dest;
	char			* path;
	char			* intf;
	DBusMessage		* create_message(const char *method);
	DBusMessage		* call(DBusMessage *msg);
	char 			* query_string(const char *method);
	int 			  query_intval(const char *method);

#endif


 };

/*--[ Globals ]--------------------------------------------------------------------------------------*/

#if defined(HAVE_DBUS)
 static const char * prefix_dest	= "br.com.bb.";
 static const char * prefix_path	= "/br/com/bb/";
#endif // HAVE_DBUS

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

#if defined(HAVE_DBUS)
DBusMessage * remote::create_message(const char *method)
{
	DBusMessage * msg = dbus_message_new_method_call(	this->dest,		// Destination
														this->path,		// Path
														this->intf,		// Interface
														method);		// method

	if (!msg)
		log("Error creating message for method %s",method);

	return msg;
}
#endif // HAVE_DBUS

rx3270 * rx3270::create_remote(const char *name)
{
	return new remote(name);
}


#if defined(HAVE_DBUS)
remote::remote(const char *name) : rx3270(REXX_DEFAULT_CHARSET,"UTF-8")
#else
remote::remote(const char *name)
#endif // HAVE_DBUS
{
#if defined(WIN32)
	static DWORD	  dwMode = PIPE_READMODE_MESSAGE;
 	char	 		  buffer[4096];
 	char			* str = strdup(name);
 	char			* ptr;

	hPipe  = INVALID_HANDLE_VALUE;

 	for(ptr=str;*ptr;ptr++)
	{
		if(*ptr == ':')
			*ptr = '_';
		else
			*ptr = tolower(*ptr);
	}

	snprintf(buffer,4095,"\\\\.\\pipe\\%s",str);

	free(str);

	if(!WaitNamedPipe(buffer,NMPWAIT_USE_DEFAULT_WAIT))
	{
		log("%s","Invalid service instance");
		return;
	}

	hPipe = CreateFile(buffer,GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);

	if(hPipe == INVALID_HANDLE_VALUE)
	{
		log("%s","Can´t create service pipe");
		return;
	}

	if(!SetNamedPipeHandleState(hPipe,&dwMode,NULL,NULL))
	{
		log("%s","Can´t set pipe state");
		CloseHandle(hPipe);
		hPipe = INVALID_HANDLE_VALUE;
		return;
	}

	// Connected

#elif defined(HAVE_DBUS)
	DBusError	  err;
	int			  rc;
	char		* str = strdup(name);
	char		* ptr;

	for(ptr=str;*ptr;ptr++)
		*ptr = tolower(*ptr);

	ptr = strchr(str,':');

	if(ptr)
	{
		size_t 		  sz;

		*(ptr++) = 0;

		// Build destination
		sz		= strlen(ptr)+strlen(str)+strlen(prefix_dest)+2;
		dest	= (char *) malloc(sz+1);
		strncpy(dest,prefix_dest,sz);
		strncat(dest,str,sz);
		strncat(dest,".",sz);
		strncat(dest,ptr,sz);

		// Build path
		sz		= strlen(str)+strlen(prefix_path);
		path	= (char *) malloc(sz+1);
		strncpy(path,prefix_path,sz);
		strncat(path,str,sz);

		// Build intf
		sz		= strlen(str)+strlen(prefix_dest)+1;
		intf	= (char *) malloc(sz+1);
		strncpy(intf,prefix_dest,sz);
		strncat(intf,str,sz);

	}
	else
	{
		size_t sz;

		// Build destination
		sz		= strlen(str)+strlen(prefix_dest)+2;
		dest	= (char *) malloc(sz+1);
		strncpy(dest,prefix_dest,sz);
		strncat(dest,str,sz);

		// Build path
		sz		= strlen(str)+strlen(prefix_path);
		path	= (char *) malloc(sz+1);
		strncpy(path,prefix_path,sz);
		strncat(path,str,sz);

		// Build intf
		sz		= strlen(str)+strlen(prefix_dest)+1;
		intf	= (char *) malloc(sz+1);
		strncpy(intf,prefix_dest,sz);
		strncat(intf,str,sz);

	}

	trace("DBUS:\nDestination:\t[%s]\nPath:\t\t[%s]\nInterface:\t[%s]",dest,path,intf);

	free(str);

	dbus_error_init(&err);

	conn = dbus_bus_get(DBUS_BUS_SESSION, &err);

	if (dbus_error_is_set(&err))
	{
		log("DBUS Connection Error (%s)", err.message);
		dbus_error_free(&err);
	}

	if(!conn)
	{
		log("%s", "DBUS Connection failed");
		return;
	}

	rc = dbus_bus_request_name(conn, "br.com.bb." PACKAGE_NAME ".rexx", DBUS_NAME_FLAG_REPLACE_EXISTING , &err);

	if (dbus_error_is_set(&err))
	{
		log("Name Error (%s)", err.message);
		dbus_error_free(&err);
		conn = NULL;
		return;
	}

	if(rc != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
	{
		log("%s", "DBUS request name failed");
		conn = NULL;
		return;
	}


#else

#endif
}

remote::~remote()
{
#if defined(WIN32)

	if(hPipe != INVALID_HANDLE_VALUE)
		CloseHandle(hPipe);

#elif defined(HAVE_DBUS)

	free(dest);
	free(path);
	free(intf);

#else

#endif
}

#if defined(HAVE_DBUS)
DBusMessage	* remote::call(DBusMessage *msg)
{
	DBusMessage		* reply;
	DBusError		  error;

	dbus_error_init(&error);
	reply = dbus_connection_send_with_reply_and_block(conn,msg,10000,&error);
	dbus_message_unref(msg);

	if(!reply)
	{
		log("%s",error.message);
		dbus_error_free(&error);
	}

	return reply;

}

char * get_string(DBusMessage * msg)
{
	char *rc = NULL;
	if(msg)
	{
		DBusMessageIter iter;

		if(dbus_message_iter_init(msg, &iter))
		{
			if(dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING)
			{
				const char * str;
				dbus_message_iter_get_basic(&iter, &str);
				trace("Response: [%s]",str);
				rc = strdup(str);
			}
#ifdef DEBUG
			else
			{
				trace("Return type is %c, expecting %c",dbus_message_iter_get_arg_type(&iter),DBUS_TYPE_STRING);
			}
#endif
		}

		dbus_message_unref(msg);
	}
	return rc;
}

char * remote::query_string(const char *method)
{
	if(conn)
		return get_string(call(create_message(method)));
	return NULL;
}

int get_intval(DBusMessage * msg)
{
	int rc = -1;

	if(msg)
	{
		DBusMessageIter iter;

		if(dbus_message_iter_init(msg, &iter))
		{
			if(dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_INT32)
			{
				dbus_int32_t iSigned;
				dbus_message_iter_get_basic(&iter, &iSigned);
				rc = (int) iSigned;
			}
#ifdef DEBUG
			else
			{
				trace("Return type is %c, expecting %c",dbus_message_iter_get_arg_type(&iter),DBUS_TYPE_INT32);
			}
#endif
		}

		dbus_message_unref(msg);
	}

	return rc;
}

int remote::query_intval(const char *method)
{
	if(conn)
		return get_intval(call(create_message(method)));
	return -1;
}


#endif // HAVE_DBUS

char * remote::get_revision(void)
{
#if defined(WIN32)

	return strdup(PACKAGE_REVISION);

#elif defined(HAVE_DBUS)

	return query_string("getRevision");

#else

	return NULL;

#endif

}


LIB3270_CSTATE remote::get_cstate(void)
{
#if defined(WIN32)

	if(hPipe != INVALID_HANDLE_VALUE)
	{
		static const struct hllapi_packet_query query		= { HLLAPI_PACKET_GET_CSTATE };
		struct hllapi_packet_result		  		response;
		DWORD							  		cbSize		= sizeof(query);
		TransactNamedPipe(hPipe,(LPVOID) &query, cbSize, &response, sizeof(response), &cbSize,NULL);
		return (LIB3270_CSTATE) response.rc;
	}

	return (LIB3270_CSTATE) -1;

#elif defined(HAVE_DBUS)

	return (LIB3270_CSTATE) query_intval("getConnectionState");

#else

	return (LIB3270_CSTATE) -1;

#endif

}

int remote::disconnect(void)
{
#if defined(WIN32)

	if(hPipe != INVALID_HANDLE_VALUE)
	{
		static const struct hllapi_packet_query query		= { HLLAPI_PACKET_DISCONNECT };
		struct hllapi_packet_result		  		response;
		DWORD							  		cbSize		= sizeof(query);
		TransactNamedPipe(hPipe,(LPVOID) &query, cbSize, &response, sizeof(response), &cbSize,NULL);
		return 0;
	}
	return -1;

#elif defined(HAVE_DBUS)

	return query_intval("disconnect");

#else

	return -1;

#endif

}

int remote::connect(const char *uri, bool wait)
{
#if defined(WIN32)
	if(hPipe != INVALID_HANDLE_VALUE)
	{
		struct hllapi_packet_connect	* pkt;
		struct hllapi_packet_result		  response;
		DWORD							  cbSize;

		cbSize	= sizeof(struct hllapi_packet_connect)+strlen(uri);
		pkt 	= (struct hllapi_packet_connect *) malloc(cbSize);

		pkt->packet_id	= HLLAPI_PACKET_CONNECT;
		pkt->wait		= (unsigned char) wait;
		strcpy(pkt->hostname,uri);

		trace("Sending %s",pkt->hostname);

		if(!TransactNamedPipe(hPipe,(LPVOID) pkt, cbSize, &response, sizeof(response), &cbSize,NULL))
		{
			errno 		= GetLastError();
			response.rc = -1;
		}

		free(pkt);

		return response.rc;

	}

#elif defined(HAVE_DBUS)

	int rc;
	DBusMessage * msg = create_message("connect");
	if(!msg)
		return -1;

	dbus_message_append_args(msg, DBUS_TYPE_STRING, &uri, DBUS_TYPE_INVALID);

	rc = get_intval(call(msg));

	if(!rc && wait)
		return wait_for_ready(120);

	return rc;

#endif

	return -1;
}

bool remote::is_connected(void)
{
#if defined(WIN32)

	if(hPipe != INVALID_HANDLE_VALUE)
	{
		static const struct hllapi_packet_query query		= { HLLAPI_PACKET_IS_CONNECTED };
		struct hllapi_packet_result		  		response;
		DWORD							  		cbSize		= sizeof(query);
		TransactNamedPipe(hPipe,(LPVOID) &query, cbSize, &response, sizeof(response), &cbSize,NULL);
		return response.rc != 0;
	}

#elif defined(HAVE_DBUS)

	return query_intval("isConnected") != 0;

#endif

	return false;
}

bool remote::is_ready(void)
{
#if defined(WIN32)

	if(hPipe != INVALID_HANDLE_VALUE)
	{
		static const struct hllapi_packet_query query		= { HLLAPI_PACKET_IS_READY };
		struct hllapi_packet_result		  		response;
		DWORD							  		cbSize		= sizeof(query);
		TransactNamedPipe(hPipe,(LPVOID) &query, cbSize, &response, sizeof(response), &cbSize,NULL);
		return response.rc != 0;
	}

#elif defined(HAVE_DBUS)

	return query_intval("isReady") != 0;

#endif

	return false;
}

int remote::iterate(bool wait)
{
#if defined(WIN32)

	return 0;

#elif defined(HAVE_DBUS)

	return 0;

#endif

	return -1;
}

int remote::wait(int seconds)
{
#if defined(WIN32)

 	time_t end = time(0)+seconds;

	while(time(0) < end)
	{
		if(!is_connected())
			return ENOTCONN;
		Sleep(500);
	}

	return 0;

#elif defined(HAVE_DBUS)

 	time_t end = time(0)+seconds;

	while(time(0) < end)
	{
		if(!is_connected())
			return ENOTCONN;
		usleep(500);
	}

	return 0;

#endif

	return -1;
}

int remote::wait_for_ready(int seconds)
{
#if defined(WIN32)

	if(hPipe != INVALID_HANDLE_VALUE)
	{
		time_t end = time(0)+seconds;

		while(time(0) < end)
		{
			if(!is_connected())
				return ENOTCONN;

			if(is_ready())
				return 0;

			Sleep(250);
		}

		return ETIMEDOUT;

	}

	return -1;

#elif defined(HAVE_DBUS)

	time_t end = time(0)+seconds;

	while(time(0) < end)
	{
		static const dbus_int32_t delay = 2;

		DBusMessage		* msg = create_message("waitForReady");
		int				  rc;

		if(!msg)
			return -1;

		dbus_message_append_args(msg, DBUS_TYPE_INT32, &delay, DBUS_TYPE_INVALID);

		rc = get_intval(call(msg));
		trace("waitForReady exits with rc=%d",rc);
		if(rc != ETIMEDOUT)
			return rc;
	}

	return ETIMEDOUT;

#else

	return -1;

#endif

}

char * remote::get_text_at(int row, int col, size_t sz)
{
#if defined(WIN32)

	if(hPipe != INVALID_HANDLE_VALUE)
	{
		struct hllapi_packet_query_at	  query		= { HLLAPI_PACKET_GET_TEXT_AT, (unsigned short) row, (unsigned short) col, (unsigned short) sz };
		struct hllapi_packet_text		* response;
		DWORD							  cbSize	= sizeof(struct hllapi_packet_text)+sz;
		char 							* text		= NULL;

		response = (struct hllapi_packet_text *) malloc(cbSize+2);
		memset(response,0,cbSize+2);

		if(!TransactNamedPipe(hPipe,(LPVOID) &query, sizeof(struct hllapi_packet_query_at), &response, cbSize, &cbSize,NULL))
			return NULL;

		if(response->packet_id)
			errno = response->packet_id;
		else
			text  = strdup(response->text);

		free(response);
		return text;

	}

#elif defined(HAVE_DBUS)

	dbus_int32_t r = (dbus_int32_t) row;
	dbus_int32_t c = (dbus_int32_t) col;
	dbus_int32_t l = (dbus_int32_t) sz;

	DBusMessage * msg = create_message("getTextAt");
	if(!msg)
		return NULL;

	trace("%s(%d,%d,%d)",__FUNCTION__,r,c,l);
	dbus_message_append_args(msg, DBUS_TYPE_INT32, &r, DBUS_TYPE_INT32, &c, DBUS_TYPE_INT32, &l, DBUS_TYPE_INVALID);

	return get_string(call(msg));

#endif

	return NULL;
}

int remote::cmp_text_at(int row, int col, const char *text)
{
#if defined(WIN32)

	if(hPipe != INVALID_HANDLE_VALUE)
	{
		struct hllapi_packet_text_at 	* query;
		struct hllapi_packet_result	  	  response;
		DWORD							  cbSize		= sizeof(struct hllapi_packet_text_at)+strlen(text);

		query = (struct hllapi_packet_text_at *) malloc(cbSize);
		query->packet_id 	= HLLAPI_PACKET_CMP_TEXT_AT;
		query->row			= row;
		query->col			= col;
		strcpy(query->text,text);

		TransactNamedPipe(hPipe,(LPVOID) query, cbSize, &response, sizeof(response), &cbSize,NULL);

		free(query);

		return response.rc;
	}


#elif defined(HAVE_DBUS)

	dbus_int32_t r = (dbus_int32_t) row;
	dbus_int32_t c = (dbus_int32_t) col;

	DBusMessage * msg = create_message("cmpTextAt");
	if(msg)
	{
		dbus_message_append_args(msg, DBUS_TYPE_INT32, &r, DBUS_TYPE_INT32, &c, DBUS_TYPE_STRING, &text, DBUS_TYPE_INVALID);
		return get_intval(call(msg));
	}

#endif

	return 0;
}

int remote::set_text_at(int row, int col, const char *str)
{
#if defined(WIN32)

	if(hPipe != INVALID_HANDLE_VALUE)
	{
		struct hllapi_packet_text_at 	* query;
		struct hllapi_packet_result	  	  response;
		DWORD							  cbSize		= sizeof(struct hllapi_packet_text_at)+strlen((const char *) str);

		query = (struct hllapi_packet_text_at *) malloc(cbSize);
		query->packet_id 	= HLLAPI_PACKET_SET_TEXT_AT;
		query->row			= row;
		query->col			= col;
		strcpy(query->text,(const char *) str);

		TransactNamedPipe(hPipe,(LPVOID) query, cbSize, &response, sizeof(response), &cbSize,NULL);

		free(query);

		return response.rc;
	}

#elif defined(HAVE_DBUS)

	dbus_int32_t r = (dbus_int32_t) row;
	dbus_int32_t c = (dbus_int32_t) col;

	DBusMessage * msg = create_message("setTextAt");
	if(msg)
	{
		dbus_message_append_args(msg, DBUS_TYPE_INT32, &r, DBUS_TYPE_INT32, &c, DBUS_TYPE_STRING, &str, DBUS_TYPE_INVALID);
		return get_intval(call(msg));
	}

#endif

	return -1;
}

int remote::set_cursor_position(int row, int col)
{
#if defined(WIN32)

	if(hPipe != INVALID_HANDLE_VALUE)
	{
		struct hllapi_packet_cursor 	query		= { HLLAPI_PACKET_SET_CURSOR_POSITION, (unsigned short) row, (unsigned short) col };
		struct hllapi_packet_result		response;
		DWORD							cbSize		= sizeof(query);
		TransactNamedPipe(hPipe,(LPVOID) &query, cbSize, &response, sizeof(response), &cbSize,NULL);
		return response.rc != 0;
	}

#elif defined(HAVE_DBUS)

	dbus_int32_t r = (dbus_int32_t) row;
	dbus_int32_t c = (dbus_int32_t) col;

	DBusMessage * msg = create_message("setCursorAt");
	if(msg)
	{
		dbus_message_append_args(msg, DBUS_TYPE_INT32, &r, DBUS_TYPE_INT32, &c, DBUS_TYPE_INVALID);
		return get_intval(call(msg));
	}

#endif

	return -1;
}

int remote::enter(void)
{
#if defined(WIN32)

	if(hPipe != INVALID_HANDLE_VALUE)
	{
		static const struct hllapi_packet_query query		= { HLLAPI_PACKET_ENTER };
		struct hllapi_packet_result		  		response;
		DWORD							  		cbSize		= sizeof(query);
		TransactNamedPipe(hPipe,(LPVOID) &query, cbSize, &response, sizeof(response), &cbSize,NULL);
		return response.rc;
	}

	return -1;

#elif defined(HAVE_DBUS)

	return query_intval("enter");

#else

	return -1;

#endif

}

int remote::pfkey(int key)
{
#if defined(WIN32)

	if(hPipe != INVALID_HANDLE_VALUE)
	{
		struct hllapi_packet_keycode	query		= { HLLAPI_PACKET_PFKEY, (unsigned short) key };
		struct hllapi_packet_result		response;
		DWORD							cbSize		= sizeof(query);
		TransactNamedPipe(hPipe,(LPVOID) &query, cbSize, &response, sizeof(response), &cbSize,NULL);
		return response.rc;
	}

#elif defined(HAVE_DBUS)

	dbus_int32_t k = (dbus_int32_t) key;

	DBusMessage * msg = create_message("pfKey");
	if(msg)
	{
		dbus_message_append_args(msg, DBUS_TYPE_INT32, &k, DBUS_TYPE_INVALID);
		return get_intval(call(msg));
	}

#endif

	return -1;
}

int remote::pakey(int key)
{
#if defined(WIN32)

	if(hPipe != INVALID_HANDLE_VALUE)
	{
		struct hllapi_packet_keycode	query		= { HLLAPI_PACKET_PAKEY, (unsigned short) key };
		struct hllapi_packet_result		response;
		DWORD							cbSize		= sizeof(query);
		TransactNamedPipe(hPipe,(LPVOID) &query, cbSize, &response, sizeof(response), &cbSize,NULL);
		return response.rc;
	}

#elif defined(HAVE_DBUS)

	dbus_int32_t k = (dbus_int32_t) key;

	DBusMessage * msg = create_message("paKey");
	if(msg)
	{
		dbus_message_append_args(msg, DBUS_TYPE_INT32, &k, DBUS_TYPE_INVALID);
		return get_intval(call(msg));
	}

#endif

	return -1;
}

void remote::set_toggle(LIB3270_TOGGLE ix, bool value)
{
#if defined(WIN32)

	if(hPipe != INVALID_HANDLE_VALUE)
	{

	}

#elif defined(HAVE_DBUS)

	dbus_int32_t i = (dbus_int32_t) ix;
	dbus_int32_t v = (dbus_int32_t) value;

	DBusMessage * msg = create_message("setToggle");
	if(msg)
	{
		dbus_message_append_args(msg, DBUS_TYPE_INT32, &i, DBUS_TYPE_INT32, &v, DBUS_TYPE_INVALID);
		get_intval(call(msg));
	}


#endif

}