aboutsummaryrefslogtreecommitdiffstats
path: root/src/keyboard.c
diff options
context:
space:
mode:
authorDaniel Colascione2012-10-07 14:31:58 -0800
committerDaniel Colascione2012-10-07 14:31:58 -0800
commit36a305a723c63fd345be65c536c52fe9765c14be (patch)
treefb89d9e103552863214c60297a65320917109357 /src/keyboard.c
parent2ab329f3b5d52a39f0a45c3d9c129f1c19560142 (diff)
parent795b1482a9e314cda32d62ac2988f573d359366e (diff)
downloademacs-36a305a723c63fd345be65c536c52fe9765c14be.tar.gz
emacs-36a305a723c63fd345be65c536c52fe9765c14be.zip
Merge from trunk
Diffstat (limited to 'src/keyboard.c')
-rw-r--r--src/keyboard.c369
1 files changed, 154 insertions, 215 deletions
diff --git a/src/keyboard.c b/src/keyboard.c
index 451ffd9d600..05b9a9dde01 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -19,6 +19,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19 19
20#include <config.h> 20#include <config.h>
21 21
22#define BLOCKINPUT_INLINE EXTERN_INLINE
22#define KEYBOARD_INLINE EXTERN_INLINE 23#define KEYBOARD_INLINE EXTERN_INLINE
23 24
24#include <stdio.h> 25#include <stdio.h>
@@ -72,19 +73,12 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
72 73
73/* Variables for blockinput.h: */ 74/* Variables for blockinput.h: */
74 75
75/* Non-zero if interrupt input is blocked right now. */ 76/* Positive if interrupt input is blocked right now. */
76volatile int interrupt_input_blocked; 77volatile int interrupt_input_blocked;
77 78
78/* Nonzero means an input interrupt has arrived 79/* True means an input interrupt or alarm signal has arrived.
79 during the current critical section. */ 80 The QUIT macro checks this. */
80int interrupt_input_pending; 81volatile bool pending_signals;
81
82/* This var should be (interrupt_input_pending || pending_atimers).
83 The QUIT macro checks this instead of interrupt_input_pending and
84 pending_atimers separately, to reduce code size. So, any code that
85 changes interrupt_input_pending or pending_atimers should update
86 this too. */
87int pending_signals;
88 82
89#define KBD_BUFFER_SIZE 4096 83#define KBD_BUFFER_SIZE 4096
90 84
@@ -225,7 +219,11 @@ static Lisp_Object last_point_position_window;
225 last event came from a macro. We use this to determine when to 219 last event came from a macro. We use this to determine when to
226 generate switch-frame events. This may be cleared by functions 220 generate switch-frame events. This may be cleared by functions
227 like Fselect_frame, to make sure that a switch-frame event is 221 like Fselect_frame, to make sure that a switch-frame event is
228 generated by the next character. */ 222 generated by the next character.
223
224 FIXME: This is modified by a signal handler so it should be volatile.
225 It's exported to Lisp, though, so it can't simply be marked
226 'volatile' here. */
229Lisp_Object internal_last_event_frame; 227Lisp_Object internal_last_event_frame;
230 228
231/* The timestamp of the last input event we received from the X server. 229/* The timestamp of the last input event we received from the X server.
@@ -389,7 +387,7 @@ int interrupts_deferred;
389 387
390/* If we support a window system, turn on the code to poll periodically 388/* If we support a window system, turn on the code to poll periodically
391 to detect C-g. It isn't actually used when doing interrupt input. */ 389 to detect C-g. It isn't actually used when doing interrupt input. */
392#if defined (HAVE_WINDOW_SYSTEM) && !defined (USE_ASYNC_EVENTS) 390#ifdef HAVE_WINDOW_SYSTEM
393#define POLL_FOR_INPUT 391#define POLL_FOR_INPUT
394#endif 392#endif
395 393
@@ -413,7 +411,6 @@ static EMACS_TIME timer_last_idleness_start_time;
413/* Function for init_keyboard to call with no args (if nonzero). */ 411/* Function for init_keyboard to call with no args (if nonzero). */
414static void (*keyboard_init_hook) (void); 412static void (*keyboard_init_hook) (void);
415 413
416static int read_avail_input (int);
417static void get_input_pending (int *, int); 414static void get_input_pending (int *, int);
418static int readable_events (int); 415static int readable_events (int);
419static Lisp_Object read_char_x_menu_prompt (ptrdiff_t, Lisp_Object *, 416static Lisp_Object read_char_x_menu_prompt (ptrdiff_t, Lisp_Object *,
@@ -440,7 +437,7 @@ static Lisp_Object restore_kboard_configuration (Lisp_Object);
440#ifdef USABLE_SIGIO 437#ifdef USABLE_SIGIO
441static void deliver_input_available_signal (int signo); 438static void deliver_input_available_signal (int signo);
442#endif 439#endif
443static void handle_interrupt (void); 440static void handle_interrupt (bool);
444static _Noreturn void quit_throw_to_read_char (int); 441static _Noreturn void quit_throw_to_read_char (int);
445static void process_special_events (void); 442static void process_special_events (void);
446static void timer_start_idle (void); 443static void timer_start_idle (void);
@@ -448,50 +445,50 @@ static void timer_stop_idle (void);
448static void timer_resume_idle (void); 445static void timer_resume_idle (void);
449static void deliver_user_signal (int); 446static void deliver_user_signal (int);
450static char *find_user_signal_name (int); 447static char *find_user_signal_name (int);
451static int store_user_signal_events (void); 448static void store_user_signal_events (void);
452 449
453/* These setters are used only in this file, so they can be private. */ 450/* These setters are used only in this file, so they can be private. */
454static inline void 451static void
455kset_echo_string (struct kboard *kb, Lisp_Object val) 452kset_echo_string (struct kboard *kb, Lisp_Object val)
456{ 453{
457 kb->INTERNAL_FIELD (echo_string) = val; 454 kb->INTERNAL_FIELD (echo_string) = val;
458} 455}
459static inline void 456static void
460kset_kbd_queue (struct kboard *kb, Lisp_Object val) 457kset_kbd_queue (struct kboard *kb, Lisp_Object val)
461{ 458{
462 kb->INTERNAL_FIELD (kbd_queue) = val; 459 kb->INTERNAL_FIELD (kbd_queue) = val;
463} 460}
464static inline void 461static void
465kset_keyboard_translate_table (struct kboard *kb, Lisp_Object val) 462kset_keyboard_translate_table (struct kboard *kb, Lisp_Object val)
466{ 463{
467 kb->INTERNAL_FIELD (Vkeyboard_translate_table) = val; 464 kb->INTERNAL_FIELD (Vkeyboard_translate_table) = val;
468} 465}
469static inline void 466static void
470kset_last_prefix_arg (struct kboard *kb, Lisp_Object val) 467kset_last_prefix_arg (struct kboard *kb, Lisp_Object val)
471{ 468{
472 kb->INTERNAL_FIELD (Vlast_prefix_arg) = val; 469 kb->INTERNAL_FIELD (Vlast_prefix_arg) = val;
473} 470}
474static inline void 471static void
475kset_last_repeatable_command (struct kboard *kb, Lisp_Object val) 472kset_last_repeatable_command (struct kboard *kb, Lisp_Object val)
476{ 473{
477 kb->INTERNAL_FIELD (Vlast_repeatable_command) = val; 474 kb->INTERNAL_FIELD (Vlast_repeatable_command) = val;
478} 475}
479static inline void 476static void
480kset_local_function_key_map (struct kboard *kb, Lisp_Object val) 477kset_local_function_key_map (struct kboard *kb, Lisp_Object val)
481{ 478{
482 kb->INTERNAL_FIELD (Vlocal_function_key_map) = val; 479 kb->INTERNAL_FIELD (Vlocal_function_key_map) = val;
483} 480}
484static inline void 481static void
485kset_overriding_terminal_local_map (struct kboard *kb, Lisp_Object val) 482kset_overriding_terminal_local_map (struct kboard *kb, Lisp_Object val)
486{ 483{
487 kb->INTERNAL_FIELD (Voverriding_terminal_local_map) = val; 484 kb->INTERNAL_FIELD (Voverriding_terminal_local_map) = val;
488} 485}
489static inline void 486static void
490kset_real_last_command (struct kboard *kb, Lisp_Object val) 487kset_real_last_command (struct kboard *kb, Lisp_Object val)
491{ 488{
492 kb->INTERNAL_FIELD (Vreal_last_command) = val; 489 kb->INTERNAL_FIELD (Vreal_last_command) = val;
493} 490}
494static inline void 491static void
495kset_system_key_syms (struct kboard *kb, Lisp_Object val) 492kset_system_key_syms (struct kboard *kb, Lisp_Object val)
496{ 493{
497 kb->INTERNAL_FIELD (system_key_syms) = val; 494 kb->INTERNAL_FIELD (system_key_syms) = val;
@@ -823,7 +820,7 @@ This function is called by the editor initialization to begin editing. */)
823 820
824 /* If we enter while input is blocked, don't lock up here. 821 /* If we enter while input is blocked, don't lock up here.
825 This may happen through the debugger during redisplay. */ 822 This may happen through the debugger during redisplay. */
826 if (INPUT_BLOCKED_P) 823 if (input_blocked_p ())
827 return Qnil; 824 return Qnil;
828 825
829 command_loop_level++; 826 command_loop_level++;
@@ -1216,8 +1213,7 @@ This also exits all active minibuffers. */)
1216 1213
1217 /* Unblock input if we enter with input blocked. This may happen if 1214 /* Unblock input if we enter with input blocked. This may happen if
1218 redisplay traps e.g. during tool-bar update with input blocked. */ 1215 redisplay traps e.g. during tool-bar update with input blocked. */
1219 while (INPUT_BLOCKED_P) 1216 totally_unblock_input ();
1220 UNBLOCK_INPUT;
1221 1217
1222 Fthrow (Qtop_level, Qnil); 1218 Fthrow (Qtop_level, Qnil);
1223} 1219}
@@ -1460,15 +1456,6 @@ command_loop_1 (void)
1460 } 1456 }
1461 } 1457 }
1462 1458
1463#if 0
1464 /* Select the frame that the last event came from. Usually,
1465 switch-frame events will take care of this, but if some lisp
1466 code swallows a switch-frame event, we'll fix things up here.
1467 Is this a good idea? */
1468 if (FRAMEP (internal_last_event_frame)
1469 && !EQ (internal_last_event_frame, selected_frame))
1470 Fselect_frame (internal_last_event_frame, Qnil);
1471#endif
1472 /* If it has changed current-menubar from previous value, 1459 /* If it has changed current-menubar from previous value,
1473 really recompute the menubar from the value. */ 1460 really recompute the menubar from the value. */
1474 if (! NILP (Vlucid_menu_bar_dirty_flag) 1461 if (! NILP (Vlucid_menu_bar_dirty_flag)
@@ -2008,9 +1995,9 @@ static struct atimer *poll_timer;
2008void 1995void
2009poll_for_input_1 (void) 1996poll_for_input_1 (void)
2010{ 1997{
2011 if (interrupt_input_blocked == 0 1998 if (! input_blocked_p ()
2012 && !waiting_for_input) 1999 && !waiting_for_input)
2013 read_avail_input (0); 2000 gobble_input ();
2014} 2001}
2015 2002
2016/* Timer callback function for poll_timer. TIMER is equal to 2003/* Timer callback function for poll_timer. TIMER is equal to
@@ -2020,10 +2007,7 @@ static void
2020poll_for_input (struct atimer *timer) 2007poll_for_input (struct atimer *timer)
2021{ 2008{
2022 if (poll_suppress_count == 0) 2009 if (poll_suppress_count == 0)
2023 { 2010 pending_signals = 1;
2024 interrupt_input_pending = 1;
2025 pending_signals = 1;
2026 }
2027} 2011}
2028 2012
2029#endif /* POLL_FOR_INPUT */ 2013#endif /* POLL_FOR_INPUT */
@@ -2299,11 +2283,10 @@ read_char (int commandflag, ptrdiff_t nmaps, Lisp_Object *maps,
2299 Lisp_Object prev_event, 2283 Lisp_Object prev_event,
2300 int *used_mouse_menu, EMACS_TIME *end_time) 2284 int *used_mouse_menu, EMACS_TIME *end_time)
2301{ 2285{
2302 volatile Lisp_Object c; 2286 Lisp_Object c;
2303 ptrdiff_t jmpcount; 2287 ptrdiff_t jmpcount;
2304 sys_jmp_buf local_getcjmp; 2288 sys_jmp_buf local_getcjmp;
2305 sys_jmp_buf save_jump; 2289 sys_jmp_buf save_jump;
2306 volatile int key_already_recorded = 0;
2307 Lisp_Object tem, save; 2290 Lisp_Object tem, save;
2308 volatile Lisp_Object previous_echo_area_message; 2291 volatile Lisp_Object previous_echo_area_message;
2309 volatile Lisp_Object also_record; 2292 volatile Lisp_Object also_record;
@@ -2535,10 +2518,7 @@ read_char (int commandflag, ptrdiff_t nmaps, Lisp_Object *maps,
2535 return c; /* wrong_kboard_jmpbuf */ 2518 return c; /* wrong_kboard_jmpbuf */
2536 2519
2537 if (! NILP (c)) 2520 if (! NILP (c))
2538 { 2521 goto exit;
2539 key_already_recorded = 1;
2540 goto non_reread_1;
2541 }
2542 } 2522 }
2543 2523
2544 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet. 2524 /* Make a longjmp point for quits to use, but don't alter getcjmp just yet.
@@ -2866,12 +2846,10 @@ read_char (int commandflag, ptrdiff_t nmaps, Lisp_Object *maps,
2866 goto wrong_kboard; 2846 goto wrong_kboard;
2867 } 2847 }
2868 2848
2869 non_reread_1:
2870
2871 /* Buffer switch events are only for internal wakeups 2849 /* Buffer switch events are only for internal wakeups
2872 so don't show them to the user. 2850 so don't show them to the user.
2873 Also, don't record a key if we already did. */ 2851 Also, don't record a key if we already did. */
2874 if (BUFFERP (c) || key_already_recorded) 2852 if (BUFFERP (c))
2875 goto exit; 2853 goto exit;
2876 2854
2877 /* Process special events within read_char 2855 /* Process special events within read_char
@@ -3344,7 +3322,7 @@ record_char (Lisp_Object c)
3344 If you, dear reader, have a better idea, you've got the source. :-) */ 3322 If you, dear reader, have a better idea, you've got the source. :-) */
3345 if (dribble) 3323 if (dribble)
3346 { 3324 {
3347 BLOCK_INPUT; 3325 block_input ();
3348 if (INTEGERP (c)) 3326 if (INTEGERP (c))
3349 { 3327 {
3350 if (XUINT (c) < 0x100) 3328 if (XUINT (c) < 0x100)
@@ -3370,7 +3348,7 @@ record_char (Lisp_Object c)
3370 } 3348 }
3371 3349
3372 fflush (dribble); 3350 fflush (dribble);
3373 UNBLOCK_INPUT; 3351 unblock_input ();
3374 } 3352 }
3375} 3353}
3376 3354
@@ -3514,10 +3492,8 @@ kbd_buffer_store_event (register struct input_event *event)
3514 Else, if EVENT is a quit event, store the quit event 3492 Else, if EVENT is a quit event, store the quit event
3515 in HOLD_QUIT, and return (thus ignoring further events). 3493 in HOLD_QUIT, and return (thus ignoring further events).
3516 3494
3517 This is used in read_avail_input to postpone the processing 3495 This is used to postpone the processing of the quit event until all
3518 of the quit event until all subsequent input events have been 3496 subsequent input events have been parsed (and discarded). */
3519 parsed (and discarded).
3520 */
3521 3497
3522void 3498void
3523kbd_buffer_store_event_hold (register struct input_event *event, 3499kbd_buffer_store_event_hold (register struct input_event *event,
@@ -3587,7 +3563,8 @@ kbd_buffer_store_event_hold (register struct input_event *event,
3587 } 3563 }
3588 3564
3589 last_event_timestamp = event->timestamp; 3565 last_event_timestamp = event->timestamp;
3590 handle_interrupt (); 3566
3567 handle_interrupt (0);
3591 return; 3568 return;
3592 } 3569 }
3593 3570
@@ -3643,7 +3620,6 @@ kbd_buffer_store_event_hold (register struct input_event *event,
3643 if (immediate_quit && NILP (Vinhibit_quit)) 3620 if (immediate_quit && NILP (Vinhibit_quit))
3644 { 3621 {
3645 immediate_quit = 0; 3622 immediate_quit = 0;
3646 pthread_sigmask (SIG_SETMASK, &empty_mask, 0);
3647 QUIT; 3623 QUIT;
3648 } 3624 }
3649 } 3625 }
@@ -3767,7 +3743,7 @@ kbd_buffer_events_waiting (int discard)
3767 3743
3768/* Clear input event EVENT. */ 3744/* Clear input event EVENT. */
3769 3745
3770static inline void 3746static void
3771clear_event (struct input_event *event) 3747clear_event (struct input_event *event)
3772{ 3748{
3773 event->kind = NO_EVENT; 3749 event->kind = NO_EVENT;
@@ -3793,14 +3769,6 @@ kbd_buffer_get_event (KBOARD **kbp,
3793 /* Start reading input again because we have processed enough to 3769 /* Start reading input again because we have processed enough to
3794 be able to accept new events again. */ 3770 be able to accept new events again. */
3795 unhold_keyboard_input (); 3771 unhold_keyboard_input ();
3796#ifdef USABLE_SIGIO
3797 if (!noninteractive)
3798 {
3799 struct sigaction action;
3800 emacs_sigaction_init (&action, deliver_input_available_signal);
3801 sigaction (SIGIO, &action, 0);
3802 }
3803#endif
3804 start_polling (); 3772 start_polling ();
3805 } 3773 }
3806#endif /* subprocesses */ 3774#endif /* subprocesses */
@@ -3843,7 +3811,7 @@ kbd_buffer_get_event (KBOARD **kbp,
3843 interrupt handlers have not read it, read it now. */ 3811 interrupt handlers have not read it, read it now. */
3844 3812
3845#ifdef USABLE_SIGIO 3813#ifdef USABLE_SIGIO
3846 gobble_input (0); 3814 gobble_input ();
3847#endif 3815#endif
3848 if (kbd_fetch_ptr != kbd_store_ptr) 3816 if (kbd_fetch_ptr != kbd_store_ptr)
3849 break; 3817 break;
@@ -3869,8 +3837,7 @@ kbd_buffer_get_event (KBOARD **kbp,
3869 wait_reading_process_output (0, 0, -1, 1, Qnil, NULL, 0); 3837 wait_reading_process_output (0, 0, -1, 1, Qnil, NULL, 0);
3870 3838
3871 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr) 3839 if (!interrupt_input && kbd_fetch_ptr == kbd_store_ptr)
3872 /* Pass 1 for EXPECT since we just waited to have input. */ 3840 gobble_input ();
3873 read_avail_input (1);
3874 } 3841 }
3875 3842
3876 if (CONSP (Vunread_command_events)) 3843 if (CONSP (Vunread_command_events))
@@ -4334,25 +4301,18 @@ decode_timer (Lisp_Object timer, EMACS_TIME *result)
4334 should be done. */ 4301 should be done. */
4335 4302
4336static EMACS_TIME 4303static EMACS_TIME
4337timer_check_2 (void) 4304timer_check_2 (Lisp_Object timers, Lisp_Object idle_timers)
4338{ 4305{
4339 EMACS_TIME nexttime; 4306 EMACS_TIME nexttime;
4340 EMACS_TIME now; 4307 EMACS_TIME now;
4341 EMACS_TIME idleness_now; 4308 EMACS_TIME idleness_now;
4342 Lisp_Object timers, idle_timers, chosen_timer; 4309 Lisp_Object chosen_timer;
4343 struct gcpro gcpro1, gcpro2, gcpro3; 4310 struct gcpro gcpro1;
4344 4311
4345 nexttime = invalid_emacs_time (); 4312 nexttime = invalid_emacs_time ();
4346 4313
4347 /* Always consider the ordinary timers. */
4348 timers = Vtimer_list;
4349 /* Consider the idle timers only if Emacs is idle. */
4350 if (EMACS_TIME_VALID_P (timer_idleness_start_time))
4351 idle_timers = Vtimer_idle_list;
4352 else
4353 idle_timers = Qnil;
4354 chosen_timer = Qnil; 4314 chosen_timer = Qnil;
4355 GCPRO3 (timers, idle_timers, chosen_timer); 4315 GCPRO1 (chosen_timer);
4356 4316
4357 /* First run the code that was delayed. */ 4317 /* First run the code that was delayed. */
4358 while (CONSP (pending_funcalls)) 4318 while (CONSP (pending_funcalls))
@@ -4501,13 +4461,35 @@ EMACS_TIME
4501timer_check (void) 4461timer_check (void)
4502{ 4462{
4503 EMACS_TIME nexttime; 4463 EMACS_TIME nexttime;
4464 Lisp_Object timers, idle_timers;
4465 struct gcpro gcpro1, gcpro2;
4466
4467 Lisp_Object tem = Vinhibit_quit;
4468 Vinhibit_quit = Qt;
4469
4470 /* We use copies of the timers' lists to allow a timer to add itself
4471 again, without locking up Emacs if the newly added timer is
4472 already ripe when added. */
4473
4474 /* Always consider the ordinary timers. */
4475 timers = Fcopy_sequence (Vtimer_list);
4476 /* Consider the idle timers only if Emacs is idle. */
4477 if (EMACS_TIME_VALID_P (timer_idleness_start_time))
4478 idle_timers = Fcopy_sequence (Vtimer_idle_list);
4479 else
4480 idle_timers = Qnil;
4481
4482 Vinhibit_quit = tem;
4483
4484 GCPRO2 (timers, idle_timers);
4504 4485
4505 do 4486 do
4506 { 4487 {
4507 nexttime = timer_check_2 (); 4488 nexttime = timer_check_2 (timers, idle_timers);
4508 } 4489 }
4509 while (EMACS_SECS (nexttime) == 0 && EMACS_NSECS (nexttime) == 0); 4490 while (EMACS_SECS (nexttime) == 0 && EMACS_NSECS (nexttime) == 0);
4510 4491
4492 UNGCPRO;
4511 return nexttime; 4493 return nexttime;
4512} 4494}
4513 4495
@@ -6734,45 +6716,10 @@ get_input_pending (int *addr, int flags)
6734 return; 6716 return;
6735 6717
6736 /* Try to read some input and see how much we get. */ 6718 /* Try to read some input and see how much we get. */
6737 gobble_input (0); 6719 gobble_input ();
6738 *addr = (!NILP (Vquit_flag) || readable_events (flags)); 6720 *addr = (!NILP (Vquit_flag) || readable_events (flags));
6739} 6721}
6740 6722
6741/* Interface to read_avail_input, blocking SIGIO or SIGALRM if necessary. */
6742
6743void
6744gobble_input (int expected)
6745{
6746#ifdef USABLE_SIGIO
6747 if (interrupt_input)
6748 {
6749 sigset_t blocked, procmask;
6750 sigemptyset (&blocked);
6751 sigaddset (&blocked, SIGIO);
6752 pthread_sigmask (SIG_BLOCK, &blocked, &procmask);
6753 read_avail_input (expected);
6754 pthread_sigmask (SIG_SETMASK, &procmask, 0);
6755 }
6756 else
6757#ifdef POLL_FOR_INPUT
6758 /* XXX This condition was (read_socket_hook && !interrupt_input),
6759 but read_socket_hook is not global anymore. Let's pretend that
6760 it's always set. */
6761 if (!interrupt_input && poll_suppress_count == 0)
6762 {
6763 sigset_t blocked, procmask;
6764 sigemptyset (&blocked);
6765 sigaddset (&blocked, SIGALRM);
6766 pthread_sigmask (SIG_BLOCK, &blocked, &procmask);
6767 read_avail_input (expected);
6768 pthread_sigmask (SIG_SETMASK, &procmask, 0);
6769 }
6770 else
6771#endif
6772#endif
6773 read_avail_input (expected);
6774}
6775
6776/* Put a BUFFER_SWITCH_EVENT in the buffer 6723/* Put a BUFFER_SWITCH_EVENT in the buffer
6777 so that read_key_sequence will notice the new current buffer. */ 6724 so that read_key_sequence will notice the new current buffer. */
6778 6725
@@ -6800,14 +6747,7 @@ record_asynch_buffer_change (void)
6800 /* Make sure no interrupt happens while storing the event. */ 6747 /* Make sure no interrupt happens while storing the event. */
6801#ifdef USABLE_SIGIO 6748#ifdef USABLE_SIGIO
6802 if (interrupt_input) 6749 if (interrupt_input)
6803 { 6750 kbd_buffer_store_event (&event);
6804 sigset_t blocked, procmask;
6805 sigemptyset (&blocked);
6806 sigaddset (&blocked, SIGIO);
6807 pthread_sigmask (SIG_BLOCK, &blocked, &procmask);
6808 kbd_buffer_store_event (&event);
6809 pthread_sigmask (SIG_SETMASK, &procmask, 0);
6810 }
6811 else 6751 else
6812#endif 6752#endif
6813 { 6753 {
@@ -6820,21 +6760,18 @@ record_asynch_buffer_change (void)
6820/* Read any terminal input already buffered up by the system 6760/* Read any terminal input already buffered up by the system
6821 into the kbd_buffer, but do not wait. 6761 into the kbd_buffer, but do not wait.
6822 6762
6823 EXPECTED should be nonzero if the caller knows there is some input. 6763 Return the number of keyboard chars read, or -1 meaning
6824
6825 Returns the number of keyboard chars read, or -1 meaning
6826 this is a bad time to try to read input. */ 6764 this is a bad time to try to read input. */
6827 6765
6828static int 6766int
6829read_avail_input (int expected) 6767gobble_input (void)
6830{ 6768{
6831 int nread = 0; 6769 int nread = 0;
6832 int err = 0; 6770 int err = 0;
6833 struct terminal *t; 6771 struct terminal *t;
6834 6772
6835 /* Store pending user signal events, if any. */ 6773 /* Store pending user signal events, if any. */
6836 if (store_user_signal_events ()) 6774 store_user_signal_events ();
6837 expected = 0;
6838 6775
6839 /* Loop through the available terminals, and call their input hooks. */ 6776 /* Loop through the available terminals, and call their input hooks. */
6840 t = terminal_list; 6777 t = terminal_list;
@@ -6847,15 +6784,18 @@ read_avail_input (int expected)
6847 int nr; 6784 int nr;
6848 struct input_event hold_quit; 6785 struct input_event hold_quit;
6849 6786
6787 if (input_blocked_p ())
6788 {
6789 pending_signals = 1;
6790 break;
6791 }
6792
6850 EVENT_INIT (hold_quit); 6793 EVENT_INIT (hold_quit);
6851 hold_quit.kind = NO_EVENT; 6794 hold_quit.kind = NO_EVENT;
6852 6795
6853 /* No need for FIONREAD or fcntl; just say don't wait. */ 6796 /* No need for FIONREAD or fcntl; just say don't wait. */
6854 while (nr = (*t->read_socket_hook) (t, expected, &hold_quit), nr > 0) 6797 while (0 < (nr = (*t->read_socket_hook) (t, &hold_quit)))
6855 { 6798 nread += nr;
6856 nread += nr;
6857 expected = 0;
6858 }
6859 6799
6860 if (nr == -1) /* Not OK to read input now. */ 6800 if (nr == -1) /* Not OK to read input now. */
6861 { 6801 {
@@ -6875,7 +6815,7 @@ read_avail_input (int expected)
6875 this process rather than to the whole process 6815 this process rather than to the whole process
6876 group? Perhaps on systems with FIONREAD Emacs is 6816 group? Perhaps on systems with FIONREAD Emacs is
6877 alone in its group. */ 6817 alone in its group. */
6878 kill (getpid (), SIGHUP); 6818 terminate_due_to_signal (SIGHUP, 10);
6879 6819
6880 /* XXX Is calling delete_terminal safe here? It calls delete_frame. */ 6820 /* XXX Is calling delete_terminal safe here? It calls delete_frame. */
6881 { 6821 {
@@ -6950,7 +6890,6 @@ decode_keyboard_code (struct tty_display_info *tty,
6950 6890
6951int 6891int
6952tty_read_avail_input (struct terminal *terminal, 6892tty_read_avail_input (struct terminal *terminal,
6953 int expected,
6954 struct input_event *hold_quit) 6893 struct input_event *hold_quit)
6955{ 6894{
6956 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than 6895 /* Using KBD_BUFFER_SIZE - 1 here avoids reading more than
@@ -7165,35 +7104,68 @@ tty_read_avail_input (struct terminal *terminal,
7165static void 7104static void
7166handle_async_input (void) 7105handle_async_input (void)
7167{ 7106{
7168 interrupt_input_pending = 0; 7107#ifdef USABLE_SIGIO
7169 pending_signals = pending_atimers;
7170
7171 while (1) 7108 while (1)
7172 { 7109 {
7173 int nread; 7110 int nread = gobble_input ();
7174 nread = read_avail_input (1);
7175 /* -1 means it's not ok to read the input now. 7111 /* -1 means it's not ok to read the input now.
7176 UNBLOCK_INPUT will read it later; now, avoid infinite loop. 7112 UNBLOCK_INPUT will read it later; now, avoid infinite loop.
7177 0 means there was no keyboard input available. */ 7113 0 means there was no keyboard input available. */
7178 if (nread <= 0) 7114 if (nread <= 0)
7179 break; 7115 break;
7180 } 7116 }
7117#endif
7181} 7118}
7182 7119
7183void 7120void
7184process_pending_signals (void) 7121process_pending_signals (void)
7185{ 7122{
7186 if (interrupt_input_pending) 7123 pending_signals = 0;
7187 handle_async_input (); 7124 handle_async_input ();
7188 do_pending_atimers (); 7125 do_pending_atimers ();
7189} 7126}
7190 7127
7128/* Undo any number of BLOCK_INPUT calls down to level LEVEL,
7129 and also (if the level is now 0) reinvoke any pending signal. */
7130
7131void
7132unblock_input_to (int level)
7133{
7134 interrupt_input_blocked = level;
7135 if (level == 0)
7136 {
7137 if (pending_signals)
7138 process_pending_signals ();
7139 }
7140 else if (level < 0)
7141 emacs_abort ();
7142}
7143
7144/* End critical section.
7145
7146 If doing signal-driven input, and a signal came in when input was
7147 blocked, reinvoke the signal handler now to deal with it. */
7148
7149void
7150unblock_input (void)
7151{
7152 unblock_input_to (interrupt_input_blocked - 1);
7153}
7154
7155/* Undo any number of BLOCK_INPUT calls,
7156 and also reinvoke any pending signal. */
7157
7158void
7159totally_unblock_input (void)
7160{
7161 unblock_input_to (0);
7162}
7163
7191#ifdef USABLE_SIGIO 7164#ifdef USABLE_SIGIO
7192 7165
7193static void 7166void
7194handle_input_available_signal (int sig) 7167handle_input_available_signal (int sig)
7195{ 7168{
7196 interrupt_input_pending = 1;
7197 pending_signals = 1; 7169 pending_signals = 1;
7198 7170
7199 if (input_available_clear_time) 7171 if (input_available_clear_time)
@@ -7203,25 +7175,10 @@ handle_input_available_signal (int sig)
7203static void 7175static void
7204deliver_input_available_signal (int sig) 7176deliver_input_available_signal (int sig)
7205{ 7177{
7206 handle_on_main_thread (sig, handle_input_available_signal); 7178 deliver_process_signal (sig, handle_input_available_signal);
7207} 7179}
7208#endif /* USABLE_SIGIO */ 7180#endif /* USABLE_SIGIO */
7209 7181
7210/* Send ourselves a SIGIO.
7211
7212 This function exists so that the UNBLOCK_INPUT macro in
7213 blockinput.h can have some way to take care of input we put off
7214 dealing with, without assuming that every file which uses
7215 UNBLOCK_INPUT also has #included the files necessary to get SIGIO. */
7216void
7217reinvoke_input_signal (void)
7218{
7219#ifdef USABLE_SIGIO
7220 handle_async_input ();
7221#endif
7222}
7223
7224
7225 7182
7226/* User signal events. */ 7183/* User signal events. */
7227 7184
@@ -7292,7 +7249,7 @@ handle_user_signal (int sig)
7292 p->npending++; 7249 p->npending++;
7293#ifdef USABLE_SIGIO 7250#ifdef USABLE_SIGIO
7294 if (interrupt_input) 7251 if (interrupt_input)
7295 kill (getpid (), SIGIO); 7252 handle_input_available_signal (sig);
7296 else 7253 else
7297#endif 7254#endif
7298 { 7255 {
@@ -7308,7 +7265,7 @@ handle_user_signal (int sig)
7308static void 7265static void
7309deliver_user_signal (int sig) 7266deliver_user_signal (int sig)
7310{ 7267{
7311 handle_on_main_thread (sig, handle_user_signal); 7268 deliver_process_signal (sig, handle_user_signal);
7312} 7269}
7313 7270
7314static char * 7271static char *
@@ -7323,29 +7280,23 @@ find_user_signal_name (int sig)
7323 return NULL; 7280 return NULL;
7324} 7281}
7325 7282
7326static int 7283static void
7327store_user_signal_events (void) 7284store_user_signal_events (void)
7328{ 7285{
7329 struct user_signal_info *p; 7286 struct user_signal_info *p;
7330 struct input_event buf; 7287 struct input_event buf;
7331 int nstored = 0; 7288 bool buf_initialized = 0;
7332 7289
7333 for (p = user_signals; p; p = p->next) 7290 for (p = user_signals; p; p = p->next)
7334 if (p->npending > 0) 7291 if (p->npending > 0)
7335 { 7292 {
7336 sigset_t blocked, procmask; 7293 if (! buf_initialized)
7337
7338 if (nstored == 0)
7339 { 7294 {
7340 memset (&buf, 0, sizeof buf); 7295 memset (&buf, 0, sizeof buf);
7341 buf.kind = USER_SIGNAL_EVENT; 7296 buf.kind = USER_SIGNAL_EVENT;
7342 buf.frame_or_window = selected_frame; 7297 buf.frame_or_window = selected_frame;
7298 buf_initialized = 1;
7343 } 7299 }
7344 nstored += p->npending;
7345
7346 sigemptyset (&blocked);
7347 sigaddset (&blocked, p->sig);
7348 pthread_sigmask (SIG_BLOCK, &blocked, &procmask);
7349 7300
7350 do 7301 do
7351 { 7302 {
@@ -7354,11 +7305,7 @@ store_user_signal_events (void)
7354 p->npending--; 7305 p->npending--;
7355 } 7306 }
7356 while (p->npending > 0); 7307 while (p->npending > 0);
7357
7358 pthread_sigmask (SIG_SETMASK, &procmask, 0);
7359 } 7308 }
7360
7361 return nstored;
7362} 7309}
7363 7310
7364 7311
@@ -8101,7 +8048,7 @@ process_tool_bar_item (Lisp_Object key, Lisp_Object def, Lisp_Object data, void
8101 8048
8102/* Access slot with index IDX of vector tool_bar_item_properties. */ 8049/* Access slot with index IDX of vector tool_bar_item_properties. */
8103#define PROP(IDX) AREF (tool_bar_item_properties, (IDX)) 8050#define PROP(IDX) AREF (tool_bar_item_properties, (IDX))
8104static inline void 8051static void
8105set_prop (ptrdiff_t idx, Lisp_Object val) 8052set_prop (ptrdiff_t idx, Lisp_Object val)
8106{ 8053{
8107 ASET (tool_bar_item_properties, idx, val); 8054 ASET (tool_bar_item_properties, idx, val);
@@ -10562,9 +10509,9 @@ The file will be closed when Emacs exits. */)
10562{ 10509{
10563 if (dribble) 10510 if (dribble)
10564 { 10511 {
10565 BLOCK_INPUT; 10512 block_input ();
10566 fclose (dribble); 10513 fclose (dribble);
10567 UNBLOCK_INPUT; 10514 unblock_input ();
10568 dribble = 0; 10515 dribble = 0;
10569 } 10516 }
10570 if (!NILP (file)) 10517 if (!NILP (file))
@@ -10753,21 +10700,21 @@ handle_interrupt_signal (int sig)
10753 from the controlling tty. */ 10700 from the controlling tty. */
10754 internal_last_event_frame = terminal->display_info.tty->top_frame; 10701 internal_last_event_frame = terminal->display_info.tty->top_frame;
10755 10702
10756 handle_interrupt (); 10703 handle_interrupt (1);
10757 } 10704 }
10758} 10705}
10759 10706
10760static void 10707static void
10761deliver_interrupt_signal (int sig) 10708deliver_interrupt_signal (int sig)
10762{ 10709{
10763 handle_on_main_thread (sig, handle_interrupt_signal); 10710 deliver_process_signal (sig, handle_interrupt_signal);
10764} 10711}
10765 10712
10766 10713
10767/* If Emacs is stuck because `inhibit-quit' is true, then keep track 10714/* If Emacs is stuck because `inhibit-quit' is true, then keep track
10768 of the number of times C-g has been requested. If C-g is pressed 10715 of the number of times C-g has been requested. If C-g is pressed
10769 enough times, then quit anyway. See bug#6585. */ 10716 enough times, then quit anyway. See bug#6585. */
10770static int force_quit_count; 10717static int volatile force_quit_count;
10771 10718
10772/* This routine is called at interrupt level in response to C-g. 10719/* This routine is called at interrupt level in response to C-g.
10773 10720
@@ -10781,7 +10728,7 @@ static int force_quit_count;
10781 non-nil, it stops the job right away. */ 10728 non-nil, it stops the job right away. */
10782 10729
10783static void 10730static void
10784handle_interrupt (void) 10731handle_interrupt (bool in_signal_handler)
10785{ 10732{
10786 char c; 10733 char c;
10787 10734
@@ -10790,13 +10737,16 @@ handle_interrupt (void)
10790 /* XXX This code needs to be revised for multi-tty support. */ 10737 /* XXX This code needs to be revised for multi-tty support. */
10791 if (!NILP (Vquit_flag) && get_named_tty ("/dev/tty")) 10738 if (!NILP (Vquit_flag) && get_named_tty ("/dev/tty"))
10792 { 10739 {
10793 /* If SIGINT isn't blocked, don't let us be interrupted by 10740 if (! in_signal_handler)
10794 another SIGINT, it might be harmful due to non-reentrancy 10741 {
10795 in I/O functions. */ 10742 /* If SIGINT isn't blocked, don't let us be interrupted by
10796 sigset_t blocked; 10743 a SIGINT. It might be harmful due to non-reentrancy
10797 sigemptyset (&blocked); 10744 in I/O functions. */
10798 sigaddset (&blocked, SIGINT); 10745 sigset_t blocked;
10799 pthread_sigmask (SIG_BLOCK, &blocked, 0); 10746 sigemptyset (&blocked);
10747 sigaddset (&blocked, SIGINT);
10748 pthread_sigmask (SIG_BLOCK, &blocked, 0);
10749 }
10800 10750
10801 fflush (stdout); 10751 fflush (stdout);
10802 reset_all_sys_modes (); 10752 reset_all_sys_modes ();
@@ -10867,7 +10817,6 @@ handle_interrupt (void)
10867#endif /* not MSDOS */ 10817#endif /* not MSDOS */
10868 fflush (stdout); 10818 fflush (stdout);
10869 init_all_sys_modes (); 10819 init_all_sys_modes ();
10870 pthread_sigmask (SIG_SETMASK, &empty_mask, 0);
10871 } 10820 }
10872 else 10821 else
10873 { 10822 {
@@ -10885,15 +10834,14 @@ handle_interrupt (void)
10885 GCPRO4 (saved.object, saved.global_code, 10834 GCPRO4 (saved.object, saved.global_code,
10886 saved.current_syntax_table, saved.old_prop); 10835 saved.current_syntax_table, saved.old_prop);
10887 Fsignal (Qquit, Qnil); 10836 Fsignal (Qquit, Qnil);
10888 /* FIXME: AFAIK, `quit' can never return, so this code is dead! */
10889 gl_state = saved; 10837 gl_state = saved;
10890 UNGCPRO; 10838 UNGCPRO;
10891 } 10839 }
10892 else 10840 else
10893 { /* Else request quit when it's safe. */ 10841 { /* Else request quit when it's safe. */
10894 if (NILP (Vquit_flag)) 10842 int count = NILP (Vquit_flag) ? 1 : force_quit_count + 1;
10895 force_quit_count = 0; 10843 force_quit_count = count;
10896 if (++force_quit_count == 3) 10844 if (count == 3)
10897 { 10845 {
10898 immediate_quit = 1; 10846 immediate_quit = 1;
10899 Vinhibit_quit = Qnil; 10847 Vinhibit_quit = Qnil;
@@ -10902,6 +10850,8 @@ handle_interrupt (void)
10902 } 10850 }
10903 } 10851 }
10904 10852
10853 pthread_sigmask (SIG_SETMASK, &empty_mask, 0);
10854
10905/* TODO: The longjmp in this call throws the NS event loop integration off, 10855/* TODO: The longjmp in this call throws the NS event loop integration off,
10906 and it seems to do fine without this. Probably some attention 10856 and it seems to do fine without this. Probably some attention
10907 needs to be paid to the setting of waiting_for_input in 10857 needs to be paid to the setting of waiting_for_input in
@@ -10911,7 +10861,7 @@ handle_interrupt (void)
10911 separate event loop thread like W32. */ 10861 separate event loop thread like W32. */
10912#ifndef HAVE_NS 10862#ifndef HAVE_NS
10913 if (waiting_for_input && !echoing) 10863 if (waiting_for_input && !echoing)
10914 quit_throw_to_read_char (1); 10864 quit_throw_to_read_char (in_signal_handler);
10915#endif 10865#endif
10916} 10866}
10917 10867
@@ -10925,22 +10875,12 @@ quit_throw_to_read_char (int from_signal)
10925 if (!from_signal && EQ (Vquit_flag, Qkill_emacs)) 10875 if (!from_signal && EQ (Vquit_flag, Qkill_emacs))
10926 Fkill_emacs (Qnil); 10876 Fkill_emacs (Qnil);
10927 10877
10928 pthread_sigmask (SIG_SETMASK, &empty_mask, 0);
10929 /* Prevent another signal from doing this before we finish. */ 10878 /* Prevent another signal from doing this before we finish. */
10930 clear_waiting_for_input (); 10879 clear_waiting_for_input ();
10931 input_pending = 0; 10880 input_pending = 0;
10932 10881
10933 Vunread_command_events = Qnil; 10882 Vunread_command_events = Qnil;
10934 10883
10935#if 0 /* Currently, sit_for is called from read_char without turning
10936 off polling. And that can call set_waiting_for_input.
10937 It seems to be harmless. */
10938#ifdef POLL_FOR_INPUT
10939 /* May be > 1 if in recursive minibuffer. */
10940 if (poll_suppress_count == 0)
10941 emacs_abort ();
10942#endif
10943#endif
10944 if (FRAMEP (internal_last_event_frame) 10884 if (FRAMEP (internal_last_event_frame)
10945 && !EQ (internal_last_event_frame, selected_frame)) 10885 && !EQ (internal_last_event_frame, selected_frame))
10946 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame), 10886 do_switch_frame (make_lispy_switch_frame (internal_last_event_frame),
@@ -11331,7 +11271,6 @@ init_keyboard (void)
11331#endif 11271#endif
11332 input_pending = 0; 11272 input_pending = 0;
11333 interrupt_input_blocked = 0; 11273 interrupt_input_blocked = 0;
11334 interrupt_input_pending = 0;
11335 pending_signals = 0; 11274 pending_signals = 0;
11336 11275
11337 /* This means that command_loop_1 won't try to select anything the first 11276 /* This means that command_loop_1 won't try to select anything the first
@@ -11352,7 +11291,7 @@ init_keyboard (void)
11352 /* Before multi-tty support, these handlers used to be installed 11291 /* Before multi-tty support, these handlers used to be installed
11353 only if the current session was a tty session. Now an Emacs 11292 only if the current session was a tty session. Now an Emacs
11354 session may have multiple display types, so we always handle 11293 session may have multiple display types, so we always handle
11355 SIGINT. There is special code in interrupt_signal to exit 11294 SIGINT. There is special code in handle_interrupt_signal to exit
11356 Emacs on SIGINT when there are no termcap frames on the 11295 Emacs on SIGINT when there are no termcap frames on the
11357 controlling terminal. */ 11296 controlling terminal. */
11358 struct sigaction action; 11297 struct sigaction action;