-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.c
More file actions
281 lines (219 loc) · 6.32 KB
/
ui.c
File metadata and controls
281 lines (219 loc) · 6.32 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
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
/* ui.c: User interface routines, but those which are independent of any UI
Copyright (c) 2002-2017 Philip Kendall
Copyright (c) 2016-2021 Sergio Baldoví
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: philip-fuse@shadowmagic.org.uk
*/
#include "config.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "libspectrum.h"
#include "fuse.h"
#include "peripherals/if1.h"
#include "peripherals/kempmouse.h"
#include "settings.h"
#include "tape.h"
#include "ui/ui.h"
#include "ui/uidisplay.h"
#include "ui/uimedia.h"
#include "ui/widget/widget.h"
#define MESSAGE_MAX_LENGTH 256
/* We don't start in a widget */
int ui_widget_level = -1;
static char last_message[ MESSAGE_MAX_LENGTH ] = "";
static size_t frames_since_last_message = 0;
static uidisplay_hotswap_reason next_hotswap_reason =
UIDISPLAY_HOTSWAP_REASON_NONE;
static int
print_error_to_stderr( ui_error_level severity, const char *message );
void
uidisplay_set_next_hotswap_reason( uidisplay_hotswap_reason reason )
{
next_hotswap_reason = reason;
}
uidisplay_hotswap_reason
uidisplay_take_next_hotswap_reason( void )
{
uidisplay_hotswap_reason reason = next_hotswap_reason;
next_hotswap_reason = UIDISPLAY_HOTSWAP_REASON_NONE;
return reason;
}
int
ui_error( ui_error_level severity, const char *format, ... )
{
int error;
va_list ap;
va_start( ap, format );
error = ui_verror( severity, format, ap );
va_end( ap );
return error;
}
int
ui_verror( ui_error_level severity, const char *format, va_list ap )
{
char message[ MESSAGE_MAX_LENGTH ];
vsnprintf( message, MESSAGE_MAX_LENGTH, format, ap );
/* Skip the message if the same message was displayed recently */
if( frames_since_last_message < 50 && !strcmp( message, last_message ) ) {
frames_since_last_message = 0;
return 0;
}
/* And store the 'last message' */
strncpy( last_message, message, MESSAGE_MAX_LENGTH );
last_message[ MESSAGE_MAX_LENGTH - 1 ] = '\0';
print_error_to_stderr( severity, message );
/* Do any UI-specific bits as well */
ui_error_specific( severity, message );
return 0;
}
ui_confirm_save_t
ui_confirm_save( const char *format, ... )
{
va_list ap;
char message[ MESSAGE_MAX_LENGTH ];
ui_confirm_save_t confirm;
va_start( ap, format );
vsnprintf( message, MESSAGE_MAX_LENGTH, format, ap );
confirm = ui_confirm_save_specific( message );
va_end( ap );
return confirm;
}
static int
print_error_to_stderr( ui_error_level severity, const char *message )
{
/* Print the error to stderr if it's more significant than just
informational */
if( severity > UI_ERROR_INFO ) {
/* For the fb UI, we don't want to write to stderr if
it's a terminal, as it's then likely to be what we're currently
using for graphics output, and writing text to it isn't a good
idea. Things are OK if we're exiting though */
#ifdef UI_FB
if( isatty( STDERR_FILENO ) && !fuse_exiting ) return 1;
#endif /* #ifdef UI_FB */
fprintf( stderr, "%s: ", fuse_progname );
switch( severity ) {
case UI_ERROR_INFO: break; /* Shouldn't happen */
case UI_ERROR_WARNING: fprintf( stderr, "warning: " ); break;
case UI_ERROR_ERROR: fprintf( stderr, "error: " ); break;
}
fprintf( stderr, "%s\n", message );
}
return 0;
}
libspectrum_error
ui_libspectrum_error( libspectrum_error error GCC_UNUSED, const char *format,
va_list ap )
{
char new_format[ 257 ];
snprintf( new_format, 256, "libspectrum: %s", format );
ui_verror( UI_ERROR_ERROR, new_format, ap );
return LIBSPECTRUM_ERROR_NONE;
}
void
ui_error_frame( void )
{
frames_since_last_message++;
}
int ui_mouse_present = 0;
int ui_mouse_grabbed = 0;
static int mouse_grab_suspended = 0;
void
ui_mouse_button( int button, int down )
{
int kempston_button = !settings_current.mouse_swap_buttons;
if( !ui_mouse_grabbed && !mouse_grab_suspended ) button = 2;
/* Possibly we'll end up handling _more_ than one mouse interface... */
switch( button ) {
case 1:
if( ui_mouse_grabbed ) kempmouse_update( 0, 0, kempston_button, down );
break;
case 3:
if( ui_mouse_grabbed ) kempmouse_update( 0, 0, !kempston_button, down );
break;
case 2:
if( ui_mouse_present && settings_current.kempston_mouse
&& !down && !mouse_grab_suspended )
ui_mouse_grabbed =
ui_mouse_grabbed ? ui_mouse_release( 1 ) : ui_mouse_grab( 0 );
break;
}
}
void
ui_mouse_motion( int x, int y )
{
if( ui_mouse_grabbed ) kempmouse_update( x, y, -1, 0 );
}
void
ui_mouse_suspend( void )
{
mouse_grab_suspended = ui_mouse_grabbed ? 2 : 1;
if( ui_mouse_grabbed )
ui_mouse_grabbed = ui_mouse_release( 1 );
}
void
ui_mouse_resume( void )
{
if( mouse_grab_suspended == 2) ui_mouse_grabbed = ui_mouse_grab( 0 );
mouse_grab_suspended = 0;
}
void
ui_menu_disk_update( void )
{
int drives_avail;
drives_avail = ui_media_drive_any_available();
/* Set the disk menu items and statusbar appropriately */
if( drives_avail ) {
ui_menu_activate( UI_MENU_ITEM_MEDIA_DISK, 1 );
ui_statusbar_update( UI_STATUSBAR_ITEM_DISK, UI_STATUSBAR_STATE_INACTIVE );
} else {
ui_menu_activate( UI_MENU_ITEM_MEDIA_DISK, 0 );
ui_statusbar_update( UI_STATUSBAR_ITEM_DISK,
UI_STATUSBAR_STATE_NOT_AVAILABLE );
}
ui_media_drive_update_parent_menus();
}
#ifdef USE_WIDGET
int
ui_widget_init( void )
{
return widget_init();
}
int
ui_widget_end( void )
{
return widget_end();
}
#else
int
ui_widget_init( void )
{
return 0;
}
int
ui_widget_end( void )
{
return 0;
}
void
ui_popup_menu( int native_key )
{
}
void
ui_widget_keyhandler( int native_key )
{
}
#endif /* #ifndef USE_WIDGET */