This file is indexed.

/usr/lib/python2.7/dist-packages/DisplayCAL/madvr.py is in dispcalgui 3.1.0.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
# -*- coding: utf-8 -*-

# See developers/interfaces/madTPG.h in the madVR package

from __future__ import with_statement
from ConfigParser import RawConfigParser
from StringIO import StringIO
from binascii import unhexlify
from time import sleep, time
from zlib import crc32
import ctypes
import errno
import getpass
import os
import platform
import socket
import struct
import sys
import threading
if sys.platform == "win32":
	import _winreg

if sys.platform == "win32":
	import win32api

import localization as lang
from log import safe_print as log_safe_print
from meta import name as appname, version
from ordereddict import OrderedDict
from util_str import safe_str, safe_unicode


CALLBACK = ctypes.CFUNCTYPE(None, ctypes.c_void_p, ctypes.POINTER(None),
							ctypes.c_char_p, ctypes.c_ulong, ctypes.c_ulonglong,
							ctypes.c_char_p, ctypes.c_ulonglong, ctypes.c_bool)


min_version = (0, 87, 14, 0)


# Search for madTPG on the local PC, connect to the first found instance
CM_ConnectToLocalInstance = 0
# Search for madTPG on the LAN, connect to the first found instance
CM_ConnectToLanInstance = 1
# Start madTPG on the local PC and connect to it
CM_StartLocalInstance = 2
# Search local PC and LAN, and let the user choose which instance to connect to
CM_ShowListDialog = 3
# Let the user enter the IP address of a PC which runs madTPG, then connect
CM_ShowIpAddrDialog = 4
# fail immediately
CM_Fail = 5


_methodnames = ("ConnectEx", "Disable3dlut", "Enable3dlut",
				"GetBlackAndWhiteLevel", "GetDeviceGammaRamp",
				"GetSelected3dlut", "GetVersion",
				"IsDisableOsdButtonPressed",
				"IsStayOnTopButtonPressed",
				"IsUseFullscreenButtonPressed",
				"SetDisableOsdButton",
				"SetDeviceGammaRamp", "SetOsdText",
				"GetPatternConfig", "SetPatternConfig",
				"ShowProgressBar", "SetProgressBarPos",
				"SetSelected3dlut", "SetStayOnTopButton",
				"SetUseFullscreenButton", "ShowRGB",
				"ShowRGBEx", "Load3dlutFile", "Disconnect",
				"Quit", "Load3dlutFromArray256")

_autonet_methodnames = ("AddConnectionCallback", "Listen", "Announce")


_lock = threading.RLock()

def safe_print(*args):
	with _lock:
		log_safe_print(*args)


def inet_pton(ip_string):
	"""
	inet_pton(string) -> packed IP representation

	Convert an IP address in string format to the  packed
	binary format used in low-level network functions.
	
	"""
	if ":" in ip_string:
		# IPv6
		return "".join([unhexlify(block.rjust(4, "0")) for block in ip_string.split(":")])
	else:
		# IPv4
		return "".join([chr(int(block)) for block in ip_string.split(".")])


def trunc(value, length):
	""" For string types, return value truncated to length """
	if isinstance(value, basestring):
		value = safe_str(value)
		if len(repr(value)) > length:
			value = value[:length - 3 - len(str(length)) - len(repr(value)) + len(value)]
			return "%r[:%i]" % (value, length)
	return repr(value)


class H3DLUT(object):

	""" 3D LUT file format used by madVR """

	# https://sourceforge.net/projects/thr3dlut

	def __init__(self, filename):
		with open(filename, "rb") as lut:
			data = lut.read()
		self.signature = data[:4]
		self.fileVersion = struct.unpack("<l", data[4:8])[0]
		self.programName = data[8:40].rstrip("\0")
		self.programVersion = struct.unpack("<q", data[40:48])[0]
		self.inputBitDepth = struct.unpack("<3l", data[48:60])
		self.inputColorEncoding = struct.unpack("<l", data[60:64])[0]
		self.outputBitDepth = struct.unpack("<l", data[64:68])[0]
		self.outputColorEncoding = struct.unpack("<l", data[68:72])[0]
		self.parametersFileOffset = struct.unpack("<l", data[72:76])[0]
		self.parametersSize = struct.unpack("<l", data[76:80])[0]
		self.lutFileOffset = struct.unpack("<l", data[80:84])[0]
		self.lutCompressionMethod = struct.unpack("<l", data[84:88])[0]
		if self.lutCompressionMethod != 0:
			raise ValueError("Compression method not supported: %i" %
							 self.lutCompressionMethod)
		self.lutCompressedSize = struct.unpack("<l", data[88:92])[0]
		self.lutUncompressedSize = struct.unpack("<l", data[92:96])[0]
		self.parametersData = data[self.parametersFileOffset:
								   self.parametersFileOffset +
								   self.parametersSize]
		self.LUTDATA = data[self.lutFileOffset:
							self.lutFileOffset + self.lutCompressedSize]
		if len(self.LUTDATA) != self.lutCompressedSize:
			raise ValueError("3DLUT size %i does not match expected size %i" %
							 (len(self.LUTDATA), self.lutCompressedSize))
		if len(data) == self.lutFileOffset + self.lutCompressedSize + 1552:
			# Calibration appendended
			self.LUTDATA += data[self.lutFileOffset + self.lutCompressedSize:
								 self.lutFileOffset + self.lutCompressedSize +
								 1552]
			self.lutCompressedSize += 1552
			self.lutUncompressedSize += 1552


