Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gg: add linux support for fn screen_size() Size fix (#23146) #23326

Merged
merged 7 commits into from
Jan 2, 2025
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion vlib/gg/gg.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,40 @@
import sokol.sgl
import sokol.gfx

@[typedef]
struct C.XRRScreenResources {
noutput int
outputs &int
}

@[typedef]
struct C.XRROutputInfo {
crtc u64
}

@[typedef]
struct C.XRRCrtcInfo {
width u32
height u32
}

fn C.XOpenDisplay(int) voidptr
fn C.XCloseDisplay(voidptr) int
fn C.DefaultScreen(voidptr) int
fn C.DefaultRootWindow(voidptr) u64
fn C.XRRGetScreenResources(voidptr, u64) &C.XRRScreenResources
fn C.XRRGetOutputPrimary(voidptr, u64) u64
fn C.XRRFreeScreenResources(&C.XRRScreenResources)
fn C.XRRGetOutputInfo(voidptr, &C.XRRScreenResources, u64) &C.XRROutputInfo
fn C.XRRFreeOutputInfo(&C.XRROutputInfo)
fn C.XRRGetCrtcInfo(voidptr, &C.XRRScreenResources, u64) &C.XRRCrtcInfo
fn C.XRRFreeCrtcInfo(&C.XRRCrtcInfo)

$if linux {
#flag -lXrandr
#include <X11/extensions/Xrandr.h>
}

$if windows {
#flag -lgdi32
#include "windows.h"
Expand Down Expand Up @@ -739,7 +773,41 @@
height: int(C.GetSystemMetrics(C.SM_CYSCREEN))
}
}
// TODO: linux, etc
$if linux {
display := C.XOpenDisplay(0)
islonely marked this conversation as resolved.
Show resolved Hide resolved
if display == unsafe { nil } {
return Size{}
}
defer { _ := C.XCloseDisplay(display) }

Check failure on line 781 in vlib/gg/gg.c.v

View workflow job for this annotation

GitHub Actions / common (ubuntu-latest)

assignment mismatch: 1 variable but `C.XCloseDisplay()` returns 0 values

Check failure on line 781 in vlib/gg/gg.c.v

View workflow job for this annotation

GitHub Actions / common (ubuntu-latest)

assignment mismatch: 1 variable but `C.XCloseDisplay()` returns 0 values

Check failure on line 781 in vlib/gg/gg.c.v

View workflow job for this annotation

GitHub Actions / common (ubuntu-latest)

assignment mismatch: 1 variable but `C.XCloseDisplay()` returns 0 values

Check failure on line 781 in vlib/gg/gg.c.v

View workflow job for this annotation

GitHub Actions / common (ubuntu-latest)

assignment mismatch: 1 variable but `C.XCloseDisplay()` returns 0 values

Check failure on line 781 in vlib/gg/gg.c.v

View workflow job for this annotation

GitHub Actions / common (ubuntu-latest)

assignment mismatch: 1 variable but `C.XCloseDisplay()` returns 0 values

Check failure on line 781 in vlib/gg/gg.c.v

View workflow job for this annotation

GitHub Actions / common (ubuntu-latest)

assignment mismatch: 1 variable but `C.XCloseDisplay()` returns 0 values

Check failure on line 781 in vlib/gg/gg.c.v

View workflow job for this annotation

GitHub Actions / common (ubuntu-latest)

assignment mismatch: 1 variable but `C.XCloseDisplay()` returns 0 values

Check failure on line 781 in vlib/gg/gg.c.v

View workflow job for this annotation

GitHub Actions / common (ubuntu-latest)

assignment mismatch: 1 variable but `C.XCloseDisplay()` returns 0 values

Check failure on line 781 in vlib/gg/gg.c.v

View workflow job for this annotation

GitHub Actions / common (ubuntu-latest)

assignment mismatch: 1 variable but `C.XCloseDisplay()` returns 0 values

Check failure on line 781 in vlib/gg/gg.c.v

View workflow job for this annotation

GitHub Actions / common (ubuntu-latest)

assignment mismatch: 1 variable but `C.XCloseDisplay()` returns 0 values

Check failure on line 781 in vlib/gg/gg.c.v

View workflow job for this annotation

GitHub Actions / common (ubuntu-latest)

assignment mismatch: 1 variable but `C.XCloseDisplay()` returns 0 values

Check failure on line 781 in vlib/gg/gg.c.v

View workflow job for this annotation

GitHub Actions / common (ubuntu-latest)

assignment mismatch: 1 variable but `C.XCloseDisplay()` returns 0 values
islonely marked this conversation as resolved.
Show resolved Hide resolved
root := C.DefaultRootWindow(display)
resources := C.XRRGetScreenResources(display, root)
if resources == unsafe { nil } {
return Size{}
}
defer { C.XRRFreeScreenResources(resources) }
primary_output := C.XRRGetOutputPrimary(display, root)
if primary_output == 0 {
return Size{}
}
for i := 0; i < resources.noutput; i++ {
if unsafe { u64(resources.outputs[i]) } == primary_output {
output_info := C.XRRGetOutputInfo(display, resources, unsafe { resources.outputs[i] })
if output_info == unsafe { nil } {
return Size{}
}
defer { C.XRRFreeOutputInfo(output_info) }
crtc_info := C.XRRGetCrtcInfo(display, resources, output_info.crtc)
if crtc_info == unsafe { nil } {
return Size{}
}
defer { C.XRRFreeCrtcInfo(crtc_info) }
return Size{
width: unsafe { int(crtc_info.width) }
height: unsafe { int(crtc_info.height) }
}
}
}
}
return Size{}
}

Expand Down
Loading