Herbert Kleebauer wrote:
> Ok, at least now I converted "Annie's flying hearts" as an example
Here the second example with a moveable window, mouse and keyboard input.
Again, for comments see the assembly source, hla.asm (NASM) or hla.mac
(Lindela).
Use mouse to convert any "HLA" to "asm", press "r" for reset and "ESC"
for exit.
/* gcc -O3 -o hla hla.c*/
#include <stdio.h>
#define abs(x) (((x) >=0) ? (x) : (- (x)))
void exit();
void x_send();
int x_receive_raw();
int x_receive();
void display();
void annie();
void init_color();
unsigned int eax, x_handle, id_next, id_incr;
int screen[320*200];
int color[64];
char brett[10][10];
#define BUF_SIZE 1024
char buf[BUF_SIZE]; int buf_rest=0; char* buf_ptr;
int mes_size; char *mes_ptr;
char fname[300];
char prot_name[256], prot_data[256]; int prot_name_size=0,
prot_data_size=0;
FILE *fp=NULL;
/**************************************************************************/
const char sockaddr_un[]=
{1,0,'/','t','m','p','/','.','X','1','1','-','u','n','i','x','/','X','0'};
/**************************************************************************/
/************************* Connection Setup
******************************/
volatile struct {char a[2]; short b[2],s1n,s1d,c;}
send1={0x6c,0,11,0,0,0,0};
/**************************************************************************/
/************************* Create Window
*********************************/
volatile struct {char a[2]; short b; int s2a,s2b; short c[2],s2x,s2y,d[2];
int e[4];}
send2={1,0,sizeof(send2)/4,0,0,0,0,400,400,0,0,0,0x802,0x8080ff,0x8005};
/**************************************************************************/
/************************* Map Window
***********************************/
volatile struct {char a[2]; short b; int s3a;}
send3={8,0,sizeof(send3)/4,0};
/**************************************************************************/
/************************* Open Font
************************************/
volatile struct {char a[2]; short b; int s4c; short c[2];char d[18];
char e[2];}
send4={45,0,sizeof(send4)/4,0,18,0,'l','u','c','i','d','a',
's','a','n','s','-','b','o','l','d','-','1','2',0,0};
/**************************************************************************/
/************************* Create GC
************************************/
volatile struct {char a[2]; short b; int s5b,s5a, c[4], s5c;}
send5={55,0,sizeof(send5)/4,0,0,1+4+8+0x4000,3,0xe02020,0x20e020,0};
/**************************************************************************/
/************************* Change GC
*************************************/
volatile struct {char a[2]; short b; int s6b, c[3];}
send6={56,0,sizeof(send6)/4,0,4+8,0x20e020,0xe02020};
/**************************************************************************/
/************************* Change GC
*************************************/
volatile struct {char a[2]; short b; int s7b, c[3];}
send7={56,0,sizeof(send7)/4,0,4+8,0xe02020,0x20e020};
/**************************************************************************/
/************************* PolyFillArc
***********************************/
volatile struct {char a[2]; short b; int s8a,s8b; short s8x,s8y,c[4];}
send8={71,0,sizeof(send8)/4,0,0,0,0,50,50,0*64,360*64};
/**************************************************************************/
/************************* ImageText8
************************************/
volatile struct {char a[2]; short b;int s9a,s9b;short s9x,s9y; char
s9t[3];
char d;}
send9={76,3,sizeof(send9)/4,0,0,0,30,'H','L','A',0};
/**************************************************************************/
/**************************************************************************/
/************************* main
*****************************************/
/**************************************************************************/
int main(nargs,args) int nargs; char **args;
{char *p, *q;
int i,j,k,n=nargs+1;
/************************* read .Xauthority
*****************************/
while ((p=args[n++]) != NULL)
{if (*(int*)p != ('H'+('O'<<8) +('M'<<16) +('E'<<24)) ) continue;
if (p[4] != '=' || p[5] == 0) continue;
p=p+5; q=fname;
for (i=0; i<256; i++) if ( (*q++ = *p++) == 0) break;
if (*(--q)) exit(0);
p="/.Xauthority"; while ((*q++ = *p++));
fp=fopen(fname,"rb");
break;
}
if (fp != NULL)
{while ( (i=getc(fp),j=getc(fp)) != EOF)
{if ( ((j<<8)+i) != 1)
{for (k=0; k<4; k++)
{i=getc(fp); i = (i<<8) + getc(fp);
for (j=0; j<i; j++) if (getc(fp) == EOF) break;
}
}
else
{for (k=0; k<2; k++)
{i=getc(fp); i = (i<<8) + getc(fp);
for (j=0; j<i; j++) if (getc(fp) == EOF) break;
}
i=getc(fp); prot_name_size = (i<<8) + getc(fp);
if (prot_name_size > 256) exit(0); p=prot_name;
for (i=0; i<prot_name_size; i++) *p++=getc(fp);
i=getc(fp); prot_data_size = (i<<8) + getc(fp);
if (prot_data_size > 256) exit(0); p=prot_data;
for (i=0; i<prot_data_size; i++) *p++=getc(fp);
break;
}
}
fclose(fp);
}
/**************************************************************************/
/************************* get socket handle
****************************/
const int gsh[] = {1,1,0};
asm volatile ("int $0x80":"=a" (x_handle): "0" (102), "b" (1), "c"
(&gsh));
if (x_handle>=-4095) exit(0);
/**************************************************************************/
/*********************** connect socket to tmp/.X11-unix/X0
***************/
const int cst[]={x_handle, (int) &sockaddr_un,sizeof(sockaddr_un)};
/* with -O3 but without volatile, cst[1/2] is intialized after the int 80
*/
asm volatile ("int $0x80":"=a" (eax): "0" (102), "b" (3), "c" (&cst));
if (eax>=-4095) exit(0);
/**************************************************************************/
/***************************** send connect message
**********************/
send1.s1n=prot_name_size;
send1.s1d=prot_data_size;
x_send((char*)&send1, sizeof(send1));
if (prot_name_size) x_send(prot_name,(prot_name_size+3)&0xfffffffc);
if (prot_data_size) x_send(prot_data,(prot_data_size+3)&0xfffffffc);
buf_ptr=buf; buf_rest=0;
n=8;
for (k=0; k<2; k++)
{while (buf_rest < n)
{i=x_receive_raw(buf_ptr,BUF_SIZE-buf_rest);
buf_rest += i; buf_ptr += i;
if ( (buf_ptr != buf) && buf[0] != 1) exit(0);
}
n= 4 * (*(short*)(buf+6)) + 8;
}
buf_ptr=buf; buf_rest=0;
/**************************************************************************/
/***************************** calculate id's
****************************/
id_next = *(int*)(buf+12); n = *(int*)(buf+16);
id_incr = n & (-n);
send2.s2a=id_next; send3.s3a=id_next; send5.s5a=id_next;
send8.s8a=id_next; send9.s9a=id_next;
id_next += id_incr;
send5.s5b=id_next; send6.s6b=id_next; send7.s7b=id_next;
send8.s8b=id_next; send9.s9b=id_next;
id_next += id_incr;
send4.s4c=id_next; send5.s5c=id_next;
id_next += id_incr;
/**************************************************************************/
/***************************** get root window id
************************/
n = (((*(short*)(buf+24)) + 43) & -4) + ((*(unsigned char*)(buf+29)) *8);
send2.s2b = *(int*)(buf+n);
/**************************************************************************/
/***************************** send CreatWindow request
*******************/
x_send((char*)&send2, sizeof(send2));
/***************************** send MapWindow request
*********************/
x_send((char*)&send3, sizeof(send3));
/***************************** send OpenFont request
**********************/
x_send((char*)&send4, sizeof(send4));
/***************************** send CreatDC request
***********************/
x_send((char*)&send5, sizeof(send5));
/**************************************************************************/
/***************************** main loop
*********************************/
display();
while (1)
{
if (x_receive() !=0 )
switch (*mes_ptr)
{case 0: exit(0);
case 2: if (mes_ptr[1] == 0x1b)
{for (i=1; i<10; i++) for (j=1; j<10; j++) brett[i][j]=0;
display();
}
else if (mes_ptr[1] == 0x09) exit(0);
break;
case 4: i = 1+(((*(short*)(mes_ptr+26))/50)&0x07);
j = 1+(((*(short*)(mes_ptr+24))/50)&0x07);
brett[i ][j ] = !brett[i ][j ];
brett[i-1][j ] = !brett[i-1][j ];
brett[i+1][j ] = !brett[i+1][j ];
brett[i ][j-1] = !brett[i ][j-1];
brett[i ][j+1] = !brett[i ][j+1];
display();
break;
case 12: display();
break;
}
}
}
/**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/***************************** display
***********************************/
void display()
{int i,j;
send8.s8y=0;
for (i=1;i<9;i++)
{send8.s8x=0;
for (j=1;j<9;j++)
{if (brett[i][j]) x_send((char*)&send8, sizeof(send8));
send8.s8x += 50;
}
send8.s8y += 50;
}
x_send((char*)&send7, sizeof(send7));
send9.s9t[0]='a'; send9.s9t[1]='s'; send9.s9t[2]='m';
send8.s8y=0; send9.s9y=30;
for (i=1;i<9;i++)
{send8.s8x=0; send9.s9x=5;
for (j=1;j<9;j++)
{if (!brett[i][j]) x_send((char*)&send8, sizeof(send8));
else x_send((char*)&send9, sizeof(send9));
send8.s8x += 50; send9.s9x += 50;
}
send8.s8y += 50; send9.s9y += 50;
}
x_send((char*)&send6, sizeof(send6));
send9.s9t[0]='H'; send9.s9t[1]='L'; send9.s9t[2]='A';
send9.s9y=30;
for (i=1;i<9;i++)
{send8.s8x=0; send9.s9x=5;
for (j=1;j<9;j++)
{if (!brett[i][j]) x_send((char*)&send9, sizeof(send9));
send9.s9x += 50;
}
send9.s9y += 50;
}
}
/**************************************************************************/
/******************** send message to X server
****************************/
void x_send(p,n) char *p; int n;
{while (n)
{eax=-11;
const int par[]={x_handle, (int)p, n, 0};
while (eax == -11)
asm volatile ("int $0x80":"=a" (eax): "0" (102), "b" (9), "c"
(&par));
if (eax>=-4095) exit(0);
n -= eax; p += eax;
}
}
/**************************************************************************/
/*********************** receive ONE message from X server
***************/
int x_receive()
{int n;
while (1)
{if (buf_rest >= 32)
{n=32;
if (*buf_ptr == 1) n += (*(int*)(buf_ptr+4))*4;
if (buf_rest >= n)
{mes_ptr=buf_ptr; mes_size=n;
buf_ptr += n; buf_rest -= n; return 1;
}
}
if (buf_ptr != buf )
{for (n=0; n<buf_rest; n++) buf[n] = *buf_ptr++;
buf_ptr=buf;
}
if ((n=x_receive_raw(buf+buf_rest,BUF_SIZE-buf_rest)) == 0) return 0;
buf_rest += n;
}
}
/**************************************************************************/
/************************ read data from X server
************************/
int x_receive_raw(p,n) char *p; int n;
{const int par[]={x_handle, (int)p, n, 0};
/* with -O3 but without "memory", modif. of buf[0] is not recognized !!!
*/
asm volatile
("int $0x80":"=a" (eax): "0" (102), "b" (10), "c" (&par):"memory");
if (eax == -11) return 0;
if (eax>=-4095) exit(0);
if (eax == 0) exit(0);
return eax;
}
/**************************************************************************/


|