home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/perl
-
- ######################################################################################
- # X-Server connection example by BRAND Olivier #
- # Create a window, handle 2 events: Mouse and Expose #
- # Notice: You have to be in xhost + to run this program, because the authorisation #
- # is NOT supported yet #
- ######################################################################################
-
- ######################################################################################
-
- #Set the port and get the display name (Without the :0.0 part)
-
- $port = 6000;
- $host = $ENV{'DISPLAY'};
- ($display) = split(/:/,$host,2);
- print "HOST = $display \n";
-
- $AF_INET = 2;
- $SOCK_STREAM = 1;
-
- ######################################################################################
-
- #X-Protocol structures
-
- $xConnClientPrefix = 'C c S S S S S';
- $xConnSetUpPrefix = 'C c S S S';
- $xConnSetup = 'L L L L S S C C C C C C C C L';
- $xDepth = 'C C S L';
- $xWindowRoot = 'L L L L L S S S S S S L C C C C';
- $xVisualType = 'L C C S L L L L';
- $xCreateWindowReq = 'C C S L L s s S S S S L L';
- $xResourceReq = 'C c S L';
- $xGenericReply = 'c c S L L L L L L L';
- $xPolySegmentReq = 'C c S L L';
- $xCreateGCReq = 'C c S L L L';
- $xGCFunc = 'L5';
- $xPoints = 'S4';
- $xButtonPress = 'C C S L L L L S S S S S C C';
- $xExpose = 'C C S L S S S S S S7';
- $xKeyPress = 'C C S L L L L S S S S S C C';
-
- $vendor = 'a64';
- $xmasque = 'L';
- $xback = 'i';
-
-
- ######################################################################################
-
-
- $SIG{'INT'} = 'dokill';
- sub dokill {
- kill 9,$child if $child;
- }
-
- ######################################################################################
-
- #Create the connection to the X-Server
-
- #Structure of the sockaddr struct
- $sockaddr = 'S n a4 x8';
-
- ($name,$aliases,$proto) = getprotobyname('tcp');
- ($name,$aliases,$port) = getservbyname($port,'tcp')
- unless $port =~ /^\d+$/;;
- ($name,$aliases,$type,$len,$servaddr) =
- gethostbyname($display);
-
- $serv = pack($sockaddr, $AF_INET, $port, $servaddr);
-
- # Make the socket filehandle.
-
- if (socket(S, $AF_INET, $SOCK_STREAM, $proto)) {
- print "socket ok\n";
- }
- else {
- die $!;
- }
-
- if(connect(S,$serv)) {
- print "connect ok\n";
- }
- else {
- die $!;
- }
-
- ######################################################################################
-
- #Prepare the X connection
-
- $conn = pack($xConnClientPrefix,0x42,0,11,0,0,0,0);
- $len = syswrite(S,$conn,12);
- print "WRITE LEN = $len\n";
- $len = sysread(S,$data,8);
- print "READ LEN = $len\n";
- @res = unpack($xConnSetUpPrefix,$data);
- printf "%d %d %d %d %d\n",@res;
- if($res[0] == 0) {
- sysread(S,$xerror,$res[1]);
- print "Connection error: $error\n";
- die $!;
- }
- $len = sysread(S,$data,32);
- print "READ LEN = $len\n";
- @connsetup = unpack($xConnSetup,$data);
- printf "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d\n",@connsetup;
- #Read the X-Vendor
- $len = sysread(S,$data,$connsetup[4]);
- @xvendor = unpack($vendor,$data);
- print "rid base = $connsetup[1]\n";
- printf "Vendor = %s\n",@xvendor;
-
- print "READ $connsetup[7] type of DEPTH\n";
- for($i=0;$i<$connsetup[7];$i++) {
- $len = sysread(S,$data,8);
- print"type of DEPTH\n";
- }
- print "READ $connsetup[6] type of ROOTS\n";
- for($i=0;$i<$connsetup[6];$i++) {
- $len = sysread(S,$data,40);
- print "LEN FIRST FOR $len\n";
- @xroot = unpack($xWindowRoot,$data);
- print "READ $xroot[15] DEPTH of ROOT $i\n";
- for($j=0;$j<$xroot[15];$j++) {
- print "READ NUMBER $j\n";
- $len = sysread(S,$data,8);
- print "READ LEN = $len\n";
- @xdepth = unpack($xDepth,$data);
- print "DEPTH of ROOT $i = $xdepth[0], READ $xdepth[2] VISUALS\n";
- for($k=0;$k<$xdepth[2];$k++) {
- $len = sysread(S,$visualtype,24);
- @visu = unpack($xVisualType,$visualtype);
- print "class $visu[1]\n";
- }
- }
- }
-
- @xvisual = unpack($xVisualType,$visualtype);
- $ridbase = $connsetup[1];
- $ridcpt = $ridbase;
- $ridcpt++;
-
- ######################################################################################
-
- #Create our first window
-
- $masque = 0x800 | 0x02;
- $window = pack($xCreateWindowReq,1,$xroot[14],10,$ridbase,$xroot[0],100,100,300,300,3,1,0,$masque);
- $len = syswrite(S,$window,32);
-
- #Background color for the window
-
- $back=pack($xback,3);
- $len = syswrite(S,$back,4);
-
- #Set ExposureMask ButtonPressMask KeyPressMask for desired events
- $mask = 1 << 15 | 1 << 2 | 1 << 0;
-
- $masque = pack($xmasque,$mask);
- $len = syswrite(S,$masque,4);
-
- #Map the window on screen
-
- $mapreq = pack($xResourceReq,8,0,2,$ridbase);
- $len = syswrite(S,$mapreq,8);
-
- #####################################################################################
-
- #Create a graphic context
-
- #Set the GC Mask: GCFunction|GCLineStyle|GCFillStyle|GCForeground|GCBackground
- $gcmasque = 1<<0 | 1<<5 | 1<<8 | 1<<2 | 1<<3;
- $maingc = pack($xCreateGCReq,55,0,9,$ridcpt,$xroot[0],$gcmasque);
- $len = syswrite(S,$maingc,16);
-
- #Assign an index for our gc, andincrement ridcpt for
- #the next object (window, gc, ...) (simple things are sometime the better ones)
-
- $maingc = $ridcpt;
- $ridcpt++;
-
- #Set: GXcopy LineSolid FillSolid Foreground Background for our GC
-
- $gcfunc = pack($xGCFunc,0x3,0,0,0,1);
- $len = syswrite(S,$gcfunc,20);
-
- #####################################################################################
-
- #X-Event Loop (here it is !!!)
-
- while (1) {
- #Wait for a server reply
-
- $len = sysread(S,$xreply,32);
- @reply = unpack($xGenericReply,$xreply);
-
- #Is the reply an error (0) or a normal reply (1) ?
-
- if($reply[0]==0) {
- print "X Error: $reply[1]\n";
- } else {
- print "Event: $reply[0]\n";
- if($reply[0] == 2) {
- print "KeyPress\n";
-
- #Decode a KeyPress structurep
- @keypress = unpack($xKeyPress,$xreply);
- $keycode = $keypress[1];
- print "keycode = $keycode\n";
- }
- if($reply[0] == 4) {
- print "ButtonPress\n";
-
- #Decode a ButtonPress structure
- @buttonpress = unpack($xButtonPress,$xreply);
- #Write the coordinates where the user has clicked on the window
- print "X=$buttonpress[9] Y=$buttonpress[10]\n";
- }
- if($reply[0] == 12) {
- print "Expose\n";
-
- #Decode a xExpose structure
- @expose = unpack($xExpose,$xreply);
- print "Refresh region: width=$expose[6] height=$expose[7] at coordinates: X=$expose[4] Y=$expose[5]\n";
- #Draw a square at the coordinates: 10,50 and height,width: 100,100
- #create a X_PolyFillRectangle structure: type = 70 and send it to the X-Server
- $rect = pack($xPolySegmentReq,70,0,5,$ridbase,$maingc);
- $len = syswrite(S,$rect,12);
- #Send the points
- $poly = pack($xPoints,10,50,100,100);
- $len = syswrite(S,$poly,8);
-
- }
- }
- }
-
-
-
-