{"id":13730939,"url":"https://github.com/Tasssadar/libenjoy","last_synced_at":"2025-05-08T03:32:17.220Z","repository":{"id":4250994,"uuid":"5376722","full_name":"Tasssadar/libenjoy","owner":"Tasssadar","description":"Simple \u0026 small C joystick library for Linux and Windows. See README","archived":false,"fork":false,"pushed_at":"2013-08-31T17:38:26.000Z","size":220,"stargazers_count":42,"open_issues_count":0,"forks_count":12,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-07T07:36:41.787Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-2.1","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Tasssadar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-08-11T02:39:03.000Z","updated_at":"2025-03-18T06:19:01.000Z","dependencies_parsed_at":"2022-09-17T22:05:38.155Z","dependency_job_id":null,"html_url":"https://github.com/Tasssadar/libenjoy","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tasssadar%2Flibenjoy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tasssadar%2Flibenjoy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tasssadar%2Flibenjoy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tasssadar%2Flibenjoy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Tasssadar","download_url":"https://codeload.github.com/Tasssadar/libenjoy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252993924,"owners_count":21837330,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T02:01:21.704Z","updated_at":"2025-05-08T03:32:16.948Z","avatar_url":"https://github.com/Tasssadar.png","language":"C","funding_links":[],"categories":["C++"],"sub_categories":[],"readme":"# libenjoy\n[![Build Status](https://travis-ci.org/Tasssadar/libenjoy.png?branch=master)](https://travis-ci.org/Tasssadar/libenjoy)\n\nLighweight (I mean simple) joystick library for Linux and Windows. It is desinged\nfor use in [Lorris](https://github.com/Tasssadar/Lorris), so it is\nthe easiest to use with Qt - just  `include(libenjoy.pri)` in your .pro file.\nIt can be however used with any C/C++ app, just `#include \"libenjoy.h\"` and make\nsure that `libenjoy.c` and `libenjoy_linux.c`/`libenjoy_win32.c` are compiled.\n\nOh, yeah, and on Windows, you have to link with winmm.lib!\n\n####Highlights\n* Small. All files combined (both Linux and Windows) have about 1000 lines.\n* Almost no additional dependencies.\n  * On Linux, it is nothing other than GCC and kernel 2.2+\n  * winmm.lib on Windows - nothing special\n* Remembers joysticks. Joystick ID is unique, and libenjoy can automatically\n  reconnect re-plugged joysticks. This works flawlessly on Linux, Windows\n  on the other hand does not like it very much. Be sure to re-plug joysticks\n  to the same USB port when using multiple joysticks at once.\n* __Thread-safety note__: all functions (for one context) should be called\n  from the same thread. The only exception is `libenjoy_poll`, which is made\n  thread safe, so that you can make another thread to poll joystick events.\n\n###WARNING!\nThe fact that **libenjoy** can handle re-plugged joystick means it\n**cannot handle two or more exactly same joysticks at once**. It will pick\nthe one which was plugged-in first and ignore the other ones. This is because\nwithout libusb, I can't get really unique device id, so there is no way for me\nto identify more than one joystick of same type. But the situation \"I just\nwanna to fix the joystick's cable without restarting the app\" is more frequent\nthan \"Hey, let's buy twelve exactly same joysticks!\", at least for me - that is\nwhy I went on with this solution.\n\n### Usage:\n```C\n#include \u003cstdio.h\u003e\n#ifdef __linux\n  #include \u003cunistd.h\u003e\n#else\n  #include \u003cwindows.h\u003e\n#endif\n\n#include \"libenjoy.h\"\n\n// This tels msvc to link agains winmm.lib. Pretty nasty though.\n#pragma comment(lib, \"winmm.lib\")\n\nint main()\n{\n    libenjoy_context *ctx = libenjoy_init(); // initialize the library\n    libenjoy_joy_info_list *info;\n\n    // Updates internal list of joysticks. If you want auto-reconnect\n    // after re-plugging the joystick, you should call this every 1s or so\n    libenjoy_enumerate(ctx);\n\n    // get list with available joysticks. structs are\n    // typedef struct libenjoy_joy_info_list {\n    //     uint32_t count;\n    //     libenjoy_joy_info **list;\n    // } libenjoy_joy_info_list;\n    //\n    // typedef struct libenjoy_joy_info {\n    //     char *name;\n    //     uint32_t id;\n    //     char opened;\n    // } libenjoy_joy_info;\n    //\n    // id is not linear (eg. you should not use vector or array), \n    // and if you disconnect joystick and then plug it in again,\n    // it should have the same ID\n    info = libenjoy_get_info_list(ctx);\n\n    if(info-\u003ecount != 0) // just get the first joystick\n    {\n        libenjoy_joystick *joy;\n        printf(\"Opening joystick %s...\", info-\u003elist[0]-\u003ename);\n        joy = libenjoy_open_joystick(ctx, info-\u003elist[0]-\u003eid);\n        if(joy)\n        {\n            int counter = 0;\n            libenjoy_event ev;\n\n            printf(\"Success!\\n\");\n            printf(\"Axes: %d btns: %d\\n\", libenjoy_get_axes_num(joy),libenjoy_get_buttons_num(joy));\n\n            while(1)\n            {\n                // Value data are not stored in library. if you want to use\n                // them, you have to store them\n\n                // That's right, only polling available\n                while(libenjoy_poll(ctx, \u0026ev))\n                {\n                    switch(ev.type)\n                    {\n                    case LIBENJOY_EV_AXIS:\n                        printf(\"%u: axis %d val %d\\n\", ev.joy_id, ev.part_id, ev.data);\n                        break;\n                    case LIBENJOY_EV_BUTTON:\n                        printf(\"%u: button %d val %d\\n\", ev.joy_id, ev.part_id, ev.data);\n                        break;\n                    case LIBENJOY_EV_CONNECTED:\n                        printf(\"%u: status changed: %d\\n\", ev.joy_id, ev.data);\n                        break;\n                    }\n                }\n#ifdef __linux\n                usleep(50000);\n#else\n                Sleep(50);\n#endif\n                counter += 50;\n                if(counter \u003e= 1000)\n                {\n                    libenjoy_enumerate(ctx);\n                    counter = 0;\n                }\n            }\n\n            // Joystick is really closed in libenjoy_poll or libenjoy_close,\n            // because closing it while libenjoy_poll is in process in another thread\n            // could cause crash. Be sure to call libenjoy_poll(ctx, NULL); (yes,\n            // you can use NULL as event) if you will not poll nor libenjoy_close\n            // anytime soon.\n            libenjoy_close_joystick(joy);\n        }\n        else\n            printf(\"Failed!\\n\");\n    }\n    else\n        printf(\"No joystick available\\n\");\n\n    // Frees memory allocated by that joystick list. Do not forget it!\n    libenjoy_free_info_list(info);\n\n    // deallocates all memory used by lib. Do not forget this!\n    // libenjoy_poll must not be called during or after this call\n    libenjoy_close(ctx); \n    return 0;\n}\n```\n\n### License\nLGPLv2.1, see COPYING. Most of libenjoy_win32.c is from libSDL - thanks!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTasssadar%2Flibenjoy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTasssadar%2Flibenjoy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTasssadar%2Flibenjoy/lists"}