1 ////////////////////////////////////////////////////////////////////////////////
2 static void fixWindowTitle (const K8Term
*t
) {
4 const char *title
= (t
!= NULL
) ? t
->title
: NULL
;
6 if (title
== NULL
|| !title
[0]) {
8 if (title
== NULL
) title
= "";
10 XStoreName(xw
.dpy
, xw
.win
, title
);
11 XChangeProperty(xw
.dpy
, xw
.win
, XA_NETWM_NAME
, XA_UTF8
, 8, PropModeReplace
, (const unsigned char *)title
, strlen(title
));
16 // find latest active terminal (but not current %-)
17 static int findTermToSwitch (void) {
18 int maxlat
= -1, idx
= -1;
20 for (int f
= 0; f
< term_count
; ++f
) {
21 if (curterm
!= term_array
[f
] && term_array
[f
]->lastActiveTime
> maxlat
) {
22 maxlat
= term_array
[f
]->lastActiveTime
;
27 if (termidx
== 0) idx
= 0; else idx
= termidx
+1;
28 if (idx
> term_count
) idx
= term_count
-1;
34 static void fixFirstTab (void) {
35 if (termidx
< firstVisibleTab
) firstVisibleTab
= termidx
;
36 else if (termidx
> firstVisibleTab
+opt_tabcount
-1) firstVisibleTab
= termidx
-opt_tabcount
+1;
37 if (firstVisibleTab
< 0) firstVisibleTab
= 0;
42 static void switchToTerm (int idx
, int redraw
) {
43 if (idx
>= 0 && idx
< term_count
&& term_array
[idx
] != NULL
&& term_array
[idx
] != curterm
) {
44 MSTime tt
= mclock_ticks();
46 if (curterm
!= NULL
) curterm
->lastActiveTime
= tt
;
48 curterm
= term_array
[termidx
];
49 curterm
->curbhidden
= 0;
50 curterm
->lastBlinkTime
= tt
;
55 k8t_tmFullDirty(curterm
);
56 fixWindowTitle(curterm
);
58 XSetWindowBackground(xw
.dpy
, xw
.win
, getColor(curterm
->defbg
));
60 if (redraw
) k8t_Draw(curterm
, 1);
61 //FIXME: optimize memory allocations
62 if (curterm
->sel
.clip
!= NULL
&& curterm
->sel
.bx
>= 0) {
63 if (lastSelStr
!= NULL
) free(lastSelStr
);
64 lastSelStr
= strdup(curterm
->sel
.clip
);
65 //fprintf(stderr, "newsel: [%s]\n", lastSelStr);
68 //fprintf(stderr, "curterm #%d\n", termidx);
69 //fprintf(stderr, "needConv: %d\n", curterm->needConv);
74 static int termallocPixmap (K8Term
*term
) {
75 term
->picbufh
= term
->row
*xw
.ch
;
76 term
->picbufw
= term
->col
*xw
.cw
;
77 term
->picbuf
= XCreatePixmap(xw
.dpy
, xw
.win
, term
->picbufw
, term
->picbufh
, XDefaultDepth(xw
.dpy
, xw
.scr
));
78 XFillRectangle(xw
.dpy
, term
->picbuf
, dc
.gc
, 0, 0, term
->picbufw
, term
->picbufh
);
84 static K8Term
*k8t_termalloc (void) {
87 if (term_count
>= term_array_size
) {
88 int newsz
= ((term_count
==0) ? 1 : term_array_size
+64);
89 K8Term
**n
= realloc(term_array
, sizeof(K8Term
*)*newsz
);
91 if (n
== NULL
&& term_count
== 0) k8t_die("out of memory!");
93 term_array_size
= newsz
;
95 if ((t
= calloc(1, sizeof(K8Term
))) == NULL
) return NULL
;
96 t
->wrbufsize
= WBUFSIZ
;
100 t
->needConv
= (needConversion
? 1 : 0);
101 t
->belltype
= (opt_audiblebell
?K8T_BELL_AUDIO
:0)|(opt_urgentbell
?K8T_BELL_URGENT
:0);
102 t
->curblink
= opt_cursorBlink
;
103 t
->curblinkinactive
= opt_cursorBlinkInactive
;
104 t
->fastredraw
= opt_fastredraw
;
105 term_array
[term_count
++] = t
;
111 // newer delete last terminal!
112 static void termfree (int idx
) {
113 if (idx
>= 0 && idx
< term_count
&& term_array
[idx
] != NULL
) {
114 K8Term
*t
= term_array
[idx
];
117 kill(t
->pid
, SIGKILL
);
126 exitcode
= t
->exitcode
;
128 if (t
->waitkeypress
) return;
130 if (idx
== termidx
) {
131 if (term_count
> 1) {
133 //switchToTerm((idx > 0) ? idx-1 : 1, 0);
134 switchToTerm(findTermToSwitch(), 0);
140 for (int y
= 0; y
< t
->row
; ++y
) free(t
->alt
[y
]);
141 for (int y
= 0; y
< t
->linecount
; ++y
) free(t
->line
[y
]);
145 if (t
->execcmd
!= NULL
) free(t
->execcmd
);
147 if (termidx
> idx
) --termidx
; // it's not current, and current is at the right
148 for (int f
= idx
+1; f
< term_count
; ++f
) term_array
[f
-1] = term_array
[f
];
150 XFreePixmap(xw
.dpy
, t
->picbuf
);
156 static void termcleanup (void) {
157 int f
= 0, needredraw
= 0;
159 while (f
< term_count
) {
160 if (term_array
[f
]->dead
) {
170 k8t_Draw(curterm
, 1);