-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_dialog_linux.odin
More file actions
220 lines (203 loc) · 5.97 KB
/
Copy pathfile_dialog_linux.odin
File metadata and controls
220 lines (203 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package file_dialog
import "core:fmt"
import "core:os"
import "core:os/os2"
import "core:strings"
when ODIN_OS == .Linux || ODIN_OS == .Darwin {
foreign libc
{
popen :: proc(command: cstring, type: cstring) -> ^FILE ---
pclose :: proc(stream: ^FILE) -> i32 ---
fgets :: proc "cdecl" (s: [^]byte, n: i32, stream: ^FILE) -> [^]u8 ---
}
foreign import libc "system:c"
FILE :: struct {}
@(private = "file")
DialogType :: enum {
Zenity,
KDialog,
}
@(private = "file")
find_installed_dialog_binary :: proc() -> (path: string, type: DialogType, ok: bool) {
zenity := find_binary_location("zenity")
kdialog := find_binary_location("kdialog")
desktop := os.get_env("XDG_CURRENT_DESKTOP", context.temp_allocator)
switch desktop {
case "KDE":
if kdialog != "" {
return kdialog, .KDialog, true
}
if zenity != "" {
return zenity, .Zenity, true
}
case "GNOME":
if zenity != "" {
return zenity, .Zenity, true
} else if kdialog != "" {
return kdialog, .KDialog, true
}
case "":
// Probably running in some window manager
if zenity != "" {
return zenity, .Zenity, true
} else if kdialog != "" {
return kdialog, .KDialog, true
}
case:
unimplemented()
}
return
}
@(private = "file")
find_binary_location :: proc(
name: string,
allocator := context.temp_allocator,
) -> (
path: string,
) {
location_buf: [1024]byte
file := popen(fmt.ctprintf("/usr/bin/env whereis %v", name), "r")
defer pclose(file)
fgets(raw_data(location_buf[:]), len(location_buf), file)
location := string(location_buf[len(fmt.tprintf("%v: ", name)):])
location, _ = strings.replace_all(location, "\n", "", context.temp_allocator)
return strings.clone(location)
}
@(private = "file")
execute_binary :: proc(fullpath: string) -> (output: string, exitcode: int) {
cmd := strings.split(fullpath, " ")
state, stdout, stderr, proc_err := os2.process_exec(
os2.Process_Desc{command = cmd},
context.allocator,
)
if state.exit_code != 0 || proc_err != nil {
fmt.printfln("Error: Process Error: %v", proc_err)
fmt.printfln("----------------- STDOUT --------------------")
fmt.printfln("%s", transmute(string)stdout)
fmt.printfln("----------------- STDERR --------------------")
fmt.printfln("%s", transmute(string)stderr)
fmt.printfln("----------------- STATUS --------------------")
fmt.printfln("%v", state)
return "", state.exit_code
}
return transmute(string)stdout, state.exit_code
}
open_file_dialog :: proc(filter: ..string, directory: bool = false) -> string {
switch _, type, _ := find_installed_dialog_binary(); type {
case .KDialog:
command: string
if directory {
command = fmt.tprintf("kdialog --getexistingdirectory")
} else {
if len(filter) == 0 {
command = fmt.tprintf("kdialog --getopenfilename")
} else {
command = fmt.tprintf(
"kdialog --getopenfilename '%v'",
strings.join(filter, " "),
)
}
}
output, exit_code := execute_binary(command)
if exit_code != 0 {
fmt.eprintfln("Error: %v", output)
return ""
}
// Remove newlines
output, _ = strings.replace_all(output, "\n", "", context.temp_allocator)
return output
case .Zenity:
command: string
if directory {
command = fmt.tprintf("zenity --file-selection --directory")
} else {
if len(filter) == 0 {
command = fmt.tprintf("zenity --file-selection")
} else {
command = fmt.tprintf(
"zenity --file-selection --file-filter='%v'",
strings.join(filter, " "),
)
}
}
output, exit_code := execute_binary(command)
if exit_code != 0 {
fmt.eprintfln("Error: %v", output)
return ""
}
// Remove newlines
output, _ = strings.replace_all(output, "\n", "", context.temp_allocator)
return output
}
unimplemented()
}
save_file_dialog :: proc(filter: ..string) -> string {
switch _, type, _ := find_installed_dialog_binary(); type {
case .KDialog:
command: string
if len(filter) == 0 {
command = fmt.tprintf("kdialog --getsavefilename")
} else {
command = fmt.tprintf("kdialog --getsavefilename '%v'", strings.join(filter, " "))
}
output, _ := execute_binary(command)
if strings.ends_with(output, "\n") {
output = output[:len(output) - 1] // Remove trailing newline
}
return output
case .Zenity:
command: string
if len(filter) == 0 {
command = fmt.tprintf("zenity --file-selection --save")
} else {
command = fmt.tprintf(
"zenity --file-selection --save --file-filter='%v'",
strings.join(filter, " "),
)
}
output, _ := execute_binary(command)
if strings.ends_with(output, "\n") {
output = output[:len(output) - 1] // Remove trailing newline
}
return output
}
unimplemented()
}
show_popup :: proc(title: string, message: string, type: PopupType) -> (result: bool) {
notification_type := type
output: int
switch _, type, _ := find_installed_dialog_binary(); type {
case .KDialog:
command: string
switch notification_type {
case .Info:
command = fmt.tprintf("kdialog --title '%v' --msgbox '%v' 5", title, message)
case .Warning:
command = fmt.tprintf("kdialog --title '%v' --sorry '%v'", title, message)
case .Error:
command = fmt.tprintf("kdialog --title '%v' --error '%v'", title, message)
case .Question:
command = fmt.tprintf("kdialog --title '%v' --yesno '%v'", title, message)
}
_, output = execute_binary(command)
case .Zenity:
command: string
switch notification_type {
case .Info:
command = fmt.tprintf("zenity --info --title='%v' --text='%v'", title, message)
case .Warning:
command = fmt.tprintf("zenity --warning --title='%v' --text='%v'", title, message)
case .Error:
command = fmt.tprintf("zenity --error --title='%v' --text='%v'", title, message)
case .Question:
command = fmt.tprintf("zenity --question --title='%v' --text='%v'", title, message)
}
_, output = execute_binary(command)
}
if output == 0 {
return true
} else {
return false
}
}
}