summaryrefslogtreecommitdiff
path: root/apps/plugins/properties.c
blob: 7cd29c126f0ae66bf20ed596f7e95ea95cacbad7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 Peter D'Hoye
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"

PLUGIN_HEADER

bool its_a_dir = false;

char str_filename[MAX_PATH];
char str_dirname[MAX_PATH];
char str_size[64];
char str_dircount[64];
char str_filecount[64];
char str_date[64];
char str_time[64];

char str_title[MAX_PATH];
char str_artist[MAX_PATH];
char str_album[MAX_PATH];
char str_duration[32];

int num_properties;

static char* filesize2string(long long size, char* pstr, int len)
{
    /* margin set at 10K boundary: 10239 B +1 => 10 KB
       routine below is 200 bytes smaller than cascaded if/else :)
       not using build-in function because of huge values (long long) */
    const char* kgb[4] = { "B", "KB", "MB", "GB" };
    int i = 0;
    while(true)
    {
        if((size < 10240) || (i > 2))
        {
            /* depends on the order in the above array */
            rb->snprintf(pstr, len, "%ld %s", (long)size, kgb[i]);
            break;
        }
        size >>= 10; /* div by 1024 */
        i++;
    }
    return pstr;
}

static bool file_properties(char* selected_file)
{
    bool found = false;
    char tstr[MAX_PATH];
    DIR* dir;
    struct dirent* entry;
    struct mp3entry id3;

    char* ptr = rb->strrchr(selected_file, '/') + 1;
    int dirlen = (ptr - selected_file);
    rb->strlcpy(tstr, selected_file, dirlen + 1);

    dir = rb->opendir(tstr);
    if (dir)
    {
        while(0 != (entry = rb->readdir(dir)))
        {
            if(!rb->strcmp(entry->d_name, selected_file+dirlen))
            {
                rb->snprintf(str_dirname, sizeof str_dirname, "Path: %s",
                             tstr);
                rb->snprintf(str_filename, sizeof str_filename, "Name: %s",
                             selected_file+dirlen);
                rb->snprintf(str_size, sizeof str_size, "Size: %s",
                             filesize2string(entry->size, tstr, sizeof tstr));
                rb->snprintf(str_date, sizeof str_date, "Date: %04d/%02d/%02d",
                    ((entry->wrtdate >> 9 ) & 0x7F) + 1980, /* year    */
                    ((entry->wrtdate >> 5 ) & 0x0F),        /* month   */
                    ((entry->wrtdate      ) & 0x1F));       /* day     */
                rb->snprintf(str_time, sizeof str_time, "Time: %02d:%02d",
                    ((entry->wrttime >> 11) & 0x1F),        /* hour    */
                    ((entry->wrttime >> 5 ) & 0x3F));       /* minutes */

                num_properties = 5;

#if (CONFIG_CODEC == SWCODEC)
                int fd = rb->open(selected_file, O_RDONLY);
                if (fd >= 0 &&
                    rb->get_metadata(&id3, fd, selected_file))
#else
                if (!rb->mp3info(&id3, selected_file))
#endif
                {
                    long dur = id3.length / 1000;           /* seconds */
                    rb->snprintf(str_artist, sizeof str_artist,
                                 "Artist: %s", id3.artist ? id3.artist : "");
                    rb->snprintf(str_title, sizeof str_title,
                                 "Title: %s", id3.title ? id3.title : "");
                    rb->snprintf(str_album, sizeof str_album,
                                 "Album: %s", id3.album ? id3.album : "");
                    num_properties += 3;

                    if (dur > 0)
                    {
                        if (dur < 3600)
                            rb->snprintf(str_duration, sizeof str_duration,
                                         "Duration: %d:%02d", (int)(dur / 60),
                                         (int)(dur % 60));
                        else
                            rb->snprintf(str_duration, sizeof str_duration,
                                         "Duration: %d:%02d:%02d",
                                         (int)(dur / 3600), 
                                         (int)(dur % 3600 / 60),
                                         (int)(dur % 60));
                        num_properties++;
                    }
                }
#if (CONFIG_CODEC == SWCODEC)
                rb->close(fd);
#endif
                found = true;
                break;
            }
        }
        rb->closedir(dir);
    }
    return found;
}

typedef struct {
    char dirname[MAX_PATH];
    int len;
    unsigned int dc;
    unsigned int fc;
    long long bc;
    char tstr[64];
    char tstr2[64];
} DPS;

