1/*
2 * Copyright (c) 2008 Apple Computer, Inc.  All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License").  You may not use this file except in compliance with the
9 * License.  Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <curses.h>
26#include "preferences.h"
27#include "userinput_order.h"
28#include "top.h"
29
30static void mode_completion(void *tinst, struct user_input_state *s) {
31    if(!strlen(s->buf)) {
32	/* Use the current mode. */
33	user_input_set_state(NULL);
34	return;
35    }
36
37    if(top_prefs_set_mode(s->buf)) {
38	char errbuf[60];
39	if(-1 == snprintf(errbuf, sizeof(errbuf), "invalid mode: %s\n",
40			  s->buf)) {
41	    user_input_set_error_state("mode error buffer overflow");
42	    return;
43	}
44	user_input_set_error_state(errbuf);
45	return;
46    }
47
48    /*Success*/
49
50    /*
51     * This has an order dependency, and assumes that the
52     * relayout will be lazy.
53     */
54    top_relayout_force();
55    top_insert(tinst);
56    user_input_set_state(NULL);
57}
58
59static void mode_draw(void *tinst, struct user_input_state *s, WINDOW *win,
60		       int row, int column) {
61    char display[60];
62
63    if(-1 == snprintf(display, sizeof(display), "mode [%s]: %s\n",
64		      top_prefs_get_mode_string(), s->buf))
65	return;
66
67    mvwaddstr(win, row, column, display);
68}
69
70struct user_input_state top_user_input_mode_state = {
71    .offset = 0,
72    .completion = mode_completion,
73    .draw = mode_draw
74};
75