Created
August 15, 2018 20:17
-
-
Save darealshinji/18de159f04220a2a2cfc72967534b2c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class tray_window : public Fl_Double_Window | |
{ | |
public: | |
tray_window(int W, int H, const char *L=0) | |
: Fl_Double_Window(W, H, L) { } | |
int handle(int event) { | |
int rv = Fl_Double_Window::handle(event); | |
switch (event) { | |
case FL_PUSH: | |
//case FL_RELEASE: | |
do_callback(); | |
rv = 1; | |
break; | |
} | |
return rv; | |
} | |
}; | |
static tray_window *win; | |
static const char *command = NULL; | |
static void callback(Fl_Widget *, void *); | |
static void close_cb(Fl_Widget *, void *); | |
static Fl_Menu_Item menu[] = { | |
{ "Launch", 0, callback, 0,0, FL_NORMAL_LABEL, 0, 14, 0 }, | |
{ "Quit", 0, close_cb, 0,0, FL_NORMAL_LABEL, 0, 14, 0 }, | |
{ 0,0,0,0,0,0,0,0,0 } | |
}; | |
static void callback(Fl_Widget *, void *) { | |
win->hide(); | |
execl("/bin/sh", "sh", "-c", command, NULL); | |
_exit(127); | |
} | |
static void show_menu_cb(Fl_Widget *) { | |
menu->popup(1,1); | |
} | |
static void close_cb(Fl_Widget *, void *) { | |
win->hide(); | |
} | |
static bool create_tray_entry(const char *) | |
{ | |
Window dock; | |
XColor c; | |
XImage *image; | |
XEvent ev; | |
char atom_tray_name[128]; | |
char buf[16]; | |
snprintf(atom_tray_name, sizeof(atom_tray_name), "_NET_SYSTEM_TRAY_S%i", fl_screen); | |
dock = XGetSelectionOwner(fl_display, XInternAtom(fl_display, atom_tray_name, False)); | |
if (!dock) { | |
return false; | |
} | |
win = new tray_window(32, 32); | |
{ | |
Fl_Box *o = new Fl_Box(0, 0, 32, 32); | |
o->type(FL_NO_BOX); | |
o->image(new Fl_PNG_Image(NULL, src_default_tray_icon_png, src_default_tray_icon_png_len)); | |
} | |
win->callback(callback); | |
//win->callback(show_menu_cb); | |
win->clear_border(); | |
win->end(); | |
win->show(); | |
memset(&ev, 0, sizeof(ev)); | |
ev.xclient.type = ClientMessage; | |
ev.xclient.window = dock; | |
ev.xclient.message_type = XInternAtom(fl_display, "_NET_SYSTEM_TRAY_OPCODE", False); | |
ev.xclient.format = 32; | |
ev.xclient.data.l[0] = fl_event_time; | |
ev.xclient.data.l[1] = 0; // SYSTEM_TRAY_REQUEST_DOCK | |
ev.xclient.data.l[2] = fl_xid(win); | |
ev.xclient.data.l[3] = 0; | |
ev.xclient.data.l[4] = 0; | |
XSendEvent(fl_display, dock, False, NoEventMask, &ev); | |
XSync(fl_display, False); | |
//trap_errors(); | |
image = XGetImage(fl_display, RootWindow(fl_display, DefaultScreen(fl_display)), | |
win->x() + 1, win->y() + 1, 1, 1, AllPlanes, XYPixmap); | |
c.pixel = XGetPixel(image, 0, 0); | |
XFree(image); | |
XQueryColor(fl_display, DefaultColormap(fl_display, DefaultScreen(fl_display)), &c); | |
snprintf(buf, sizeof(buf), "0x%02x%02x%02x00", | |
static_cast<int>(c.red >> 8), | |
static_cast<int>(c.green >> 8), | |
static_cast<int>(c.blue >> 8)); | |
win->color(strtoul(buf, 0, 16)); | |
win->redraw(); | |
//if (untrap_errors()) { | |
/* Handle failure */ | |
//} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment