3

I've been trying several things to emulate a serial mouse for the DoxBox emulator (in order to play a 2-player game in Settlers 2).

I haven't found any working solution yet, so I tried making my own. It seems that dosbox can use any /dev/ttyS* as a serial input. So I thought directly writing some bits to a FIFO to mascarade as a serial mouse should do the thing! Unfortunately it doesn't really work, as I'm obviously missing some steps (in particular initialisation, telling the driver that it is a mouse?), and I feel like I don't really understand the workings of serial ports.

I haven't found much on mouse protocol except the Microsoft serial mouse protocol. I tried outputting reasonable bytes. But nothing seems to really happen, and dosbox says it can't open the serial port.

Here's what I implemented:

def bits(byte):                                                                                                                                         
    b = []                                                                               
    for i in xrange(8):                                                                                                                                 
        b.append((byte >> i) & 1)                                                                                                                       
    return b                                                                                                                                            
def pack(*args):
    # packs the bits into a string                                                                                                                                        
    s = ""                                                                                                                                              
    for i in args:                                                                                                                                      
        v = 0                                                                                                                                           
        for bit in i:                                                                                                                                   
            v = v<<1                                                                                                                                    
            v+=bit                                                                                                                                      
        s+=chr(v)                                                                                                                                       
    return s                                                                                                                                            
def makebytes(ld,rd,dx,dy): 
    # left down, right down, delta x, delta y                                                                                   
    # create 3 byte message for mouse
    dx = bits(dx)                                                                                                                                       
    dy = bits(dy)                                                                                                                                       
    A = [0,1,                                                                                                                                           
         1 if ld else 0,                                                                                                                                
         1 if rd else 0,                                                                                                                                
         dx[7],dx[6],                                                                                                                                   
         dy[7],dy[6]]                                                                                                                                   
    B = [0,0]+dx[::-1][2:]                                                                                                                              
    C = [0,0]+dy[::-1][2:]                                                                                                                              
    return pack(A,B,C)                                                                                                                                  
# this is the FIFO I created with go+rw                                                                                                                                              
f = file("/dev/ttyS42",'w')                                                                                                                             
print "Got"                                                                                                                                          
import time                                                                                                                                             
while 1:                                                                                                                                                
    # send some mouse movement                                                                                                                                                    
    f.write(makebytes(0,0,10,10))                                                                                                                         
    print "sent 1"                                                                                                                                        
    time.sleep(0.5)                                                                                                                                     
    f.write(makebytes(0,0,-10,-10))                                                                                                                       
    print "sent 2"

So, I don't know where to look now, and any help would be appreciated.

2 Answers 2

4
+50

Looking at the DosBox source it seems you'll have trouble to use a FIFO because of how dosbox handles the serial port.

In src/hardware/serialport/libserial.cpp, line 295 you see how the serial device is opened in unices :

cp->porthandle = open (extended_portname, O_RDWR | O_NOCTTY | O_NONBLOCK);

In any way, using O_RDWR with a FIFO leads to an unspecified behavior (see open(3) about O_RDWR).

Moreover, even if the open() call succeeds, you will be blocked when the DosBox serial lib tries to get the terminal attributes of your device (line 298 of the same file)

result = tcgetattr(cp->porthandle,&cp->backup);

tcgetattr() will fail because it cannot retrieve termios data from a FIFO.

I guess a FIFO is not the way to go to emulate your device, you should either try to roll your own kernel module, modify dosbox or use an external adaptater to connect a physical second mouse on a serial port.

3

Instead of using a FIFO, you may want to try a Pseudo TTY

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.