tmux  3.2a
About: tmux is a terminal multiplexer that lets you switch easily between several programs in one terminal.
  Fossies Dox: tmux-3.2a.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

window-clock.c
Go to the documentation of this file.
1 /* $OpenBSD$ */
2 
3 /*
4  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24 
25 #include "tmux.h"
26 
27 static struct screen *window_clock_init(struct window_mode_entry *,
28  struct cmd_find_state *, struct args *);
29 static void window_clock_free(struct window_mode_entry *);
30 static void window_clock_resize(struct window_mode_entry *, u_int, u_int);
31 static void window_clock_key(struct window_mode_entry *, struct client *,
32  struct session *, struct winlink *, key_code,
33  struct mouse_event *);
34 
35 static void window_clock_timer_callback(int, short, void *);
37 
38 const struct window_mode window_clock_mode = {
39  .name = "clock-mode",
40 
41  .init = window_clock_init,
42  .free = window_clock_free,
43  .resize = window_clock_resize,
44  .key = window_clock_key,
45 };
46 
48  struct screen screen;
49  time_t tim;
50  struct event timer;
51 };
52 
53 const char window_clock_table[14][5][5] = {
54  { { 1,1,1,1,1 }, /* 0 */
55  { 1,0,0,0,1 },
56  { 1,0,0,0,1 },
57  { 1,0,0,0,1 },
58  { 1,1,1,1,1 } },
59  { { 0,0,0,0,1 }, /* 1 */
60  { 0,0,0,0,1 },
61  { 0,0,0,0,1 },
62  { 0,0,0,0,1 },
63  { 0,0,0,0,1 } },
64  { { 1,1,1,1,1 }, /* 2 */
65  { 0,0,0,0,1 },
66  { 1,1,1,1,1 },
67  { 1,0,0,0,0 },
68  { 1,1,1,1,1 } },
69  { { 1,1,1,1,1 }, /* 3 */
70  { 0,0,0,0,1 },
71  { 1,1,1,1,1 },
72  { 0,0,0,0,1 },
73  { 1,1,1,1,1 } },
74  { { 1,0,0,0,1 }, /* 4 */
75  { 1,0,0,0,1 },
76  { 1,1,1,1,1 },
77  { 0,0,0,0,1 },
78  { 0,0,0,0,1 } },
79  { { 1,1,1,1,1 }, /* 5 */
80  { 1,0,0,0,0 },
81  { 1,1,1,1,1 },
82  { 0,0,0,0,1 },
83  { 1,1,1,1,1 } },
84  { { 1,1,1,1,1 }, /* 6 */
85  { 1,0,0,0,0 },
86  { 1,1,1,1,1 },
87  { 1,0,0,0,1 },
88  { 1,1,1,1,1 } },
89  { { 1,1,1,1,1 }, /* 7 */
90  { 0,0,0,0,1 },
91  { 0,0,0,0,1 },
92  { 0,0,0,0,1 },
93  { 0,0,0,0,1 } },
94  { { 1,1,1,1,1 }, /* 8 */
95  { 1,0,0,0,1 },
96  { 1,1,1,1,1 },
97  { 1,0,0,0,1 },
98  { 1,1,1,1,1 } },
99  { { 1,1,1,1,1 }, /* 9 */
100  { 1,0,0,0,1 },
101  { 1,1,1,1,1 },
102  { 0,0,0,0,1 },
103  { 1,1,1,1,1 } },
104  { { 0,0,0,0,0 }, /* : */
105  { 0,0,1,0,0 },
106  { 0,0,0,0,0 },
107  { 0,0,1,0,0 },
108  { 0,0,0,0,0 } },
109  { { 1,1,1,1,1 }, /* A */
110  { 1,0,0,0,1 },
111  { 1,1,1,1,1 },
112  { 1,0,0,0,1 },
113  { 1,0,0,0,1 } },
114  { { 1,1,1,1,1 }, /* P */
115  { 1,0,0,0,1 },
116  { 1,1,1,1,1 },
117  { 1,0,0,0,0 },
118  { 1,0,0,0,0 } },
119  { { 1,0,0,0,1 }, /* M */
120  { 1,1,0,1,1 },
121  { 1,0,1,0,1 },
122  { 1,0,0,0,1 },
123  { 1,0,0,0,1 } },
124 };
125 
126 static void
127 window_clock_timer_callback(__unused int fd, __unused short events, void *arg)
128 {
129  struct window_mode_entry *wme = arg;
130  struct window_pane *wp = wme->wp;
131  struct window_clock_mode_data *data = wme->data;
132  struct tm now, then;
133  time_t t;
134  struct timeval tv = { .tv_sec = 1 };
135 
136  evtimer_del(&data->timer);
137  evtimer_add(&data->timer, &tv);
138 
139  if (TAILQ_FIRST(&wp->modes) != wme)
140  return;
141 
142  t = time(NULL);
143  gmtime_r(&t, &now);
144  gmtime_r(&data->tim, &then);
145  if (now.tm_min == then.tm_min)
146  return;
147  data->tim = t;
148 
150  wp->flags |= PANE_REDRAW;
151 }
152 
153 static struct screen *
155  __unused struct cmd_find_state *fs, __unused struct args *args)
156 {
157  struct window_pane *wp = wme->wp;
158  struct window_clock_mode_data *data;
159  struct screen *s;
160  struct timeval tv = { .tv_sec = 1 };
161 
162  wme->data = data = xmalloc(sizeof *data);
163  data->tim = time(NULL);
164 
165  evtimer_set(&data->timer, window_clock_timer_callback, wme);
166  evtimer_add(&data->timer, &tv);
167 
168  s = &data->screen;
169  screen_init(s, screen_size_x(&wp->base), screen_size_y(&wp->base), 0);
170  s->mode &= ~~MODE_CURSOR;
171 
173 
174  return (s);
175 }
176 
177 static void
179 {
180  struct window_clock_mode_data *data = wme->data;
181 
182  evtimer_del(&data->timer);
183  screen_free(&data->screen);
184  free(data);
185 }
186 
187 static void
188 window_clock_resize(struct window_mode_entry *wme, u_int sx, u_int sy)
189 {
190  struct window_clock_mode_data *data = wme->data;
191  struct screen *s = &data->screen;
192 
193  screen_resize(s, sx, sy, 0);
195 }
196 
197 static void
199  __unused struct session *s, __unused struct winlink *wl,
201 {
203 }
204 
205 static void
207 {
208  struct window_pane *wp = wme->wp;
209  struct window_clock_mode_data *data = wme->data;
210  struct screen_write_ctx ctx;
211  int colour, style;
212  struct screen *s = &data->screen;
213  struct grid_cell gc;
214  char tim[64], *ptr;
215  time_t t;
216  struct tm *tm;
217  u_int i, j, x, y, idx;
218 
219  colour = options_get_number(wp->window->options, "clock-mode-colour");
220  style = options_get_number(wp->window->options, "clock-mode-style");
221 
222  screen_write_start(&ctx, s);
223 
224  t = time(NULL);
225  tm = localtime(&t);
226  if (style == 0) {
227  strftime(tim, sizeof tim, "%l:%M ", localtime(&t));
228  if (tm->tm_hour >= 12)
229  strlcat(tim, "PM", sizeof tim);
230  else
231  strlcat(tim, "AM", sizeof tim);
232  } else
233  strftime(tim, sizeof tim, "%H:%M", tm);
234 
235  screen_write_clearscreen(&ctx, 8);
236 
237  if (screen_size_x(s) < 6 * strlen(tim) || screen_size_y(s) < 6) {
238  if (screen_size_x(s) >= strlen(tim) && screen_size_y(s) != 0) {
239  x = (screen_size_x(s) / 2) - (strlen(tim) / 2);
240  y = screen_size_y(s) / 2;
241  screen_write_cursormove(&ctx, x, y, 0);
242 
243  memcpy(&gc, &grid_default_cell, sizeof gc);
245  gc.fg = colour;
246  screen_write_puts(&ctx, &gc, "%s", tim);
247  }
248 
249  screen_write_stop(&ctx);
250  return;
251  }
252 
253  x = (screen_size_x(s) / 2) - 3 * strlen(tim);
254  y = (screen_size_y(s) / 2) - 3;
255 
256  memcpy(&gc, &grid_default_cell, sizeof gc);
258  gc.bg = colour;
259  for (ptr = tim; *ptr != '\0'; ptr++) {
260  if (*ptr >= '0' && *ptr <= '9')
261  idx = *ptr - '0';
262  else if (*ptr == ':')
263  idx = 10;
264  else if (*ptr == 'A')
265  idx = 11;
266  else if (*ptr == 'P')
267  idx = 12;
268  else if (*ptr == 'M')
269  idx = 13;
270  else {
271  x += 6;
272  continue;
273  }
274 
275  for (j = 0; j < 5; j++) {
276  for (i = 0; i < 5; i++) {
277  screen_write_cursormove(&ctx, x + i, y + j, 0);
278  if (window_clock_table[idx][j][i])
279  screen_write_putc(&ctx, &gc, ' ');
280  }
281  }
282  x += 6;
283  }
284 
285  screen_write_stop(&ctx);
286 }
#define __unused
Definition: compat.h:60
size_t strlcat(char *, const char *, size_t)
const struct grid_cell grid_default_cell
Definition: grid.c:39
key_code key
Definition: key-string.c:32
long long options_get_number(struct options *oo, const char *name)
Definition: options.c:699
void screen_write_start(struct screen_write_ctx *ctx, struct screen *s)
Definition: screen-write.c:284
void screen_write_putc(struct screen_write_ctx *ctx, const struct grid_cell *gcp, u_char ch)
Definition: screen-write.c:321
void screen_write_puts(struct screen_write_ctx *ctx, const struct grid_cell *gcp, const char *fmt,...)
Definition: screen-write.c:464
void screen_write_cursormove(struct screen_write_ctx *ctx, int px, int py, int origin)
void screen_write_stop(struct screen_write_ctx *ctx)
Definition: screen-write.c:296
void screen_write_clearscreen(struct screen_write_ctx *ctx, u_int bg)
void screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
Definition: screen.c:276
void screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
Definition: screen.c:74
void screen_free(struct screen *s)
Definition: screen.c:122
Definition: tmux.h:1435
Definition: tmux.h:1608
u_char flags
Definition: tmux.h:691
int bg
Definition: tmux.h:693
int fg
Definition: tmux.h:692
Definition: tmux.h:816
int mode
Definition: tmux.h:832
Definition: tmux.h:1179
Definition: tmux.h:799
struct screen screen
Definition: window-clock.c:48
Definition: tmux.h:927
void * data
Definition: tmux.h:932
struct window_pane * wp
Definition: tmux.h:928
const char * name
Definition: tmux.h:906
int flags
Definition: tmux.h:977
struct screen base
Definition: tmux.h:1021
struct window * window
Definition: tmux.h:962
struct options * options
Definition: tmux.h:1085
#define GRID_FLAG_NOPALETTE
Definition: tmux.h:679
unsigned long long key_code
Definition: tmux.h:177
#define screen_size_y(s)
Definition: tmux.h:882
#define PANE_REDRAW
Definition: tmux.h:978
#define screen_size_x(s)
Definition: tmux.h:881
void window_pane_reset_mode(struct window_pane *)
Definition: window.c:1105
const char window_clock_table[14][5][5]
Definition: window-clock.c:53
static struct screen * window_clock_init(struct window_mode_entry *, struct cmd_find_state *, struct args *)
Definition: window-clock.c:154
static void window_clock_key(struct window_mode_entry *, struct client *, struct session *, struct winlink *, key_code, struct mouse_event *)
Definition: window-clock.c:198
static void window_clock_resize(struct window_mode_entry *, u_int, u_int)
Definition: window-clock.c:188
const struct window_mode window_clock_mode
Definition: window-clock.c:38
static void window_clock_timer_callback(int, short, void *)
Definition: window-clock.c:127
static void window_clock_free(struct window_mode_entry *)
Definition: window-clock.c:178
static void window_clock_draw_screen(struct window_mode_entry *)
Definition: window-clock.c:206
void * xmalloc(size_t size)
Definition: xmalloc.c:27