2 * The initial "port" of dwm to curses was done by
4 * © 2007-2015 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))
53 volatile sig_atomic_t need_resize
;
58 void (*arrange
)(void);
61 typedef struct Client Client
;
67 volatile sig_atomic_t editor_died
;
72 unsigned short int id
;
80 volatile sig_atomic_t died
;
101 #define ALT(k) ((k) + (161 - 'a'))
102 #if defined CTRL && defined _AIX
106 #define CTRL(k) ((k) & 0x1F)
108 #define CTRL_ALT(k) ((k) + (129 - 'a'))
113 void (*cmd
)(const char *args
[]);
114 const char *args
[MAX_ARGS
];
119 typedef unsigned int KeyCombo
[MAX_KEYS
];
136 enum { BAR_TOP
, BAR_BOTTOM
, BAR_OFF
};
142 unsigned short int h
;
143 unsigned short int y
;
151 unsigned short int id
;
167 #define LENGTH(arr) (sizeof(arr) / sizeof((arr)[0]))
168 #define MAX(x, y) ((x) > (y) ? (x) : (y))
169 #define MIN(x, y) ((x) < (y) ? (x) : (y))
170 #define TAGMASK ((1 << LENGTH(tags)) - 1)
173 #define debug(format, args...)
178 /* commands for use by keybindings */
179 static void create(const char *args
[]);
180 static void copymode(const char *args
[]);
181 static void focusn(const char *args
[]);
182 static void focusnext(const char *args
[]);
183 static void focusnextnm(const char *args
[]);
184 static void focusprev(const char *args
[]);
185 static void focusprevnm(const char *args
[]);
186 static void focuslast(const char *args
[]);
187 static void killclient(const char *args
[]);
188 static void paste(const char *args
[]);
189 static void quit(const char *args
[]);
190 static void redraw(const char *args
[]);
191 static void scrollback(const char *args
[]);
192 static void send(const char *args
[]);
193 static void setlayout(const char *args
[]);
194 static void incnmaster(const char *args
[]);
195 static void setmfact(const char *args
[]);
196 static void startup(const char *args
[]);
197 static void tag(const char *args
[]);
198 static void togglebar(const char *args
[]);
199 static void togglebarpos(const char *args
[]);
200 static void toggleminimize(const char *args
[]);
201 static void togglemouse(const char *args
[]);
202 static void togglerunall(const char *args
[]);
203 static void toggletag(const char *args
[]);
204 static void toggleview(const char *args
[]);
205 static void viewprevtag(const char *args
[]);
206 static void view(const char *args
[]);
207 static void zoom(const char *args
[]);
209 /* commands for use by mouse bindings */
210 static void mouse_focus(const char *args
[]);
211 static void mouse_fullscreen(const char *args
[]);
212 static void mouse_minimize(const char *args
[]);
213 static void mouse_zoom(const char *args
[]);
215 /* functions and variables available to layouts via config.h */
216 static Client
* nextvisible(Client
*c
);
217 static void focus(Client
*c
);
218 static void resize(Client
*c
, int x
, int y
, int w
, int h
);
219 extern Screen screen
;
220 static unsigned int waw
, wah
, wax
, way
;
221 static Client
*clients
= NULL
;
226 /* global variables */
227 static const char *dvtm_name
= "dvtm";
228 Screen screen
= { .mfact
= MFACT
, .nmaster
= NMASTER
, .history
= SCROLL_HISTORY
};
229 static Client
*stack
= NULL
;
230 static Client
*sel
= NULL
;
231 static Client
*lastsel
= NULL
;
232 static Client
*msel
= NULL
;
233 static unsigned int seltags
;
234 static unsigned int tagset
[2] = { 1, 1 };
235 static bool mouse_events_enabled
= ENABLE_MOUSE
;
236 static Layout
*layout
= layouts
;
237 static StatusBar bar
= { .fd
= -1, .lastpos
= BAR_POS
, .pos
= BAR_POS
, .autohide
= BAR_AUTOHIDE
, .h
= 1 };
238 static CmdFifo cmdfifo
= { .fd
= -1 };
239 static const char *shell
;
240 static Register copyreg
;
241 static volatile sig_atomic_t running
= true;
242 static bool runinall
= false;
245 eprint(const char *errstr
, ...) {
247 va_start(ap
, errstr
);
248 vfprintf(stderr
, errstr
, ap
);
253 error(const char *errstr
, ...) {
255 va_start(ap
, errstr
);
256 vfprintf(stderr
, errstr
, ap
);
262 isarrange(void (*func
)()) {
263 return func
== layout
->arrange
;
267 isvisible(Client
*c
) {
268 return c
->tags
& tagset
[seltags
];
272 is_content_visible(Client
*c
) {
275 if (isarrange(fullscreen
))
277 return isvisible(c
) && !c
->minimized
;
281 nextvisible(Client
*c
) {
282 for (; c
&& !isvisible(c
); c
= c
->next
);
293 if (bar
.pos
== BAR_TOP
) {
296 } else if (bar
.pos
== BAR_BOTTOM
) {
304 if (bar
.pos
!= BAR_OFF
) {
305 bar
.lastpos
= bar
.pos
;
312 if (bar
.pos
== BAR_OFF
)
313 bar
.pos
= bar
.lastpos
;
318 int sx
, sy
, x
, y
, width
;
319 unsigned int occupied
= 0, urgent
= 0;
320 if (bar
.pos
== BAR_OFF
)
323 for (Client
*c
= clients
; c
; c
= c
->next
) {
329 getyx(stdscr
, sy
, sx
);
333 for (unsigned int i
= 0; i
< LENGTH(tags
); i
++){
334 if (tagset
[seltags
] & (1 << i
))
336 else if (urgent
& (1 << i
))
338 else if (occupied
& (1 << i
))
339 attrset(TAG_OCCUPIED
);
342 printw(TAG_SYMBOL
, tags
[i
]);
346 addstr(layout
->symbol
);
350 int maxwidth
= screen
.w
- x
- 2;
355 wchar_t wbuf
[sizeof bar
.text
];
356 size_t numchars
= mbstowcs(wbuf
, bar
.text
, sizeof bar
.text
);
358 if (numchars
!= (size_t)-1 && (width
= wcswidth(wbuf
, maxwidth
)) != -1) {
360 for (pos
= 0; pos
+ width
< maxwidth
; pos
++)
363 for (size_t i
= 0; i
< numchars
; i
++) {
364 pos
+= wcwidth(wbuf
[i
]);
374 mvaddch(bar
.y
, screen
.w
- 1, BAR_END
);
375 attrset(NORMAL_ATTR
);
377 wnoutrefresh(stdscr
);
382 return (bar
.pos
!= BAR_OFF
) || (clients
&& clients
->next
);
386 draw_border(Client
*c
) {
388 int x
, y
, maxlen
, attrs
= NORMAL_ATTR
;
392 if (sel
== c
|| (runinall
&& !c
->minimized
))
393 attrs
= SELECTED_ATTR
;
394 if (sel
!= c
&& c
->urgent
)
397 wattrset(c
->window
, attrs
);
398 getyx(c
->window
, y
, x
);
399 mvwhline(c
->window
, 0, 0, ACS_HLINE
, c
->w
);
403 if ((size_t)maxlen
< sizeof(c
->title
)) {
404 t
= c
->title
[maxlen
];
405 c
->title
[maxlen
] = '\0';
408 mvwprintw(c
->window
, 0, 2, "[%s%s#%d]",
409 *c
->title
? c
->title
: "",
410 *c
->title
? " | " : "",
413 c
->title
[maxlen
] = t
;
414 wmove(c
->window
, y
, x
);
418 draw_content(Client
*c
) {
419 vt_draw(c
->term
, c
->window
, c
->has_title_line
, 0);
424 if (is_content_visible(c
)) {
425 redrawwin(c
->window
);
428 if (!isarrange(fullscreen
) || sel
== c
)
430 wnoutrefresh(c
->window
);
435 if (!nextvisible(clients
)) {
444 if (!isarrange(fullscreen
)) {
445 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
)) {
451 /* as a last step the selected window is redrawn,
452 * this has the effect that the cursor position is
461 unsigned int m
= 0, n
= 0;
462 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
)) {
468 attrset(NORMAL_ATTR
);
469 if (bar
.fd
== -1 && bar
.autohide
) {
470 if ((!clients
|| !clients
->next
) && n
== 1)
476 if (m
&& !isarrange(fullscreen
))
479 if (m
&& !isarrange(fullscreen
)) {
480 unsigned int i
= 0, nw
= waw
/ m
, nx
= wax
;
481 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
)) {
483 resize(c
, nx
, way
+wah
, ++i
== m
? waw
- nx
: nw
, 1);
490 wnoutrefresh(stdscr
);
502 for (int o
= 1; c
; c
= nextvisible(c
->next
), o
++)
507 attachafter(Client
*c
, Client
*a
) { /* attach c after a */
511 for (a
= clients
; a
&& a
->next
; a
= a
->next
);
519 for (int o
= a
->order
; c
; c
= nextvisible(c
->next
))
525 attachstack(Client
*c
) {
534 c
->prev
->next
= c
->next
;
536 c
->next
->prev
= c
->prev
;
537 for (d
= nextvisible(c
->next
); d
; d
= nextvisible(d
->next
))
542 c
->next
= c
->prev
= NULL
;
546 settitle(Client
*c
) {
547 char *term
, *t
= title
;
548 if (!t
&& sel
== c
&& *c
->title
)
550 if (t
&& (term
= getenv("TERM")) && !strstr(term
, "linux"))
551 printf("\033]0;%s\007", t
);
555 detachstack(Client
*c
) {
557 for (tc
=&stack
; *tc
&& *tc
!= c
; tc
=&(*tc
)->snext
);
564 for (c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
570 lastsel
->urgent
= false;
571 if (!isarrange(fullscreen
)) {
572 draw_border(lastsel
);
573 wnoutrefresh(lastsel
->window
);
582 if (isarrange(fullscreen
)) {
586 wnoutrefresh(c
->window
);
589 curs_set(c
&& !c
->minimized
&& vt_cursor_visible(c
->term
));
593 applycolorrules(Client
*c
) {
594 const ColorRule
*r
= colorrules
;
595 short fg
= r
->color
->fg
, bg
= r
->color
->bg
;
596 attr_t attrs
= r
->attrs
;
598 for (unsigned int i
= 1; i
< LENGTH(colorrules
); i
++) {
600 if (strstr(c
->title
, r
->title
)) {
608 vt_default_colors_set(c
->term
, attrs
, fg
, bg
);
612 term_title_handler(Vt
*term
, const char *title
) {
613 Client
*c
= (Client
*)vt_data_get(term
);
615 strncpy(c
->title
, title
, sizeof(c
->title
) - 1);
616 c
->title
[title
? sizeof(c
->title
) - 1 : 0] = '\0';
618 if (!isarrange(fullscreen
) || sel
== c
)
624 term_urgent_handler(Vt
*term
) {
625 Client
*c
= (Client
*)vt_data_get(term
);
630 if (!isarrange(fullscreen
) && sel
!= c
&& isvisible(c
))
635 move_client(Client
*c
, int x
, int y
) {
636 if (c
->x
== x
&& c
->y
== y
)
638 debug("moving, x: %d y: %d\n", x
, y
);
639 if (mvwin(c
->window
, y
, x
) == ERR
) {
640 eprint("error moving, x: %d y: %d\n", x
, y
);
648 resize_client(Client
*c
, int w
, int h
) {
649 bool has_title_line
= show_border();
650 bool resize_window
= c
->w
!= w
|| c
->h
!= h
;
652 debug("resizing, w: %d h: %d\n", w
, h
);
653 if (wresize(c
->window
, h
, w
) == ERR
) {
654 eprint("error resizing, w: %d h: %d\n", w
, h
);
660 if (resize_window
|| c
->has_title_line
!= has_title_line
) {
661 c
->has_title_line
= has_title_line
;
662 vt_resize(c
->app
, h
- has_title_line
, w
);
664 vt_resize(c
->editor
, h
- has_title_line
, w
);
669 resize(Client
*c
, int x
, int y
, int w
, int h
) {
670 resize_client(c
, w
, h
);
671 move_client(c
, x
, y
);
675 get_client_by_coord(unsigned int x
, unsigned int y
) {
676 if (y
< way
|| y
>= wah
)
678 if (isarrange(fullscreen
))
680 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
)) {
681 if (x
>= c
->x
&& x
< c
->x
+ c
->w
&& y
>= c
->y
&& y
< c
->y
+ c
->h
) {
682 debug("mouse event, x: %d y: %d client: %d\n", x
, y
, c
->order
);
690 sigchld_handler(int sig
) {
695 while ((pid
= waitpid(-1, &status
, WNOHANG
)) != 0) {
697 if (errno
== ECHILD
) {
698 /* no more child processes */
701 eprint("waitpid: %s\n", strerror(errno
));
705 debug("child with pid %d died\n", pid
);
707 for (Client
*c
= clients
; c
; c
= c
->next
) {
712 if (c
->editor
&& vt_pid_get(c
->editor
) == pid
) {
713 c
->editor_died
= true;
723 sigwinch_handler(int sig
) {
724 screen
.need_resize
= true;
728 sigterm_handler(int sig
) {
733 resize_screen(void) {
736 if (ioctl(0, TIOCGWINSZ
, &ws
) == -1) {
737 getmaxyx(stdscr
, screen
.h
, screen
.w
);
739 screen
.w
= ws
.ws_col
;
740 screen
.h
= ws
.ws_row
;
743 debug("resize_screen(), w: %d h: %d\n", screen
.w
, screen
.h
);
745 resizeterm(screen
.h
, screen
.w
);
746 wresize(stdscr
, screen
.h
, screen
.w
);
753 keybinding(KeyCombo keys
) {
754 unsigned int keycount
= 0;
755 while (keycount
< MAX_KEYS
&& keys
[keycount
])
757 for (unsigned int b
= 0; b
< LENGTH(bindings
); b
++) {
758 for (unsigned int k
= 0; k
< keycount
; k
++) {
759 if (keys
[k
] != bindings
[b
].keys
[k
])
761 if (k
== keycount
- 1)
769 bitoftag(const char *tag
) {
771 for (i
= 0; (i
< LENGTH(tags
)) && (tags
[i
] != tag
); i
++);
772 return (i
< LENGTH(tags
)) ? (1 << i
) : ~0;
777 bool allminimized
= true;
778 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
)) {
780 allminimized
= false;
784 if (allminimized
&& nextvisible(clients
)) {
786 toggleminimize(NULL
);
792 tag(const char *args
[]) {
795 sel
->tags
= bitoftag(args
[0]) & TAGMASK
;
800 toggletag(const char *args
[]) {
803 unsigned int newtags
= sel
->tags
^ (bitoftag(args
[0]) & TAGMASK
);
811 toggleview(const char *args
[]) {
812 unsigned int newtagset
= tagset
[seltags
] ^ (bitoftag(args
[0]) & TAGMASK
);
814 tagset
[seltags
] = newtagset
;
820 view(const char *args
[]) {
821 unsigned int newtagset
= bitoftag(args
[0]) & TAGMASK
;
822 if (tagset
[seltags
] != newtagset
&& newtagset
) {
823 seltags
^= 1; /* toggle sel tagset */
824 tagset
[seltags
] = newtagset
;
830 viewprevtag(const char *args
[]) {
837 unsigned int len
= 1;
838 char buf
[8] = { '\e' };
841 /* pass characters following escape to the underlying app */
842 nodelay(stdscr
, TRUE
);
843 for (int t
; len
< sizeof(buf
) && (t
= getch()) != ERR
; len
++)
845 nodelay(stdscr
, FALSE
);
848 for (Client
*c
= runinall
? nextvisible(clients
) : sel
; c
; c
= nextvisible(c
->next
)) {
849 if (is_content_visible(c
)) {
852 vt_write(c
->term
, buf
, len
);
854 vt_keypress(c
->term
, code
);
866 if (mouse_events_enabled
) {
867 mask
= BUTTON1_CLICKED
| BUTTON2_CLICKED
;
868 for (unsigned int i
= 0; i
< LENGTH(buttons
); i
++)
869 mask
|= buttons
[i
].mask
;
871 mousemask(mask
, NULL
);
872 #endif /* CONFIG_MOUSE */
876 checkshell(const char *shell
) {
877 if (shell
== NULL
|| *shell
== '\0' || *shell
!= '/')
879 if (!strcmp(strrchr(shell
, '/')+1, dvtm_name
))
881 if (access(shell
, X_OK
))
888 const char *shell
= getenv("SHELL");
891 if (checkshell(shell
))
893 if ((pw
= getpwuid(getuid())) && checkshell(pw
->pw_shell
))
901 setlocale(LC_CTYPE
, "");
906 keypad(stdscr
, TRUE
);
910 vt_keytable_set(keytable
, LENGTH(keytable
));
911 for (unsigned int i
= 0; i
< LENGTH(colors
); i
++) {
914 colors
[i
].fg
= colors
[i
].fg256
;
916 colors
[i
].bg
= colors
[i
].bg256
;
918 colors
[i
].pair
= vt_color_reserve(colors
[i
].fg
, colors
[i
].bg
);
923 sigemptyset(&sa
.sa_mask
);
924 sa
.sa_handler
= sigwinch_handler
;
925 sigaction(SIGWINCH
, &sa
, NULL
);
926 sa
.sa_handler
= sigchld_handler
;
927 sigaction(SIGCHLD
, &sa
, NULL
);
928 sa
.sa_handler
= sigterm_handler
;
929 sigaction(SIGTERM
, &sa
, NULL
);
930 sa
.sa_handler
= SIG_IGN
;
931 sigaction(SIGPIPE
, &sa
, NULL
);
941 Client
*next
= nextvisible(clients
);
944 toggleminimize(NULL
);
952 wnoutrefresh(c
->window
);
955 if (!clients
&& LENGTH(actions
)) {
956 if (!strcmp(c
->cmd
, shell
))
979 unlink(cmdfifo
.file
);
982 static char *getcwd_by_pid(Client
*c
) {
986 snprintf(buf
, sizeof buf
, "/proc/%d/cwd", c
->pid
);
987 return realpath(buf
, NULL
);
991 create(const char *args
[]) {
993 const char *pargs
[4];
994 char buf
[8], *cwd
= NULL
;
995 const char *env
[] = {
996 "DVTM_WINDOW_ID", buf
,
1000 if (args
&& args
[0]) {
1011 Client
*c
= calloc(1, sizeof(Client
));
1014 c
->tags
= tagset
[seltags
];
1015 c
->id
= ++cmdfifo
.id
;
1016 snprintf(buf
, sizeof buf
, "%d", c
->id
);
1018 if (!(c
->window
= newwin(wah
, waw
, way
, wax
))) {
1023 c
->term
= c
->app
= vt_create(screen
.h
, screen
.w
, screen
.history
);
1031 if (args
&& args
[1]) {
1032 strncpy(c
->title
, args
[1], sizeof(c
->title
) - 1);
1033 c
->title
[sizeof(c
->title
) - 1] = '\0';
1035 if (args
&& args
[2])
1036 cwd
= !strcmp(args
[2], "$CWD") ? getcwd_by_pid(sel
) : (char*)args
[2];
1037 c
->pid
= vt_forkpty(c
->term
, shell
, pargs
, cwd
, env
, NULL
, NULL
);
1038 if (args
&& args
[2] && !strcmp(args
[2], "$CWD"))
1040 vt_data_set(c
->term
, c
);
1041 vt_title_handler_set(c
->term
, term_title_handler
);
1042 vt_urgent_handler_set(c
->term
, term_urgent_handler
);
1045 debug("client with pid %d forked\n", c
->pid
);
1052 copymode(const char *args
[]) {
1053 if (!sel
|| sel
->editor
)
1055 if (!(sel
->editor
= vt_create(sel
->h
- sel
->has_title_line
, sel
->w
, 0)))
1058 char *ed
= getenv("DVTM_EDITOR");
1059 int *to
= &sel
->editor_fds
[0], *from
= NULL
;
1060 sel
->editor_fds
[0] = sel
->editor_fds
[1] = -1;
1063 ed
= getenv("EDITOR");
1065 ed
= getenv("PAGER");
1067 ed
= editors
[0].name
;
1069 const char **argv
= (const char*[]){ ed
, "-", NULL
, NULL
};
1071 bool colored
= false;
1073 for (unsigned int i
= 0; i
< LENGTH(editors
); i
++) {
1074 if (!strcmp(editors
[i
].name
, ed
)) {
1075 for (int j
= 1; editors
[i
].argv
[j
]; j
++) {
1076 if (strstr(editors
[i
].argv
[j
], "%d")) {
1077 int line
= vt_content_start(sel
->app
);
1078 snprintf(argline
, sizeof(argline
), "+%d", line
);
1081 argv
[j
] = editors
[i
].argv
[j
];
1084 if (editors
[i
].filter
)
1085 from
= &sel
->editor_fds
[1];
1086 colored
= editors
[i
].color
;
1091 if (vt_forkpty(sel
->editor
, ed
, argv
, NULL
, NULL
, to
, from
) < 0) {
1092 vt_destroy(sel
->editor
);
1097 sel
->term
= sel
->editor
;
1099 if (sel
->editor_fds
[0] != -1) {
1101 size_t len
= vt_content_get(sel
->app
, &buf
, colored
);
1104 ssize_t res
= write(sel
->editor_fds
[0], cur
, len
);
1106 if (errno
== EAGAIN
|| errno
== EINTR
)
1114 close(sel
->editor_fds
[0]);
1115 sel
->editor_fds
[0] = -1;
1119 vt_write(sel
->editor
, args
[0], strlen(args
[0]));
1123 focusn(const char *args
[]) {
1124 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
)) {
1125 if (c
->order
== atoi(args
[0])) {
1128 toggleminimize(NULL
);
1135 focusnext(const char *args
[]) {
1139 for (c
= sel
->next
; c
&& !isvisible(c
); c
= c
->next
);
1141 for (c
= clients
; c
&& !isvisible(c
); c
= c
->next
);
1147 focusnextnm(const char *args
[]) {
1152 c
= nextvisible(c
->next
);
1154 c
= nextvisible(clients
);
1155 } while (c
->minimized
&& c
!= sel
);
1160 focusprev(const char *args
[]) {
1164 for (c
= sel
->prev
; c
&& !isvisible(c
); c
= c
->prev
);
1166 for (c
= clients
; c
&& c
->next
; c
= c
->next
);
1167 for (; c
&& !isvisible(c
); c
= c
->prev
);
1174 focusprevnm(const char *args
[]) {
1179 for (c
= c
->prev
; c
&& !isvisible(c
); c
= c
->prev
);
1181 for (c
= clients
; c
&& c
->next
; c
= c
->next
);
1182 for (; c
&& !isvisible(c
); c
= c
->prev
);
1184 } while (c
&& c
!= sel
&& c
->minimized
);
1189 focuslast(const char *args
[]) {
1195 killclient(const char *args
[]) {
1198 debug("killing client with pid: %d\n", sel
->pid
);
1199 kill(-sel
->pid
, SIGKILL
);
1203 paste(const char *args
[]) {
1204 if (sel
&& copyreg
.data
)
1205 vt_write(sel
->term
, copyreg
.data
, copyreg
.len
);
1209 quit(const char *args
[]) {
1215 redraw(const char *args
[]) {
1216 for (Client
*c
= clients
; c
; c
= c
->next
) {
1217 if (!c
->minimized
) {
1220 wnoutrefresh(c
->window
);
1227 scrollback(const char *args
[]) {
1228 if (!is_content_visible(sel
))
1231 if (!args
[0] || atoi(args
[0]) < 0)
1232 vt_scroll(sel
->term
, -sel
->h
/2);
1234 vt_scroll(sel
->term
, sel
->h
/2);
1237 curs_set(vt_cursor_visible(sel
->term
));
1241 send(const char *args
[]) {
1242 if (sel
&& args
&& args
[0])
1243 vt_write(sel
->term
, args
[0], strlen(args
[0]));
1247 setlayout(const char *args
[]) {
1250 if (!args
|| !args
[0]) {
1251 if (++layout
== &layouts
[LENGTH(layouts
)])
1252 layout
= &layouts
[0];
1254 for (i
= 0; i
< LENGTH(layouts
); i
++)
1255 if (!strcmp(args
[0], layouts
[i
].symbol
))
1257 if (i
== LENGTH(layouts
))
1259 layout
= &layouts
[i
];
1265 incnmaster(const char *args
[]) {
1268 if (isarrange(fullscreen
) || isarrange(grid
))
1270 /* arg handling, manipulate nmaster */
1271 if (args
[0] == NULL
) {
1272 screen
.nmaster
= NMASTER
;
1273 } else if (sscanf(args
[0], "%d", &delta
) == 1) {
1274 if (args
[0][0] == '+' || args
[0][0] == '-')
1275 screen
.nmaster
+= delta
;
1277 screen
.nmaster
= delta
;
1278 if (screen
.nmaster
< 1)
1285 setmfact(const char *args
[]) {
1288 if (isarrange(fullscreen
) || isarrange(grid
))
1290 /* arg handling, manipulate mfact */
1291 if (args
[0] == NULL
) {
1292 screen
.mfact
= MFACT
;
1293 } else if (sscanf(args
[0], "%f", &delta
) == 1) {
1294 if (args
[0][0] == '+' || args
[0][0] == '-')
1295 screen
.mfact
+= delta
;
1297 screen
.mfact
= delta
;
1298 if (screen
.mfact
< 0.1)
1300 else if (screen
.mfact
> 0.9)
1307 startup(const char *args
[]) {
1308 for (unsigned int i
= 0; i
< LENGTH(actions
); i
++)
1309 actions
[i
].cmd(actions
[i
].args
);
1313 togglebar(const char *args
[]) {
1314 if (bar
.pos
== BAR_OFF
)
1318 bar
.autohide
= false;
1324 togglebarpos(const char *args
[]) {
1325 switch (bar
.pos
== BAR_OFF
? bar
.lastpos
: bar
.pos
) {
1327 bar
.pos
= BAR_BOTTOM
;
1338 toggleminimize(const char *args
[]) {
1343 /* the last window can't be minimized */
1344 if (!sel
->minimized
) {
1345 for (n
= 0, c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
))
1351 sel
->minimized
= !sel
->minimized
;
1353 /* check whether the master client was minimized */
1354 if (sel
== nextvisible(clients
) && sel
->minimized
) {
1355 c
= nextvisible(sel
->next
);
1360 for (; c
&& (t
= nextvisible(c
->next
)) && !t
->minimized
; c
= t
);
1362 } else if (m
->minimized
) {
1363 /* non master window got minimized move it above all other
1367 for (c
= nextvisible(clients
); c
&& (t
= nextvisible(c
->next
)) && !t
->minimized
; c
= t
);
1369 } else { /* window is no longer minimized, move it to the master area */
1378 togglemouse(const char *args
[]) {
1379 mouse_events_enabled
= !mouse_events_enabled
;
1384 togglerunall(const char *args
[]) {
1385 runinall
= !runinall
;
1390 zoom(const char *args
[]) {
1395 if (args
&& args
[0])
1397 if ((c
= sel
) == nextvisible(clients
))
1398 if (!(c
= nextvisible(c
->next
)))
1404 toggleminimize(NULL
);
1408 /* commands for use by mouse bindings */
1410 mouse_focus(const char *args
[]) {
1412 if (msel
->minimized
)
1413 toggleminimize(NULL
);
1417 mouse_fullscreen(const char *args
[]) {
1419 if (isarrange(fullscreen
))
1426 mouse_minimize(const char *args
[]) {
1428 toggleminimize(NULL
);
1432 mouse_zoom(const char *args
[]) {
1438 get_cmd_by_name(const char *name
) {
1439 for (unsigned int i
= 0; i
< LENGTH(commands
); i
++) {
1440 if (!strcmp(name
, commands
[i
].name
))
1441 return &commands
[i
];
1447 handle_cmdfifo(void) {
1449 char *p
, *s
, cmdbuf
[512], c
;
1451 switch (r
= read(cmdfifo
.fd
, cmdbuf
, sizeof cmdbuf
- 1)) {
1460 /* find the command name */
1461 for (; *p
== ' ' || *p
== '\n'; p
++);
1462 for (s
= p
; *p
&& *p
!= ' ' && *p
!= '\n'; p
++);
1465 if (*s
&& (cmd
= get_cmd_by_name(s
)) != NULL
) {
1468 const char *args
[MAX_ARGS
], *arg
;
1469 memset(args
, 0, sizeof(args
));
1470 /* if arguments were specified in config.h ignore the one given via
1471 * the named pipe and thus skip everything until we find a new line
1473 if (cmd
->action
.args
[0] || c
== '\n') {
1474 debug("execute %s", s
);
1475 cmd
->action
.cmd(cmd
->action
.args
);
1476 while (*p
&& *p
!= '\n')
1480 /* no arguments were given in config.h so we parse the command line */
1484 for (; (c
= *p
); p
++) {
1487 /* remove the escape character '\\' move every
1488 * following character to the left by one position
1508 /* remove trailing quote if there is one */
1509 if (*(p
- 1) == '\'' || *(p
- 1) == '\"')
1512 /* remove leading quote if there is one */
1513 if (*arg
== '\'' || *arg
== '\"')
1515 if (argc
< MAX_ARGS
)
1525 if (c
== '\n' || *p
== '\n') {
1528 debug("execute %s", s
);
1529 for(int i
= 0; i
< argc
; i
++)
1530 debug(" %s", args
[i
]);
1532 cmd
->action
.cmd(args
);
1542 handle_mouse(void) {
1546 if (getmouse(&event
) != OK
)
1548 msel
= get_client_by_coord(event
.x
, event
.y
);
1553 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
);
1555 vt_mouse(msel
->term
, event
.x
- msel
->x
, event
.y
- msel
->y
, event
.bstate
);
1557 for (i
= 0; i
< LENGTH(buttons
); i
++) {
1558 if (event
.bstate
& buttons
[i
].mask
)
1559 buttons
[i
].action
.cmd(buttons
[i
].action
.args
);
1563 #endif /* CONFIG_MOUSE */
1567 handle_statusbar(void) {
1570 switch (r
= read(bar
.fd
, bar
.text
, sizeof bar
.text
- 1)) {
1572 strncpy(bar
.text
, strerror(errno
), sizeof bar
.text
- 1);
1573 bar
.text
[sizeof bar
.text
- 1] = '\0';
1581 p
= bar
.text
+ r
- 1;
1582 for (; p
>= bar
.text
&& *p
== '\n'; *p
-- = '\0');
1583 for (; p
>= bar
.text
&& *p
!= '\n'; --p
);
1585 memmove(bar
.text
, p
+ 1, strlen(p
));
1591 handle_editor(Client
*c
) {
1592 if (!copyreg
.data
&& (copyreg
.data
= malloc(screen
.history
)))
1593 copyreg
.size
= screen
.history
;
1595 while (c
->editor_fds
[1] != -1 && copyreg
.len
< copyreg
.size
) {
1596 ssize_t len
= read(c
->editor_fds
[1], copyreg
.data
+ copyreg
.len
, copyreg
.size
- copyreg
.len
);
1605 if (copyreg
.len
== copyreg
.size
) {
1607 if (!(copyreg
.data
= realloc(copyreg
.data
, copyreg
.size
))) {
1613 c
->editor_died
= false;
1614 c
->editor_fds
[1] = -1;
1615 vt_destroy(c
->editor
);
1620 wnoutrefresh(c
->window
);
1624 open_or_create_fifo(const char *name
, const char **name_created
) {
1629 if ((fd
= open(name
, O_RDWR
|O_NONBLOCK
)) == -1) {
1630 if (errno
== ENOENT
&& !mkfifo(name
, S_IRUSR
|S_IWUSR
)) {
1631 *name_created
= name
;
1634 error("%s\n", strerror(errno
));
1638 if (fstat(fd
, &info
) == -1)
1639 error("%s\n", strerror(errno
));
1640 if (!S_ISFIFO(info
.st_mode
))
1641 error("%s is not a named pipe\n", name
);
1648 eprint("usage: dvtm [-v] [-M] [-m mod] [-d delay] [-h lines] [-t title] "
1649 "[-s status-fifo] [-c cmd-fifo] [cmd...]\n");
1654 parse_args(int argc
, char *argv
[]) {
1656 const char *name
= argv
[0];
1658 if (name
&& (name
= strrchr(name
, '/')))
1659 dvtm_name
= name
+ 1;
1660 if (!getenv("ESCDELAY"))
1662 for (int arg
= 1; arg
< argc
; arg
++) {
1663 if (argv
[arg
][0] != '-') {
1664 const char *args
[] = { argv
[arg
], NULL
, NULL
};
1672 if (argv
[arg
][1] != 'v' && argv
[arg
][1] != 'M' && (arg
+ 1) >= argc
)
1674 switch (argv
[arg
][1]) {
1676 puts("dvtm-"VERSION
" © 2007-2015 Marc André Tanner");
1679 mouse_events_enabled
= !mouse_events_enabled
;
1682 char *mod
= argv
[++arg
];
1683 if (mod
[0] == '^' && mod
[1])
1684 *mod
= CTRL(mod
[1]);
1685 for (unsigned int b
= 0; b
< LENGTH(bindings
); b
++)
1686 if (bindings
[b
].keys
[0] == MOD
)
1687 bindings
[b
].keys
[0] = *mod
;
1691 set_escdelay(atoi(argv
[++arg
]));
1694 else if (ESCDELAY
> 1000)
1698 screen
.history
= atoi(argv
[++arg
]);
1701 title
= argv
[++arg
];
1704 bar
.fd
= open_or_create_fifo(argv
[++arg
], &bar
.file
);
1709 cmdfifo
.fd
= open_or_create_fifo(argv
[++arg
], &cmdfifo
.file
);
1710 if (!(fifo
= realpath(argv
[arg
], NULL
)))
1711 error("%s\n", strerror(errno
));
1712 setenv("DVTM_CMD_FIFO", fifo
, 1);
1723 main(int argc
, char *argv
[]) {
1725 unsigned int key_index
= 0;
1726 memset(keys
, 0, sizeof(keys
));
1727 sigset_t emptyset
, blockset
;
1729 setenv("DVTM", VERSION
, 1);
1730 if (!parse_args(argc
, argv
)) {
1735 sigemptyset(&emptyset
);
1736 sigemptyset(&blockset
);
1737 sigaddset(&blockset
, SIGWINCH
);
1738 sigaddset(&blockset
, SIGCHLD
);
1739 sigprocmask(SIG_BLOCK
, &blockset
, NULL
);
1745 if (screen
.need_resize
) {
1747 screen
.need_resize
= false;
1751 FD_SET(STDIN_FILENO
, &rd
);
1753 if (cmdfifo
.fd
!= -1) {
1754 FD_SET(cmdfifo
.fd
, &rd
);
1759 FD_SET(bar
.fd
, &rd
);
1760 nfds
= MAX(nfds
, bar
.fd
);
1763 for (Client
*c
= clients
; c
; ) {
1764 if (c
->editor
&& c
->editor_died
)
1766 if (!c
->editor
&& c
->died
) {
1767 Client
*t
= c
->next
;
1772 int pty
= c
->editor
? vt_pty_get(c
->editor
) : vt_pty_get(c
->app
);
1774 nfds
= MAX(nfds
, pty
);
1779 r
= pselect(nfds
+ 1, &rd
, NULL
, NULL
, NULL
, &emptyset
);
1781 if (r
== -1 && errno
== EINTR
)
1789 if (FD_ISSET(STDIN_FILENO
, &rd
)) {
1792 keys
[key_index
++] = code
;
1793 KeyBinding
*binding
= NULL
;
1794 if (code
== KEY_MOUSE
) {
1797 } else if ((binding
= keybinding(keys
))) {
1798 unsigned int key_length
= 0;
1799 while (key_length
< MAX_KEYS
&& binding
->keys
[key_length
])
1801 if (key_index
== key_length
) {
1802 binding
->action
.cmd(binding
->action
.args
);
1804 memset(keys
, 0, sizeof(keys
));
1808 memset(keys
, 0, sizeof(keys
));
1812 if (r
== 1) /* no data available on pty's */
1816 if (cmdfifo
.fd
!= -1 && FD_ISSET(cmdfifo
.fd
, &rd
))
1819 if (bar
.fd
!= -1 && FD_ISSET(bar
.fd
, &rd
))
1822 for (Client
*c
= clients
; c
; c
= c
->next
) {
1823 if (FD_ISSET(vt_pty_get(c
->term
), &rd
)) {
1824 if (vt_process(c
->term
) < 0 && errno
== EIO
) {
1826 c
->editor_died
= true;
1833 if (c
!= sel
&& is_content_visible(c
)) {
1835 wnoutrefresh(c
->window
);
1839 if (is_content_visible(sel
)) {
1841 curs_set(vt_cursor_visible(sel
->term
));
1842 wnoutrefresh(sel
->window
);