clipboard: fix v -cstrict -cc gcc vlib/clipboard/clipboard_test.v

This commit is contained in:
Delyan Angelov 2023-10-15 21:21:24 +03:00
parent 4bc9a8f8d8
commit f54f156d25
7 changed files with 31 additions and 30 deletions

View file

@ -22,9 +22,9 @@ struct WndClassEx {
fn C.RegisterClassEx(class &WndClassEx) int fn C.RegisterClassEx(class &WndClassEx) int
fn C.GetClipboardOwner() &C.HWND fn C.GetClipboardOwner() C.HWND
fn C.CreateWindowEx(dwExStyle i64, lpClassName &u16, lpWindowName &u16, dwStyle i64, x int, y int, nWidth int, nHeight int, hWndParent i64, hMenu voidptr, h_instance voidptr, lpParam voidptr) &C.HWND fn C.CreateWindowEx(dwExStyle i64, lpClassName &u16, lpWindowName &u16, dwStyle i64, x int, y int, nWidth int, nHeight int, hWndParent i64, hMenu voidptr, h_instance voidptr, lpParam voidptr) C.HWND
// fn C.MultiByteToWideChar(CodePage u32, dw_flags u16, lpMultiByteStr byteptr, cbMultiByte int, lpWideCharStr u16, cchWideChar int) int // fn C.MultiByteToWideChar(CodePage u32, dw_flags u16, lpMultiByteStr byteptr, cbMultiByte int, lpWideCharStr u16, cchWideChar int) int
fn C.EmptyClipboard() fn C.EmptyClipboard()
@ -59,7 +59,7 @@ pub struct Clipboard {
max_retries int max_retries int
retry_delay int retry_delay int
mut: mut:
hwnd C.HWND hwnd voidptr
foo int // TODO remove foo int // TODO remove
} }
@ -96,15 +96,16 @@ fn new_clipboard() &Clipboard {
lpsz_menu_name: 0 lpsz_menu_name: 0
h_icon_sm: 0 h_icon_sm: 0
} }
if C.RegisterClassEx(&wndclass) == 0 && C.GetLastError() != u32(C.ERROR_CLASS_ALREADY_EXISTS) { if C.RegisterClassEx(voidptr(&wndclass)) == 0
&& C.GetLastError() != u32(C.ERROR_CLASS_ALREADY_EXISTS) {
println('Failed registering class.') println('Failed registering class.')
} }
hwnd := C.CreateWindowEx(0, wndclass.lpsz_class_name, wndclass.lpsz_class_name, 0, hwnd := C.CreateWindowEx(0, wndclass.lpsz_class_name, wndclass.lpsz_class_name, 0,
0, 0, 0, 0, C.HWND_MESSAGE, C.NULL, C.NULL, C.NULL) 0, 0, 0, 0, C.HWND_MESSAGE, C.NULL, C.NULL, C.NULL)
if hwnd == C.NULL { if hwnd == unsafe { nil } {
println('Error creating window!') println('Error creating window!')
} }
cb.hwnd = hwnd cb.hwnd = voidptr(hwnd)
return cb return cb
} }
@ -116,7 +117,7 @@ pub fn (cb &Clipboard) check_availability() bool {
// has_ownership returns true if the contents of // has_ownership returns true if the contents of
// the clipboard were created by this clipboard instance. // the clipboard were created by this clipboard instance.
pub fn (cb &Clipboard) has_ownership() bool { pub fn (cb &Clipboard) has_ownership() bool {
return C.GetClipboardOwner() == cb.hwnd return voidptr(C.GetClipboardOwner()) == cb.hwnd
} }
// clear empties the clipboard contents. // clear empties the clipboard contents.
@ -140,13 +141,13 @@ const cp_utf8 = 65001
// the string.to_wide doesn't work with SetClipboardData, don't know why // the string.to_wide doesn't work with SetClipboardData, don't know why
fn to_wide(text string) C.HGLOBAL { fn to_wide(text string) C.HGLOBAL {
len_required := C.MultiByteToWideChar(clipboard.cp_utf8, C.MB_ERR_INVALID_CHARS, text.str, len_required := C.MultiByteToWideChar(clipboard.cp_utf8, C.MB_ERR_INVALID_CHARS, voidptr(text.str),
text.len + 1, C.NULL, 0) text.len + 1, C.NULL, 0)
buf := C.GlobalAlloc(C.GMEM_MOVEABLE, i64(sizeof(u16)) * len_required) buf := C.GlobalAlloc(C.GMEM_MOVEABLE, i64(sizeof(u16)) * len_required)
if buf != unsafe { nil } { if buf != unsafe { nil } {
mut locked := &u16(C.GlobalLock(buf)) mut locked := &u16(C.GlobalLock(buf))
C.MultiByteToWideChar(clipboard.cp_utf8, C.MB_ERR_INVALID_CHARS, text.str, text.len + 1, C.MultiByteToWideChar(clipboard.cp_utf8, C.MB_ERR_INVALID_CHARS, voidptr(text.str),
locked, len_required) text.len + 1, locked, len_required)
unsafe { unsafe {
locked[len_required - 1] = u16(0) locked[len_required - 1] = u16(0)
} }

View file

@ -28,7 +28,7 @@ pub fn close(handle voidptr) bool {
// sym returns an address of an exported function or variable from a given module. // sym returns an address of an exported function or variable from a given module.
pub fn sym(handle voidptr, symbol string) voidptr { pub fn sym(handle voidptr, symbol string) voidptr {
return C.GetProcAddress(handle, symbol.str) return C.GetProcAddress(handle, voidptr(symbol.str))
} }
// dlerror provides a text error diagnostic message for functions in `dl` // dlerror provides a text error diagnostic message for functions in `dl`

View file

@ -554,7 +554,7 @@ pub fn get_raw_line() string {
h_input := C.GetStdHandle(C.STD_INPUT_HANDLE) h_input := C.GetStdHandle(C.STD_INPUT_HANDLE)
mut bytes_read := u32(0) mut bytes_read := u32(0)
if is_atty(0) > 0 { if is_atty(0) > 0 {
x := C.ReadConsole(h_input, buf, max_line_chars * 2, &bytes_read, 0) x := C.ReadConsole(h_input, buf, max_line_chars * 2, voidptr(&bytes_read), 0)
if !x { if !x {
return tos(buf, 0) return tos(buf, 0)
} }
@ -563,7 +563,7 @@ pub fn get_raw_line() string {
mut offset := 0 mut offset := 0
for { for {
pos := buf + offset pos := buf + offset
res := C.ReadFile(h_input, pos, 1, &u32(&bytes_read), 0) res := C.ReadFile(h_input, pos, 1, voidptr(&bytes_read), 0)
if !res && offset == 0 { if !res && offset == 0 {
return tos(buf, 0) return tos(buf, 0)
} }
@ -605,7 +605,7 @@ pub fn get_raw_stdin() []u8 {
mut offset := 0 mut offset := 0
for { for {
pos := buf + offset pos := buf + offset
res := C.ReadFile(h_input, pos, block_bytes, &u32(&bytes_read), 0) res := C.ReadFile(h_input, pos, block_bytes, voidptr(&bytes_read), 0)
offset += bytes_read offset += bytes_read
if !res { if !res {
break break

View file

@ -486,7 +486,7 @@ pub fn uname() Uname {
pub fn hostname() !string { pub fn hostname() !string {
hostname := [255]u16{} hostname := [255]u16{}
size := u32(255) size := u32(255)
res := C.GetComputerNameW(&hostname[0], &size) res := C.GetComputerNameW(&hostname[0], voidptr(&size))
if !res { if !res {
return error(get_error_msg(int(C.GetLastError()))) return error(get_error_msg(int(C.GetLastError())))
} }
@ -496,7 +496,7 @@ pub fn hostname() !string {
pub fn loginname() !string { pub fn loginname() !string {
loginname := [255]u16{} loginname := [255]u16{}
size := u32(255) size := u32(255)
res := C.GetUserNameW(&loginname[0], &size) res := C.GetUserNameW(&loginname[0], voidptr(&size))
if !res { if !res {
return error(get_error_msg(int(C.GetLastError()))) return error(get_error_msg(int(C.GetLastError())))
} }

View file

@ -12,7 +12,7 @@ pub fn input_password(prompt string) !string {
std_handle := C.GetStdHandle(C.STD_INPUT_HANDLE) std_handle := C.GetStdHandle(C.STD_INPUT_HANDLE)
mut mode := u32(0) mut mode := u32(0)
unsafe { C.GetConsoleMode(std_handle, &mode) } unsafe { C.GetConsoleMode(std_handle, voidptr(&mode)) }
unsafe { C.SetConsoleMode(std_handle, mode & (~u32(C.ENABLE_ECHO_INPUT))) } unsafe { C.SetConsoleMode(std_handle, mode & (~u32(C.ENABLE_ECHO_INPUT))) }
defer { defer {

View file

@ -8,14 +8,14 @@ fn C.GetProcAddress(handle voidptr, procname &u8) voidptr
fn C.TerminateProcess(process HANDLE, exit_code u32) bool fn C.TerminateProcess(process HANDLE, exit_code u32) bool
fn C.PeekNamedPipe(hNamedPipe voidptr, lpBuffer voidptr, nBufferSize int, lpBytesRead voidptr, lpTotalBytesAvail voidptr, lpBytesLeftThisMessage voidptr) bool fn C.PeekNamedPipe(hNamedPipe voidptr, lpBuffer voidptr, nBufferSize int, lpBytesRead voidptr, lpTotalBytesAvail voidptr, lpBytesLeftThisMessage voidptr) bool
type FN_NTSuspendResume = fn (voidptr) type FN_NTSuspendResume = fn (voidptr) u64
fn ntdll_fn(name &u8) FN_NTSuspendResume { fn ntdll_fn(name &char) FN_NTSuspendResume {
ntdll := C.GetModuleHandleA(c'NTDLL') ntdll := C.GetModuleHandleA(c'NTDLL')
if ntdll == 0 { if ntdll == 0 {
return FN_NTSuspendResume(0) return FN_NTSuspendResume(0)
} }
the_fn := FN_NTSuspendResume(C.GetProcAddress(ntdll, name)) the_fn := FN_NTSuspendResume(C.GetProcAddress(ntdll, voidptr(name)))
return the_fn return the_fn
} }
@ -123,7 +123,7 @@ fn (mut p Process) win_spawn_process() int {
to_be_freed << work_folder_ptr to_be_freed << work_folder_ptr
} }
create_process_ok := C.CreateProcessW(0, &wdata.command_line[0], 0, 0, C.TRUE, creation_flags, create_process_ok := C.CreateProcessW(0, voidptr(&wdata.command_line[0]), 0, 0, C.TRUE, creation_flags,
0, work_folder_ptr, voidptr(&start_info), voidptr(&wdata.proc_info)) 0, work_folder_ptr, voidptr(&start_info), voidptr(&wdata.proc_info))
failed_cfn_report_error(create_process_ok, 'CreateProcess') failed_cfn_report_error(create_process_ok, 'CreateProcess')
if p.use_stdio_ctl { if p.use_stdio_ctl {
@ -212,7 +212,7 @@ fn (mut p Process) win_read_string(idx int, maxbytes int) (string, int) {
return '', 0 return '', 0
} }
mut bytes_avail := int(0) mut bytes_avail := int(0)
if !C.PeekNamedPipe(rhandle, unsafe { nil }, int(0), unsafe { nil }, &bytes_avail, if !C.PeekNamedPipe(rhandle, unsafe { nil }, int(0), unsafe { nil }, voidptr(&bytes_avail),
unsafe { nil }) { unsafe { nil }) {
return '', 0 return '', 0
} }

View file

@ -64,20 +64,20 @@ fn make_unix_time(t C.tm) i64 {
fn init_win_time_freq() u64 { fn init_win_time_freq() u64 {
f := u64(0) f := u64(0)
C.QueryPerformanceFrequency(&f) C.QueryPerformanceFrequency(voidptr(&f))
return f return f
} }
fn init_win_time_start() u64 { fn init_win_time_start() u64 {
s := u64(0) s := u64(0)
C.QueryPerformanceCounter(&s) C.QueryPerformanceCounter(voidptr(&s))
return s return s
} }
// sys_mono_now returns a *monotonically increasing time*, NOT a time adjusted for daylight savings, location etc. // sys_mono_now returns a *monotonically increasing time*, NOT a time adjusted for daylight savings, location etc.
pub fn sys_mono_now() u64 { pub fn sys_mono_now() u64 {
tm := u64(0) tm := u64(0)
C.QueryPerformanceCounter(&tm) // XP or later never fail C.QueryPerformanceCounter(voidptr(&tm)) // XP or later never fail
return (tm - time.start_time) * 1000000000 / time.freq_time return (tm - time.start_time) * 1000000000 / time.freq_time
} }
@ -86,7 +86,7 @@ pub fn sys_mono_now() u64 {
[inline] [inline]
fn vpc_now() u64 { fn vpc_now() u64 {
tm := u64(0) tm := u64(0)
C.QueryPerformanceCounter(&tm) C.QueryPerformanceCounter(voidptr(&tm))
return tm return tm
} }
@ -112,7 +112,7 @@ pub fn (t Time) local() Time {
millisecond: u16(t.nanosecond / 1_000_000) millisecond: u16(t.nanosecond / 1_000_000)
} }
st_local := SystemTime{} st_local := SystemTime{}
C.SystemTimeToTzSpecificLocalTime(unsafe { nil }, &st_utc, &st_local) C.SystemTimeToTzSpecificLocalTime(unsafe { nil }, voidptr(&st_utc), voidptr(&st_local))
t_local := Time{ t_local := Time{
year: st_local.year year: st_local.year
month: st_local.month month: st_local.month
@ -133,9 +133,9 @@ fn win_now() Time {
ft_utc := C._FILETIME{} ft_utc := C._FILETIME{}
C.GetSystemTimeAsFileTime(&ft_utc) C.GetSystemTimeAsFileTime(&ft_utc)
st_utc := SystemTime{} st_utc := SystemTime{}
C.FileTimeToSystemTime(&ft_utc, &st_utc) C.FileTimeToSystemTime(&ft_utc, voidptr(&st_utc))
st_local := SystemTime{} st_local := SystemTime{}
C.SystemTimeToTzSpecificLocalTime(unsafe { nil }, &st_utc, &st_local) C.SystemTimeToTzSpecificLocalTime(unsafe { nil }, voidptr(&st_utc), voidptr(&st_local))
t := Time{ t := Time{
year: st_local.year year: st_local.year
month: st_local.month month: st_local.month
@ -157,7 +157,7 @@ fn win_utc() Time {
ft_utc := C._FILETIME{} ft_utc := C._FILETIME{}
C.GetSystemTimeAsFileTime(&ft_utc) C.GetSystemTimeAsFileTime(&ft_utc)
st_utc := SystemTime{} st_utc := SystemTime{}
C.FileTimeToSystemTime(&ft_utc, &st_utc) C.FileTimeToSystemTime(&ft_utc, voidptr(&st_utc))
t := Time{ t := Time{
year: st_utc.year year: st_utc.year
month: st_utc.month month: st_utc.month