2008-10-02 18:47:02 +00:00
|
|
|
/**************************************************************************
|
|
|
|
*
|
|
|
|
* Tint2 : clock
|
2009-01-17 14:07:56 +00:00
|
|
|
*
|
2008-10-02 18:47:02 +00:00
|
|
|
* Copyright (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
|
2009-01-17 14:07:56 +00:00
|
|
|
*
|
2008-10-02 18:47:02 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <cairo.h>
|
|
|
|
#include <cairo-xlib.h>
|
|
|
|
#include <pango/pangocairo.h>
|
2009-06-04 18:41:14 +00:00
|
|
|
#include <unistd.h>
|
2008-10-02 18:47:02 +00:00
|
|
|
|
|
|
|
#include "window.h"
|
|
|
|
#include "server.h"
|
|
|
|
#include "area.h"
|
2009-10-18 17:54:09 +00:00
|
|
|
#include "panel.h"
|
|
|
|
#include "taskbar.h"
|
2008-10-02 18:47:02 +00:00
|
|
|
#include "clock.h"
|
|
|
|
|
|
|
|
|
2009-06-04 18:41:14 +00:00
|
|
|
char *time1_format;
|
|
|
|
char *time2_format;
|
|
|
|
char *clock_lclick_command;
|
|
|
|
char *clock_rclick_command;
|
2009-02-03 20:40:46 +00:00
|
|
|
struct timeval time_clock;
|
|
|
|
int time_precision;
|
2009-02-07 23:28:13 +00:00
|
|
|
PangoFontDescription *time1_font_desc;
|
|
|
|
PangoFontDescription *time2_font_desc;
|
2009-02-10 23:16:10 +00:00
|
|
|
static char buf_time[40];
|
|
|
|
static char buf_date[40];
|
2009-02-03 20:40:46 +00:00
|
|
|
|
|
|
|
|
2009-06-05 18:53:49 +00:00
|
|
|
void init_precision()
|
|
|
|
{
|
|
|
|
if (!time1_format) time_precision = 60;
|
|
|
|
else if (strchr(time1_format, 'S')) time_precision = 1;
|
|
|
|
else if (strchr(time1_format, 'T')) time_precision = 1;
|
|
|
|
else if (strchr(time1_format, 'r')) time_precision = 1;
|
|
|
|
else time_precision = 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-15 17:22:48 +00:00
|
|
|
void init_clock()
|
2008-10-02 18:47:02 +00:00
|
|
|
{
|
2009-06-09 17:19:59 +00:00
|
|
|
init_precision();
|
2009-10-18 17:54:09 +00:00
|
|
|
|
2009-06-09 17:19:59 +00:00
|
|
|
// update clock to force update (-time_precision)
|
|
|
|
struct timeval stv;
|
|
|
|
gettimeofday(&stv, 0);
|
|
|
|
time_clock.tv_sec = stv.tv_sec - time_precision;
|
|
|
|
time_clock.tv_sec -= time_clock.tv_sec % time_precision;
|
2009-10-18 17:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void init_clock_panel(void *p)
|
|
|
|
{
|
|
|
|
Panel *panel =(Panel*)p;
|
|
|
|
Clock *clock = &panel->clock;
|
2009-10-22 20:31:53 +00:00
|
|
|
int time_height, time_height_ink, date_height, date_height_ink;
|
2009-10-18 17:54:09 +00:00
|
|
|
|
|
|
|
clock->area.parent = p;
|
|
|
|
clock->area.panel = p;
|
|
|
|
clock->area._draw_foreground = draw_clock;
|
|
|
|
clock->area._resize = resize_clock;
|
|
|
|
clock->area.resize = 1;
|
|
|
|
clock->area.redraw = 1;
|
|
|
|
|
|
|
|
strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
|
|
|
|
get_text_size(time1_font_desc, &time_height_ink, &time_height, panel->area.height, buf_time, strlen(buf_time));
|
|
|
|
if (time2_format) {
|
|
|
|
strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
|
|
|
|
get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (panel_horizontal) {
|
|
|
|
// panel horizonal => fixed height and posy
|
|
|
|
clock->area.posy = panel->area.pix.border.width + panel->area.paddingy;
|
|
|
|
clock->area.height = panel->area.height - (2 * clock->area.posy);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// panel vertical => fixed width, height, posy and posx
|
|
|
|
clock->area.posy = panel->area.pix.border.width + panel->area.paddingxlr;
|
|
|
|
clock->area.height = (2 * clock->area.paddingxlr) + (time_height + date_height);
|
|
|
|
clock->area.posx = panel->area.pix.border.width + panel->area.paddingy;
|
|
|
|
clock->area.width = panel->area.width - (2 * panel->area.pix.border.width) - (2 * panel->area.paddingy);
|
|
|
|
}
|
|
|
|
|
|
|
|
clock->time1_posy = (clock->area.height - time_height) / 2;
|
|
|
|
if (time2_format) {
|
|
|
|
strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
|
|
|
|
get_text_size(time2_font_desc, &date_height_ink, &date_height, panel->area.height, buf_date, strlen(buf_date));
|
2009-06-09 17:19:59 +00:00
|
|
|
|
2009-10-18 17:54:09 +00:00
|
|
|
clock->time1_posy -= ((date_height_ink + 2) / 2);
|
|
|
|
clock->time2_posy = clock->time1_posy + time_height + 2 - (time_height - time_height_ink)/2 - (date_height - date_height_ink)/2;
|
2009-02-15 17:22:48 +00:00
|
|
|
}
|
2008-10-02 18:47:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-28 23:04:53 +00:00
|
|
|
void draw_clock (void *obj, cairo_t *c, int active)
|
2009-02-10 23:16:10 +00:00
|
|
|
{
|
2009-09-07 21:41:21 +00:00
|
|
|
Clock *clock = obj;
|
|
|
|
PangoLayout *layout;
|
2009-02-10 23:16:10 +00:00
|
|
|
|
2009-09-07 21:41:21 +00:00
|
|
|
layout = pango_cairo_create_layout (c);
|
2009-02-10 23:16:10 +00:00
|
|
|
|
2009-09-07 21:41:21 +00:00
|
|
|
// draw layout
|
|
|
|
pango_layout_set_font_description (layout, time1_font_desc);
|
|
|
|
pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
|
|
|
|
pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
|
|
|
|
pango_layout_set_text (layout, buf_time, strlen(buf_time));
|
2009-02-10 23:16:10 +00:00
|
|
|
|
2009-09-07 21:41:21 +00:00
|
|
|
cairo_set_source_rgba (c, clock->font.color[0], clock->font.color[1], clock->font.color[2], clock->font.alpha);
|
2009-02-10 23:16:10 +00:00
|
|
|
|
2009-09-07 21:41:21 +00:00
|
|
|
pango_cairo_update_layout (c, layout);
|
|
|
|
cairo_move_to (c, 0, clock->time1_posy);
|
|
|
|
pango_cairo_show_layout (c, layout);
|
2009-02-10 23:16:10 +00:00
|
|
|
|
2009-09-07 21:41:21 +00:00
|
|
|
if (time2_format) {
|
|
|
|
pango_layout_set_font_description (layout, time2_font_desc);
|
|
|
|
pango_layout_set_indent(layout, 0);
|
|
|
|
pango_layout_set_text (layout, buf_date, strlen(buf_date));
|
|
|
|
pango_layout_set_width (layout, clock->area.width * PANGO_SCALE);
|
2009-02-10 23:16:10 +00:00
|
|
|
|
2009-09-07 21:41:21 +00:00
|
|
|
pango_cairo_update_layout (c, layout);
|
|
|
|
cairo_move_to (c, 0, clock->time2_posy);
|
|
|
|
pango_cairo_show_layout (c, layout);
|
|
|
|
}
|
2009-02-10 23:16:10 +00:00
|
|
|
|
2009-09-07 21:41:21 +00:00
|
|
|
g_object_unref (layout);
|
2009-02-10 23:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void resize_clock (void *obj)
|
2008-10-02 18:47:02 +00:00
|
|
|
{
|
2009-09-07 21:41:21 +00:00
|
|
|
Clock *clock = obj;
|
|
|
|
PangoLayout *layout;
|
|
|
|
int time_width, date_width, new_width;
|
2008-10-02 18:47:02 +00:00
|
|
|
|
2009-09-07 21:41:21 +00:00
|
|
|
clock->area.redraw = 1;
|
|
|
|
time_width = date_width = 0;
|
|
|
|
strftime(buf_time, sizeof(buf_time), time1_format, localtime(&time_clock.tv_sec));
|
|
|
|
if (time2_format)
|
|
|
|
strftime(buf_date, sizeof(buf_date), time2_format, localtime(&time_clock.tv_sec));
|
2008-10-02 18:47:02 +00:00
|
|
|
|
2009-06-21 22:01:31 +00:00
|
|
|
// vertical panel doen't adjust width
|
|
|
|
if (!panel_horizontal) return;
|
|
|
|
|
2009-09-07 21:41:21 +00:00
|
|
|
//printf(" resize_clock\n");
|
|
|
|
cairo_surface_t *cs;
|
|
|
|
cairo_t *c;
|
2009-02-10 23:16:10 +00:00
|
|
|
Pixmap pmap;
|
|
|
|
pmap = XCreatePixmap (server.dsp, server.root_win, clock->area.width, clock->area.height, server.depth);
|
|
|
|
|
2009-09-07 21:41:21 +00:00
|
|
|
cs = cairo_xlib_surface_create (server.dsp, pmap, server.visual, clock->area.width, clock->area.height);
|
|
|
|
c = cairo_create (cs);
|
|
|
|
layout = pango_cairo_create_layout (c);
|
2008-10-02 18:47:02 +00:00
|
|
|
|
2009-09-07 21:41:21 +00:00
|
|
|
// check width
|
2009-06-21 22:01:31 +00:00
|
|
|
pango_layout_set_font_description (layout, time1_font_desc);
|
|
|
|
pango_layout_set_indent(layout, 0);
|
|
|
|
pango_layout_set_text (layout, buf_time, strlen(buf_time));
|
|
|
|
pango_layout_get_pixel_size (layout, &time_width, NULL);
|
|
|
|
if (time2_format) {
|
|
|
|
pango_layout_set_font_description (layout, time2_font_desc);
|
|
|
|
pango_layout_set_indent(layout, 0);
|
|
|
|
pango_layout_set_text (layout, buf_date, strlen(buf_date));
|
|
|
|
pango_layout_get_pixel_size (layout, &date_width, NULL);
|
|
|
|
}
|
2009-02-10 23:16:10 +00:00
|
|
|
|
2009-06-21 22:01:31 +00:00
|
|
|
if (time_width > date_width) new_width = time_width;
|
|
|
|
else new_width = date_width;
|
|
|
|
new_width += (2*clock->area.paddingxlr) + (2*clock->area.pix.border.width);
|
2009-01-17 14:07:56 +00:00
|
|
|
|
2009-09-27 17:50:10 +00:00
|
|
|
Panel *panel = ((Area*)obj)->panel;
|
|
|
|
clock->area.posx = panel->area.width - clock->area.width - panel->area.paddingxlr - panel->area.pix.border.width;
|
2009-02-13 21:54:42 +00:00
|
|
|
|
2009-09-27 17:50:10 +00:00
|
|
|
if (new_width > clock->area.width || new_width < (clock->area.width-6)) {
|
2009-09-07 21:41:21 +00:00
|
|
|
// resize clock
|
|
|
|
// we try to limit the number of resize
|
|
|
|
// printf("clock_width %d, new_width %d\n", clock->area.width, new_width);
|
2009-06-21 22:01:31 +00:00
|
|
|
clock->area.width = new_width + 1;
|
2009-02-13 21:54:42 +00:00
|
|
|
|
2009-09-07 21:41:21 +00:00
|
|
|
// resize other objects on panel
|
2009-06-08 18:49:50 +00:00
|
|
|
panel->area.resize = 1;
|
2009-06-18 20:26:40 +00:00
|
|
|
#ifdef ENABLE_BATTERY
|
2009-06-08 18:49:50 +00:00
|
|
|
panel->battery.area.resize = 1;
|
2009-06-18 20:26:40 +00:00
|
|
|
#endif
|
2009-03-07 10:05:15 +00:00
|
|
|
systray.area.resize = 1;
|
2009-02-13 21:54:42 +00:00
|
|
|
panel_refresh = 1;
|
2009-09-07 21:41:21 +00:00
|
|
|
}
|
2009-01-17 14:07:56 +00:00
|
|
|
|
2009-09-07 21:41:21 +00:00
|
|
|
g_object_unref (layout);
|
|
|
|
cairo_destroy (c);
|
|
|
|
cairo_surface_destroy (cs);
|
|
|
|
XFreePixmap (server.dsp, pmap);
|
2008-10-02 18:47:02 +00:00
|
|
|
}
|
|
|
|
|
2009-06-04 18:41:14 +00:00
|
|
|
|
|
|
|
void clock_action(int button)
|
|
|
|
{
|
|
|
|
char *command = 0;
|
|
|
|
switch (button) {
|
|
|
|
case 1:
|
|
|
|
command = clock_lclick_command;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
command = clock_rclick_command;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (command) {
|
|
|
|
pid_t pid;
|
|
|
|
pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
execl("/bin/sh", "/bin/sh", "-c", command, NULL);
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|