Debian Codegraph @

src/term_win32.c

raw
1 /*
2 term_win32: Windows-specifc terminal functionality
3
4 This is a very lightweight terminal library, just the minimum to
5
6 - get at the width of the terminal (if there is one)
7 - be able to read single keys being pressed for control
8 - maybe also switch of echoing of input
9
10 copyright 2008-2022 by the mpg123 project - free software under the terms of the LGPL 2.1
11 see COPYING and AUTHORS files in distribution or http://mpg123.org
12 initially written by Thomas Orgis and Jonathan Yong
13 */
14
15
16 #include "config.h"
17 #include "compat/compat.h"
18
19 #include "terms.h"
20
21 #define WIN32_LEAN_AND_MEAN 1
22 #include <io.h>
23 #include <ctype.h>
24 #include <windows.h>
25 #include <wincon.h>
26
27 #include "common/debug.h"
28
29 static HANDLE consoleintput = INVALID_HANDLE_VALUE;
30 static HANDLE consoleoutput = INVALID_HANDLE_VALUE;
31 static HANDLE getconsoleintput(void){
32 DWORD mode;
33 if(consoleintput == INVALID_HANDLE_VALUE){
34 consoleintput = CreateFileW(L"CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
35 if(consoleintput == INVALID_HANDLE_VALUE || consoleintput == NULL)
36 return consoleintput;
37 GetConsoleMode(consoleintput, &mode);
38 mode |= ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT|ENABLE_WINDOW_INPUT;
39 mode &= ~(ENABLE_ECHO_INPUT|ENABLE_QUICK_EDIT_MODE|ENABLE_MOUSE_INPUT);
40 SetConsoleMode(consoleintput, mode);
41 }
42 return consoleintput;
43 }
44
45 static HANDLE getconsole(void){
46 if(consoleoutput == INVALID_HANDLE_VALUE){
47 consoleoutput = CreateFileW(L"CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
48 }
49 return consoleoutput;
50 }
51
52 // No fun for windows until we reorganize control character stuff.
53 int term_have_fun(int fd, int want_visuals)
54 {
55 return 0;
56 }
57
58 int term_setup(void)
59 {
60 return 0;
61 }
62
63 void term_restore(void){
64 CloseHandle(consoleintput);
65 CloseHandle(consoleoutput);
66 consoleintput = INVALID_HANDLE_VALUE;
67 consoleoutput = INVALID_HANDLE_VALUE;
68 }
69
70 int term_width(int fd)
71 {
72 CONSOLE_SCREEN_BUFFER_INFO pinfo;
73 HANDLE h;
74
75 h = getconsole();
76
77 if(h == INVALID_HANDLE_VALUE || h == NULL)
78 return -1;
79 if(GetConsoleScreenBufferInfo(h, &pinfo))
80 // One less than actual width, as Terminal advances
81 // to next line with the last character.
82 return pinfo.dwMaximumWindowSize.X -1;
83 return -1;
84 }
85
86 int term_present(void){
87 return GetConsoleWindow() ? 1 : 0;
88 }
89
90 /* Get the next pressed key, if any.
91 Returns 1 when there is a key, 0 if not. */
92 int term_get_key(int stopped, int do_delay, char *val){
93 INPUT_RECORD record;
94 HANDLE input;
95 DWORD res;
96
97 input = getconsoleintput();
98 if(input == NULL || input == INVALID_HANDLE_VALUE)
99 return 0;
100
101 while(WaitForSingleObject(input, stopped ? INFINITE : (do_delay==1 ? 10 : (do_delay==-1 ? 1 : 0))) == WAIT_OBJECT_0){
102 do_delay = 0;
103 if(!ReadConsoleInput(input, &record, 1, &res))
104 return 0;
105 if(record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown){
106 *val = record.Event.KeyEvent.uChar.AsciiChar;
107 return 1;
108 }
109 }
110
111 return 0;
112 }