aboutsummaryrefslogtreecommitdiff
path: root/moc_x11.c
blob: efc4ce37c92add11a2c5ca91aa0a4fe7691e3c23 (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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>  //Header file for sleep(). man 3 sleep for details.
#include <errno.h>
 
#include <pthread.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>

#include "moc_library.h"

void *xmalloc (size_t size)
{
  void *p;


  if ((p = malloc(size)) == NULL) { 
    fprintf(stderr, "Can't allocate memory!");
    exit(0);
  }
  return p;
}

/* Obtains the next X11 event with specified timeout. */
  static int XNextEventTimed(Display* dsp, XEvent* event_return, long millis) {
  if (millis == 0)
  {
    XNextEvent(dsp, event_return);
    return True;
  }

  struct timeval tv;
  tv.tv_sec = millis / 1000;
  tv.tv_usec = (millis % 1000) * 1000;

  XFlush(dsp);
  if (XPending(dsp))
  {
    XNextEvent(dsp, event_return);
    return True;
  }
  else
  {
    int fd = ConnectionNumber(dsp);
    fd_set readset;
    FD_ZERO(&readset);
    FD_SET(fd, &readset);
    if (select(fd+1, &readset, NULL, NULL, &tv) <= 0)
    {
      return False;
    }
    else
    {
      if (XPending(dsp))
      {
        XNextEvent(dsp, event_return);
        return True;
      }
      else
      {
        return False;
      }
    }
  }
}

char *time_format(int sec) {

  int h = (sec/3600);
  int m = (sec -(3600*h))/60;
  int s = (sec -(3600*h)-(m*60));

  char *str = xmalloc(256);
  if(h) {
    sprintf(str, "%d:%2d:%02d",h,m,s);
  } else {
    sprintf(str, "%02d:%02d",m,s);
  }
  return str;
}

int main() {

    Display* dpy = XOpenDisplay(NULL);
    if (dpy == NULL)
    {
      fprintf(stderr, "Cannot open display\n");
      exit(1);
    }

    int s = DefaultScreen(dpy);
    Window win = XCreateSimpleWindow(dpy, RootWindow(dpy, s), 10, 10, 660, 200, 1,
        BlackPixel(dpy, s), WhitePixel(dpy, s));
    XSelectInput(dpy, win, ExposureMask | ButtonPress | KeyPressMask);
    XMapWindow(dpy, win);

    XStoreName(dpy, win, "mocicon");

    Atom WM_DELETE_WINDOW = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
    XSetWMProtocols(dpy, win, &WM_DELETE_WINDOW, 1);

    struct moc *mh = moc_init();
    if(mh == NULL) {
      return 1;
    }

    XEvent e;
    while(1) {
      int got_event = XNextEventTimed(dpy, &e, 250);

      if(mh->status != CONNECTED) {
        printf("can't connect\n");
      }

      XClearWindow(dpy, win);
        int y_offset = 20;
        int x = 10;
        pthread_mutex_lock(&mh->lock);
        if(mh->state == PLAYING || mh->state == PAUSED) {
          char *timestr= time_format(mh->time);

          XDrawString(dpy, win, DefaultGC(dpy, s), x, y_offset, mh->title, strlen(mh->title));
          y_offset += 15;
          XDrawString(dpy, win, DefaultGC(dpy, s), x, y_offset, mh->artist, strlen(mh->artist));
          y_offset += 15;
          XDrawString(dpy, win, DefaultGC(dpy, s), x, y_offset, mh->album, strlen(mh->album));
          y_offset += 15;
          XDrawString(dpy, win, DefaultGC(dpy, s), x, y_offset, timestr, strlen(timestr));
          y_offset += 15;


          free(timestr);

        }
        pthread_mutex_unlock(&mh->lock);

      }
    };