2 * DRM based mode setting test program
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
41 #include "xf86drmMode.h"
46 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
52 uint32_t crtc_w, crtc_h;
56 uint32_t enabled, x, y;
61 static struct cursor cursors[MAX_CURSORS];
64 static pthread_t cursor_thread;
65 static int cursor_running;
68 * Timer driven program loops through these steps to move/enable/disable
73 void (*run)(struct cursor *cursor, const struct cursor_step *step);
79 static uint32_t indx, count;
81 static void set_cursor(struct cursor *cursor, const struct cursor_step *step)
83 int enabled = (step->arg ^ count) & 0x1;
87 handle = cursor->bo_handle;
89 cursor->enabled = enabled;
91 drmModeSetCursor(cursor->fd, cursor->crtc_id, handle, cursor->w, cursor->h);
94 static void move_cursor(struct cursor *cursor, const struct cursor_step *step)
100 drmModeSetCursor(cursor->fd, cursor->crtc_id,
101 cursor->bo_handle, cursor->w, cursor->h);
103 /* calculate new cursor position: */
104 x += cursor->dx * step->arg;
105 y += cursor->dy * step->arg;
110 } else if (x > (int)cursor->crtc_w) {
111 x = cursor->crtc_w - 1;
118 } else if (y > (int)cursor->crtc_h) {
119 y = cursor->crtc_h - 1;
126 drmModeMoveCursor(cursor->fd, cursor->crtc_id, x, y);
129 static const struct cursor_step steps[] = {
130 { set_cursor, 10, 0, 1 }, /* enable */
131 { move_cursor, 1, 100, 1 },
132 { move_cursor, 1, 10, 10 },
133 { set_cursor, 1, 100, 0 }, /* disable/enable loop */
134 { move_cursor, 1, 10, 10 },
135 { move_cursor, 9, 100, 1 },
136 { move_cursor, 11, 100, 5 },
137 { set_cursor, 17, 10, 0 }, /* disable/enable loop */
138 { move_cursor, 9, 100, 1 },
139 { set_cursor, 13, 10, 0 }, /* disable/enable loop */
140 { move_cursor, 9, 100, 1 },
141 { set_cursor, 13, 10, 0 }, /* disable/enable loop */
142 { set_cursor, 10, 0, 0 }, /* disable */
145 static void *cursor_thread_func(void *data)
147 while (cursor_running) {
148 const struct cursor_step *step = &steps[indx % ARRAY_SIZE(steps)];
151 for (i = 0; i < ncursors; i++) {
152 struct cursor *cursor = &cursors[i];
153 step->run(cursor, step);
156 /* iterate to next count/step: */
157 if (count < step->repeat) {
164 usleep(1000 * step->msec);
170 int cursor_init(int fd, uint32_t bo_handle, uint32_t crtc_id,
171 uint32_t crtc_w, uint32_t crtc_h, uint32_t w, uint32_t h)
173 struct cursor *cursor = &cursors[ncursors];
175 assert(ncursors < MAX_CURSORS);
178 cursor->bo_handle = bo_handle;
179 cursor->crtc_id = crtc_id;
180 cursor->crtc_w = crtc_w;
181 cursor->crtc_h = crtc_h;
196 int cursor_start(void)
199 pthread_create(&cursor_thread, NULL, cursor_thread_func, NULL);
200 printf("starting cursor\n");
204 int cursor_stop(void)
207 pthread_join(cursor_thread, NULL);
208 printf("cursor stopped\n");