class MadTPG(object):

	""" Minimal madTPG controller class """

	def __init__(self):
		self._connection_callbacks = []

		# We only expose stuff we might actually use.

		# Find madHcNet32.dll
		clsid = "{E1A8B82A-32CE-4B0D-BE0D-AA68C772E423}"
		try:
			key = _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT,
								  r"CLSID\%s\InprocServer32" % clsid)
			value, valuetype = _winreg.QueryValueEx(key, "")
		except:
			raise RuntimeError(lang.getstr("madvr.not_found"))
		self.dllpath = os.path.join(os.path.split(value)[0], "madHcNet32.dll")
		if not value or not os.path.isfile(self.dllpath):
			raise OSError(lang.getstr("not_found", self.dllpath))
		handle = win32api.LoadLibrary(self.dllpath)
		self.mad = ctypes.WinDLL(self.dllpath, handle=handle)

		try:
			# Set expected return value types
			for methodname in _methodnames + _autonet_methodnames:
				if methodname == "AddConnectionCallback":
					continue
				if methodname in _autonet_methodnames:
					prefix = "AutoNet"
				else:
					prefix = "madVR"
				getattr(self.mad, prefix + "_" + methodname).restype = ctypes.c_bool

			# Set expected argument types
			self.mad.madVR_ShowRGB.argtypes = [ctypes.c_double] * 3
			self.mad.madVR_ShowRGBEx.argtypes = [ctypes.c_double] * 6
		except AttributeError:
			raise RuntimeError(lang.getstr("madhcnet.outdated",
										   tuple(reversed(os.path.split(self.dllpath))) +
										   min_version))

	def __del__(self):
		if hasattr(self, "mad"):
			self.disconnect()

	def __getattr__(self, name):
		# Instead of writing individual method wrappers, we use Python's magic
		# to handle this for us. Note that we're sticking to pythonic method
		# names, so 'disable_3dlut' instead of 'Disable3dlut' etc.

		# Convert from pythonic method name to CamelCase
		methodname = "".join(part.capitalize() for part in name.split("_"))

		# Check if this is a madVR method we support
		if methodname not in _methodnames + _autonet_methodnames:
			raise AttributeError("%r object has no attribute %r" %
								 (self.__class__.__name__, name))

		# Return the method
		if methodname in _autonet_methodnames:
			prefix = "AutoNet"
		else:
			prefix = "madVR"
		return getattr(self.mad, prefix + "_" + methodname)

	def add_connection_callback(self, callback, param, component):
		"""
		Handles callbacks for added/closed connections to playback components
		
		Leave "component" empty to get notification about all components.
		
		The callback function has to take eight arguments:
		param, connection, ip, pid, module, component, instance, is_new_instance
		
		"""
		callback = CALLBACK(callback)
		self.mad.AutoNet_AddConnectionCallback(callback, param, component)
		self._connection_callbacks.append(callback)

	def connect(self, method1=CM_ConnectToLocalInstance, timeout1=1000,
				method2=CM_ConnectToLanInstance, timeout2=3000,
				method3=CM_ShowListDialog, timeout3=0, method4=CM_Fail,
				timeout4=0, parentwindow=None):
		""" Find, select or launch a madTPG instance and connect to it """
		return self.mad.madVR_ConnectEx(method1, timeout1, method2, timeout2,
										method3, timeout3, method4, timeout4,
										parentwindow)

	def get_black_and_white_level(self):
		""" Return madVR output level setup """
		blacklvl, whitelvl = ctypes.c_long(), ctypes.c_long()
		result = self.mad.madVR_GetBlackAndWhiteLevel(*[ctypes.byref(v) for v in
														(blacklvl, whitelvl)])
		return result and (blacklvl.value, whitelvl.value)

	def get_device_gamma_ramp(self):
		""" Calls the win32 API 'GetDeviceGammaRamp' """
		ramp = ((ctypes.c_ushort * 256) * 3)()
		result = self.mad.madVR_GetDeviceGammaRamp(ramp)
		return result and ramp

	def get_pattern_config(self):
		"""
		Return the pattern config as 4-tuple
		
		Pattern area in percent        1-100
		Background level in percent    0-100
		Background mode                0 = constant gray
		                               1 = APL - gamma light
		                               2 = APL - linear light
		Black border width in pixels   0-100
		"""
		area, bglvl, bgmode, border = [ctypes.c_long() for i in xrange(4)]
		result = self.mad.madVR_GetPatternConfig(*[ctypes.byref(v) for v in
												   (area, bglvl, bgmode,
												    border)])
		return result and (area.value, bglvl.value, bgmode.value, border.value)

	def get_selected_3dlut(self):
		thr3dlut = ctypes.c_ulong()
		result = self.mad.madVR_GetSelected3dlut(ctypes.byref(thr3dlut))
		return result and thr3dlut.value

	def get_version(self):
		version = ctypes.c_ulong()
		result = self.mad.madVR_GetVersion(ctypes.byref(version))
		version = tuple(struct.unpack(">B", c)[0] for c in
						struct.pack(">I", version.value))
		return result and version

	def show_rgb(self, r, g, b, bgr=None, bgg=None, bgb=None):
		""" Shows a specific RGB color test pattern """
		if not None in (bgr, bgg, bgb):
			return self.mad.madVR_ShowRGBEx(r, g, b, bgr, bgg, bgb)
		else:
			return self.mad.madVR_ShowRGB(r, g, b)

	@property
	def uri(self):
		return self.dllpath


