【问题标题】:swig xcb lib and rubyswig xcb lib 和 ruby
【发布时间】:2012-04-06 07:57:34
【问题描述】:

我想为 xcb 创建一个基本的 ruby​​ 模块供我自己使用。 我尝试了一个基本测试,我用 swig 包装头文件: 这是我的 xcb_ruby.i

%module ruxcby 
%{
#include <xcb/xcb.h>
#include <xcb/xcb_util.h>
#include <xcb/xcb_aux.h
#include <xcb/xcb_atom.h>
#include <xcb/xcb_event.h>
%}
%include "/usr/include/xcb/xproto.h"
%include "/usr/include/xcb/xcb.h"
%include "/usr/include/xcb/xcb_atom.h"
%include "/usr/include/xcb/xcb_aux.h"
%include "/usr/include/xcb/xcb_event.h"

当我在 irb 中尝试时,我能够连接以初始化连接,从这个连接中获取信息,但是 ruby​​ 接口就像在 C 中一样。我想要一个更面向对象的接口。

例如在 xcb.h 中有:

typedef struct xcb_connection_t xcb_connection_t;
xcb_connection_t * xcb_connect(const char *displayname, int *screenp);
int xcb_flush( xcb_connection_t *);
uint32_t xcb_generate_id( xcb_connection_t *);
void xcb_disconnect(xcb_connection_t * );

我想要一个 Connection 类,其中包含 new/connect()、flush()、generate_id() 和 disconnect() 方法 这是我的新 xcb_ruby.i:

%module ruxcby
%{
#include <xcb/xcb.h>
#include <xcb/xproto.h>
%}
%import "/usr/include/xcb/xproto.h"

typedef struct xcb_connection_t {
} Connection;

%extend Connection 
{
  Connection(const char *displayname, int *screenp)
  {
    Connection * c;
    c = xcb_connect(displayname, screenp);
    return c;
  }
  int flush()
  {
    return xcb_flush($self);
  }
  xcb_generic_event_t *wait_for_event()
  {
    return xcb_wait_for_event($self);
  }
  void disconnect()
  {
    return xcb_disconnect($self);
  }
  uint32_t generate_id()
  {
    return xcb_generate_id($self);
  }
};

如果我在生成 c 文件后尝试编译,则会出现错误: 错误:未知类型名称“连接”

谁能告诉我哪里错了?

谢谢

编辑

我做了一些修改,现在我可以编译它了,但我仍然得到一些错误:

%module ruxcby
%{
#include <xcb/xcb.h>
#include <xcb/xproto.h>
typedef struct {
  xcb_connection_t * ptr;
} Connection;
%}
%import "/usr/include/xcb/xproto.h"
%feature("autodoc" , "1");

typedef struct {
  xcb_connection_t * ptr;
} Connection;
%extend Connection {
  Connection(const char *displayname, int *screenp)
 {
    Connection * c ;
    c->ptr = xcb_connect(displayname, screenp);
    return c;
 }
 int flush()
 {  
    return xcb_flush($self->ptr);
 }
 xcb_generic_event_t *wait_for_event()
 {
    return xcb_wait_for_event($self->ptr);
 }
 int connection_has_error()
 {
    return xcb_connection_has_error($self->ptr);
 }
 void disconnect()
 {
   return xcb_disconnect($self->ptr);
 }
 uint32_t generate_id()
 {
    return xcb_generate_id($self->ptr);
 }
};

现在我可以编译我的模块并使用它了:

require './ruxcby'
=> true
conn=Connection.new(nil, nil)
=> #<Ruxcby::Connection:0x0000000223dfc8>

但是当我尝试另一种方法时出现错误:

conn.connection_has_error 
ObjectPreviouslyDeleted: Expected argument 0 of type Connection *, but got Ruxcby::Connection #<Ruxcby::

该方法似乎存在,但在将参数传递给该方法时仍然存在问题。

有什么想法吗??

【问题讨论】:

    标签: ruby swig xcb


    【解决方案1】:

    我提出的解决方案也许可以帮助某人: 当我扩展 Connection 类时,我没有为我的结构分配内存:

    %extend Connection {
    Connection(const char *displayname, int *screenp)
    {
      Connection * c ;
      c = (Connection * ) malloc(sizeof(Connection));
      c->ptr = xcb_connect(displayname, screenp);
      return c;
    }
    ~Connection()
    {
      free($self);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-05
      • 2012-06-19
      • 1970-01-01
      相关资源
      最近更新 更多