static bool _dir_properties(DPS* dps)
{
    /* recursively scan directories in search of files
       and informs the user of the progress */
    bool result;
    int dirlen;
    DIR* dir;
    struct dirent* entry;

    result = true;
    dirlen = rb->strlen(dps->dirname);
    dir = rb->opendir(dps->dirname);
    if (!dir)
        return false; /* open error */

    /* walk through the directory content */
    while(result && (0 != (entry = rb->readdir(dir))))
    {
        /* append name to current directory */
        rb->snprintf(dps->dirname+dirlen, dps->len-dirlen, "/%s",
                     entry->d_name);

        if (entry->attribute & ATTR_DIRECTORY)
        {   
            if (!rb->strcmp((char *)entry->d_name, ".") ||
                !rb->strcmp((char *)entry->d_name, ".."))
                continue; /* skip these */

            dps->dc++; /* new directory */
            rb->lcd_clear_display();
            rb->lcd_puts(0,0,"SCANNING...");
            rb->lcd_puts(0,1,dps->dirname);
            rb->lcd_puts(0,2,entry->d_name);
            rb->snprintf(dps->tstr, 64, "Directories: %d", dps->dc);
            rb->lcd_puts(0,3,dps->tstr);
            rb->snprintf(dps->tstr, 64, "Files: %d", dps->fc);
            rb->lcd_puts(0,4,dps->tstr);
            rb->snprintf(dps->tstr, 64, "Size: %s",
                filesize2string(dps->bc, dps->tstr2, 64));
            rb->lcd_puts(0,5,dps->tstr);
            rb->lcd_update();

             /* recursion */
            result = _dir_properties(dps);
        }
        else
        {   
            dps->fc++; /* new file */
            dps->bc += entry->size;
        }
        if(ACTION_STD_CANCEL == rb->get_action(CONTEXT_STD,TIMEOUT_NOBLOCK))
            result = false;
        rb->yield();
    }
    rb->closedir(dir);
    return result;
}

static bool dir_properties(char* selected_file)
{
    DPS dps;
    char tstr[64];
    rb->strlcpy(dps.dirname, selected_file, MAX_PATH);
    dps.len = MAX_PATH;
    dps.dc = 0;
    dps.fc = 0;
    dps.bc = 0;
    if(false == _dir_properties(&dps))
        return false;

    rb->strlcpy(str_dirname, selected_file, MAX_PATH);
    rb->snprintf(str_dircount, sizeof str_dircount, "Subdirs: %d", dps.dc);
    rb->snprintf(str_filecount, sizeof str_filecount, "Files: %d", dps.fc);
    rb->snprintf(str_size, sizeof str_size, "Size: %s",
                 filesize2string(dps.bc, tstr, sizeof tstr));
    num_properties = 4;
    return true;
}

static const char * get_props(int selected_item, void* data,
                              char *buffer, size_t buffer_len)
{
    (void)data;

    switch(selected_item)
    {
        case 0:
            rb->strlcpy(buffer, str_dirname, buffer_len);
            break;
        case 1:
            rb->strlcpy(buffer, its_a_dir ? str_dircount : str_filename,
                        buffer_len);
            break;
        case 2:
            rb->strlcpy(buffer, its_a_dir ? str_filecount : str_size, buffer_len);
            break;
        case 3:
            rb->strlcpy(buffer, its_a_dir ? str_size : str_date, buffer_len);
            break;
        case 4:
            rb->strlcpy(buffer, its_a_dir ? "" : str_time, buffer_len);
            break;
        case 5:
            rb->strlcpy(buffer, its_a_dir ? "" : str_artist, buffer_len);
            break;
        case 6:
            rb->strlcpy(buffer, its_a_dir ? "" : str_title, buffer_len);
            break;
        case 7:
            rb->strlcpy(buffer, its_a_dir ? "" : str_album, buffer_len);
            break;
        case 8:
            rb->strlcpy(buffer, its_a_dir ? "" : str_duration, buffer_len);
            break;
        default:
            return "ERROR";
    }
    return buffer;
}

enum plugin_status plugin_start(const void* parameter)
{
    struct gui_synclist properties_lists;
    int button;
    bool quit = false;
    char file[MAX_PATH];
    if(!parameter) return PLUGIN_ERROR;
    rb->strcpy(file, (const char *) parameter);