class MadTPG_Net(object):

	""" Implementation of madVR network protocol in pure python """

	# Wireshark filter to help ananlyze traffic:
	# (tcp.dstport != 1900 and tcp.dstport != 443) or (udp.dstport != 1900 and udp.dstport != 137 and udp.dstport != 138 and udp.dstport != 5355 and udp.dstport != 547 and udp.dstport != 10111)

	def __init__(self):
		self._cast_sockets = {}
		self._casts = []
		self._client_sockets = OrderedDict()
		self._commandno = 0
		self._commands = {}
		hostname = socket.gethostname()
		self._host = socket.gethostbyname(hostname)
		self._incoming = {}
		self._ips = [i[4][0] for i in socket.getaddrinfo(hostname, None)]
		self._pid = 0
		self._reset()
		self._server_sockets = {}
		self._threads = []
		#self.broadcast_ports = (39568, 41513, 45817, 48591, 48912)
		self.broadcast_ports = (37018, 10658, 63922, 53181, 4287)
		self.clients = OrderedDict()
		self.debug = 0
		self.listening = True
		#self.multicast_ports = (34761, )
		self.multicast_ports = (51591, )
		self._event_handlers = {"on_client_added": [],
								"on_client_confirmed": [],
								"on_client_removed": [],
								"on_client_updated": []}
		#self.server_ports = (37612, 43219, 47815, 48291, 48717)
		self.server_ports = (60562, 51130, 54184, 41916, 19902)
		ip = self._host.split(".")
		ip.pop()
		ip.append("255")
		self.broadcast_ip = ".".join(ip)
		self.multicast_ip = "235.117.220.191"

	def listen(self):
		self.listening = True
		# Connection listen sockets
		for port in self.server_ports:
			if ("", port) in self._server_sockets:
				continue
			sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
			sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
			sock.settimeout(0)
			try:
				sock.bind(("", port))
				sock.listen(1)
				thread = threading.Thread(target=self._conn_accept_handler,
										  args=(sock, "", port))
				self._threads.append(thread)
				thread.start()
			except socket.error, exception:
				safe_print("MadTPG_Net: TCP Port %i: %s" % (port, exception))
		# Broadcast listen sockets
		for port in self.broadcast_ports:
			if (self.broadcast_ip, port) in self._cast_sockets:
				continue
			sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
			sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
			sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
			sock.settimeout(0)
			try:
				sock.bind(("", port))
				thread = threading.Thread(target=self._cast_receive_handler,
										  args=(sock, self.broadcast_ip, port))
				self._threads.append(thread)
				thread.start()
			except socket.error, exception:
				safe_print("MadTPG_Net: UDP Port %i: %s" % (port, exception))
		# Multicast listen socket
		for port in self.multicast_ports:
			if (self.multicast_ip, port) in self._cast_sockets:
				continue
			sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
			sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
			sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32)
			sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP,
							struct.pack("4sl",
										socket.inet_aton(self.multicast_ip),
										socket.INADDR_ANY))
			sock.settimeout(0)
			try:
				sock.bind(("", port))
				thread = threading.Thread(target=self._cast_receive_handler,
										  args=(sock, self.multicast_ip, port))
				self._threads.append(thread)
				thread.start()
			except socket.error, exception:
				safe_print("MadTPG_Net: UDP Port %i: %s" % (port, exception))

	def bind(self, event_name, handler):
		""" Bind a handler to an event """
		if not event_name in self._event_handlers:
			self._event_handlers[event_name] = []
		self._event_handlers[event_name].append(handler)

	def unbind(self, event_name, handler=None):
		"""
		Unbind (remove) a handler from an event
		
		If handler is None, remove all handlers for the event.
		
		"""
		if event_name in self._event_handlers:
			if handler in self._event_handlers[event_name]:
				self._event_handlers[event_name].remove(handler)
				return handler
			else:
				return self._event_handlers.pop(event_name)

	def _dispatch_event(self, event_name, event_data=None):
		""" Dispatch events """
		if self.debug:
			safe_print("MadTPG_Net: Dispatching", event_name)
		for handler in self._event_handlers.get(event_name, []):
			handler(event_data)

	def _reset(self):
		self._client_socket = None

	def _conn_accept_handler(self, sock, host, port):
		if self.debug:
			safe_print("MadTPG_Net: Entering incoming connection thread for port",
					   port)
		self._server_sockets[(host, port)] = sock
		while getattr(self, "listening", False):
			try:
				# Wait for connection
				conn, addr = sock.accept()
			except socket.timeout, exception:
				# Should never happen for non-blocking socket
				safe_print("MadTPG_Net: In incoming connection thread for port %i:" %
						   port, exception)
				continue
			except socket.error, exception:
				if exception.errno == errno.EWOULDBLOCK:
					sleep(.05)
					continue
				safe_print("MadTPG_Net: Exception in incoming connection "
						   "thread for %s:%i:" % addr[:2], exception)
				break
			conn.settimeout(0)
			with _lock:
				if self.debug:
					safe_print("MadTPG_Net: Incoming connection from %s:%s to %s:%s" %
							   (addr[:2] + conn.getsockname()[:2]))
				if addr in self._client_sockets:
					if self.debug:
						safe_print("MadTPG_Net: Already connected from %s:%s to %s:%s" %
								   (addr[:2] + conn.getsockname()[:2]))
					self._shutdown(conn, addr)
				else:
					self._client_sockets[addr] = conn
					thread = threading.Thread(target=self._receive_handler,
											  args=(addr, conn, ))
					self._threads.append(thread)
					thread.start()
		self._server_sockets.pop((host, port))
		self._shutdown(sock, (host, port))
		if self.debug:
			safe_print("MadTPG_Net: Exiting incoming connection thread for port",
					   port)

	def _receive_handler(self, addr, conn):
		if self.debug:
			safe_print("MadTPG_Net: Entering receiver thread for %s:%s" %
					   addr[:2])
		self._incoming[addr] = []
		hello = self._hello(conn)
		blob = ""
		send_bye = True
		while (hello and addr in self._client_sockets and
			   getattr(self, "listening", False)):
			# Wait for incoming message
			try:
				incoming = conn.recv(4096)
			except socket.timeout, exception:
				# Should never happen for non-blocking socket
				safe_print("MadTPG_Net: In receiver thread for %s:%i:" %
						   addr[:2], exception)
				continue
			except socket.error, exception:
				if exception.errno == errno.EWOULDBLOCK:
					sleep(.001)
					continue
				if exception.errno not in (errno.EBADF,
										   errno.ECONNRESET) or self.debug:
					safe_print("MadTPG_Net: In receiver thread for %s:%i:" %
							   addr[:2], exception)
				send_bye = False
				break
			else:
				with _lock:
					if not incoming:
						# Connection broken
						if self.debug:
							safe_print("MadTPG_Net: Client %s:%i stopped sending" %
									   addr[:2])
						break
					blob += incoming
					if self.debug:
						safe_print("MadTPG_Net: Received from %s:%s:" %
								   addr[:2])
					while blob and addr in self._client_sockets:
						try:
							record, blob = self._parse(blob)
						except ValueError, exception:
							safe_print("MadTPG_Net:", exception)
							# Invalid, discard
							blob = ""
						else:
							if record is None:
								# Need more data
								break
							self._process(record, conn)
		with _lock:
			self._remove_client(addr, send_bye=addr in self._client_sockets and
											   send_bye)
			self._incoming.pop(addr)
		if self.debug:
			safe_print("MadTPG_Net: Exiting receiver thread for %s:%s" %
					   addr[:2])

	def _remove_client(self, addr, send_bye=True):
		""" Remove client from list of connected clients """
		if addr in self._client_sockets:
			conn = self._client_sockets.pop(addr)
			if send_bye:
				self._send(conn, "bye",
						   component=self.clients.get(addr,
													  {}).get("component", ""))
			if addr in self.clients:
				client = self.clients.pop(addr)
				if self.debug:
					safe_print("MadTPG_Net: Removed client %s:%i" %
							   addr[:2])
				self._dispatch_event("on_client_removed", (addr, client))
			if (self._client_socket and
				self._client_socket == conn):
				self._reset()
			self._shutdown(conn, addr)

	def _cast_receive_handler(self, sock, host, port):
		if host == self.broadcast_ip:
			cast = "broadcast"
		elif host == self.multicast_ip:
			cast = "multicast"
		else:
			cast = "unknown"
		if self.debug:
			safe_print("MadTPG_Net: Entering receiver thread for %s port %i" %
					   (cast, port))
		self._cast_sockets[(host, port)] = sock
		while getattr(self, "listening", False):
			try:
				data, addr = sock.recvfrom(4096)
			except socket.timeout, exception:
				safe_print("MadTPG_Net: In receiver thread for %s port %i:" %
						   (cast, port), exception)
				continue
			except socket.error, exception:
				if exception.errno == errno.EWOULDBLOCK:
					sleep(.05)
					continue
				if exception.errno != errno.ECONNRESET or self.debug:
					safe_print("MadTPG_Net: In receiver thread for %s port %i:" %
							   (cast, port), exception)
				break
			else:
				with _lock:
					if self.debug:
						safe_print("MadTPG_Net: Received %s from %s:%s: %r" %
								   (cast, addr[0], addr[1], data))
					if not addr in self._casts:
						for c_port in self.server_ports:
							if (addr[0], c_port) in self._client_sockets:
								if self.debug:
									safe_print("MadTPG_Net: Already connected to %s:%s" %
											   (addr[0], c_port))
							elif (("", c_port) in self._server_sockets and
								  addr[0] in self._ips):
								if self.debug:
									safe_print("MadTPG_Net: Don't connect to self %s:%s" %
											   (addr[0], c_port))
							else:
								conn = self._get_client_socket(addr[0], c_port)
								threading.Thread(target=self._connect,
												 args=(conn, addr[0], c_port)).start()
					else:
						self._casts.remove(addr)
						if self.debug:
							safe_print("MadTPG_Net: Ignoring own %s from %s:%s" %
									   (cast, addr[0], addr[1]))
		self._cast_sockets.pop((host, port))
		self._shutdown(sock, (host, port))
		if self.debug:
			safe_print("MadTPG_Net: Exiting %s receiver thread for port %i" %
					   (cast, port))

	def __del__(self):
		self.shutdown()

	def _shutdown(self, sock, addr):
		try:
			# Will fail if the socket isn't connected, i.e. if there
			# was an error during the call to connect()
			sock.shutdown(socket.SHUT_RDWR)
		except socket.error, exception:
			if exception.errno != errno.ENOTCONN:
				safe_print("MadTPG_Net: SHUT_RDWR for %s:%i failed:" %
						   addr[:2], exception)
		sock.close()

	def shutdown(self):
		self.disconnect()
		self.listening = False
		while self._threads:
			thread = self._threads.pop()
			if thread.isAlive():
				thread.join()

	def __getattr__(self, name):
		# Instead of writing individual method wrappers, we use Python's magic
		# to handle this for us. Note that we're sticking to pythonic method
		# names, so 'disable_3dlut' instead of 'Disable3dlut' etc.

		# Convert from pythonic method name to CamelCase
		methodname = "".join(part.capitalize() for part in name.split("_"))

		if methodname == "ShowRgb":
			methodname = "ShowRGB"

		# Check if this is a madVR method we support
		if methodname not in _methodnames:
			raise AttributeError("%r object has no attribute %r" %
								 (self.__class__.__name__, name))

		# Call the method and return the result
		return MadTPG_Net_Sender(self, self._client_socket, methodname)

	def announce(self):
		""" Anounce ourselves """
		for port in self.multicast_ports:
			sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
			sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 32)
			sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 0)
			sock.settimeout(1)
			sock.connect((self.multicast_ip, port))
			addr = sock.getsockname()
			self._casts.append(addr)
			if self.debug:
				safe_print("MadTPG_Net: Sending multicast from %s:%s to port %i" %
						   (addr[0], addr[1], port))
			sock.sendall(struct.pack("<i", 0))
			self._shutdown(sock, (self.multicast_ip, port))
		for port in self.broadcast_ports:
			sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
			sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
			sock.settimeout(1)
			sock.connect((self.broadcast_ip, port))
			addr = sock.getsockname()
			self._casts.append(addr)
			if self.debug:
				safe_print("MadTPG_Net: Sending broadcast from %s:%s to port %i" %
						   (addr[0], addr[1], port))
			sock.sendall(struct.pack("<i", 0))
			self._shutdown(sock, (self.broadcast_ip, port))

	def connect(self, method1=CM_ConnectToLanInstance, timeout1=4000,
				method2=CM_ShowListDialog, timeout2=0,
				method3=CM_Fail, timeout3=0, method4=CM_Fail,
				timeout4=0, parentwindow=None):
		""" Find or select a madTPG instance on the network and connect to it """
		for i in xrange(1, 5):
			method = locals()["method%i" % i]
			timeout = locals()["timeout%i" % i] / 1000.0
			if method in (CM_ConnectToLanInstance, CM_ShowListDialog):
				if not self._cast_sockets:
					self.listen()
					# Give a little time for the user to acknowledge any
					# OS firewall prompts
					sleep(3)
				if method == CM_ShowListDialog:
					# TODO: Implement
					pass
				else:
					# Re-use existing connection
					if not self._wait_for_client(None, 0.001):
						# Otherwise, announce ourselves
						self.announce()
					if self._wait_for_client(None, timeout - 0.001):
						return True
			elif method == CM_ShowIpAddrDialog:
				# TODO: Implement
				pass
		return False

	def connect_to_ip(self, ip, timeout=1000):
		""" Connect to madTPG running under a known IP address """
		ip = socket.gethostbyname(ip)
		for port in self.server_ports:
			conn = self._get_client_socket(ip, port)
			threading.Thread(target=self._connect,
							 args=(conn, ip, port, timeout / 1000.0)).start()
		return self._wait_for_client((ip, port), timeout / 1000.0)

	def _get_client_socket(self, host, port, timeout=1):
		""" Return a new or existing client socket """
		if (host, port) in self._client_sockets:
			return self._client_sockets[(host, port)]
		sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		sock.settimeout(timeout)
		self._client_sockets[(host, port)] = sock
		return sock

	def _connect(self, sock, host, port, timeout=1):
		""" Connect to IP:PORT, return socket """
		if self.debug:
			safe_print("MadTPG_Net: Connecting to %s:%s..." %
					   (host, port))
		try:
			sock.connect((host, port))
		except socket.error, exception:
			if self.debug:
				safe_print("MadTPG_Net: Connecting to %s:%s failed:" %
						   (host, port), exception)
			with _lock:
				self._remove_client((host, port), False)
		else:
			if self.debug:
				safe_print("MadTPG_Net: Connected to %s:%s" % (host, port))
			sock.settimeout(0)
			thread = threading.Thread(target=self._receive_handler,
									  args=((host, port), sock, ))
			self._threads.append(thread)
			thread.start()

	def disconnect(self, stop=True):
		returnvalue = False
		conn = self._client_socket
		if conn:
			returnvalue = True
			if stop:
				returnvalue = self._send(conn, "StopTestPattern")
		self._reset()
		return returnvalue

	def _process(self, record, conn):
		""" Process madVR packet """
		command = record["command"]
		if command not in ("bye", "confirm", "hello", "reply"):
			# Ignore
			return
		addr = conn.getpeername()
		commandno = record["commandNo"]
		component = record["component"]
		params = record["params"]
		client = OrderedDict()
		client["processId"] = record["processId"]
		client["module"] = record["module"]
		client["component"] = component
		client["instance"] = record["instance"]
		if command == "reply":
			if params == "+":
				params = True
			elif params == "-":
				params = False
		elif command == "confirm":
			if addr not in self.clients:
				self.clients[addr] = client
				self._dispatch_event("on_client_added",
									 (addr, self.clients[addr]))
			self.clients[addr]["confirmed"] = True
			self._dispatch_event("on_client_confirmed",
								 (addr, self.clients[addr]))
		elif command == "hello":
			client.update(params)
			if addr not in self.clients:
				self.clients[addr] = client
				if self._is_master(conn):
					# Prevent duplicate connections
					for c_addr, c_client in self.clients.iteritems():
						if (c_client.get("confirmed") and
							c_client["processId"] == client["processId"] and
							c_client["module"] == client["module"]):
							if self.debug:
								safe_print("MadTPG_Net: Preventing duplicate connection %s:%i" %
										   addr[:2])
							self._remove_client(addr)
							return
				self._dispatch_event("on_client_added", (addr, client))
			else:
				client_copy = self.clients[addr].copy()
				self.clients[addr].update(client)
				if self.clients[addr] != client_copy:
					self._dispatch_event("on_client_updated",
										 (addr, self.clients[addr]))
			if (not self.clients[addr].get("confirmed") and
				self._is_master(conn) and
				self._send(conn, "confirm", component="")):
				# We are master, sent confirm packet
				self.clients[addr]["confirmed"] = True
				self._dispatch_event("on_client_confirmed",
									 (addr, self.clients[addr]))
				# Close duplicate connections
				for c_addr, c_client in self.clients.iteritems():
					if (c_addr != addr and
						c_client["processId"] == client["processId"] and
						c_client["module"] == client["module"]):
						if self.debug:
							safe_print("MadTPG_Net: Closing duplicate connection %s:%i" %
									   c_addr[:2])
						self._remove_client(c_addr)
		elif command == "bye":
			if self.debug:
				safe_print("MadTPG_Net: Client %s:%i disconnected" % addr[:2])
			self._remove_client(addr)
		self._incoming[addr].append((commandno, command, params, component))

	def get_version(self):
		""" Return madVR version """
		return (self._client_socket and
				self.clients.get(self._client_socket.getpeername(),
								 {}).get("mvrVersion") or False)

	def _assemble_hello_params(self):
		""" Assemble 'hello' packet parameters """
		info = [("computerName", safe_unicode(socket.gethostname().upper())),
				("userName", safe_unicode(getpass.getuser())),
				("os", "%s %s" % (platform.system(), platform.release())),
				("exeFile", os.path.basename(sys.executable)), ("exeVersion",
															    version),
				("exeDescr", ""), ("exeIcon", "")]
		params = ""
		for key, value in info:
			params += ("%s=%s\t" % (key, value)).encode("UTF-16-LE", "replace")
		return params

	def _hello(self, conn):
		""" Send 'hello' packet. Return boolean wether send succeeded or not """
		params = self._assemble_hello_params()
		return self._send(conn, "hello", params, "")

	def _is_master(self, conn):
		""" Return wether our end of the connection is the master or not """
		local = conn.getsockname()
		remote = conn.getpeername()
		return (inet_pton(local[0]) > inet_pton(remote[0]) or
				(inet_pton(local[0]) == inet_pton(remote[0]) and
				 self.clients[remote]["processId"] < os.getpid()))

	def _expect(self, conn, commandno=-1, command=None, params=(), component="",
				timeout=3):
		""" Wait until expected reply or timeout. Return reply params or False. """
		if not isinstance(params, (list, tuple)):
			params = (params, )
		addr = conn.getpeername()
		start = end = time()
		while end - start < timeout:
			for reply in self._incoming.get(addr, []):
				r_commandno, r_command, r_params, r_component = reply
				if (commandno in (r_commandno, -1) and
					command in (r_command, None)
					and not params or (r_params in params) and
					component in (r_component, None)):
					self._incoming[addr].remove(reply)
					return r_params
			sleep(0.001)
			end = time()
		if self.debug:
			safe_print("MadTPG_Net: Timeout exceeded while waiting for reply")
		return False

	def _wait_for_client(self, addr=None, timeout=1):
		""" Wait for (first) madTPG client connection and handshake """
		start = end = time()
		while end - start < timeout:
			clients = self.clients.copy()
			if clients:
				if addr:
					c_addrs = [addr]
				else:
					c_addrs = clients.keys()
				for c_addr in c_addrs:
					client = clients.get(c_addr)
					conn = self._client_sockets.get(c_addr)
					if (client and
						client["component"] == "madTPG" and
						client.get("confirmed") and conn and
						self._send(conn, "StartTestPattern")):
						self._client_socket = conn
						return True
			sleep(0.001)
			end = time()
		return False

	def _parse(self, blob=""):
		""" Consume blob, return record + remaining blob """
		if len(blob) < 12:
			return None, blob
		crc = struct.unpack("<I", blob[8:12])[0]
		# Check CRC
		check = crc32(blob[:8]) & 0xFFFFFFFF
		if check != crc:
			raise ValueError("MadTPG_Net: Invalid madVR packet: CRC check "
							 "failed: Expected %i, got %i" % (crc, check))
		datalen = struct.unpack("<i", blob[4:8])[0]
		if len(blob) < datalen + 12:
			return None, blob
		record = OrderedDict([("magic", blob[0:4]),
							  ("len", struct.unpack("<i", blob[4:8])[0]),
							  ("crc", struct.unpack("<i", blob[8:12])[0]),
							  ("processId",
							   struct.unpack("<i", blob[12:16])[0]),
							  ("module", struct.unpack("<q", blob[16:24])[0]),
							  ("commandNo",
							   struct.unpack("<i", blob[24:28])[0]),
							  ("sizeOfComponent",
							   struct.unpack("<i", blob[28:32])[0])])
		a = 32
		b = a + record["sizeOfComponent"]
		if b > len(blob):
			raise ValueError("Corrupt madVR packet: Expected component "
							 "len %i, got %i" % (b - a, len(blob[a:b])))
		record["component"] = blob[a:b]
		a = b + 8
		if a > len(blob):
			raise ValueError("Corrupt madVR packet: Expected instance "
							 "len %i, got %i" % (a - b, len(blob[b:a])))
		record["instance"] = struct.unpack("<q", blob[b:a])[0]
		b = a + 4
		if b > len(blob):
			raise ValueError("Corrupt madVR packet: Expected sizeOfCommand "
							 "len %i, got %i" % (b - a, len(blob[a:b])))
		record["sizeOfCommand"] = struct.unpack("<i", blob[a:b])[0]
		a = b + record["sizeOfCommand"]
		if a > len(blob):
			raise ValueError("Corrupt madVR packet: Expected command "
							 "len %i, got %i" % (a - b, len(blob[b:a])))
		record["command"] = command = blob[b:a]
		b = a + 4
		if b > len(blob):
			raise ValueError("Corrupt madVR packet: Expected sizeOfParams "
							 "len %i, got %i" % (b - a, len(blob[a:b])))
		record["sizeOfParams"] = struct.unpack("<i", blob[a:b])[0]
		a = b + record["sizeOfParams"]
		if a > record["len"] + 12:
			raise ValueError("Corrupt madVR packet: Expected params "
							 "len %i, got %i" % (a - b, len(blob[b:a])))
		params = blob[b:a]
		if self.debug > 1:
			record["rawParams"] = params
		if command == "hello":
			io = StringIO("[Default]\n" +
						  "\n".join(params.decode("UTF-16-LE",
												  "replace").strip().split("\t")))
			cfg = RawConfigParser()
			cfg.optionxform = str
			cfg.readfp(io)
			params = OrderedDict(cfg.items("Default"))
			# Convert version strings to tuples with integers
			for param in ("mvr", "exe"):
				param += "Version"
				if param in params:
					values = params[param].split(".")
					for i, value in enumerate(values):
						try:
							values[i] = int(value)
						except ValueError:
							pass
					params[param] = tuple(values)
		elif command == "reply":
			commandno = record["commandNo"]
			repliedcomamnd = self._commands.get(commandno)
			if repliedcomamnd:
				self._commands.pop(commandno)
				if repliedcomamnd == "GetBlackAndWhiteLevel":
					if len(params) == 8:
						params = struct.unpack("<ii", params)
					else:
						params = False
				elif repliedcomamnd == "GetDeviceGammaRamp":
					# Convert to ushort_Array_256_Array_3
					ramp = ((ctypes.c_ushort * 256) * 3)()
					if len(params) == 1536:
						for j in xrange(3):
							for i in xrange(256):
								ramp[j][i] = int(round(struct.unpack("<H", params[:2])[0]))
								params = params[2:]
						params = ramp
					else:
						params = False
				elif repliedcomamnd == "GetPatternConfig":
					if len(params) == 16:
						params = struct.unpack("<iiii", params)
					else:
						params = False
				elif repliedcomamnd in ("GetSelected3dlut", ):
					if len(params) == 4:
						params = struct.unpack("<i", params[0:4])[0]
					else:
						params = False
			else:
				# Got a reply for a command we never issued?
				if self.debug:
					safe_print("MadTPG_Net: Got reply %i for unknown command" %
							   commandno)
		record["params"] = params
		if self.debug:
			with _lock:
				safe_print(record["processId"], record["module"],
						   record["commandNo"], record["component"],
						   record["instance"], record["command"])
				for key, value in record.iteritems():
					if key == "params" or self.debug > 2:
						if isinstance(value, dict):
							safe_print("  %s:" % key)
							for subkey, subvalue in value.iteritems():
								if self.debug < 2 and subkey != "exeFile":
									continue
								safe_print("    %s = %s" % (subkey.ljust(16),
															trunc(subvalue, 56)))
						elif self.debug > 1:
							safe_print("  %s = %s" % (key.ljust(16),
													  trunc(value, 58)))
		blob = blob[a:]
		return record, blob

	def _assemble(self, conn, commandno=1, command="", params="", component="madTPG"):
		""" Assemble packet """
		magic = "mad."
		data = struct.pack("<i", os.getpid())  # processId
		data += struct.pack("<q", id(sys.modules[__name__]))  # module/DLL handle
		data += struct.pack("<i", commandno)
		data += struct.pack("<i", len(component))  # sizeOfComponent
		data += component
		if component == "madTPG":
			instance = self.clients.get(conn.getpeername(), {}).get("instance", 0)
		else:
			instance = 0
		data += struct.pack("<q", instance)  # instance
		data += struct.pack("<i", len(command))  # sizeOfCommand
		data += command
		data += struct.pack("<i", len(params))  # sizeOfParams
		data += params
		datalen = len(data)
		packet = magic + struct.pack("<i", datalen)
		packet += struct.pack("<I", crc32(packet) & 0xFFFFFFFF)
		packet += data
		if self.debug > 1:
			with _lock:
				safe_print("MadTPG_Net: Assembled madVR packet:")
				self._parse(packet)
		return packet

	def _send(self, conn, command="", params="", component="madTPG"):
		""" Send madTPG command and return reply """
		if not conn:
			return False
		self._commandno += 1
		commandno = self._commandno
		try:
			packet = self._assemble(conn, commandno, command, params,
									component)
			if self.debug:
				safe_print("MadTPG_Net: Sending command %i %r to %s:%s" %
						   ((commandno, command) + conn.getpeername()[:2]))
			while packet:
				try:
					bytes_sent = conn.send(packet)
				except socket.error, exception:
					if exception.errno == errno.EAGAIN:
						# Mac OS X: Resource temporarily unavailable
						sleep(0.001)
						if bytes_sent == -1:
							continue
					else:
						raise
				if bytes_sent == 0:
					raise socket.error(errno.ENOLINK, "Link has been severed")
				packet = packet[bytes_sent:]
		except socket.error, exception:
			safe_print("MadTPG_Net: Sending command %i %r failed" %
					   (commandno, command), exception)
			return False
		if command not in ("confirm", "hello", "reply",
						   "bye") and not command.startswith("store:"):
			self._commands[commandno] = command
			# Get reply
			if self.debug:
				safe_print("MadTPG_Net: Expecting reply for command %i %r" %
						   (commandno, command))
			return self._expect(conn, commandno, "reply")
		return True

	@property
	def uri(self):
		return "%s:%s" % (self._client_socket and
						  self._client_socket.getpeername()[:2] or
						  ("0.0.0.0", 0))


