Skip to content

Commit

Permalink
* ext/curses/curses.c (cWindow, cMouseEvent): made typed data.
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34332 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Jan 18, 2012
1 parent 8c6e0d3 commit d3b540c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Wed Jan 18 18:06:45 2012 Nobuyoshi Nakada <[email protected]>

* ext/curses/curses.c (cWindow, cMouseEvent): made typed data.

Wed Jan 18 12:49:15 2012 Aaron Patterson <[email protected]>

* ext/psych/lib/psych/visitors/to_ruby.rb: Added support for loading
Expand Down
69 changes: 48 additions & 21 deletions ext/curses/curses.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,34 @@ no_window(void)
#define GetWINDOW(obj, winp) do {\
if (!OBJ_TAINTED(obj) && rb_safe_level() >= 4)\
rb_raise(rb_eSecurityError, "Insecure: operation on untainted window");\
Data_Get_Struct((obj), struct windata, (winp));\
TypedData_Get_Struct((obj), struct windata, &windata_type, (winp));\
if ((winp)->window == 0) no_window();\
} while (0)

static void
free_window(struct windata *winp)
window_free(void *p)
{
struct windata *winp = p;
if (winp->window && winp->window != stdscr) delwin(winp->window);
winp->window = 0;
xfree(winp);
}

static size_t
window_memsize(const void *p)
{
const struct windata *winp = p;
size_t size = sizeof(*winp);
if (!winp) return 0;
if (winp->window && winp->window != stdscr) size += sizeof(winp->window);
return size;
}

static const rb_data_type_t windata_type = {
"windata",
{0, window_free, window_memsize,}
};

static VALUE
prep_window(VALUE class, WINDOW *window)
{
Expand All @@ -110,7 +126,7 @@ prep_window(VALUE class, WINDOW *window)
}

obj = rb_obj_alloc(class);
Data_Get_Struct(obj, struct windata, winp);
TypedData_Get_Struct(obj, struct windata, &windata_type, winp);
winp->window = window;

return obj;
Expand Down Expand Up @@ -1241,17 +1257,33 @@ no_mevent(void)
#define GetMOUSE(obj, data) do {\
if (!OBJ_TAINTED(obj) && rb_safe_level() >= 4)\
rb_raise(rb_eSecurityError, "Insecure: operation on untainted mouse");\
Data_Get_Struct((obj), struct mousedata, (data));\
TypedData_Get_Struct((obj), struct mousedata, &mousedata_type, (data));\
if ((data)->mevent == 0) no_mevent();\
} while (0)

static void
curses_mousedata_free(struct mousedata *mdata)
curses_mousedata_free(void *p)
{
struct mousedata *mdata = p;
if (mdata->mevent)
xfree(mdata->mevent);
}

static size_t
curses_mousedata_memsize(const void *p)
{
const struct mousedata *mdata = p;
size_t size = sizeof(*mdata);
if (!mdata) return 0;
if (mdata->mevent) size += sizeof(mdata->mevent);
return size;
}

static const rb_data_type_t mousedata_type = {
"mousedata",
{0, curses_mousedata_free, curses_mousedata_memsize,}
};

/*
* Document-method: Curses.getmouse
*
Expand All @@ -1268,8 +1300,8 @@ curses_getmouse(VALUE obj)
VALUE val;

curses_stdscr();
val = Data_Make_Struct(cMouseEvent,struct mousedata,
0,curses_mousedata_free,mdata);
val = TypedData_Make_Struct(cMouseEvent,struct mousedata,
&mousedata_type,mdata);
mdata->mevent = (MEVENT*)xmalloc(sizeof(MEVENT));
return (getmouse(mdata->mevent) == OK) ? val : Qnil;
}
Expand Down Expand Up @@ -1438,7 +1470,7 @@ window_s_allocate(VALUE class)
{
struct windata *winp;

return Data_Make_Struct(class, struct windata, 0, free_window, winp);
return TypedData_Make_Struct(class, struct windata, &windata_type, winp);
}

/*
Expand All @@ -1460,7 +1492,7 @@ window_initialize(VALUE obj, VALUE h, VALUE w, VALUE top, VALUE left)

rb_secure(4);
curses_init_screen();
Data_Get_Struct(obj, struct windata, winp);
TypedData_Get_Struct(obj, struct windata, &windata_type, winp);
if (winp->window) delwin(winp->window);
window = newwin(NUM2INT(h), NUM2INT(w), NUM2INT(top), NUM2INT(left));
wclear(window);
Expand Down Expand Up @@ -2459,15 +2491,6 @@ window_timeout(VALUE obj, VALUE delay)
/*--------------------------- class Pad ----------------------------*/

#ifdef HAVE_NEWPAD
/* returns a Curses::Pad object */
static VALUE
pad_s_allocate(VALUE class)
{
struct windata *padp;

return Data_Make_Struct(class, struct windata, 0, free_window, padp);
}

/*
* Document-method: Curses::Pad.new
*
Expand All @@ -2486,7 +2509,7 @@ pad_initialize(VALUE obj, VALUE h, VALUE w)

rb_secure(4);
curses_init_screen();
Data_Get_Struct(obj, struct windata, padp);
TypedData_Get_Struct(obj, struct windata, &windata_type, padp);
if (padp->window) delwin(padp->window);
window = newpad(NUM2INT(h), NUM2INT(w));
wclear(window);
Expand All @@ -2495,8 +2518,11 @@ pad_initialize(VALUE obj, VALUE h, VALUE w)
return obj;
}

#if 1
#define pad_subpad window_subwin
#else
/*
* Document-method: Curses::Pad.subwin
* Document-method: Curses::Pad.subpad
* call-seq:
* subpad(height, width, begin_x, begin_y)
*
Expand All @@ -2522,6 +2548,7 @@ pad_subpad(VALUE obj, VALUE height, VALUE width, VALUE begin_x, VALUE begin_y)

return pad;
}
#endif

/*
* Document-method: Curses::Pad.refresh
Expand Down Expand Up @@ -2835,7 +2862,7 @@ Init_curses(void)
*
*/
cPad = rb_define_class_under(mCurses, "Pad", cWindow);
rb_define_alloc_func(cPad, pad_s_allocate);
/* inherits alloc_func from cWindow */
rb_define_method(cPad, "initialize", pad_initialize, 2);
rb_define_method(cPad, "subpad", pad_subpad, 4);
rb_define_method(cPad, "refresh", pad_refresh, 6);
Expand Down

0 comments on commit d3b540c

Please sign in to comment.