    /* determine if it's a file or a directory */
    bool found = false;
    DIR* dir;
    struct dirent* entry;
    char* ptr = rb->strrchr(file, '/') + 1;
    int dirlen = (ptr - file);
    rb->strlcpy(str_dirname, file, dirlen + 1);

    dir = rb->opendir(str_dirname);
    if (dir)
    {
        while(0 != (entry = rb->readdir(dir)))
        {
            if(!rb->strcmp(entry->d_name, file+dirlen))
            {
                its_a_dir = entry->attribute & ATTR_DIRECTORY ? true : false;
                found = true;
                break;
            }
        }
        rb->closedir(dir);
    }
    /* now we know if it's a file or a dir or maybe something failed */
    
    if(!found)
    {
        /* weird: we couldn't find the entry. This Should Never Happen (TM) */
        rb->splashf(0, "File/Dir not found: %s", file);
        rb->action_userabort(TIMEOUT_BLOCK);
        return PLUGIN_OK;
    }

    /* get the info depending on its_a_dir */
    if(!(its_a_dir ? dir_properties(file) : file_properties(file)))
    {
        /* something went wrong (to do: tell user what it was (nesting,...) */
        rb->splash(0, "Failed to gather information");
        rb->action_userabort(TIMEOUT_BLOCK);
        return PLUGIN_OK;
    }

    rb->gui_synclist_init(&properties_lists, &get_props, file, false, 1, NULL);
    rb->gui_synclist_set_title(&properties_lists, its_a_dir ?
                                                  "Directory properties" :
                                                  "File properties", NOICON);
    rb->gui_synclist_set_icon_callback(&properties_lists, NULL);
    rb->gui_synclist_set_nb_items(&properties_lists, num_properties);
    rb->gui_synclist_limit_scroll(&properties_lists, true);
    rb->gui_synclist_select_item(&properties_lists, 0);
    rb->gui_synclist_draw(&properties_lists);

    while(!quit)
    {
        button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
        if (rb->gui_synclist_do_button(&properties_lists,&button,LIST_WRAP_ON))
            continue;
        switch(button)
        {
            case ACTION_STD_CANCEL:
                quit = true;
                break;
            default:
                if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
                    return PLUGIN_USB_CONNECTED;
        }
    }

