alot of renaming...
[k8sterm.git] / src / termswitch.c
blobd1c3710b1a6f0837c7e29282df157eece87d15e0
1 ////////////////////////////////////////////////////////////////////////////////
2 static void fixWindowTitle (const K8Term *t) {
3 if (t != NULL) {
4 const char *title = (t != NULL) ? t->title : NULL;
5 //
6 if (title == NULL || !title[0]) {
7 title = opt_title;
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;
23 idx = f;
26 if (idx < 0) {
27 if (termidx == 0) idx = 0; else idx = termidx+1;
28 if (idx > term_count) idx = term_count-1;
30 return idx;
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;
38 updateTabBar = 1;
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;
47 termidx = idx;
48 curterm = term_array[termidx];
49 curterm->curbhidden = 0;
50 curterm->lastBlinkTime = tt;
52 fixFirstTab();
54 xseturgency(0);
55 k8t_tmFullDirty(curterm);
56 fixWindowTitle(curterm);
57 updateTabBar = 1;
58 XSetWindowBackground(xw.dpy, xw.win, getColor(curterm->defbg));
59 xclearunused();
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);
67 xfixsel();
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);
80 return 1;
84 static K8Term *k8t_termalloc (void) {
85 K8Term *t;
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) die("out of memory!");
92 term_array = n;
93 term_array_size = newsz;
95 if ((t = calloc(1, sizeof(K8Term))) == NULL) return NULL;
96 t->wrbufsize = WBUFSIZ;
97 t->deffg = defaultFG;
98 t->defbg = defaultBG;
99 t->dead = 1;
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;
107 return 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];
116 if (t->pid != 0) {
117 kill(t->pid, SIGKILL);
118 return;
121 if (t->cmdfd >= 0) {
122 close(t->cmdfd);
123 t->cmdfd = -1;
126 exitcode = t->exitcode;
128 if (t->waitkeypress) return;
130 if (idx == termidx) {
131 if (term_count > 1) {
132 t->dead = 1;
133 //switchToTerm((idx > 0) ? idx-1 : 1, 0);
134 switchToTerm(findTermToSwitch(), 0);
135 return;
137 curterm = NULL;
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]);
142 free(t->dirty);
143 free(t->alt);
144 free(t->line);
145 if (t->execcmd != NULL) free(t->execcmd);
146 // condense array
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];
149 --term_count;
150 XFreePixmap(xw.dpy, t->picbuf);
151 free(t);
156 static void termcleanup (void) {
157 int f = 0, needredraw = 0;
159 while (f < term_count) {
160 if (term_array[f]->dead) {
161 termfree(f);
162 needredraw = 1;
163 } else {
164 ++f;
168 if (needredraw) {
169 updateTabBar = 1;
170 k8t_Draw(curterm, 1);