1#include <time.h>
2#include <string.h>
3
4#include <X11/Xlib.h>
5#include <X11/Xft/Xft.h>
6
7#include "glitch.h"
8#include "config.h"
9
10extern WindowManager wm;
11
12void widget_desktop_indicator(void) {
13 int screen_width = DisplayWidth(wm.dpy, wm.screen);
14 int padding = 3;
15
16 char buf[8];
17 snprintf(buf, sizeof(buf), "%u", wm.current_desktop);
18
19 XGlyphInfo extents;
20 XftTextExtentsUtf8(wm.dpy, wm.font, (FcChar8 *)buf, strlen(buf), &extents);
21
22 int size = (wm.font->height > extents.width ? wm.font->height : extents.width) + padding * 2;
23 int x = screen_width - size - 10;
24 int y = 10;
25
26 // Draw the background square.
27 XftDrawRect(wm.xft_draw, &wm.xft_bg_color, x, y, size, size);
28
29 // Center the text in the square.
30 int text_x = x + (size - extents.width) / 2 + extents.x;
31 int text_y = y + (size - wm.font->ascent - wm.font->descent) / 2 + wm.font->ascent;
32
33 XftDrawStringUtf8(wm.xft_draw, &wm.xft_color, wm.font, text_x, text_y, (FcChar8 *)buf, strlen(buf));
34}
35
36void widget_mic_indicator(void) {
37 int screen_width = DisplayWidth(wm.dpy, wm.screen);
38 int padding = 3;
39
40 // Desktop indicator size
41 char desktop_buf[8];
42 snprintf(desktop_buf, sizeof(desktop_buf), "%u", wm.current_desktop);
43 XGlyphInfo desktop_extents;
44 XftTextExtentsUtf8(wm.dpy, wm.font, (FcChar8 *)desktop_buf, strlen(desktop_buf), &desktop_extents);
45 int desktop_size = (wm.font->height > desktop_extents.width ? wm.font->height : desktop_extents.width) + padding * 2;
46
47 // Layout indicator size
48 LayoutMode mode = wm.layout_modes[wm.current_desktop];
49 const char *layout_buf = (mode == LAYOUT_TILING) ? "T" : "F";
50 XGlyphInfo layout_extents;
51 XftTextExtentsUtf8(wm.dpy, wm.font, (FcChar8 *)layout_buf, strlen(layout_buf), &layout_extents);
52 int layout_size_w = (wm.font->height > layout_extents.width ? wm.font->height : layout_extents.width) + padding * 2;
53
54 const char *buf = "MIC";
55 XGlyphInfo extents;
56 XftTextExtentsUtf8(wm.dpy, wm.font, (FcChar8 *)buf, strlen(buf), &extents);
57
58 int size_w = extents.width + padding * 4;
59 int size_h = desktop_size;
60 int x = screen_width - desktop_size - layout_size_w - size_w - 20;
61 int y = 10;
62
63 XftColor *bg = wm.mic_muted ? &wm.xft_mic_muted_bg : &wm.xft_mic_active_bg;
64 XftColor *fg = wm.mic_muted ? &wm.xft_mic_muted_fg : &wm.xft_mic_active_fg;
65
66 // Draw the background.
67 XftDrawRect(wm.xft_draw, bg, x, y, size_w, size_h);
68
69 // Center the text.
70 int text_x = x + (size_w - extents.width) / 2 + extents.x;
71 int text_y = y + (size_h - wm.font->ascent - wm.font->descent) / 2 + wm.font->ascent;
72
73 XftDrawStringUtf8(wm.xft_draw, fg, wm.font, text_x, text_y, (FcChar8 *)buf, strlen(buf));
74}
75
76void widget_layout_indicator(void) {
77 int screen_width = DisplayWidth(wm.dpy, wm.screen);
78 int padding = 3;
79
80 // Desktop indicator size
81 char desktop_buf[8];
82 snprintf(desktop_buf, sizeof(desktop_buf), "%u", wm.current_desktop);
83 XGlyphInfo desktop_extents;
84 XftTextExtentsUtf8(wm.dpy, wm.font, (FcChar8 *)desktop_buf, strlen(desktop_buf), &desktop_extents);
85 int desktop_size = (wm.font->height > desktop_extents.width ? wm.font->height : desktop_extents.width) + padding * 2;
86
87 LayoutMode mode = wm.layout_modes[wm.current_desktop];
88 const char *buf = (mode == LAYOUT_TILING) ? "T" : "F";
89 XGlyphInfo extents;
90 XftTextExtentsUtf8(wm.dpy, wm.font, (FcChar8 *)buf, strlen(buf), &extents);
91
92 int size_w = (wm.font->height > extents.width ? wm.font->height : extents.width) + padding * 2;
93 int size_h = desktop_size;
94 int x = screen_width - desktop_size - size_w - 15;
95 int y = 10;
96
97 XftColor *bg = (mode == LAYOUT_TILING) ? &wm.xft_layout_tile_bg : &wm.xft_layout_float_bg;
98 XftColor *fg = (mode == LAYOUT_TILING) ? &wm.xft_layout_tile_fg : &wm.xft_layout_float_fg;
99
100 // Draw the background.
101 XftDrawRect(wm.xft_draw, bg, x, y, size_w, size_h);
102
103 // Center the text.
104 int text_x = x + (size_w - extents.width) / 2 + extents.x;
105 int text_y = y + (size_h - wm.font->ascent - wm.font->descent) / 2 + wm.font->ascent;
106
107 XftDrawStringUtf8(wm.xft_draw, fg, wm.font, text_x, text_y, (FcChar8 *)buf, strlen(buf));
108}
109
110void widget_datetime(void) {
111 int screen_width = DisplayWidth(wm.dpy, wm.screen);
112 int padding = 3;
113
114 // Desktop indicator size
115 char desktop_buf[8];
116 snprintf(desktop_buf, sizeof(desktop_buf), "%u", wm.current_desktop);
117 XGlyphInfo desktop_extents;
118 XftTextExtentsUtf8(wm.dpy, wm.font, (FcChar8 *)desktop_buf, strlen(desktop_buf), &desktop_extents);
119 int desktop_size = (wm.font->height > desktop_extents.width ? wm.font->height : desktop_extents.width) + padding * 2;
120
121 // Layout indicator size
122 LayoutMode mode = wm.layout_modes[wm.current_desktop];
123 const char *layout_buf = (mode == LAYOUT_TILING) ? "T" : "F";
124 XGlyphInfo layout_extents;
125 XftTextExtentsUtf8(wm.dpy, wm.font, (FcChar8 *)layout_buf, strlen(layout_buf), &layout_extents);
126 int layout_size_w = (wm.font->height > layout_extents.width ? wm.font->height : layout_extents.width) + padding * 2;
127
128 // Mic indicator size
129 const char *mic_buf = "MIC";
130 XGlyphInfo mic_extents;
131 XftTextExtentsUtf8(wm.dpy, wm.font, (FcChar8 *)mic_buf, strlen(mic_buf), &mic_extents);
132 int mic_size_w = mic_extents.width + padding * 4;
133
134 int offset_x = desktop_size + layout_size_w + mic_size_w + 35;
135
136 char time_buf[64];
137 time_t now = time(NULL);
138 struct tm *tm_info = localtime(&now);
139 strftime(time_buf, sizeof(time_buf), time_format, tm_info);
140
141 XGlyphInfo time_extents;
142 XftTextExtentsUtf8(wm.dpy, wm.font, (FcChar8 *)time_buf, strlen(time_buf), &time_extents);
143
144 int time_x = screen_width - offset_x - time_extents.xOff;
145 int y = 10;
146 int win_height = desktop_size;
147
148 // Draw the background.
149 XftDrawRect(wm.xft_draw, &wm.xft_root_bg_color, time_x - 50, y, time_extents.xOff + 50, win_height);
150
151 // Draw the time.
152 int time_text_y = y + (win_height - wm.font->ascent - wm.font->descent) / 2 + wm.font->ascent;
153 XftDrawStringUtf8(wm.xft_draw, &wm.xft_color, wm.font, time_x, time_text_y, (FcChar8 *)time_buf, strlen(time_buf));
154}
155
156void redraw_widgets(void) {
157 widget_desktop_indicator();
158 widget_layout_indicator();
159 widget_mic_indicator();
160 widget_datetime();
161}