Skip to content

Commit

Permalink
Added support to pass null as a window monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-deluna committed Sep 15, 2024
1 parent 845b08f commit 25e8b30
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 15 deletions.
4 changes: 2 additions & 2 deletions docs/API/GLFW/glfwSetWindowMonitor.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Sets the mode, monitor, video mode and placement of a window.

```php
function glfwSetWindowMonitor(\GLFWwindow $window, \GLFWmonitor $monitor, int $xpos, int $ypos, int $width, int $height, int $refreshRate) : void
function glfwSetWindowMonitor(\GLFWwindow $window, ?\GLFWmonitor $monitor, int $xpos, int $ypos, int $width, int $height, int $refreshRate) : void
```

This function sets the monitor that the window uses for full screen mode or,
Expand All @@ -28,7 +28,7 @@ arguments

: 1. `\GLFWwindow` `$window` The window whose monitor, size or video mode to
set.
2. `\GLFWmonitor` `$monitor` The desired monitor, or `NULL` to set windowed
2. `?\GLFWmonitor` `$monitor` The desired monitor, or `NULL` to set windowed
mode.
3. `int` `$xpos` The desired x-coordinate of the upper-left corner of the
content area.
Expand Down
1 change: 1 addition & 0 deletions generator/build
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ $gen->adjust(PHPGlfwAdjustments\GLFWSwapBuffersAdjustment::class);
$gen->adjust(PHPGlfwAdjustments\GLFWCallbacksAdjustment::class);
$gen->adjust(PHPGlfwAdjustments\GLFWCreateWindowAdjustment::class);
$gen->adjust(PHPGlfwAdjustments\GLFWGetVideoModesAdjustment::class);
$gen->adjust(PHPGlfwAdjustments\GLFWSetWindowMonitorAdjustment::class);
$gen->adjust(PHPGlfwAdjustments\GLShaderSourceAdjustment::class);
$gen->adjust(PHPGlfwAdjustments\GLBufferDataAdjustment::class);
$gen->adjust(PHPGlfwAdjustments\GLVertexAttribPointerAdjustment::class);
Expand Down
5 changes: 5 additions & 0 deletions generator/src/ExtArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public static function make(string $name, string $argumentType) : ExtArgument
*/
public bool $isParsed = true;

/**
* Is the argument nullable? can null be passed instead of the desired type?
*/
public bool $isNullable = false;

