2 * The initial "port" of dwm to curses was done by
4 * © 2007-2016 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
[]);
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 focusid(const char *args
[]);
183 static void focusnext(const char *args
[]);
184 static void focusnextnm(const char *args
[]);
185 static void focusprev(const char *args
[]);
186 static void focusprevnm(const char *args
[]);
187 static void focuslast(const char *args
[]);
188 static void killclient(const char *args
[]);
189 static void paste(const char *args
[]);
190 static void quit(const char *args
[]);
191 static void redraw(const char *args
[]);
192 static void scrollback(const char *args
[]);
193 static void send(const char *args
[]);
194 static void setlayout(const char *args
[]);
195 static void incnmaster(const char *args
[]);
196 static void setmfact(const char *args
[]);
197 static void startup(const char *args
[]);
198 static void tag(const char *args
[]);
199 static void tagid(const char *args
[]);
200 static void togglebar(const char *args
[]);
201 static void togglebarpos(const char *args
[]);
202 static void toggleminimize(const char *args
[]);
203 static void togglemouse(const char *args
[]);
204 static void togglerunall(const char *args
[]);
205 static void toggletag(const char *args
[]);
206 static void toggleview(const char *args
[]);
207 static void viewprevtag(const char *args
[]);
208 static void view(const char *args
[]);
209 static void zoom(const char *args
[]);
211 /* commands for use by mouse bindings */
212 static void mouse_focus(const char *args
[]);
213 static void mouse_fullscreen(const char *args
[]);
214 static void mouse_minimize(const char *args
[]);
215 static void mouse_zoom(const char *args
[]);
217 /* functions and variables available to layouts via config.h */
218 static Client
* nextvisible(Client
*c
);
219 static void focus(Client
*c
);
220 static void resize(Client
*c
, int x
, int y
, int w
, int h
);
221 extern Screen screen
;
222 static unsigned int waw
, wah
, wax
, way
;
223 static Client
*clients
= NULL
;
228 /* global variables */
229 static const char *dvtm_name
= "dvtm";
230 Screen screen
= { .mfact
= MFACT
, .nmaster
= NMASTER
, .history
= SCROLL_HISTORY
};
231 static Client
*stack
= NULL
;
232 static Client
*sel
= NULL
;
233 static Client
*lastsel
= NULL
;
234 static Client
*msel
= NULL
;
235 static unsigned int seltags
;
236 static unsigned int tagset
[2] = { 1, 1 };
237 static bool mouse_events_enabled
= ENABLE_MOUSE
;
238 static Layout
*layout
= layouts
;
239 static StatusBar bar
= { .fd
= -1, .lastpos
= BAR_POS
, .pos
= BAR_POS
, .autohide
= BAR_AUTOHIDE
, .h
= 1 };
240 static CmdFifo cmdfifo
= { .fd
= -1 };
241 static const char *shell
;
242 static Register copyreg
;
243 static volatile sig_atomic_t running
= true;
244 static bool runinall
= false;
247 eprint(const char *errstr
, ...) {
249 va_start(ap
, errstr
);
250 vfprintf(stderr
, errstr
, ap
);
255 error(const char *errstr
, ...) {
257 va_start(ap
, errstr
);
258 vfprintf(stderr
, errstr
, ap
);
264 isarrange(void (*func
)()) {
265 return func
== layout
->arrange
;
269 isvisible(Client
*c
) {
270 return c
->tags
& tagset
[seltags
];
274 is_content_visible(Client
*c
) {
277 if (isarrange(fullscreen
))
279 return isvisible(c
) && !c
->minimized
;
283 nextvisible(Client
*c
) {
284 for (; c
&& !isvisible(c
); c
= c
->next
);
295 if (bar
.pos
== BAR_TOP
) {
298 } else if (bar
.pos
== BAR_BOTTOM
) {
306 if (bar
.pos
!= BAR_OFF
) {
307 bar
.lastpos
= bar
.pos
;
314 if (bar
.pos
== BAR_OFF
)
315 bar
.pos
= bar
.lastpos
;
320 int sx
, sy
, x
, y
, width
;
321 unsigned int occupied
= 0, urgent
= 0;
322 if (bar
.pos
== BAR_OFF
)
325 for (Client
*c
= clients
; c
; c
= c
->next
) {
331 getyx(stdscr
, sy
, sx
);
335 for (unsigned int i
= 0; i
< LENGTH(tags
); i
++){
336 if (tagset
[seltags
] & (1 << i
))
338 else if (urgent
& (1 << i
))
340 else if (occupied
& (1 << i
))
341 attrset(TAG_OCCUPIED
);
344 printw(TAG_SYMBOL
, tags
[i
]);
347 attrset(runinall
? TAG_SEL
: TAG_NORMAL
);
348 addstr(layout
->symbol
);
353 int maxwidth
= screen
.w
- x
- 2;
358 wchar_t wbuf
[sizeof bar
.text
];
359 size_t numchars
= mbstowcs(wbuf
, bar
.text
, sizeof bar
.text
);
361 if (numchars
!= (size_t)-1 && (width
= wcswidth(wbuf
, maxwidth
)) != -1) {
363 for (pos
= 0; pos
+ width
< maxwidth
; pos
++)
366 for (size_t i
= 0; i
< numchars
; i
++) {
367 pos
+= wcwidth(wbuf
[i
]);
377 mvaddch(bar
.y
, screen
.w
- 1, BAR_END
);
378 attrset(NORMAL_ATTR
);
380 wnoutrefresh(stdscr
);
385 return (bar
.pos
!= BAR_OFF
) || (clients
&& clients
->next
);
389 draw_border(Client
*c
) {
391 int x
, y
, maxlen
, attrs
= NORMAL_ATTR
;
395 if (sel
!= c
&& c
->urgent
)
397 if (sel
== c
|| (runinall
&& !c
->minimized
))
398 attrs
= SELECTED_ATTR
;
400 wattrset(c
->window
, attrs
);
401 getyx(c
->window
, y
, x
);
402 mvwhline(c
->window
, 0, 0, ACS_HLINE
, c
->w
);
406 if ((size_t)maxlen
< sizeof(c
->title
)) {
407 t
= c
->title
[maxlen
];
408 c
->title
[maxlen
] = '\0';
411 mvwprintw(c
->window
, 0, 2, "[%s%s#%d]",
412 *c
->title
? c
->title
: "",
413 *c
->title
? " | " : "",
416 c
->title
[maxlen
] = t
;
417 wmove(c
->window
, y
, x
);
421 draw_content(Client
*c
) {
422 vt_draw(c
->term
, c
->window
, c
->has_title_line
, 0);
427 if (is_content_visible(c
)) {
428 redrawwin(c
->window
);
431 if (!isarrange(fullscreen
) || sel
== c
)
433 wnoutrefresh(c
->window
);
438 if (!nextvisible(clients
)) {
447 if (!isarrange(fullscreen
)) {
448 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
)) {
453 /* as a last step the selected window is redrawn,
454 * this has the effect that the cursor position is
463 unsigned int m
= 0, n
= 0;
464 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
)) {
470 attrset(NORMAL_ATTR
);
471 if (bar
.fd
== -1 && bar
.autohide
) {
472 if ((!clients
|| !clients
->next
) && n
== 1)
478 if (m
&& !isarrange(fullscreen
))
481 if (m
&& !isarrange(fullscreen
)) {
482 unsigned int i
= 0, nw
= waw
/ m
, nx
= wax
;
483 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
)) {
485 resize(c
, nx
, way
+wah
, ++i
== m
? waw
- nx
: nw
, 1);
492 wnoutrefresh(stdscr
);
504 for (int o
= 1; c
; c
= nextvisible(c
->next
), o
++)
509 attachafter(Client
*c
, Client
*a
) { /* attach c after a */
513 for (a
= clients
; a
&& a
->next
; a
= a
->next
);
521 for (int o
= a
->order
; c
; c
= nextvisible(c
->next
))
527 attachstack(Client
*c
) {
536 c
->prev
->next
= c
->next
;
538 c
->next
->prev
= c
->prev
;
539 for (d
= nextvisible(c
->next
); d
; d
= nextvisible(d
->next
))
544 c
->next
= c
->prev
= NULL
;
548 settitle(Client
*c
) {
549 char *term
, *t
= title
;
550 if (!t
&& sel
== c
&& *c
->title
)
552 if (t
&& (term
= getenv("TERM")) && !strstr(term
, "linux")) {
553 printf("\033]0;%s\007", t
);
559 detachstack(Client
*c
) {
561 for (tc
= &stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
568 for (c
= stack
; c
&& !isvisible(c
); c
= c
->snext
);
574 lastsel
->urgent
= false;
575 if (!isarrange(fullscreen
)) {
576 draw_border(lastsel
);
577 wnoutrefresh(lastsel
->window
);
586 if (isarrange(fullscreen
)) {
590 wnoutrefresh(c
->window
);
593 curs_set(c
&& !c
->minimized
&& vt_cursor_visible(c
->term
));
597 applycolorrules(Client
*c
) {
598 const ColorRule
*r
= colorrules
;
599 short fg
= r
->color
->fg
, bg
= r
->color
->bg
;
600 attr_t attrs
= r
->attrs
;
602 for (unsigned int i
= 1; i
< LENGTH(colorrules
); i
++) {
604 if (strstr(c
->title
, r
->title
)) {
612 vt_default_colors_set(c
->term
, attrs
, fg
, bg
);
616 term_title_handler(Vt
*term
, const char *title
) {
617 Client
*c
= (Client
*)vt_data_get(term
);
619 strncpy(c
->title
, title
, sizeof(c
->title
) - 1);
620 c
->title
[title
? sizeof(c
->title
) - 1 : 0] = '\0';
622 if (!isarrange(fullscreen
) || sel
== c
)
628 term_urgent_handler(Vt
*term
) {
629 Client
*c
= (Client
*)vt_data_get(term
);
634 if (!isarrange(fullscreen
) && sel
!= c
&& isvisible(c
))
639 move_client(Client
*c
, int x
, int y
) {
640 if (c
->x
== x
&& c
->y
== y
)
642 debug("moving, x: %d y: %d\n", x
, y
);
643 if (mvwin(c
->window
, y
, x
) == ERR
) {
644 eprint("error moving, x: %d y: %d\n", x
, y
);
652 resize_client(Client
*c
, int w
, int h
) {
653 bool has_title_line
= show_border();
654 bool resize_window
= c
->w
!= w
|| c
->h
!= h
;
656 debug("resizing, w: %d h: %d\n", w
, h
);
657 if (wresize(c
->window
, h
, w
) == ERR
) {
658 eprint("error resizing, w: %d h: %d\n", w
, h
);
664 if (resize_window
|| c
->has_title_line
!= has_title_line
) {
665 c
->has_title_line
= has_title_line
;
666 vt_resize(c
->app
, h
- has_title_line
, w
);
668 vt_resize(c
->editor
, h
- has_title_line
, w
);
673 resize(Client
*c
, int x
, int y
, int w
, int h
) {
674 resize_client(c
, w
, h
);
675 move_client(c
, x
, y
);
679 get_client_by_coord(unsigned int x
, unsigned int y
) {
680 if (y
< way
|| y
>= wah
)
682 if (isarrange(fullscreen
))
684 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
)) {
685 if (x
>= c
->x
&& x
< c
->x
+ c
->w
&& y
>= c
->y
&& y
< c
->y
+ c
->h
) {
686 debug("mouse event, x: %d y: %d client: %d\n", x
, y
, c
->order
);
694 sigchld_handler(int sig
) {
699 while ((pid
= waitpid(-1, &status
, WNOHANG
)) != 0) {
701 if (errno
== ECHILD
) {
702 /* no more child processes */
705 eprint("waitpid: %s\n", strerror(errno
));
709 debug("child with pid %d died\n", pid
);
711 for (Client
*c
= clients
; c
; c
= c
->next
) {
716 if (c
->editor
&& vt_pid_get(c
->editor
) == pid
) {
717 c
->editor_died
= true;
727 sigwinch_handler(int sig
) {
728 screen
.need_resize
= true;
732 sigterm_handler(int sig
) {
737 resize_screen(void) {
740 if (ioctl(0, TIOCGWINSZ
, &ws
) == -1) {
741 getmaxyx(stdscr
, screen
.h
, screen
.w
);
743 screen
.w
= ws
.ws_col
;
744 screen
.h
= ws
.ws_row
;
747 debug("resize_screen(), w: %d h: %d\n", screen
.w
, screen
.h
);
749 resizeterm(screen
.h
, screen
.w
);
750 wresize(stdscr
, screen
.h
, screen
.w
);
757 keybinding(KeyCombo keys
, unsigned int keycount
) {
758 for (unsigned int b
= 0; b
< LENGTH(bindings
); b
++) {
759 for (unsigned int k
= 0; k
< keycount
; k
++) {
760 if (keys
[k
] != bindings
[b
].keys
[k
])
762 if (k
== keycount
- 1)
770 bitoftag(const char *tag
) {
774 for (i
= 0; (i
< LENGTH(tags
)) && strcmp(tags
[i
], tag
); i
++);
775 return (i
< LENGTH(tags
)) ? (1 << i
) : 0;
780 bool allminimized
= true;
781 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
)) {
783 allminimized
= false;
787 if (allminimized
&& nextvisible(clients
)) {
789 toggleminimize(NULL
);
795 tag(const char *args
[]) {
798 sel
->tags
= bitoftag(args
[0]) & TAGMASK
;
803 tagid(const char *args
[]) {
804 if (!args
[0] || !args
[1])
807 const int win_id
= atoi(args
[0]);
808 for (Client
*c
= clients
; c
; c
= c
->next
) {
809 if (c
->id
== win_id
) {
810 unsigned int ntags
= c
->tags
;
811 for (unsigned int i
= 1; i
< MAX_ARGS
&& args
[i
]; i
++) {
812 if (args
[i
][0] == '+')
813 ntags
|= bitoftag(args
[i
]+1);
814 else if (args
[i
][0] == '-')
815 ntags
&= ~bitoftag(args
[i
]+1);
817 ntags
= bitoftag(args
[i
]);
830 toggletag(const char *args
[]) {
833 unsigned int newtags
= sel
->tags
^ (bitoftag(args
[0]) & TAGMASK
);
841 toggleview(const char *args
[]) {
842 unsigned int newtagset
= tagset
[seltags
] ^ (bitoftag(args
[0]) & TAGMASK
);
844 tagset
[seltags
] = newtagset
;
850 view(const char *args
[]) {
851 unsigned int newtagset
= bitoftag(args
[0]) & TAGMASK
;
852 if (tagset
[seltags
] != newtagset
&& newtagset
) {
853 seltags
^= 1; /* toggle sel tagset */
854 tagset
[seltags
] = newtagset
;
860 viewprevtag(const char *args
[]) {
867 unsigned int len
= 1;
868 char buf
[8] = { '\e' };
871 /* pass characters following escape to the underlying app */
872 nodelay(stdscr
, TRUE
);
873 for (int t
; len
< sizeof(buf
) && (t
= getch()) != ERR
; len
++)
875 nodelay(stdscr
, FALSE
);
878 for (Client
*c
= runinall
? nextvisible(clients
) : sel
; c
; c
= nextvisible(c
->next
)) {
879 if (is_content_visible(c
)) {
882 vt_write(c
->term
, buf
, len
);
884 vt_keypress(c
->term
, code
);
896 if (mouse_events_enabled
) {
897 mask
= BUTTON1_CLICKED
| BUTTON2_CLICKED
;
898 for (unsigned int i
= 0; i
< LENGTH(buttons
); i
++)
899 mask
|= buttons
[i
].mask
;
901 mousemask(mask
, NULL
);
902 #endif /* CONFIG_MOUSE */
906 checkshell(const char *shell
) {
907 if (shell
== NULL
|| *shell
== '\0' || *shell
!= '/')
909 if (!strcmp(strrchr(shell
, '/')+1, dvtm_name
))
911 if (access(shell
, X_OK
))
918 const char *shell
= getenv("SHELL");
921 if (checkshell(shell
))
923 if ((pw
= getpwuid(getuid())) && checkshell(pw
->pw_shell
))
931 setlocale(LC_CTYPE
, "");
936 keypad(stdscr
, TRUE
);
940 vt_keytable_set(keytable
, LENGTH(keytable
));
941 for (unsigned int i
= 0; i
< LENGTH(colors
); i
++) {
944 colors
[i
].fg
= colors
[i
].fg256
;
946 colors
[i
].bg
= colors
[i
].bg256
;
948 colors
[i
].pair
= vt_color_reserve(colors
[i
].fg
, colors
[i
].bg
);
952 memset(&sa
, 0, sizeof sa
);
954 sigemptyset(&sa
.sa_mask
);
955 sa
.sa_handler
= sigwinch_handler
;
956 sigaction(SIGWINCH
, &sa
, NULL
);
957 sa
.sa_handler
= sigchld_handler
;
958 sigaction(SIGCHLD
, &sa
, NULL
);
959 sa
.sa_handler
= sigterm_handler
;
960 sigaction(SIGTERM
, &sa
, NULL
);
961 sa
.sa_handler
= SIG_IGN
;
962 sigaction(SIGPIPE
, &sa
, NULL
);
972 Client
*next
= nextvisible(clients
);
975 toggleminimize(NULL
);
983 wnoutrefresh(c
->window
);
986 if (!clients
&& LENGTH(actions
)) {
987 if (!strcmp(c
->cmd
, shell
))
1010 unlink(cmdfifo
.file
);
1013 static char *getcwd_by_pid(Client
*c
) {
1017 snprintf(buf
, sizeof buf
, "/proc/%d/cwd", c
->pid
);
1018 return realpath(buf
, NULL
);
1022 create(const char *args
[]) {
1023 const char *pargs
[4] = { shell
, NULL
};
1024 char buf
[8], *cwd
= NULL
;
1025 const char *env
[] = {
1026 "DVTM_WINDOW_ID", buf
,
1030 if (args
&& args
[0]) {
1035 Client
*c
= calloc(1, sizeof(Client
));
1038 c
->tags
= tagset
[seltags
];
1039 c
->id
= ++cmdfifo
.id
;
1040 snprintf(buf
, sizeof buf
, "%d", c
->id
);
1042 if (!(c
->window
= newwin(wah
, waw
, way
, wax
))) {
1047 c
->term
= c
->app
= vt_create(screen
.h
, screen
.w
, screen
.history
);
1054 c
->cmd
= (args
&& args
[0]) ? args
[0] : shell
;
1055 if (args
&& args
[1]) {
1056 strncpy(c
->title
, args
[1], sizeof(c
->title
) - 1);
1057 c
->title
[sizeof(c
->title
) - 1] = '\0';
1059 if (args
&& args
[2])
1060 cwd
= !strcmp(args
[2], "$CWD") ? getcwd_by_pid(sel
) : (char*)args
[2];
1061 c
->pid
= vt_forkpty(c
->term
, shell
, pargs
, cwd
, env
, NULL
, NULL
);
1062 if (args
&& args
[2] && !strcmp(args
[2], "$CWD"))
1064 vt_data_set(c
->term
, c
);
1065 vt_title_handler_set(c
->term
, term_title_handler
);
1066 vt_urgent_handler_set(c
->term
, term_urgent_handler
);
1069 debug("client with pid %d forked\n", c
->pid
);
1076 copymode(const char *args
[]) {
1077 if (!sel
|| sel
->editor
)
1079 if (!(sel
->editor
= vt_create(sel
->h
- sel
->has_title_line
, sel
->w
, 0)))
1082 char *ed
= getenv("DVTM_EDITOR");
1083 int *to
= &sel
->editor_fds
[0], *from
= NULL
;
1084 sel
->editor_fds
[0] = sel
->editor_fds
[1] = -1;
1087 ed
= getenv("EDITOR");
1089 ed
= getenv("PAGER");
1091 ed
= editors
[0].name
;
1093 const char **argv
= (const char*[]){ ed
, "-", NULL
, NULL
};
1095 bool colored
= false;
1097 for (unsigned int i
= 0; i
< LENGTH(editors
); i
++) {
1098 if (!strcmp(editors
[i
].name
, ed
)) {
1099 for (int j
= 1; editors
[i
].argv
[j
]; j
++) {
1100 if (strstr(editors
[i
].argv
[j
], "%d")) {
1101 int line
= vt_content_start(sel
->app
);
1102 snprintf(argline
, sizeof(argline
), "+%d", line
);
1105 argv
[j
] = editors
[i
].argv
[j
];
1108 if (editors
[i
].filter
)
1109 from
= &sel
->editor_fds
[1];
1110 colored
= editors
[i
].color
;
1115 if (vt_forkpty(sel
->editor
, ed
, argv
, NULL
, NULL
, to
, from
) < 0) {
1116 vt_destroy(sel
->editor
);
1121 sel
->term
= sel
->editor
;
1123 if (sel
->editor_fds
[0] != -1) {
1125 size_t len
= vt_content_get(sel
->app
, &buf
, colored
);
1128 ssize_t res
= write(sel
->editor_fds
[0], cur
, len
);
1130 if (errno
== EAGAIN
|| errno
== EINTR
)
1138 close(sel
->editor_fds
[0]);
1139 sel
->editor_fds
[0] = -1;
1143 vt_write(sel
->editor
, args
[0], strlen(args
[0]));
1147 focusn(const char *args
[]) {
1148 for (Client
*c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
)) {
1149 if (c
->order
== atoi(args
[0])) {
1152 toggleminimize(NULL
);
1159 focusid(const char *args
[]) {
1163 const int win_id
= atoi(args
[0]);
1164 for (Client
*c
= clients
; c
; c
= c
->next
) {
1165 if (c
->id
== win_id
) {
1168 toggleminimize(NULL
);
1169 if (!isvisible(c
)) {
1170 c
->tags
|= tagset
[seltags
];
1179 focusnext(const char *args
[]) {
1183 for (c
= sel
->next
; c
&& !isvisible(c
); c
= c
->next
);
1185 for (c
= clients
; c
&& !isvisible(c
); c
= c
->next
);
1191 focusnextnm(const char *args
[]) {
1196 c
= nextvisible(c
->next
);
1198 c
= nextvisible(clients
);
1199 } while (c
->minimized
&& c
!= sel
);
1204 focusprev(const char *args
[]) {
1208 for (c
= sel
->prev
; c
&& !isvisible(c
); c
= c
->prev
);
1210 for (c
= clients
; c
&& c
->next
; c
= c
->next
);
1211 for (; c
&& !isvisible(c
); c
= c
->prev
);
1218 focusprevnm(const char *args
[]) {
1223 for (c
= c
->prev
; c
&& !isvisible(c
); c
= c
->prev
);
1225 for (c
= clients
; c
&& c
->next
; c
= c
->next
);
1226 for (; c
&& !isvisible(c
); c
= c
->prev
);
1228 } while (c
&& c
!= sel
&& c
->minimized
);
1233 focuslast(const char *args
[]) {
1239 killclient(const char *args
[]) {
1242 debug("killing client with pid: %d\n", sel
->pid
);
1243 kill(-sel
->pid
, SIGKILL
);
1247 paste(const char *args
[]) {
1248 if (sel
&& copyreg
.data
)
1249 vt_write(sel
->term
, copyreg
.data
, copyreg
.len
);
1253 quit(const char *args
[]) {
1259 redraw(const char *args
[]) {
1260 for (Client
*c
= clients
; c
; c
= c
->next
) {
1261 if (!c
->minimized
) {
1264 wnoutrefresh(c
->window
);
1271 scrollback(const char *args
[]) {
1272 if (!is_content_visible(sel
))
1275 if (!args
[0] || atoi(args
[0]) < 0)
1276 vt_scroll(sel
->term
, -sel
->h
/2);
1278 vt_scroll(sel
->term
, sel
->h
/2);
1281 curs_set(vt_cursor_visible(sel
->term
));
1285 send(const char *args
[]) {
1286 if (sel
&& args
&& args
[0])
1287 vt_write(sel
->term
, args
[0], strlen(args
[0]));
1291 setlayout(const char *args
[]) {
1294 if (!args
|| !args
[0]) {
1295 if (++layout
== &layouts
[LENGTH(layouts
)])
1296 layout
= &layouts
[0];
1298 for (i
= 0; i
< LENGTH(layouts
); i
++)
1299 if (!strcmp(args
[0], layouts
[i
].symbol
))
1301 if (i
== LENGTH(layouts
))
1303 layout
= &layouts
[i
];
1309 incnmaster(const char *args
[]) {
1312 if (isarrange(fullscreen
) || isarrange(grid
))
1314 /* arg handling, manipulate nmaster */
1315 if (args
[0] == NULL
) {
1316 screen
.nmaster
= NMASTER
;
1317 } else if (sscanf(args
[0], "%d", &delta
) == 1) {
1318 if (args
[0][0] == '+' || args
[0][0] == '-')
1319 screen
.nmaster
+= delta
;
1321 screen
.nmaster
= delta
;
1322 if (screen
.nmaster
< 1)
1329 setmfact(const char *args
[]) {
1332 if (isarrange(fullscreen
) || isarrange(grid
))
1334 /* arg handling, manipulate mfact */
1335 if (args
[0] == NULL
) {
1336 screen
.mfact
= MFACT
;
1337 } else if (sscanf(args
[0], "%f", &delta
) == 1) {
1338 if (args
[0][0] == '+' || args
[0][0] == '-')
1339 screen
.mfact
+= delta
;
1341 screen
.mfact
= delta
;
1342 if (screen
.mfact
< 0.1)
1344 else if (screen
.mfact
> 0.9)
1351 startup(const char *args
[]) {
1352 for (unsigned int i
= 0; i
< LENGTH(actions
); i
++)
1353 actions
[i
].cmd(actions
[i
].args
);
1357 togglebar(const char *args
[]) {
1358 if (bar
.pos
== BAR_OFF
)
1362 bar
.autohide
= false;
1368 togglebarpos(const char *args
[]) {
1369 switch (bar
.pos
== BAR_OFF
? bar
.lastpos
: bar
.pos
) {
1371 bar
.pos
= BAR_BOTTOM
;
1382 toggleminimize(const char *args
[]) {
1387 /* the last window can't be minimized */
1388 if (!sel
->minimized
) {
1389 for (n
= 0, c
= nextvisible(clients
); c
; c
= nextvisible(c
->next
))
1395 sel
->minimized
= !sel
->minimized
;
1397 /* check whether the master client was minimized */
1398 if (sel
== nextvisible(clients
) && sel
->minimized
) {
1399 c
= nextvisible(sel
->next
);
1404 for (; c
&& (t
= nextvisible(c
->next
)) && !t
->minimized
; c
= t
);
1406 } else if (m
->minimized
) {
1407 /* non master window got minimized move it above all other
1411 for (c
= nextvisible(clients
); c
&& (t
= nextvisible(c
->next
)) && !t
->minimized
; c
= t
);
1413 } else { /* window is no longer minimized, move it to the master area */
1422 togglemouse(const char *args
[]) {
1423 mouse_events_enabled
= !mouse_events_enabled
;
1428 togglerunall(const char *args
[]) {
1429 runinall
= !runinall
;
1435 zoom(const char *args
[]) {
1440 if (args
&& args
[0])
1442 if ((c
= sel
) == nextvisible(clients
))
1443 if (!(c
= nextvisible(c
->next
)))
1449 toggleminimize(NULL
);
1453 /* commands for use by mouse bindings */
1455 mouse_focus(const char *args
[]) {
1457 if (msel
->minimized
)
1458 toggleminimize(NULL
);
1462 mouse_fullscreen(const char *args
[]) {
1464 if (isarrange(fullscreen
))
1471 mouse_minimize(const char *args
[]) {
1473 toggleminimize(NULL
);
1477 mouse_zoom(const char *args
[]) {
1483 get_cmd_by_name(const char *name
) {
1484 for (unsigned int i
= 0; i
< LENGTH(commands
); i
++) {
1485 if (!strcmp(name
, commands
[i
].name
))
1486 return &commands
[i
];
1492 handle_cmdfifo(void) {
1494 char *p
, *s
, cmdbuf
[512], c
;
1497 r
= read(cmdfifo
.fd
, cmdbuf
, sizeof cmdbuf
- 1);
1506 /* find the command name */
1507 for (; *p
== ' ' || *p
== '\n'; p
++);
1508 for (s
= p
; *p
&& *p
!= ' ' && *p
!= '\n'; p
++);
1511 if (*s
&& (cmd
= get_cmd_by_name(s
)) != NULL
) {
1514 const char *args
[MAX_ARGS
], *arg
;
1515 memset(args
, 0, sizeof(args
));
1516 /* if arguments were specified in config.h ignore the one given via
1517 * the named pipe and thus skip everything until we find a new line
1519 if (cmd
->action
.args
[0] || c
== '\n') {
1520 debug("execute %s", s
);
1521 cmd
->action
.cmd(cmd
->action
.args
);
1522 while (*p
&& *p
!= '\n')
1526 /* no arguments were given in config.h so we parse the command line */
1530 for (; (c
= *p
); p
++) {
1533 /* remove the escape character '\\' move every
1534 * following character to the left by one position
1554 /* remove trailing quote if there is one */
1555 if (*(p
- 1) == '\'' || *(p
- 1) == '\"')
1558 /* remove leading quote if there is one */
1559 if (*arg
== '\'' || *arg
== '\"')
1561 if (argc
< MAX_ARGS
)
1571 if (c
== '\n' || *p
== '\n') {
1574 debug("execute %s", s
);
1575 for(int i
= 0; i
< argc
; i
++)
1576 debug(" %s", args
[i
]);
1578 cmd
->action
.cmd(args
);
1587 handle_mouse(void) {
1591 if (getmouse(&event
) != OK
)
1593 msel
= get_client_by_coord(event
.x
, event
.y
);
1598 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
);
1600 vt_mouse(msel
->term
, event
.x
- msel
->x
, event
.y
- msel
->y
, event
.bstate
);
1602 for (i
= 0; i
< LENGTH(buttons
); i
++) {
1603 if (event
.bstate
& buttons
[i
].mask
)
1604 buttons
[i
].action
.cmd(buttons
[i
].action
.args
);
1608 #endif /* CONFIG_MOUSE */
1612 handle_statusbar(void) {
1615 switch (r
= read(bar
.fd
, bar
.text
, sizeof bar
.text
- 1)) {
1617 strncpy(bar
.text
, strerror(errno
), sizeof bar
.text
- 1);
1618 bar
.text
[sizeof bar
.text
- 1] = '\0';
1626 p
= bar
.text
+ r
- 1;
1627 for (; p
>= bar
.text
&& *p
== '\n'; *p
-- = '\0');
1628 for (; p
>= bar
.text
&& *p
!= '\n'; --p
);
1630 memmove(bar
.text
, p
+ 1, strlen(p
));
1636 handle_editor(Client
*c
) {
1637 if (!copyreg
.data
&& (copyreg
.data
= malloc(screen
.history
)))
1638 copyreg
.size
= screen
.history
;
1640 while (c
->editor_fds
[1] != -1 && copyreg
.len
< copyreg
.size
) {
1641 ssize_t len
= read(c
->editor_fds
[1], copyreg
.data
+ copyreg
.len
, copyreg
.size
- copyreg
.len
);
1650 if (copyreg
.len
== copyreg
.size
) {
1652 if (!(copyreg
.data
= realloc(copyreg
.data
, copyreg
.size
))) {
1658 c
->editor_died
= false;
1659 c
->editor_fds
[1] = -1;
1660 vt_destroy(c
->editor
);
1665 wnoutrefresh(c
->window
);
1669 open_or_create_fifo(const char *name
, const char **name_created
) {
1674 if ((fd
= open(name
, O_RDWR
|O_NONBLOCK
)) == -1) {
1675 if (errno
== ENOENT
&& !mkfifo(name
, S_IRUSR
|S_IWUSR
)) {
1676 *name_created
= name
;
1679 error("%s\n", strerror(errno
));
1683 if (fstat(fd
, &info
) == -1)
1684 error("%s\n", strerror(errno
));
1685 if (!S_ISFIFO(info
.st_mode
))
1686 error("%s is not a named pipe\n", name
);
1693 eprint("usage: dvtm [-v] [-M] [-m mod] [-d delay] [-h lines] [-t title] "
1694 "[-s status-fifo] [-c cmd-fifo] [cmd...]\n");
1699 parse_args(int argc
, char *argv
[]) {
1701 const char *name
= argv
[0];
1703 if (name
&& (name
= strrchr(name
, '/')))
1704 dvtm_name
= name
+ 1;
1705 if (!getenv("ESCDELAY"))
1707 for (int arg
= 1; arg
< argc
; arg
++) {
1708 if (argv
[arg
][0] != '-') {
1709 const char *args
[] = { argv
[arg
], NULL
, NULL
};
1717 if (argv
[arg
][1] != 'v' && argv
[arg
][1] != 'M' && (arg
+ 1) >= argc
)
1719 switch (argv
[arg
][1]) {
1721 puts("dvtm-"VERSION
" © 2007-2016 Marc André Tanner");
1724 mouse_events_enabled
= !mouse_events_enabled
;
1727 char *mod
= argv
[++arg
];
1728 if (mod
[0] == '^' && mod
[1])
1729 *mod
= CTRL(mod
[1]);
1730 for (unsigned int b
= 0; b
< LENGTH(bindings
); b
++)
1731 if (bindings
[b
].keys
[0] == MOD
)
1732 bindings
[b
].keys
[0] = *mod
;
1736 set_escdelay(atoi(argv
[++arg
]));
1739 else if (ESCDELAY
> 1000)
1743 screen
.history
= atoi(argv
[++arg
]);
1746 title
= argv
[++arg
];
1749 bar
.fd
= open_or_create_fifo(argv
[++arg
], &bar
.file
);
1754 cmdfifo
.fd
= open_or_create_fifo(argv
[++arg
], &cmdfifo
.file
);
1755 if (!(fifo
= realpath(argv
[arg
], NULL
)))
1756 error("%s\n", strerror(errno
));
1757 setenv("DVTM_CMD_FIFO", fifo
, 1);
1768 main(int argc
, char *argv
[]) {
1770 unsigned int key_index
= 0;
1771 memset(keys
, 0, sizeof(keys
));
1772 sigset_t emptyset
, blockset
;
1774 setenv("DVTM", VERSION
, 1);
1775 if (!parse_args(argc
, argv
)) {
1780 sigemptyset(&emptyset
);
1781 sigemptyset(&blockset
);
1782 sigaddset(&blockset
, SIGWINCH
);
1783 sigaddset(&blockset
, SIGCHLD
);
1784 sigprocmask(SIG_BLOCK
, &blockset
, NULL
);
1790 if (screen
.need_resize
) {
1792 screen
.need_resize
= false;
1796 FD_SET(STDIN_FILENO
, &rd
);
1798 if (cmdfifo
.fd
!= -1) {
1799 FD_SET(cmdfifo
.fd
, &rd
);
1804 FD_SET(bar
.fd
, &rd
);
1805 nfds
= MAX(nfds
, bar
.fd
);
1808 for (Client
*c
= clients
; c
; ) {
1809 if (c
->editor
&& c
->editor_died
)
1811 if (!c
->editor
&& c
->died
) {
1812 Client
*t
= c
->next
;
1817 int pty
= c
->editor
? vt_pty_get(c
->editor
) : vt_pty_get(c
->app
);
1819 nfds
= MAX(nfds
, pty
);
1824 r
= pselect(nfds
+ 1, &rd
, NULL
, NULL
, NULL
, &emptyset
);
1833 if (FD_ISSET(STDIN_FILENO
, &rd
)) {
1836 keys
[key_index
++] = code
;
1837 KeyBinding
*binding
= NULL
;
1838 if (code
== KEY_MOUSE
) {
1841 } else if ((binding
= keybinding(keys
, key_index
))) {
1842 unsigned int key_length
= MAX_KEYS
;
1843 while (key_length
> 1 && !binding
->keys
[key_length
-1])
1845 if (key_index
== key_length
) {
1846 binding
->action
.cmd(binding
->action
.args
);
1848 memset(keys
, 0, sizeof(keys
));
1852 memset(keys
, 0, sizeof(keys
));
1856 if (r
== 1) /* no data available on pty's */
1860 if (cmdfifo
.fd
!= -1 && FD_ISSET(cmdfifo
.fd
, &rd
))
1863 if (bar
.fd
!= -1 && FD_ISSET(bar
.fd
, &rd
))
1866 for (Client
*c
= clients
; c
; c
= c
->next
) {
1867 if (FD_ISSET(vt_pty_get(c
->term
), &rd
)) {
1868 if (vt_process(c
->term
) < 0 && errno
== EIO
) {
1870 c
->editor_died
= true;
1877 if (c
!= sel
&& is_content_visible(c
)) {
1879 wnoutrefresh(c
->window
);
1883 if (is_content_visible(sel
)) {
1885 curs_set(vt_cursor_visible(sel
->term
));
1886 wnoutrefresh(sel
->window
);