2 * The initial "port" of dwm to curses was done by
4 * © 2007-2014 Marc André Tanner <mat at brain-dump dot org>
6 * It is highly inspired by the original X11 dwm and
7 * reuses some code of it which is mostly
9 * © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
11 * See LICENSE for details.
17 #include <sys/select.h>
19 #include <sys/ioctl.h>
22 #include <sys/types.h>
34 #if defined __CYGWIN__ || defined __sun
43 #ifndef NCURSES_REENTRANT
44 # define set_escdelay(d) (ESCDELAY = (d))
52 volatile sig_atomic_t need_resize
;
57 void (*arrange
)(void);
60 typedef struct Client Client
;
66 volatile sig_atomic_t editor_died
;
71 unsigned short int id
;
78 volatile sig_atomic_t died
;
99 #define ALT(k) ((k) + (161 - 'a'))
100 #if defined CTRL && defined _AIX
104 #define CTRL(k) ((k) & 0x1F)
106 #define CTRL_ALT(k) ((k) + (129 - 'a'))
111 void (*cmd
)(const char *args
[]);
112 const char *args
[MAX_ARGS
];
117 typedef unsigned int KeyCombo
[MAX_KEYS
];
134 enum { BAR_TOP
, BAR_BOTTOM
, BAR_OFF
};
135 enum { ALIGN_LEFT
, ALIGN_RIGHT
};
141 unsigned short int h
;
142 unsigned short int y
;
150 unsigned short int id
;
165 #define countof(arr) (sizeof(arr) / sizeof((arr)[0]))
166 #define sstrlen(str) (sizeof(str) - 1)
167 #define max(x, y) ((x) > (y) ? (x) : (y))
170 #define debug(format, args...)
175 /* commands for use by keybindings */
176 static void create(const char *args
[]);
177 static void copymode(const char *args
[]);
178 static void focusn(const char *args
[]);
179 static void focusnext(const char *args
[]);
180 static void focusnextnm(const char *args
[]);
181 static void focusprev(const char *args
[]);
182 static void focusprevnm(const char *args
[]);
183 static void focuslast(const char *args
[]);
184 static void killclient(const char *args
[]);
185 static void paste(const char *args
[]);
186 static void quit(const char *args
[]);
187 static void redraw(const char *args
[]);
188 static void scrollback(const char *args
[]);
189 static void send(const char *args
[]);
190 static void setlayout(const char *args
[]);
191 static void setmfact(const char *args
[]);
192 static void startup(const char *args
[]);
193 static void tag(const char *args
[]);
194 static void togglebar(const char *args
[]);
195 static void togglebell(const char *key
[]);
196 static void toggleminimize(const char *args
[]);
197 static void togglemouse(const char *args
[]);
198 static void togglerunall(const char *args
[]);
199 static void toggletag(const char *args
[]);
200 static void toggleview(const char *args
[]);
201 static void viewprevtag(const char *args
[]);
202 static void view(const char *args
[]);
203 static void zoom(const char *args
[]);
205 /* commands for use by mouse bindings */
206 static void mouse_focus(const char *args
[]);
207 static void mouse_fullscreen(const char *args
[]);
208 static void mouse_minimize(const char *args
[]);
209 static void mouse_zoom(const char *args
[]);
211 /* functions and variables available to layouts via config.h */
212 static Client
* nextvisible(Client
*c
);
213 static void focus(Client
*c
);
214 static void resize(Client
*c
, int x
, int y
, int w
, int h
);
215 extern Screen screen
;
216 static unsigned int waw
, wah
, wax
, way
;
217 static Client
*clients
= NULL
;
222 /* global variables */
223 static const char *dvtm_name
= "dvtm";
224 Screen screen
= { .mfact
= MFACT
, .history
= SCROLL_HISTORY
};
225 static Client
*stack
= NULL
;
226 static Client
*sel
= NULL
;
227 static Client
*lastsel
= NULL
;
228 static Client
*msel
= NULL
;
229 static bool seltags
[countof(tags
)] = {[0] = true};
230 static bool prevtags
[countof(tags
)];
231 static bool mouse_events_enabled
= ENABLE_MOUSE
;
232 static Layout
*layout
= layouts
;
233 static StatusBar bar
= { .fd
= -1, .pos
= BAR_POS
, .autohide
= BAR_AUTOHIDE
, .h
= 1 };
234 static CmdFifo cmdfifo
= { .fd
= -1 };
235 static const char *shell
;
236 static Register copyreg
;
237 static volatile sig_atomic_t running
= true;
238 static bool runinall
= false;
241 eprint(const char *errstr
, ...) {
243 va_start(ap
, errstr
);
244 vfprintf(stderr
, errstr
, ap
);
249 error(const char *errstr
, ...) {
251 va_start(ap
, errstr
);
252 vfprintf(stderr
, errstr
, ap
);
258 isarrange(void (*func
)()) {
259 return func
== layout
->arrange
;
263 isvisible(Client
*c
) {
264 for (unsigned int i
= 0; i
< countof(tags
); i
++)
265 if (c
->tags
[i
] && seltags
[i
])
271 is_content_visible(Client
*c
) {
274 if (isarrange(fullscreen
))
276 return isvisible(c
) && !c
->minimized
;
280 nextvisible(Client
*c
) {
281 for (; c
&& !isvisible(c
); c
= c
->next
);
286 nextbytag(Client
*c
, int tag
) {
287 for (; c
&& !c
->tags
[tag
]; c
= c
->next
);
292 isoccupied(unsigned int t
) {
293 for (Client
*c
= clients
; c
; c
= c
->next
)
304 for (c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
))
307 for (c
= nextbytag(clients
, tag
); c
; c
= nextbytag(c
->next
, tag
))
318 if (bar
.pos
== BAR_TOP
) {
321 } else if (bar
.pos
== BAR_BOTTOM
) {
330 if (bar
.pos
== BAR_OFF
)
332 getyx(stdscr
, sy
, sx
);
335 for (unsigned int i
= 0; i
< countof(tags
); i
++){
338 else if (isoccupied(i
))
339 attrset(TAG_OCCUPIED
);
342 printw(TAG_SYMBOL
, tags
[i
]);
343 /* -2 because we assume %s is contained in TAG_SYMBOL */
344 x
+= sstrlen(TAG_SYMBOL
) - 2 + strlen(tags
[i
]);
350 wchar_t wbuf
[sizeof bar
.text
];
351 int w
, maxwidth
= screen
.w
- x
- 2;
353 if (mbstowcs(wbuf
, bar
.text
, sizeof bar
.text
) == (size_t)-1)
355 if ((w
= wcswidth(wbuf
, maxwidth
)) == -1)
357 if (BAR_ALIGN
== ALIGN_RIGHT
) {
358 for (int i
= 0; i
+ w
< maxwidth
; i
++)
362 if (BAR_ALIGN
== ALIGN_LEFT
) {
363 for (; w
< maxwidth
; w
++)
367 mvaddch(bar
.y
, screen
.w
- 1, ']');
368 attrset(NORMAL_ATTR
);
370 wnoutrefresh(stdscr
);
375 return (bar
.pos
!= BAR_OFF
) || (clients
&& clients
->next
);
379 draw_border(Client
*c
) {
385 wattrset(c
->window
, (sel
== c
|| (runinall
&& !c
->minimized
)) ? SELECTED_ATTR
: NORMAL_ATTR
);
386 getyx(c
->window
, y
, x
);
387 mvwhline(c
->window
, 0, 0, ACS_HLINE
, c
->w
);
388 maxlen
= c
->w
- (2 + sstrlen(TITLE
) - sstrlen("%s%sd") + sstrlen(SEPARATOR
) + 2);
391 if ((size_t)maxlen
< sizeof(c
->title
)) {
392 t
= c
->title
[maxlen
];
393 c
->title
[maxlen
] = '\0';
396 mvwprintw(c
->window
, 0, 2, TITLE
,
397 *c
->title
? c
->title
: "",
398 *c
->title
? SEPARATOR
: "",
401 c
->title
[maxlen
] = t
;
402 wmove(c
->window
, y
, x
);
406 draw_content(Client
*c
) {
407 vt_draw(c
->term
, c
->window
, c
->has_title_line
, 0);
412 if (is_content_visible(c
)) {
413 redrawwin(c
->window
);
416 if (!isarrange(fullscreen
) || sel
== c
)
418 wnoutrefresh(c
->window
);
423 if (!nextvisible(clients
)) {
432 if (!isarrange(fullscreen
)) {
433 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
)) {
439 /* as a last step the selected window is redrawn,
440 * this has the effect that the cursor position is
450 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
))
454 attrset(NORMAL_ATTR
);
455 if (bar
.fd
== -1 && bar
.autohide
) {
457 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
), n
++);
458 if ((!clients
|| !clients
->next
) && n
== 1)
464 if (m
&& !isarrange(fullscreen
))
467 if (m
&& !isarrange(fullscreen
)) {
468 int nw
= waw
/ m
, nx
= wax
;
469 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
)) {
471 resize(c
, nx
, way
+wah
, nw
, 1);
479 wnoutrefresh(stdscr
);
491 for (int o
= 1; c
; c
= nextvisible(c
->next
), o
++)
496 attachafter(Client
*c
, Client
*a
) { /* attach c after a */
500 for (a
= clients
; a
&& a
->next
; a
= a
->next
);
508 for (int o
= a
->order
; c
; c
= nextvisible(c
->next
))
514 attachstack(Client
*c
) {
523 c
->prev
->next
= c
->next
;
525 c
->next
->prev
= c
->prev
;
526 for (d
= nextvisible(c
->next
); d
; d
= nextvisible(d
->next
))
531 c
->next
= c
->prev
= NULL
;
535 settitle(Client
*c
) {
536 char *term
, *t
= title
;
537 if (!t
&& sel
== c
&& *c
->title
)
539 if (t
&& (term
= getenv("TERM")) && !strstr(term
, "linux"))
540 printf("\033]0;%s\007", t
);
544 detachstack(Client
*c
) {
546 for (tc
=&stack
; *tc
&& *tc
!= c
; tc
=&(*tc
)->snext
);
554 for (c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
559 if (tmp
&& !isarrange(fullscreen
)) {
561 wnoutrefresh(tmp
->window
);
568 if (isarrange(fullscreen
)) {
572 wnoutrefresh(c
->window
);
575 curs_set(c
&& !c
->minimized
&& vt_cursor_visible(c
->term
));
579 applycolorrules(Client
*c
) {
580 const ColorRule
*r
= colorrules
;
581 short fg
= r
->color
->fg
, bg
= r
->color
->bg
;
582 attr_t attrs
= r
->attrs
;
584 for (unsigned int i
= 1; i
< countof(colorrules
); i
++) {
586 if (strstr(c
->title
, r
->title
)) {
594 vt_default_colors_set(c
->term
, attrs
, fg
, bg
);
598 term_title_handler(Vt
*term
, const char *title
) {
599 Client
*c
= (Client
*)vt_data_get(term
);
601 strncpy(c
->title
, title
, sizeof(c
->title
) - 1);
602 c
->title
[title
? sizeof(c
->title
) - 1 : 0] = '\0';
604 if (!isarrange(fullscreen
) || sel
== c
)
610 move_client(Client
*c
, int x
, int y
) {
611 if (c
->x
== x
&& c
->y
== y
)
613 debug("moving, x: %d y: %d\n", x
, y
);
614 if (mvwin(c
->window
, y
, x
) == ERR
) {
615 eprint("error moving, x: %d y: %d\n", x
, y
);
623 resize_client(Client
*c
, int w
, int h
) {
624 bool has_title_line
= show_border();
625 bool resize_window
= c
->w
!= w
|| c
->h
!= h
;
627 debug("resizing, w: %d h: %d\n", w
, h
);
628 if (wresize(c
->window
, h
, w
) == ERR
) {
629 eprint("error resizing, w: %d h: %d\n", w
, h
);
635 if (resize_window
|| c
->has_title_line
!= has_title_line
) {
636 c
->has_title_line
= has_title_line
;
637 vt_resize(c
->app
, h
- has_title_line
, w
);
639 vt_resize(c
->editor
, h
- has_title_line
, w
);
644 resize(Client
*c
, int x
, int y
, int w
, int h
) {
645 resize_client(c
, w
, h
);
646 move_client(c
, x
, y
);
650 get_client_by_coord(unsigned int x
, unsigned int y
) {
651 if (y
< way
|| y
>= wah
)
653 if (isarrange(fullscreen
))
655 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
)) {
656 if (x
>= c
->x
&& x
< c
->x
+ c
->w
&& y
>= c
->y
&& y
< c
->y
+ c
->h
) {
657 debug("mouse event, x: %d y: %d client: %d\n", x
, y
, c
->order
);
665 sigchld_handler(int sig
) {
670 while ((pid
= waitpid(-1, &status
, WNOHANG
)) != 0) {
672 if (errno
== ECHILD
) {
673 /* no more child processes */
676 eprint("waitpid: %s\n", strerror(errno
));
680 debug("child with pid %d died\n", pid
);
682 for (Client
*c
= clients
; c
; c
= c
->next
) {
687 if (c
->editor
&& vt_pid_get(c
->editor
) == pid
) {
688 c
->editor_died
= true;
698 sigwinch_handler(int sig
) {
699 screen
.need_resize
= true;
703 sigterm_handler(int sig
) {
708 resize_screen(void) {
711 if (ioctl(0, TIOCGWINSZ
, &ws
) == -1) {
712 getmaxyx(stdscr
, screen
.h
, screen
.w
);
714 screen
.w
= ws
.ws_col
;
715 screen
.h
= ws
.ws_row
;
718 debug("resize_screen(), w: %d h: %d\n", screen
.w
, screen
.h
);
720 resizeterm(screen
.h
, screen
.w
);
721 wresize(stdscr
, screen
.h
, screen
.w
);
728 keybinding(KeyCombo keys
) {
729 unsigned int keycount
= 0;
730 while (keycount
< MAX_KEYS
&& keys
[keycount
])
732 for (unsigned int b
= 0; b
< countof(bindings
); b
++) {
733 for (unsigned int k
= 0; k
< keycount
; k
++) {
734 if (keys
[k
] != bindings
[b
].keys
[k
])
736 if (k
== keycount
- 1)
744 idxoftag(const char *tag
) {
746 for (i
= 0; (i
< countof(tags
)) && (tags
[i
] != tag
); i
++);
747 return (i
< countof(tags
)) ? i
: 0;
753 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
)) {
759 if (!nm
&& nextvisible(clients
)) {
761 toggleminimize(NULL
);
768 tag(const char *args
[]) {
773 for (i
= 0; i
< countof(tags
); i
++)
774 sel
->tags
[i
] = (NULL
== args
[0]);
775 i
= idxoftag(args
[0]);
782 toggletag(const char *args
[]) {
787 i
= idxoftag(args
[0]);
788 sel
->tags
[i
] = !sel
->tags
[i
];
789 for (j
= 0; j
< countof(tags
) && !sel
->tags
[j
]; j
++);
790 if (j
== countof(tags
))
791 sel
->tags
[i
] = true; /* at least one tag must be enabled */
796 toggleview(const char *args
[]) {
799 i
= idxoftag(args
[0]);
800 seltags
[i
] = !seltags
[i
];
801 for (j
= 0; j
< countof(tags
) && !seltags
[j
]; j
++);
802 if (j
== countof(tags
))
803 seltags
[i
] = true; /* at least one tag must be viewed */
808 view(const char *args
[]) {
809 memcpy(prevtags
, seltags
, sizeof seltags
);
810 for (unsigned int i
= 0; i
< countof(tags
); i
++)
811 seltags
[i
] = (NULL
== args
[0]);
812 seltags
[idxoftag(args
[0])] = true;
817 viewprevtag(const char *args
[]) {
818 static bool tmp
[countof(tags
)];
819 memcpy(tmp
, seltags
, sizeof seltags
);
820 memcpy(seltags
, prevtags
, sizeof seltags
);
821 memcpy(prevtags
, tmp
, sizeof seltags
);
827 unsigned int len
= 1;
828 char buf
[8] = { '\e' };
831 /* pass characters following escape to the underlying app */
832 nodelay(stdscr
, TRUE
);
833 for (int t
; len
< sizeof(buf
) && (t
= getch()) != ERR
; len
++)
835 nodelay(stdscr
, FALSE
);
838 for (Client
*c
= runinall
? nextvisible(clients
) : sel
; c
; c
= nextvisible(c
->next
)) {
839 if (is_content_visible(c
)) {
841 vt_write(c
->term
, buf
, len
);
843 vt_keypress(c
->term
, code
);
855 if (mouse_events_enabled
) {
856 mask
= BUTTON1_CLICKED
| BUTTON2_CLICKED
;
857 for (unsigned int i
= 0; i
< countof(buttons
); i
++)
858 mask
|= buttons
[i
].mask
;
860 mousemask(mask
, NULL
);
861 #endif /* CONFIG_MOUSE */
865 checkshell(const char *shell
) {
866 if (shell
== NULL
|| *shell
== '\0' || *shell
!= '/')
868 if (!strcmp(strrchr(shell
, '/')+1, dvtm_name
))
870 if (access(shell
, X_OK
))
877 const char *shell
= getenv("SHELL");
880 if (checkshell(shell
))
882 if ((pw
= getpwuid(getuid())) && checkshell(pw
->pw_shell
))
890 setlocale(LC_CTYPE
, "");
894 keypad(stdscr
, TRUE
);
898 vt_keytable_set(keytable
, countof(keytable
));
899 for (unsigned int i
= 0; i
< countof(colors
); i
++) {
902 colors
[i
].fg
= colors
[i
].fg256
;
904 colors
[i
].bg
= colors
[i
].bg256
;
906 colors
[i
].pair
= vt_color_reserve(colors
[i
].fg
, colors
[i
].bg
);
911 sigemptyset(&sa
.sa_mask
);
912 sa
.sa_handler
= sigwinch_handler
;
913 sigaction(SIGWINCH
, &sa
, NULL
);
914 sa
.sa_handler
= sigchld_handler
;
915 sigaction(SIGCHLD
, &sa
, NULL
);
916 sa
.sa_handler
= sigterm_handler
;
917 sigaction(SIGTERM
, &sa
, NULL
);
918 sa
.sa_handler
= SIG_IGN
;
919 sigaction(SIGPIPE
, &sa
, NULL
);
930 if ((t
= nextvisible(clients
))) {
932 toggleminimize(NULL
);
940 wnoutrefresh(c
->window
);
943 if (!clients
&& countof(actions
)) {
944 if (!strcmp(c
->cmd
, shell
))
967 unlink(cmdfifo
.file
);
970 static char *getcwd_by_pid(Client
*c
) {
974 snprintf(buf
, sizeof buf
, "/proc/%d/cwd", c
->pid
);
975 return realpath(buf
, NULL
);
978 /* commands for use by keybindings */
980 create(const char *args
[]) {
981 Client
*c
= calloc(1, sizeof(Client
) + sizeof(seltags
));
984 memcpy(c
->tags
, seltags
, sizeof seltags
);
985 const char *cmd
= (args
&& args
[0]) ? args
[0] : shell
;
986 const char *pargs
[] = { "/bin/sh", "-c", cmd
, NULL
};
987 c
->id
= ++cmdfifo
.id
;
988 char buf
[8], *cwd
= NULL
;
989 snprintf(buf
, sizeof buf
, "%d", c
->id
);
990 const char *env
[] = {
991 "DVTM_WINDOW_ID", buf
,
995 if (!(c
->window
= newwin(wah
, waw
, way
, wax
))) {
1000 c
->term
= c
->app
= vt_create(screen
.h
, screen
.w
, screen
.history
);
1008 if (args
&& args
[1]) {
1009 strncpy(c
->title
, args
[1], sizeof(c
->title
) - 1);
1010 c
->title
[sizeof(c
->title
) - 1] = '\0';
1012 if (args
&& args
[2])
1013 cwd
= !strcmp(args
[2], "$CWD") ? getcwd_by_pid(sel
) : (char*)args
[2];
1014 c
->pid
= vt_forkpty(c
->term
, "/bin/sh", pargs
, cwd
, env
, NULL
, NULL
);
1015 if (args
&& args
[2] && !strcmp(args
[2], "$CWD"))
1017 vt_data_set(c
->term
, c
);
1018 vt_title_handler_set(c
->term
, term_title_handler
);
1021 debug("client with pid %d forked\n", c
->pid
);
1028 copymode(const char *args
[]) {
1029 if (!sel
|| sel
->editor
)
1031 if (!(sel
->editor
= vt_create(sel
->h
- sel
->has_title_line
, sel
->w
, 0)))
1034 char *ed
= getenv("DVTM_EDITOR");
1035 int *to
= &sel
->editor_fds
[0], *from
= NULL
;
1036 sel
->editor_fds
[0] = sel
->editor_fds
[1] = -1;
1039 ed
= getenv("EDITOR");
1041 ed
= getenv("PAGER");
1043 ed
= editors
[0].name
;
1045 const char **argv
= (const char*[]){ ed
, "-", NULL
};
1047 for (unsigned int i
= 0; i
< countof(editors
); i
++) {
1048 if (!strcmp(editors
[i
].name
, ed
)) {
1049 argv
= (const char*[]){ ed
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
};
1050 for (int j
= 1; editors
[i
].argv
[j
]; j
++) {
1051 if (strstr(editors
[i
].argv
[j
], "%d")) {
1053 int line
= vt_content_start(sel
->app
);
1054 snprintf(argline
, sizeof(argline
), "+%d", line
);
1057 argv
[j
] = editors
[i
].argv
[j
];
1060 if (editors
[i
].filter
)
1061 from
= &sel
->editor_fds
[1];
1066 if (vt_forkpty(sel
->editor
, ed
, argv
, NULL
, NULL
, to
, from
) < 0) {
1067 vt_destroy(sel
->editor
);
1072 sel
->term
= sel
->editor
;
1074 if (sel
->editor_fds
[0] != -1) {
1076 size_t len
= vt_content_get(sel
->app
, &buf
);
1079 ssize_t res
= write(sel
->editor_fds
[0], cur
, len
);
1081 if (errno
== EAGAIN
|| errno
== EINTR
)
1089 close(sel
->editor_fds
[0]);
1090 sel
->editor_fds
[0] = -1;
1094 vt_write(sel
->editor
, args
[0], strlen(args
[0]));
1098 focusn(const char *args
[]) {
1099 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
)) {
1100 if (c
->order
== atoi(args
[0])) {
1103 toggleminimize(NULL
);
1110 focusnext(const char *args
[]) {
1114 for (c
= sel
->next
; c
&& !isvisible(c
); c
= c
->next
);
1116 for (c
= clients
; c
&& !isvisible(c
); c
= c
->next
);
1122 focusnextnm(const char *args
[]) {
1127 c
= nextvisible(c
->next
);
1129 c
= nextvisible(clients
);
1130 } while (c
->minimized
&& c
!= sel
);
1135 focusprev(const char *args
[]) {
1139 for (c
= sel
->prev
; c
&& !isvisible(c
); c
= c
->prev
);
1141 for (c
= clients
; c
&& c
->next
; c
= c
->next
);
1142 for (; c
&& !isvisible(c
); c
= c
->prev
);
1149 focusprevnm(const char *args
[]) {
1154 for (c
= c
->prev
; c
&& !isvisible(c
); c
= c
->prev
);
1156 for (c
= clients
; c
&& c
->next
; c
= c
->next
);
1157 for (; c
&& !isvisible(c
); c
= c
->prev
);
1159 } while (c
&& c
!= sel
&& c
->minimized
);
1164 focuslast(const char *args
[]) {
1170 killclient(const char *args
[]) {
1173 debug("killing client with pid: %d\n", sel
->pid
);
1174 kill(-sel
->pid
, SIGKILL
);
1178 paste(const char *args
[]) {
1179 if (sel
&& copyreg
.data
)
1180 vt_write(sel
->term
, copyreg
.data
, copyreg
.len
);
1184 quit(const char *args
[]) {
1190 redraw(const char *args
[]) {
1191 for (Client
*c
= clients
; c
; c
= c
->next
) {
1192 if (!c
->minimized
) {
1195 wnoutrefresh(c
->window
);
1202 scrollback(const char *args
[]) {
1203 if (!is_content_visible(sel
))
1206 if (!args
[0] || atoi(args
[0]) < 0)
1207 vt_scroll(sel
->term
, -sel
->h
/2);
1209 vt_scroll(sel
->term
, sel
->h
/2);
1212 curs_set(vt_cursor_visible(sel
->term
));
1216 send(const char *args
[]) {
1217 if (sel
&& args
&& args
[0])
1218 vt_write(sel
->term
, args
[0], strlen(args
[0]));
1222 setlayout(const char *args
[]) {
1225 if (!args
|| !args
[0]) {
1226 if (++layout
== &layouts
[countof(layouts
)])
1227 layout
= &layouts
[0];
1229 for (i
= 0; i
< countof(layouts
); i
++)
1230 if (!strcmp(args
[0], layouts
[i
].symbol
))
1232 if (i
== countof(layouts
))
1234 layout
= &layouts
[i
];
1240 setmfact(const char *args
[]) {
1243 if (isarrange(fullscreen
) || isarrange(grid
))
1245 /* arg handling, manipulate mfact */
1246 if (args
[0] == NULL
) {
1247 screen
.mfact
= MFACT
;
1248 } else if (1 == sscanf(args
[0], "%f", &delta
)) {
1249 if (args
[0][0] == '+' || args
[0][0] == '-')
1250 screen
.mfact
+= delta
;
1252 screen
.mfact
= delta
;
1253 if (screen
.mfact
< 0.1)
1255 else if (screen
.mfact
> 0.9)
1262 startup(const char *args
[]) {
1263 for (unsigned int i
= 0; i
< countof(actions
); i
++)
1264 actions
[i
].cmd(actions
[i
].args
);
1268 togglebar(const char *args
[]) {
1269 if (bar
.pos
== BAR_OFF
)
1270 bar
.pos
= (BAR_POS
== BAR_OFF
) ? BAR_TOP
: BAR_POS
;
1273 bar
.autohide
= false;
1279 togglebell(const char *args
[]) {
1280 vt_togglebell(sel
->term
);
1284 toggleminimize(const char *args
[]) {
1289 /* the last window can't be minimized */
1290 if (!sel
->minimized
) {
1291 for (n
= 0, c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
))
1297 sel
->minimized
= !sel
->minimized
;
1299 /* check whether the master client was minimized */
1300 if (sel
== nextvisible(clients
) && sel
->minimized
) {
1301 c
= nextvisible(sel
->next
);
1306 for (; c
&& (t
= nextvisible(c
->next
)) && !t
->minimized
; c
= t
);
1308 } else if (m
->minimized
) {
1309 /* non master window got minimized move it above all other
1313 for (c
= nextvisible(clients
); c
&& (t
= nextvisible(c
->next
)) && !t
->minimized
; c
= t
);
1315 } else { /* window is no longer minimized, move it to the master area */
1324 togglemouse(const char *args
[]) {
1325 mouse_events_enabled
= !mouse_events_enabled
;
1330 togglerunall(const char *args
[]) {
1331 runinall
= !runinall
;
1336 zoom(const char *args
[]) {
1341 if (args
&& args
[0])
1343 if ((c
= sel
) == nextvisible(clients
))
1344 if (!(c
= nextvisible(c
->next
)))
1350 toggleminimize(NULL
);
1354 /* commands for use by mouse bindings */
1356 mouse_focus(const char *args
[]) {
1358 if (msel
->minimized
)
1359 toggleminimize(NULL
);
1363 mouse_fullscreen(const char *args
[]) {
1365 if (isarrange(fullscreen
))
1372 mouse_minimize(const char *args
[]) {
1374 toggleminimize(NULL
);
1378 mouse_zoom(const char *args
[]) {
1384 get_cmd_by_name(const char *name
) {
1385 for (unsigned int i
= 0; i
< countof(commands
); i
++) {
1386 if (!strcmp(name
, commands
[i
].name
))
1387 return &commands
[i
];
1393 handle_cmdfifo(void) {
1395 char *p
, *s
, cmdbuf
[512], c
;
1397 switch (r
= read(cmdfifo
.fd
, cmdbuf
, sizeof cmdbuf
- 1)) {
1406 /* find the command name */
1407 for (; *p
== ' ' || *p
== '\n'; p
++);
1408 for (s
= p
; *p
&& *p
!= ' ' && *p
!= '\n'; p
++);
1411 if (*s
&& (cmd
= get_cmd_by_name(s
)) != NULL
) {
1414 const char *args
[MAX_ARGS
], *arg
;
1415 memset(args
, 0, sizeof(args
));
1416 /* if arguments were specified in config.h ignore the one given via
1417 * the named pipe and thus skip everything until we find a new line
1419 if (cmd
->action
.args
[0] || c
== '\n') {
1420 debug("execute %s", s
);
1421 cmd
->action
.cmd(cmd
->action
.args
);
1422 while (*p
&& *p
!= '\n')
1426 /* no arguments were given in config.h so we parse the command line */
1430 for (; (c
= *p
); p
++) {
1433 /* remove the escape character '\\' move every
1434 * following character to the left by one position
1454 /* remove trailing quote if there is one */
1455 if (*(p
- 1) == '\'' || *(p
- 1) == '\"')
1458 /* remove leading quote if there is one */
1459 if (*arg
== '\'' || *arg
== '\"')
1461 if (argc
< MAX_ARGS
)
1471 if (c
== '\n' || *p
== '\n') {
1474 debug("execute %s", s
);
1475 for(int i
= 0; i
< argc
; i
++)
1476 debug(" %s", args
[i
]);
1478 cmd
->action
.cmd(args
);
1488 handle_mouse(void) {
1492 if (getmouse(&event
) != OK
)
1494 msel
= get_client_by_coord(event
.x
, event
.y
);
1499 debug("mouse x:%d y:%d cx:%d cy:%d mask:%d\n", event
.x
, event
.y
, event
.x
- msel
->x
, event
.y
- msel
->y
, event
.bstate
);
1501 vt_mouse(msel
->term
, event
.x
- msel
->x
, event
.y
- msel
->y
, event
.bstate
);
1503 for (i
= 0; i
< countof(buttons
); i
++) {
1504 if (event
.bstate
& buttons
[i
].mask
)
1505 buttons
[i
].action
.cmd(buttons
[i
].action
.args
);
1509 #endif /* CONFIG_MOUSE */
1513 handle_statusbar(void) {
1516 switch (r
= read(bar
.fd
, bar
.text
, sizeof bar
.text
- 1)) {
1518 strncpy(bar
.text
, strerror(errno
), sizeof bar
.text
- 1);
1519 bar
.text
[sizeof bar
.text
- 1] = '\0';
1527 p
= bar
.text
+ r
- 1;
1528 for (; p
>= bar
.text
&& *p
== '\n'; *p
-- = '\0');
1529 for (; p
>= bar
.text
&& *p
!= '\n'; --p
);
1531 memmove(bar
.text
, p
+ 1, strlen(p
));
1537 handle_editor(Client
*c
) {
1538 if (!copyreg
.data
&& (copyreg
.data
= malloc(screen
.history
)))
1539 copyreg
.size
= screen
.history
;
1541 while (c
->editor_fds
[1] != -1 && copyreg
.len
< copyreg
.size
) {
1542 ssize_t len
= read(c
->editor_fds
[1], copyreg
.data
+ copyreg
.len
, copyreg
.size
- copyreg
.len
);
1551 if (copyreg
.len
== copyreg
.size
) {
1553 if (!(copyreg
.data
= realloc(copyreg
.data
, copyreg
.size
))) {
1559 c
->editor_died
= false;
1560 c
->editor_fds
[1] = -1;
1561 vt_destroy(c
->editor
);
1566 wnoutrefresh(c
->window
);
1570 open_or_create_fifo(const char *name
, const char **name_created
) {
1575 if ((fd
= open(name
, O_RDWR
|O_NONBLOCK
)) == -1) {
1576 if (errno
== ENOENT
&& !mkfifo(name
, S_IRUSR
|S_IWUSR
)) {
1577 *name_created
= name
;
1580 error("%s\n", strerror(errno
));
1584 if (fstat(fd
, &info
) == -1)
1585 error("%s\n", strerror(errno
));
1586 if (!S_ISFIFO(info
.st_mode
))
1587 error("%s is not a named pipe\n", name
);
1594 eprint("usage: dvtm [-v] [-M] [-m mod] [-d delay] [-h lines] [-t title] "
1595 "[-s status-fifo] [-c cmd-fifo] [cmd...]\n");
1600 parse_args(int argc
, char *argv
[]) {
1602 const char *name
= argv
[0];
1604 if (name
&& (name
= strrchr(name
, '/')))
1605 dvtm_name
= name
+ 1;
1606 if (!getenv("ESCDELAY"))
1608 for (int arg
= 1; arg
< argc
; arg
++) {
1609 if (argv
[arg
][0] != '-') {
1610 const char *args
[] = { argv
[arg
], NULL
, NULL
};
1618 if (argv
[arg
][1] != 'v' && argv
[arg
][1] != 'M' && (arg
+ 1) >= argc
)
1620 switch (argv
[arg
][1]) {
1622 puts("dvtm-"VERSION
" © 2007-2014 Marc André Tanner");
1625 mouse_events_enabled
= !mouse_events_enabled
;
1628 char *mod
= argv
[++arg
];
1629 if (mod
[0] == '^' && mod
[1])
1630 *mod
= CTRL(mod
[1]);
1631 for (unsigned int b
= 0; b
< countof(bindings
); b
++)
1632 if (bindings
[b
].keys
[0] == MOD
)
1633 bindings
[b
].keys
[0] = *mod
;
1637 set_escdelay(atoi(argv
[++arg
]));
1640 else if (ESCDELAY
> 1000)
1644 screen
.history
= atoi(argv
[++arg
]);
1647 title
= argv
[++arg
];
1650 bar
.fd
= open_or_create_fifo(argv
[++arg
], &bar
.file
);
1655 cmdfifo
.fd
= open_or_create_fifo(argv
[++arg
], &cmdfifo
.file
);
1656 if (!(fifo
= realpath(argv
[arg
], NULL
)))
1657 error("%s\n", strerror(errno
));
1658 setenv("DVTM_CMD_FIFO", fifo
, 1);
1669 main(int argc
, char *argv
[]) {
1671 unsigned int key_index
= 0;
1672 memset(keys
, 0, sizeof(keys
));
1673 sigset_t emptyset
, blockset
;
1675 setenv("DVTM", VERSION
, 1);
1676 if (!parse_args(argc
, argv
)) {
1681 sigemptyset(&emptyset
);
1682 sigemptyset(&blockset
);
1683 sigaddset(&blockset
, SIGWINCH
);
1684 sigaddset(&blockset
, SIGCHLD
);
1685 sigprocmask(SIG_BLOCK
, &blockset
, NULL
);
1691 if (screen
.need_resize
) {
1693 screen
.need_resize
= false;
1697 FD_SET(STDIN_FILENO
, &rd
);
1699 if (cmdfifo
.fd
!= -1) {
1700 FD_SET(cmdfifo
.fd
, &rd
);
1705 FD_SET(bar
.fd
, &rd
);
1706 nfds
= max(nfds
, bar
.fd
);
1709 for (Client
*c
= clients
; c
; ) {
1710 if (c
->editor
&& c
->editor_died
)
1712 if (!c
->editor
&& c
->died
) {
1713 Client
*t
= c
->next
;
1718 int pty
= c
->editor
? vt_pty_get(c
->editor
) : vt_pty_get(c
->app
);
1720 nfds
= max(nfds
, pty
);
1725 r
= pselect(nfds
+ 1, &rd
, NULL
, NULL
, NULL
, &emptyset
);
1727 if (r
== -1 && errno
== EINTR
)
1735 if (FD_ISSET(STDIN_FILENO
, &rd
)) {
1738 keys
[key_index
++] = code
;
1739 KeyBinding
*binding
= NULL
;
1740 if (code
== KEY_MOUSE
) {
1742 } else if ((binding
= keybinding(keys
))) {
1743 unsigned int key_length
= 0;
1744 while (key_length
< MAX_KEYS
&& binding
->keys
[key_length
])
1746 if (key_index
== key_length
) {
1747 binding
->action
.cmd(binding
->action
.args
);
1749 memset(keys
, 0, sizeof(keys
));
1753 memset(keys
, 0, sizeof(keys
));
1757 if (r
== 1) /* no data available on pty's */
1761 if (cmdfifo
.fd
!= -1 && FD_ISSET(cmdfifo
.fd
, &rd
))
1764 if (bar
.fd
!= -1 && FD_ISSET(bar
.fd
, &rd
))
1767 for (Client
*c
= clients
; c
; c
= c
->next
) {
1768 if (FD_ISSET(vt_pty_get(c
->term
), &rd
)) {
1769 if (vt_process(c
->term
) < 0 && errno
== EIO
) {
1771 c
->editor_died
= true;
1778 if (c
!= sel
&& is_content_visible(c
)) {
1780 wnoutrefresh(c
->window
);
1784 if (is_content_visible(sel
)) {
1786 curs_set(vt_cursor_visible(sel
->term
));
1787 wnoutrefresh(sel
->window
);