-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscad-dbus.el
192 lines (162 loc) · 7.32 KB
/
scad-dbus.el
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
;;; scad-dbus.el --- Control a running OpenSCAD instance via D-Bus
;; Author: Len Trigg
;; Maintainer: Len Trigg <lenbok@gmail.com>
;; URL: https://github.com/Lenbok/scad-dbus/
;; 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;
;; Control a running OpenSCAD instance via the D-Bus interface.
;;
;; Since D-Bus seems to be linux-only, you need to be on linux.
;;
;; Enable DBus in OpenSCAD via:
;; Preferences-> Features -> input-driver-dbus
;; Preferences-> Axes -> DBus
;;
;; https://www.gnu.org/software/emacs/manual/html_mono/dbus.html
;;
;; However, as of writing, OpenSCAD seems to have a bug where the D-Bus
;; interface won't execute action commands until after some menu items
;; have manually been selected. See:
;; https://github.com/openscad/openscad/issues/3367
;;; Code:
(require 'dbus)
(require 'scad-mode)
(require 'hydra)
(defconst scad-dbus-service-name "org.openscad.OpenSCAD"
"The name of the service that OpenSCAD registers with D-Bus.")
(defun scad-dbus--service-available ()
"Return non-nil if OpenSCAD is listening on the D-Bus."
(member scad-dbus-service-name (dbus-list-known-names :session)))
(defun scad-dbus-connected ()
"Echo to message area whether OpenSCAD is listening on the D-Bus."
(interactive)
(if (scad-dbus--service-available)
(message "OpenSCAD is connected")
(message "OpenSCAD is not connected")))
(defun scad-dbus--call-method (method &rest args)
"Call the OpenSCAD method METHOD with ARGS over D-Bus."
(apply 'dbus-call-method
:session ; use the session (not system) bus
scad-dbus-service-name ; service name
"/org/openscad/OpenSCAD/Application" ; path name
"org.openscad.OpenSCAD" ; interface name
method args))
(defun scad-dbus--call-action (name)
"Call the OpenSCAD method \"action\" with NAME over D-Bus."
(scad-dbus--call-method "action" name))
;; Use this to ask OpenSCAD for get the list of available actions
(defun scad-dbus-list-actions ()
"List OpenSCAD action methods to a help buffer."
(interactive)
(with-help-window "*scad-dbus-output*"
(with-current-buffer "*scad-dbus-output*"
(insert "** OpenSCAD DBus Action Commands **\n"
(mapconcat 'identity
(scad-dbus--call-method "getActions")
"\n")))))
;; Macros to define interactive commands binding to dbus method calls
(defmacro scad-dbus--3-axis-command (command method idx amount)
`(defun ,command ()
,(format "Instructs OpenSCAD to %s the %s axis by %s." method (nth idx '("x" "y" "z")) amount)
(interactive)
(scad-dbus--call-method ,method
,@(make-list idx 0.0)
,amount
,@(make-list (- 2 idx) 0.0))))
(defmacro scad-dbus--1-axis-command (command method amount)
`(defun ,command ()
,(format "Instructs OpenSCAD to %s by %s." method amount)
(interactive)
(scad-dbus--call-method ,method ,amount)))
(defmacro scad-dbus--action-command (command method)
`(defun ,command ()
,(format "Instructs OpenSCAD to call %s." method)
(interactive)
(scad-dbus--call-action ,method)))
;; (macroexpand `(scad-dbus--action-command scad-export-stl "fileActionExportSTL"))
;; (macroexpand `(scad-dbus--1-axis-command scad-call-zoom+ "zoom" 40.0))
;; (macroexpand `(scad-dbus--3-axis-command scad-call-rotx+ "rotate" 2 5.0))
;; Define commands for direct camera control
(scad-dbus--3-axis-command scad-dbus-rotx+ "rotate" 0 5.0)
(scad-dbus--3-axis-command scad-dbus-rotx- "rotate" 0 -5.0)
(scad-dbus--3-axis-command scad-dbus-roty+ "rotate" 1 5.0)
(scad-dbus--3-axis-command scad-dbus-roty- "rotate" 1 -5.0)
(scad-dbus--3-axis-command scad-dbus-rotz+ "rotate" 2 5.0)
(scad-dbus--3-axis-command scad-dbus-rotz- "rotate" 2 -5.0)
(scad-dbus--3-axis-command scad-dbus-trnsx+ "translate" 0 5.0)
(scad-dbus--3-axis-command scad-dbus-trnsx- "translate" 0 -5.0)
(scad-dbus--3-axis-command scad-dbus-trnsy+ "translate" 1 5.0)
(scad-dbus--3-axis-command scad-dbus-trnsy- "translate" 1 -5.0)
(scad-dbus--3-axis-command scad-dbus-trnsz+ "translate" 2 5.0)
(scad-dbus--3-axis-command scad-dbus-trnsz- "translate" 2 -5.0)
(scad-dbus--1-axis-command scad-dbus-zoom+ "zoom" 40.0)
(scad-dbus--1-axis-command scad-dbus-zoom- "zoom" -40.0)
;; Commands from File menu
(scad-dbus--action-command scad-dbus-export-stl "fileActionExportSTL")
(scad-dbus--action-command scad-dbus-export-image "fileActionExportImage")
(scad-dbus--action-command scad-dbus-quit "fileActionQuit")
;; Commands from Design menu
(scad-dbus--action-command scad-dbus-render "designActionRender")
(scad-dbus--action-command scad-dbus-preview "designActionPreview")
;; Commands from View menu
(scad-dbus--action-command scad-dbus-view-top "viewActionTop")
(scad-dbus--action-command scad-dbus-view-front "viewActionFront")
(scad-dbus--action-command scad-dbus-view-diagonal "viewActionDiagonal")
(scad-dbus--action-command scad-dbus-view-reset "viewActionResetView")
(scad-dbus--action-command scad-dbus-view-all "viewActionViewAll")
(scad-dbus--action-command scad-dbus-view-center "viewActionCenter")
(scad-dbus--action-command scad-dbus-view-zoom-in "viewActionZoomIn")
(scad-dbus--action-command scad-dbus-view-zoom-out "viewActionZoomOut")
(scad-dbus--action-command scad-dbus-view-axes "viewActionShowAxes")
(scad-dbus--action-command scad-dbus-view-console "windowActionHideConsole")
(scad-dbus--action-command scad-dbus-view-errors "windowActionHideErrorLog")
(defhydra hydra-scad-dbus (:foreign-keys warn)
"
View^^ ^^^^^^^^ Extras^^
[_a_] all [_h_][_j_][_k_][_l_] rotate [_xc_] console [_xi_] export image
[_c_] center [_H_][_J_][_K_][_L_] translate [_xe_] error log [_xs_] export STL
[_t_] top ^^^^[_u_][_i_] zoom [_xa_] axes
[_f_] front ^^^^^^^^ [_xp_] preview
[_d_] diagonal ^^^^^^[_r_] reset [_xr_] render
"
("t" scad-dbus-view-top)
("f" scad-dbus-view-front)
("d" scad-dbus-view-diagonal)
("c" scad-dbus-view-center)
("a" scad-dbus-view-all)
("r" scad-dbus-view-reset)
("h" scad-dbus-rotz-)
("j" scad-dbus-rotx+)
("k" scad-dbus-rotx-)
("l" scad-dbus-rotz+)
("H" scad-dbus-trnsx-)
("J" scad-dbus-trnsz-)
("K" scad-dbus-trnsz+)
("L" scad-dbus-trnsx+)
("i" scad-dbus-zoom+)
("u" scad-dbus-zoom-)
("xc" scad-dbus-view-console)
("xe" scad-dbus-view-errors)
("xa" scad-dbus-view-axes)
("xp" scad-dbus-render)
("xr" scad-dbus-render)
("xs" scad-dbus-export-stl)
("xi" scad-dbus-export-image)
("q" nil "quit"))
(define-key scad-mode-map (kbd "C-c o") 'hydra-scad-dbus/body)
(provide 'scad-dbus)
;;; scad-dbus.el ends here