Skip to content

Commit 71fce25

Browse files
windows support added
1 parent 8fc46cd commit 71fce25

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

src/Application.php

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Class Application
45
*
@@ -7,9 +8,11 @@
78
*
89
* Integrates the Tcl/Tk event processing with PHP application logic via FFI.
910
*/
11+
1012
namespace PhpGui;
1113

12-
class Application {
14+
class Application
15+
{
1316
private ProcessTCL $tcl;
1417
private bool $running = false;
1518
private string $appId;
@@ -20,13 +23,18 @@ class Application {
2023
* Initializes the Tcl interpreter, configures the root window,
2124
* and sets up the exit procedure using a quit signal file.
2225
*/
23-
public function __construct() {
26+
public function __construct()
27+
{
2428
$this->tcl = ProcessTCL::getInstance();
2529
$this->appId = uniqid('app_');
2630
$this->tcl->evalTcl("package require Tk");
27-
$this->tcl->evalTcl("wm withdraw ."); // Suppress the default root window
28-
// Update exit_app to write a quit signal file
29-
$this->tcl->evalTcl("proc ::exit_app {} { set ::forever 1; set f [open \"/tmp/phpgui_quit.txt\" w]; puts \$f 1; close \$f }");
31+
$this->tcl->evalTcl("wm withdraw .");
32+
33+
34+
$quitFile = (PHP_OS_FAMILY === 'Windows')
35+
? str_replace('\\', '/', sys_get_temp_dir()) . "/phpgui_quit.txt"
36+
: "/tmp/phpgui_quit.txt";
37+
$this->tcl->evalTcl("proc ::exit_app {} { set ::forever 1; set f [open \"$quitFile\" w]; puts \$f 1; close \$f }");
3038
}
3139

3240
/**
@@ -35,27 +43,33 @@ public function __construct() {
3543
* Processes Tcl events continuously, checks for callback and quit signals,
3644
* and terminates the loop when a quit signal is received.
3745
*/
38-
public function run(): void {
46+
public function run(): void
47+
{
3948
if ($this->running) {
4049
return;
4150
}
4251

4352
$this->running = true;
44-
// Main loop: process Tcl events, callback file, and quit file
4553
while ($this->running) {
4654
$this->tcl->evalTcl("update");
47-
$callbackFile = '/tmp/phpgui_callback.txt';
55+
// Use OS-based callback file path.
56+
$callbackFile = (PHP_OS_FAMILY === 'Windows')
57+
? str_replace('\\', '/', sys_get_temp_dir()) . "/phpgui_callback.txt"
58+
: "/tmp/phpgui_callback.txt";
4859
if (file_exists($callbackFile)) {
4960
$id = trim(file_get_contents($callbackFile));
5061
unlink($callbackFile);
5162
ProcessTCL::getInstance()->executeCallback($id);
5263
}
53-
$quitFile = '/tmp/phpgui_quit.txt';
64+
// Use OS-based quit file path.
65+
$quitFile = (PHP_OS_FAMILY === 'Windows')
66+
? str_replace('\\', '/', sys_get_temp_dir()) . "/phpgui_quit.txt"
67+
: "/tmp/phpgui_quit.txt";
5468
if (file_exists($quitFile)) {
5569
unlink($quitFile);
5670
$this->running = false;
5771
}
58-
usleep(100000); // sleep for 0.1 second
72+
usleep(100000);
5973
}
6074
$this->quit();
6175
}
@@ -65,12 +79,13 @@ public function run(): void {
6579
*
6680
* Stops the main event loop and exits the application.
6781
*/
68-
public function quit(): void {
82+
public function quit(): void
83+
{
6984
if (!$this->running) {
7085
return;
7186
}
72-
87+
7388
$this->running = false;
74-
exit(0);
89+
exit(0);
7590
}
7691
}

src/ProcessTCL.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ private function __construct()
3030
{
3131
$libDir = dirname(__DIR__) . '/src/lib/';
3232
if (PHP_OS_FAMILY === 'Windows') {
33-
$libPath = $libDir . 'tcl86t.dll';
33+
$libPath = $libDir . 'windows/bin/tcl86t.dll';
3434
$libPath = str_replace('/', '\\', $libPath);
3535
} elseif (PHP_OS_FAMILY === 'Darwin') {
36-
$libPath = $libDir . 'libtcl9.0.dylib';
36+
$libPath = $libDir . 'libtcl9.0.dylib'; //temporary path for macOS
3737
} else {
3838
$libPath = $libDir . 'libtcl8.6.so';
3939
}
@@ -126,20 +126,21 @@ private function getInterp()
126126
*/
127127
private function definePhpCallbackBridge($interp): void
128128
{
129+
$tempDir = str_replace('\\', '/', sys_get_temp_dir());
130+
$callbackFile = $tempDir . "/phpgui_callback.txt";
131+
129132
$this->evalTcl('
130133
namespace eval php {
131134
variable callbacks
132135
array set callbacks {}
133136
}
134137
');
135-
$this->evalTcl('
136-
proc php::executeCallback {id} {
137-
set f [open "/tmp/phpgui_callback.txt" w]
138-
puts $f $id
139-
close $f
138+
$this->evalTcl("proc php::executeCallback {id} {
139+
set f [open \"{$callbackFile}\" w]
140+
puts \$f \$id
141+
close \$f
140142
update
141-
}
142-
');
143+
}");
143144
}
144145

145146
/**

0 commit comments

Comments
 (0)