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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//
// Copyright (C) 2019 Robert Gill <locke@sdf.org>
//
// This file is a part of newt-rs.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License version 2.1 as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//

use libc::{c_char,c_void};
use std::ffi::{CStr,CString};
use std::mem::size_of;

use newt_sys::{newtComponent,newtGrid,newtGridElement,newtWinEntry};
use crate::constants::NEWT_GRID_EMPTY;
use crate::intern::funcs::*;
use crate::windows::WinEntry;

pub fn grid_new(func: *const c_void, types: Vec<newtGridElement>,
                values: Vec<newtComponent>, len: usize) -> newtGrid {
    unsafe {
        let grid: newtGrid;
        llvm_asm! {
            "movq $3,     %rcx
             movq $2,     %rsi
             movq $1,     %rdi

             subq $$8,    %rsp
             movq $4,     %rax
             pushq        %rax
             xorq %rbx,   %rbx
             addq $$2,    %rbx

             sub $$3,     %rcx
             jz           reg0${:uid}
             cmp $$-1,    %rcx
             je           null_r8${:uid}
             cmp $$-2,    %rcx
             je           null_rdx${:uid}
             movq %rcx,   %rax
             shl $$1,     %rax
             addq %rax,   %rbx

             loop${:uid}:
             movq (%rsi), %rax
             pushq        %rax
             addq $$8,    %rsi
             movl (%rdi), %eax
             pushq        %rax
             addq $$4,    %rdi
             loop         loop${:uid}

             reg0${:uid}:
             movq (%rsi), %r9
             addq $$8,    %rsi
             movl (%rdi), %eax
             movq %rax,   %r8
             addq $$4,    %rdi

             reg1${:uid}:
             movq (%rsi), %rcx
             addq $$8,    %rsi
             movl (%rdi), %edx
             addq $$4,    %rdi

             reg2${:uid}:
             movq (%rsi), %rsi
             movl (%rdi), %eax
             movq %rax,   %rdi

             callq        *$5
             movq %rax,   $0

             shl $$3,     %rbx
             addq %rbx,   %rsp
             jmp          exit${:uid}

             null_r8${:uid}:
             xorq %r8,    %r8
             jmp          reg1${:uid}

             null_rdx${:uid}:
             xorq %rdx,   %rdx
             jmp          reg2${:uid}

             exit${:uid}:"

             : "=r"(grid)
             : "m"(types.as_ptr()), "m"(values.as_ptr()),
               "m"(len), "i"(NEWT_GRID_EMPTY), "r"(func)
             : "rsp", "rax", "rbx", "rcx", "rdi", "rsi", "r8", "r9"
        }
        grid
    }
}

pub fn button_bar_new(buttons: &[&str], buf: *mut newtComponent) -> newtGrid {
    let buttons = str_slice_to_cstring_vec(buttons);
    let mut button_ptrs = cstring_vec_to_ptrs(&buttons);
    button_ptrs.reverse();

    unsafe {
        let grid: newtGrid;
        llvm_asm! {
            "movq $3,     %rcx
             movq $2,     %rsi
             movq $1,     %rdi
             addq $$8,    %rdi

             subq $$8,    %rsp
             xorq %rax,   %rax
             pushq        %rax
             xorq %rbx,   %rbx
             addq $$2,    %rbx

             sub $$3,     %rcx
             jz           reg0${:uid}
             cmp $$-1,    %rcx
             je           null_r8${:uid}
             cmp $$-2,    %rcx
             je           null_rdx${:uid}
             movq %rcx,   %rax
             shl $$1,     %rax
             addq %rax,   %rbx

             loop${:uid}:
             movq %rsi,   %rax
             pushq        %rax
             addq $$8,    %rsi
             movq (%rdi), %rax
             pushq        %rax
             addq $$8,    %rdi
             loop         loop${:uid}

             reg0${:uid}:
             movq %rsi,   %r9
             addq $$8,    %rsi
             movq (%rdi), %r8
             addq $$8,    %rdi

             reg1${:uid}:
             movq %rsi,   %rcx
             addq $$8,    %rsi
             movq (%rdi), %rdx
             addq $$8,    %rdi

             reg2${:uid}:
             movq (%rdi), %rdi

             call newtButtonBar
             movq %rax,   $0

             shl $$3,     %rbx
             addq %rbx,   %rsp
             jmp          exit${:uid}

             null_r8${:uid}:
             xorq %r8,    %r8
             jmp          reg1${:uid}

             null_rdx${:uid}:
             xorq %rdx,   %rdx
             jmp          reg2${:uid}

             exit${:uid}:"

            : "=r"(grid)
            : "m"(button_ptrs.as_ptr()), "m"(buf), "m"(buttons.len())
            : "rsp", "rax", "rbx", "rcx", "rdi", "rsi", "r8", "r9"
        }
        grid
    }
}

