dbuschat.tk 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env wish
  2. # Chat input line for use with the Cool VL Viewer D-Bus interface for Lua
  3. # (c)2024 Henri Beauchamp. This script is provided under the GPL license.
  4. #
  5. # Note: either the dbus-tcl package or the dbus-send command must be installed
  6. # on the system for this script to work. The viewer must be running (with a
  7. # user logged in) so that its D-Bus server runs and accepts commands. It also
  8. # must be configured to accept Lua commands via D-Bus (via the menu option
  9. # "Advanced" -> "Lua scripting" -> "Accept Lua commands from D-Bus").
  10. if {$tcl_version < 8.4} {
  11. tk_messageBox -type ok -icon error \
  12. -message "This program needs Tcl/Tk v8.4 or newer.\nThe current version is: $tcl_version"
  13. exit
  14. }
  15. set DbusDestination "com.secondlife.ViewerAppAPIService"
  16. set DbusPath "/com/secondlife/ViewerAppAPI"
  17. set DbusInterface "com.secondlife.ViewerAppAPI"
  18. set DbusMethod "LuaExec"
  19. set dbuscmd ""
  20. if {[catch {package require dbus} result]} {
  21. set dbuscmd "dbus-send"
  22. }
  23. wm title . "Cool VL Viewer chat input"
  24. # Attempt to set an application icon for this Tk window. Note: PNG format only
  25. # supported in Tk v8.6 and newer.
  26. if {$tcl_version >= 8.6} {
  27. catch {
  28. set script_path [file dirname [file normalize [info script]]]
  29. set icon_file [file join $script_path "cvlv_icon.png"]
  30. image create photo .i1 -format png -file $icon_file
  31. wm iconphoto . -default .i1
  32. #wm withdraw .
  33. #wm state . normal
  34. }
  35. }
  36. set range 0
  37. set animate 0
  38. proc sendchat {emote} {
  39. global dbuscmd range animate DbusDestination DbusPath DbusInterface DbusMethod
  40. set chattext [.input.text get 1.0 end]
  41. if {[string trim $chattext] == ""} {
  42. return
  43. }
  44. set chattext [string map { \[\[ (( \]\] )) } $chattext]
  45. if {$emote} {
  46. set chattext "/me $chattext"
  47. }
  48. set type ""
  49. if {$range == -1} {
  50. set type "whisper"
  51. } elseif {$range == 1} {
  52. set type "shout"
  53. }
  54. if {$animate} {
  55. append type " animate"
  56. }
  57. set command "SendChat(\[\[$chattext\]\],'$type')"
  58. if {$dbuscmd eq ""} {
  59. set dbus_id [dbus connect session]
  60. catch {dbus call $dbus_id -autostart 0 -dest $DbusDestination -signature s $DbusPath $DbusInterface $DbusMethod $command}
  61. dbus close $dbus_id
  62. } else {
  63. catch {exec $dbuscmd --session --type=method_call --dest=$DbusDestination $DbusPath $DbusInterface.$DbusMethod string:$command}
  64. }
  65. .input.text delete 1.0 end
  66. }
  67. frame .caption -borderwidth 2
  68. frame .input -borderwidth 2
  69. frame .buttons -borderwidth 2
  70. pack .caption .input .buttons -side top
  71. label .caption.label -text "Enter you chat text below:"
  72. pack .caption.label
  73. text .input.text -width 80 -height 6 -wrap word -relief sunken -yscrollcommand {.input.scrollbar set}
  74. scrollbar .input.scrollbar -command {.input.text yview}
  75. pack .input.text .input.scrollbar -side left -fill y
  76. button .buttons.say -text "Chat" -command {sendchat 0}
  77. button .buttons.emote -text "Emote" -command {sendchat 1}
  78. button .buttons.clear -text "Clear" -command {.input.text delete 1.0 end}
  79. radiobutton .buttons.whisper -text "Whisper" -variable range -value -1
  80. radiobutton .buttons.normal -text "Say" -variable range -value 0
  81. radiobutton .buttons.shout -text "Shout" -variable range -value 1
  82. checkbutton .buttons.animate -text "Animate" -variable animate
  83. button .buttons.close -text "Close" -command {exit}
  84. label .buttons.space1 -text " "
  85. label .buttons.space2 -text " "
  86. label .buttons.space3 -text " "
  87. label .buttons.space4 -text " "
  88. label .buttons.space5 -text " "
  89. pack .buttons.whisper .buttons.normal .buttons.shout \
  90. .buttons.space1 .buttons.say .buttons.space2 .buttons.emote \
  91. .buttons.space3 .buttons.animate \
  92. .buttons.space4 .buttons.clear .buttons.space5 .buttons.close -side left