trainman419 / python-cec

Other
171 stars 42 forks source link

Use the librarys in built SendKeyPress function #36

Open sparky3387 opened 5 years ago

sparky3387 commented 5 years ago

Im trying to modify your library so it supports the inbuilt sendkeypress (and sendkeyrelease), but am having issues, here is what I have come up with so far (it appears to work, but nothing happens and it crashes with a stack smash):

static PyObject * Device_send_key_press(Device * self, PyObject * args) {
   unsigned char keycode;
   bool bWait;
   cec_user_control_code keypress;
   if( PyArg_ParseTuple(args, "bp:sendkeypress", &keycode,
         &bWait) ) {
      keypress = (cec_user_control_code) keycode;
      bool success;
      Py_BEGIN_ALLOW_THREADS
      cec_logical_address destination = self->addr;
      success = adapter->SendKeypress(destination, keypress, bWait);
      Py_END_ALLOW_THREADS
      if( success ) {
         Py_RETURN_TRUE;
      } else {
         Py_RETURN_FALSE;
      }
   } else {
      return NULL;
   }
}
nforro commented 5 years ago

I don't see anything obviously wrong with your code, if you want help with the crash, you have to provide more info, ideally complete backtrace, and also the python code you are using.

BTW, you can do the same with this code:

opcode = cec.CEC_OPCODE_USER_CONTROL_PRESSED
parameters = bytes([keycode])
device.transmit(opcode, parameters)

You will lose the bWait argument, but in case of CEC_OPCODE_USER_CONTROL_PRESSED it's noop anyway.