aboutsummaryrefslogtreecommitdiffstats
path: root/fb/fbmenu.c
blob: 850cd1102479de485a99cd1a3aa00ab6afbbfbe9 (plain)
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
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>

#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <getopt.h>
#include <sys/ioctl.h>
#include <linux/fb.h>

static int fb;
struct fb_var_screeninfo   fb_var;
static GtkWidget *window = NULL;
char *deflt;

void usage() {
    printf("\nUsage:\n\n\tfbmenu <timeout in second> <menu title> <menu entry 1> <menu entry 2> ... <menu entry n>\n\n");
    exit(1);
}


void message_dialog_clicked (GtkButton *button, gpointer user_data)
{     
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_widget_destroyed), &window);
  gtk_widget_destroy (window);
  printf("\nRESULT=\"%s\"\n", (char *) user_data);
  exit(0);
}    

static GtkWidget *
create_bbox (gint  horizontal,
	     char *title, 
	     gint  spacing,
	     gint  layout,
	     int argc,
	     char **argv)
{
  GtkWidget *frame;
  GtkWidget *bbox;
  GtkWidget *button;
  int i;
	
  frame = gtk_frame_new (title);

  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
  if (horizontal)
    bbox = gtk_hbutton_box_new ();
  else
    bbox = gtk_vbutton_box_new ();

  gtk_container_set_border_width (GTK_CONTAINER (bbox), 5);
  gtk_container_add (GTK_CONTAINER (frame), bbox);

  gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), layout);
  gtk_box_set_spacing (GTK_BOX (bbox), spacing);
 
  for (i = 0; i < argc; i++) { 
      button = gtk_button_new_with_mnemonic (argv[i]);
      g_signal_connect (button, "clicked", G_CALLBACK (message_dialog_clicked), argv[i]);
      gtk_container_add (GTK_CONTAINER (bbox), button);
  }
  return frame;
}

GtkWidget *
do_button_box (GtkWidget *do_widget, int argc, char** argv)
{
  static GtkWidget *window = NULL;
  GtkWidget *main_vbox;
  GtkWidget *eventbox;
  GtkWidget *hbox;
  int x,y;
	
  if (!window)
  {
    if (-1 == ioctl(fb,FBIOGET_VSCREENINFO,&fb_var)) {
	    x = 800;
	    y = 600;
    } else {
	    x = fb_var.xres;
	    y = fb_var.yres;
    }


    window = gtk_window_new (GTK_WINDOW_POPUP);
    gtk_window_set_screen (GTK_WINDOW (window),
			   gtk_widget_get_screen (do_widget));
    gtk_window_set_title (GTK_WINDOW (window), argv[0]);
    gtk_window_resize(GTK_WINDOW(window), x, y);
    
    //gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    g_signal_connect (window, "destroy",
		      G_CALLBACK (gtk_widget_destroyed),
		      &window);
    
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);

    main_vbox = gtk_vbox_new (FALSE, 0);
    gtk_container_add (GTK_CONTAINER (window), main_vbox);
    
    hbox = gtk_hbox_new (FALSE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (hbox), 10);
    gtk_box_pack_start (GTK_BOX (main_vbox), 
			hbox,
			TRUE, FALSE, 5);
    eventbox = gtk_event_box_new ();
    gtk_event_box_set_visible_window (GTK_EVENT_BOX (eventbox), TRUE);
    gtk_box_pack_start (GTK_BOX (hbox), eventbox, TRUE, FALSE, 5);
    gtk_container_add (GTK_CONTAINER (eventbox), create_bbox (FALSE, argv[0], 30, GTK_BUTTONBOX_START, argc - 1, argv + 1));
  }

  if (!GTK_WIDGET_VISIBLE (window))
    {
      gtk_widget_show_all (window);
    }
  else
    {	 
      gtk_widget_destroy (window);
      window = NULL;
    }

  return window;
}

void return_default(int x) {
    printf("\nRESULT=\"%s\"\n", deflt);
    exit(0);
}

int filter_argv (int argc, char **argv, char **nargv) {
    int i, j=0, k, done;
    for (i = 0; i < argc; i++) {
	done = 0;
	for (k = 0; k < j; k++) {
	    if (!strcmp(argv[i], nargv[k])) {
		done = 1;
	    }
	}
	if (done) continue;
	nargv[j++] = argv[i];
    }
    return j;
}

int main(int argc, char **argv) {
    int i;
    int shift = 2;
    int title = 2;
    int nargc;
    char *nargv[argc];
    
    if (argc < 4) {
	usage();
    }
    if (argc == 4) {
	printf("\nRESULT=\"%s\"\n", argv[3]);
	exit(0);
    }
    deflt = argv[3];
    nargv[0] = argv[2];
    nargc = filter_argv(argc - shift - 1, argv + shift + 1, nargv + 1);
    if (nargc == 1) {
	printf("\nRESULT=\"%s\"\n", nargv[0]);
	exit(0);
    }

    fb = open("/dev/fb0",O_RDWR);
    if (-1 == fb && getenv("DISPLAY") == NULL) {
	signal(SIGALRM, return_default);
	alarm(atoi(argv[1]));
	do { 
	    fprintf(stderr,"\n");
	    for (i = 0; i < nargc; i++) { 
		fprintf(stderr,"%d - %s\n", i + 1, nargv[i]);
	    }
	    fprintf(stderr,"\n%s: ", argv[title]);
	    if (!scanf("%d", &i)) scanf("%*s");
	} while (i < 1 || i > nargc);
	printf("\nRESULT=\"%s\"\n", nargv[i - 1]);
    } else {
	gtk_init (&argc, &argv);
	do_button_box(window, nargc + 1, nargv);
	g_timeout_add(atoi(argv[1]) * 1000, (GSourceFunc) return_default, NULL);
	gtk_main ();
    }
    return 0;
}