Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

translating Linux keycode to Windows Raw keycode

1 view
Skip to first unread message

ericmatteson...@hotmail.com

unread,
Jun 21, 2008, 1:57:56 AM6/21/08
to
--------- ltowkey.c is next */
// reverse bits in each number in array 0-255;
// then translate Linux XWindows raw keycode
// to Microsoft Windows raw keycode.
// This ltowkey.c program is
// Copyright C 2008 by Eric Matteson
// Permission is hereby granted to
// copy this source code file
// ltowkey.c and to publish it on
// the Internet and to use it at least for
// non-profit use.
// To compile and run on Linux
// gcc ltowkey.c -o ltowkey.out
// ./ltowkey.out > result.txt
#include<stdio.h>
int lkey512[512];
int keyconv[256];
int keyl,keyw,keyvalue,skeyvalue;
char mframe[]={"0123456789"};
long posdigit(char dppcmp,char* ccglis)
{
long pozdigit;
int pozzctr,pozza;
pozdigit=47;
pozzctr=0;
pozza=1;
while(pozza > 0)
{
if(dppcmp == *(ccglis+pozzctr))
{
pozdigit=pozzctr;
pozza=0;
}
if(pozza > 0)
{
pozzctr=pozzctr+1;
if(pozzctr > 31)pozza=0;
}
}
if(pozdigit > 15)pozdigit=pozdigit-16;
return pozdigit;
}
long numberget(char* absi,long startx,
long widthx,long basex)
{
// use alselset(crz0160,linenumber) instead
// of simple pointer when
// reading number from a line in crz0160.
long nget,ngctr,ngdigit;
char ddcmp;
char nbglis[]="0123456789ABCDEF0123456789abcdef!";
nget=0;
ngctr=0;
// ***ERROR*** initially forgot to include startx
ngdaa: ddcmp = *(absi+(ngctr+startx-1));
ngctr ++;
ngdigit=posdigit(ddcmp,nbglis);
if(ngdigit<16)goto ngdac;
if(ngctr < widthx)goto ngdaa;
goto ngetbot;
ngdac: ngctr --;
ngdbb: ddcmp = *(absi+(ngctr+startx-1));
ngctr ++;
ngdigit=posdigit(ddcmp,nbglis);
if(ngdigit>15)goto ngetbot;
nget *= basex;
nget += ngdigit;
if(ngctr<widthx)goto ngdbb;
ngetbot: ngctr=0;
return nget;
}
int redcst(int rdspos,int rdswid,
int rdsbase,char* rdsjc)
{
int tedrst;
long ledrst,ldspos,ldswid,ldsbase;
ldspos=(long)rdspos;
ldswid=(long)rdswid;
ldsbase=(long)rdsbase;
ledrst=numberget(rdsjc,ldspos,ldswid,ldsbase);
tedrst=(int)ledrst;
return tedrst;
}
void ilinemake(int* iixt,char* xxdd)
{
int xsspos,xssmany,xssbase,xssctr,xsssub;
int xssterm,xtssub,xsstw;
// xsspos 999 xssbase-1 F
// xssmany 99 xsstw 9
// 9 f 5
xsspos=redcst(1,3,10,xxdd);
xssbase=redcst(4,1,16,xxdd);
xssmany=redcst(5,2,10,xxdd);
xssctr=0;
if((xssbase == 9)||(xssbase == 15))
{
xsstw = redcst(7,1,16,xxdd);
while(xssctr < xssmany)
{
xsssub = (xssctr * xsstw) + 8;
xtssub = xssctr + xsspos;
xssterm=redcst(xsssub,xsstw,(xssbase+1),xxdd);
*(iixt + xtssub) = xssterm;
xssctr=xssctr+1;
}
}
if(xssbase == 5)
{
while(xssctr < xssmany)
{
xsssub = xssctr + 6;
xtssub = xssctr + xsspos;
xssterm = (int)(*(xxdd + xsssub));
*(iixt + xtssub)=xssterm;
xssctr = xssctr+1;
}
}
}
void iktmany(int* imxt)
{
int iktmanyctr;
iktmanyctr=0;
while(iktmanyctr < 512)
{
*(imxt + iktmanyctr)=131065;
iktmanyctr=iktmanyctr+1;
}
// ----------------------------------------
ilinemake(imxt,"079F0423738392D");
ilinemake(imxt,"083F0423435362B");
ilinemake(imxt,"087F052313233302E");
ilinemake(imxt,"097F04324724824924B");
ilinemake(imxt,"102F06324D24F250251252253");
// ----------------------------------------
ilinemake(imxt,"063F0122A");
ilinemake(imxt,"112F0122F");
ilinemake(imxt,"050F01210");
ilinemake(imxt,"062901216");
ilinemake(imxt,"022F01208");
ilinemake(imxt,"009F0121B");
ilinemake(imxt,"023F01209");
// --
ilinemake(imxt,"0105101234567890");
ilinemake(imxt,"024510qwertyuiop");
ilinemake(imxt,"038509asdfghjkl");
ilinemake(imxt,"052507zxcvbnm");
// --
ilinemake(imxt,"0369013013");
ilinemake(imxt,"108F0120D");
ilinemake(imxt,"0659013032");
ilinemake(imxt,"020F0222D3D");
ilinemake(imxt,"051F0125C");
ilinemake(imxt,"034F0225B5D");
ilinemake(imxt,"047F0223B27");
ilinemake(imxt,"059F0322C2E2F");
ilinemake(imxt,"049F01260");
// ------------------------------------------
ilinemake(imxt,"305F0127E");
ilinemake(imxt,"2669103033064035036037094038042040041");
ilinemake(imxt,"280510QWERTYUIOP");
ilinemake(imxt,"294509ASDFGHJKL");
ilinemake(imxt,"308507ZXCVBNM");
// -------------
ilinemake(imxt,"276F0225F2B");
ilinemake(imxt,"307F0127C");
ilinemake(imxt,"290F0227B7D");
ilinemake(imxt,"303F0223A22");
ilinemake(imxt,"315F0323C3E3F");
// --
}
// LINUX KEYBOARD TRANSLATOR TOP
// lreadc redcst
// need xxkeyhold next
// iidoskey is Linux key translator
// for XWindows applications programs.
// result is similar to DOS key value.
int iidoskey(int kkll,int kshiftb,
int ixkeyhold[],const char* klinuxcm)
{
int klinuxmm,zandtogi;
int lidoskrs,lidoslr;
int wasdkeyused;
// x=120 v=118
char clinuxcm;
clinuxcm = *klinuxcm;
klinuxmm = (int)clinuxcm;
lidoskrs=131072;
wasdkeyused=0;
if(klinuxmm == 120)
{
// *klinuxcm == 'x' for xwindows key
// conversion
lidoslr = kkll;
if((lidoslr >= 0)&&(lidoslr < 256))
{
if(kshiftb == 1)lidoslr=kkll+256;
lidoskrs = ixkeyhold[lidoslr];
// add 512 to special keys so that they
// match Windows 98 special keys.
if(lidoskrs > 511)lidoskrs=lidoskrs+512;
wasdkeyused=1;
}
}
if(klinuxmm == 118)
{
// klinuxcm == "v" for int vga_getkey()
// within <vga.h>
zandtogi=1;
if(kkll < redcst(1,2,16,"00"))zandtogi=0;
if(kkll > redcst(1,2,16,"7E"))zandtogi=0;
if(zandtogi == 1)
{
lidoskrs=kkll;
}
if(kkll == redcst(1,2,16,"7F"))lidoskrs=8;
// -----------------------------------------
if(kkll==redcst(1,8,16,"1B5B317E"))lidoskrs=512+71;
if(kkll==redcst(1,8,16,"001B5B41"))lidoskrs=512+72;
if(kkll==redcst(1,8,16,"1B5B357E"))lidoskrs=512+73;
if(kkll==redcst(1,8,16,"001B5B44"))lidoskrs=512+75;
if(kkll==redcst(1,8,16,"001B5B43"))lidoskrs=512+77;
if(kkll==redcst(1,8,16,"1B5B347E"))lidoskrs=512+79;
if(kkll==redcst(1,8,16,"001B5B42"))lidoskrs=512+80;
if(kkll==redcst(1,8,16,"1B5B367E"))lidoskrs=512+81;
if(kkll==redcst(1,8,16,"1B5B327E"))lidoskrs=512+82;
if(kkll==redcst(1,8,16,"1B5B337E"))lidoskrs=512+83;
// ----------------------------------------
wasdkeyused=1;
}
if(wasdkeyused == 0)lidoskrs=klinuxmm;
return lidoskrs;
}
// end of int iidoskey(int,int,int[],const char*)
// --------------------------------------------
// isdoskey is translation from Windows 98
// raw keycode to key used in Windows 98
// applications program similar to DOS key
// value.
int isdoskey(int ipwarg,int snz)
{
// *lpwarg is virtual key code (NOT scan code)
// snz is zero for nonshifted.
int tu;
int r;
// tu=(int)*lpwarg;
tu=ipwarg;
r=0;
if((tu>64)&&(tu<91))
// if key is a letter key 65-90
{
r=tu;
// letter keys use warg is always uppercase
// version of that key;
if(snz==0)r=tu+32;
}
if((tu>47)&&(tu<58)&&(snz==0))r=tu;
// number keys are same as *lpwarg.
// Shifted keys require conversion ifs.
if((snz!=0)&&(tu==48))r=41;
if((snz!=0)&&(tu==49))r=33;
if((snz!=0)&&(tu==50))r=64;
if((snz!=0)&&(tu==51))r=35;
if((snz!=0)&&(tu==52))r=36;
if((snz!=0)&&(tu==53))r=37;
if((snz!=0)&&(tu==54))r=94;
if((snz!=0)&&(tu==55))r=38;
if((snz!=0)&&(tu==56))r=42;
if((snz==0)&&(tu==106))r=42;
if((snz!=0)&&(tu==57))r=40;
// *lpwarg is the same value for both shifted
// and non-shifted versions of each key
if((tu==192)&&(snz==0))r=96;
if((tu==192)&&(snz!=0))r=126;
if((tu==189)&&(snz==0))r=45;
if((tu==109)&&(snz==0))r=45;
if((tu==189)&&(snz!=0))r=95;
if((tu==187)&&(snz==0))r=61;
if((tu==187)&&(snz!=0))r=43;
if((tu==107)&&(snz==0))r=43;
if((tu==220)&&(snz==0))r=92;
if((tu==220)&&(snz!=0))r=124;
if(tu==27)r=27;
if(tu==8)r=8;
if(tu==13)r=13;
if(tu==32)r=32;
// if(tu==9)r=32;
// set tab key 9 to blank 32 to clean it up.
if((tu==219)&&(snz==0))r=91;
if((tu==219)&&(snz!=0))r=123;
if((tu==221)&&(snz==0))r=93;
if((tu==221)&&(snz!=0))r=125;
if((tu==186)&&(snz==0))r=59;
if((tu==186)&&(snz!=0))r=58;
if((tu==222)&&(snz==0))r=39;
if((tu==222)&&(snz!=0))r=34;
if((tu==188)&&(snz==0))r=44;
if((tu==188)&&(snz!=0))r=60;
if((tu==190)&&(snz==0))r=46;
if((tu==110)&&(snz==0))r=46;
if((tu==190)&&(snz!=0))r=62;
if((tu==191)&&(snz==0))r=47;
if((tu==111)&&(snz==0))r=47;
if((tu==191)&&(snz!=0))r=63;
if(snz==0)
{
// add 1024 to extended keys such as arrow
// and page up after converting.
if(tu==37)r=75+1024;
if(tu==39)r=77+1024;
if(tu==38)r=72+1024;
if(tu==40)r=80+1024;
if(tu==33)r=73+1024;
if(tu==34)r=81+1024;
if(tu==35)r=79+1024;
if(tu==36)r=71+1024;
if(tu==45)r=82+1024;
if(tu==46)r=83+1024;
}
if((tu>95)&&(tu<106)&&(snz==0))r=tu-48;
if((snz==0)&&(tu>111)&&(tu<122))r=(tu-53)+1024;
return r;
}
// end of isdoskey
int hmanyout(char* outres,int insign)
{
int intun,base,irem,iprod,frac,isgn;
int revctr,fwctr;
int dst[15];
revctr=10;
isgn=0;
base=10;
intun=insign;
if(insign < 0)
{
intun = 0 - insign;
isgn=1;
}
while(revctr >= 0)
{
frac = intun / base;
iprod = base * frac;
irem = intun - iprod;
if(irem < 0)
{
frac = frac - 1;
iprod = base * frac;
irem = intun - iprod;
}
dst[revctr] = irem;
intun = frac;
revctr = revctr - 1;
}
revctr=0;
irem=0;
while((irem == 0)&&(revctr < 9))
{
revctr=revctr+1;
irem=dst[revctr];
if(irem != 0)revctr=revctr-1;
}
// max revctr == 9
// rightmost digit at dst[10]
if(isgn == 1)
{
*(outres)='-';
}
while(revctr < 10)
{
irem=dst[revctr+1];
// mframe[] is list
// of character digits
*(outres + isgn)=mframe[irem];
isgn=isgn+1;
revctr=revctr+1;
}
if(insign != (0-1))
{
*(outres + isgn)=',';
isgn = isgn + 1;
}
if(insign == (0-1))
{
*(outres + isgn)='}';
*(outres + (isgn+1))=';';
isgn = isgn + 2;
}
*(outres + isgn)=(char)13;
*(outres + (isgn+1))=(char)10;
return isgn;
}
int revbits(int revin)
{
int revout,revctr,revterm;
revctr=0;
revout=0;
revterm=revin;
revloop: revctr=revctr+1;
revout = revout + revout;
revout = revout + (revterm & 1);
revterm = revterm >> 1;
if(revctr < 8)goto revloop;
return revout;
}
int main(int ui,char** uuc)
{
char tex[80];
int olop,ilop,tctr,twid,treb,xctr;
FILE* revhan;
revhan = stdout;
iktmany(lkey512);
xctr=0;
olop=0;
while(olop < 32)
{
tctr=0;
while(tctr < 80)
{
tex[tctr]=(char)32;
tctr=tctr+1;
}
twid=4 ;
if(olop == 0)
{
tex[3]='i';
tex[4]='n';
tex[5]='t';
// 6 is blank
tex[7]='t';
tex[8]='r';
tex[9]='e';
tex[10]='v';
tex[11]='[';
tex[12]=']';
tex[13]='=';
tex[14]='{';
// length = 15;
twid=15;
}
fwrite(tex,1,twid,revhan);
ilop=0;
while(ilop < 8)
{
treb=revbits(xctr);
if(treb > 127)treb=treb-256;
twid=hmanyout(tex,treb);
if(ilop == 7)twid = twid + 2;
xctr=xctr+1;
fwrite(tex,1,twid,revhan);
ilop=ilop+1;
}
olop=olop+1;
}
// begin matching of keycodes
keyl=0;
while(keyl < 256)
{
keyconv[keyl]=399;
keyl=keyl+1;
// initialize array elements to
// invalid key
}
keyl=0;
while(keyl < 256)
{
// keyl is Linux raw keycode
keyvalue = iidoskey(keyl,0,lkey512,"x");
skeyvalue = iidoskey(keyl,1,lkey512,"x");
keyw=0;
while(keyw < 256)
{
// keyw is Windows 98 raw keycode
if(keyvalue == isdoskey(keyw,0))
{
if(skeyvalue == isdoskey(keyw,1))
{
if((keyconv[keyl])>255)keyconv[keyl]=keyw;
// Subscript is Linux keycode.
// data value is sought Windows 98 keycode
}
}
keyw=keyw+1;
}
keyl=keyl+1;
}
keyl=0;
while(keyl < 256)
{
// repeating to catch duplicate keys that
// exist as unshifted only and need
// second pass.
// keyl is Linux XWindows keycode
keyvalue = iidoskey(keyl,0,lkey512,"x");
// keyvalue is translated value.
keyw=0;
while(keyw < 256)
{
// keyw is Windows 98 keycode
if(keyvalue == isdoskey(keyw,0))
{
if((keyconv[keyl]) > 255)keyconv[keyl]=keyw;
}
keyw=keyw+1;
}
keyl=keyl+1;
}
keyconv[50]=16;
keyconv[62]=16;
// Linux Xwindows uses 50 or 62 for
// shift key. Windows 98 uses 16
// for shift key.
keyconv[255]=0-1;
// end of key translation
xctr=0;
olop=0;
while(olop < 32)
{
tctr=0;
while(tctr < 80)
{
tex[tctr]=(char)32;
tctr=tctr+1;
}
twid=4 ;
if(olop == 0)
{
tex[3]='i';
tex[4]='n';
tex[5]='t';
// 6 is blank
tex[7]='w';
tex[8]='k';
tex[9]='e';
tex[10]='y';
tex[11]='[';
tex[12]=']';
tex[13]='=';
tex[14]='{';
// length = 15;
twid=15;
}
fwrite(tex,1,twid,revhan);
ilop=0;
while(ilop < 8)
{
treb=keyconv[xctr];
// treb=revbits(xctr);
// if(treb > 127)treb=treb-256;
twid=hmanyout(tex,treb);
if(ilop == 7)twid = twid + 2;
xctr=xctr+1;
fwrite(tex,1,twid,revhan);
ilop=ilop+1;
}
olop=olop+1;
}
return 0;
}
// end of ltowkey.c
/* after end of program


0 new messages