Class: Teek::Interp
- Inherits:
-
Object
- Object
- Teek::Interp
- Defined in:
- ext/teek/tcltkbridge.c
Class Method Summary collapse
-
.instance_count ⇒ Object
--------------------------------------------------------- TclTkIp.instance_count - Number of live interpreter instances ---------------------------------------------------------.
-
.instances ⇒ Object
--------------------------------------------------------- TclTkIp.instances - Array of live interpreter instances ---------------------------------------------------------.
Instance Method Summary collapse
-
#callback_ids ⇒ Object
--------------------------------------------------------- Interp#callback_ids - Currently registered callback id strings (test/introspection use: asserting exactly which ids survive a release, not just how many) ---------------------------------------------------------.
-
#coords_to_window(root_x, root_y) ⇒ Object
--------------------------------------------------------- Interp#coords_to_window(root_x, root_y).
-
#create_console ⇒ Object
--------------------------------------------------------- Interp#create_console - Create Tk console window.
-
#create_slave(*args) ⇒ Object
--------------------------------------------------------- Interp#create_slave(name, safe=false) - Create child interpreter.
-
#delete ⇒ Object
--------------------------------------------------------- Interp#delete - Explicitly delete interpreter ---------------------------------------------------------.
-
#deleted? ⇒ Boolean
--------------------------------------------------------- Interp#deleted? - Check if interpreter was deleted ---------------------------------------------------------.
-
#do_one_event(*args) ⇒ Object
--------------------------------------------------------- Interp#do_one_event(flags = ALL_EVENTS) - Process single event.
-
#font_metrics(font_name) ⇒ Object
--------------------------------------------------------- Interp#font_metrics(font_name).
-
#get_root_coords(window_path) ⇒ Object
--------------------------------------------------------- Interp#get_root_coords(window_path).
-
#initialize(*args) ⇒ Object
constructor
--------------------------------------------------------- Interp#initialize(name=nil, opts={}) - Create Tcl interp and load Tk.
- #mainloop ⇒ Object
-
#measure_chars(*args) ⇒ Object
--------------------------------------------------------- Interp#measure_chars(font_name, text, max_pixels, opts={}).
-
#native_window_handle(window_path) ⇒ Object
--------------------------------------------------------- Interp#native_window_handle(window_path).
-
#on_main_thread? ⇒ Boolean
Check if current thread is the main Tcl thread.
-
#photo_blank(photo_path) ⇒ Object
--------------------------------------------------------- Interp#photo_blank(photo_path).
-
#photo_expand(photo_path, width_val, height_val) ⇒ Object
--------------------------------------------------------- Interp#photo_expand(photo_path, width, height).
-
#photo_get_image(*args) ⇒ Object
--------------------------------------------------------- Interp#photo_get_image(photo_path, opts={}).
-
#photo_get_pixel(photo_path, x_val, y_val) ⇒ Object
--------------------------------------------------------- Interp#photo_get_pixel(photo_path, x, y).
-
#photo_get_size(photo_path) ⇒ Object
--------------------------------------------------------- Interp#photo_get_size(photo_path).
-
#photo_put_zoomed_block(*args) ⇒ Object
--------------------------------------------------------- Interp#photo_put_zoomed_block(photo_path, pixel_data, width, height, opts={}).
-
#photo_set_size(photo_path, width_val, height_val) ⇒ Object
--------------------------------------------------------- Interp#photo_set_size(photo_path, width, height).
-
#queue_for_main(proc) ⇒ Object
Queue a proc to run on the main Tcl thread (fire-and-forget).
-
#register_callback(proc) ⇒ Object
--------------------------------------------------------- Interp#register_callback(proc) - Store proc, return ID ---------------------------------------------------------.
-
#safe? ⇒ Boolean
--------------------------------------------------------- Interp#safe? - Check if interpreter is running in safe mode.
-
#tcl_eval(script) ⇒ Object
--------------------------------------------------------- Interp#tcl_eval(script) - Evaluate Tcl script string.
-
#tcl_get_var(name) ⇒ Object
--------------------------------------------------------- Interp#tcl_get_var(name) - Get Tcl variable value ---------------------------------------------------------.
-
#tcl_invoke(*args) ⇒ Object
--------------------------------------------------------- Interp#tcl_invoke(*args) - Invoke Tcl command with args.
-
#tcl_set_var(name, value) ⇒ Object
--------------------------------------------------------- Interp#tcl_set_var(name, value) - Set Tcl variable ---------------------------------------------------------.
-
#tcl_version ⇒ Object
--------------------------------------------------------- Interp#tcl_version / #tk_version - Get version strings ---------------------------------------------------------.
-
#thread_timer_ms ⇒ Object
--------------------------------------------------------- Interp#thread_timer_ms / #thread_timer_ms= - Get/set timer interval ---------------------------------------------------------.
- #thread_timer_ms=(val) ⇒ Object
- #tk_version ⇒ Object
-
#unregister_callback(id) ⇒ Object
--------------------------------------------------------- Interp#unregister_callback(id) - Remove proc by ID ---------------------------------------------------------.
-
#window_geometry(window_path) ⇒ Object
--------------------------------------------------------- Interp#window_geometry(window_path).
Constructor Details
#initialize(*args) ⇒ Object
Interp#initialize(name=nil, opts={}) - Create Tcl interp and load Tk
Arguments:
name - Ignored (legacy compatibility)
opts - Options hash
Options:
:thread_timer_ms - Timer interval for thread-aware mainloop (default: 5)
Controls how often Ruby threads get a chance to run
during Tk.mainloop.
Tradeoffs:
- 1ms: Very responsive threads, higher CPU when idle
- 5ms: Good balance (default)
- 10ms: Lower CPU, slight thread latency
- 20ms: Minimal CPU, noticeable latency for threads
- 0: Disable timer (threads won't run during mainloop)
Initialization order (verified empirically on Tcl/Tk 9.0.3):
- Tcl_FindExecutable - sets up internal paths (NOT stubbed)
- Tcl_CreateInterp - create interpreter (NOT stubbed)
- Tcl_InitStubs - bootstrap stubs table
- Set argc/argv/argv0 - Tk_Init reads these
- Tcl_Init - load Tcl runtime
- Tk_Init - load Tk runtime (NOT stubbed - must come BEFORE Tk_InitStubs!)
- Tk_InitStubs - bootstrap Tk stubs table (AFTER Tk_Init)
CRITICAL: Tk_Init before Tk_InitStubs. Tk_InitStubs internally calls Tk_Init if not already done, causing "window already exists" error if you then call Tk_Init yourself.
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'ext/teek/tcltkbridge.c', line 307
static VALUE
interp_initialize(int argc, VALUE *argv, VALUE self)
{
struct tcltk_interp *tip;
const char *tcl_version;
const char *tk_version;
VALUE name, opts, val;
TypedData_Get_Struct(self, struct tcltk_interp, &interp_type, tip);
/* Parse legacy (name, opts) or new (opts) argument forms */
rb_scan_args(argc, argv, "02", &name, &opts);
/* name is ignored - kept for legacy compatibility */
/* Check for options in opts hash */
if (!NIL_P(opts) && TYPE(opts) == T_HASH) {
val = rb_hash_aref(opts, ID2SYM(rb_intern("thread_timer_ms")));
if (!NIL_P(val)) {
int ms = NUM2INT(val);
if (ms < 0) {
rb_raise(rb_eArgError, "thread_timer_ms must be >= 0 (got %d)", ms);
}
tip->timer_interval_ms = ms;
}
}
/* 1. Tell Tcl where to find itself (once per process) */
if (!tcl_stubs_initialized) {
find_executable_bootstrap("ruby");
}
/* 2. Create Tcl interpreter (using bootstrap to handle Tcl 8.6) */
tip->interp = create_interp_bootstrap();
if (tip->interp == NULL) {
rb_raise(eTclError, "failed to create Tcl interpreter");
}
/* 3. Initialize Tcl stubs - MUST be before any other Tcl calls */
tcl_version = Tcl_InitStubs(tip->interp, TCL_VERSION, 0);
if (tcl_version == NULL) {
const char *err = Tcl_GetStringResult(tip->interp);
Tcl_DeleteInterp(tip->interp);
tip->interp = NULL;
rb_raise(eTclError, "Tcl_InitStubs failed: %s", err);
}
/* 4. Set up argc/argv/argv0 before Tcl_Init (required for proper init) */
Tcl_Eval(tip->interp, "set argc 0; set argv {}; set argv0 tcltkbridge");
/* 5. Initialize Tcl runtime */
if (Tcl_Init(tip->interp) != TCL_OK) {
const char *err = Tcl_GetStringResult(tip->interp);
Tcl_DeleteInterp(tip->interp);
tip->interp = NULL;
rb_raise(eTclError, "Tcl_Init failed: %s", err);
}
/* 6. Initialize Tk runtime - must come BEFORE Tk_InitStubs */
if (Tk_Init(tip->interp) != TCL_OK) {
const char *err = Tcl_GetStringResult(tip->interp);
Tcl_DeleteInterp(tip->interp);
tip->interp = NULL;
rb_raise(eTclError, "Tk_Init failed: %s", err);
}
/* Hide the Tk console if it was auto-created during Tk_Init.
* On macOS/Windows, Tk may create a console window depending on
* how the process was launched. "catch" handles Linux where the
* console command does not exist. */
Tcl_Eval(tip->interp, "catch {console hide}");
/* 7. Initialize Tk stubs - after Tk_Init */
tk_version = Tk_InitStubs(tip->interp, TK_VERSION, 0);
if (tk_version == NULL) {
const char *err = Tcl_GetStringResult(tip->interp);
Tcl_DeleteInterp(tip->interp);
tip->interp = NULL;
rb_raise(eTclError, "Tk_InitStubs failed: %s", err);
}
tcl_stubs_initialized = 1;
/* 8. Register Tcl commands for Ruby integration */
Tcl_CreateObjCommand(tip->interp, "ruby_callback",
ruby_callback_proc, (ClientData)tip, NULL);
Tcl_CreateObjCommand(tip->interp, "ruby",
ruby_eval_proc, (ClientData)tip, NULL);
Tcl_CreateObjCommand(tip->interp, "ruby_eval",
ruby_eval_proc, (ClientData)tip, NULL);
/* 9. Register callback for when Tcl deletes this interpreter */
Tcl_CallWhenDeleted(tip->interp, interp_deleted_callback, (ClientData)tip);
/* 10. Track this instance for multi-interp safety checks */
rb_ary_push(live_instances, self);
/* 11. Store the main thread ID for cross-thread event queuing */
tip->main_thread_id = Tcl_GetCurrentThread();
return self;
}
|
Class Method Details
.instance_count ⇒ Object
TclTkIp.instance_count - Number of live interpreter instances
1385 1386 1387 1388 1389 |
# File 'ext/teek/tcltkbridge.c', line 1385
static VALUE
tcltkip_instance_count(VALUE klass)
{
return LONG2NUM(RARRAY_LEN(live_instances));
}
|
.instances ⇒ Object
TclTkIp.instances - Array of live interpreter instances
1395 1396 1397 1398 1399 |
# File 'ext/teek/tcltkbridge.c', line 1395
static VALUE
tcltkip_instances(VALUE klass)
{
return rb_ary_dup(live_instances);
}
|
Instance Method Details
#callback_ids ⇒ Object
Interp#callback_ids - Currently registered callback id strings (test/introspection use: asserting exactly which ids survive a release, not just how many)
593 594 595 596 597 598 |
# File 'ext/teek/tcltkbridge.c', line 593
static VALUE
interp_callback_ids(VALUE self)
{
struct tcltk_interp *tip = get_interp(self);
return rb_funcall(tip->callbacks, rb_intern("keys"), 0);
}
|
#coords_to_window(root_x, root_y) ⇒ Object
Interp#coords_to_window(root_x, root_y)
Find which window contains the given screen coordinates (hit testing).
Arguments:
root_x - X coordinate in root window (screen) coordinates
root_y - Y coordinate in root window (screen) coordinates
Returns window path string, or nil if no Tk window at that location.
See: https://manpages.ubuntu.com/manpages/kinetic/man3/Tk_CoordsToWindow.3tk.html
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'ext/teek/tkwin.c', line 159
static VALUE
interp_coords_to_window(VALUE self, VALUE root_x, VALUE root_y)
{
struct tcltk_interp *tip = get_interp(self);
Tk_Window mainWin;
Tk_Window foundWin;
const char *pathName;
/* Get the main window for application reference */
mainWin = Tk_MainWindow(tip->interp);
if (!mainWin) {
rb_raise(eTclError, "Tk not initialized (no main window)");
}
/* Find window at coordinates */
foundWin = Tk_CoordsToWindow(NUM2INT(root_x), NUM2INT(root_y), mainWin);
if (!foundWin) {
return Qnil;
}
/* Get window path name */
pathName = Tk_PathName(foundWin);
if (!pathName) {
return Qnil;
}
return rb_utf8_str_new_cstr(pathName);
}
|
#create_console ⇒ Object
Interp#create_console - Create Tk console window
Creates a console window for platforms without a real terminal. See: https://www.tcl-lang.org/man/tcl8.6/TkLib/CrtConsoleChan.htm
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 |
# File 'ext/teek/tcltkbridge.c', line 1452
static VALUE
interp_create_console(VALUE self)
{
struct tcltk_interp *tip = get_interp(self);
/*
* tcl_interactive is normally set by tclsh/wish at startup.
* When embedding Tcl in Ruby, we must set it ourselves.
* console.tcl checks this to decide whether to show the console window:
* if 0, the window starts hidden (wm withdraw); if 1, it's shown.
* See: https://github.com/tcltk/tk/blob/main/library/console.tcl#L144
*/
if (Tcl_GetVar(tip->interp, "tcl_interactive", TCL_GLOBAL_ONLY) == NULL) {
Tcl_SetVar(tip->interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
}
Tk_InitConsoleChannels(tip->interp);
if (Tk_CreateConsoleWindow(tip->interp) != TCL_OK) {
rb_raise(eTclError, "failed to create console window: %s",
Tcl_GetStringResult(tip->interp));
}
return Qtrue;
}
|
#create_slave(*args) ⇒ Object
Interp#create_slave(name, safe=false) - Create child interpreter
Creates a Tcl slave interpreter with the given name. If safe is true, the slave runs in safe mode (restricted commands).
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 |
# File 'ext/teek/tcltkbridge.c', line 1334
static VALUE
interp_create_slave(int argc, VALUE *argv, VALUE self)
{
struct tcltk_interp *master = get_interp(self);
struct tcltk_interp *slave;
VALUE name, safemode, new_ip;
int safe;
Tcl_Interp *slave_interp;
rb_scan_args(argc, argv, "11", &name, &safemode);
StringValue(name);
safe = RTEST(safemode) ? 1 : 0;
/* Create the slave interpreter */
slave_interp = Tcl_CreateSlave(master->interp, StringValueCStr(name), safe);
if (slave_interp == NULL) {
rb_raise(eTclError, "failed to create slave interpreter");
}
/* Wrap in a new TclTkIp Ruby object */
new_ip = TypedData_Make_Struct(cInterp, struct tcltk_interp,
&interp_type, slave);
slave->interp = slave_interp;
slave->deleted = 0;
slave->callbacks = rb_hash_new();
slave->thread_queue = rb_ary_new();
slave->next_id = 1;
slave->timer_interval_ms = DEFAULT_TIMER_INTERVAL_MS;
slave->main_thread_id = Tcl_GetCurrentThread();
/* Register Ruby integration commands in the slave */
Tcl_CreateObjCommand(slave->interp, "ruby_callback",
ruby_callback_proc, (ClientData)slave, NULL);
Tcl_CreateObjCommand(slave->interp, "ruby",
ruby_eval_proc, (ClientData)slave, NULL);
Tcl_CreateObjCommand(slave->interp, "ruby_eval",
ruby_eval_proc, (ClientData)slave, NULL);
/* Register callback for when Tcl deletes this interpreter */
Tcl_CallWhenDeleted(slave->interp, interp_deleted_callback, (ClientData)slave);
/* Track this instance */
rb_ary_push(live_instances, new_ip);
return new_ip;
}
|
#delete ⇒ Object
Interp#delete - Explicitly delete interpreter
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 |
# File 'ext/teek/tcltkbridge.c', line 1063
static VALUE
interp_delete(VALUE self)
{
struct tcltk_interp *tip;
TypedData_Get_Struct(self, struct tcltk_interp, &interp_type, tip);
if (tip->interp && !tip->deleted) {
Tcl_DeleteInterp(tip->interp);
tip->deleted = 1;
/* Remove from live instances tracking */
rb_ary_delete(live_instances, self);
}
return Qnil;
}
|
#deleted? ⇒ Boolean
Interp#deleted? - Check if interpreter was deleted
1035 1036 1037 1038 1039 1040 1041 |
# File 'ext/teek/tcltkbridge.c', line 1035
static VALUE
interp_deleted_p(VALUE self)
{
struct tcltk_interp *tip;
TypedData_Get_Struct(self, struct tcltk_interp, &interp_type, tip);
return (tip->deleted || tip->interp == NULL) ? Qtrue : Qfalse;
}
|
#do_one_event(*args) ⇒ Object
Interp#do_one_event(flags = ALL_EVENTS) - Process single event
Returns true if event was processed, false if nothing to do.
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 |
# File 'ext/teek/tcltkbridge.c', line 1015
static VALUE
interp_do_one_event(int argc, VALUE *argv, VALUE self)
{
int flags = TCL_ALL_EVENTS;
int result;
/* Optional flags argument */
if (argc > 0) {
flags = NUM2INT(argv[0]);
}
result = Tcl_DoOneEvent(flags);
return result ? Qtrue : Qfalse;
}
|
#font_metrics(font_name) ⇒ Object
Interp#font_metrics(font_name)
Get font metrics using Tk_GetFontMetrics. Faster than querying via Tcl font metrics command.
Arguments:
font_name - Font description string (e.g., "Helvetica 12", "TkDefaultFont")
Returns Hash with:
:ascent - Pixels from baseline to top of highest character
:descent - Pixels from baseline to bottom of lowest character
:linespace - Total line height (ascent + descent)
See: https://www.tcl-lang.org/man/tcl9.0/TkLib/FontId.html
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'ext/teek/tkfont.c', line 79
static VALUE
interp_font_metrics(VALUE self, VALUE font_name)
{
struct tcltk_interp *tip = get_interp(self);
Tk_Window mainWin;
Tk_Font tkfont;
Tk_FontMetrics fm;
const char *font_str;
VALUE result;
StringValue(font_name);
font_str = StringValueCStr(font_name);
/* Get the main window for font allocation */
mainWin = Tk_MainWindow(tip->interp);
if (!mainWin) {
rb_raise(eTclError, "Tk not initialized (no main window)");
}
/* Get the font */
tkfont = Tk_GetFont(tip->interp, mainWin, font_str);
if (!tkfont) {
rb_raise(eTclError, "font not found: %s - %s",
font_str, Tcl_GetStringResult(tip->interp));
}
/* Get font metrics */
Tk_GetFontMetrics(tkfont, &fm);
/* Build result hash */
result = rb_hash_new();
rb_hash_aset(result, ID2SYM(rb_intern("ascent")), INT2NUM(fm.ascent));
rb_hash_aset(result, ID2SYM(rb_intern("descent")), INT2NUM(fm.descent));
rb_hash_aset(result, ID2SYM(rb_intern("linespace")), INT2NUM(fm.linespace));
/* Release the font */
Tk_FreeFont(tkfont);
return result;
}
|
#get_root_coords(window_path) ⇒ Object
Interp#get_root_coords(window_path)
Get absolute screen coordinates of a window's upper-left corner.
Arguments:
window_path - Tk window path (e.g., ".", ".frame.button")
Returns [x, y] array of root window coordinates.
See: https://www.tcl-lang.org/man/tcl9.0/TkLib/GetRootCrd.html
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'ext/teek/tkwin.c', line 76
static VALUE
interp_get_root_coords(VALUE self, VALUE window_path)
{
struct tcltk_interp *tip = get_interp(self);
Tk_Window mainWin;
Tk_Window tkwin;
int x, y;
StringValue(window_path);
/* Get the main window for hierarchy reference */
mainWin = Tk_MainWindow(tip->interp);
if (!mainWin) {
rb_raise(eTclError, "Tk not initialized (no main window)");
}
/* Find the target window by path */
tkwin = Tk_NameToWindow(tip->interp, StringValueCStr(window_path), mainWin);
if (!tkwin) {
rb_raise(eTclError, "window not found: %s", StringValueCStr(window_path));
}
/* Get root coordinates */
Tk_GetRootCoords(tkwin, &x, &y);
return rb_ary_new_from_args(2, INT2NUM(x), INT2NUM(y));
}
|
#mainloop ⇒ Object
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 |
# File 'ext/teek/tcltkbridge.c', line 1131
static VALUE
interp_mainloop(VALUE self)
{
struct tcltk_interp *tip = get_interp(self);
/* Start recurring timer if interval > 0 */
if (tip->timer_interval_ms > 0) {
Tcl_CreateTimerHandler(tip->timer_interval_ms, keepalive_timer_proc, (ClientData)tip);
}
while (Tk_GetNumMainWindows() > 0) {
/* Process one event (timer ensures this returns periodically) */
Tcl_DoOneEvent(TCL_ALL_EVENTS);
/* Yield to other Ruby threads by releasing and reacquiring GVL */
if (tip->timer_interval_ms > 0) {
rb_thread_call_without_gvl(thread_yield_func, NULL, RUBY_UBF_IO, NULL);
}
/* Check for Ruby interrupts (Ctrl-C, etc) */
rb_thread_check_ints();
}
return Qnil;
}
|
#measure_chars(*args) ⇒ Object
Interp#measure_chars(font_name, text, max_pixels, opts={})
Measure how many characters/bytes of text fit within a pixel width limit. Useful for text truncation, ellipsis, and line wrapping.
Arguments:
font_name - Font description string (e.g., "Helvetica 12")
text - Text string to measure
max_pixels - Maximum pixel width allowed (-1 for unlimited)
opts - Optional hash:
:partial_ok - Allow partial character at boundary (default: false)
:whole_words - Break only at word boundaries (default: false)
:at_least_one - Always return at least one character (default: false)
Returns Hash with:
:bytes - Number of bytes that fit within max_pixels
:width - Actual pixel width of those bytes
See: https://www.tcl-lang.org/man/tcl9.0/TkLib/MeasureChar.html
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'ext/teek/tkfont.c', line 142
static VALUE
interp_measure_chars(int argc, VALUE *argv, VALUE self)
{
struct tcltk_interp *tip = get_interp(self);
VALUE font_name, text, max_pixels_val, opts;
Tk_Window mainWin;
Tk_Font tkfont;
const char *font_str;
const char *text_str;
int max_pixels;
int flags;
int length;
int num_bytes;
VALUE result;
rb_scan_args(argc, argv, "31", &font_name, &text, &max_pixels_val, &opts);
StringValue(font_name);
StringValue(text);
font_str = StringValueCStr(font_name);
text_str = StringValueCStr(text);
max_pixels = NUM2INT(max_pixels_val);
/* Parse flags from options */
flags = 0;
if (!NIL_P(opts) && TYPE(opts) == T_HASH) {
VALUE val;
val = rb_hash_aref(opts, ID2SYM(rb_intern("partial_ok")));
if (RTEST(val)) flags |= TK_PARTIAL_OK;
val = rb_hash_aref(opts, ID2SYM(rb_intern("whole_words")));
if (RTEST(val)) flags |= TK_WHOLE_WORDS;
val = rb_hash_aref(opts, ID2SYM(rb_intern("at_least_one")));
if (RTEST(val)) flags |= TK_AT_LEAST_ONE;
}
/* Get the main window for font allocation */
mainWin = Tk_MainWindow(tip->interp);
if (!mainWin) {
rb_raise(eTclError, "Tk not initialized (no main window)");
}
/* Get the font */
tkfont = Tk_GetFont(tip->interp, mainWin, font_str);
if (!tkfont) {
rb_raise(eTclError, "font not found: %s - %s",
font_str, Tcl_GetStringResult(tip->interp));
}
/* Measure characters */
num_bytes = Tk_MeasureChars(tkfont, text_str, (int)strlen(text_str),
max_pixels, flags, &length);
/* Release the font */
Tk_FreeFont(tkfont);
/* Build result hash */
result = rb_hash_new();
rb_hash_aset(result, ID2SYM(rb_intern("bytes")), INT2NUM(num_bytes));
rb_hash_aset(result, ID2SYM(rb_intern("width")), INT2NUM(length));
return result;
}
|
#native_window_handle(window_path) ⇒ Object
Interp#native_window_handle(window_path)
Returns the platform-native window handle for SDL2 embedding.
macOS: NSWindow* (via Tk_MacOSXGetNSWindowForDrawable)
X11: X Window ID (via Tk_WindowId)
Windows: HWND (via Tk_GetHWND)
The return value is an Integer suitable for passing to SDL_CreateWindowFrom. On macOS this is a pointer to an NSWindow object; on X11/Windows it's a window identifier.
The window must be mapped (visible) before calling this.
Use update_idletasks first to ensure geometry is committed.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'ext/teek/tkwin.c', line 205
static VALUE
interp_native_window_handle(VALUE self, VALUE window_path)
{
struct tcltk_interp *tip = get_interp(self);
Tk_Window mainWin;
Tk_Window tkwin;
Drawable drawable;
StringValue(window_path);
mainWin = Tk_MainWindow(tip->interp);
if (!mainWin) {
rb_raise(eTclError, "Tk not initialized (no main window)");
}
tkwin = Tk_NameToWindow(tip->interp, StringValueCStr(window_path), mainWin);
if (!tkwin) {
rb_raise(eTclError, "window not found: %s", StringValueCStr(window_path));
}
/* Force the window to be mapped so we get a valid native handle */
Tk_MakeWindowExist(tkwin);
drawable = Tk_WindowId(tkwin);
if (!drawable) {
rb_raise(eTclError, "window has no native handle (not mapped?): %s",
StringValueCStr(window_path));
}
#ifdef __APPLE__
{
/* macOS: convert Tk drawable to NSWindow* for SDL2 */
void *nswindow = Tk_MacOSXGetNSWindowForDrawable(drawable);
if (!nswindow) {
rb_raise(eTclError, "could not get NSWindow for: %s",
StringValueCStr(window_path));
}
return ULL2NUM((uintptr_t)nswindow);
}
#elif defined(_WIN32) || defined(__CYGWIN__)
{
/* Windows: convert to HWND */
HWND hwnd = Tk_GetHWND(drawable);
return ULL2NUM((uintptr_t)hwnd);
}
#else
/* X11: Drawable is already the X Window ID */
return ULL2NUM((uintptr_t)drawable);
#endif
}
|
#on_main_thread? ⇒ Boolean
Check if current thread is the main Tcl thread
847 848 849 850 851 852 853 |
# File 'ext/teek/tcltkbridge.c', line 847
static VALUE
interp_on_main_thread_p(VALUE self)
{
struct tcltk_interp *tip = get_interp(self);
Tcl_ThreadId current = Tcl_GetCurrentThread();
return (current == tip->main_thread_id) ? Qtrue : Qfalse;
}
|
#photo_blank(photo_path) ⇒ Object
Interp#photo_blank(photo_path)
Clear a photo image to transparent using Tk_PhotoBlank. Faster than setting all pixels manually.
Arguments:
photo_path - Tcl path of the photo image (e.g., "i00001")
Returns nil.
See: https://www.tcl-lang.org/man/tcl9.0/TkLib/FindPhoto.htm
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'ext/teek/tkphoto.c', line 461
static VALUE
interp_photo_blank(VALUE self, VALUE photo_path)
{
struct tcltk_interp *tip = get_interp(self);
Tk_PhotoHandle photo;
StringValue(photo_path);
photo = Tk_FindPhoto(tip->interp, StringValueCStr(photo_path));
if (!photo) {
rb_raise(eTclError, "photo image not found: %s", StringValueCStr(photo_path));
}
Tk_PhotoBlank(photo);
return Qnil;
}
|
#photo_expand(photo_path, width_val, height_val) ⇒ Object
Interp#photo_expand(photo_path, width, height)
Expand a photo image to at least the given dimensions using Tk_PhotoExpand. Will not shrink the image if it is already larger.
Arguments:
photo_path - Tcl path of the photo image
width - Minimum width in pixels
height - Minimum height in pixels
Returns nil.
See: https://www.tcl-lang.org/man/tcl8.6/TkLib/FindPhoto.htm
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 |
# File 'ext/teek/tkphoto.c', line 538
static VALUE
interp_photo_expand(VALUE self, VALUE photo_path, VALUE width_val, VALUE height_val)
{
struct tcltk_interp *tip = get_interp(self);
Tk_PhotoHandle photo;
int width, height;
StringValue(photo_path);
width = NUM2INT(width_val);
height = NUM2INT(height_val);
if (width < 0 || height < 0) {
rb_raise(rb_eArgError, "width and height must be non-negative");
}
photo = Tk_FindPhoto(tip->interp, StringValueCStr(photo_path));
if (!photo) {
rb_raise(eTclError, "photo image not found: %s", StringValueCStr(photo_path));
}
if (Tk_PhotoExpand(tip->interp, photo, width, height) != TCL_OK) {
rb_raise(eTclError, "Tk_PhotoExpand failed: %s",
Tcl_GetStringResult(tip->interp));
}
return Qnil;
}
|
#photo_get_image(*args) ⇒ Object
Interp#photo_get_image(photo_path, opts={})
Read pixel data from a photo image using Tk_PhotoGetImage.
Arguments:
photo_path - Tcl path of the photo image (e.g., "i00001")
opts - Optional hash:
:x, :y - source offsets (default 0,0)
:width, :height - region size (default: full image)
:unpack - if true, return flat array of integers
instead of binary string (default: false)
Returns a Hash with:
:data - Binary string of RGBA pixels (4 bytes per pixel), OR
:pixels - Flat array of integers [r,g,b,a,r,g,b,a,...] if unpack: true
:width - Width of returned data
:height - Height of returned data
See: https://www.tcl-lang.org/man/tcl9.0/TkLib/FindPhoto.htm
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'ext/teek/tkphoto.c', line 294
static VALUE
interp_photo_get_image(int argc, VALUE *argv, VALUE self)
{
struct tcltk_interp *tip = get_interp(self);
VALUE photo_path, opts, result;
Tk_PhotoHandle photo;
Tk_PhotoImageBlock block;
int x_off, y_off, req_width, req_height;
int img_width, img_height;
int actual_width, actual_height;
int do_unpack;
unsigned char *src;
int x, y;
int r_off, g_off, b_off, a_off;
rb_scan_args(argc, argv, "11", &photo_path, &opts);
StringValue(photo_path);
/* Find the photo image by Tcl path */
photo = Tk_FindPhoto(tip->interp, StringValueCStr(photo_path));
if (!photo) {
rb_raise(eTclError, "photo image not found: %s", StringValueCStr(photo_path));
}
/* Get image info */
if (!Tk_PhotoGetImage(photo, &block)) {
rb_raise(eTclError, "failed to get photo image data");
}
img_width = block.width;
img_height = block.height;
/* Parse options */
x_off = 0;
y_off = 0;
req_width = img_width;
req_height = img_height;
do_unpack = 0;
if (!NIL_P(opts) && TYPE(opts) == T_HASH) {
VALUE val;
val = rb_hash_aref(opts, ID2SYM(rb_intern("x")));
if (!NIL_P(val)) x_off = NUM2INT(val);
val = rb_hash_aref(opts, ID2SYM(rb_intern("y")));
if (!NIL_P(val)) y_off = NUM2INT(val);
val = rb_hash_aref(opts, ID2SYM(rb_intern("width")));
if (!NIL_P(val)) req_width = NUM2INT(val);
val = rb_hash_aref(opts, ID2SYM(rb_intern("height")));
if (!NIL_P(val)) req_height = NUM2INT(val);
val = rb_hash_aref(opts, ID2SYM(rb_intern("unpack")));
if (RTEST(val)) do_unpack = 1;
}
/* Validate and clamp region */
if (x_off < 0) x_off = 0;
if (y_off < 0) y_off = 0;
if (x_off >= img_width || y_off >= img_height) {
rb_raise(rb_eArgError, "offset outside image bounds");
}
actual_width = req_width;
actual_height = req_height;
if (x_off + actual_width > img_width) actual_width = img_width - x_off;
if (y_off + actual_height > img_height) actual_height = img_height - y_off;
if (actual_width <= 0 || actual_height <= 0) {
rb_raise(rb_eArgError, "invalid region size");
}
/* Get channel offsets from the block */
r_off = block.offset[0];
g_off = block.offset[1];
b_off = block.offset[2];
a_off = block.offset[3];
/* Build result hash */
result = rb_hash_new();
rb_hash_aset(result, ID2SYM(rb_intern("width")), INT2NUM(actual_width));
rb_hash_aset(result, ID2SYM(rb_intern("height")), INT2NUM(actual_height));
if (do_unpack) {
/* Return flat array of integers: [r,g,b,a,r,g,b,a,...] */
long num_values = (long)actual_width * actual_height * 4;
VALUE pixels = rb_ary_new_capa(num_values);
for (y = 0; y < actual_height; y++) {
src = block.pixelPtr + (y_off + y) * block.pitch + x_off * block.pixelSize;
for (x = 0; x < actual_width; x++) {
rb_ary_push(pixels, INT2FIX(src[r_off]));
rb_ary_push(pixels, INT2FIX(src[g_off]));
rb_ary_push(pixels, INT2FIX(src[b_off]));
rb_ary_push(pixels, INT2FIX((block.pixelSize >= 4) ? src[a_off] : 255));
src += block.pixelSize;
}
}
rb_hash_aset(result, ID2SYM(rb_intern("pixels")), pixels);
} else {
/* Return binary string */
VALUE data_str = rb_str_new(NULL, (long)actual_width * actual_height * 4);
unsigned char *dst = (unsigned char *)RSTRING_PTR(data_str);
for (y = 0; y < actual_height; y++) {
src = block.pixelPtr + (y_off + y) * block.pitch + x_off * block.pixelSize;
for (x = 0; x < actual_width; x++) {
*dst++ = src[r_off];
*dst++ = src[g_off];
*dst++ = src[b_off];
*dst++ = (block.pixelSize >= 4) ? src[a_off] : 255;
src += block.pixelSize;
}
}
rb_hash_aset(result, ID2SYM(rb_intern("data")), data_str);
}
return result;
}
|
#photo_get_pixel(photo_path, x_val, y_val) ⇒ Object
Interp#photo_get_pixel(photo_path, x, y)
Read a single pixel from a photo image using Tk_PhotoGetImage. Faster than going through Tcl's "$photo get x y" string parsing.
Arguments:
photo_path - Tcl path of the photo image
x - X coordinate
y - Y coordinate
Returns [r, g, b, a] array.
See: https://www.tcl-lang.org/man/tcl8.6/TkLib/FindPhoto.htm
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 |
# File 'ext/teek/tkphoto.c', line 582
static VALUE
interp_photo_get_pixel(VALUE self, VALUE photo_path, VALUE x_val, VALUE y_val)
{
struct tcltk_interp *tip = get_interp(self);
Tk_PhotoHandle photo;
Tk_PhotoImageBlock block;
int x, y;
unsigned char *src;
int r_off, g_off, b_off, a_off;
StringValue(photo_path);
x = NUM2INT(x_val);
y = NUM2INT(y_val);
photo = Tk_FindPhoto(tip->interp, StringValueCStr(photo_path));
if (!photo) {
rb_raise(eTclError, "photo image not found: %s", StringValueCStr(photo_path));
}
if (!Tk_PhotoGetImage(photo, &block)) {
rb_raise(eTclError, "failed to get photo image data");
}
if (x < 0 || x >= block.width || y < 0 || y >= block.height) {
rb_raise(rb_eArgError, "coordinates (%d, %d) outside image bounds (%d x %d)",
x, y, block.width, block.height);
}
r_off = block.offset[0];
g_off = block.offset[1];
b_off = block.offset[2];
a_off = block.offset[3];
src = block.pixelPtr + y * block.pitch + x * block.pixelSize;
return rb_ary_new_from_args(4,
INT2FIX(src[r_off]),
INT2FIX(src[g_off]),
INT2FIX(src[b_off]),
INT2FIX((block.pixelSize >= 4) ? src[a_off] : 255));
}
|
#photo_get_size(photo_path) ⇒ Object
Interp#photo_get_size(photo_path)
Get dimensions of a photo image using Tk_PhotoGetSize. Faster than querying via Tcl commands.
Arguments:
photo_path - Tcl path of the photo image (e.g., "i00001")
Returns [width, height] array.
See: https://www.tcl-lang.org/man/tcl9.0/TkLib/FindPhoto.htm
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
# File 'ext/teek/tkphoto.c', line 428
static VALUE
interp_photo_get_size(VALUE self, VALUE photo_path)
{
struct tcltk_interp *tip = get_interp(self);
Tk_PhotoHandle photo;
int width, height;
StringValue(photo_path);
photo = Tk_FindPhoto(tip->interp, StringValueCStr(photo_path));
if (!photo) {
rb_raise(eTclError, "photo image not found: %s", StringValueCStr(photo_path));
}
Tk_PhotoGetSize(photo, &width, &height);
return rb_ary_new_from_args(2, INT2NUM(width), INT2NUM(height));
}
|
#photo_put_zoomed_block(*args) ⇒ Object
Interp#photo_put_zoomed_block(photo_path, pixel_data, width, height, opts={})
Fast pixel writes with zoom/subsample using Tk_PhotoPutZoomedBlock. Writes pixels and scales in a single operation - faster than put_block + copy.
Arguments:
photo_path - Tcl path of the photo image (e.g., "i00001")
pixel_data - Binary string of pixels (4 bytes per pixel)
width - Source image width in pixels
height - Source image height in pixels
opts - Optional hash:
:x, :y - destination offsets (default 0,0)
:zoom_x, :zoom_y - zoom factors (default 1,1)
:subsample_x, :subsample_y - subsample factors (default 1,1)
:format - :rgba (default) or :argb
:composite - :set (default, overwrite) or :overlay (alpha blend)
The pixel_data must be exactly width * height * 4 bytes. Zoom replicates pixels (zoom=3 makes each pixel 3x3). Subsample skips pixels (subsample=2 takes every other pixel).
Format :argb expects pixels packed as 0xAARRGGBB integers (little-endian: B,G,R,A bytes). This matches SDL2 and many graphics libraries.
See: https://www.tcl-lang.org/man/tcl8.6/TkLib/FindPhoto.htm
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'ext/teek/tkphoto.c', line 152
static VALUE
interp_photo_put_zoomed_block(int argc, VALUE *argv, VALUE self)
{
struct tcltk_interp *tip = get_interp(self);
VALUE photo_path, pixel_data, width_val, height_val, opts;
Tk_PhotoHandle photo;
Tk_PhotoImageBlock block;
int width, height, x_off, y_off;
int zoom_x, zoom_y, subsample_x, subsample_y;
int dest_width, dest_height;
int is_argb = 0;
int comp_rule = TK_PHOTO_COMPOSITE_SET;
long expected_size;
rb_scan_args(argc, argv, "41", &photo_path, &pixel_data, &width_val, &height_val, &opts);
StringValue(photo_path);
StringValue(pixel_data);
width = NUM2INT(width_val);
height = NUM2INT(height_val);
/* Validate dimensions */
if (width <= 0 || height <= 0) {
rb_raise(rb_eArgError, "width and height must be positive");
}
/* Validate data size */
expected_size = (long)width * height * 4;
if (RSTRING_LEN(pixel_data) != expected_size) {
rb_raise(rb_eArgError, "pixel_data size mismatch: expected %ld bytes, got %ld",
expected_size, RSTRING_LEN(pixel_data));
}
/* Parse options with defaults */
x_off = 0;
y_off = 0;
zoom_x = 1;
zoom_y = 1;
subsample_x = 1;
subsample_y = 1;
if (!NIL_P(opts) && TYPE(opts) == T_HASH) {
VALUE val;
val = rb_hash_aref(opts, ID2SYM(rb_intern("x")));
if (!NIL_P(val)) x_off = NUM2INT(val);
val = rb_hash_aref(opts, ID2SYM(rb_intern("y")));
if (!NIL_P(val)) y_off = NUM2INT(val);
val = rb_hash_aref(opts, ID2SYM(rb_intern("zoom_x")));
if (!NIL_P(val)) zoom_x = NUM2INT(val);
val = rb_hash_aref(opts, ID2SYM(rb_intern("zoom_y")));
if (!NIL_P(val)) zoom_y = NUM2INT(val);
val = rb_hash_aref(opts, ID2SYM(rb_intern("subsample_x")));
if (!NIL_P(val)) subsample_x = NUM2INT(val);
val = rb_hash_aref(opts, ID2SYM(rb_intern("subsample_y")));
if (!NIL_P(val)) subsample_y = NUM2INT(val);
val = rb_hash_aref(opts, ID2SYM(rb_intern("format")));
if (!NIL_P(val) && TYPE(val) == T_SYMBOL) {
if (rb_intern("argb") == SYM2ID(val)) {
is_argb = 1;
}
}
val = rb_hash_aref(opts, ID2SYM(rb_intern("composite")));
if (!NIL_P(val) && TYPE(val) == T_SYMBOL) {
if (rb_intern("overlay") == SYM2ID(val)) {
comp_rule = TK_PHOTO_COMPOSITE_OVERLAY;
}
}
}
/* Validate zoom/subsample */
if (zoom_x <= 0 || zoom_y <= 0) {
rb_raise(rb_eArgError, "zoom factors must be positive");
}
if (subsample_x <= 0 || subsample_y <= 0) {
rb_raise(rb_eArgError, "subsample factors must be positive");
}
/* Find the photo image by Tcl path */
photo = Tk_FindPhoto(tip->interp, StringValueCStr(photo_path));
if (!photo) {
rb_raise(eTclError, "photo image not found: %s", StringValueCStr(photo_path));
}
/* Set up the pixel block structure */
block.pixelPtr = (unsigned char *)RSTRING_PTR(pixel_data);
block.width = width;
block.height = height;
block.pitch = width * 4;
block.pixelSize = 4;
if (is_argb) {
/* ARGB: 0xAARRGGBB stored little-endian as bytes: [B, G, R, A] */
block.offset[0] = 2; /* Red at byte 2 */
block.offset[1] = 1; /* Green at byte 1 */
block.offset[2] = 0; /* Blue at byte 0 */
block.offset[3] = 3; /* Alpha at byte 3 */
} else {
/* RGBA: [R, G, B, A] */
block.offset[0] = 0;
block.offset[1] = 1;
block.offset[2] = 2;
block.offset[3] = 3;
}
/* Calculate destination dimensions */
dest_width = (width / subsample_x) * zoom_x;
dest_height = (height / subsample_y) * zoom_y;
/* Write pixels with zoom/subsample */
if (Tk_PhotoPutZoomedBlock(tip->interp, photo, &block, x_off, y_off,
dest_width, dest_height,
zoom_x, zoom_y, subsample_x, subsample_y,
comp_rule) != TCL_OK) {
rb_raise(eTclError, "Tk_PhotoPutZoomedBlock failed: %s",
Tcl_GetStringResult(tip->interp));
}
return Qnil;
}
|
#photo_set_size(photo_path, width_val, height_val) ⇒ Object
Interp#photo_set_size(photo_path, width, height)
Set the dimensions of a photo image using Tk_PhotoSetSize.
Arguments:
photo_path - Tcl path of the photo image
width - New width in pixels
height - New height in pixels
Returns nil.
See: https://www.tcl-lang.org/man/tcl8.6/TkLib/FindPhoto.htm
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 |
# File 'ext/teek/tkphoto.c', line 494
static VALUE
interp_photo_set_size(VALUE self, VALUE photo_path, VALUE width_val, VALUE height_val)
{
struct tcltk_interp *tip = get_interp(self);
Tk_PhotoHandle photo;
int width, height;
StringValue(photo_path);
width = NUM2INT(width_val);
height = NUM2INT(height_val);
if (width < 0 || height < 0) {
rb_raise(rb_eArgError, "width and height must be non-negative");
}
photo = Tk_FindPhoto(tip->interp, StringValueCStr(photo_path));
if (!photo) {
rb_raise(eTclError, "photo image not found: %s", StringValueCStr(photo_path));
}
if (Tk_PhotoSetSize(tip->interp, photo, width, height) != TCL_OK) {
rb_raise(eTclError, "Tk_PhotoSetSize failed: %s",
Tcl_GetStringResult(tip->interp));
}
return Qnil;
}
|
#queue_for_main(proc) ⇒ Object
Queue a proc to run on the main Tcl thread (fire-and-forget)
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 |
# File 'ext/teek/tcltkbridge.c', line 827
static VALUE
interp_queue_for_main(VALUE self, VALUE proc)
{
struct tcltk_interp *tip;
VALUE cmd_hash;
TypedData_Get_Struct(self, struct tcltk_interp, &interp_type, tip);
if (tip->deleted || tip->interp == NULL) {
rb_raise(eTclError, "interpreter has been deleted");
}
cmd_hash = rb_hash_new();
rb_hash_aset(cmd_hash, ID2SYM(sym_type), sym_proc_val);
rb_hash_aset(cmd_hash, ID2SYM(sym_proc), proc);
return queue_command_internal(tip, cmd_hash, 0);
}
|
#register_callback(proc) ⇒ Object
Interp#register_callback(proc) - Store proc, return ID
561 562 563 564 565 566 567 568 569 570 571 572 573 |
# File 'ext/teek/tcltkbridge.c', line 561
static VALUE
interp_register_callback(VALUE self, VALUE proc)
{
struct tcltk_interp *tip = get_interp(self);
char id_buf[32];
VALUE id_str;
snprintf(id_buf, sizeof(id_buf), "cb%lu", tip->next_id++);
id_str = rb_utf8_str_new_cstr(id_buf);
rb_hash_aset(tip->callbacks, id_str, proc);
return id_str;
}
|
#safe? ⇒ Boolean
Interp#safe? - Check if interpreter is running in safe mode
Safe interpreters have restricted access to dangerous commands like file I/O, exec, socket, etc. Created via create_slave(name, true).
See: https://www.tcl-lang.org/man/tcl/TclCmd/interp.html#M30
1052 1053 1054 1055 1056 1057 |
# File 'ext/teek/tcltkbridge.c', line 1052
static VALUE
interp_safe_p(VALUE self)
{
struct tcltk_interp *tip = get_interp(self);
return Tcl_IsSafe(tip->interp) ? Qtrue : Qfalse;
}
|
#tcl_eval(script) ⇒ Object
Interp#tcl_eval(script) - Evaluate Tcl script string
Thread-safe: automatically routes through event queue if called from a background thread.
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 |
# File 'ext/teek/tcltkbridge.c', line 862
static VALUE
interp_tcl_eval(VALUE self, VALUE script)
{
struct tcltk_interp *tip = get_interp(self);
Tcl_ThreadId current = Tcl_GetCurrentThread();
const char *script_cstr;
int result;
StringValue(script);
/* If on background thread, queue to main thread and wait */
if (current != tip->main_thread_id) {
VALUE cmd_hash = rb_hash_new();
rb_hash_aset(cmd_hash, ID2SYM(sym_type), sym_eval);
rb_hash_aset(cmd_hash, ID2SYM(sym_script), script);
return queue_command_internal(tip, cmd_hash, 1);
}
/* On main thread - execute directly */
script_cstr = StringValueCStr(script);
result = Tcl_Eval(tip->interp, script_cstr);
if (result != TCL_OK) {
raise_tcl_error(tip->interp, result);
}
return rb_utf8_str_new_cstr(Tcl_GetStringResult(tip->interp));
}
|
#tcl_get_var(name) ⇒ Object
Interp#tcl_get_var(name) - Get Tcl variable value
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 |
# File 'ext/teek/tcltkbridge.c', line 961
static VALUE
interp_tcl_get_var(VALUE self, VALUE name)
{
struct tcltk_interp *tip = get_interp(self);
const char *name_cstr;
const char *value;
StringValue(name);
name_cstr = StringValueCStr(name);
value = Tcl_GetVar(tip->interp, name_cstr, TCL_GLOBAL_ONLY);
if (value == NULL) {
return Qnil;
}
return rb_utf8_str_new_cstr(value);
}
|
#tcl_invoke(*args) ⇒ Object
Interp#tcl_invoke(*args) - Invoke Tcl command with args
This is the workhorse - creates widgets, configures them, etc. Thread-safe: automatically routes through event queue if called from a background thread.
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 |
# File 'ext/teek/tcltkbridge.c', line 899
static VALUE
interp_tcl_invoke(int argc, VALUE *argv, VALUE self)
{
struct tcltk_interp *tip = get_interp(self);
Tcl_ThreadId current = Tcl_GetCurrentThread();
Tcl_Obj **objv;
int i, result;
VALUE ret;
if (argc == 0) {
rb_raise(rb_eArgError, "wrong number of arguments (given 0, expected 1+)");
}
/* If on background thread, queue to main thread and wait */
if (current != tip->main_thread_id) {
VALUE cmd_hash = rb_hash_new();
VALUE args_ary = rb_ary_new4(argc, argv);
rb_hash_aset(cmd_hash, ID2SYM(sym_type), sym_invoke);
rb_hash_aset(cmd_hash, ID2SYM(sym_args), args_ary);
return queue_command_internal(tip, cmd_hash, 1);
}
/* On main thread - execute directly */
objv = ALLOCA_N(Tcl_Obj *, argc);
for (i = 0; i < argc; i++) {
VALUE arg = argv[i];
const char *str;
Tcl_Size len;
if (NIL_P(arg)) {
str = "";
len = 0;
} else {
StringValue(arg);
str = RSTRING_PTR(arg);
len = RSTRING_LEN(arg);
}
objv[i] = Tcl_NewStringObj(str, len);
Tcl_IncrRefCount(objv[i]);
}
/* Invoke the command */
result = Tcl_EvalObjv(tip->interp, argc, objv, 0);
/* Clean up Tcl objects */
for (i = 0; i < argc; i++) {
Tcl_DecrRefCount(objv[i]);
}
if (result != TCL_OK) {
raise_tcl_error(tip->interp, result);
}
ret = rb_utf8_str_new_cstr(Tcl_GetStringResult(tip->interp));
return ret;
}
|
#tcl_set_var(name, value) ⇒ Object
Interp#tcl_set_var(name, value) - Set Tcl variable
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 |
# File 'ext/teek/tcltkbridge.c', line 983
static VALUE
interp_tcl_set_var(VALUE self, VALUE name, VALUE value)
{
struct tcltk_interp *tip = get_interp(self);
const char *name_cstr;
const char *value_cstr;
const char *result;
StringValue(name);
name_cstr = StringValueCStr(name);
if (NIL_P(value)) {
value_cstr = "";
} else {
StringValue(value);
value_cstr = StringValueCStr(value);
}
result = Tcl_SetVar(tip->interp, name_cstr, value_cstr, TCL_GLOBAL_ONLY);
if (result == NULL) {
rb_raise(eTclError, "failed to set variable '%s'", name_cstr);
}
return value;
}
|
#tcl_version ⇒ Object
Interp#tcl_version / #tk_version - Get version strings
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 |
# File 'ext/teek/tcltkbridge.c', line 1083
static VALUE
interp_tcl_version(VALUE self)
{
struct tcltk_interp *tip = get_interp(self);
const char *version = Tcl_GetVar(tip->interp, "tcl_patchLevel", TCL_GLOBAL_ONLY);
if (version == NULL) {
return Qnil;
}
return rb_utf8_str_new_cstr(version);
}
|
#thread_timer_ms ⇒ Object
Interp#thread_timer_ms / #thread_timer_ms= - Get/set timer interval
1173 1174 1175 1176 1177 1178 |
# File 'ext/teek/tcltkbridge.c', line 1173
static VALUE
interp_get_thread_timer_ms(VALUE self)
{
struct tcltk_interp *tip = get_interp(self);
return INT2NUM(tip->timer_interval_ms);
}
|
#thread_timer_ms=(val) ⇒ Object
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 |
# File 'ext/teek/tcltkbridge.c', line 1180
static VALUE
interp_set_thread_timer_ms(VALUE self, VALUE val)
{
struct tcltk_interp *tip = get_interp(self);
int ms = NUM2INT(val);
if (ms < 0) {
rb_raise(rb_eArgError, "thread_timer_ms must be >= 0 (got %d)", ms);
}
tip->timer_interval_ms = ms;
return val;
}
|
#tk_version ⇒ Object
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 |
# File 'ext/teek/tcltkbridge.c', line 1094
static VALUE
interp_tk_version(VALUE self)
{
struct tcltk_interp *tip = get_interp(self);
const char *version = Tcl_GetVar(tip->interp, "tk_patchLevel", TCL_GLOBAL_ONLY);
if (version == NULL) {
return Qnil;
}
return rb_utf8_str_new_cstr(version);
}
|
#unregister_callback(id) ⇒ Object
Interp#unregister_callback(id) - Remove proc by ID
579 580 581 582 583 584 585 |
# File 'ext/teek/tcltkbridge.c', line 579
static VALUE
interp_unregister_callback(VALUE self, VALUE id)
{
struct tcltk_interp *tip = get_interp(self);
rb_hash_delete(tip->callbacks, id);
return Qnil;
}
|
#window_geometry(window_path) ⇒ Object
Interp#window_geometry(window_path)
Get a window's screen position and interior size in one call.
Arguments:
window_path - Tk window path (e.g., ".", ".settings")
Returns [root_x, root_y, width, height].
root_x/root_y are absolute screen coordinates (Tk_GetRootCoords). width/height are the interior size excluding any border (Tk_Width/Tk_Height).
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'ext/teek/tkwin.c', line 118
static VALUE
interp_window_geometry(VALUE self, VALUE window_path)
{
struct tcltk_interp *tip = get_interp(self);
Tk_Window mainWin;
Tk_Window tkwin;
int x, y;
StringValue(window_path);
mainWin = Tk_MainWindow(tip->interp);
if (!mainWin) {
rb_raise(eTclError, "Tk not initialized (no main window)");
}
tkwin = Tk_NameToWindow(tip->interp, StringValueCStr(window_path), mainWin);
if (!tkwin) {
rb_raise(eTclError, "window not found: %s", StringValueCStr(window_path));
}
Tk_GetRootCoords(tkwin, &x, &y);
return rb_ary_new_from_args(4,
INT2NUM(x), INT2NUM(y),
INT2NUM(Tk_Width(tkwin)), INT2NUM(Tk_Height(tkwin)));
}
|