///
/// Open a window containing a [`Listbox`][listbox] menu.
///
/// _Requires that the `asm` feature be enabled._
///
/// * `title` - The window title.
/// * `text` - The message to display in the window.
/// * `suggested_width` - The preferred width for the window.
/// * `flex_down` - The minimum allowed difference between `suggested_width`
///                 and actual width.
/// * `flex_up` - The maximum allowed difference between `suggested_width`
///               and actual width.
/// * `max_list_height` - The maximum height to display the list of items.
/// * `items` - A slice containing the text for each item in the list.
/// * `buttons` - A slice containing the text for a number of buttons to display
///               in the window.
///
/// Returns a tuple pair as `(button, item)` where `button` is the button
/// number pressed to close the window and `item` is the item number in the
/// list that was selected.
///
/// [listbox]: ../widgets/struct.Listbox.html
///
#[allow(clippy::too_many_arguments)]
pub fn win_menu(title: &str, text: &str, suggested_width: i32, flex_down: i32,
                flex_up: i32, max_list_height: i32, items: &[&str],
                buttons: &[&str]) -> (i32, i32) {
    let mut rv: i32;
    let list_item: i32 = 0;

    let title = CString::new(title).unwrap();
    let text  = CString::new(text).unwrap();

    let items = str_slice_to_cstring_vec(items);
    let item_ptrs = cstring_vec_to_ptrs(&items);

    let buttons = str_slice_to_cstring_vec(buttons);
    let mut button_ptrs = cstring_vec_to_ptrs(&buttons);
    button_ptrs.reverse();

    unsafe {
        llvm_asm! {
            "movq $10,    %rcx
             movq $9,     %rsi
             movq %rcx,   %rbx

             test $$1,    %rcx
             jz           loop${:uid}

             subq $$8,    %rsp
             addq $$1,    %rbx

             loop${:uid}:
             movq (%rsi), %rax
             pushq        %rax
             addq $$8,    %rsi
             loop         loop${:uid}

             movq $8,     %rax
             pushq        %rax
             movq $7,     %rax
             pushq        %rax

             movq $1,     %rdi
             movq $2,     %rsi
             mov  $3,     %rdx
             mov  $4,     %rcx
             mov  $5,     %r8
             mov  $6,     %r9

             call newtWinMenu
             mov  %eax,   $0

             addq $$2,    %rbx
             shl  $$3,    %rbx
             addq %rbx,   %rsp"

            : "=r"(rv)
            : "m"(title.as_ptr()), "m"(text.as_ptr()), "m"(suggested_width),
              "m"(flex_down), "m"(flex_up), "m"(max_list_height),
              "m"(item_ptrs.as_ptr()), "m"(&list_item),
              "m"(button_ptrs.as_ptr()), "m"(button_ptrs.len())
            : "rsp", "rax", "rbx", "rcx", "rdx", "rdi", "rsi", "r8", "r9"
        }
    }

    (rv, list_item)
}

