1 /* wmusic - a xmms remote-controlling DockApp
2 * Copyright (C) 2000-2001 Bastien Nocera <hadess@hadess.net>
3 * Maintained by John Chapin <john+wmusic@jtan.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #define DBL_CLICK_INTERVAL 250 /* double click interval in milliseconds */
21 #define ARROW_INTERVAL 100 /* arrow update interval in milliseconds */
22 #define SCROLL_INTERVAL 300 /* scroll update interval in milliseconds */
23 #define SEPARATOR " ** " /* The separator for the scrolling title */
24 #define DISPLAYSIZE 6 /* width of text to display (running title) */
26 #include <libdockapp/dockapp.h>
27 #include <playerctl/playerctl.h>
33 #include "wmusic-master.xpm"
34 #include "wmusic-digits.xpm"
36 /*---------------------------------------------------------------------------*/
38 /*---------------------------------------------------------------------------*/
40 void copyNumArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
);
41 void ActionPlay(int x
, int y
, DARect rect
, void *data
);
42 void ActionPause(int x
, int y
, DARect rect
, void *data
);
43 void ActionEject(int x
, int y
, DARect rect
, void *data
);
44 void ActionPrev(int x
, int y
, DARect rect
, void *data
);
45 void ActionNext(int x
, int y
, DARect rect
, void *data
);
46 void ActionStop(int x
, int y
, DARect rect
, void *data
);
47 void ActionFastr(int x
, int y
, DARect rect
, void *data
);
48 void ActionFastf(int x
, int y
, DARect rect
, void *data
);
49 void ToggleWins(int x
, int y
, DARect rect
, void *data
);
50 void ToggleVol(int x
, int y
, DARect rect
, void *data
);
51 void ChangeVol(int x
, int y
, DARect rect
, void *data
);
52 void ToggleTime(int x
, int y
, DARect rect
, void *data
);
53 void buttonPress(int button
, int state
, int x
, int y
);
54 void buttonRelease(int button
, int state
, int x
, int y
);
55 int PlayerConnect(void);
56 void DisplayRoutine();
57 void DrawPos (int pos
);
58 void DrawTime(int time
);
60 void DrawVolume(void);
61 void DrawTitle(char *title
);
62 void ExecuteXmms(void);
64 /*----------------------------------------------------------------------------*/
66 /*----------------------------------------------------------------------------*/
69 char *displayName
= "";
73 /* Dockapp variables */
74 PlayerctlPlayer
*player
;
75 char *xmms_cmd
= "xmms";
76 Bool main_vis
=0, pl_vis
=0, eq_vis
=0;
77 unsigned int volume_step
= 5;
82 unsigned int arrow_pos
= 0;
83 Bool pause_norotate
= 0;
86 Pixmap pixmap
, mask
; /* Pixmap that is displayed */
87 Pixmap pixnum
, masknum
; /* Pixmap source */
89 int left_pressed
= 0; /* for pseudo drag callback */
90 int motion_event
= 0; /* on motion events we do not want(too fast) display update */
92 static DAActionRect buttonRects
[] = {
93 {{5, 39, 14, 9}, ActionPrev
},
94 {{19, 39, 14, 9}, ActionNext
},
95 {{33, 39, 13, 9}, ActionFastr
},
96 {{46, 39, 13, 9}, ActionFastf
},
97 {{5, 48, 11, 11}, ActionEject
},
98 {{16, 48, 21, 11}, ActionPlay
},
99 {{37, 48, 11, 11}, ActionPause
},
100 {{48, 48, 11, 11}, ActionStop
}
103 static DAActionRect toggleRect
[] = {
104 {{5, 5, 54, 30}, ToggleWins
}
107 static DAActionRect globRect
[] = {
108 {{0, 0, 64, 64}, ToggleVol
}
111 static DAActionRect displayRects
[] = {
112 {{5, 5, 54, 12}, ToggleTime
},
115 static DAActionRect volumeRects
[] = {
116 {{5, 17, 38, 8 }, ChangeVol
}
119 static DAProgramOption options
[] = {
120 {"-c", "--command", "Command to launch xmms", DOString
, False
,
122 {"-d", "--display", "Display to use", DOString
, False
, {&displayName
} },
123 {"-r", "--run", "Run xmms on startup", DONone
, False
, {NULL
} },
124 {"-V", "--volume", "Stepping of the wheel volume control (in percent)",
125 DONatural
, False
, {&volume_step
} },
126 {"-a", "--rotate-arrow", "Do not rotate the arrow, when paused",
127 DONone
, False
, {NULL
} },
128 {"-l", "--time-left", "Show time left instead of time remaining by default",
129 DONone
, False
, {NULL
} },
130 {"-R", "--run-excusive", "Run xmms on startup, exit when xmms exits", DONone
, False
, {NULL
} }
140 static glyphdescr glyphs
[] = {
141 {L
'-', 67, 83}, {L
'.', 73, 83}, {L
'\x27', 79, 83},
142 {L
'(', 85, 83}, {L
')', 91, 83}, {L
'*', 97, 83}, {L
'/', 103, 83},
144 {L
'0', 1, 83}, {L
'1', 7, 83}, {L
'2', 13, 83}, {L
'3', 19, 83}, {L
'4', 25, 83},
145 {L
'5', 31, 83}, {L
'6', 37, 83}, {L
'7', 43, 83}, {L
'8', 49, 83}, {L
'9', 55, 83},
149 {L
'A', 1, 73}, {L
'a', 1, 73},
150 {L
'B', 7, 73}, {L
'b', 7, 73},
151 {L
'C', 13, 73}, {L
'c', 13, 73},
152 {L
'D', 19, 73}, {L
'd', 19, 73},
153 {L
'E', 25, 73}, {L
'e', 25, 73},
155 {L
'F', 31, 73}, {L
'f', 31, 73},
156 {L
'G', 37, 73}, {L
'g', 37, 73},
157 {L
'H', 43, 73}, {L
'h', 43, 73},
158 {L
'I', 49, 73}, {L
'i', 49, 73},
159 {L
'J', 55, 73}, {L
'j', 55, 73},
161 {L
'K', 61, 73}, {L
'k', 61, 73},
162 {L
'L', 67, 73}, {L
'l', 67, 73},
163 {L
'M', 73, 73}, {L
'm', 73, 73},
164 {L
'N', 79, 73}, {L
'n', 79, 73},
165 {L
'O', 85, 73}, {L
'o', 85, 73},
167 {L
'P', 91, 73}, {L
'p', 91, 73},
168 {L
'Q', 97, 73}, {L
'q', 97, 73},
169 {L
'R',103, 73}, {L
'r',103, 73},
170 {L
'S',109, 73}, {L
's',109, 73},
171 {L
'T',115, 73}, {L
't',115, 73},
173 {L
'U',121, 73}, {L
'u',121, 73},
174 {L
'V',127, 73}, {L
'v',127, 73},
175 {L
'W',133, 73}, {L
'w',133, 73},
176 {L
'X',139, 73}, {L
'x',139, 73},
177 {L
'Y',145, 73}, {L
'y',145, 73},
179 {L
'Z',151, 73}, {L
'z',151, 73},
182 {L
'\x42e', 1, 93}, {L
'\x44e', 1, 93}, /* cyrillic Yu */
184 {L
'\x410', 7, 93}, {L
'\x430', 7, 93}, /* cyrillic A */
185 {L
'\x411', 13, 93}, {L
'\x431', 13, 93}, /* cyrillic Be */
186 {L
'\x426', 19, 93}, {L
'\x446', 19, 93}, /* cyrillic Ce */
187 {L
'\x414', 25, 93}, {L
'\x434', 25, 93}, /* cyrillic De */
188 {L
'\x415', 31, 93}, {L
'\x435', 31, 93}, /* cyrillic Ye */
190 {L
'\x424', 37, 93}, {L
'\x444', 37, 93}, /* cyrillic eF */
191 {L
'\x413', 43, 93}, {L
'\x433', 43, 93}, /* cyrillic Ge */
192 {L
'\x425', 49, 93}, {L
'\x445', 49, 93}, /* cyrillic Ha */
193 {L
'\x418', 55, 93}, {L
'\x438', 55, 93}, /* cyrillic I */
194 {L
'\x419', 61, 93}, {L
'\x439', 61, 93}, /* cyrillic I-kratkoe */
196 {L
'\x41a', 67, 93}, {L
'\x43a', 67, 93}, /* cyrillic Ka */
197 {L
'\x41b', 73, 93}, {L
'\x43b', 73, 93}, /* cyrillic eL */
198 {L
'\x41c', 79, 93}, {L
'\x43c', 79, 93}, /* cyrillic eM */
199 {L
'\x41d', 85, 93}, {L
'\x43d', 85, 93}, /* cyrillic eN */
200 {L
'\x41e', 91, 93}, {L
'\x43e', 91, 93}, /* cyrillic O */
202 {L
'\x41f', 97, 93}, {L
'\x43f', 97, 93}, /* cyrillic Pe */
203 {L
'\x42f',103, 93}, {L
'\x44f',103, 93}, /* cyrillic Ya */
204 {L
'\x420',109, 93}, {L
'\x440',109, 93}, /* cyrillic eR */
205 {L
'\x421',115, 93}, {L
'\x441',115, 93}, /* cyrillic eS */
206 {L
'\x422',121, 93}, {L
'\x442',121, 93}, /* cyrillic Te */
208 {L
'\x423',127, 93}, {L
'\x443',127, 93}, /* cyrillic U */
209 {L
'\x416',133, 93}, {L
'\x436',133, 93}, /* cyrillic Je */
210 {L
'\x412',139, 93}, {L
'\x432',139, 93}, /* cyrillic Ve */
211 {L
'\x42c',145, 93}, {L
'\x44c',145, 93}, /* cyrillic MyagkijZnak */
212 {L
'\x42b',151, 93}, {L
'\x44b',151, 93}, /* cyrillic Y */
214 {L
'\x417',157, 93}, {L
'\x437',157, 93}, /* cyrillic Ze */
215 {L
'\x428',163, 93}, {L
'\x448',163, 93}, /* cyrillic Sha */
216 {L
'\x42d',169, 93}, {L
'\x44d',169, 93}, /* cyrillic E */
217 {L
'\x429',175, 93}, {L
'\x449',175, 93}, /* cyrillic Scha */
218 {L
'\x427',181, 93}, {L
'\x447',181, 93}, /* cyrillic Che */
220 {L
'\x42a',187, 93}, {L
'\x44a',187, 93}, /* cyrillic TvyordyiZnak */
221 {L
'\x404',115, 83}, {L
'\x454',115, 83}, /* ukrainian IE */
222 {L
'\x406', 49, 73}, {L
'\x456', 49, 73}, /* ukrainian I */
223 {L
'\x407',109, 83}, {L
'\x457',109, 83}, /* ukrainian YI */
224 {L
'\x491', 43, 93}, {L
'\x490', 43, 93}, /* ukrainian GHE with upturn */
226 {L
'\x401',121, 83}, {L
'\x451',121, 83}, /* cyrillic Yo */
231 /*----------------------------------------------------------------------------*/
233 /*----------------------------------------------------------------------------*/
235 void copyNumArea(int x
, int y
, int sx
, int sy
, int dx
, int dy
)
237 XCopyArea(DADisplay
, pixnum
, pixmap
, gc
, x
, y
, sx
, sy
, dx
, dy
);
240 void buttonDraw(DARect rect
)
242 copyNumArea((rect
.x
)-5, (rect
.y
)-8, rect
.width
, rect
.height
,
247 void ActionPlay(int x
, int y
, DARect rect
, void *data
)
252 GError
*error
= NULL
;
254 playerctl_player_play(player
, &error
);
256 DAWarning("Could not execute command: %s",
261 void ActionPause(int x
, int y
, DARect rect
, void *data
)
266 GError
*error
= NULL
;
268 playerctl_player_pause(player
, &error
);
270 DAWarning("Could not execute command: %s",
275 void ActionEject(int x
, int y
, DARect rect
, void *data
)
280 DAWarning("Eject function is no longer supported.");
284 void ActionPrev(int x
, int y
, DARect rect
, void *data
)
289 GError
*error
= NULL
;
291 playerctl_player_previous(player
, &error
);
293 DAWarning("Could not execute command: %s",
298 void ActionNext(int x
, int y
, DARect rect
, void *data
)
303 GError
*error
= NULL
;
305 playerctl_player_next(player
, &error
);
307 DAWarning("Could not execute command: %s",
312 void ActionStop(int x
, int y
, DARect rect
, void *data
)
317 GError
*error
= NULL
;
319 playerctl_player_stop(player
, &error
);
321 DAWarning("Could not execute command: %s",
326 void ActionFastr(int x
, int y
, DARect rect
, void *data
)
331 GError
*error
= NULL
;
333 playerctl_player_seek(player
, -10000000, &error
);
335 DAWarning("Could not execute command: %s",
340 void ActionFastf(int x
, int y
, DARect rect
, void *data
)
345 GError
*error
= NULL
;
347 playerctl_player_seek(player
, 10000000, &error
);
349 DAWarning("Could not execute command: %s",
354 void ToggleWins(int x
, int y
, DARect rect
, void *data
)
357 if ( (ev
.xbutton
.time
-click_time
) <= DBL_CLICK_INTERVAL
)
362 click_time
=ev
.xbutton
.time
;
367 void ToggleVol(int x
, int y
, DARect rect
, void *data
)
372 g_object_get(player
, "volume", &volume
, NULL
);
374 if (*(int*)data
== 1)
375 factor
= 0.01 * volume_step
;
377 factor
= -0.01 * volume_step
;
385 g_object_set(player
, "volume", volume
, NULL
);
388 void ChangeVol(int x
, int y
, DARect rect
, void *data
)
390 float volume
= ((float)x
)/38;
391 g_object_set(player
, "volume", volume
, NULL
);
394 void ToggleTime(int x
, int y
, DARect rect
, void *data
)
401 void buttonPress(int button
, int state
, int x
, int y
)
410 DAProcessActionRects(x
, y
, buttonRects
, sizeof(buttonRects
)/sizeof(DAActionRect
), tmp
);
411 DAProcessActionRects(x
, y
, displayRects
, sizeof(displayRects
)/sizeof(DAActionRect
), tmp
);
415 DAProcessActionRects(x
, y
, toggleRect
, sizeof(toggleRect
)/sizeof(DAActionRect
), NULL
);
420 DAProcessActionRects(x
, y
, toggleRect
, sizeof(toggleRect
)/sizeof(DAActionRect
), tmp
);
422 if ((button
== 4) || (button
== 5))
426 /* Wheel scrolls down */
428 DAProcessActionRects(x
, y
, globRect
, sizeof(globRect
)/sizeof(DAActionRect
), &tmp
);
430 /* Wheel scrolls up */
432 DAProcessActionRects(x
, y
, globRect
, sizeof(globRect
)/sizeof(DAActionRect
), &tmp
);
437 DAProcessActionRects(x
, y
, toggleRect
, sizeof(toggleRect
)/sizeof(DAActionRect
), NULL
);
440 void buttonRelease(int button
, int state
, int x
, int y
)
448 copyNumArea(0,51, 54, 20, 5,39);
450 DAProcessActionRects(x
, y
, buttonRects
, sizeof(buttonRects
)/sizeof(DAActionRect
), NULL
);
455 void buttonDrag(int x
, int y
)
458 if (left_pressed
==1) {
459 DAProcessActionRects(x
,y
, volumeRects
, 1, NULL
);
465 int PlayerConnect(void)
467 GError
*error
= NULL
;
468 static int previous_error_code
= 0;
469 static char* player_name
= NULL
;
471 player
= playerctl_player_new(NULL
, &error
);
473 /* don't spam error message */
474 if (error
->code
!= previous_error_code
)
475 DAWarning("Connection to player failed: %s",
477 previous_error_code
= error
->code
;
481 previous_error_code
= 0;
483 g_object_get(player
, "player_name", &player_name
, NULL
);
484 player_name
++; /* get rid of opening dot */
486 DAWarning("Connected to %s", player_name
);
492 void DisplayRoutine()
494 int time
= 0, length
= 0, position
= 100;
496 GError
*error
= NULL
;
505 title
= strdup("--");
509 char *length_str
, *position_str
, *status
;
511 g_object_get(player
, "status", &status
, NULL
);
513 if (!strcmp(status
, "Playing") ||
514 !strcmp(status
, "Paused")) {
515 g_object_get(player
, "position", &time
, NULL
);
517 title
= playerctl_player_get_title(player
,
520 DAWarning("%s", error
->message
);
523 playerctl_player_print_metadata_prop(
524 player
, "mpris:length", &error
);
526 DAWarning("%s", error
->message
);
528 length
= atoi(length_str
);
533 playerctl_player_print_metadata_prop(
534 player
, "xesam:trackNumber",
537 DAWarning("%s", error
->message
);
539 position
= atoi(position_str
);
543 if (!strcmp(status
, "Paused") && pause_norotate
)
545 } else { /* not playing or paused */
546 title
= strdup("--");
551 } else { /* status undefined */
552 title
= strdup("--");
559 if (t_time
&& length
) DrawTime((length
-time
) / 1000);
560 else DrawTime(time
/ 1000);
572 void DrawPos (int pos
)
579 sprintf(posstr
, "%02d", pos
);
583 copyNumArea((*p
-'0')*6 + 1, 1, 6, 7, (i
*6)+39, 7);
588 void DrawTime(int time
)
597 * up to 99 minutes and 59 seconds
598 * up to 99 hours and 59 minutes
603 sprintf(timestr
, "%02d%02d", time
/ 60, time
% 60);
607 sprintf(timestr
, "%02d%02d", time
/ 3600,
610 sprintf(timestr
, "%02d%02d", 0, 0);
616 copyNumArea((*p
-'0')*7 + 2, 11, 7, 9, i
<2 ?(i
*7)+7:(i
*7)+12, 7);
623 copyNumArea((arrow_pos
*8)+30, 22, 8, 9, 47, 19);
625 if (arrow_pos
> 4) arrow_pos
= 0;
628 void DrawVolume(void)
631 double volume_double
;
633 g_object_get(player
, "volume", &volume_double
, NULL
);
634 volume
= (int)(36 * volume_double
);
637 copyNumArea(61, 0, volume
, 6, 7, 18);
638 copyNumArea(97, 0, 36-volume
, 6, 7+volume
, 18);
641 void DrawKbps(int bps
)
647 if (bps
> 999000) bps
=0;
648 sprintf(kbpstr
, "%03d", bps
/ 1000);
652 copyNumArea((*p
-'0')*6 + 1, 1, 6, 7, (i
*6)+1, 26);
655 copyNumArea(55, 39, 18, 8, 25, 26);
658 void DrawChar(wchar_t wc
, int x
, int y
)
661 for(i
= 0; i
< sizeof(glyphs
)/sizeof(glyphdescr
) - 1; ++i
)
663 if(wc
== glyphs
[i
].c
)
666 copyNumArea(glyphs
[i
].x
, glyphs
[i
].y
, 6, 8, x
, y
);
669 int DrawChars(char *title
, int tpos
, int pos
)
673 mbtowc(NULL
, NULL
, 0);
674 while(*title
&& (pos
<= (tpos
+ DISPLAYSIZE
)))
676 int len
= mbtowc(&wc
, title
, MB_CUR_MAX
);
679 DrawChar(wc
, (pos
- tpos
)*6 + 7, 26);
685 void DrawTitle(char *name
)
687 int len
, pos
, tpos
= title_pos
;
692 len
= pos
= DrawChars(name
, tpos
, 0);
696 DrawChars(" ", tpos
, pos
);
700 if(pos
<= tpos
+ DISPLAYSIZE
)
701 pos
= DrawChars(SEPARATOR
, tpos
, pos
);
703 if(pos
<= tpos
+ DISPLAYSIZE
)
704 DrawChars(name
, tpos
, pos
);
706 if(tpos
>= len
+ strlen(SEPARATOR
))
709 title_pos
= title_pos
+ 0.5;
712 void ExecuteXmms(void)
717 command
=malloc(strlen(xmms_cmd
)+5);
718 sprintf(command
, "%s &", xmms_cmd
);
719 status
= system(command
);
722 fprintf(stderr
, "XMMS can't be launched, exiting...");
725 while (!PlayerConnect())
729 /*----------------------------------------------------------------------------*/
731 /*----------------------------------------------------------------------------*/
733 int main(int argc
, char **argv
)
735 short unsigned int height
, width
;
736 DACallbacks callbacks
={NULL
, buttonPress
, buttonRelease
, buttonDrag
,
740 DAParseArguments(argc
, argv
, options
,
741 sizeof(options
)/sizeof(DAProgramOption
),
742 "XMMS remote control by Bastien Nocera <hadess@hadess.net>",
745 setlocale(LC_ALL
, "");
746 DAInitialize(displayName
, "wmusic", 64, 64, argc
, argv
);
747 DASetCallbacks(&callbacks
);
750 DAMakePixmapFromData(wmusic_master_xpm
, &pixmap
,
751 &mask
, &height
, &width
);
752 DAMakePixmapFromData(wmusic_digits_xpm
, &pixnum
,
753 &masknum
, &height
, &width
);
754 gc
= DefaultGC(DADisplay
, DefaultScreen(DADisplay
));
755 DASetShapeWithOffset(mask
, 0, 0);
760 /* End of initialization */
762 if (options
[4].used
) pause_norotate
=1;
763 if (options
[5].used
) t_time
=1;
764 if (options
[6].used
) run_excusive
=1;
768 /* Launch xmms if it's not running and -r or -R was used */
769 if ((!player
) && (options
[2].used
|| run_excusive
))
774 /* Update the display */
778 if (DANextEventOrTimeout(&ev
, 100))