    return PLUGIN_OK;
}
#define BUF_LEN_MOUSE 4 #define MOUSE_ACCEL_FACTOR(count) ((count) / 2) #define MOUSE_DO(item, step) (buf[(item)] = ((uint8_t)(step))) static uint8_t buf_set_mouse(unsigned char *buf, int id) { static int count = 0; int step = MOUSE_STEP; memset(buf, 0, BUF_LEN_MOUSE); /* Set proper button */ switch (id) { case HID_MOUSE_BUTTON_LEFT: case HID_MOUSE_LDRAG_UP: case HID_MOUSE_LDRAG_UP_REP: case HID_MOUSE_LDRAG_DOWN: case HID_MOUSE_LDRAG_DOWN_REP: case HID_MOUSE_LDRAG_LEFT: case HID_MOUSE_LDRAG_LEFT_REP: case HID_MOUSE_LDRAG_RIGHT: case HID_MOUSE_LDRAG_RIGHT_REP: MOUSE_DO(MOUSE_BUTTON, MOUSE_BUTTON_LEFT); break; case HID_MOUSE_BUTTON_RIGHT: case HID_MOUSE_RDRAG_UP: case HID_MOUSE_RDRAG_UP_REP: case HID_MOUSE_RDRAG_DOWN: case HID_MOUSE_RDRAG_DOWN_REP: case HID_MOUSE_RDRAG_LEFT: case HID_MOUSE_RDRAG_LEFT_REP: case HID_MOUSE_RDRAG_RIGHT: case HID_MOUSE_RDRAG_RIGHT_REP: MOUSE_DO(MOUSE_BUTTON, MOUSE_BUTTON_RIGHT); break; case HID_MOUSE_BUTTON_LEFT_REL: case HID_MOUSE_BUTTON_RIGHT_REL: /* Keep buf empty, to mark mouse button release */ break; default: break; } /* Handle mouse accelarated movement */ switch (id) { case HID_MOUSE_UP: case HID_MOUSE_DOWN: case HID_MOUSE_LEFT: case HID_MOUSE_RIGHT: case HID_MOUSE_LDRAG_UP: case HID_MOUSE_LDRAG_DOWN: case HID_MOUSE_LDRAG_LEFT: case HID_MOUSE_LDRAG_RIGHT: case HID_MOUSE_RDRAG_UP: case HID_MOUSE_RDRAG_DOWN: case HID_MOUSE_RDRAG_LEFT: case HID_MOUSE_RDRAG_RIGHT: count = 0; break; case HID_MOUSE_UP_REP: case HID_MOUSE_DOWN_REP: case HID_MOUSE_LEFT_REP: case HID_MOUSE_RIGHT_REP: case HID_MOUSE_LDRAG_UP_REP: case HID_MOUSE_LDRAG_DOWN_REP: case HID_MOUSE_LDRAG_LEFT_REP: case HID_MOUSE_LDRAG_RIGHT_REP: case HID_MOUSE_RDRAG_UP_REP: case HID_MOUSE_RDRAG_DOWN_REP: case HID_MOUSE_RDRAG_LEFT_REP: case HID_MOUSE_RDRAG_RIGHT_REP: count++; /* TODO: Find a better mouse accellaration algorithm */ step *= 1 + MOUSE_ACCEL_FACTOR(count); if (step > 255) step = 255; break; default: break; } /* Move/scroll mouse */ switch (id) { case HID_MOUSE_UP: case HID_MOUSE_UP_REP: case HID_MOUSE_LDRAG_UP: case HID_MOUSE_LDRAG_UP_REP: case HID_MOUSE_RDRAG_UP: case HID_MOUSE_RDRAG_UP_REP: MOUSE_DO(MOUSE_Y, -step); break; case HID_MOUSE_DOWN: case HID_MOUSE_DOWN_REP: case HID_MOUSE_LDRAG_DOWN: case HID_MOUSE_LDRAG_DOWN_REP: case HID_MOUSE_RDRAG_DOWN: case HID_MOUSE_RDRAG_DOWN_REP: MOUSE_DO(MOUSE_Y, step); break; case HID_MOUSE_LEFT: case HID_MOUSE_LEFT_REP: case HID_MOUSE_LDRAG_LEFT: case HID_MOUSE_LDRAG_LEFT_REP: case HID_MOUSE_RDRAG_LEFT: case HID_MOUSE_RDRAG_LEFT_REP: MOUSE_DO(MOUSE_X, -step); break; case HID_MOUSE_RIGHT: case HID_MOUSE_RIGHT_REP: case HID_MOUSE_LDRAG_RIGHT: case HID_MOUSE_LDRAG_RIGHT_REP: case HID_MOUSE_RDRAG_RIGHT: case HID_MOUSE_RDRAG_RIGHT_REP: MOUSE_DO(MOUSE_X, step); break; case HID_MOUSE_SCROLL_UP: MOUSE_DO(MOUSE_WHEEL, MOUSE_WHEEL_STEP); break; case HID_MOUSE_SCROLL_DOWN: MOUSE_DO(MOUSE_WHEEL, -MOUSE_WHEEL_STEP); break; default: break; } return BUF_LEN_MOUSE; } #endif /* HAVE_USB_HID_MOUSE */ static size_t descriptor_report_get(unsigned char *dest) { unsigned char *report = dest; usb_hid_report_t *usb_hid_report; /* Keyboard control */ usb_hid_report = &usb_hid_reports[REPORT_ID_KEYBOARD]; usb_hid_report->usage_page = HID_USAGE_PAGE_KEYBOARD_KEYPAD; usb_hid_report->buf_set = buf_set_keyboard; usb_hid_report->is_key_released = 1; pack_parameter(&report, 0, 1, USAGE_PAGE, HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS); pack_parameter(&report, 0, 0, CONSUMER_USAGE, HID_GENERIC_DESKTOP_KEYBOARD); pack_parameter(&report, 0, 1, COLLECTION, COLLECTION_APPLICATION); pack_parameter(&report, 0, 1, REPORT_ID, REPORT_ID_KEYBOARD); pack_parameter(&report, 0, 1, USAGE_PAGE, HID_GENERIC_DESKTOP_KEYPAD); pack_parameter(&report, 0, 1, USAGE_MINIMUM, HID_KEYBOARD_LEFT_CONTROL); pack_parameter(&report, 0, 1, USAGE_MAXIMUM, HID_KEYBOARD_RIGHT_GUI); pack_parameter(&report, 1, 1, LOGICAL_MINIMUM, 0); pack_parameter(&report, 1, 1, LOGICAL_MAXIMUM, 1); pack_parameter(&report, 0, 1, REPORT_SIZE, 1); pack_parameter(&report, 0, 1, REPORT_COUNT, 8); pack_parameter(&report, 0, 1, INPUT, MAIN_ITEM_VARIABLE); pack_parameter(&report, 0, 1, REPORT_SIZE, 1); pack_parameter(&report, 0, 1, REPORT_COUNT, 5); pack_parameter(&report, 0, 1, USAGE_PAGE, HID_USAGE_PAGE_LEDS); pack_parameter(&report, 0, 1, USAGE_MINIMUM, HID_LED_NUM_LOCK); pack_parameter(&report, 0, 1, USAGE_MAXIMUM, HID_LED_KANA); pack_parameter(&report, 0, 1, OUTPUT, MAIN_ITEM_VARIABLE); pack_parameter(&report, 0, 1, REPORT_SIZE, 3); pack_parameter(&report, 0, 1, REPORT_COUNT, 1); pack_parameter(&report, 0, 1, OUTPUT, MAIN_ITEM_CONSTANT); pack_parameter(&report, 0, 1, REPORT_SIZE, 8); pack_parameter(&report, 0, 1, REPORT_COUNT, 6); pack_parameter(&report, 1, 1, LOGICAL_MINIMUM, 0); pack_parameter(&report, 1, 1, LOGICAL_MAXIMUM, HID_KEYBOARD_EX_SEL); pack_parameter(&report, 0, 1, USAGE_PAGE, HID_USAGE_PAGE_KEYBOARD_KEYPAD); pack_parameter(&report, 0, 1, USAGE_MINIMUM, 0); pack_parameter(&report, 0, 1, USAGE_MAXIMUM, HID_KEYBOARD_EX_SEL); pack_parameter(&report, 0, 1, INPUT, 0); PACK_VAL(report, END_COLLECTION); /* Consumer usage controls - play/pause, stop, etc. */ usb_hid_report = &usb_hid_reports[REPORT_ID_CONSUMER]; usb_hid_report->usage_page = HID_USAGE_PAGE_CONSUMER; usb_hid_report->buf_set = buf_set_consumer; usb_hid_report->is_key_released = 1; pack_parameter(&report, 0, 1, USAGE_PAGE, HID_USAGE_PAGE_CONSUMER); pack_parameter(&report, 0, 0, CONSUMER_USAGE, HID_CONSUMER_USAGE_CONSUMER_CONTROL); pack_parameter(&report, 0, 1, COLLECTION, COLLECTION_APPLICATION); pack_parameter(&report, 0, 1, REPORT_ID, REPORT_ID_CONSUMER); pack_parameter(&report, 0, 1, REPORT_SIZE, 16); pack_parameter(&report, 0, 1, REPORT_COUNT, 2); pack_parameter(&report, 1, 1, LOGICAL_MINIMUM, 1); pack_parameter(&report, 1, 1, LOGICAL_MAXIMUM, 652); pack_parameter(&report, 0, 1, USAGE_MINIMUM, HID_CONSUMER_USAGE_CONSUMER_CONTROL); pack_parameter(&report, 0, 1, USAGE_MAXIMUM, HID_CONSUMER_USAGE_AC_SEND); pack_parameter(&report, 0, 1, INPUT, MAIN_ITEM_NO_PREFERRED | MAIN_ITEM_NULL_STATE); PACK_VAL(report, END_COLLECTION); #ifdef HAVE_USB_HID_MOUSE /* Mouse control */ usb_hid_report = &usb_hid_reports[REPORT_ID_MOUSE]; usb_hid_report->usage_page = HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS; usb_hid_report->buf_set = buf_set_mouse; usb_hid_report->is_key_released = 0; pack_parameter(&report, 0, 1, USAGE_PAGE, HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS); pack_parameter(&report, 0, 0, CONSUMER_USAGE, HID_GENERIC_DESKTOP_MOUSE); pack_parameter(&report, 0, 1, COLLECTION, COLLECTION_APPLICATION); pack_parameter(&report, 0, 1, REPORT_ID, REPORT_ID_MOUSE); pack_parameter(&report, 0, 0, CONSUMER_USAGE, HID_GENERIC_DESKTOP_POINTER); pack_parameter(&report, 0, 1, COLLECTION, COLLECTION_PHYSICAL); pack_parameter(&report, 0, 1, USAGE_PAGE, HID_USAGE_PAGE_BUTTON); pack_parameter(&report, 0, 1, USAGE_MINIMUM, 1); pack_parameter(&report, 0, 1, USAGE_MAXIMUM, 8); pack_parameter(&report, 1, 1, LOGICAL_MINIMUM, 0); pack_parameter(&report, 1, 1, LOGICAL_MAXIMUM, 1); pack_parameter(&report, 0, 1, REPORT_SIZE, 1); pack_parameter(&report, 0, 1, REPORT_COUNT, 8); pack_parameter(&report, 0, 1, INPUT, MAIN_ITEM_VARIABLE); pack_parameter(&report, 0, 1, USAGE_PAGE, HID_USAGE_PAGE_GENERIC_DESKTOP_CONTROLS); pack_parameter(&report, 0, 0, CONSUMER_USAGE, HID_GENERIC_DESKTOP_X); pack_parameter(&report, 0, 0, CONSUMER_USAGE, HID_GENERIC_DESKTOP_Y); pack_parameter(&report, 0, 0, CONSUMER_USAGE, HID_GENERIC_DESKTOP_WHEEL); pack_parameter(&report, 0, 1, LOGICAL_MINIMUM, -127 & 0xFF); pack_parameter(&report, 0, 1, LOGICAL_MAXIMUM, 127); pack_parameter(&report, 0, 1, REPORT_SIZE, 8); pack_parameter(&report, 0, 1, REPORT_COUNT, 3); pack_parameter(&report, 0, 1, INPUT, MAIN_ITEM_VARIABLE | MAIN_ITEM_RELATIVE); PACK_VAL(report, END_COLLECTION); PACK_VAL(report, END_COLLECTION); #endif /* HAVE_USB_HID_MOUSE */ return (size_t)(report - dest); } static void descriptor_hid_get(unsigned char **dest) { hid_descriptor.wDescriptorLength0= descriptor_report_get(report_descriptor); logf("hid: desc len %u", hid_descriptor.wDescriptorLength0); buf_dump(report_descriptor, hid_descriptor.wDescriptorLength0, "desc"); PACK_DATA(dest, hid_descriptor); } int usb_hid_get_config_descriptor(unsigned char *dest, int max_packet_size) { (void)max_packet_size; unsigned char *orig_dest = dest; logf("hid: config desc."); /* Interface descriptor */ interface_descriptor.bInterfaceNumber = usb_interface; PACK_DATA(&dest, interface_descriptor); /* HID descriptor */ descriptor_hid_get(&dest); /* Endpoint descriptor */ endpoint_descriptor.wMaxPacketSize = 8; endpoint_descriptor.bInterval = 8; endpoint_descriptor.bEndpointAddress = ep_in; PACK_DATA(&dest, endpoint_descriptor); return (int)(dest - orig_dest); } void usb_hid_init_connection(void) { logf("hid: init connection"); active = true; currently_sending = false; } /* called by usb_core_init() */ void usb_hid_init(void) { logf("hid: init"); for (int i = 0; i < HID_NUM_BUFFERS; i++) send_buffer_len[i] = 0; cur_buf_prepare = 0; cur_buf_send = 0; active = true; currently_sending = false; } void usb_hid_disconnect(void) { logf("hid: disconnect"); active = false; currently_sending = false; } /* called by usb_core_transfer_complete() */ void usb_hid_transfer_complete(int ep, int dir, int status, int length) { (void)ep; (void)length; logf("HID: transfer complete: %d %d %d %d",ep,dir,status,length); if (dir == USB_DIR_IN && !status) { send_buffer_len[cur_buf_send] = 0; HID_BUF_INC(cur_buf_send); currently_sending = false; usb_hid_try_send_drv(); } } /* The DAP is registered as a keyboard with several LEDs, therefore the OS sends * LED report to notify the DAP whether Num Lock / Caps Lock etc. are enabled. * In order to allow sending info to the DAP, the Set Report mechanism can be * used by defining vendor specific output reports and send them from the host * to the DAP using the host's custom driver */ static int usb_hid_set_report(struct usb_ctrlrequest *req) { static unsigned char buf[SET_REPORT_BUF_LEN] USB_DEVBSS_ATTR __attribute__((aligned(32))); int length; if ((req->wValue >> 8) != REPORT_TYPE_OUTPUT) { logf("Unsopported report type"); return 1; } if ((req->wValue & 0xff) != REPORT_ID_KEYBOARD) { logf("Wrong report id"); return 2; } if (req->wIndex != (uint16_t)usb_interface) { logf("Wrong interface"); return 3; } length = req->wLength; if (length != SET_REPORT_BUF_LEN) { logf("Wrong length"); return 4; } memset(buf, 0, length); usb_drv_recv(EP_CONTROL, buf, length); #ifdef LOGF_ENABLE if (buf[1] & 0x01) logf("Num Lock enabled"); if (buf[1] & 0x02) logf("Caps Lock enabled"); if (buf[1] & 0x04) logf("Scroll Lock enabled"); if (buf[1] & 0x08) logf("Compose enabled"); if (buf[1] & 0x10) logf("Kana enabled"); #endif /* Defining other LEDs and setting them from the USB host (OS) can be used * to send messages to the DAP */ return 0; } /* called by usb_core_control_request() */ bool usb_hid_control_request(struct usb_ctrlrequest *req, unsigned char *dest) { switch (req->bRequestType & USB_TYPE_MASK) { case USB_TYPE_STANDARD: { unsigned char *orig_dest = dest; uint8_t type = req->wValue >> 8; size_t len; logf("hid: type %d %s", type, (type == USB_DT_HID) ? "hid" : ((type == USB_DT_REPORT) ? "report" : "")); switch (type) { case USB_DT_HID: descriptor_hid_get(&dest); break; case USB_DT_REPORT: len = descriptor_report_get(report_descriptor); memcpy(dest, &report_descriptor, len); dest += len; break; } if (dest != orig_dest) { usb_drv_recv(EP_CONTROL, NULL, 0); /* ack */ usb_drv_send(EP_CONTROL, orig_dest, dest - orig_dest); return true; } break; } case USB_TYPE_CLASS: { logf("req %d %s", req->bRequest, (req->bRequest == USB_HID_SET_IDLE) ? "set idle" : ((req->bRequest == USB_HID_SET_REPORT) ? "set report" : "")); switch (req->bRequest) { case USB_HID_SET_REPORT: if (usb_hid_set_report(req)) break; case USB_HID_SET_IDLE: usb_drv_send(EP_CONTROL, NULL, 0); /* ack */ return true; } break; } case USB_TYPE_VENDOR: logf("hid: unsup. ven. req"); break; } return false; } static void usb_hid_try_send_drv(void) { int rc; int length = send_buffer_len[cur_buf_send]; if (!length) return; if (currently_sending) { logf("HID: Already sending"); return; } logf("HID: Sending %d bytes",length); rc = usb_drv_send_nonblocking(ep_in, send_buffer[cur_buf_send], length); currently_sending = true; if (rc) send_buffer_len[cur_buf_send] = 0; } static void usb_hid_queue(unsigned char *data, int length) { if (!active || length <= 0) return; /* Buffer overflow - item still in use */ if (send_buffer_len[cur_buf_prepare]) return; /* Prepare buffer for sending */ if (length > HID_BUF_SIZE_MSG) length = HID_BUF_SIZE_MSG; memcpy(send_buffer[cur_buf_prepare], data, length); send_buffer_len[cur_buf_prepare] = length; HID_BUF_INC(cur_buf_prepare); } void usb_hid_send(usage_page_t usage_page, int id) { uint8_t length; static unsigned char buf[HID_BUF_SIZE_CMD] USB_DEVBSS_ATTR __attribute__((aligned(32))); usb_hid_report_t *report = NULL; for (uint8_t report_id = 1; report_id < REPORT_ID_COUNT; report_id++) if (usb_hid_reports[report_id].usage_page == usage_page) { report = &usb_hid_reports[report_id]; buf[0] = report_id; break; } if (!report) { logf("Unsupported usage_page"); return; } logf("Sending cmd 0x%x", id); length = report->buf_set(&buf[1], id) + 1; logf("length %u", length); if (!length) return; /* Key pressed */ buf_dump(buf, length, "key press"); usb_hid_queue(buf, length); if (report->is_key_released) { /* Key released */ memset(&buf[1], 0, length-1); buf_dump(buf, length, "key release"); usb_hid_queue(buf, length); } usb_hid_try_send_drv(); }