###############################################################################
# BRLTTY - A background process providing access to the console screen (when in
#          text mode) for a blind person using a refreshable braille display.
#
# Copyright (C) 1995-2025 by The BRLTTY Developers.
#
# BRLTTY comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU Lesser General Public License, as published by the Free Software
# Foundation; either version 2.1 of the License, or (at your option) any
# later version. Please see the file LICENSE-LGPL for details.
#
# Web Page: http://brltty.app/
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################

#########################################################################
# Udev rules for starting BRLTTY when a USB braille device is connected #
#########################################################################

SUBSYSTEM=="usb_device", GOTO="brltty_usb_begin"
SUBSYSTEM=="usb", KERNEL!="*:*", GOTO="brltty_usb_begin"
GOTO="brltty_device_end"

LABEL="brltty_usb_begin"
# BEGIN_USB_BRAILLE_DEVICES
# END_USB_BRAILLE_DEVICES
GOTO="brltty_device_end"

LABEL="brltty_usb_run"
SYMLINK+="brltty/USB-Mfr:$attr{manufacturer}-Prd:$attr{product}-Ser:$attr{serial}"
ENV{BRLTTY_BRAILLE_DEVICE}="usb:vendor=0X$sysfs{idVendor}+product=0X$sysfs{idProduct}+serial=$sysfs{serial}"
GOTO="brltty_device_run"

LABEL="brltty_device_run"
TEST=="/sys/fs/cgroup/systemd", GOTO="brltty_systemd"
TEST=="/run/systemd/system", GOTO="brltty_systemd"
ENV{BRLTTY_PID_FILE}="/run/brltty/brltty.$kernel.pid"

TEST=="/etc/init", GOTO="brltty_upstart"
TEST=="/etc/event.d", GOTO="brltty_upstart"

ACTION=="add", GOTO="brltty_udev_add"
ACTION=="remove", GOTO="brltty_udev_remove"
GOTO="brltty_device_end"

LABEL="brltty_udev_add"
RUN+="@HELPERS_DIRECTORY@/udev-wrapper"
GOTO="brltty_device_end"

LABEL="brltty_udev_remove"
RUN+="@HELPERS_DIRECTORY@/udev-wrapper -C"
GOTO="brltty_device_end"

LABEL="brltty_upstart"
ACTION=="add", GOTO="brltty_upstart_start"
ACTION=="remove", GOTO="brltty_upstart_stop"
GOTO="brltty_device_end"

LABEL="brltty_upstart_start"
PROGRAM="/sbin/initctl status brltty", RESULT=="*running*", GOTO="brltty_upstart_restart"
RUN+="/sbin/initctl start --quiet --no-wait brltty 'BRLTTY_BRAILLE_DEVICE=$env{BRLTTY_BRAILLE_DEVICE}' 'BRLTTY_BRAILLE_DRIVER=$env{BRLTTY_BRAILLE_DRIVER}'"
GOTO="brltty_device_end"

LABEL="brltty_upstart_restart"
RUN+="/sbin/initctl restart --quiet --no-wait brltty 'BRLTTY_BRAILLE_DEVICE=$env{BRLTTY_BRAILLE_DEVICE}' 'BRLTTY_BRAILLE_DRIVER=$env{BRLTTY_BRAILLE_DRIVER}'"
GOTO="brltty_device_end"

LABEL="brltty_upstart_stop"
RUN+="/sbin/initctl stop --quiet --no-wait brltty"
GOTO="brltty_device_end"

LABEL="brltty_systemd"
TAG+="systemd"
ACTION=="add", GOTO="brltty_systemd_add"
GOTO="brltty_device_end"

LABEL="brltty_systemd_add"
ENV{SYSTEMD_WANTS}+="brltty-device@.service"
GOTO="brltty_device_end"

LABEL="brltty_device_end"