/**
* Constrcutor
* @param string $name The name of the argument
Expand Down
4 changes: 2 additions & 2 deletions generator/src/ExtArgument/InternalPtrObjectArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function typeIsOfSameInternalSize() : bool
public function getCharId() : string
{
$c = $this->charid;
if ($this->isOptional()) $c .= '!';
if ($this->isOptional() || $this->isNullable) $c .= '!';

return $c;
}
Expand Down Expand Up @@ -78,7 +78,7 @@ public function getZendParameterParseArguments() : array
*/
public function getUsePrepCode() : string
{
return $this->argInternalPtrObject->getInternalPtrFromZValDeclarationCode($this->name, $this->getZValName(), $this->defaultValue);
return $this->argInternalPtrObject->getInternalPtrFromZValDeclarationCode($this->name, $this->getZValName(), $this->defaultValue, $this->isNullable);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions generator/src/ExtFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public function getPHPStubArgument(ExtArgument $arg, bool $allowDefaultValue = t
{
// IPO argument
if ($arg->argumentType === ExtType::T_IPO) {
$buffer = ($arg->isOptional() ? '?' : '') . $arg->argInternalPtrObject->getPHPClassName() . ' ' . '$' . $arg->name;
$buffer = ($arg->isOptional() || $arg->isNullable ? '?' : '') . $arg->argInternalPtrObject->getPHPClassName() . ' ' . '$' . $arg->name;

if ($arg->isOptional() && $allowDefaultValue) {
$buffer .= ' = ' . $arg->defaultValue;
Expand All @@ -339,7 +339,7 @@ public function getPHPStubArgument(ExtArgument $arg, bool $allowDefaultValue = t
}
// class entry argument
elseif ($arg->argumentType === ExtType::T_CE && $arg instanceof CEObjectArgument) {
$buffer = ($arg->isOptional() ? '?' : '') . $arg->getPHPClassName() . ' ' . ($arg->explicitPassedByReference ? '&' : '') . '$' . $arg->name;
$buffer = ($arg->isOptional() || $arg->isNullable ? '?' : '') . $arg->getPHPClassName() . ' ' . ($arg->explicitPassedByReference ? '&' : '') . '$' . $arg->name;

if ($arg->isOptional() && $allowDefaultValue) {
$buffer .= ' = ' . $arg->defaultValue;
Expand Down
4 changes: 2 additions & 2 deletions generator/src/ExtInternalPtrObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ public function getObjectMinitHelperFunctionName()
/**
* Returns C code that declares the given varname of the internal type by resolving a zval potiner
*/
public function getInternalPtrFromZValDeclarationCode(string $internalVarName, string $zvalVarName, ?string $defaultValue = null) : string
public function getInternalPtrFromZValDeclarationCode(string $internalVarName, string $zvalVarName, ?string $defaultValue = null, bool $isNullable = false) : string
{
if ($defaultValue !== null) {
if ($defaultValue !== null || $isNullable) {
$b = $this->type . ' ' . $internalVarName . ' = NULL;' . PHP_EOL;
$b .= sprintf("if (%s != NULL && Z_TYPE_P(%s) == IS_OBJECT) {\n", $zvalVarName, $zvalVarName);
$b .= sprintf(" %s = %s(%s);\n}", $internalVarName, $this->getInternalPtrFromZValPtrFunctionName(), $zvalVarName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace PHPGlfwAdjustments;

use ExtGenerator;

class GLFWSetWindowMonitorAdjustment implements AdjustmentInterface
{
/**
* Recieves an instance of the extension generator before beeing built to
* make changes / adjustments for the extension to handle edge cases whithout
* adding a million ifs into the parser code.
*/
public function handle(ExtGenerator $gen) : void
{
$setMonitorFunc = $gen->getFunctionByName('glfwSetWindowMonitor');

// monitor needs to be optional (nullable) if you want to force back
// windowed mode
$setMonitorFunc->arguments[1]->isNullable = true;
}
}
2 changes: 1 addition & 1 deletion phpglfw.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -2715,7 +2715,7 @@ function glfwHideWindow(GLFWwindow $window) : void {}
function glfwFocusWindow(GLFWwindow $window) : void {}
function glfwRequestWindowAttention(GLFWwindow $window) : void {}
function glfwGetWindowMonitor(GLFWwindow $window) : GLFWmonitor {}
function glfwSetWindowMonitor(GLFWwindow $window, GLFWmonitor $monitor, int $xpos, int $ypos, int $width, int $height, int $refreshRate) : void {}
function glfwSetWindowMonitor(GLFWwindow $window, ?GLFWmonitor $monitor, int $xpos, int $ypos, int $width, int $height, int $refreshRate) : void {}
function glfwGetWindowAttrib(GLFWwindow $window, int $attrib) : int {}
function glfwSetWindowAttrib(GLFWwindow $window, int $attrib, int $value) : void {}
function glfwSetWindowPosCallback(GLFWwindow $window, callable $callback) : void {}
Expand Down
4 changes: 2 additions & 2 deletions phpglfw_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: b0d79ff50f0ceba7e80a0de9e20cc3b5dfd7fa96 */
* Stub hash: 5987166c03bdd5557449c93647771b2487551a2c */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_glCullFace, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, mode, IS_LONG, 0)
Expand Down Expand Up @@ -2051,7 +2051,7 @@ ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_glfwSetWindowMonitor, 0, 7, IS_VOID, 0)
ZEND_ARG_OBJ_INFO(0, window, GLFWwindow, 0)
ZEND_ARG_OBJ_INFO(0, monitor, GLFWmonitor, 0)
ZEND_ARG_OBJ_INFO(0, monitor, GLFWmonitor, 1)
ZEND_ARG_TYPE_INFO(0, xpos, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, ypos, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, width, IS_LONG, 0)
Expand Down
7 changes: 5 additions & 2 deletions phpglfw_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -11337,11 +11337,14 @@ PHP_FUNCTION(glfwSetWindowMonitor)
zend_long width;
zend_long height;
zend_long refreshRate;
if (zend_parse_parameters(ZEND_NUM_ARGS() , "OOlllll", &window_zval, phpglfw_glfwwindow_ce, &monitor_zval, phpglfw_glfwmonitor_ce, &xpos, &ypos, &width, &height, &refreshRate) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() , "OO!lllll", &window_zval, phpglfw_glfwwindow_ce, &monitor_zval, phpglfw_glfwmonitor_ce, &xpos, &ypos, &width, &height, &refreshRate) == FAILURE) {
return;
}
GLFWwindow* window = phpglfw_glfwwindowptr_from_zval_ptr(window_zval);
GLFWmonitor* monitor = phpglfw_glfwmonitorptr_from_zval_ptr(monitor_zval);
GLFWmonitor* monitor = NULL;
if (monitor_zval != NULL && Z_TYPE_P(monitor_zval) == IS_OBJECT) {
monitor = phpglfw_glfwmonitorptr_from_zval_ptr(monitor_zval);
}
glfwSetWindowMonitor(window, monitor, xpos, ypos, width, height, refreshRate);
}

Expand Down
4 changes: 2 additions & 2 deletions stubs/phpglfw.php
Original file line number Diff line number Diff line change
Expand Up @@ -9495,7 +9495,7 @@ function glfwGetWindowMonitor(GLFWwindow $window) : GLFWmonitor {}
*
* @param GLFWwindow $window The window whose monitor, size or video mode to
* set.
* @param GLFWmonitor $monitor The desired monitor, or `NULL` to set windowed
* @param ?GLFWmonitor $monitor The desired monitor, or `NULL` to set windowed
* mode.
* @param int $xpos The desired x-coordinate of the upper-left corner of the
* content area.
Expand All @@ -9510,7 +9510,7 @@ function glfwGetWindowMonitor(GLFWwindow $window) : GLFWmonitor {}
*
* @return void
*/
function glfwSetWindowMonitor(GLFWwindow $window, GLFWmonitor $monitor, int $xpos, int $ypos, int $width, int $height, int $refreshRate) : void {}
function glfwSetWindowMonitor(GLFWwindow $window, ?GLFWmonitor $monitor, int $xpos, int $ypos, int $width, int $height, int $refreshRate) : void {}

/**
* Returns an attribute of the specified window.
Expand Down

0 comments on commit 25e8b30

Please sign in to comment.