///
/// Open a window containing a number of text [`Entry`s][entry].
///
/// _Requires that the `asm` feature be enabled._
///
/// * `title` - The window title.
/// * `text` - The message to display in the window.
/// * `suggested_width` - The preferred width for the window.
/// * `flex_down` - The minimum allowed difference between `suggested_width`
///                 and actual width.
/// * `flex_up` - The maximum allowed difference between `suggested_width`
///               and actual width.
/// * `data_width` - The field width for all `Entry`s.
/// * `entries` - A slice containing a list of [`WinEntry`s][win_entry]
///               providing initial settings for each `Entry` field.
/// * `buttons` - A slice containing the text for a number of buttons to
///               display in the window.
///
/// Returns the number of the button pressed to close the window.
///
/// Each `WinEntry` in the `entries` array will be modified to contain the
/// data entered by the user. This can be accessed via the
/// [`WinEntry.value()`][win_entry_value] function.
///
/// [entry]: ../widgets/struct.Entry.html
/// [win_entry]: ../windows/struct.WinEntry.html
/// [win_entry_value]: ../windows/struct.WinEntry.html#method.value
///
#[allow(clippy::too_many_arguments)]
pub fn win_entries(title: &str, text: &str, suggested_width: i32,
                   flex_down: i32, flex_up: i32, data_width: i32,
                   entries: &mut [WinEntry], buttons: &[&str]) -> i32 {
    let mut rv: i32;

    let title = CString::new(title).unwrap();
    let text  = CString::new(text).unwrap();

    let buttons = str_slice_to_cstring_vec(buttons);
    let mut button_ptrs = cstring_vec_to_ptrs(&buttons);
    button_ptrs.reverse();

    let entries_buf: *mut newtWinEntry;
    let mut entries_text: Vec<CString> = Vec::new();

    let values_buf: *mut *mut c_char;
    let mut values_text: Vec<CString> = Vec::new();

    unsafe {
        let size = size_of::<newtWinEntry>() * (entries.len() + 1);
        entries_buf = libc::malloc(size) as *mut newtWinEntry;
        libc::memset(entries_buf as *mut c_void, 0, size);

        let size = size_of::<*mut c_char>() * (entries.len());
        values_buf = libc::malloc(size) as *mut *mut c_char;
        libc::memset(values_buf as *mut c_void, 0, size);

        for (cnt, entry) in entries.iter().enumerate() {
            let entry_buf = entries_buf.add(cnt);
            let value_buf = values_buf.add(cnt);
            let text = CString::new(entry.text.as_str()).unwrap();
            let value = CString::new(entry.value.as_str()).unwrap();
            *value_buf = value.as_ptr() as *mut c_char;

            (*entry_buf).text = text.as_ptr() as *mut c_char;
            (*entry_buf).value = value_buf;
            (*entry_buf).flags = entry.flags;
            entries_text.push(text);
            values_text.push(value);
        }

        llvm_asm! {
            "movq $9,     %rcx
             movq $8,     %rsi
             movq %rcx,   %rbx

             test $$1,    %rcx
             jnz          loop${:uid}

             subq $$8,    %rsp
             addq $$1,    %rbx

             loop${:uid}:
             movq (%rsi), %rax
             pushq        %rax
             addq $$8,    %rsi
             loop         loop${:uid}

             movq $7,     %rax
             pushq        %rax

             movq $1,     %rdi
             movq $2,     %rsi
             mov  $3,     %rdx
             mov  $4,     %rcx
             mov  $5,     %r8
             mov  $6,     %r9

             call newtWinEntries
             mov  %eax,   $0

             addq $$1,    %rbx
             shl  $$3,    %rbx
             addq %rbx,   %rsp"

            : "=r"(rv)
            : "m"(title.as_ptr()), "m"(text.as_ptr()), "m"(suggested_width),
              "m"(flex_down), "m"(flex_up), "m"(data_width), "m"(entries_buf),
              "m"(button_ptrs.as_ptr()),  "m"(button_ptrs.len())
            : "rsp", "rax", "rbx", "rcx", "rdx", "rdi", "rsi", "r8", "r9"
        }

        for (cnt, entry) in entries.iter_mut().enumerate() {
            let buf = entries_buf.add(cnt);
            let value = CStr::from_ptr(*(*buf).value).to_str().unwrap();
            entry.value = String::from(value);
            libc::free(*(*buf).value as *mut c_void);
        }

        libc::free(entries_buf as *mut c_void);
        libc::free(values_buf as *mut c_void);
    }

    rv
}