support multiple uris in playlist dnd
[uwplayer.git] / application / Sidebar.c
1 /*
2  * Copyright 2022 Olaf Wintermann
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a 
5  * copy of this software and associated documentation files (the "Software"), 
6  * to deal in the Software without restriction, including without limitation 
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
8  * and/or sell copies of the Software, and to permit persons to whom the 
9  * Software is furnished to do so, subject to the following conditions:
10  * 
11  * The above copyright notice and this permission notice shall be included in 
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
20  * DEALINGS IN THE SOFTWARE.
21  */
22
23 #include "Sidebar.h"
24 #include "SidebarP.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30
31 #include "xdnd.h"
32 #include "utils.h"
33 #include "playlist.h"
34 #include "ucx/string.h"
35
36
37 static void sidebar_class_init(void);
38 static void sidebar_class_part_init (WidgetClass wc);
39 static void sidebar_init(Widget request, Widget neww, ArgList args, Cardinal *num_args);
40 static void sidebar_destroy(Widget widget);
41 static void sidebar_resize(Widget widget);
42 static void sidebar_realize(Widget widget, XtValueMask *mask, XSetWindowAttributes *attributes);
43 static void sidebar_expose(Widget widget, XEvent *event, Region region);
44 static Boolean sidebar_set_values(Widget old, Widget request, Widget neww, ArgList args, Cardinal *num_args);
45 static void sidebar_insert_child(Widget child);
46 Boolean sidebar_acceptfocus(Widget widget, Time *time);
47
48 static void FocusInAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
49 static void SelectElmAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
50 static void PopupAP(Widget w, XEvent *event, String *args, Cardinal *nArgs);
51
52 static void sidebar_xdnd_callback(Widget w, XtPointer udata, XtPointer cdata);
53
54
55 static XtResource resources[] = {
56
57 };
58
59 static XtActionsRec actionslist[] = {
60   {"focusIn", FocusInAP},
61   {"selectElm", SelectElmAP},
62   {"popup", PopupAP}
63 };
64
65 static char defaultTranslations[] = "<FocusIn>:  focusIn()\n"
66                                     "<Btn1Down>: selectElm()\n"
67                                     "<Btn3Down>: popup()";
68
69
70 static XtResource constraints[] = {};
71
72 SidebarClassRec sidebarWidgetClassRec = {
73     // Core Class
74     {
75         (WidgetClass)&xmManagerClassRec,
76         "XnSidebar",                         // class_name
77         sizeof(SidebarRec),                  // widget_size
78         sidebar_class_init,                  // class_initialize
79         sidebar_class_part_init,             // class_part_initialize
80         FALSE,                           // class_inited
81         sidebar_init,                        // initialize
82         NULL,                            // initialize_hook
83         sidebar_realize,                     // realize
84         actionslist,                     // actions
85         XtNumber(actionslist),           // num_actions
86         resources,                       // resources
87         XtNumber(resources),             // num_resources
88         NULLQUARK,                       // xrm_class
89         True,                            // compress_motion
90         True,                            // compress_exposure
91         True,                            // compress_enterleave
92         False,                           // visible_interest
93         sidebar_destroy,                     // destroy
94         sidebar_resize,                      // resize
95         sidebar_expose,                 // expose
96         sidebar_set_values,                  // set_values
97         NULL,                            // set_values_hook
98         XtInheritSetValuesAlmost,        // set_values_almost
99         NULL,                            // get_values_hook
100         sidebar_acceptfocus,                 // accept_focus
101         XtVersion,                       // version
102         NULL,                            // callback_offsets
103         defaultTranslations,             // tm_table
104         XtInheritQueryGeometry,          // query_geometry
105         XtInheritDisplayAccelerator,     // display_accelerator
106         NULL,                            // extension
107     },
108     // Composite Class
109     {
110         XtInheritGeometryManager, // geometry_manager 
111         XtInheritChangeManaged,   // change_managed
112         sidebar_insert_child,         // insert_child 
113         XtInheritDeleteChild,     // delete_child  
114         NULL,                     // extension   
115     },
116     // Constraint Class
117     {
118         constraints,                 // resources
119         XtNumber(constraints),       // num_resources   
120         sizeof(XmFormConstraintRec), // constraint_size  
121         NULL,                        // initialize 
122         NULL,                        // destroy
123         NULL,                        // set_value
124         NULL,                        // extension 
125     },
126     // XmManager Class
127     {
128         XtInheritTranslations, // translations
129         NULL, // syn_resources
130         0,    // num_syn_resources
131         NULL, // syn_constraint_resources
132         0,    // num_syn_constraint_resources
133         XmInheritParentProcess, // parent_process
134         NULL  // extension
135     },
136     // Sidebar Class
137     {
138         0
139     }
140 };
141
142 WidgetClass xnSidebarWidgetClass = (WidgetClass)&sidebarWidgetClassRec;
143
144
145 static void sidebar_class_init(void) {
146
147 }
148
149 static void sidebar_class_part_init (WidgetClass wc) {
150     SidebarClassRec *sidebarClass = (SidebarClassRec*)wc;
151     XmManagerClassRec *managerClass = (XmManagerClassRec*)xmManagerWidgetClass;
152     
153     sidebarClass->constraint_class.initialize = managerClass->constraint_class.initialize;
154     sidebarClass->constraint_class.set_values = managerClass->constraint_class.set_values;
155 }
156
157
158
159 static void sidebar_init(Widget request, Widget neww, ArgList args, Cardinal *num_args) {
160     Sidebar sb = (Sidebar)neww;
161     
162     // initialize everything except XftDraw or other stuff that needs a Window
163     
164     XftColor fg, bg;
165     fg.color.red = 0;
166     fg.color.green = 0;
167     fg.color.blue = 0;
168     fg.color.alpha = 0xFFFF;
169     
170     bg.color.red = 0xFFFF;
171     bg.color.green = 0xFFFF;
172     bg.color.blue = 0xFFFF;
173     bg.color.alpha = 0xFFFF;
174     
175     sb->sidebar.fg = fg;
176     sb->sidebar.bg = bg;
177     
178     sb->sidebar.font = FontFromName(XtDisplay(neww), "Sans:size=11");
179     if(!sb->sidebar.font) {
180         fprintf(stderr, "Cannot open font.\nAbort.\n");
181         exit(-1);
182     }
183     
184     XftFont *xftFont = sb->sidebar.font->fonts->font;
185     int padding = 2;
186     int fontheight = xftFont->ascent;
187     sb->sidebar.elmHeight = fontheight + 2*padding;
188 }
189
190
191
192 static void sidebar_destroy(Widget widget) {
193     
194 }
195
196 static int testresize = 1;
197 static void sidebar_resize(Widget widget) {
198     if(testresize) {
199         XtWidgetGeometry geom;
200         geom.request_mode = CWWidth | CWHeight;
201         geom.width = 200;
202         geom.height = 2000;
203         XtWidgetGeometry result;
204         //XtMakeGeometryRequest(widget, &geom, &result);
205         testresize = 0;
206     }
207     printf("resize\n");
208 }
209
210 static int xdnd_initialized = 0;
211
212 static void sidebar_realize(Widget widget, XtValueMask *mask, XSetWindowAttributes *attributes) {
213     (xmManagerClassRec.core_class.realize)(widget, mask, attributes);
214     
215     if(!xdnd_initialized) {
216         XdndInit(XtDisplay(widget), XtWidgetToApplicationContext(widget), sidebar_xdnd_callback, widget);
217     }
218     XdndEnable(widget);
219     
220     Screen *screen = widget->core.screen;
221     Visual *visual = screen->root_visual;
222     for(int i=0;i<screen->ndepths;i++) {
223         Depth d = screen->depths[i];
224         if(d.depth == widget->core.depth) {
225             visual = d.visuals;
226             break;
227         }
228     }
229     
230     Sidebar sb = (Sidebar)widget;
231     sb->sidebar.d = XftDrawCreate(
232             XtDisplay(widget),
233             XtWindow(widget),
234             visual,
235             widget->core.colormap);
236     
237     Dimension w, h;
238     
239     Widget parent = XtParent(widget);
240     XtVaSetValues(parent, XmNbackground, WhitePixelOfScreen(XtScreen(parent)), NULL);
241     
242     XtMakeResizeRequest(widget, 200, 100, &w, &h);
243     
244     
245 }
246
247 static void sidebar_expose(Widget widget, XEvent *event, Region region) {
248     //printf("expose\n");
249     Dimension w, h;
250     //XtMakeResizeRequest(widget, 200, 2000, &w, &h);
251     
252     w = widget->core.width;
253     h = widget->core.height;
254     
255     Widget parent = XtParent(widget);
256     
257     Sidebar s = (Sidebar)widget;
258     XftFont *xftFont = s->sidebar.font->fonts->font;
259     UcxList *tracks = s->sidebar.window->playlist.tracks;
260     size_t numTracks = ucx_list_size(tracks);
261     
262     int fontheight = xftFont->ascent;
263     int height = s->sidebar.elmHeight;
264     
265     
266     int list_height = numTracks * height;
267     if((list_height > s->core.height) || (w < parent->core.width)) {
268         XtMakeResizeRequest(widget, parent->core.width, list_height + 5, &w, &h);
269     }
270     
271     
272     XftDrawRect(s->sidebar.d, &s->sidebar.bg, 0, 0, w, h);
273     
274     
275     //printf("current track: %d\n", s->sidebar.window->playlist.current_track);
276     
277     int i = 0;
278     UCX_FOREACH(elm, s->sidebar.window->playlist.tracks) {
279         char *name = util_resource_name(elm->data);
280         XftColor *cg = &s->sidebar.fg;
281         if(i == s->sidebar.window->playlist.current_track) {
282             XftDrawRect(s->sidebar.d, &s->sidebar.fg, 0, i*height, s->core.width, height);
283             cg = &s->sidebar.bg;
284         }
285         
286         XftDrawString8(s->sidebar.d, cg, s->sidebar.font->fonts->font, 10, i*height + xftFont->ascent, (const FcChar8*)name, strlen(name));
287         
288         i++;
289     }
290 }
291
292 static Boolean sidebar_set_values(Widget old, Widget request, Widget neww, ArgList args, Cardinal *num_args) {
293     Boolean r = False;
294
295     return r;
296 }
297
298 static void sidebar_insert_child(Widget child) {
299     (xmManagerClassRec.composite_class.insert_child)(child);
300 }
301
302 Boolean sidebar_acceptfocus(Widget widget, Time *time) {
303     return 0;
304 }
305
306
307 static void FocusInAP(Widget w, XEvent *event, String *args, Cardinal *nArgs) {
308     
309 }
310
311 static void SelectElmAP(Widget w, XEvent *event, String *args, Cardinal *nArgs) {
312     //printf("btn1\n");
313     XButtonEvent *e = &event->xbutton;
314     Sidebar s = (Sidebar)w;
315     
316     int selected = e->y / s->sidebar.elmHeight;
317     PlayListPlayTrack(s->sidebar.window, selected);
318     
319     SidebarRepaint(w);
320 }
321
322 static void PopupAP(Widget w, XEvent *event, String *args, Cardinal *nArgs) {
323     //printf("btn3\n");
324 }
325
326
327 static void open_uri(Sidebar s, sstr_t uri) {
328     char *urilist = uri.ptr;
329     size_t len = uri.length;
330     
331     size_t start = 0;
332     if(len > 7 && !memcmp(urilist, "file://", 7)) {
333         start = 7;
334     }
335     
336     int err = 0;
337     
338     // urldecode
339     char *path = malloc(len + 1);
340     int k = 0;
341     for(int i=start;i<len;i++) {
342         char c = urilist[i];
343         if(c == '%') {
344             if(i + 2 < len) {
345                 char code[3];
346                 code[0] = urilist[i+1];
347                 code[1] = urilist[i+2];
348                 code[2] = '\0';
349                 
350                 errno = 0;
351                 char *end = NULL;
352                 int ascii = (int)strtol(code, &end, 16);
353                 if(errno == 0 && end == &code[2]) {
354                     path[k] = ascii;
355                     i += 2;
356                 } else {
357                     err = 1;
358                     break;
359                 }
360             } else {
361                 err = 1;
362                 break;
363             }
364         } else if(c == '\n' || c == '\r') {
365             break;
366         } else {
367             path[k] = c;
368         }
369         
370         k++;
371     }
372     path[k] = '\0';
373     
374     PlayListAddFile(s->sidebar.window, path);
375     
376     free(path);
377 }
378
379 static void sidebar_xdnd_callback(Widget w, XtPointer udata, XtPointer cdata) {
380     printf("xdnd\n");
381     fflush(stdout);
382     
383     Sidebar s = (Sidebar)cdata;
384     
385     scstr_t urlist = scstr(udata);
386     
387     ssize_t nuris;
388     sstr_t *uris = scstrsplit(urlist, scstr("\r\n"), &nuris);
389     
390     for(int i=0;i<nuris;i++) {
391         if(uris[i].length > 0) {
392             open_uri(s, uris[i]);
393         }
394     }
395     
396     SidebarRepaint((Widget)s);
397 }
398
399
400 /* --------------------------- public --------------------------- */
401
402 Widget CreateSidebar(
403         Widget parent,
404         String name,
405         ArgList arglist,
406         Cardinal argcount)
407 {
408     return XtCreateWidget(name, xnSidebarWidgetClass, parent, arglist, argcount);
409 }
410
411 void SidebarSetWindow(Widget widget, MainWindow *win) {
412     Sidebar sb = (Sidebar)widget;
413     sb->sidebar.window = win;
414 }
415
416 void SidebarRepaint(Widget widget) {
417     XClearArea(XtDisplay(widget), XtWindow(widget), 0, 0, widget->core.width, widget->core.height, true);
418 }