// Simulates the user pressing F6 in the active X11 session.
// F6 -> IDC_FOCUS_NEXT_PANE -> RotatePaneFocus -> the screen-reader-announced
// way to focus the inactive download bubble. This is the keypress that
// reaches BrowserFocusControllerDelegateViews::
// ActivateFirstInactiveBubbleForAccessibility() and dereferences the stale
// initially_focused_view pointer.
//
// Build:  gcc send_f6.c -o send_f6 -lX11 -lXtst
// Run:    ./send_f6 [count]

#include <X11/Xlib.h>
#include <X11/extensions/XTest.h>
#include <X11/keysym.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char** argv) {
  int count = (argc > 1) ? atoi(argv[1]) : 3;
  Display* d = XOpenDisplay(NULL);
  if (!d) {
    fprintf(stderr, "send_f6: cannot open display\n");
    return 1;
  }
  KeyCode kc = XKeysymToKeycode(d, XK_F6);
  for (int i = 0; i < count; ++i) {
    XTestFakeKeyEvent(d, kc, True, CurrentTime);
    XTestFakeKeyEvent(d, kc, False, CurrentTime);
    XFlush(d);
    fprintf(stderr, "send_f6: sent F6 (%d/%d)\n", i + 1, count);
    usleep(150 * 1000);
  }
  XCloseDisplay(d);
  return 0;
}
