Skip to content

Commit

Permalink
term.ui: fix exception thrown in GetConsoleMode on windows (stop abus…
Browse files Browse the repository at this point in the history
…ing consts as globals) (#19529)
  • Loading branch information
Wertzui123 authored Oct 10, 2023
1 parent 2ce209d commit a1c16a7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
10 changes: 3 additions & 7 deletions vlib/term/ui/input_nix.c.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2020-2021 Raúl Hernández. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
[has_globals]
module ui

struct ExtraContext {
Expand All @@ -11,7 +12,7 @@ mut:
read_all_bytes bool = true
}

const ctx_ptr = &Context(unsafe { nil })
__global ctx_ptr = &Context(unsafe { nil })

// init initializes the terminal console with Config `cfg`.
pub fn init(cfg Config) &Context {
Expand All @@ -20,12 +21,7 @@ pub fn init(cfg Config) &Context {
}
ctx.read_buf = []u8{cap: cfg.buffer_size}

// lmao
unsafe {
x := &ui.ctx_ptr
*x = ctx
_ = x
}
ctx_ptr = ctx
return ctx
}

Expand Down
20 changes: 9 additions & 11 deletions vlib/term/ui/input_windows.c.v
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
// Copyright (c) 2020-2021 Raúl Hernández. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
[has_globals]
module ui

import os
import time

const buf_size = 64

const ctx_ptr = &Context(unsafe { nil })
__global ctx_ptr = &Context(unsafe { nil })

const stdin_at_startup = u32(0)
__global stdin_at_startup = u32(0)

struct ExtraContext {
mut:
Expand All @@ -21,14 +22,14 @@ mut:
}

fn restore_terminal_state() {
if unsafe { ui.ctx_ptr != 0 } {
if ui.ctx_ptr.cfg.use_alternate_buffer {
if unsafe { ctx_ptr != 0 } {
if ctx_ptr.cfg.use_alternate_buffer {
// clear the terminal and set the cursor to the origin
print('\x1b[2J\x1b[3J')
print('\x1b[?1049l')
flush_stdout()
}
C.SetConsoleMode(ui.ctx_ptr.stdin_handle, ui.stdin_at_startup)
C.SetConsoleMode(ctx_ptr.stdin_handle, stdin_at_startup)
}
load_title()
os.flush()
Expand All @@ -46,7 +47,7 @@ pub fn init(cfg Config) &Context {
panic('could not get stdin handle')
}
// save the current input mode, to be restored on exit
if !C.GetConsoleMode(stdin_handle, &ui.stdin_at_startup) {
if !C.GetConsoleMode(stdin_handle, &stdin_at_startup) {
panic('could not get stdin console mode')
}

Expand Down Expand Up @@ -80,14 +81,11 @@ pub fn init(cfg Config) &Context {
flush_stdout()
}

unsafe {
x := &ui.ctx_ptr
*x = ctx
}
ctx_ptr = ctx
C.atexit(restore_terminal_state)
for code in ctx.cfg.reset {
os.signal_opt(code, fn (_ os.Signal) {
mut c := unsafe { ui.ctx_ptr }
mut c := ctx_ptr
if unsafe { c != 0 } {
c.cleanup()
}
Expand Down
8 changes: 4 additions & 4 deletions vlib/term/ui/termios_nix.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn restore_terminal_state_signal(_ os.Signal) {

fn restore_terminal_state() {
termios_reset()
mut c := unsafe { ctx_ptr }
mut c := ctx_ptr
if unsafe { c != 0 } {
c.paused = true
load_title()
Expand Down Expand Up @@ -118,7 +118,7 @@ fn (mut ctx Context) termios_setup() ! {
C.atexit(restore_terminal_state)
os.signal_opt(.tstp, restore_terminal_state_signal) or {}
os.signal_opt(.cont, fn (_ os.Signal) {
mut c := unsafe { ctx_ptr }
mut c := ctx_ptr
if unsafe { c != 0 } {
c.termios_setup() or { panic(err) }
c.window_height, c.window_width = get_terminal_size()
Expand All @@ -133,7 +133,7 @@ fn (mut ctx Context) termios_setup() ! {
}) or {}
for code in ctx.cfg.reset {
os.signal_opt(code, fn (_ os.Signal) {
mut c := unsafe { ctx_ptr }
mut c := ctx_ptr
if unsafe { c != 0 } {
c.cleanup()
}
Expand All @@ -142,7 +142,7 @@ fn (mut ctx Context) termios_setup() ! {
}

os.signal_opt(.winch, fn (_ os.Signal) {
mut c := unsafe { ctx_ptr }
mut c := ctx_ptr
if unsafe { c != 0 } {
c.window_height, c.window_width = get_terminal_size()

Expand Down

0 comments on commit a1c16a7

Please sign in to comment.