2 #include <X11/Xft/Xft.h>
10 #include <semaphore.h>
18 int exit_code
= EXIT_SUCCESS
;
20 static void die(const char *format
, ...)
24 vfprintf(stderr
, format
, ap
);
25 fprintf(stderr
, "\n");
30 int get_max_len(char *body
, XftFont
*font
, int max_text_width
)
32 int eol
= strlen(body
);
34 XftTextExtentsUtf8(display
, font
, (FcChar8
*)body
, eol
, &info
);
36 if (info
.width
> max_text_width
)
39 eol
= max_text_width
/ font
->max_advance_width
;
42 while (info
.width
< max_text_width
)
45 XftTextExtentsUtf8(display
, font
, (FcChar8
*)body
, eol
, &info
);
51 for (int i
= 0; i
< eol
; i
++)
58 if (info
.width
< max_text_width
)
63 while (body
[eol
] != ' ' && eol
)
75 event
.type
= ButtonPress
;
76 XSendEvent(display
, window
, 0, 0, &event
);
82 exit_code
= EXIT_ACTION
;
86 int main(int argc
, char *argv
[])
89 die("Usage: %s body", argv
[0]);
91 signal(SIGALRM
, expire
);
92 signal(SIGTERM
, expire
);
93 signal(SIGINT
, expire
);
94 signal(SIGUSR1
, SIG_IGN
);
95 signal(SIGUSR2
, SIG_IGN
);
97 display
= XOpenDisplay(0);
100 die("Cannot open display");
102 int screen
= DefaultScreen(display
);
103 Visual
*visual
= DefaultVisual(display
, screen
);
104 Colormap colormap
= DefaultColormap(display
, screen
);
106 int screen_width
= DisplayWidth(display
, screen
);
107 int screen_height
= DisplayHeight(display
, screen
);
111 XSetWindowAttributes attributes
;
112 attributes
.override_redirect
= True
;
113 XftColorAllocName(display
, visual
, colormap
, background_color
, &color
);
114 attributes
.background_pixel
= color
.pixel
;
115 XftColorAllocName(display
, visual
, colormap
, border_color
, &color
);
116 attributes
.border_pixel
= color
.pixel
;
118 XftFont
*font
= XftFontOpenName(display
, screen
, font_pattern
);
120 int num_of_lines
= 0;
121 int max_text_width
= width
- 2 * padding
;
123 char **words
= malloc(words_size
* sizeof(char *));
125 die("malloc failed");
127 for (int i
= 1; i
< argc
; i
++)
129 char *body
= argv
[i
];
131 for (unsigned int eol
= get_max_len(body
, font
, max_text_width
); eol
<= strlen(body
) && eol
; body
+= eol
, num_of_lines
++, eol
= get_max_len(body
, font
, max_text_width
))
133 if (words_size
<= num_of_lines
)
135 words
= realloc(words
, (words_size
+= 5) * sizeof(char *));
137 die("malloc failed");
139 words
[num_of_lines
] = malloc((eol
+ 1) * sizeof(char));
140 if (!words
[num_of_lines
])
141 die("malloc failed");
142 strncpy(words
[num_of_lines
], body
, eol
);
143 words
[num_of_lines
][eol
] = '\0';
147 unsigned int x
= pos_x
;
148 unsigned int y
= pos_y
;
149 unsigned int text_height
= font
->ascent
- font
->descent
;
150 unsigned int height
= (num_of_lines
- 1) * line_spacing
+ num_of_lines
* text_height
+ 2 * padding
;
152 if (corner
== TOP_RIGHT
|| corner
== BOTTOM_RIGHT
)
153 x
= screen_width
- width
- border_size
* 2 - pos_x
;
155 if (corner
== BOTTOM_LEFT
|| corner
== BOTTOM_RIGHT
)
156 y
= screen_height
- height
- border_size
* 2 - pos_y
;
158 window
= XCreateWindow(display
, RootWindow(display
, screen
), x
, y
, width
, height
, border_size
, DefaultDepth(display
, screen
), CopyFromParent
, visual
,
159 CWOverrideRedirect
| CWBackPixel
| CWBorderPixel
, &attributes
);
161 XftDraw
*draw
= XftDrawCreate(display
, window
, visual
, colormap
);
162 XftColorAllocName(display
, visual
, colormap
, font_color
, &color
);
164 XSelectInput(display
, window
, ExposureMask
| ButtonPress
);
166 XMapWindow(display
, window
);
168 sem_t
*mutex
= sem_open("/herbe", O_CREAT
, 0644, 1);
171 signal(SIGUSR1
, expire
);
172 signal(SIGUSR2
, action
);
180 XNextEvent(display
, &event
);
182 if (event
.type
== Expose
)
184 XClearWindow(display
, window
);
185 for (int i
= 0; i
< num_of_lines
; i
++)
186 XftDrawStringUtf8(draw
, &color
, font
, padding
, line_spacing
* i
+ text_height
* (i
+ 1) + padding
, (FcChar8
*)words
[i
], strlen(words
[i
]));
188 if (event
.type
== ButtonPress
)
190 if (event
.xbutton
.button
== DISMISS_BUTTON
)
192 else if (event
.xbutton
.button
== ACTION_BUTTON
)
194 exit_code
= EXIT_ACTION
;
203 for (int i
= 0; i
< num_of_lines
; i
++)
207 XftDrawDestroy(draw
);
208 XftColorFree(display
, visual
, colormap
, &color
);
209 XftFontClose(display
, font
);
210 XCloseDisplay(display
);