class MadTPG_Net_Sender(object):

	def __init__(self, madtpg, conn, command):
		self.madtpg = madtpg
		self._conn = conn
		if command == "Quit":
			command = "Exit"
		self.command = command

	def __call__(self, *args, **kwargs):
		if self.command == "Load3dlutFile":
			lut = H3DLUT(args[0])
			lutdata = lut.LUTDATA
			self.command = "Load3dlut"
		elif self.command == "Load3dlutFromArray256":
			lutdata = args[0]
			self.command = "Load3dlut"
		if self.command == "Load3dlut":
			params = struct.pack("<i", args[1])  # Save to settings?
			params += struct.pack("<i", args[2])  # 3D LUT slot
			params += lutdata
		elif self.command == "SetDeviceGammaRamp":
			params = ""
			for j in xrange(3):
				for i in xrange(256):
					if args[0] is None:
						# Clear device gamma ramp
						v = i * 257
					else:
						# Convert ushort_Array_256_Array_3 to string
						v = args[0][j][i]
					params += struct.pack("<H", v)
		elif self.command in ("SetDisableOsdButton", "SetStayOnTopButton",
							  "SetUseFullscreenButton"):
			if args[0]:
				params = "+"
			else:
				params = "-"
		elif self.command == "SetOsdText":
			params = args[0].encode("UTF-16-LE")
		elif self.command in ("SetPatternConfig", "SetProgressBarPos"):
			params = "|".join(str(v) for v in args)
		elif self.command == "ShowRGB":
			r, g, b, bgr, bgg, bgb = (None, ) * 6
			for name in ("r", "g", "b", "bgr", "bgg", "bgb"):
				locals()[name] = kwargs.get(name)
			if len(args) >= 3:
				r, g, b = args[:3]
			if len(args) > 3:
				bgr = args[3]
			if len(args) > 4:
				bgg = args[4]
			if len(args) > 5:
				bgb = args[5]
			rgb = r, g, b
			if not None in (bgr, bgg, bgb):
				command += "Ex"
				rgb += (bgr, bgg, bgb)
			if None in (r, g, b):
				raise TypeError("show_rgb() takes at least 4 arguments (%i given)" %
								len(filter(lambda v: v, rgb)))
			params = "|".join(str(v) for v in rgb)
		else:
			params = str(*args)
		return self.madtpg._send(self._conn, self.command, params)


if __name__ == "__main__":
	import config
	config.initcfg()
	lang.init()
	if sys.platform == "win32":
		madtpg = MadTPG()
	else:
		madtpg = MadTPG_Net()
	if madtpg.connect(method3=CM_StartLocalInstance, timeout3=5000):
		madtpg.show_rgb(1, 0, 0)